-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrance_fun.cpp
5326 lines (4702 loc) · 171 KB
/
trance_fun.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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//Copyright 2019 Hust.
//Author: HYL
//Descriptor:
/*
*/
#include <iostream>
#include <cstring>
#include <regex>
#include <cassert>
#include "tool_fun.h"
#include "trance_fun.h"
#include "out_put_fun.h"
#include "memory_manage_reg.h"
#include "memory_manage_data.h"
#include "debug_info.h"
using namespace::std;
class RegManage;
class DataManage;
void ChangeComma(string &str){
//if the str is already compiler language
//call void asm sideeffect "lea eax, n, /norr, r0, r0", "~{$1}"()
if(str.find("call") != string::npos && \
str.find("asm") != string::npos && \
str.find("sideeffect") != string::npos)
return;
for(int i = 0; i < str.size(); i++){
if(str[i] == ',' || str[i] == ':' || \
str[i] == '(' || str[i] == ')'){
str[i] = ' ';
}
}
}
/*the beignning of program
*/
void ProgramBegin(){
OutPutOrg("org", "0x0000");
OutPutJump("bra", "Preprocess", " ");
//OutPutJump("bra", "begin");
OutPutOrg("org", "0x0008");
OutPutJump("bra", "Hint", " ");
// OutPutLabel("begin");
OutPutOrg("org", "0x0020");
OutPutLabel("Preprocess", " ");
OutPutJumpPure("call", "ClearRam", " ");
}
//the interrupt of the program
//
void ProgramHint(){
OutPutLabel("Hint", " ");
OutPutOrg("retfie", "1");
OutPutOrg("end", " ");
}
//coreAdd_And
//core fun or logic operatore
void CoreFunOfLogicOperator(SplitWord wordCon, string namewf, \
string namelw, string IR_name){
//Instr: a = R + Constant or reverse
if(wordCon.opCol.size() <= 2){
string op_des = wordCon.opCol[0];
string op_src1 = wordCon.vaCol[4];
string op_src2 = wordCon.vaCol[5];
string op_des_type = wordCon.vaCol[3];
string op_src1_type = wordCon.vaCol[3];
string op_src2_type = wordCon.vaCol[3];
RegManage* reg_manage_obj = RegManage::getInstance();
//add fun name before variable
string current_fun_name = \
reg_manage_obj->GetValueFromWhichFunStack();
if(!current_fun_name.empty()){
op_des = current_fun_name + "." + op_des;
}
//deal with op_des
reg_manage_obj->AllocateRegToGenVal(op_des, op_des_type, 1);
vector<string> op_des_addr = \
reg_manage_obj->GetActualAddrFromGenVal(op_des, 0);
int reg_num = reg_manage_obj->HowBigType(op_src1_type);
vector<string> value1, value2;
regex reg(".*\%.*");
if(regex_match(op_src1, reg)){
op_src1 = current_fun_name + "." + op_src1;
value1 = reg_manage_obj->GetActualAddrFromGenVal(op_src1, 0);
} else{
// value1 = opSrc1;
}
//Instr: a = Constant + R
if(regex_match(op_src2, reg)){
if(!current_fun_name.empty()){
op_src2 = current_fun_name + "." + op_src2;
}
value2 = reg_manage_obj->GetActualAddrFromGenVal(op_src2, 0);
value1 = reg_manage_obj->GetSplitSectionOfANum(op_src1, \
reg_num);
if(reg_manage_obj->CheckAllOperatorType(op_src1_type, \
op_src2_type, \
op_des_type)){
for(int i = 0; i < reg_num; i++){
OutPut("movf", value2[i], IR_name);
OutPut(namelw, "." + value1[i], IR_name);
OutPut("movwf", op_des_addr[i], IR_name);
}
}
return;
}
//Instr: a = R + Constant
else {
//Instr: ~A
if(!op_src2.compare("-1") && !namelw.compare("xorlw")){
if(reg_manage_obj->CheckAllOperatorType(op_src1_type, \
op_des_type)){
for(int i = 0; i < reg_num; i++){
OutPut("comf", value1[i], IR_name); //Instr: ~A
OutPut("movwf", op_des_addr[i], IR_name);
}
}
}
//Instr: B = !A
else if(!op_src2.compare("true") && \
!namelw.compare("xorlw")){
//Instr: B = !A
vector<string> constant = \
reg_manage_obj->GetSplitSectionOfANum("1", reg_num);
if(reg_manage_obj->CheckAllOperatorType(op_src1_type, \
op_des_type)){
for(int i = 0; i < reg_num; i++){
OutPut("movlw", constant[i], IR_name);
//OutPut("movlw", "1");
OutPut(namelw, "." + value1[i], IR_name);
OutPut("movwf", op_des_addr[i], IR_name);
}
}
}
else {
vector<string> constant = \
reg_manage_obj->GetSplitSectionOfANum(op_src2, reg_num);
if(reg_manage_obj->CheckAllOperatorType(op_src1_type, \
op_des_type)){
for(int i = 0; i < reg_num; i++){
OutPut("movf", value1[i], IR_name);
OutPut(namelw, "." + constant[i], IR_name);
OutPut("movwf", op_des_addr[i], IR_name);
}
}
}
}
}
//Instr: a = R + R
else if(wordCon.opCol.size() >= 3){
string op_des = wordCon.opCol[0];
string op_src1 = wordCon.opCol[1];
string op_src2 = wordCon.opCol[2];
string op_des_type = wordCon.vaCol[3];
RegManage* reg_manage_obj = RegManage::getInstance();
//add fun name before variable
string current_fun_name = \
reg_manage_obj->GetValueFromWhichFunStack();
if(!current_fun_name.empty()){
op_src1 = current_fun_name + "." + op_src1;
op_src2 = current_fun_name + "." + op_src2;
op_des = current_fun_name + "." + op_des;
}
vector<string> value1 = \
reg_manage_obj->GetActualAddrFromGenVal(op_src1, 0);
vector<string> value2 = \
reg_manage_obj->GetActualAddrFromGenVal(op_src2, 0);
reg_manage_obj->AllocateRegToGenVal(op_des, op_des_type, 1);
vector<string> reg_name = \
reg_manage_obj->GetActualAddrFromGenVal(op_des, 0);
int reg_num = reg_name.size();
if(value1.size() == value2.size() && \
value2.size() == reg_name.size()){
for(int i = 0; i < reg_num; i++){
OutPut("movf", value1[i], IR_name);
OutPut(namewf, value2[i], IR_name);
OutPut("movwf", reg_name[i], IR_name);
}
}else {
cout << "the type is not correspond!" << endl;
}
}
}
void TranceLoad(SplitWord wordCon, string IR_name){
#define VEC wordCon.vaCol
regex regular_expr_struct("%struct.*");
//get element from array that store in data area and reg
//it dispair in the array is global or constant type
if(find(VEC.begin(), VEC.end(), "getelementptr") != VEC.end() && \
find(VEC.begin(), VEC.end(), "inbounds") != VEC.end() && \
find(VEC.begin(), VEC.end(), "x") != VEC.end()){
int array_dimension = count(VEC.begin(), VEC.end(), "x");
string op_des, op_des_type, array_name;
int elem_index = 0;
//the array is one dimension
if(array_dimension == 2){
op_des = wordCon.vaCol[0];
op_des_type = wordCon.vaCol[3];
array_name = VEC[13];
//whcih elem in the array that should be taken
//change the index to dec number
elem_index = ChangeStrToDec(wordCon.vaCol[17]);
}
//the array is two dimension
else if(array_dimension == 4){
op_des = wordCon.vaCol[0];
op_des_type = wordCon.vaCol[3];
array_name = VEC[17];
//whcih elem in the array that should be taken
//change the index to dec number
elem_index = ChangeStrToDec(wordCon.vaCol[21]) * \
ChangeStrToDec(wordCon.vaCol[9].substr(1)) + \
ChangeStrToDec(wordCon.vaCol[23]);
}
MemoryManage memory_manage_obj = MemoryManage();
//get the index that dedicated the variable store which map
int which_store_map_index = \
memory_manage_obj.VarStoreWhichMap(array_name);
//the array variable store in reg_addr_map
if(which_store_map_index == 1){
RegManage* reg_manage_obj = RegManage::getInstance();
//add fun name before variable
string current_fun_name = \
reg_manage_obj->GetValueFromWhichFunStack();
if(!current_fun_name.empty()){
array_name = current_fun_name + "." + array_name;
op_des = current_fun_name + "." + op_des;
}
//allocate addr for the op_des
reg_manage_obj->AllocateRegToGenVal(op_des, op_des_type, 1);
//get the actual addr of the op_des
vector<string> reg_name = \
reg_manage_obj->GetActualAddrFromGenVal(op_des, 0);
//get the elem addr
vector<string> elem_addr_vec = \
reg_manage_obj->GetActualAddrFromGenVal(array_name,\
elem_index);
int reg_num = elem_addr_vec.size();
for(int i = 0; i < reg_num; i++){
OutPut("movf", elem_addr_vec[i], IR_name);
OutPut("movwf", reg_name[i], IR_name);
}
}
//the array variable store in global_addr_map
else if(which_store_map_index == 2){
RegManage* reg_manage_obj = RegManage::getInstance();
//add fun name before variable
string current_fun_name = \
reg_manage_obj->GetValueFromWhichFunStack();
if(!current_fun_name.empty()){
op_des = current_fun_name + "." + op_des;
}
//allocate addr for the op_des
reg_manage_obj->AllocateRegToGenVal(op_des, op_des_type, 1);
//get the actual addr of the op_des
vector<string> reg_name = \
reg_manage_obj->GetActualAddrFromGenVal(op_des, 0);
//get the elem addr
vector<string> elem_addr_vec = \
reg_manage_obj->GetActualAddrFromGlobalVal(array_name, \
elem_index);
int reg_num = elem_addr_vec.size();
for(int i = 0; i < reg_num; i++){
OutPut("movf", elem_addr_vec[i], IR_name);
OutPut("movwf", reg_name[i], IR_name);
}
}
//the array variable store in data_area_addr_map
else if(which_store_map_index == 4){
DataAreaManage* data_area_manage_obj = \
DataAreaManage::getInstance();
RegManage* reg_manage_obj = RegManage::getInstance();
//add fun name before variable
string current_fun_name = \
reg_manage_obj->GetValueFromWhichFunStack();
if(!current_fun_name.empty()){
op_des = current_fun_name + "." + op_des;
}
//aloca acutal register for the op_des
reg_manage_obj->AllocateRegToGenVal(op_des, op_des_type, 1);
//get the actual reg of op_es
vector<string> reg_name = \
reg_manage_obj->GetActualAddrFromGenVal(op_des, 0);
//get the elem addr in map
vector<string> elem_addr_vec = \
data_area_manage_obj->GetActualAddrFromVal(array_name,\
elem_index);
int reg_num = elem_addr_vec.size();
for(int i = 0; i < reg_num; i++){
string addr_h(elem_addr_vec[i].substr(0,2));
string addr_l(elem_addr_vec[i].substr(2,2));
OutPut("movlw", "." + addr_h, IR_name);
OutPut("movwf", "tblptrh", IR_name);
OutPut("movlw", "." + addr_l, IR_name);
OutPut("movwf", "tblptrl", IR_name);
OutPut("tblrd", "*", IR_name);
OutPut("movwf", "tablat", IR_name);
OutPut("movf", "tablat", IR_name);
OutPut("movwf", reg_name[i], IR_name);
}
}
}
//get element from global struct type that store in global map
else if(find(VEC.begin(), VEC.end(), "load") != VEC.end() && \
find(VEC.begin(), VEC.end(),"getelementptr")!=VEC.end()&&\
regex_match(VEC[7], regular_expr_struct)){
//destination variable
string op_des = VEC[0];
//destination variable type
string op_des_type = VEC[3];
//struct name
string struct_name = VEC[9];
//which elem
int elem_index = ChangeStrToDec(VEC[13]);
RegManage * reg_manage_obj = RegManage::getInstance();
MemoryManage memory_manage_obj = MemoryManage();
//add fun name before variable
string current_fun_name = \
memory_manage_obj.GetValueFromWhichFunStack();
if(!current_fun_name.empty()){
op_des = current_fun_name + "." + op_des;
}
//get the index that dedicated the variable store which map
int which_store_map_index = \
memory_manage_obj.VarStoreWhichMap(struct_name);
//the struct variable store in reg_addr_map
if(which_store_map_index == 1){
}
//the struct variable store in global_addr_map
else if(which_store_map_index == 2){
//get the elem_index adddr in struct_name that store in
//global_addr_map
vector<string> elem_addr = \
reg_manage_obj->GetActualAddrFromGlobalVal(struct_name, \
elem_index);
reg_manage_obj->AllocateRegToGenVal(op_des, op_des_type, 1);
vector<string> op_des_addr_vec = \
reg_manage_obj->GetActualAddrFromGenVal(op_des, 0);
if(elem_addr.size() != op_des_addr_vec.size()){
throw "TranceLoad(): op_des size and op_src size no equal!";
}
for(int i = 0; i < elem_addr.size(); i++){
OutPut("movf", elem_addr[i], IR_name);
OutPut("movwf", op_des_addr_vec[i], IR_name);
}
}
//the struct variable store in data_area_map
else if(which_store_map_index == 4){
DataAreaManage *data_area_manage_obj = \
DataAreaManage::getInstance();
//get the elem_index adddr in struct_name
vector<string> elem_addr = \
data_area_manage_obj->GetActualAddrFromVal(struct_name,\
elem_index);
//deal with op_des
reg_manage_obj->AllocateRegToGenVal(op_des, op_des_type, 1);
vector<string> op_des_addr = \
reg_manage_obj->GetActualAddrFromGenVal(op_des, 0);
if(elem_addr.size() != op_des_addr.size()){
throw "TranceLoad(): op_des size and op_src size no "\
"equal!";
}
for(int i = 0; i < elem_addr.size(); i++){
string addr_h(elem_addr[i].substr(0,2));
string addr_l(elem_addr[i].substr(2,2));
OutPut("movlw", "." + addr_h, IR_name);
OutPut("movwf", "tblptrh", IR_name);
OutPut("movlw", "." + addr_l, IR_name);
OutPut("movwf", "tblptrl", IR_name);
OutPut("tblrd", "*", IR_name);
OutPut("movwf", "tablat", IR_name);
OutPut("movf", "tablat", IR_name);
OutPut("movwf", op_des_addr[i], IR_name);
}
}
}
//get element from register
else {
string op_src1_type = VEC[3];
string op_src2_type = VEC[4];
int op_num1, op_num2;
op_num1 = op_num2 = 0;
for(auto elem : op_src1_type)
if(elem == '*')
op_num1++;
for(auto elem : op_src2_type)
if(elem == '*')
op_num2++;
if(!VEC[3].compare("volatile") && \
!VEC[6].compare("inttoptr")){
string op_src = VEC[8];
string op_des = VEC[0];
string op_src_type = VEC[4];
string op_des_type = VEC[7];
DataStoreInfo core_info; //reg_name;
RegManage* reg_manage_obj = RegManage::getInstance();
//add fun name before variable
string current_fun_name = \
reg_manage_obj->GetValueFromWhichFunStack();
if(!current_fun_name.empty()){
op_des = current_fun_name + "." + op_des;
}
int reg_num = reg_manage_obj->HowBigType(op_src_type);
vector<string> addr_vec = \
reg_manage_obj->GetSplitSectionOfANum(op_src, reg_num);
int addr_name_num = ChangeStrToDec(op_src);
string addr_name = ChangeDecToHex(addr_name_num);
core_info.actual_addr.push_back("0s" + addr_name);
core_info.data_type = "i8";
reg_manage_obj->CopyAMapForGenVal(op_des, core_info);
}
//for point ex: %6 = load i8**, i8*** %4, align 4, !dbg !12
else if(op_num1 > 1 && op_num2 > 1){
string op_des = VEC[0];
string op_src = VEC[5];
RegManage* reg_manage_obj = RegManage::getInstance();
//add fun name before variable
string current_fun_name = \
reg_manage_obj->GetValueFromWhichFunStack();
if(!current_fun_name.empty()){
op_src = current_fun_name + "." + op_src;
op_des = current_fun_name + "." + op_des;
}
if(reg_manage_obj->WhetherPtrAdditionalInfo(op_src)){
//op_src = reg_manage_obj->GetPtrAdditionalInfo(op_src);
op_src = reg_manage_obj->GetPtrInfoFromRecord(op_src);
}
string point_info = \
reg_manage_obj->GetPtrInfoFromRecord(op_src);
reg_manage_obj->AddPtrAdditionalInfo(op_des, point_info);
}
// %9 = load i32*, i32** %5, align 4, !dbg !15
else if(op_num1 == 1 && op_num2 == 2){
string op_src = VEC[5];
string op_des = VEC[0];
string op_des_type = VEC[3];
DataStoreInfo core_info;
RegManage* reg_manage_obj = RegManage::getInstance();
//add fun name before variable
string current_fun_name = \
reg_manage_obj->GetValueFromWhichFunStack();
if(!current_fun_name.empty()){
op_src = current_fun_name + "." + op_src;
op_des = current_fun_name + "." + op_des;
}
vector<string> op_src_vec, op_des_vec;
if(reg_manage_obj->WhetherPtrDeliverMap(op_src)){
//get font addr via deliver map
op_src_vec = \
reg_manage_obj->GetElemFromPtrDeliverMap(op_src);
//get correct addr via the font addr
op_des_vec = \
reg_manage_obj->GetElemAddrFromPtrAddr(op_src_vec[0], 0);
}
else {
cout << "Error! The ptr isn't assigned!" << endl;
}
//for(auto elem : op_des_vec) cout << "al " << elem << endl;
/*
//i8* is two reg, but op_desv_vec.size() equal one
//we should make it equal
int elem_size = reg_manage_obj->HowBigType(op_des_type);
int addr_size = op_des_vec.size();
if(elem_size > addr_size){
for(int i = 0; i < elem_size - addr_size; i++){
op_des_vec.push_back("0x000");
}
}
*/
//i8* to i8
auto index = op_des_type.find("*");
core_info.data_type = op_des_type.substr(0, index);
core_info.actual_addr = op_des_vec;
reg_manage_obj->CopyAMapForGenVal(op_des, core_info);
}
// %9 = load i32, i32* %5, align 4, !dbg !15
else if(op_num1 == 0 && op_num2 == 1) {
string op_src = wordCon.vaCol[5];
string op_des = wordCon.vaCol[0];
DataStoreInfo core_info; //reg_name;
//the two regex are used to distict global var or general var
//global var
regex reg1(".*@.+");
// general var
regex reg2(".*\%.+");
RegManage* reg_manage_obj = RegManage::getInstance();
//add fun name before variable
string current_fun_name = \
reg_manage_obj->GetValueFromWhichFunStack();
if(!current_fun_name.empty()){
op_src = current_fun_name + "." + op_src;
op_des = current_fun_name + "." + op_des;
}
/*
//deal with the ptr
//if this op src is ptr
if(reg_manage_obj->WhetherPtrAdditionalInfo(op_src)){
op_src = reg_manage_obj->GetPtrAdditionalInfo(op_src);
//op_src = reg_manage_obj->GetPtrInfoFromRecord(op_src);
}
*/
if(regex_match(op_src, reg1)){
core_info = reg_manage_obj->GetAllInfoFromGlobalVal(op_src);
}
else if(regex_match(op_src, reg2)){
core_info = reg_manage_obj->GetAllInfoFromGenVal(op_src);
}
reg_manage_obj->CopyAMapForGenVal(op_des, core_info);
}
}
#undef VEC
}
void TranceStore(SplitWord wordCon, string IR_name){
#define VEC wordCon.vaCol
regex regular_expr_struct("%struct.*");
// Store the value to the elem in array
// store i32 99, i32* getelementptr inbounds ([3 x i32],
// [3 x i32]* @arr_one, i32 0, i32 1), align 4, !dbg !13
if(find(VEC.begin(), VEC.end(), "getelementptr") != VEC.end() && \
find(VEC.begin(), VEC.end(), "x") != VEC.end() && \
!VEC[4].compare("getelementptr")){
int array_dimension = count(VEC.begin(), VEC.end(), "x");
string op_src, op_src_type, array_name, which_elem_index;
int elem_index = 0;
//the array is one dimension
if(array_dimension == 2){
op_src = VEC[2];
op_src_type = VEC[1];
array_name = VEC[12];
//store which elem in the array
which_elem_index = VEC[16];
//change the index to dec number
elem_index = ChangeStrToDec(which_elem_index);
}
//the array is two dimension
else if(array_dimension == 4){
op_src = VEC[2];
op_src_type = VEC[1];
array_name = VEC[16];
//store value to which elem in this array
elem_index = ChangeStrToDec(VEC[8].substr(1)) *
ChangeStrToDec(VEC[20]) + ChangeStrToDec(VEC[22]);
}
MemoryManage memory_manage_obj = MemoryManage();
RegManage* reg_manage_obj = RegManage::getInstance();
int reg_num = memory_manage_obj.HowBigType(op_src_type);
//source variable, it may be a reg addr or split value
vector<string> src_addr_or_value_vec;
//decide the op_src wheather a register or a constant
//the reg_or_constant math register
//get the src value
regex reg_or_constant(".*\%.*");
if(regex_match(op_src, reg_or_constant)){
//add fun name before variable
string current_fun_name = \
reg_manage_obj->GetValueFromWhichFunStack();
if(!current_fun_name.empty()){
op_src = current_fun_name + "." + op_src;
}
src_addr_or_value_vec = \
reg_manage_obj->GetActualAddrFromGenVal(op_src, 0);
}
//the src is constant
else {
src_addr_or_value_vec = \
memory_manage_obj.GetSplitSectionOfANum(op_src, reg_num);
}
//get the index that dedicated the variable store which map
int which_store_map_index = \
memory_manage_obj.VarStoreWhichMap(array_name);
//the value should store in reg_addr_map
if(which_store_map_index == 1){
//add fun name before variable
string current_fun_name = \
reg_manage_obj->GetValueFromWhichFunStack();
if(!current_fun_name.empty()){
array_name = current_fun_name + "." + array_name;
}
//get the actual addr of the op_des
vector<string> elem_addr_vec = \
reg_manage_obj->GetActualAddrFromGenVal(array_name, \
elem_index);
for(int i = 0; i < reg_num; i++){
OutPut("movlw", "." + src_addr_or_value_vec[i], IR_name);
OutPut("movwf", elem_addr_vec[i], IR_name);
}
}
//the value should store in global_addr_map
else if(which_store_map_index == 2){
//get the actual addr of the op_des
vector<string> elem_addr_vec = \
reg_manage_obj->GetActualAddrFromGlobalVal(array_name, \
elem_index);
for(int i = 0; i < reg_num; i++){
OutPut("movlw", "." + src_addr_or_value_vec[i], IR_name);
OutPut("movwf", elem_addr_vec[i], IR_name);
}
}
//the value should store in data_area_map
else if(which_store_map_index == 4){
DataAreaManage* data_area_manage_obj = \
DataAreaManage::getInstance();
//get the actual addr of the op_des
vector<string> elem_addr_vec = \
data_area_manage_obj->GetActualAddrFromVal(array_name, \
elem_index);
for(int i = 0; i < reg_num; i++){
OutPut("movlw", "." + src_addr_or_value_vec[i], IR_name);
OutPut("movwf", "tablat", IR_name);
OutPut("movlw", "." + elem_addr_vec[i].substr(0,2), IR_name);
OutPut("movwf", "tblptrh", IR_name);
OutPut("movlw", "." + elem_addr_vec[i].substr(2,4), IR_name);
OutPut("movwf", "tblptrl", IR_name);
OutPut("tblwr", "*", IR_name);
}
}
}
// store the global array addr to the ptr
// store i32* getelementptr inbounds ([3 x i32], [3 x i32]*
//@arr_one, i32 0, i32 0), i32** %5, align 4, !dbg !14
else if(find(VEC.begin(),VEC.end(),"getelementptr")!= VEC.end() &&\
find(VEC.begin(), VEC.end(), "x") != VEC.end() && \
!VEC[2].compare("getelementptr")){
int array_dimension = count(VEC.begin(), VEC.end(), "x");
string array_name, op_des;
int elem_size, elem_index, elem_begin;
//the array is one dimension
if(array_dimension == 2){
array_name = VEC[10];
op_des = VEC[16];
elem_size = ChangeStrToDec(VEC[7].substr(1));
elem_index = ChangeStrToDec(VEC[12]);
elem_begin = elem_index * elem_size;
}
//the array is two dimension
else if(array_dimension == 4) {
auto getelementptr_index = \
count(VEC.begin(), VEC.end(), "getelementptr");
if(getelementptr_index == 1){
array_name = VEC[14];
op_des = VEC[22];
elem_size = ChangeStrToDec(VEC[6].substr(1));
elem_index = ChangeStrToDec(VEC[18]);
elem_begin = elem_index * elem_size;
}
else if(getelementptr_index == 2){
array_name = VEC[18];
op_des = VEC[28];
elem_size = ChangeStrToDec(VEC[10].substr(1));
elem_index = ChangeStrToDec(VEC[22]);
elem_begin = elem_index * elem_size + ChangeStrToDec(VEC[26]);
int total_size = ChangeStrToDec(VEC[13].substr(1)) * \
ChangeStrToDec(VEC[15].substr(1));
if(elem_begin >= total_size) {
throw "Error! Elem index out of range!";
}
}
}
RegManage* reg_manage_obj = RegManage::getInstance();
//add fun name before variable
string current_fun_name = \
reg_manage_obj->GetValueFromWhichFunStack();
if(!current_fun_name.empty()){
op_des = current_fun_name + "." + op_des;
}
/*
if(reg_manage_obj->WhetherPtrAdditionalInfo(op_des)){
op_des = reg_manage_obj->GetPtrInfoFromRecord(op_des);
}
if(reg_manage_obj->WhetherPtrDeliverMap(op_des)){
op_des = reg_manage_obj->GetElemFromPtrDeliverMap(op_des);
}
*/
int which_store_map_index = \
reg_manage_obj->VarStoreWhichMap(array_name);
//the array variable store in reg_addr_map
if(which_store_map_index == 1) {
cout << "Warning! contniue to do!" << endl;
}
//the array variable store in global_addr_map
else if(which_store_map_index == 2){
vector<string> op_des_vec = \
reg_manage_obj->GetActualAddrFromGenVal(op_des, 0);
vector<string> elem_addr_vec = \
reg_manage_obj->GetActualAddrFromGlobalVal(array_name, \
elem_begin);
string ptr_elem = elem_addr_vec[0];
reg_manage_obj->AddElemIntoPtrDeliverMap(op_des,\
elem_addr_vec);
string ptr_elem_first = "0x" + ptr_elem.substr(3);
string ptr_elem_second = "0x0" + ptr_elem.substr(2,1);
OutPut("movlw", "0a" + ptr_elem_first, IR_name);
OutPut("movwf", op_des_vec[0], IR_name);
OutPut("movlw", "0a" + ptr_elem_second, IR_name);
OutPut("movwf", op_des_vec[1], IR_name);
}
//the array variable store in data_area_addr_map
else if(which_store_map_index == 4){
vector<string> op_des_vec = \
reg_manage_obj->GetActualAddrFromGenVal(op_des, 0);
DataAreaManage *data_area_manage_obj = \
DataAreaManage::getInstance();
vector<string> elem_addr_vec = \
data_area_manage_obj->GetActualAddrFromVal(array_name,\
elem_index * elem_size);
string ptr_elem = elem_addr_vec[0];
reg_manage_obj->AddElemIntoPtrDeliverMap(op_des,\
elem_addr_vec);
string ptr_elem_first = ptr_elem.substr(2);
string ptr_elem_second = ptr_elem.substr(0,2);
OutPut("movlw", "0a" + ptr_elem_first, IR_name);
OutPut("movwf", op_des_vec[0], IR_name);
OutPut("movlw", "0a" + ptr_elem_second, IR_name);
OutPut("movwf", op_des_vec[1], IR_name);
throw "Warning! continue to do!";
}
}
//store the value to the elem in struct
else if(find(VEC.begin(),VEC.end(),"getelementptr")!= VEC.end()&& \
regex_match(VEC[6], regular_expr_struct)){
string op_src = VEC[2];
string struct_var_name = VEC[8];
string elem_index_str = VEC[12];
string op_src_type = VEC[1];
//change the string elem_index_str to int type
int elem_index = ChangeHexToDec(elem_index_str);
RegManage *reg_manage_obj = RegManage::getInstance();
int reg_num = reg_manage_obj->HowBigType(op_src_type);
//source variable, it may be a reg addr or split value
vector<string> src_addr_or_value_vec;
//decide the op_src wheather a register or a constance
//the reg_or_constant match register
//get the src value
regex reg_or_constant(".*\%.*");
if(regex_match(op_src, reg_or_constant)){
//add fun name before variable
string current_fun_name = \
reg_manage_obj->GetValueFromWhichFunStack();
if(!current_fun_name.empty()){
op_src = current_fun_name + "." + op_src;
}
src_addr_or_value_vec = \
reg_manage_obj->GetActualAddrFromGenVal(op_src, 0);
}
//the src is constant
else {
src_addr_or_value_vec = \
reg_manage_obj->GetSplitSectionOfANum(op_src,reg_num);
}
//we should know where the struct_var_name store?
int where_struct_var_name_store = \
reg_manage_obj->VarStoreWhichMap(struct_var_name);
//the struct_var_name store in reg_addr_map
if(where_struct_var_name_store == 1){
reg_manage_obj->GetActualAddrFromGenVal(struct_var_name, \
elem_index);
}
//the struct_var_name store in global_addr_map;
//the shruct_var_name don't be stored in data area map.
//because the variable store in data area map is constant
else if(where_struct_var_name_store == 2){
vector<string> elem_addr_vec = \
reg_manage_obj->GetActualAddrFromGlobalVal(struct_var_name,\
elem_index);
for(int i = 0; i < reg_num; i++){
OutPut("movlw", "." + src_addr_or_value_vec[i], IR_name);
OutPut("movwf", elem_addr_vec[i], IR_name);
}
}
//struc var_name store in data_area_map;
else if(where_struct_var_name_store == 4){
DataAreaManage *data_area_manage_obj = \
DataAreaManage::getInstance();
vector<string> elem_addr_vec = \
data_area_manage_obj->GetActualAddrFromVal(struct_var_name,\
elem_index);
for(int i = 0; i < reg_num; i++){
OutPut("movlw", "." + src_addr_or_value_vec[i], IR_name);
OutPut("movwf", "tablat", IR_name);
OutPut("movlw", "." + elem_addr_vec[i].substr(0,2), IR_name);
OutPut("movwf", "tblptrh", IR_name);
OutPut("movlw", "." + elem_addr_vec[i].substr(2,4), IR_name);
OutPut("movwf", "tblptrl", IR_name);
OutPut("tblwr", "*", IR_name);
}
}
}
//Store the value to the single variabel
else{
string op_src1 = wordCon.vaCol[2];
string op_src2 = wordCon.vaCol[4];
string op_src1_type = wordCon.vaCol[1];
string op_src2_type = wordCon.vaCol[3];
if(!VEC[1].compare("volatile") && \
!VEC[5].compare("inttoptr")){
op_src1 = VEC[3];
op_src2 = VEC[7];
op_src1_type = VEC[2];
op_src2_type = VEC[9];
}
RegManage* reg_manage_obj = RegManage::getInstance();
regex res(".*\%.*");
//Instr: store R to R
if(regex_match(op_src1, res)){
//if no fun parameter
if(!reg_manage_obj->WhetherHaveFunPara()) {
//for point
int op_num1, op_num2;
op_num1 = op_num2 = 0;
for(auto elem : op_src1_type)
if(elem == '*')
op_num1 ++;
for(auto elem : op_src2_type)
if(elem == '*')
op_num2 ++;
//deal with the special reg
if(!VEC[1].compare("volatile") && \
!VEC[5].compare("inttoptr")){
int reg_num_index = reg_manage_obj->HowBigType(op_src2);
if(reg_num_index > 1){
throw "Error: TranceStore() reg must be store in 8 bit reg";
}
//add fun name before variable
string current_fun_name = \
reg_manage_obj->GetValueFromWhichFunStack();
if(!current_fun_name.empty()){
op_src1 = current_fun_name + "." + op_src1;
}
vector<string> reg_name_src1 = \
reg_manage_obj->GetActualAddrFromGenVal(op_src1, 0);
int addr_name_num = ChangeStrToDec(op_src2);
string addr_name = ChangeDecToHex(addr_name_num);
OutPut("movf", reg_name_src1[0], IR_name);
OutPut("movwf", "0s" + addr_name, IR_name);
}
//for point ex: store i8* %2, i8** %3, align 4, !dbg !9
else if(op_num1 > 1 || op_num2 > 1){
//add fun name before variable
string current_fun_name = \
reg_manage_obj->GetValueFromWhichFunStack();
if(!current_fun_name.empty()){
op_src1 = current_fun_name + "." + op_src1;
op_src2 = current_fun_name + "." + op_src2;
}
vector<string> op_src1_vec = \
reg_manage_obj->GetActualAddrFromGenVal(op_src1, 0);
vector<string> op_src2_vec = \
reg_manage_obj->GetActualAddrFromGenVal(op_src2, 0);
reg_manage_obj->AddPtrInfoToRecord(op_src2, op_src1);
reg_manage_obj->AddPtrAdditionalInfo(op_src2, op_src1);
//select the smaller size
int vec_size = \
op_src1_vec.size() > op_src2_vec.size()? \
op_src2_vec.size() : op_src1_vec.size();
reg_manage_obj->AddElemIntoPtrDeliverMap(op_src2, \
op_src1_vec);
//get the front addr
string ptr_elem = op_src1_vec[0];
string ptr_elem_first = "0x" + ptr_elem.substr(3);
string ptr_elem_second = "0x0" + ptr_elem.substr(2,1);
OutPut("movlw", "0a" + ptr_elem_first, IR_name);
OutPut("movwf", op_src2_vec[0], IR_name);
OutPut("movlw", "0a" + ptr_elem_second, IR_name);
OutPut("movwf", op_src2_vec[1], IR_name);
/*
for(int i = 0; i < vec_size; i++){
OutPut("movlw", "0a" + op_src1_vec[i], IR_name);
OutPut("movwf", op_src2_vec[i], IR_name);
}
*/
}
//deal with common store
else {
//add fun name before variable
string current_fun_name = \
reg_manage_obj->GetValueFromWhichFunStack();
if(!current_fun_name.empty()){
op_src1 = current_fun_name + "." + op_src1;
op_src2 = current_fun_name + "." + op_src2;
}