Skip to content
This repository was archived by the owner on Aug 7, 2022. It is now read-only.

Commit 8813cfc

Browse files
author
tinsir888
authored
Add files via upload
1 parent 84e38e0 commit 8813cfc

36 files changed

+3454
-0
lines changed

include/AsmBuilder.h

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef __ASMBUILDER_H__
2+
#define __ASMBUILDER_H__
3+
4+
#include "MachineCode.h"
5+
6+
7+
class AsmBuilder
8+
{
9+
private:
10+
MachineUnit* mUnit; // mahicne unit
11+
MachineFunction* mFunction; // current machine code function;
12+
MachineBlock* mBlock; // current machine code block;
13+
int cmpOpcode; // CmpInstruction opcode, for CondInstruction;
14+
public:
15+
void setUnit(MachineUnit* unit) { this->mUnit = unit; };
16+
void setFunction(MachineFunction* func) { this->mFunction = func; };
17+
void setBlock(MachineBlock* block) { this->mBlock = block; };
18+
void setCmpOpcode(int opcode) { this->cmpOpcode = opcode; };
19+
MachineUnit* getUnit() { return this->mUnit; };
20+
MachineFunction* getFunction() { return this->mFunction; };
21+
MachineBlock* getBlock() { return this->mBlock; };
22+
int getCmpOpcode() { return this->cmpOpcode; };
23+
};
24+
25+
26+
#endif

include/Ast.h

