-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.h
123 lines (93 loc) · 2.78 KB
/
compiler.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
#ifndef COMPILER_H
#define COMPILER_H
#define ASMJIT_STATIC
#include "include/peglib.h"
#include "asmjit/asmjit.h"
#include "interpreter.h"
using namespace asmjit;
//
//
//
#define CONSTANT_POOL_SIZE 32
class constantPoolManager
{
private:
unsigned int stringCpIdx = 0;
std::string stringConstantPool[CONSTANT_POOL_SIZE];
public:
constantPoolManager() { };
~constantPoolManager() { };
const char* getStringConstantPtr(std::string& s)
{
unsigned int idx = 0;
bool found = false;
while ((!found) && (idx < CONSTANT_POOL_SIZE))
{
if (stringConstantPool[idx] == s)
{
found = true;
return stringConstantPool[idx].c_str();
}
idx += 1;
}
if (!found)
{
// TODO: check bounds!!!
stringConstantPool[stringCpIdx] = s;
stringCpIdx += 1;
return stringConstantPool[stringCpIdx - 1].c_str();
}
return NULL; // unreacheable code (I hope)
}
};
//
//
//
struct liaCompilerVariable
{
std::string name;
liaVariableType type;
std::variant<bool, int, long long, std::string> value;
x86::Gp vreg;
};
struct liaCompiledFunction
{
FuncNode* nodePtr;
std::string name;
};
struct liaCompilerEnvironment
{
std::unordered_map<std::string, liaCompilerVariable> varMap;
};
//
//
//
class liaCompiler
{
private:
JitRuntime rt;
CodeHolder codeChunk;
Section* dataSection;
x86::Compiler* jitter;
FileLogger* logger;
constantPoolManager constPoolMgr;
std::vector<liaCompiledFunction> listOfCompiledFunctions;
void generatePrintNewline();
void generatePrintCode(const std::shared_ptr<peg::Ast>& theAst, liaCompilerEnvironment* env);
void compileFunctionCall(const std::shared_ptr<peg::Ast>& theAst, liaCompilerEnvironment* env);
void compileVarAssignment(const std::shared_ptr<peg::Ast>& theAst,liaCompilerEnvironment* env);
void compilePostincrementStmt(const std::shared_ptr<peg::Ast>& theAst,liaCompilerEnvironment* env, int inc);
void compileMultiplyDivideStmt(const std::shared_ptr<peg::Ast>& theAst, liaCompilerEnvironment* env, bool isMultiply);
void compileShiftStatement(const std::shared_ptr<peg::Ast>& theAst, liaCompilerEnvironment* env, int verse);
void compileWhileStmt(const std::shared_ptr<peg::Ast>& theAst,liaCompilerEnvironment* env);
void compileIfStatement(const std::shared_ptr<peg::Ast>& theAst,liaCompilerEnvironment* env);
liaCompilerVariable* addvarOrUpdateEnvironment(liaCompilerVariable* v, liaCompilerEnvironment* env);
void compileCodeBlock(const std::shared_ptr<peg::Ast>& theAst, liaCompilerEnvironment* env);
void compileSimpleCondition(const std::shared_ptr<peg::Ast>& theAst, liaCompilerEnvironment* env,Label& loopLabel,bool invert);
public:
int compile(const std::shared_ptr<peg::Ast>& theAst, std::vector<std::string> params, std::vector<liaFunction>& functionList);
void exeCute();
liaCompiler();
~liaCompiler();
};
#endif