This repository has been archived by the owner on Mar 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
interpreter.c
290 lines (269 loc) · 5.46 KB
/
interpreter.c
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* Project name:
* Implementace interpretu imperativního jazyka IFJ14
*
* Repository:
* https://github.com/Dasio/IFJ
*
* Team:
* Dávid Mikuš (xmikus15)
* Peter Hostačný (xhosta03)
* Tomáš Kello (xkello00)
* Adam Lučanský (xlucan01)
* Michaela Lukášová (xlukas09)
*/
#include "interpreter.h"
/**
* "Global" tape containing instructions
*/
InstructionVector *tape;
extern int instr_counter;
/**
* Pointer to instruction tape at beginning
*/
int64_t IP = 0;
Stack stack = {.BP = 0};
void initInterpret() {
stack.vect = StackDataVectorInit(DEFAULT_STACK_SIZE);
initialized_items.stack = true;
// TODO: Test optimal initial size
if(!tape) {
tape = InstructionVectorInit(512);
initialized_items.tape = true;
}
}
/**
* CORE
*/
void generateExprInstruction(InstructionOp op, Operand* a, Operand* b, Operand* c) {
instr_counter++;
int instruction_offset = op << 8 | b->var_type << 6 | c->var_type << 4 |
b->data_type << 2 | c->data_type;
assert(instruction_table[instruction_offset]);
Instruction i = (Instruction) {.instr = instruction_table[instruction_offset],
.dst = *a, .src_1 = *b, .src_2 = *c};
InstructionVectorAppend(tape, i);
}
void generateInstruction(InstructionOp op, Operand* a, Operand* b, Operand* c) {
instr_counter++;
InstrFuncPtr i_ptr = NULL;
switch(op) {
case PUSH:
switch(b->var_type)
{
case LOCAL:
switch(b->data_type)
{
case STRING:
i_ptr = Instr_PUSH_LS;
break;
case DOUBLE:
i_ptr = Instr_PUSH_LD;
break;
case INT:
i_ptr = Instr_PUSH_LI;
break;
case BOOL:
i_ptr = Instr_PUSH_LB;
break;
default:
break;
}
break;
case GLOBAL:
switch(b->data_type)
{
case STRING:
i_ptr = Instr_PUSH_GS;
break;
case DOUBLE:
i_ptr = Instr_PUSH_GD;
break;
case INT:
i_ptr = Instr_PUSH_GI;
break;
case BOOL:
i_ptr = Instr_PUSH_GB;
break;
default:
break;
}
break;
case CONST:
i_ptr = Instr_PUSH_C;
break;
default:
break;
}
break;
case PUSHX:
switch(b->var_type)
{
case LOCAL:
switch(b->data_type)
{
case STRING:
i_ptr = Instr_PUSHX_LS;
break;
case DOUBLE:
i_ptr = Instr_PUSHX_LD;
break;
case INT:
i_ptr = Instr_PUSHX_LI;
break;
case BOOL:
i_ptr = Instr_PUSHX_LB;
break;
default:
break;
}
break;
case GLOBAL:
switch(b->data_type)
{
case STRING:
i_ptr = Instr_PUSHX_GS;
break;
case DOUBLE:
i_ptr = Instr_PUSHX_GD;
break;
case INT:
i_ptr = Instr_PUSHX_GI;
break;
case BOOL:
i_ptr = Instr_PUSHX_GB;
break;
default:
break;
}
break;
case CONST:
i_ptr = Instr_PUSH_C;
break;
default:
break;
}
break;
case JMP_F:
i_ptr = Instr_JMP_F;
break;
case JMP:
i_ptr = Instr_JMP;
break;
case CALL:
i_ptr = Instr_CALL;
break;
case CALL_LENGTH:
i_ptr = Instr_CALL_LENGTH;
break;
case CALL_COPY:
i_ptr = Instr_CALL_COPY;
break;
case CALL_FIND:
i_ptr = Instr_CALL_FIND;
break;
case CALL_SORT:
i_ptr = Instr_CALL_SORT;
break;
case RET:
i_ptr = Instr_RET;
break;
case MOV:
switch(b->data_type)
{
case STRING:
i_ptr = Instr_MOV_GS;
break;
case DOUBLE:
i_ptr = Instr_MOV_GD;
break;
case INT:
i_ptr = Instr_MOV_GI;
break;
case BOOL:
i_ptr = Instr_MOV_GB;
break;
default:
break;
}
break;
case READLN:
if (a->var_type == GLOBAL) {
switch(a->data_type)
{
case STRING:
i_ptr = Instr_READLN_GS;
break;
case DOUBLE:
i_ptr = Instr_READLN_GD;
break;
case INT:
i_ptr = Instr_READLN_GI;
break;
default:
break;
}
}
if (a->var_type == LOCAL) {
switch(a->data_type)
{
case STRING:
i_ptr = Instr_READLN_LS;
break;
case DOUBLE:
i_ptr = Instr_READLN_LD;
break;
case INT:
i_ptr = Instr_READLN_LI;
break;
default:
break;
}
}
break;
case WRITE:
i_ptr = Instr_WRITE;
break;
case HALT:
i_ptr = Instr_HALT;
break;
default:
i_ptr = Instr_CALL;
break;
}
Instruction i = (Instruction) {.instr = i_ptr, .dst = *a, .src_1 = *b, .src_2 = *c};
InstructionVectorAppend(tape, i);
}
void dumpTape() {
assert(tape);
uint32_t tape_length = tape->used;
Instruction *first = InstructionVectorFirst(tape);
fprintf(stderr, "============================\n");
fprintf(stderr, "TAPE: \t \t offset");
fprintf(stderr, "\n");
for(uint32_t i = 0; i < tape_length; i++) {
Instruction *inst = first+i;
fprintf(stderr, "%d\t%s\t", i, stringifyInstructionPtr(inst->instr));
fprintf(stderr, " \t%ld", inst->dst.offset);
fprintf(stderr, "\n");
}
fprintf(stderr, "TAPE DUMP OVER === IP: %lu ==\n", IP);
}
void runInterpretation() {
assert(tape && "Call initInterpret() before running interpreter");
Instruction *first = InstructionVectorFirst(tape);
assert(first && "Instruction tape is empty!");
//dumpTape();
while(true) {
Instruction *i = first + IP;
(i->instr)(i);
fflush(stdout);
/*
fprintf(stderr, "============================\n");
fprintf(stderr, "|IP: %ld - %s\n", IP, stringifyInstructionPtr(i->instr));
dumpStack();
fprintf(stderr, "\n\n");
*/
IP++;
}
}