+187
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
#ifndef __AST_H__
2+
#define __AST_H__
3+
4+
#include <fstream>
5+
#include "Operand.h"
6+
7+
class SymbolEntry;
8+
class Unit;
9+
class Function;
10+
class BasicBlock;
11+
class Instruction;
12+
class IRBuilder;
13+
14+
class Node
15+
{
16+
private:
17+
static int counter;
18+
int seq;
19+
protected:
20+
std::vector<Instruction*> true_list;
21+
std::vector<Instruction*> false_list;
22+
static IRBuilder *builder;
23+
void backPatch(std::vector<Instruction*> &list, BasicBlock*bb);
24+
std::vector<Instruction*> merge(std::vector<Instruction*> &list1, std::vector<Instruction*> &list2);
25+
26+
public:
27+
Node();
28+
int getSeq() const {return seq;};
29+
static void setIRBuilder(IRBuilder*ib) {builder = ib;};
30+
virtual void output(int level) = 0;
31+
virtual void typeCheck() = 0;
32+
virtual void genCode() = 0;
33+
std::vector<Instruction*>& trueList() {return true_list;}
34+
std::vector<Instruction*>& falseList() {return false_list;}
35+
};
36+
37+
class ExprNode : public Node
38+
{
39+
protected:
40+
SymbolEntry *symbolEntry;
41+
Operand *dst; // The result of the subtree is stored into dst.
42+
public:
43+
ExprNode(SymbolEntry *symbolEntry) : symbolEntry(symbolEntry){};
44+
Operand* getOperand() {return dst;};
45+
SymbolEntry* getSymPtr() {return symbolEntry;};
46+
};
47+
48+
class BinaryExpr : public ExprNode
49+
{
50+
private:
51+
int op;
52+
ExprNode *expr1, *expr2;
53+
public:
54+
enum {ADD, SUB, AND, OR, LESS, GREATER};
55+
BinaryExpr(SymbolEntry *se, int op, ExprNode*expr1, ExprNode*expr2) : ExprNode(se), op(op), expr1(expr1), expr2(expr2){dst = new Operand(se);};
56+
void output(int level);
57+
void typeCheck();
58+
void genCode();
59+
};
60+
61+
class Constant : public ExprNode
62+
{
63+
public:
64+
Constant(SymbolEntry *se) : ExprNode(se){dst = new Operand(se);};
65+
void output(int level);
66+
void typeCheck();
67+
void genCode();
68+
};
69+
70+
class Id : public ExprNode
71+
{
72+
public:
73+
Id(SymbolEntry *se) : ExprNode(se){SymbolEntry *temp = new TemporarySymbolEntry(se->getType(), SymbolTable::getLabel()); dst = new Operand(temp);};
74+
void output(int level);
75+
void typeCheck();
76+
void genCode();
77+
};
78+
79+
class StmtNode : public Node
80+
{};
81+
82+
class CompoundStmt : public StmtNode
83+
{
84+
private:
85+
StmtNode *stmt;
86+
public:
87+
CompoundStmt(StmtNode *stmt) : stmt(stmt) {};
88+
void output(int level);
89+
void typeCheck();
90+
void genCode();
91+
};
92+
93+
class SeqNode : public StmtNode
94+
{
95+
private:
96+
StmtNode *stmt1, *stmt2;
97+
public:
98+
SeqNode(StmtNode *stmt1, StmtNode *stmt2) : stmt1(stmt1), stmt2(stmt2){};
99+
void output(int level);
100+
void typeCheck();
101+
void genCode();
102+
};
103+
104+
class DeclStmt : public StmtNode
105+
{
106+
private:
107+
Id *id;
108+
public:
109+
DeclStmt(Id *id) : id(id){};
110+
void output(int level);
111+
void typeCheck();
112+
void genCode();
113+
};
114+
115+
class IfStmt : public StmtNode
116+
{
117+
private:
118+
ExprNode *cond;
119+
StmtNode *thenStmt;
120+
public:
121+
IfStmt(ExprNode *cond, StmtNode *thenStmt) : cond(cond), thenStmt(thenStmt){};
122+
void output(int level);
123+
void typeCheck();
124+
void genCode();
125+
};
126+
127+
class IfElseStmt : public StmtNode
128+
{
129+
private:
130+
ExprNode *cond;
131+
StmtNode *thenStmt;
132+
StmtNode *elseStmt;
133+
public:
134+
IfElseStmt(ExprNode *cond, StmtNode *thenStmt, StmtNode *elseStmt) : cond(cond), thenStmt(thenStmt), elseStmt(elseStmt) {};
135+
void output(int level);
136+
void typeCheck();
137+
void genCode();
138+
};
139+
140+
class ReturnStmt : public StmtNode
141+
{
142+
private:
143+
ExprNode *retValue;
144+
public:
145+
ReturnStmt(ExprNode*retValue) : retValue(retValue) {};
146+
void output(int level);
147+
void typeCheck();
148+
void genCode();
149+
};
150+
151+
class AssignStmt : public StmtNode
152+
{
153+
private:
154+
ExprNode *lval;
155+
ExprNode *expr;
156+
public:
157+
AssignStmt(ExprNode *lval, ExprNode *expr) : lval(lval), expr(expr) {};
158+
void output(int level);
159+
void typeCheck();
160+
void genCode();
161+
};
162+
163+
class FunctionDef : public StmtNode
164+
{
165+
private:
166+
SymbolEntry *se;
167+
StmtNode *stmt;
168+
public:
169+
FunctionDef(SymbolEntry *se, StmtNode *stmt) : se(se), stmt(stmt){};
170+
void output(int level);
171+
void typeCheck();
172+
void genCode();
173+
};
174+
175+
class Ast
176+
{
177+
private:
178+
Node* root;
179+
public:
180+
Ast() {root = nullptr;}
181+
void setRoot(Node*n) {root = n;}
182+
void output();
183+
void typeCheck();
184+
void genCode(Unit *unit);
185+
};
186+
187+
#endif

