-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcodegenerator.h
99 lines (83 loc) · 2.93 KB
/
codegenerator.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
#ifndef _CODE_GENERATOR_
#define _CODE_GENERATOR_
#include <vector>
#include <iostream>
#include "globals.h"
#include <map>
#include "mifgenerator.h"
#include "instructions.h"
ConditionCodes translateCondition(int operation);
void hr(std::string);
class CodeGenerator
{
public:
void setTerminalDebug(bool displayable);
void setMode(bool isBios, bool isOS);
virtual void generate(TreeNode *node);
void generateCodeToJumpToOS();
void linker();
std::vector<Instruction *> getCode()
{
return code;
}
std::map<std::string, Instruction *> getDestMap()
{
return labelDestMap;
}
std::map<std::string, std::vector<BranchLabel *>> getOriginMap()
{
return labelOriginMap;
}
protected:
std::string generatedCode;
std::vector<Instruction *> code;
std::map<std::string, Instruction *> labelDestMap;
std::map<std::string, std::vector<BranchLabel *>> labelOriginMap;
bool shouldPrintGeneratedCodeOnScreen;
bool shouldShowVisitingMessages;
TreeNode *mainActivation;
bool isBios, isOS;
//Private methods
void print(Instruction *instruction);
void generateCode(TreeNode *node);
void createHeader();
void createOSHeader();
void createStandardHeader(int number);
void createFooter();
void generateCodeForAnyNode(TreeNode *);
void generateCodeForStmtNode(TreeNode *node);
void generateCodeForExprNode(TreeNode *node);
void generateOperationCode(TreeNode *);
void generateRegisterOperation(TreeNode *);
void generateOptimizedOperation(TreeNode *);
void generateCodeForBranch(std::string branch_name, ConditionCodes condition, TreeNode *child = NULL, bool isJumpAndLink = false);
void generateCodeForIf(TreeNode *node);
void generateCodeForIfElse(TreeNode *node);
void generateCodeForPop(Registers reg);
void registerLabelInstruction(std::string label, Instruction *Instruction);
void generateCodeForConst(int, Registers);
int fetchVarOffsetAsInteger(TreeNode *node);
int fetchVarOffsetByName(std::string variable, std::string scope);
void loadVariable(TreeNode *node, Registers reg);
void printLabelNop(std::string);
void setDebugName(std::string name);
void DestroyARAndExitFunction(TreeNode *);
void generateGlobalAR();
void generateCodeForFunctionActivation(TreeNode *node);
void generateRunTimeSystem();
void destroyGlobalAR();
void buildAR(int localVariableCount, int argumentCount, TreeNode *argumentNode);
void jumpAndLink(std::string);
void printRegister(Registers reg);
void pushArguments(int argumentCount, TreeNode *argumentNode);
void generateCodeForSimpleVariableAssignment(TreeNode *variable, TreeNode *value);
void pushMultipleIfNeeded(int number);
void popMultipleIfNeeded(int number);
SystemCalls getFooterSystemCall();
};
class DummyCodeGenerator : public CodeGenerator
{
public:
void generate(TreeNode *node);
};
#endif