-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlexparse1.cpp
1380 lines (1051 loc) · 32.5 KB
/
lexparse1.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
#include "llvm/ADT/STLExtras.h"
#include<bits/stdc++.h>
using namespace std;
//1. This is an attempt to write a complete lexer and parser for ROP Gadgets.
//2. Support is given to 2 instructions - mov and pop.
//3. After understanding the concept, will extend it to all the common instructions.
// IDEAL CONTROL FLOW OF THE PARSER: (Lexer helps the parser!) - getNextToken() - sexy job!
// int main()
// |
// MainLoop()
// |
// --------------if eof---------------|--------if new label found----------
// | |
// DisplayAST() HandleLabel() - Just a Stub!
// | |
// IR Generation using AST(Next job!) ParseLabel() - Adds label to root's vector
// |
// -->----ParseAll() - Has options to parse all instructions.
// | |
// | ----mov------ParseMov()
// | |
// | ---push------ParsePush()
// | |
// | ----pop------ParsePop()
// | |
// | ----add------ParseAdd()
// | |
// | ----sub------ParseSub()
// | |
// | ----inc------ParseInc()
// | |
// | ----dec------ParseDec()
// | |
// ---<------| 1. The lexer works properly. No bugs encoutered so far.
//2. Handle the above mentioned asm instructions - Operands being registers and
//
//
//
//
//Improvements required immediately:
//
//1. Support for Memory - operands. - Eg: DWORD PTR[rbp-0x10] - Work on Lexer
//2. Memory locations which are immediate values : Work on parser. (Lexer will give it as an immediate value)
//3. Error handling - Not written so far assuming we get proper instructions because we will be parsing already made executables.
//4. Adding support for more instructions
//=======================================LEXER START========================================//
//This is bad!
FILE *fp;
enum {
tok_eof = -1,
tok_inst_mov = -2,
tok_inst_push = -3,
tok_inst_pop = -4,
tok_inst_add = -5,
tok_inst_sub = -6,
tok_inst_inc = -7,
tok_inst_dec = -8,
tok_inst_mul = -9,
tok_inst_div = -10,
tok_reg_64 = -11,
tok_reg_32 = -12,
tok_reg_16 = -13,
tok_reg_8 = -14,
tok_mem_64 = -15,
tok_mem_32 = -16,
tok_mem_16 = -17,
tok_mem_8 = -18,
tok_imm = -19,
tok_ret = -20,
tok_call = -21,
tok_label = -22,
tok_unknown = -23,
};
std::string IdentifierStr; //For both RegisterNames, Instructions, Memory, Labels.
std::string ImmValue; //For immediate values.
unsigned long int OpSize;
int gettok(FILE *fp) {
IdentifierStr.clear();
ImmValue.clear();
OpSize = 0;
char LastChar = ' ';
//Ignore all white spaces.
//Danger: The ',' parsing is not done properly.
while(isspace(LastChar) || LastChar == '\n' || LastChar == '\r')
LastChar = fgetc(fp);
//Simply eat up all comments.
if(LastChar == ';') {
do
LastChar = fgetc(fp);
while(LastChar != EOF && LastChar != '\n' && LastChar != '\r');
if(LastChar != EOF)
return gettok(fp);
}
//Identifiers - Instructions, Registers, Labels(Only alphabets(not even _))
if(isalpha(LastChar)) {
IdentifierStr = LastChar;
while(isalnum(LastChar = fgetc(fp)))
IdentifierStr += LastChar;
//Memory manipulation Instructions.
if(IdentifierStr == "mov")
return tok_inst_mov;
else if(IdentifierStr == "pop")
return tok_inst_pop;
else if(IdentifierStr == "push")
return tok_inst_push;
//ALU Instructions.
else if(IdentifierStr == "add")
return tok_inst_add;
else if(IdentifierStr == "sub")
return tok_inst_sub;
else if(IdentifierStr == "mul")
return tok_inst_mul;
else if(IdentifierStr == "div")
return tok_inst_div;
else if(IdentifierStr == "inc")
return tok_inst_inc;
else if(IdentifierStr == "dec")
return tok_inst_dec;
//ret
else if(IdentifierStr == "ret")
return tok_ret;
//call
else if(IdentifierStr == "call")
return tok_call;
//64-bit registers.
else if(IdentifierStr == "rax" ||
IdentifierStr == "rbx" ||
IdentifierStr == "rcx" ||
IdentifierStr == "rdx" ||
IdentifierStr == "rsi" ||
IdentifierStr == "rdi" ||
IdentifierStr == "rsp" ||
IdentifierStr == "rbp" ||
IdentifierStr == "r8" ||
IdentifierStr == "r9" ||
IdentifierStr == "r10" ||
IdentifierStr == "r11" ||
IdentifierStr == "r12" ||
IdentifierStr == "r13" ||
IdentifierStr == "r14" ||
IdentifierStr == "r15") {
OpSize = 64;
return tok_reg_64;
}
//32-bit subregisters.
else if(IdentifierStr == "eax" ||
IdentifierStr == "ebx" ||
IdentifierStr == "ecx" ||
IdentifierStr == "edx" ||
IdentifierStr == "esi" ||
IdentifierStr == "edi" ||
IdentifierStr == "r8d" ||
IdentifierStr == "r9d" ||
IdentifierStr == "r10d" ||
IdentifierStr == "r11d" ||
IdentifierStr == "r12d" ||
IdentifierStr == "r13d" ||
IdentifierStr == "r14d" ||
IdentifierStr == "r15d") {
OpSize = 32;
return tok_reg_32;
}
//16-bit subregisters.
else if(IdentifierStr == "ax" ||
IdentifierStr == "bx" ||
IdentifierStr == "cx" ||
IdentifierStr == "dx" ||
IdentifierStr == "si" ||
IdentifierStr == "di" ||
IdentifierStr == "r8w" ||
IdentifierStr == "r9w" ||
IdentifierStr == "r10w" ||
IdentifierStr == "r11w" ||
IdentifierStr == "r12w" ||
IdentifierStr == "r13w" ||
IdentifierStr == "r14w" ||
IdentifierStr == "r15w") {
OpSize = 16;
return tok_reg_16;
}
//8-bit subregisters.
else if(IdentifierStr == "al" ||
IdentifierStr == "bl" ||
IdentifierStr == "cl" ||
IdentifierStr == "dl" ||
IdentifierStr == "sil" ||
IdentifierStr == "dil"||
IdentifierStr == "r8b" ||
IdentifierStr == "r9b" ||
IdentifierStr == "r10b" ||
IdentifierStr == "r11b" ||
IdentifierStr == "r12b" ||
IdentifierStr == "r13b" ||
IdentifierStr == "r14b" ||
IdentifierStr == "r15b") {
OpSize = 8;
return tok_reg_16;
}
//Label of the form LabelName: (No Spaces between LabelName and :)
else if(LastChar == ':') {
return tok_label;
}
else
cerr<<"Unknown Identifier: "<<IdentifierStr<<endl;
return tok_unknown;
}
//Immediate values.
if(isdigit(LastChar)) {
std::string NumStr;
char SecondChar;
SecondChar = fgetc(fp);
//Support for numbers of the form 0x....
//For now, this lexer doesn't support hex numbers of the form 1234H or 1234h
if(LastChar == '0' && SecondChar == 'x') {
NumStr += LastChar;
NumStr += SecondChar;
while(isdigit(LastChar = fgetc(fp)))
NumStr += LastChar;
}
//Support for normal decimal numbers.
else if(SecondChar != 'x') {
NumStr += LastChar;
NumStr += SecondChar;
while(isdigit(LastChar = fgetc(fp)))
NumStr += LastChar;
}
ImmValue = NumStr;
return tok_imm;
}
//Check for end of file
else if(LastChar == EOF)
return tok_eof;
//Important (This is the ',' between 1st and 2nd operand)
else if((LastChar = getchar()) == ',')
return gettok(fp);
}
//===================================LEXER END===================================//
//===================================AST CLASSES=================================//
//=======================Memory Menipulation Instructions===================//
//Instruction base class
class instAST {
public:
~instAST() {}
};
//=========mov start!==========//
/*
//mov base class
class movAST {
public:
virtual ~movAST();
};
*/
//There are 5 categories in mov classfied based on the operands.
//mov DstReg, SrcReg
class movRegRegAST : public instAST {
std::string DstReg;
std::string SrcReg;
char size;
public:
movRegRegAST(std::string &Reg1, std::string &Reg2, char s)
: DstReg(Reg1), SrcReg(Reg2), size(s) {}
};
//mov DstReg, SrcMem
class movRegMemAST : public instAST {
std::string DstReg;
std::string SrcMem;
char size;
public:
movRegMemAST(std::string &Reg, std::string &Mem, char s)
: DstReg(Reg), SrcMem(Mem), size(s) {}
};
//mov DstMem, SrcReg
class movMemRegAST : public instAST {
std::string DstMem;
std::string SrcReg;
char size;
public:
movMemRegAST(std::string &Mem, std::string &Reg, char s)
: DstMem(Mem), SrcReg(Reg), size(s){}
};
//mov DstMem, SrcImm
class movMemImmAST : public instAST {
std::string DstMem;
std::string SrcImm;
char size;
public:
movMemImmAST(std::string &Mem, std::string &Imm, char s)
: DstMem(Mem), SrcImm(Imm), size(s) {}
};
//mov DstReg, SrcImm
class movRegImmAST : public instAST {
std::string DstReg;
std::string SrcImm;
char size;
public:
movRegImmAST(std::string &Reg, std::string &Imm, char s)
: DstReg(Reg), SrcImm(Imm), size(s) {}
};
//========mov done!=========//
//========pop start!========//
/*
//pop base class
class popAST {
public:
~popAST() {}
};
*/
//There are 2 categories.
//pop Reg
class popRegAST : public instAST {
std::string DstReg;
public:
popRegAST(std::string &Reg) : DstReg(Reg) {}
};
//pop Mem
class popMemAST : public instAST {
std::string DstMem;
public:
popMemAST(std::string &Mem) : DstMem(Mem) {}
};
//==========pop done!============//
//==========push start!==========//
/*
//push base class
class pushAST {
public:
~pushAST() {}
};
*/
//There are 3 categories
//push Reg
class pushRegAST : public instAST {
std::string SrcReg;
public:
pushRegAST(std::string &Reg) : SrcReg(Reg) {}
};
//push Mem
class pushMemAST : public instAST {
std::string SrcMem;
public:
pushMemAST(std::string &Mem) : SrcMem(Mem) {}
};
//push Imm
class pushImmAST : public instAST {
std::string SrcImm;
public:
pushImmAST(std::string &Imm) : SrcImm(Imm) {}
};
//============push done!==============//
//==================ALU Instructions=================//
//==========add start!===========//
/*
//add base class
class addAST {
public:
~addAST() {}
};
*/
//There are 5 categories
//add Reg, Reg
class addRegRegAST : public instAST {
std::string DstReg;
std::string SrcReg;
char size;
public:
addRegRegAST(std::string &Reg1, std::string &Reg2, char s)
: DstReg(Reg1), SrcReg(Reg2), size(s) {}
};
//add Reg, Mem
class addRegMemAST : public instAST {
std::string DstReg;
std::string SrcMem;
char size;
public:
addRegMemAST(std::string &Reg, std::string &Mem, char s)
: DstReg(Reg), SrcMem(Mem), size(s) {}
};
//add Reg, Imm
class addRegImmAST : public instAST {
std::string DstReg;
std::string SrcImm;
char size;
public:
addRegImmAST(std::string &Reg, std::string &Imm, char s)
: DstReg(Reg), SrcImm(Imm), size(s) {}
};
//add Mem, Reg
class addMemRegAST : public instAST {
std::string DstMem;
std::string SrcReg;
char size;
public:
addMemRegAST(std::string &Mem, std::string &Reg, char s)
: DstMem(Mem), SrcReg(Reg), size(s) {}
};
//add Mem, Imm
class addMemImmAST : public instAST {
std::string DstMem;
std::string SrcImm;
char size;
public:
addMemImmAST(std::string &Mem, std::string &Imm, char s)
: DstMem(Mem), SrcImm(Imm), size(s) {}
};
//==============add done!================//
//==============sub start!===============//
/*
//sub base class
class subAST {
public:
~subAST() {}
};
*/
//There are 5 categories
//sub Reg, Reg
class subRegRegAST : public instAST {
std::string DstReg;
std::string SrcReg;
char size;
public:
subRegRegAST(std::string &Reg1, std::string &Reg2, char s)
: DstReg(Reg1), SrcReg(Reg2), size(s) {}
};
//sub Reg, Mem
class subRegMemAST : public instAST {
std::string DstReg;
std::string SrcMem;
char size;
public:
subRegMemAST(std::string &Reg, std::string &Mem, char s)
: DstReg(Reg), SrcMem(Mem), size(s) {}
};
//sub Reg, Imm
class subRegImmAST : public instAST {
std::string DstReg;
std::string SrcImm;
char size;
public:
subRegImmAST(std::string &Reg, std::string &Imm, char s)
: DstReg(Reg), SrcImm(Imm), size(s) {}
};
//sub Mem, Reg
class subMemRegAST : public instAST {
std::string DstMem;
std::string SrcReg;
char size;
public:
subMemRegAST(std::string &Mem, std::string &Reg, char s)
: DstMem(Mem), SrcReg(Reg), size(s) {}
};
//sub Mem, Imm
class subMemImmAST : public instAST {
std::string DstMem;
std::string SrcImm;
char size;
public:
subMemImmAST(std::string &Mem, std::string &Imm, char s)
: DstMem(Mem), SrcImm(Imm), size(s) {}
};
//==================sub done!================//
//==========inc start!================//
/*
//inc base class
class incAST {
public:
~incAST() {}
};
*/
//There are 2 variations
//inc Reg
class incRegAST : public instAST {
std::string Reg;
char size;
public:
incRegAST(std::string &R, char s) : Reg(R) , size(s){}
};
//inc Mem
class incMemAST : public instAST {
std::string Mem;
char size;
public:
incMemAST(std::string &M, char s) : Mem(M) , size(s){}
};
//===========inc done!=============//
//===========dec start!===============//
/*
//dec base class
class decAST {
public:
~decAST() {}
};
*/
//There are 2 variations.
//dec Reg
class decRegAST : public instAST {
std::string Reg;
char size;
public:
decRegAST(std::string &R, char s) : Reg(R) , size(s){}
};
//dec Mem
class decMemAST : public instAST {
std::string Mem;
char size;
public:
decMemAST(std::string &M, char s) : Mem(M) , size(s){}
};
//===============dec done!================//
//===============ret start!===============//
/*
//ret base class
class retAST {
public:
~retAST() {}
};
*/
//There are 4 variations.
//TODO: Learn what those variations mean.
//=============ret done!================//
//==============call start!================//
/*
//call base class
class callAST {
public:
~callAST() {}
};
*/
//There are 2 variations.
//call Reg
class callRegAST : public instAST {
std::string Reg;
public:
callRegAST(std::string &R) : Reg(R) {}
};
//call Imm
class callImmAST : public instAST {
std::string Imm;
public:
callImmAST(std::string &I) : Imm(I) {}
};
instAST *inst;
//===============call done!=====================//
//===============Label(Similar to a function)==================//
class labelAST {
std::string LabelName;
std::vector<instAST*> Inst;
public:
labelAST(std::string Name){
LabelName = Name;
}
void AddInst(instAST* I) {
Inst.push_back(I);
}
};
labelAST *label;
//=============Label done!=====================//
//=============root start!=======================//
class rootAST {
std::vector<labelAST *> Label;
public:
rootAST() {}
void AddLabel(labelAST* L) {
Label.push_back(L);
}
};
static rootAST *Root;
//===============root done!=====================//
//====================AST CLASSES DONE=========================//
//====================Parser Helper functions===========//
//Token Buffer
static int CurTok;
static int getNextToken() {
CurTok = gettok(fp);
return CurTok;
}
//3 Error handling routines.
std::unique_ptr<instAST> LogError(const char *Str) {
fprintf(stderr, "LogError: %s\n", Str);
return nullptr;
}
std::unique_ptr<labelAST> LogErrorL(const char *Str) {
LogError(Str);
return nullptr;
}
std::unique_ptr<rootAST> LogErrorR(const char *Str) {
LogError(Str);
return nullptr;
}
instAST *ptr;
//Parsing mov instruction.
void ParseMov(labelAST *label) {
cout<<"Parsing mov"<<endl;
int tok1, tok2;
std::string str1, str2;
unsigned long int size;
tok1 = getNextToken();
str1 = IdentifierStr;
size = OpSize;
cout<<"ParseMov: tok1 = "<<CurTok<<", str1 = "<<str1<<", size = "<<size<<endl;
tok2 = getNextToken();
if(IdentifierStr.empty() && !ImmValue.empty() )
str2 = ImmValue;
else if(!IdentifierStr.empty() && ImmValue.empty())
str2 = IdentifierStr;
cout<<"ParseMov: tok2 = "<<CurTok<<", str2 = "<<str2<<", size = "<<size<<endl;
//64-bit mov
if(tok1 == tok_reg_64) {
if(tok2 == tok_reg_64)
ptr = new movRegRegAST(str1, str2, size);
else if(tok2 == tok_mem_64)
ptr = new movRegMemAST(str1, str2, size);
else if(tok2 == tok_imm)
ptr = new movRegImmAST(str1, str2, size);
}
else if(tok1 == tok_mem_64) {
if(tok2 == tok_reg_64)
ptr = new movMemRegAST(str1, str2, size);
else if(tok2 == tok_imm)
ptr = new movMemImmAST(str1, str2, size);
}
//32-bit mov
if(tok1 == tok_reg_32) {
if(tok2 == tok_reg_32)
ptr = new movRegRegAST(str1, str2, size);
else if(tok2 == tok_mem_32)
ptr = new movRegMemAST(str1, str2, size);
else if(tok2 == tok_imm)
ptr = new movRegImmAST(str1, str2, size);
}
else if(tok1 == tok_mem_32) {
if(tok2 == tok_reg_32)
ptr = new movMemRegAST(str1, str2, size);
else if(tok2 == tok_imm)
ptr = new movMemImmAST(str1, str2, size);
}
//16-bit mov
if(tok1 == tok_reg_16) {
if(tok2 == tok_reg_16)
ptr = new movRegRegAST(str1, str2, size);
else if(tok2 == tok_mem_16)
ptr = new movRegMemAST(str1, str2, size);
else if(tok2 == tok_imm)
ptr = new movRegImmAST(str1, str2, size);
}
else if(tok1 == tok_mem_16) {
if(tok2 == tok_reg_16)
ptr = new movMemRegAST(str1, str2, size);
else if(tok2 == tok_imm)
ptr = new movMemImmAST(str1, str2, size);
}
//8-bit mov
if(tok1 == tok_reg_8) {
if(tok2 == tok_reg_8)
ptr = new movRegRegAST(str1, str2, size);
else if(tok2 == tok_mem_8)
ptr = new movRegMemAST(str1, str2, size);
else if(tok2 == tok_imm)
ptr = new movRegImmAST(str1, str2, size);
}
else if(tok1 == tok_mem_8) {
if(tok2 == tok_reg_8)
ptr = new movMemRegAST(str1, str2, size);
else if(tok2 == tok_imm)
ptr = new movMemImmAST(str1, str2, size);
}
label->AddInst(ptr);
getNextToken();
}
//Parsing pop instruction.
void ParsePop(labelAST *label) {
getNextToken();
cout<<"ParsePop: CurTok = "<<CurTok<<", IdentifierStr = "<<IdentifierStr<<endl;
if(CurTok == tok_reg_64) {
ptr = new popRegAST(IdentifierStr);
}
else if(CurTok == tok_mem_64) {
ptr = new popMemAST(IdentifierStr);
}
label->AddInst(ptr);
getNextToken();
}
//Parsing push instruction.
void ParsePush(labelAST *label) {
getNextToken();
if(CurTok == tok_reg_64) {
ptr = new pushRegAST(IdentifierStr);
}
else if(CurTok == tok_mem_64) {
ptr = new pushMemAST(IdentifierStr);
}
else if(CurTok == tok_imm) {
ptr = new pushImmAST(IdentifierStr);
}
cout<<"ParsePush: CurTok = "<<CurTok<<", "<<"IdentifierStr = "<<IdentifierStr<<", ImmValue = "<<ImmValue<<endl;
label->AddInst(ptr);
getNextToken();
}
//Parsing add instruction.
void ParseAdd(labelAST *label) {
std::string str1, str2;
int tok1, tok2;
int size;
tok1 = getNextToken();
str1 = IdentifierStr;
size = OpSize;
tok2 = getNextToken();
if(tok2 == tok_imm)
str2 = ImmValue;
else
str2 = IdentifierStr;
cout<<"ParseAdd: Operand1 = "<<str1<<", Operand2 = "<<str2<<endl;
//64-bit operands.
if(tok1 == tok_reg_64) {
if(tok2 == tok_reg_64) {
ptr = new addRegRegAST(str1, str2, size);
}
else if(tok2 == tok_mem_64) {
ptr = new addRegMemAST(str1, str2, size);
}
else if(tok2 == tok_imm) {
ptr = new addRegImmAST(str1, str2, size);
}
}
else if(tok1 == tok_mem_64) {
if(tok2 == tok_reg_64) {
ptr = new addMemRegAST(str1, str2, size);
}
else if(tok2 == tok_imm) {
ptr = new addMemImmAST(str1, str2, size);
}
}
//32-bit Operands.
if(tok1 == tok_reg_32) {
if(tok2 == tok_reg_32) {
ptr = new addRegRegAST(str1, str2, size);
}
else if(tok2 == tok_mem_32) {
ptr = new addRegMemAST(str1, str2, size);
}
else if(tok2 == tok_imm) {
ptr = new addRegImmAST(str1, str2, size);
}
}
else if(tok1 == tok_mem_32) {
if(tok2 == tok_reg_32) {
ptr = new addMemRegAST(str1, str2, size);
}
else if(tok2 == tok_imm) {
ptr = new addMemImmAST(str1, str2, size);
}
}
//16-bit Operands
if(tok1 == tok_reg_16) {
if(tok2 == tok_reg_16) {
ptr = new addRegRegAST(str1, str2, size);
}
else if(tok2 == tok_mem_16) {
ptr = new addRegMemAST(str1, str2, size);
}
else if(tok2 == tok_imm) {
ptr = new addRegImmAST(str1, str2, size);
}
}
else if(tok1 == tok_mem_16) {
if(tok2 == tok_reg_16) {
ptr = new addMemRegAST(str1, str2, size);
}
else if(tok2 == tok_imm) {
ptr = new addMemImmAST(str1, str2, size);
}
}
//8-bit operands.