include/BasicBlock.h

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#ifndef __BASIC_BLOCK_H__
2+
#define __BASIC_BLOCK_H__
3+
#include <vector>
4+
#include <set>
5+
#include "Instruction.h"
6+
#include "AsmBuilder.h"
7+
8+
class Function;
9+
10+
class BasicBlock
11+
{
12+
typedef std::vector<BasicBlock *>::iterator bb_iterator;
13+
14+
private:
15+
std::vector<BasicBlock *> pred, succ;
16+
Instruction *head;
17+
Function *parent;
18+
int no;
19+
20+
public:
21+
BasicBlock(Function *);
22+
~BasicBlock();
23+
void insertFront(Instruction *);
24+
void insertBack(Instruction *);
25+
void insertBefore(Instruction *, Instruction *);
26+
void remove(Instruction *);
27+
bool empty() const { return head->getNext() == head;}
28+
void output() const;
29+
bool succEmpty() const { return succ.empty(); };
30+
bool predEmpty() const { return pred.empty(); };
31+
void addSucc(BasicBlock *);
32+
void removeSucc(BasicBlock *);
33+
void addPred(BasicBlock *);
34+
void removePred(BasicBlock *);
35+
int getNo() { return no; };
36+
Function *getParent() { return parent; };
37+
Instruction* begin() { return head->getNext();};
38+
Instruction* end() { return head;};
39+
Instruction* rbegin() { return head->getPrev();};
40+
Instruction* rend() { return head;};
41+
bb_iterator succ_begin() { return succ.begin(); };
42+
bb_iterator succ_end() { return succ.end(); };
43+
bb_iterator pred_begin() { return pred.begin(); };
44+
bb_iterator pred_end() { return pred.end(); };
45+
int getNumOfPred() const { return pred.size(); };
46+
int getNumOfSucc() const { return succ.size(); };
47+
void genMachineCode(AsmBuilder*);
48+
};
49+
50+
#endif

include/Function.h

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#ifndef __FUNCTION_H__
2+
#define __FUNCTION_H__
3+
4+
#include <vector>
5+
#include <map>
6+
#include <set>
7+
#include <algorithm>
8+
#include <iostream>
9+
#include "BasicBlock.h"
10+
#include "SymbolTable.h"
11+
#include "AsmBuilder.h"
12+
13+
class Unit;
14+
15+
class Function
16+
{
17+
typedef std::vector<BasicBlock *>::iterator iterator;
18+
typedef std::vector<BasicBlock *>::reverse_iterator reverse_iterator;
19+
20+
private:
21+
std::vector<BasicBlock *> block_list;
22+
SymbolEntry *sym_ptr;
23+
BasicBlock *entry;
24+
Unit *parent;
25+
26+
public:
27+
Function(Unit *, SymbolEntry *);
28+
~Function();
29+
void insertBlock(BasicBlock *bb) { block_list.push_back(bb); };
30+
BasicBlock *getEntry() { return entry; };
31+
void remove(BasicBlock *bb);
32+
void output() const;
33+
std::vector<BasicBlock *> &getBlockList(){return block_list;};
34+
iterator begin() { return block_list.begin(); };
35+
iterator end() { return block_list.end(); };
36+
reverse_iterator rbegin() { return block_list.rbegin(); };
37+
reverse_iterator rend() { return block_list.rend(); };
38+
SymbolEntry *getSymPtr() { return sym_ptr; };
39+
void genMachineCode(AsmBuilder*);
40+
};
41+
42+
#endif

include/IRBuilder.h

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef __IRBUILDER_H__
2+
#define __IRBUILDER_H__
3+
4+
class Unit;
5+
class Function;
6+
class BasicBlock;
7+
8+
class IRBuilder
9+
{
10+
private:
11+
Unit *unit;
12+
BasicBlock *insertBB; // The current basicblock that instructions should be inserted into.
13+
14+
public:
15+
IRBuilder(Unit*unit) : unit(unit){};
16+
void setInsertBB(BasicBlock*bb){insertBB = bb;};
17+
Unit* getUnit(){return unit;};
18+
BasicBlock* getInsertBB(){return insertBB;};
19+
};
20+
21+
#endif

0 commit comments

Comments
 (0)