-
Notifications
You must be signed in to change notification settings - Fork 1
/
BFCompiler.cpp
160 lines (129 loc) · 4.79 KB
/
BFCompiler.cpp
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <iostream>
#include "BFCompiler.h"
#include "BFIncrement.h"
#include "BFDataIncrement.h"
#include "BFReed.h"
#include "BFWrite.h"
#include "BFLoop.h"
void BF::declareFunctions(LLVMModuleRef module) {
// types
LLVMTypeRef void_type = LLVMVoidType();
LLVMTypeRef int8_type = LLVMInt32Type();
LLVMTypeRef int32_type = LLVMInt32Type();
// types pointers
LLVMTypeRef int8_type_ptr = LLVMPointerType(int8_type, 0);
// calloc
LLVMTypeRef calloc_function_args_type[] = {
int32_type, int32_type
};
LLVMTypeRef calloc_function_type = LLVMFunctionType(int8_type_ptr, calloc_function_args_type, 2, false);
LLVMAddFunction(module, "calloc", calloc_function_type);
// free
LLVMTypeRef free_function_args_type[] = {
int8_type_ptr
};
LLVMTypeRef free_function_type = LLVMFunctionType(void_type, free_function_args_type, 1, false);
LLVMAddFunction(module, "free", free_function_type);
// putchar
LLVMTypeRef putchar_function_args_type[] = {
int32_type
};
LLVMTypeRef putchar_function_type = LLVMFunctionType(int32_type, putchar_function_args_type, 1, false);
LLVMAddFunction(module, "putchar", putchar_function_type);
// getchar
LLVMTypeRef getchar_function_type = LLVMFunctionType(int32_type, nullptr, 0, false);
LLVMAddFunction(module, "getchar", getchar_function_type);
}
BF::BFMain BF::buildMain(LLVMContextRef context, LLVMModuleRef module, LLVMBuilderRef builder) {
LLVMTypeRef int32_type = LLVMInt32Type();
LLVMTypeRef main_function_type = LLVMFunctionType(int32_type, nullptr, 0, false);
LLVMValueRef main_function = LLVMAddFunction(module, "main", main_function_type);
LLVMBasicBlockRef bb = LLVMAppendBasicBlockInContext(context, main_function, "entry");
LLVMPositionBuilderAtEnd(builder, bb);
LLVMValueRef calloc_function = LLVMGetNamedFunction(module, "calloc");
LLVMValueRef calloc_function_args[] {
LLVMConstInt(int32_type, 30000, false),
LLVMConstInt(int32_type, 1, false),
};
LLVMValueRef cells = LLVMBuildCall(builder, calloc_function, calloc_function_args, 2, "cells");
LLVMValueRef cellIndex = LLVMBuildAlloca(builder, int32_type, "cell_index_ptr");
return {
bb, cells, cellIndex, main_function
};
}
void BF::buildClear(LLVMContextRef context, LLVMModuleRef module, LLVMBuilderRef builder, BFMain data) {
LLVMValueRef free_function = LLVMGetNamedFunction(module, "free");
LLVMValueRef free_function_args[] {
data.cells
};
LLVMBuildCall(builder, free_function, free_function_args, 1, "");
LLVMBuildRet(builder, LLVMConstInt(LLVMInt32TypeInContext(context), 0, false));
}
int BF::findClose(std::string source, int index) {
assert((source[index] == '[') &&
"Looking for ']' but not starting from a '['");
int count = 0;
for (int i = index; i < source.length(); ++i) {
switch (source[i]) {
case '[':
count++;
break;
case ']':
count--;
break;
}
if (count == 0) {
return i;
}
}
return -1;
}
std::vector<BF::Instruction *> BF::parse(const std::string& source, int from, int to, BF::BFMain data) {
std::vector<BF::Instruction *> prog;
int i = from;
while (i < to) {
switch (source[i]) {
case '+': {
prog.push_back(new BF::Increment(data.cells, data.cellIndex));
break;
}
case '-': {
prog.push_back(new BF::Increment(data.cells, data.cellIndex, -1));
break;
}
case '>': {
prog.push_back(new BF::DataIncrement(data.cells, data.cellIndex));
break;
}
case '<': {
prog.push_back(new BF::DataIncrement(data.cells, data.cellIndex, -1));
break;
}
case ',': {
prog.push_back(new BF::Read(data.cells, data.cellIndex));
break;
}
case '.': {
prog.push_back(new BF::Write(data.cells, data.cellIndex));
break;
}
case '[': {
int close = BF::findClose(source, i);
if (close == -1) {
std::cerr << "Unmatched '[' at position " << i << "\n";
exit(-3);
}
prog.push_back(new BF::Loop(data.cells, data.cellIndex,
parse(source, i + 1, close, data)));
i = close;
break;
}
case ']': {
std::cerr << "Unmatched ']' at position " << i << "\n";
exit(-3);
}
}
++i;
}
return prog;
}