-
Notifications
You must be signed in to change notification settings - Fork 0
/
semantics.h
98 lines (88 loc) · 3.12 KB
/
semantics.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
#ifndef SEMANTICS_H_INCLUDED
#define SEMANTICS_H_INCLUDED
struct tblitem
{
enum obj_t { CONST, VAR, FUNCTION };
static const int NOUSE = 233;
int objtype; //type of the object, one of obj_t
string name; //name of the object
int datatype; //data type of a variable, item type of an array,
//and return type of a function. one of type_t
bool isarray; //if the object is an array
int value; //value of a constant number, index of string table,
//size of an array, index of function
int addr; //the address (offset) of the object
};
extern vector<tblitem> glbtbl;//global symbol table
extern vector<string> strtbl;//string table
struct qoperand
{
enum opr_t { IMD, GLB_OBJ, LCL_OBJ, TMP, LABEL, BLANK, STRING };
int type; //the type of the operand, one of opr_t
int value; //value of immediate number; index of object in table;
//No. of temp variables; No. of label
int datatype;//the data type of the operand, one of type_t
bool operator== (const qoperand &o) const { return (type==o.type && value==o.value); }
bool operator< (const qoperand &o) const
{
return ((type!=o.type)? (type<o.type): (value<o.value));
}
};
struct qi
{
enum oprt_t {
ADD, SUB, NEG, MUL, DIV, JLT, JGT, JLE, JGE, JEQ, JNE, JZ, JMP,
ASSIGN, ARRAYLOAD, ARRAYASS, PUSH, CALL, RD, WR, RET, LABEL, NOP,
};
int op;
qoperand A, B, D;
};
struct funcitem
{
int index; //index of the function in glbtbl
int argnum; //amount of arguments
int varsize; //size of space used by local variables
int tmpamt; //amount of temp variables
//TODO: int stored_space;
vector<tblitem> lcltbl;//local symbol table
vector<qi> qilist;//quad-element instruction table
};
extern vector<funcitem> functbl;//function table
extern int context;
extern qoperand foundopr;
extern const qoperand BLANKOP;
void tbl_dump();
bool findidt(const string &nm);
bool isfunction(const string &idt);
bool insertobj(int objtyp, const string& nm, int typ, bool isarray, int val=0);
bool insertpara(int typ, const string &nm);
void buildcontext(int rettyp, const string& nm);
void loadcontext(int ct);
void exitcontext();
qoperand newtmp(int type);
qoperand arrayload(qoperand arropr, qoperand index);
void arrayass(qoperand arr, qoperand index, qoperand val);
void assign(qoperand var, qoperand val);
qoperand muldiv(int op, qoperand f1, qoperand f2);
qoperand addsub(int op, qoperand t1, qoperand t2);
qoperand neg(qoperand t);
void push(qoperand arg);
qoperand call(qoperand func);
void ret(bool withvalue, qoperand value);
void jz(qoperand cond, qoperand lbl);
void jifnot(int op, qoperand e1, qoperand e2, qoperand lbl);
void setlabel(qoperand lbl);
void jmp(qoperand lbl);
void rd(qoperand opr);
qoperand insertstr(const string &str);
void wr(qoperand opr);
qoperand newlabel();
void checkmain();
static inline tblitem &getitem(qoperand opr)
{
return (opr.type == qoperand::GLB_OBJ)?
glbtbl.at(opr.value):
functbl.at(context).lcltbl.at(opr.value);
}
string qotostr(qoperand qo);
#endif // SEMANTICS_H_INCLUDED