|
asherkin@263
|
1 |
/* vim: set ts=4 sw=4 tw=99 et: |
|
asherkin@263
|
2 |
* |
|
asherkin@263
|
3 |
* Copyright (C) 2012 David Anderson |
|
asherkin@263
|
4 |
* |
|
asherkin@263
|
5 |
* This file is part of JITCraft. |
|
asherkin@263
|
6 |
* |
|
asherkin@263
|
7 |
* JITCraft is free software: you can redistribute it and/or modify it under |
|
asherkin@263
|
8 |
* the terms of the GNU General Public License as published by the Free |
|
asherkin@263
|
9 |
* Software Foundation, either version 3 of the License, or (at your option) |
|
asherkin@263
|
10 |
* any later version. |
|
asherkin@263
|
11 |
* |
|
asherkin@263
|
12 |
* Foobar is distributed in the hope that it will be useful, but WITHOUT ANY |
|
asherkin@263
|
13 |
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
asherkin@263
|
14 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
|
asherkin@263
|
15 |
* |
|
asherkin@263
|
16 |
* You should have received a copy of the GNU General Public License along with |
|
asherkin@263
|
17 |
* JITCraft. If not, see http://www.gnu.org/licenses/. |
|
asherkin@263
|
18 |
*/ |
|
asherkin@263
|
19 |
#ifndef _include_bytecode_compiler_h_ |
|
asherkin@263
|
20 |
#define _include_bytecode_compiler_h_ |
|
asherkin@263
|
21 |
|
|
asherkin@263
|
22 |
#include "../Functions.h" |
|
asherkin@263
|
23 |
#include "AST.h" |
|
asherkin@263
|
24 |
#include "BytecodeEmitter.h" |
|
asherkin@263
|
25 |
|
|
asherkin@263
|
26 |
namespace ke { |
|
asherkin@263
|
27 |
|
|
asherkin@263
|
28 |
class Module; |
|
asherkin@263
|
29 |
class Array; |
|
asherkin@263
|
30 |
class ArrayMap; |
|
asherkin@263
|
31 |
|
|
asherkin@263
|
32 |
struct ModuleData |
|
asherkin@263
|
33 |
{ |
|
asherkin@263
|
34 |
ScopedRoot<Module> module; |
|
asherkin@263
|
35 |
PoolList<ScopedRoot<Function> > publics; |
|
asherkin@263
|
36 |
ScopedRoot<FixedArray> natives; |
|
asherkin@263
|
37 |
}; |
|
asherkin@263
|
38 |
|
|
asherkin@263
|
39 |
class BytecodeCompiler : public AstVisitor |
|
asherkin@263
|
40 |
{ |
|
asherkin@263
|
41 |
Zone *zone_; |
|
asherkin@263
|
42 |
ParseTree *tree_; |
|
asherkin@263
|
43 |
BytecodeEmitter emitter_; |
|
asherkin@263
|
44 |
ScopedRoot<FixedArray> locals_; |
|
asherkin@263
|
45 |
Scope *scope_; |
|
asherkin@263
|
46 |
ModuleData *moduleData_; |
|
asherkin@263
|
47 |
|
|
asherkin@263
|
48 |
class LoopScope |
|
asherkin@263
|
49 |
{ |
|
asherkin@263
|
50 |
Scope *scope_; |
|
asherkin@263
|
51 |
BytecodeEmitter::Label *bk_; |
|
asherkin@263
|
52 |
BytecodeEmitter::Label *ct_; |
|
asherkin@263
|
53 |
LoopScope **prevp_; |
|
asherkin@263
|
54 |
LoopScope *prev_; |
|
asherkin@263
|
55 |
|
|
asherkin@263
|
56 |
public: |
|
asherkin@263
|
57 |
LoopScope(Scope *scope, BytecodeEmitter::Label *bk, BytecodeEmitter::Label *ct, |
|
asherkin@263
|
58 |
LoopScope **prevp) |
|
asherkin@263
|
59 |
: scope_(scope), |
|
asherkin@263
|
60 |
bk_(bk), |
|
asherkin@263
|
61 |
ct_(ct), |
|
asherkin@263
|
62 |
prevp_(prevp), |
|
asherkin@263
|
63 |
prev_(*prevp) |
|
asherkin@263
|
64 |
{ |
|
asherkin@263
|
65 |
*prevp_ = this; |
|
asherkin@263
|
66 |
} |
|
asherkin@263
|
67 |
|
|
asherkin@263
|
68 |
~LoopScope() |
|
asherkin@263
|
69 |
{ |
|
asherkin@263
|
70 |
*prevp_ = prev_; |
|
asherkin@263
|
71 |
} |
|
asherkin@263
|
72 |
|
|
asherkin@263
|
73 |
BytecodeEmitter::Label *break_() { |
|
asherkin@263
|
74 |
return bk_; |
|
asherkin@263
|
75 |
} |
|
asherkin@263
|
76 |
BytecodeEmitter::Label *continue_() { |
|
asherkin@263
|
77 |
return ct_; |
|
asherkin@263
|
78 |
} |
|
asherkin@263
|
79 |
Scope *scope() { |
|
asherkin@263
|
80 |
return scope_; |
|
asherkin@263
|
81 |
} |
|
asherkin@263
|
82 |
}; |
|
asherkin@263
|
83 |
|
|
asherkin@263
|
84 |
LoopScope *loop_; |
|
asherkin@263
|
85 |
|
|
asherkin@263
|
86 |
public: |
|
asherkin@263
|
87 |
class AutoEnterScope |
|
asherkin@263
|
88 |
{ |
|
asherkin@263
|
89 |
BytecodeCompiler *bc_; |
|
asherkin@263
|
90 |
Scope *saved_; |
|
asherkin@263
|
91 |
|
|
asherkin@263
|
92 |
public: |
|
asherkin@263
|
93 |
AutoEnterScope(BytecodeCompiler *bc, Scope *scope) |
|
asherkin@263
|
94 |
: bc_(bc), |
|
asherkin@263
|
95 |
saved_(bc->scope_) |
|
asherkin@263
|
96 |
{ |
|
asherkin@263
|
97 |
if (scope) |
|
asherkin@263
|
98 |
bc->scope_ = scope; |
|
asherkin@263
|
99 |
} |
|
asherkin@263
|
100 |
~AutoEnterScope() { |
|
asherkin@263
|
101 |
bc_->scope_ = saved_; |
|
asherkin@263
|
102 |
} |
|
asherkin@263
|
103 |
}; |
|
asherkin@263
|
104 |
|
|
asherkin@263
|
105 |
private: |
|
asherkin@263
|
106 |
void recordLifoSlot(Scope *scope); |
|
asherkin@263
|
107 |
void unwindTo(Scope *scope); |
|
asherkin@263
|
108 |
void visitForTest(Expression *expression, |
|
asherkin@263
|
109 |
BytecodeEmitter::Label *trueBranch, |
|
asherkin@263
|
110 |
BytecodeEmitter::Label *falseBranch, |
|
asherkin@263
|
111 |
BytecodeEmitter::Label *fallthrough); |
|
asherkin@263
|
112 |
Code *compileCode(Scope *scope, unsigned lineno); |
|
asherkin@263
|
113 |
Function *compileFunction(ModuleData *module, FunctionStatement *fun); |
|
asherkin@263
|
114 |
void visitForALU(Type *needed, Expression *node); |
|
asherkin@263
|
115 |
|
|
asherkin@263
|
116 |
void getEnvSlot(Variable *var); |
|
asherkin@263
|
117 |
void setEnvSlot(Variable *var); |
|
asherkin@263
|
118 |
|
|
asherkin@263
|
119 |
bool tableswitch(SwitchStatement *node); |
|
asherkin@263
|
120 |
|
|
asherkin@263
|
121 |
public: |
|
asherkin@263
|
122 |
BytecodeCompiler(Zone *zone, ParseTree *tree, ModuleData *module); |
|
asherkin@263
|
123 |
|
|
asherkin@263
|
124 |
Module *compile(); |
|
asherkin@263
|
125 |
|
|
asherkin@263
|
126 |
public: |
|
asherkin@263
|
127 |
void visit(NameProxy *name); |
|
asherkin@263
|
128 |
void visit(Assignment *node); |
|
asherkin@263
|
129 |
void visit(BinaryExpression *node); |
|
asherkin@263
|
130 |
void visit(ReturnStatement *node); |
|
asherkin@263
|
131 |
void visit(ForStatement *node); |
|
asherkin@263
|
132 |
void visit(BlockStatement *node); |
|
asherkin@263
|
133 |
void visit(VariableDeclaration *node); |
|
asherkin@263
|
134 |
void visit(IntegerLiteral *node); |
|
asherkin@263
|
135 |
void visit(BooleanLiteral *node); |
|
asherkin@263
|
136 |
void visit(FloatLiteral *node); |
|
asherkin@263
|
137 |
void visit(StringLiteral *node); |
|
asherkin@263
|
138 |
void visit(ExpressionStatement *node); |
|
asherkin@263
|
139 |
void visit(FunctionStatement *node); |
|
asherkin@263
|
140 |
void visit(CallExpression *node); |
|
asherkin@263
|
141 |
void visit(IfStatement *node); |
|
asherkin@263
|
142 |
void visit(IndexExpression *node); |
|
asherkin@263
|
143 |
void visit(EnumStatement *node); |
|
asherkin@263
|
144 |
void visit(WhileStatement *node); |
|
asherkin@263
|
145 |
void visit(BreakStatement *node); |
|
asherkin@263
|
146 |
void visit(ContinueStatement *node); |
|
asherkin@263
|
147 |
void visit(IncDecExpression *node); |
|
asherkin@263
|
148 |
void visit(UnaryExpression *node); |
|
asherkin@263
|
149 |
void visit(TernaryExpression *node); |
|
asherkin@263
|
150 |
void visit(StructureStatement *node); |
|
asherkin@263
|
151 |
void visit(FieldExpression *node); |
|
asherkin@263
|
152 |
void visit(SwitchStatement *node); |
|
asherkin@263
|
153 |
void visit(FunctionTypeStatement *node); |
|
asherkin@263
|
154 |
void visit(ArrayLiteral *node); |
|
asherkin@263
|
155 |
void visit(ImportStatement *node); |
|
asherkin@263
|
156 |
}; |
|
asherkin@263
|
157 |
|
|
asherkin@263
|
158 |
} |
|
asherkin@263
|
159 |
|
|
asherkin@263
|
160 |
#endif // _include_bytecode_generator_h_ |