-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbytecodecompilationctx.cpp
More file actions
56 lines (52 loc) · 1.64 KB
/
Copy pathbytecodecompilationctx.cpp
File metadata and controls
56 lines (52 loc) · 1.64 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
#include "bytecodecompilationctx.h"
#include "../utils.h"
#include "class.h"
BytecodeCompilationContext *BytecodeCompilationContext::create() {
BytecodeCompilationContext2 bcc = Gc::alloc<BytecodeCompilationContext>();
bcc->code = NULL;
bcc->ranges_ = (TokenRange *)Gc_malloc(sizeof(TokenRange));
bcc->ranges_[0].token = Token::PlaceholderToken;
bcc->ranges_[0].range_ = 0;
bcc->size = 0;
bcc->capacity = 1;
bcc->present_range = 0;
bcc->code = Bytecode::create();
bcc->code->ctx = bcc;
bcc->lastOpcode = Bytecode::CODE_add;
return bcc;
}
void BytecodeCompilationContext::insert_token(Token t) {
if(size == capacity) {
size_t n = Utils::nextAllocationSize(capacity, size + 1);
ranges_ = (TokenRange *)Gc_realloc(
ranges_, sizeof(TokenRange) * capacity, sizeof(TokenRange) * n);
std::fill_n(&ranges_[capacity], n - capacity,
TokenRange{Token::PlaceholderToken, 0});
capacity = n;
}
if(size > 0) {
ranges_[size - 1].range_ = code->getip() - ranges_[size - 1].range_;
}
ranges_[size].token = t;
ranges_[size++].range_ = code->getip();
}
Token BytecodeCompilationContext::get_token(size_t ip) {
if(size == 0)
return Token::PlaceholderToken;
size_t start = 0;
size_t i = 0;
while(i < size && start + ranges_[i].range_ < ip) {
start += ranges_[i].range_;
i++;
}
if(i >= size)
i = size - 1;
return ranges_[i].token;
}
void BytecodeCompilationContext::finalize() {
if(size != capacity) {
ranges_ = (TokenRange *)Gc_realloc(
ranges_, sizeof(TokenRange) * capacity, sizeof(TokenRange) * size);
capacity = size;
}
}