Repository navigation
Expand file tree
/
Copy pathcode_generator.cpp
More file actions
628 lines (587 loc) · 19.7 KB
/
Copy pathcode_generator.cpp
File metadata and controls
628 lines (587 loc) · 19.7 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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
#include "code_generator.hpp"
#include "evaluator.cpp"
#include <cstdint>
#include <fstream>
#include <ios>
#include <iostream>
#include <unordered_map>
std::ofstream code("a.frisc");
std::unordered_map<std::string, std::string>
code_global_variables; // name -> address
std::unordered_map<std::string, std::vector<std::string>> global_var_init;
std::unordered_map<std::string, std::vector<std::vector<std::string>>>
global_arr_init;
std::multimap<std::string, std::pair<std::string, int>> code_global_arrays;
std::unordered_map<int, std::string> code_constants;
std::unordered_map<std::string, std::string> code_functions;
std::multimap<std::string, int> code_local_variables;
std::vector<std::pair<std::string, bool>> increment_after;
std::unordered_map<int, std::vector<std::pair<std::string, int>>>
for_var_update;
std::unordered_map<std::string, int> function_arrays;
int logical_skip = 0;
int skip_count = 0;
int loop_counter = 0;
int break_counter = 0;
int mod_op = 0;
int div_op = 0;
int mul_op = 0;
int not_op = 0;
int not_negative = 0;
int for_loop_skip = 0;
void code_init(void) {
code << std::dec;
code << "\tMOVE 40000, R7" << std::endl;
code << "\tSUB R7, %D 4, R2" << std::endl;
code << "\tCALL F0" << std::endl;
code << "\tHALT" << std::endl;
}
void save_context(void) {
code << std::dec;
code << "\tPUSH R0" << std::endl;
code << "\tPUSH R1" << std::endl;
code << "\tPUSH R2" << std::endl;
code << "\tPUSH R3" << std::endl;
code << "\tPUSH R4" << std::endl;
code << "\tPUSH R5" << std::endl;
}
void refresh_context(void) {
code << std::dec;
code << "\tPOP R5" << std::endl;
code << "\tPOP R4" << std::endl;
code << "\tPOP R3" << std::endl;
code << "\tPOP R2" << std::endl;
code << "\tPOP R1" << std::endl;
code << "\tPOP R0" << std::endl;
}
void fn_def(std::string name, int argc) {
code << std::dec;
code << name << "\tSUB R7, %D " << 4 * argc << ", R7" << std::endl;
}
void return_sp(void) {
code << std::dec;
code << "\tMOVE R2, R7" << std::endl;
code << "\tRET" << std::endl;
}
void branch_if() {
code << std::dec;
code << "\tPOP R0" << std::endl;
code << "\tCMP R0,0" << std::endl;
code << "\tJP_EQ S" << logical_skip << std::endl;
}
void branch_else() {
code << std::dec;
code << "S" << logical_skip++ << std::endl;
}
void operation_mod(void) {
code << std::dec;
code << "MD" << mod_op << "\tPOP R0" << std::endl; // second operand
code << "\tPOP R1" << std::endl; // first operand
code << "\tCMP R1, R0" << std::endl;
code << "\tJP_SLT MD" << mod_op + 1 << std::endl;
code << "\tSUB R1, R0, R1" << std::endl;
code << "\tPUSH R1" << std::endl;
code << "\tPUSH R0" << std::endl;
code << "\tJP MD" << mod_op << std::endl;
code << "MD" << ++mod_op << "\tPUSH R1" << std::endl;
++mod_op;
}
void operation_mul(void) {
code << std::dec;
code << "\tMOVE %D 0, R4" << std::endl;
code << "\tMOVE %D 0, R6" << std::endl;
code << "\tPOP R0" << std::endl;
code << "\tPOP R1" << std::endl;
code << "\tCMP R1, %D 0" << std::endl;
code << "\tJP_SGE NG" << not_negative << std::endl;
code << "\tXOR R1, %D -1, R1" << std::endl;
code << "\tADD R1, %D 1, R1" << std::endl;
code << "\tXOR R6, %D -1, R6" << std::endl;
code << "NG" << not_negative++ << "\tCMP R0, %D 0" << std::endl;
code << "\tJP_SGE NG" << not_negative << std::endl;
code << "\tXOR R0, %D -1, R0" << std::endl;
code << "\tADD R0, %D 1, R0" << std::endl;
code << "\tXOR R6, %D -1, R6" << std::endl;
code << "\tJP NG" << not_negative << std::endl;
code << "ML" << mul_op << "\tPOP R0" << std::endl; // second operand
code << "\tPOP R1" << std::endl; // first operand
code << "NG" << not_negative++ << "\tCMP R0, 0" << std::endl;
code << "\tJP_EQ ML" << mul_op + 1 << std::endl;
code << "\tADD R4, R1, R4" << std::endl;
code << "\tSUB R0, 1, R0" << std::endl;
code << "\tPUSH R1" << std::endl;
code << "\tPUSH R0" << std::endl;
code << "\tJP ML" << mul_op << std::endl;
code << "ML" << ++mul_op << "\tCMP R6, %D 0" << std::endl;
code << "\tJP_EQ NG" << not_negative << std::endl;
code << "\tXOR R4, R6, R4" << std::endl;
code << "\tADD R4, %D 1, R4" << std::endl;
code << "NG" << not_negative++ << "\tPUSH R4" << std::endl;
++mul_op;
}
void operation_div(void) {
// we subtract the second operand from the first operand while the first is
// larger then the second, on each cycle the result in register R4 is
// incremented by 1
code << std::dec;
code << "\tMOVE %D 0, R4" << std::endl;
code << "\tMOVE %D 0, R6" << std::endl;
code << "\tPOP R0" << std::endl;
code << "\tPOP R1" << std::endl;
code << "\tCMP R1, %D 0" << std::endl;
code << "\tJP_SGE NG" << not_negative << std::endl;
code << "\tXOR R1, %D -1, R1" << std::endl;
code << "\tADD R1, %D 1, R1" << std::endl;
code << "\tXOR R6, %D -1, R6" << std::endl;
code << "NG" << not_negative++ << "\tCMP R0, %D 0" << std::endl;
code << "\tJP_SGE NG" << not_negative << std::endl;
code << "\tXOR R0, %D -1, R0" << std::endl;
code << "\tADD R0, %D 1, R0" << std::endl;
code << "\tXOR R6, %D -1, R6" << std::endl;
code << "\tJP NG" << not_negative << std::endl;
code << "DV" << div_op << "\tPOP R0" << std::endl; // second operand
code << "\tPOP R1" << std::endl; // first operand
code << "NG" << not_negative++ << "\tCMP R1, R0" << std::endl;
code << "\tJP_SLT DV" << div_op + 1 << std::endl;
code << "\tADD R4, 1, R4" << std::endl;
code << "\tSUB R1, R0, R1" << std::endl;
code << "\tPUSH R1" << std::endl;
code << "\tPUSH R0" << std::endl;
code << "\tJP DV" << div_op << std::endl;
code << "DV" << ++div_op << "\tCMP R6, %D 0" << std::endl;
code << "\tJP_EQ NG" << not_negative << std::endl;
code << "\tXOR R4, R6, R4" << std::endl;
code << "\tADD R4, %D 1, R4" << std::endl;
code << "NG" << not_negative++ << "\tPUSH R4" << std::endl;
++div_op;
}
void not_operator() {
// compare result with 0
// 1. case -> result is equal to 0 -> convert to 1
// 2. case -> result is not equal to 0 -> convert to 0
code << "\tPOP R0" << std::endl;
code << "\tCMP R0, 0" << std::endl;
code << "\tJP_EQ NT" << not_op << std::endl;
code << "\tMOVE %D 0, R0" << std::endl;
code << "\tJP NT" << not_op + 1 << std::endl;
code << "NT" << not_op++ << "\tMOVE %D 1, R0" << std::endl;
code << "NT" << not_op << "\tPUSH R0" << std::endl;
}
void tilde_operator() {
code << "\tPOP R0" << std::endl;
code << "\tXOR R0, 0FFFFFFFF, R0" << std::endl;
code << "\tPUSH R0" << std::endl;
}
void call_fn(std::string name, size_t argc) {
code << std::dec;
int cnt = argc;
if (argc > 0) {
do {
code << "\tADD R7, %D 4, R7" << std::endl;
} while (argc--);
}
code << "\tSUB R7, %D 4, R2" << std::endl;
code << "\tCALL " << code_functions.at(name) << std::endl;
if (cnt) {
code << "\tSUB R7, %D 4, R7" << std::endl;
}
}
void load_var(std::string name) {
// first check local defs!
code << std::dec;
if (code_local_variables.count(name)) {
auto range = code_local_variables.equal_range(name);
code << "\tLOAD R0, " << "(R2-0" << std::hex << std::uppercase
<< std::prev(range.second)->second << ')' << std::endl;
code << std::dec;
code << "\tPUSH R0" << std::endl;
} else if (code_global_variables.count(name)) {
code << "\tLOAD R0, " << "(" << code_global_variables.at(name) << ')'
<< std::endl;
code << "\tPUSH R0" << std::endl;
}
}
void load_array(std::string name) {
code << std::dec;
code << "\tPOP R0" << std::endl;
code << "\tSHL R0, 2, R0" << std::endl;
if (function_arrays.count(name)) {
int loc = function_arrays.at(name);
code << "\tLOAD R1, " << "(R2-0" << std::hex << std::uppercase << loc
<< ')' << std::endl;
code << "\tADD R0, R1, R1" << std::endl;
code << "\tLOAD R0, (R1)" << std::endl;
code << "\tPUSH R0" << std::endl;
}
// saved in memory -> moving down
else if (code_global_arrays.count(name)) {
auto range = code_global_arrays.equal_range(name);
std::string loc = std::prev(range.second)->second.first;
code << "\tMOVE " << loc << ", R1" << std::endl;
code << "\tADD R0, R1, R1" << std::endl;
code << "\tLOAD R0, (R1)" << std::endl;
code << "\tPUSH R0" << std::endl;
}
}
void store_global_var(std::string name) {
code << "\tPOP R0" << std::endl;
code << "\tSTORE R0, " << '(' << code_global_variables.at(name) << ')'
<< std::endl;
}
void store_local_var(std::string name) {
code << "\tPOP R0" << std::endl;
code << "\tSTORE R0, " << "(R2-0" << std::hex << std::uppercase
<< std::prev(code_local_variables.equal_range(name).second)->second
<< ')' << std::endl;
code << std::dec << std::endl;
}
void store_global_arr(std::string name, int index) {
code << "\tPOP R3" << std::endl;
code << "\tPOP R0" << std::endl;
code << "\tSHL R0, 2, R0" << std::endl;
code << "\tMOVE "
<< std::prev(code_global_arrays.equal_range(name).second)->second.first
<< ", R1" << std::endl;
code << "\tADD R1, R0, R1" << std::endl;
code << "\tSTORE R3, (R1)" << std::endl;
code << std::dec;
}
void push_empty_var() {
// possibly cannot initialize to 0
code << "\tMOVE %D 0, R0" << std::endl;
code << "\tPUSH R0" << std::endl;
}
void store_func_arr(std::string name, int index) {
code << "\tPOP R3" << std::endl; // updated value in register R3
// next thing on stack should always be the index
// we take the index and multiply by 4 -> left shift by 2 bits
code << "\tPOP R0" << std::endl;
code << "\tSHL R0, 2, R0" << std::endl;
code << "\tLOAD R1, " << "(R2-0" << std::hex << std::uppercase
<< function_arrays.at(name) << ')' << std::endl;
code << std::dec;
code << "\tADD R1, R0, R1" << std::endl;
code << "\tSTORE R3, (R1)" << std::endl;
}
void send_arr(std::string name) {
code << "\tMOVE "
<< std::prev(code_global_arrays.equal_range(name).second)->second.first
<< ", R0" << std::endl;
code << "\tPUSH R0" << std::endl;
}
void send_function_arr(std::string name) {
int offset = function_arrays.at(name);
code << "\tLOAD R0, " << "(R2-0" << std::hex << std::uppercase << offset
<< ')' << std::endl;
code << std::dec;
code << "\tPUSH R0" << std::endl;
}
void equal_comparison(bool eq) {
code << std::dec;
if (eq) {
code << "\tPOP R0" << std::endl;
code << "\tPOP R1" << std::endl;
code << "\tCMP R0, R1" << std::endl;
code << "\tJP_EQ S" << logical_skip << std::endl;
code << "\tMOVE %D 0, R0" << std::endl;
code << "\tPUSH R0" << std::endl;
code << "\tJP S" << logical_skip + 1 << std::endl;
code << "S" << logical_skip++ << std::endl;
code << "\tMOVE %D 1, R0" << std::endl;
code << "\tPUSH R0" << std::endl;
code << "S" << logical_skip++ << std::endl;
} else {
code << "\tPOP R0" << std::endl;
code << "\tPOP R1" << std::endl;
code << "\tCMP R0, R1" << std::endl;
code << "\tJP_NE S" << logical_skip << std::endl;
code << "\tMOVE %D 0, R0" << std::endl;
code << "\tPUSH R0" << std::endl;
code << "\tJP S" << logical_skip + 1 << std::endl;
code << "S" << logical_skip++ << std::endl;
code << "\tMOVE %D 1, R0" << std::endl;
code << "\tPUSH R0" << std::endl;
code << "S" << logical_skip++ << std::endl;
}
}
void relation_comparison(std::string op) {
code << std::dec;
if (op == "OP_LT") {
code << "\tPOP R0" << std::endl;
code << "\tPOP R1" << std::endl;
code << "\tCMP R1, R0" << std::endl;
code << "\tJP_SLT S" << logical_skip << std::endl;
code << "\tMOVE %D 0, R0" << std::endl;
code << "\tPUSH R0" << std::endl;
code << "\tJP S" << logical_skip + 1 << std::endl;
code << "S" << logical_skip++ << std::endl;
code << "\tMOVE %D 1, R0" << std::endl;
code << "\tPUSH R0" << std::endl;
code << "S" << logical_skip++ << std::endl;
} else if (op == "OP_GT") {
code << "\tPOP R0" << std::endl;
code << "\tPOP R1" << std::endl;
code << "\tCMP R1, R0" << std::endl;
code << "\tJP_SGT S" << logical_skip << std::endl;
code << "\tMOVE %D 0, R0" << std::endl;
code << "\tPUSH R0" << std::endl;
code << "\tJP S" << logical_skip + 1 << std::endl;
code << "S" << logical_skip++ << std::endl;
code << "\tMOVE %D 1, R0" << std::endl;
code << "\tPUSH R0" << std::endl;
code << "S" << logical_skip++ << std::endl;
} else if (op == "OP_LTE") {
code << "\tPOP R0" << std::endl;
code << "\tPOP R1" << std::endl;
code << "\tCMP R1, R0" << std::endl;
code << "\tJP_SLE S" << logical_skip << std::endl;
code << "\tMOVE %D 0, R0" << std::endl;
code << "\tPUSH R0" << std::endl;
code << "\tJP S" << logical_skip + 1 << std::endl;
code << "S" << logical_skip++ << std::endl;
code << "\tMOVE %D 1, R0" << std::endl;
code << "\tPUSH R0" << std::endl;
code << "S" << logical_skip++ << std::endl;
} else if (op == "OP_GTE") {
code << "\tPOP R0" << std::endl;
code << "\tPOP R1" << std::endl;
code << "\tCMP R1, R0" << std::endl;
code << "\tJP_SGE S" << logical_skip << std::endl;
code << "\tMOVE %D 0, R0" << std::endl;
code << "\tPUSH R0" << std::endl;
code << "\tJP S" << logical_skip + 1 << std::endl;
code << "S" << logical_skip++ << std::endl;
code << "\tMOVE %D 1, R0" << std::endl;
code << "\tPUSH R0" << std::endl;
code << "S" << logical_skip++ << std::endl;
}
}
void variable_increment_before(std::string var, bool plus) {
code << std::dec;
load_var(var);
code << "\tPOP R0" << std::endl;
if (plus) {
code << "\tADD R0, 1, R0" << std::endl;
code << "\tPUSH R0" << std::endl;
} else {
code << "\tSUB R0, 1, R0" << std::endl;
code << "\tPUSH R0" << std::endl;
}
if (code_local_variables.count(var)) {
store_local_var(var);
} else if (code_global_variables.count(var)) {
store_global_var(var);
}
load_var(var);
}
void variable_increment_after(void) {
code << std::dec;
for (std::pair<std::string, int> var : increment_after) {
load_var(var.first);
if (var.second == false) {
code << "\tSUB R0, %D 1, R0" << std::endl;
code << "\tPUSH R0" << std::endl;
} else {
code << "\tADD R0, %D 1, R0" << std::endl;
code << "\tPUSH R0" << std::endl;
}
if (code_local_variables.count(var.first)) {
store_local_var(var.first);
} else if (code_global_variables.count(var.first)) {
store_global_var(var.first);
}
}
increment_after.clear();
}
void loop_break(void) { code << "\tJP BR" << break_counter << std::endl; }
void loop_continue(void) { code << "\tJP CN" << break_counter << std::endl; }
void while_start(void) { code << "L" << loop_counter << std::endl; }
void while_check(void) {
code << "\tPOP R0" << std::endl;
code << "\tCMP R0, 0" << std::endl;
code << "\tJP_EQ E" << loop_counter << std::endl;
}
void while_end(void) {
code << "CN" << break_counter << "\tJP L" << loop_counter << std::endl;
code << "E" << loop_counter++ << std::endl;
code << "BR" << break_counter++ << std::endl;
}
void forc_start(void) {
code << "L" << loop_counter << "\tMOVE %D 0, R5" << std::endl;
}
void forc_check(void) {
code << "\tPOP R0" << std::endl;
code << "\tCMP R0, 0" << std::endl;
code << "\tJP_EQ E" << loop_counter << std::endl;
}
void forc_skip_first(void) {
code << "I" << for_loop_skip << "\tCMP R5, 0" << std::endl;
code << "\tJP_EQ K" << for_loop_skip << std::endl;
}
void forc_skip_second(void) {
code << "\tCMP R5, 1" << std::endl;
code << "\tJP_EQ J" << for_loop_skip << std::endl;
code << "\tCMP R5, 2" << std::endl;
code << "\tJP_EQ AE" << for_loop_skip << std::endl;
code << "K" << for_loop_skip << std::endl;
}
void inf_loop() {
code << "\tMOVE %D 1, R0" << std::endl;
code << "\tPUSH R0" << std::endl;
}
void forc_end(void) {
code << "CN" << break_counter << "\tMOVE %D 1, R5" << std::endl;
code << "\tJP I" << for_loop_skip << std::endl;
code << "J" << for_loop_skip << "\tJP L" << loop_counter << std::endl;
code << "E" << loop_counter++ << "\tMOVE %D 2, R5" << std::endl;
code << "\tJP I" << for_loop_skip << std::endl;
code << "AE" << for_loop_skip++ << std::endl;
code << "BR" << break_counter++ << std::endl;
}
void load_ret_val(void) { code << "\tPOP R6" << std::endl; }
void push_ret_val(void) { code << "\tPUSH R6" << std::endl; }
void unary_operation(int8_t op) {
if (op == -1) {
code << "\tPOP R0" << std::endl;
code << "\tSUB R0, 1, R0" << std::endl;
code << "\tPUSH R0" << std::endl;
} else {
code << "\tPOP R0" << std::endl;
code << "\tADD R0, 1, R0" << std::endl;
code << "\tPUSH R0" << std::endl;
}
}
void binary_operation(int8_t op) {
// Not commutative
if (op == -1) {
code << "\tPOP R0" << std::endl; // second operand
code << "\tPOP R1" << std::endl; // first operand
code << "\tSUB R1, R0, R0" << std::endl;
code << "\tPUSH R0" << std::endl;
} else {
code << "\tPOP R0" << std::endl; // second operand
code << "\tPOP R1" << std::endl; // first operand
code << "\tADD R1, R0, R0" << std::endl;
code << "\tPUSH R0" << std::endl;
}
}
void bitwise_operation(uint8_t op) {
// Commutative, Associative
if (op == 1) {
// AND
code << "\tPOP R0" << std::endl;
code << "\tPOP R1" << std::endl;
code << "\tAND R1, R0, R0" << std::endl;
code << "\tPUSH R0" << std::endl;
} else if (op == 2) {
// OR
code << "\tPOP R0" << std::endl;
code << "\tPOP R1" << std::endl;
code << "\tOR R1, R0, R0" << std::endl;
code << "\tPUSH R0" << std::endl;
} else if (op == 3) {
// XOR
code << "\tPOP R0" << std::endl;
code << "\tPOP R1" << std::endl;
code << "\tXOR R1, R0, R0" << std::endl;
code << "\tPUSH R0" << std::endl;
}
}
void logical_start(uint8_t op) {
if (op == 1) {
// &&
code << "\tPOP R0" << std::endl;
code << "\tCMP R0, 0" << std::endl;
code << "\tJP_EQ S" << logical_skip << std::endl;
code << "\tPUSH R0" << std::endl;
} else {
// ||
code << "\tPOP R0" << std::endl;
code << "\tCMP R0, 0" << std::endl;
code << "\tJP_NE S" << logical_skip << std::endl;
code << "\tPUSH R0" << std::endl;
}
}
void logical_check(uint8_t op) {
if (op == 1) {
// &&
code << "\tPOP R0" << std::endl;
code << "\tPOP R1" << std::endl;
code << "\tAND R0, R1, R0" << std::endl;
code << "\tPUSH R0" << std::endl;
} else {
// ||
code << "\tPOP R0" << std::endl;
code << "\tPOP R1" << std::endl;
code << "\tOR R0, R1, R0" << std::endl;
code << "\tPUSH R0" << std::endl;
}
}
void logical_end(void) { code << "S" << logical_skip++ << std::endl; }
void load_const(std::string var) {
code << "\tLOAD R0, " << '(' << var << ')' << std::endl;
code << "\tPUSH R0" << std::endl;
}
void fill_globals(void) {
code << std::dec;
for (auto x : code_global_variables) {
if (global_var_init.count(x.first)) {
code << x.second << "\tDW %D "
<< evaluate(global_var_init.at(x.first)) << std::endl;
} else {
code << x.second << std::endl;
}
}
for (auto x : code_global_arrays) {
if (global_arr_init.count(x.first)) {
if (global_arr_init.at(x.first).size() > 0) {
code << x.second.first << "\tDW %D "
<< evaluate(global_arr_init.at(x.first).at(0))
<< std::endl;
} else {
code << x.second.first << std::endl;
}
for (size_t i = 1; i < x.second.second; ++i) {
if (global_arr_init.at(x.first).size() > i) {
code << "\tDW %D "
<< evaluate(global_arr_init.at(x.first).at(i))
<< std::endl;
} else {
code << "\tDW %D 0" << std::endl;
}
}
} else {
code << x.second.first << "\tDW %D 0" << std::endl;
for (size_t i = 1; i < x.second.second; ++i) {
code << "\tDW %D 0" << std::endl;
}
}
}
}
void fill_consts(void) {
code << std::dec;
for (auto x : code_constants) {
code << x.second << "\tDW %D " << x.first << std::endl;
}
code.close();
}
void return_to_stack(bool array) {
// dumb initial design if we are updating the element of array then we are
// saving the value in register R3, and if not then we are saving the value
// in register R0
if (array) {
code << "\tPUSH R3" << std::endl;
} else {
code << "\tPUSH R0" << std::endl;
}
}
void skip_start(void) { code << "\tJP SK" << skip_count << std::endl; }
void skip_end(void) { code << "SK" << skip_count++ << std::endl; }
void arr_init(std::string name, int index) {
code << "\tPOP R0" << std::endl;
code << "\tMOVE "
<< std::prev(code_global_arrays.equal_range(name).second)->second.first
<< ", R1" << std::endl;
code << "\tADD R1, %D " << index * 4 << ", R1 " << std::endl;
code << "\tSTORE R0, (R1)" << std::endl;
}