-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.h
2866 lines (2532 loc) · 81.2 KB
/
ast.h
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
/*-----------------------------------------------------------------*
* Defines the class Node for the Abstract Syntax Tree. Other, *
* special types of nodes will inherit from this class. All nodes *
* have a pointer to their Parent, their lexical location and *
* a function to print the subtree rooted at them in-order, along *
* with other members. *
*-----------------------------------------------------------------*/
#ifndef AST_H_
#define AST_H_
#include <stdlib.h> // for NULL
#include "location.h"
#include "list.h"
#include <bits/stdc++.h> // later replace by specific header
// TODO: For now the unsigned versions of operators are same as the signed versions.
// Fix this later.
/**
* The base class from which all other classes inherit.
* Defines some (virtual) functions common to every class.
*/
class Node{
protected:
yyltype *location; /**< Holds the lineno and colno of the starting of the class in-code. */
Node *parent; /**< A pointer to the parent of this node in the AST. */
public:
/**
* A plain constructor. Nothing to see here.
* @param loc the location of the first token which
* is part of this class.
*/
Node(yyltype loc);
/**
* Another plain constructor. Nothing to see here.
*/
Node();
virtual ~Node() {}
/**
* Returns the location of (the beginning of) this class.
*/
yyltype *GetLocation() {
return this->location;
}
/**
* A helper function to set the parent of the class.
* @param p The class which must be set as parent.
*/
void SetParent(Node *p) {
this->parent = p;
}
/**
* Returns the parent of the class.
*/
Node *GetParent() {
return this->parent;
}
/**
* Returns the name to be printed for the node. Should
* be overriden by inheriting classes to their respective
* names.
*/
virtual const char *GetPrintNameForNode() = 0;
/**
* Prints the preorder traversal of the abstract syntax (sub)tree
* rooted at this node. Must _not_ be overriden by inheriting
* classes: they must override PrintChildren instead. Prints
* only the name of the node (rest is printed in PrintChildren)
* @param indentlevel The indentation level for printing the name,
* determined by the depth of the node in the AST.
* @param label A label that must be prepened before the class name.
* Not used for now.
*/
void Print(int indentlevel, const char *label = NULL);
/**
* Prints the extra information about the specific class and then calls
* Print() on its children. This function _must_ be overriden by inheriting classes.
* @param indentlevel The indentation level for printing the name,
* determined by the depth of the node in the AST.
*/
virtual void PrintChildren(int indentlevel) {}
/**
* Performs some basic analysis of the class, such as listing
* the functional calls and updating the parameter types for the function calls.
* Also adds entries to the global symbol table when called for functions.
* Must be overriden by inheriting classes.
*/
virtual void Analyze() {}
/**
* Performs the code generation of the decompiled c code. Then proceeds
* to call GenerateCode() on the child classes recursively. Must be
* overriden by inheriting classes
* @param indentlevel The indentation level for printing the name,
* determined by the depth of the node in the AST.
*/
virtual string GenerateCode(int indentlevel) { return ""; }
};
class Error : public Node
{
public:
Error() : Node() {}
const char *GetPrintNameForNode() { return "Error"; }
};
class FuncBodyList;
class FuncBody;
class StmtList;
class Stmt;
class MainCmd;
class Operand;
class LocInfo;
class TypeInfo;
class Expr;
class MemType;
class Flags;
class Flag;
class Dest;
class Comparison;
class Condition;
class ExprList;
/**
* The root node of the entire AST. Represents the whole program. Composed of
* function bodies, of which the last one is (unless there is no main()) the
* main() function.
*/
class Program : public Node {
protected:
/**
* The list of function bodies in the program.
*/
List<FuncBody *> *funcbodylist;
public:
/**
* A simple constructor.
* @param bodylist The list of function bodies to be assigned to funcbodylist
*/
Program(List<FuncBody *> *bodylist);
/**
* Returns the name of the (type of) node.
*/
const char *GetPrintNameForNode() {
return "Program";
}
/**
* Calls Print() on the children of this node.
* @param indentlevel indentation level of the current node in accordance with
* depth in the AST.
*/
void PrintChildren(int indentlevel);
/**
* There is no analysis to be done at this stage, so just calls Analyze()
* on the children.
*/
void Analyze();
/**
* Generates code for the whole program. Prints the preamble code and then
* proceeds to call GenerateCode() for the function bodies in turn.
* @param indentlevel The indentation level of the code to be printed. Is
* typically 0 at the program level.
*/
string GenerateCode(int indentlevel);
};
/**
* The class representing a function and its body (not only the body, despite
* the confusing name). Holds information about the name of the function, the
* number and types of parameters, the return value and its type; and has pointers
* to the statements of its body which constitute the node's children.
*/
class FuncBody : public Node {
protected:
/**
* The statements that form the body of the function.
*/
List<Stmt *> *stmts;
/**
* A list of types, where the n-th type is the type of the n-th argument
* that is passed to the function.
*/
List<string> *types;
/**
* The list of register names that the function expects to be passed as
* parameters, in the order of being passed (in the reverse order of the
* declaration in the source file).
*/
List<int> *regs;
/**
* The name with which the function was declared in the source file, which
* will be subsequently used when calling the function.
*/
const char *name;
/**
* The number of arguments that are to be passed to the function.
*/
int numArgs;
/**
* The size on the stack that must be reserved for the parameters and temporary
* variables before executing member statements.
*/
int sz;
/**
* The return type of the function.
*/
string retType;
public :
/**
* A simple constructor.
* @param ss The list of statements to be assigned to the function
* @param n The name to be assigned to the function.
*/
FuncBody(List<Stmt *> *ss, const char *n);
/**
* Returns the name of (the type of) class, i.e. FuncBody.
*/
const char *GetPrintNameForNode() {
return "FuncBody";
}
/**
* Prints class-specific information and then calls Print()
* on the children of this node in the AST.
* * @param indentlevel indentation level of the current node in accordance with
* depth in the AST.
*/
void PrintChildren(int indentlevel);
/**
* Adds an entry to the global symbol table for this function, and then
* calls Analyze() on the children. Note that the number and types of
* args can only be inferred upon finding a call to this function
* which we facilitate using the global symbol table.
*/
void Analyze();
/**
* Called by the body of Analyze() in call statements. Used to set the
* number, types and the registers of the arguments to this function,
* along with the return type.
* @param types A list of types of arguments to the function, in the same
* order as regs (i.e. the order or passing and the reverse order of declaration)
* @param regs The registers in which the respective arguments are passed.
* @param rtype The return type that should be assigned to the function.
*/
void setTypes(List<string> *types, List<int> *regs, string rtype);
/**
* Generates code for the function. Prints the part such as void f(int a, ...) {
* and also the return x; } part. Also checks for special names of functions
* such as main(), printf() and scanf() and takes special actions for them. Then
* calls GenerateCode() on the children.
* @param indentlevel The level of indentation of the printed code.
*/
string GenerateCode(int indentlevel);
};
/**
* The basic class defining a statement of code. The subsequent types of statements
* derive from this class. Since the only function of the class is to act as an
* umbrella for the deriving classes, no new functions are defined here.
*/
class Stmt : public Node {
public:
/**
* For the constructors we can just call the super's constructor.
*/
Stmt() : Node() {}
/**
* @param loc the location of the first token which
* is part of this class.
*/
Stmt(yyltype loc) : Node(loc) {}
};
/**
* A note is a type of statement in GCC RTL which marks specific boundaries,
* such as the beginning or end of a code block, and holds the numbers of
* the next and previous code block. However, it has no impact
* on the decompiled code.
*/
class Note : public Stmt {
public :
/**
* A simple constructor: just call the one from super()
*/
Note() : Stmt() {}
/**
* @param loc the location of the first token which
* is part of this class.
*/
Note(yyltype loc) : Stmt(loc) {} // We don't care about the notes
/**
* Returns the name to be printed for this node.
*/
const char *GetPrintNameForNode() {
return "Note";
}
/**
* Calls Print() over the node's children.
* @param indentlevel The level of indentation at which the name of the node
* is to be printed, in accordance with the depth in the AST.
*/
void PrintChildren(int indentlevel);
/**
* Analyze does not do anything for a note, since anyway we will print it as
* a comment.
*/
void Analyze() { }
/**
* The generated code for a note is a just a comment saying that there was
* a note here.
*/
string GenerateCode(int indentlevel);
};
/**
* A barrier is another type of statement in RTL. Once again
* it has little of interest to us, since it primarily acts as a
* marker to tell GCC to not (re)order statements between the two
* portions separated by the barrier.
*/
class Barrier : public Stmt {
public:
/**
* We simply call the super's constructor.
*/
Barrier() : Stmt() {}
/**
* @param loc the location of the first token which
* is part of this class.
*/
Barrier(yyltype loc) : Stmt() {}
/**
* Returns the name to be printed for this node in the
* AST.
*/
const char *GetPrintNameForNode() {
return "Barrier";
}
/**
* Does nothing, since a barrier does not have any
* children in the AST.
*/
void PrintChildren(int indentlevel);
/**
* Again, does nothing since a barrier will also be printed
* only as a comment in the decompiled code.
*/
void Analyze() { }
/**
* Generates a comment saying that there existed a barrier here.
*/
string GenerateCode(int indentlevel);
};
/**
* A code label in RTL acts like a label in assembly: it gives the
* program a place to jump to.
*/
class CodeLabel : public Stmt {
protected:
/**
* In our decompiled code we just print labelxyz for label
* number xyz where the xyz is the number of the label.
* This value is stored in labelno.
*/
int labelno;
public:
/**
* The constructor just copies the label number from the
* argument to the class member.
* @param lno The label number to assign to the code label.
*/
CodeLabel(int lno) { labelno = lno; }
/**
* Returns the name to be printed for this node
* in the AST.
*/
const char *GetPrintNameForNode() {
return "CodeLabel";
}
/**
* Does nothing since a code label does not have children
* in the AST.
*/
void PrintChildren(int indentlevel);
/**
* Does nothing since there is no information to be
* extracted from a code label which will be used in the
* code generation phase.
*/
void Analyze() { }
/**
* Generates the c code for the label. Typically label number x
* becomes the (properly indented) statement "labelx:\n"
* @param indentlevel The level of indentation at which the code
* must be printed.
*/
string GenerateCode(int indentlevel);
};
/**
* A common umbrella class for both 'IntConstant' and '-IntConstant' types
* of operands. Represents an integer.
*/
class Integer : public Node {
protected:
/**
* The value of the represented integer.
*/
int value;
public:
/**
* Set's value to val.
* @param val the value to be assigned to the integer.
*/
Integer(int val);
/**
* Returns the name to be printed for the class.
*/
const char *GetPrintNameForNode() {
return "Integer";
}
// The difference of Integer from T_IntConstant is that Integer also allows negative integers
// If the integer is negative then remember to pass -yylval.intconstant instead of +
/**
* Does nothing since an Integer does not have children in the
* AST.
*/
void PrintChildren(int indentlevel);
/**
* A helper function which returns the value of the
* represented integer.
*/
int getValue() { return value; }
/**
* There is no analysis to be done for an integer.
*/
void Analyze() { }
};
/**
* An instruction is the basic unit of code in GCC RTL along with jump_insn.
* It is further divided into types of statements which are collected under
* the umbrella of a main command.
*/
class Insn : public Stmt {
protected:
/**
* The command represented by the statement.
*/
MainCmd *maincmd;
public:
/**
* A simple constructor.
* @param mc the main command of this statement.
*/
Insn(MainCmd *mc);
/**
* Returns the name to print for this node when
* printing the AST.
*/
const char *GetPrintNameForNode() {
return "Insn";
}
/**
* Calls Print() on the main command of the statement.
*/
void PrintChildren(int indentlevel);
/**
* Simply calls Analyze() on the main command.
*/
void Analyze() { }
/**
* Again, simply calls GenerateCode(indentlevel) on the
* main command.
*/
string GenerateCode(int indentlevel);
};
/**
* The main command in a statement. It is just an umbrella over the various
* types of commands, which each inherit from it. Subsequently, no extra
* function is defined in this class.
*/
class MainCmd : public Node {
public:
/**
* A simple constructor.
*/
MainCmd() : Node() {}
/**
* @param loc the location of the first token which
* is part of this class.
*/
MainCmd(yyltype loc) : Node(loc) {}
// MainCmd is of two types so we define nothing here
};
/**
* A plain command is the first type of main command. It consists of
* simply a single command (in contrast with the parallel command which
* has multiple commands to be executed in paralllel). Since the PlainCmd
* once again is of many types, we create a class for each type that inherit
* from it.
*/
class PlainCmd : public MainCmd {
public:
/**
* We just call the constructor of the super class.
*/
PlainCmd() : MainCmd() {}
/**
* @param loc the location of the first token which
* is part of this class.
*/
PlainCmd(yyltype loc) : MainCmd(loc) {}
// Again, PlainCmd is of three types so we define nothing here
};
/**
* A parallel command is the other type of a main command. It consists of many
* individual commands to be executed in parallel, of which the last is usually
* (but not always) a clobber command.
*/
class ParallelCmd : public MainCmd {
protected:
/**
* The list of commands to be executed in parallel.
*/
List<PlainCmd *> *cmds;
public:
/**
* A simple constructor.
* @param cs The list of commands to be assigned to cmds.
*/
ParallelCmd(List<PlainCmd *> *cs);
/**
* Returns the name to be printed for this class while
* printing the AST.
*/
const char *GetPrintNameForNode() {
return "ParallelCmd";
}
/**
* Calls print on each of the commands in cmds in turn.
* @param indentlevel The indentation level at which the current class (not
* the children) must be printed, in accordance with the depth in the AST.
*/
void PrintChildren(int indentlevel);
/**
* Simply calls Analyze() on each of the commands in cmds in turn.
*/
void Analyze() { }
/**
* Generates code for each of the commands in cmds, in turn.
*/
string GenerateCode(int indentlevel);
};
/**
* A command used in RTL to clobber the contents of a register,
* i.e. to indicate that the value in the register is no
* longer safe to use. Does not impact the decompilation process.
*/
class ClobberCmd : public PlainCmd {
public:
/**
* We just call the constructor of the super.
*/
ClobberCmd() : PlainCmd() {}
/**
* @param loc the location of the first token which
* is part of this class.
*/
ClobberCmd(yyltype loc) : PlainCmd(loc) {}
/**
* Returns the name to be printed for this class.
*/
const char *GetPrintNameForNode() {
return "ClobberCmd";
}
/**
* Does nothing since a clobber command does not have children
* in the AST.
* @param indentlevel The level of indentation at which this particular
* class (not its children) must be printed.
*/
void PrintChildren(int indentlevel);
/**
* Does nothing since there is no analysis to be
* done in a clobber command.
*/
void Analyze() { }
/**
* Generates a comment indicating that there was a clobber command
* here.
* @param indentlevel The level of indentation at which the comment
* must be printed.
*/
string GenerateCode(int indentlevel);
};
/**
* A set command sets the value of the left operand to the value
* of the right operand.
*/
class SetCmd : public PlainCmd {
protected:
/**
* The operands
*/
Operand *op1, *op2;
public:
/**
* @param o1 The left operand.
* @param o2 The right operand.
*/
SetCmd(Operand *o1, Operand *o2);
/**
* Returns the name to be printed for this node.
*/
const char *GetPrintNameForNode() {
return "SetCmd";
}
/**
* Calls Print() on the children of this node, i.e.
* the left and right operands.
* @param indentlevel The level of indentation at which the
* current node's name must be printed.
*/
void PrintChildren(int indentlevel);
void Analyze() { }
/**
* Generates code for the set command. First calls GenerateCommand()
* on the two operands, and then takes the returned names of the respective
* "addresses" (which may be temporaries such as "temp1" or registers such
* as "regs[24]") and sets the first to the second.
*/
string GenerateCode(int indentlevel);
};
/**
* A use command denotes the end of the current function.
*/
class UseCmd : public PlainCmd {
public:
/**
* A simple constructor.
*/
UseCmd() : PlainCmd() {}
/**
* @param loc the location of the first token which
* is part of this class.
*/
UseCmd(yyltype loc) : PlainCmd(loc) {}
/**
* Returns the name to be printed for this node.
*/
const char *GetPrintNameForNode() {
return "UseCmd";
}
/**
* Doesn't do anything since a use command has no children
* in the AST.
* @param indentlevel The level of indentation at which the current
* node's name must be printed.
*/
void PrintChildren(int indentlevel);
/**
* No analysis to be done for use commands.
*/
void Analyze() { }
/**
* Since use commands are primarily markers, no code
* needs to be generated for them.
*/
string GenerateCode(int indentlevel) { return ""; }
};
/**
* A operand to be used in a statement. It can be of various types,
* so the current class is just an umbrella class from which
* other classes inherit.
*/
class Operand : public Node {
public:
/**
* We just call the constructor of the super class.
*/
Operand() : Node() {}
/**
* @param loc the location of the first token which
* is part of this class.
*/
Operand(yyltype loc) : Node(loc) {}
// There are 3 possibilities for operand, as below
};
/**
* The most basic type of operand: just an integer constant.
*/
class IntOperand : public Operand {
protected:
/**
* The value of the integer.
*/
int intvalue;
public:
/**
* A simple constructor.
* @param iv The value to be assigned to the integer,
*/
IntOperand(int iv);
/**
* Returns the name to be printed for the current node.
*/
const char *GetPrintNameForNode() {
return "IntOperand";
}
/**
* Does nothing since an integer operand has no children in the
* AST.
* @param indentlevel The level of indentation at which to print
* the current node.
*/
void PrintChildren(int indentlevel);
/**
* No analysis to be done for an integer expression.
*/
void Analyze() { }
/**
* The code generated for an integer expression is just the value
* of the expression, which is passed upward to be used in the
* parent statements/expressions.
* @param indentlevel The level of indentation at which to print
* the code for the current class.
*/
string GenerateCode(int indentlevel) { return to_string(intvalue); }
};
/**
* An expression operand consists of an expression (which could be of
* various types as below) and location and type information which decide
* the width and hence the type into which the resultant value of the
* expression must be cast.
*/
class ExprOperand : public Operand {
protected:
/**
* Information about the location, i.e. memory/register
* and flags.
*/
LocInfo *linfo;
/**
* Type information (i.e. width), i.e. single integer, double integer
* etc.
*/
TypeInfo *tinfo;
/**
* The expression itself.
*/
Expr *expr;
public:
/**
* A simple constructor.
* @param li The location information to be assigned to linfo.
* @param ti The type information to be assigned to tinfo.
* @param e The expression.
*/
ExprOperand(LocInfo *li, TypeInfo *ti, Expr *e);
/**
* Returns the name to be printed for this node.
*/
const char *GetPrintNameForNode() {
return "ExprOperand";
}
/**
* Calls Print() on the children of this node.
* @param indentlevel The level of indentation at which the current
* node must be printed.
*/
void PrintChildren(int indentlevel);
/**
* No analysis to be done for an expression operand.
*/
void Analyze() { }
/**
* Generates code for the expression operand, such that the value is
* stored in a new temporary variable. Returns the name of the temporary
* variable.
* @param indentlevel The level of indentation for the generated code.
*/
string GenerateCode(int indentlevel);
};
/**
* An operand which extends the result of another operand to
* a higher width (which is given by typeinfo)
*/
class ExtendOperand : public Operand {
protected:
/**
* The width to which the inner operand is extended.
*/
TypeInfo *tinfo;
/**
* The inner operand.
*/
Operand *op;
public:
/**
* A simple constructor.
* @param ti The type information to be assigned to tinfo.
* @param o The inner operand.
*/
ExtendOperand(TypeInfo *ti, Operand *o);
/**
* The name to be printed for the current node.
*/
const char *GetPrintNameForNode() {
return "ExtendOperand";
}
/**
* Calls Print() on the children of this node in the AST.
* @param indentlevel The level of indentation at which the
* current node must be printed.
*/
void PrintChildren(int indentlevel);
/**
* No analysis to be done for ExtendOperand.
*/
void Analyze() { }
/**
* Generates code to extend the value of the inner operand
* to the specified width, and returns the name of the temporary
* variable in which the result is stored.
* @param indentlevel The level of indentation at which the code
* is generated.
*/
string GenerateCode(int indentlevel);
};
/**
* A dereference operand dereferences the address depicted by the inner operand
* and reads a value of the specified width from it.
*/
class DerefOperand : public Operand {
protected:
/**
* Information about location, i.e. reg or mem.
*/
LocInfo *linfo;
/**
* Type information, i.e. width: single integer, double integer etc.
*/
TypeInfo *tinfo;
/**
* The inner operand.
*/
Operand *op;
public:
/**
* A simple constructor.
* @param li The location information.
* @param ti The type information.
* @param o The inner operand.
*/
DerefOperand(LocInfo *li, TypeInfo *ti, Operand *o);
/**
* Returns the name to be printed for the current node.
*/
const char *GetPrintNameForNode() {
return "DerefOperand";
}
/**
* Calls Print() on the children of this node in the AST.
*/
void PrintChildren(int indentlevel);
/**
* No analysis to be done for a DerefOperand.
*/
void Analyze() { }
/**
* Generates code for dereferencing the address pointed to by
* the inner operand, and returns the value of the temporary variable
* in which the result is stored.
* @param indentlevel The level of indentation at which the code is generated.
*/
string GenerateCode(int indentlevel);
};
/**
* An operand which denotes the variable which has the
* specified symbol associated with it.
*/
class SymbolRefOperand : public Operand {
protected:
/**
* The symbol which specifies the variable/value.
*/
const char *symbol;
public:
/**
* A simple constructor.
* @param s The name of the symbol.
*/
SymbolRefOperand(const char *s);
/**
* Returns the name to be printed for this node.
*/
const char *GetPrintNameForNode() {
return "SymbolRefOperand";