-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis_IR.cpp
528 lines (479 loc) · 16 KB
/
analysis_IR.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
//Copyright 2019 Hust.
//Author: HYL
//Descriptor:
/*
*/
#include <iostream>
#include <fstream>
#include <regex>
#include <map>
#include <vector>
#include <sstream>
#include <string>
#include "trance_fun.h"
#include "debug_info.h"
#include "memory_manage_data.h"
#include "memory_manage_reg.h"
using namespace::std;
class DebugInfo;
//This fun get ram and rom info from cfg file
void GetRamAndRomInfo(string ram_info_file_name, \
vector<string>& ram_range_vec, \
vector<string>& rom_range_vec,\
string &core_name){
//Open file and judge whether it is open
ifstream IR_file(ram_info_file_name, ios_base::in);
if(!IR_file.is_open()){
throw "Error: GetRamAndRomInfo() Open file failed!";
}
//find core name
//../../../sdic3004.inc
/*string core_name_temp = ram_info_file_name;
auto char_index = ram_info_file_name.rfind('/');
auto comma_index = ram_info_file_name.rfind('.');
int len = comma_index - char_index;
core_name = core_name_temp.substr(char_index, len);
*/
//get ram info
string single_line;
bool whether_have_find_target_core = false;
bool whether_have_finished = false;
//[SD3101]
regex core_name_regex(".+" + core_name + ".+");
smatch r1;
//RAMRange=0x001-0x04C
regex ram_range_regex("RAMRange.+");
//ROMRange=0x0000-0x3FFF
regex rom_range_regex("ROMRange.+");
//[SD3101]
regex bracket_regex(".+SD.+");//.+]");
//get every line of this file
while(getline(IR_file, single_line)){
string single_word;
istringstream iss(single_line);
//get every word of this line
while(iss >> single_word) {
//find the target core
if(regex_match(single_word, r1, bracket_regex)){
if(whether_have_find_target_core){
whether_have_finished = true;
break;
}
else if(regex_match(single_word, core_name_regex)){
whether_have_find_target_core = true;
continue;
}
}
if(whether_have_find_target_core == false) continue;
//it is processor item
if(!single_word.compare("Processor")){
}
//it is range of ram
if(regex_match(single_word, ram_range_regex)){
auto equal_index = single_word.find("=");
string ram_range = single_word.substr(equal_index + 1);
ram_range_vec.push_back(ram_range);
}
//it is range of rom
if(regex_match(single_word, rom_range_regex)){
auto equal_index = single_word.find("=");
string rom_range = single_word.substr(equal_index + 1);
rom_range_vec.push_back(rom_range);
}
}
if(whether_have_finished) break;
}
//if there are not core name in cfg file
if(!(whether_have_finished && whether_have_find_target_core)){
throw "Error: There aren't " + core_name + " in " + \
ram_info_file_name;
}
}
void DealWithRamAndRomInfo(vector<string>& ram_range_vec, \
vector<string>& rom_range_vec,\
string& core_name){
RegManage* reg_manage_obj = RegManage::getInstance();
reg_manage_obj->InitialRamRange(ram_range_vec, core_name);
DataAreaManage* data_area_manage_obj = DataAreaManage::getInstance();
data_area_manage_obj->InitialRomRange(rom_range_vec, core_name);
}
//deal with special reg.
//transform .h file to .inc file and record the name and addr in map
//special_reg_map is the map of special reg name and addr
//path_name is the file path
//core_name is the type of singlechip
void DealWithSpecialRegInfo(map<string, string>& special_reg_map, \
string& path_name, \
string& core_name){
//Open file and judge whether it is open
ifstream IR_file(path_name, ios_base::in);
if(!IR_file.is_open()){
throw "Error: DealWithSpecialRegInfo Open special reg " \
"file failed!";
}
auto index = path_name.rfind(".");
string output_file = path_name.substr(0, index) + ".inc";
vector<string> output_vec;
string single_line;
while(getline(IR_file, single_line)){
auto is_define = single_line.find("#define");
if(is_define == string::npos) continue;
string output_single_line;
string single_word, map_index, map_string;
istringstream iss(single_line);
int index = 0;
while(iss >> single_word){
if(index == 1) {
output_single_line = output_single_line + single_word;
map_index = single_word;
}
if(index == 4){
auto bracket_index = single_word.find(")");
string temp = single_word.substr(bracket_index + 1);
map_string = temp.substr(0, temp.size() - 1);
output_single_line = \
output_single_line + "\t EQU\t" + map_string + "\n";
}
++index;
}
//this single line is #define long long long
if(index <= 4) continue;
//0xFD9 <-> FSR2L map
//FD9 <-> FSR2L
special_reg_map.insert(make_pair(map_string.substr(2), map_index));
output_vec.push_back(output_single_line);
}
//output the map to the .inc file
std::ofstream f_out;
f_out.open(output_file, ios_base::out);
if(!f_out.is_open()) throw "Error: output file open failed!";
for(auto elem : output_vec) f_out << elem;
f_out.close();
/*
string single_line;
regex equ_upper_regex(".+EQU.+");
regex equ_lower_regex(".+equ.+");
//get every line of this file
while(getline(IR_file, single_line)){
if(regex_match(single_line, equ_lower_regex) || \
regex_match(single_line, equ_upper_regex)){
string single_word;
istringstream iss(single_line);
vector<string> map_vec;
//get every word of this line
while(iss >> single_word){
if(!single_word.compare("EQU") || \
!single_word.compare("equ"))
continue;
map_vec.push_back(single_word);
}
//PCL EQU 0XXXH
string map_index = map_vec[1].substr(1, map_vec[1].size() - 2);
special_reg_map.insert(make_pair(map_index, map_vec[0]));
}
}
*/
}
void FirstOpenFileAndProcessGdbInfo(string &file_name){
//Open file
ifstream IR_file(file_name,ios_base::in);
if(!IR_file.is_open()){
throw "Error: FirstOpenFileAndProcessDbgInfo() Open file failed!";
}
string single_line;
//get every line
while(getline(IR_file, single_line)){
//deal with the special char
ChangeComma(single_line);
//deal debug info and create a record in Instr_degbug_info
DebugInfo debug_info_object = DebugInfo();
string single_line_debug = single_line;
string single_word;
istringstream iss(single_line);
// regex reg1("\%\\d+\b"); //for operator %x
regex reg1("\%.+");
//regex reg2("^\@\w+\b=\bglobal");//for global var @varName = global
regex gdb_index("!.*");
SplitWord word_con;
int word_order = 0;
//get every word in line
// yzk 更正了误将包含type的调试行处理为指令行的bug
while (iss >> single_word) {
//whether it is a gdb statement
if (word_order == 0 && regex_match(single_word, gdb_index)) {
word_con.instrName = static_cast<SDICKeyWord>(27);
}
else if (SDIC_operate_set.find(single_word) != \
SDIC_operate_set.end() && \
word_con.instrName == 0) {
auto index = SDIC_operate_set.find(single_word);
word_con.instrName = static_cast<SDICKeyWord>(index->second);
}
word_con.vaCol.push_back(single_word);
++word_order;
}
//this line is invalid
if(word_con.instrName == knull) continue;
switch(word_con.instrName){
case gdb: {
TranceFirstGdb(word_con, "gdb"); break;
}
default: break;
}
}
if(IR_file.is_open()){
IR_file.close();
}
}
void OpenFileAndDeal(string &file_name) {
//Open file
ifstream IR_file(file_name,ios_base::in);
if(!IR_file.is_open()){
throw "Error: OpenFileAndDeal() Open file failed!";
}
unordered_map<string, string> gdb_var_type;//用于存储gdb中var的type、scope等信息
string single_line;
while(getline(IR_file, single_line)){//get every line
//deal with the special char
ChangeComma(single_line);
//deal debug info and create a record in Instr_degbug_info
DebugInfo debug_info_object = DebugInfo();
string single_line_debug = single_line;
string single_word;
istringstream iss(single_line);
// regex reg1("\%\\d+\b"); //for operator %x
regex reg1("\%.+");
//regex reg2("^\@\w+\b=\bglobal");//for global var @varName = global
regex gdb_index("!.*");
SplitWord word_con;
int word_order = 0;
//get every word in line
while (iss >> single_word) { //yzk 更正了误将包含type的调试行处理为指令行的bug
//whether it is a gdb statement
if (word_order == 0 && regex_match(single_word, gdb_index)) {
word_con.instrName = static_cast<SDICKeyWord>(27);
}
else if (SDIC_operate_set.find(single_word) != \
SDIC_operate_set.end() && \
word_con.instrName == 0) {
auto index = SDIC_operate_set.find(single_word);
word_con.instrName = static_cast<SDICKeyWord>(index->second);
}
else if (regex_match(single_word, reg1)) {
word_con.opCol.push_back(single_word);
}
word_con.vaCol.push_back(single_word);
++word_order;
}
//this line is invalid
if(word_con.instrName == knull) continue;
switch(word_con.instrName){
case alloc: {
debug_info_object.CreateAInstrDebugRecord("alloc", \
single_line_debug);
TranceAlloca(word_con, "alloc"); break; }
case load: {
debug_info_object.CreateAInstrDebugRecord("load", \
single_line_debug);
TranceLoad(word_con, "load"); break;}
case store: {
debug_info_object.CreateAInstrDebugRecord("store", \
single_line_debug);
TranceStore(word_con, "store"); break;}
case add: {
debug_info_object.CreateAInstrDebugRecord("add", \
single_line_debug);
TranceAdd(word_con, "add"); break;}
case sub: {
debug_info_object.CreateAInstrDebugRecord("sub", \
single_line_debug);
TranceSub(word_con, "sub"); break;}
case ret: {
debug_info_object.CreateAInstrDebugRecord("ret", \
single_line_debug);
TranceRet(word_con, "ret"); break;}
case fcmp: {
debug_info_object.CreateAInstrDebugRecord("fcmp", \
single_line_debug);
TranceFcmp(word_con, "fcmp"); break;}
case br: {
debug_info_object.CreateAInstrDebugRecord("br", \
single_line_debug);
TranceBr(word_con, "br"); break;}
case label: {
debug_info_object.CreateAInstrDebugRecord("label", \
single_line_debug);
TranceLabel(word_con, "label"); break;}
case globa: {
debug_info_object.CreateAInstrDebugRecord("global", \
single_line_debug);
TranceGlobal(word_con, "global"); break;}
case defin: {
debug_info_object.CreateAInstrDebugRecord("define", \
single_line_debug);
TranceDefine(word_con, "define"); break;}
case call: {
debug_info_object.CreateAInstrDebugRecord("call", \
single_line_debug);
TranceCall(word_con, "call"); break;}
case anda: {
debug_info_object.CreateAInstrDebugRecord("and", \
single_line_debug);
TranceAnd(word_con, "and"); break;}
case ora: {
debug_info_object.CreateAInstrDebugRecord("or", \
single_line_debug);
TranceOr(word_con, "or"); break;}
case xora: {
debug_info_object.CreateAInstrDebugRecord("xor", \
single_line_debug);
TranceXor(word_con, "xor"); break;}
case shl: {
debug_info_object.CreateAInstrDebugRecord("shl", \
single_line_debug);
TranceShl(word_con, "shl"); break;}
case ashr: {
debug_info_object.CreateAInstrDebugRecord("ashr", \
single_line_debug);
TranceAshr(word_con, "ashr"); break;}
case zext: {
debug_info_object.CreateAInstrDebugRecord("zext", \
single_line_debug);
TranceZext(word_con, "zext"); break;}
case phi: {
debug_info_object.CreateAInstrDebugRecord("phi", \
single_line_debug);
TrancePhi(word_con, "phi"); break;}
case constant: {
debug_info_object.CreateAInstrDebugRecord("constant", \
single_line_debug);
TranceConstant(word_con,"constant");break;}
case type: {
debug_info_object.CreateAInstrDebugRecord("type", \
single_line_debug);
TranceType(word_con, "type"); break;}
case getelementptr: {
if(find(word_con.vaCol.begin(), word_con.vaCol.end(), \
"store")!= word_con.vaCol.end()){
debug_info_object.CreateAInstrDebugRecord("store", \
single_line_debug);
TranceStore(word_con, "store");
}
else if(find(word_con.vaCol.begin(), word_con.vaCol.end(), \
"call") != word_con.vaCol.end()){
debug_info_object.CreateAInstrDebugRecord("call", \
single_line_debug);
TranceCall(word_con, "call");
}
else if(find(word_con.vaCol.begin(), word_con.vaCol.end(), \
"load") != word_con.vaCol.end()){
debug_info_object.CreateAInstrDebugRecord("load", \
single_line_debug);
TranceLoad(word_con, "load");
}
else{
debug_info_object.CreateAInstrDebugRecord("getelementptr", \
single_line_debug);
TranceGetelementptr(word_con, "getelementptr");
}
break;
}
case bitcast: {
if(find(word_con.vaCol.begin(), word_con.vaCol.end(), \
"call") != word_con.vaCol.end()){
debug_info_object.CreateAInstrDebugRecord("call", \
single_line_debug);
TranceCall(word_con, "call");
}
else {
debug_info_object.CreateAInstrDebugRecord("bitcast", \
single_line_debug);
TranceBitcast(word_con, "bitcast");
}
break;
}
case sext: {
debug_info_object.CreateAInstrDebugRecord("sext", \
single_line_debug);
TranceSext(word_con, "sext"); break;}
case trunct: {
debug_info_object.CreateAInstrDebugRecord("trunc", \
single_line_debug);
TranceTrunc(word_con, "trunc"); break; }
case selectt: {
debug_info_object.CreateAInstrDebugRecord("select", \
single_line_debug);
TranceSelect(word_con, "select"); break;}
case gdb: {
TranceGdb(word_con, "gdb",file_name,gdb_var_type);
break;
}
case switch_begin: {
debug_info_object.CreateAInstrDebugRecord("switch", \
single_line_debug);
TranceSwitch(word_con, "switch"); break;
}
case switch_label: {
if(find(word_con.vaCol.begin(), word_con.vaCol.end(), \
"switch") != word_con.vaCol.end()){
debug_info_object.CreateAInstrDebugRecord("switch", \
single_line_debug);
TranceSwitch(word_con, "switch"); break;
}
else if(find(word_con.vaCol.begin(), word_con.vaCol.end(),
"br") != word_con.vaCol.end()){
debug_info_object.CreateAInstrDebugRecord("br", \
single_line_debug);
TranceBr(word_con, "br"); break;
}
else {
debug_info_object.CreateAInstrDebugRecord("switch", \
single_line_debug);
TranceLabelSwitch(word_con, "switch"); break;
}
}
case switch_end: {
debug_info_object.CreateAInstrDebugRecord("switch", \
single_line_debug);
TranceSwitchEnd(word_con, "switch"); break;
}
case mul: {
debug_info_object.CreateAInstrDebugRecord("mul", \
single_line_debug);
TranceMul(word_con, "mul"); break;
}
case sdiv: {
debug_info_object.CreateAInstrDebugRecord("sdiv", \
single_line_debug);
TranceSdiv(word_con, "sdiv"); break;
}
case ptrtoint: {
debug_info_object.CreateAInstrDebugRecord("ptrtoint", \
single_line_debug);
TrancePtrToInt(word_con, "ptrtoint"); break;
}
case udiv: {
debug_info_object.CreateAInstrDebugRecord("udiv", \
single_line_debug);
TranceSdiv(word_con, "udiv"); break;
}
case srem: {
debug_info_object.CreateAInstrDebugRecord("srem", \
single_line_debug);
TranceRem(word_con, "srem"); break;
}
case urem: {
debug_info_object.CreateAInstrDebugRecord("urem", \
single_line_debug);
TranceRem(word_con, "urem"); break;
}
default: break;
}
// debug_info_object.CreateAInstrDebugRecord(case_name, \
// single_line_debug);
}
//Close file
if(IR_file.is_open()){
IR_file.close();
}
};