-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctioncompilationctx.cpp
More file actions
60 lines (53 loc) · 1.83 KB
/
Copy pathfunctioncompilationctx.cpp
File metadata and controls
60 lines (53 loc) · 1.83 KB
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
#include "functioncompilationctx.h"
#include "bytecodecompilationctx.h"
#include "class.h"
FunctionCompilationContext *FunctionCompilationContext::create(String2 name,
int arity,
bool isStatic,
bool isva) {
FunctionCompilationContext2 fcc = Gc::alloc<FunctionCompilationContext>();
fcc->slotCount = 0;
fcc->bcc = NULL;
fcc->f = NULL;
fcc->slotmap = NULL;
// initialize the members
fcc->slotmap = (SlotMap *)Gc_malloc(sizeof(SlotMap));
::new(fcc->slotmap) SlotMap();
fcc->f = Function::create(name, arity, isva, isStatic);
fcc->bcc = BytecodeCompilationContext::create();
fcc->f->code = fcc->bcc->code;
return fcc;
}
BytecodeCompilationContext *FunctionCompilationContext::get_codectx() {
return bcc;
}
Function *FunctionCompilationContext::get_fn() {
return f;
}
int FunctionCompilationContext::create_slot(Value s, int scopeID) {
if(slotmap->contains(s)) {
// reassign the variable to the new scope
slotmap[0][s].scopeID = scopeID;
return (*slotmap)[s].slot;
}
slotmap[0][s] = VarState{slotCount++, scopeID};
bcc->code->insertSlot();
return slotCount - 1;
}
bool FunctionCompilationContext::has_slot(Value s, int scopeID) {
return slotmap->contains(s) && slotmap[0][s].scopeID <= scopeID;
}
int FunctionCompilationContext::get_slot(Value s) {
return slotmap[0][s].slot;
}
#ifdef DEBUG
#include "../format.h"
void FunctionCompilationContext::disassemble(WritableStream &os) {
os.write("Slots: ");
for(auto &a : slotmap[0]) {
os.write(a.first.toString()->str(), "(", a.second.slot, "), ");
}
os.write("\n");
f->disassemble(os);
}
#endif