-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenx86_64.cpp
More file actions
620 lines (580 loc) · 22.6 KB
/
Copy pathgenx86_64.cpp
File metadata and controls
620 lines (580 loc) · 22.6 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
#include "yadfa.h"
struct code_generation_error : public std::runtime_error {
code_generation_error(const char *what) : std::runtime_error(what) {}
};
struct function_definition
{
std::string name;
function_instruction function;
};
struct variable_info {
size_t index = std::numeric_limits<size_t>::max();
std::string type;
};
using function_instruction_vec = std::map<std::string, function_definition>;
void dump_x86_64(const asmjit::CodeHolder &code) {
using namespace asmjit;
CodeBuffer &buffer = code.textSection()->buffer();
for (size_t i = 0; i < buffer.size(); i++)
printf("%02X", buffer[i]);
std::cout << '\n';
}
std::map<std::string, variable_info>
populate_variable_indexes(const instruction_vec &i_vec) {
std::map<std::string, variable_info> variables_indexes;
size_t num_variables = 0;
for (size_t i_index = 0; i_index != i_vec.size(); ++i_index) {
const auto &instr = i_vec[i_index];
if (instr->type == op_var) {
++num_variables;
auto var_name =
static_cast<binary_instruction *>(i_vec[i_index].get())->arg_1;
auto var_type =
static_cast<binary_instruction *>(i_vec[i_index].get())->arg_2;
variables_indexes[var_name] = variable_info{num_variables, var_type};
}
}
return variables_indexes;
}
void gen_prolog(asmjit::x86::Assembler &a) {
using namespace asmjit;
a.push(x86::rbp);
a.mov(x86::rbp, x86::rsp);
}
void gen_epilog(asmjit::x86::Assembler &a) {
using namespace asmjit;
a.pop(x86::rbp);
}
size_t
gen_allocation(const std::map<std::string, variable_info> &variables_indexes,
asmjit::x86::Assembler &a) {
using namespace asmjit;
// TODO hardcoded for now, only 32 bit values
constexpr size_t variable_size = 8;
const size_t allocated_mem = variables_indexes.size() * variable_size;
a.sub(x86::rsp, allocated_mem);
return allocated_mem;
}
void deallocate_and_return(size_t allocated_mem, asmjit::x86::Assembler &a) {
using namespace asmjit;
a.add(x86::rsp, allocated_mem);
gen_epilog(a);
a.ret();
}
void populate_label(const instruction_vec &i_vec, asmjit::x86::Assembler &a,
std::map<size_t, asmjit::Label> &label_per_instruction,
int index) {
const auto &instr = i_vec[index];
auto instruction_label = a.newLabel();
label_per_instruction[index] = instruction_label;
}
asmjit::x86::Gp get_register_by_index(int index) {
using namespace asmjit;
x86::Gp reg;
switch (index) {
case 1:
reg = x86::rdi;
break;
case 2:
reg = x86::rsi;
break;
case 3:
reg = x86::rdx;
break;
case 4:
reg = x86::rcx;
break;
case 5:
reg = x86::r8;
break;
case 6:
reg = x86::r9;
break;
default:
return reg;
}
return reg;
}
void push_arguments_for_builtin_fun(
asmjit::x86::Assembler &a,
const std::map<std::string, variable_info> &variables_info,
const builtin_function &builtin_fun, const std::vector<std::string> &args) {
using namespace asmjit;
constexpr auto max_number_args_via_register = 6;
constexpr size_t variable_size = 8;
auto args_number = args.size() - 1; // first arg is always function name
int args_on_stack = args_number - max_number_args_via_register;
if (args_number <= max_number_args_via_register) {
for (int arg_index = 1; arg_index != args.size(); ++arg_index) {
if (!isdigit(args[arg_index].front())) {
auto var_info_it = variables_info.find(args[arg_index]);
if (var_info_it != variables_info.end()) {
auto var_offset = var_info_it->second.index * (-variable_size);
a.mov(get_register_by_index(arg_index),
x86::dword_ptr(x86::rbp, var_offset));
}
} else {
a.mov(get_register_by_index(arg_index), std::stoi(args[arg_index]));
}
}
} else {
if (args_on_stack > 0) {
for (int arg_index = 1; arg_index != max_number_args_via_register + 1;
++arg_index) {
if (!isdigit(args[arg_index].front())) {
} else {
a.mov(get_register_by_index(arg_index), std::stoi(args[arg_index]));
}
}
// arguments are passed in reverse order from last to first one
// and first will have index 7
// as first 6 arguments are passed via registers
for (int arg_on_stack_index = args_on_stack; arg_on_stack_index != 0;
--arg_on_stack_index) {
if (!isdigit(args[max_number_args_via_register + arg_on_stack_index]
.front())) {
} else {
a.push(std::stoi(
args[max_number_args_via_register + arg_on_stack_index]));
}
}
}
}
}
void push_arguments_for_def_fun(
asmjit::x86::Assembler &a,
const std::map<std::string, variable_info> &variables_info,
const std::vector<std::string> &args) {
using namespace asmjit;
constexpr auto max_number_args_via_register = 6;
constexpr size_t variable_size = 8;
auto args_number = args.size() - 1; // first arg is always function name
int args_on_stack = args_number - max_number_args_via_register;
if (args_number <= max_number_args_via_register) {
for (int arg_index = 1; arg_index != args.size(); ++arg_index) {
if (!isdigit(args[arg_index].front())) {
auto var_info_it = variables_info.find(args[arg_index]);
if (var_info_it != variables_info.end()) {
auto var_offset = var_info_it->second.index * (-variable_size);
a.mov(get_register_by_index(arg_index),
x86::dword_ptr(x86::rbp, var_offset));
}
} else {
a.mov(get_register_by_index(arg_index), std::stoi(args[arg_index]));
}
}
} else {
if (args_on_stack > 0) {
for (int arg_index = 1; arg_index != max_number_args_via_register + 1;
++arg_index) {
if (!isdigit(args[arg_index].front())) {
} else {
a.mov(get_register_by_index(arg_index), std::stoi(args[arg_index]));
}
}
// arguments are passed in reverse order from last to first one
// and first will have index 7
// as first 6 arguments are passed via registers
for (int arg_on_stack_index = args_on_stack; arg_on_stack_index != 0;
--arg_on_stack_index) {
if (!isdigit(args[max_number_args_via_register + arg_on_stack_index]
.front())) {
} else {
a.push(std::stoi(
args[max_number_args_via_register + arg_on_stack_index]));
}
}
}
}
}
void gen_x64_instruction(const instruction_vec &i_vec,
std::map<std::string, variable_info> &variables_info,
std::map<size_t, asmjit::Label> &label_per_instruction,
std::map<std::string, asmjit::Label> &function_labels,
function_instruction_vec &function_vec,
asmjit::x86::Assembler &a, const label_table <able,
int index,
const builtin_functions_map &builtin_functions) {
using namespace asmjit;
const auto &instr = i_vec[index];
a.bind(label_per_instruction[index]);
// for now mov handles lvalues as well as rvalues
// other instructions always use lvalues
if (instr->type == op_mov) {
auto var_name = static_cast<binary_instruction *>(instr.get())->arg_1;
auto var_value = static_cast<binary_instruction *>(instr.get())->arg_2;
auto var_info = variables_info[var_name];
std::uint8_t variable_size = 8;
auto var_offset = var_info.index * (-variable_size);
if (!isdigit(var_value[0])) {
auto rhs_info = variables_info[var_value];
auto rhs_offset = rhs_info.index * (-variable_size);
a.mov(x86::rax, x86::qword_ptr(x86::rbp, rhs_offset));
a.mov(x86::qword_ptr(x86::rbp, var_offset), x86::rax);
} else {
a.mov(x86::dword_ptr(x86::rbp, var_offset), std::stoi(var_value));
}
}
if (instr->type == op_add) {
auto arg_1 = static_cast<three_addr_instruction *>(instr.get())->arg_1;
auto arg_2 = static_cast<three_addr_instruction *>(instr.get())->arg_2;
auto arg_3 = static_cast<three_addr_instruction *>(instr.get())->arg_3;
// TODO assuming args are lvalues
std::uint8_t variable_size = 8;
auto arg_1_info = variables_info[arg_1];
auto arg_2_info = variables_info[arg_2];
auto arg_3_info = variables_info[arg_3];
auto arg_1_offset = arg_1_info.index * (-variable_size);
auto arg_2_offset = arg_2_info.index * (-variable_size);
auto arg_3_offset = arg_3_info.index * (-variable_size);
a.mov(x86::rax, x86::dword_ptr(x86::rbp, arg_2_offset));
a.add(x86::rax, x86::dword_ptr(x86::rbp, arg_3_offset));
a.mov(x86::dword_ptr(x86::rbp, arg_1_offset), x86::eax);
}
if (instr->type == op_sub) {
auto arg_1 = static_cast<three_addr_instruction *>(instr.get())->arg_1;
auto arg_2 = static_cast<three_addr_instruction *>(instr.get())->arg_2;
auto arg_3 = static_cast<three_addr_instruction *>(instr.get())->arg_3;
// TODO assuming args are lvalues
std::uint8_t variable_size = 8;
auto arg_1_info = variables_info[arg_1];
auto arg_2_info = variables_info[arg_2];
auto arg_3_info = variables_info[arg_3];
auto arg_1_offset = arg_1_info.index * (-variable_size);
auto arg_2_offset = arg_2_info.index * (-variable_size);
auto arg_3_offset = arg_3_info.index * (-variable_size);
a.mov(x86::rax, x86::dword_ptr(x86::rbp, arg_2_offset));
a.sub(x86::rax, x86::dword_ptr(x86::rbp, arg_3_offset));
a.mov(x86::dword_ptr(x86::rbp, arg_1_offset), x86::eax);
}
if (instr->type == op_mul) {
auto arg_1 = static_cast<three_addr_instruction *>(instr.get())->arg_1;
auto arg_2 = static_cast<three_addr_instruction *>(instr.get())->arg_2;
auto arg_3 = static_cast<three_addr_instruction *>(instr.get())->arg_3;
// TODO assuming args are lvalues
std::uint8_t variable_size = 8;
auto arg_1_info = variables_info[arg_1];
auto arg_2_info = variables_info[arg_2];
auto arg_3_info = variables_info[arg_3];
auto arg_1_offset = arg_1_info.index * (-variable_size);
auto arg_2_offset = arg_2_info.index * (-variable_size);
auto arg_3_offset = arg_3_info.index * (-variable_size);
a.mov(x86::rax, x86::dword_ptr(x86::rbp, arg_2_offset));
a.mov(x86::rcx, x86::dword_ptr(x86::rbp, arg_3_offset));
a.mul(x86::rcx);
a.mov(x86::dword_ptr(x86::rbp, arg_1_offset), x86::eax);
}
if (instr->type == op_div) {
auto arg_1 = static_cast<three_addr_instruction *>(instr.get())->arg_1;
auto arg_2 = static_cast<three_addr_instruction *>(instr.get())->arg_2;
auto arg_3 = static_cast<three_addr_instruction *>(instr.get())->arg_3;
// TODO assuming args are lvalues
std::uint8_t variable_size = 8;
auto arg_1_info = variables_info[arg_1];
auto arg_2_info = variables_info[arg_2];
auto arg_3_info = variables_info[arg_3];
auto arg_1_offset = arg_1_info.index * (-variable_size);
auto arg_2_offset = arg_2_info.index * (-variable_size);
auto arg_3_offset = arg_3_info.index * (-variable_size);
a.mov(x86::rax, x86::dword_ptr(x86::rbp, arg_2_offset));
a.cdq();
a.idiv(x86::dword_ptr(x86::rbp, arg_3_offset));
a.mov(x86::dword_ptr(x86::rbp, arg_1_offset), x86::eax);
}
if (instr->type == op_push) {
// TODO assuming args are lvalues
std::uint8_t variable_size = 8;
auto arg = static_cast<unary_instruction *>(instr.get())->arg_1;
auto arg_info = variables_info[arg];
auto arg_offset = arg_info.index * (-variable_size);
a.push(x86::dword_ptr(x86::rbp, arg_offset));
}
if (instr->type == op_pop) {
// TODO assuming args are lvalues
std::uint8_t variable_size = 8;
auto arg = static_cast<unary_instruction *>(instr.get())->arg_1;
auto arg_info = variables_info[arg];
auto arg_offset = arg_info.index * (-variable_size);
a.pop(x86::dword_ptr(x86::rbp, arg_offset));
}
if (instr->type == op_jmp) {
// TODO handle lvalues
// only jmp backward for now
int next_instruction_index = 0;
auto arg = static_cast<unary_instruction *>(instr.get())->arg_1;
// only if digit for now
if (isdigit(arg[0])) {
auto jmp_offset = std::stoi(arg);
if (jmp_offset > 0) {
jmp_offset += 1;
}
next_instruction_index = index + jmp_offset;
} else {
auto label_it = ltable.instance.find(arg);
if (label_it != ltable.instance.end()) {
next_instruction_index = label_it->second;
} else {
std::string message = "label : " + arg + " does not exists";
throw code_generation_error(message.c_str());
}
}
auto label_it = label_per_instruction.find(next_instruction_index);
if (label_it == label_per_instruction.end()) {
code_generation_error("instruction is out of range");
}
a.jmp(label_it->second);
}
switch (instr->type) {
case op_cmp_eq:
case op_cmp_neq:
case op_cmp_gt:
case op_cmp_lt:
case op_cmp_lte:
case op_cmp_gte:
auto arg_1 = static_cast<three_addr_instruction *>(instr.get())->arg_1;
auto arg_2 = static_cast<three_addr_instruction *>(instr.get())->arg_2;
auto arg_3 = static_cast<three_addr_instruction *>(instr.get())->arg_3;
// TODO assuming args are lvalues
std::uint8_t variable_size = 8;
auto arg_1_info = variables_info[arg_1];
auto arg_2_info = variables_info[arg_2];
auto arg_3_info = variables_info[arg_3];
auto arg_1_offset = arg_1_info.index * (-variable_size);
auto arg_2_offset = arg_2_info.index * (-variable_size);
auto arg_3_offset = arg_3_info.index * (-variable_size);
a.mov(x86::eax, x86::dword_ptr(x86::rbp, arg_2_offset));
a.cmp(x86::eax, x86::dword_ptr(x86::rbp, arg_3_offset));
auto false_label = a.newLabel();
auto end_label = a.newLabel();
if (instr->type == op_cmp_eq) {
a.jne(false_label);
} else if (instr->type == op_cmp_neq) {
a.je(false_label);
} else if (instr->type == op_cmp_gt) {
a.jng(false_label);
} else if (instr->type == op_cmp_lt) {
a.jnl(false_label);
} else if (instr->type == op_cmp_lte) {
a.jnle(false_label);
} else if (instr->type == op_cmp_gte) {
a.jnge(false_label);
}
a.mov(x86::eax, 1);
a.mov(x86::dword_ptr(x86::rbp, arg_1_offset), x86::eax);
a.jmp(end_label);
a.bind(false_label);
a.mov(x86::eax, 0);
a.mov(x86::dword_ptr(x86::rbp, arg_1_offset), x86::eax);
a.bind(end_label);
break;
}
if (instr->type == op_if) {
auto false_label = a.newLabel();
auto arg = static_cast<binary_instruction *>(instr.get())->arg_1;
auto arg_info = variables_info[arg];
std::uint8_t variable_size = 8;
auto arg_offset = arg_info.index * (-variable_size);
auto offset = static_cast<binary_instruction *>(instr.get())->arg_2;
a.mov(x86::ebx, x86::dword_ptr(x86::rbp, arg_offset));
a.cmp(x86::ebx, 0);
a.jng(false_label);
// only if digit for now
int next_instruction_index = 0;
if (isdigit(offset[0])) {
auto jmp_offset = std::stoi(offset);
next_instruction_index = index + jmp_offset;
} else {
auto label_it = ltable.instance.find(offset);
if (label_it != ltable.instance.end()) {
next_instruction_index = label_it->second;
} else {
std::string message = "label : " + arg + " does not exists";
throw code_generation_error(message.c_str());
}
}
auto label_it = label_per_instruction.find(next_instruction_index);
if (label_it == label_per_instruction.end()) {
code_generation_error("instruction is out of range");
}
a.jmp(label_it->second);
a.bind(false_label);
}
if (instr->type == op_nop) {
a.nop();
}
if (instr->type == op_function) {
// function codegen is postponed
auto args = static_cast<function_instruction *>(instr.get())->args;
auto body =
std::move(static_cast<function_instruction *>(instr.get())->body);
function_definition def{args.front(), function_instruction(op_function, args,
std::move(body))};
def.name = args.front();
function_vec.insert({args.front(), def});
}
if (instr->type == op_call) {
auto args = static_cast<call_instruction *>(instr.get())->args;
constexpr size_t number_of_args_passed_via_regs = 6;
constexpr size_t bytes_64 = 8;
constexpr size_t fun_name_arg = 1;
// first argument is always function name
// number of arguments has to be calculaled by
// subtract it and number passed via regs (6) from all arguments
// and multiply by size of each argument on stack which 8 bytes
int deallocateArgMem =
(args.size() - fun_name_arg - number_of_args_passed_via_regs) *
bytes_64;
auto fun_name = args.front();
auto label_it = function_labels.find(fun_name);
if (label_it == function_labels.end()) {
std::string message = "function " + fun_name + " does not exits";
auto builtin_functions_it = builtin_functions.find(fun_name);
if (builtin_functions_it == builtin_functions.end()) {
throw code_generation_error(message.c_str());
} else {
// TODO only rvalue arguments for now
push_arguments_for_builtin_fun(a, variables_info,
builtin_functions_it->second, args);
a.call(asmjit::imm(builtin_functions_it->second.function_pointer));
if (args.size() > number_of_args_passed_via_regs + fun_name_arg) {
a.add(x86::rsp, deallocateArgMem);
}
}
} else {
// TODO only rvalue arguments for now
push_arguments_for_def_fun(a, variables_info, args);
a.call(label_it->second);
if (args.size() > number_of_args_passed_via_regs + fun_name_arg) {
a.add(x86::rsp, deallocateArgMem);
}
}
}
// op_ret is no-op for now
if (instr->type == op_ret) {
}
if (instr->type == op_pop_args) {
auto args = static_cast<pop_args_instruction *>(instr.get())->args;
// TODO for now it's taking only first six arguments via registers
for (size_t arg_index = 0; arg_index != args.size(); ++arg_index) {
assert(arg_index < 6);
auto var_info = variables_info[args[arg_index].first];
std::uint8_t variable_size = 8;
auto var_offset = var_info.index * (-variable_size);
a.mov(x86::qword_ptr(x86::rbp, var_offset),
get_register_by_index(arg_index + 1));
}
}
}
void gen_x64(const instruction_vec &i_vec, const asmjit::JitRuntime &rt,
asmjit::CodeHolder &code, const label_table <able,
const builtin_functions_map &builtin_functions) {
using namespace asmjit;
code.init(rt.environment());
x86::Assembler a(&code);
std::map<size_t, Label> label_per_instruction;
std::map<std::string, Label> function_labels;
auto variables_indexes = populate_variable_indexes(i_vec);
function_instruction_vec function_vec;
auto mainLabel = a.newLabel();
a.jmp(mainLabel);
// there are two passes
// first we traverse all functions
// and cache them
// then we traverse cache to generate code for each
// funtion
for (int index = 0; index != i_vec.size(); ++index) {
if (i_vec[index]->type != op_function)
continue;
gen_x64_instruction(i_vec, variables_indexes, label_per_instruction,
function_labels, function_vec, a, ltable, index,
builtin_functions);
}
// allocate function arguments
for (auto &fun : function_vec) {
auto &function_def = fun.second;
auto &function_body = function_def.function.body;
const auto &function_args = function_def.function.args;
if (!function_args.empty()) {
std::vector<std::pair<std::string, std::string>> args_vec;
for (size_t arg_index = 1; arg_index != function_args.size();
arg_index = arg_index + 2) {
args_vec.push_back(
{function_args[arg_index], function_args[arg_index + 1]});
}
for (size_t arg_index = 1; arg_index != function_args.size();
arg_index = arg_index + 2) {
function_body.insert(
function_body.begin(),
std::make_unique<pop_args_instruction>(op_pop_args, args_vec));
function_body.insert(function_body.begin(),
std::make_unique<binary_instruction>(
op_var, function_args[arg_index],
function_args[arg_index + 1]));
}
}
}
// traverse internal function cache and generate code
// generate code for functions
for (const auto &fun : function_vec) {
auto function_name = fun.first;
const auto& function_def = fun.second;
const auto &function_body = function_def.function.body;
const auto &function_args = function_def.function.args;
auto function_label = a.newLabel();
function_labels[function_name] = function_label;
auto variables_indexes_function_body =
populate_variable_indexes(function_body);
a.bind(function_label);
gen_prolog(a);
// allocate memory
auto allocated_mem_fun = gen_allocation(variables_indexes_function_body, a);
for (int body_index = 0; body_index != function_body.size(); ++body_index) {
populate_label(function_body, a, label_per_instruction, body_index);
}
for (int body_index = 0; body_index != function_body.size(); ++body_index) {
gen_x64_instruction(function_body, variables_indexes_function_body,
label_per_instruction, function_labels, function_vec,
a, ltable, body_index, builtin_functions);
}
// deallocate
deallocate_and_return(allocated_mem_fun, a);
}
// generate code for "main" function
a.bind(mainLabel);
gen_prolog(a);
// allocate memory
auto allocated_mem = gen_allocation(variables_indexes, a);
for (int index = 0; index != i_vec.size(); ++index) {
populate_label(i_vec, a, label_per_instruction, index);
}
for (int index = 0; index != i_vec.size(); ++index) {
gen_x64_instruction(i_vec, variables_indexes, label_per_instruction,
function_labels, function_vec, a, ltable, index,
builtin_functions);
}
// deallocate
deallocate_and_return(allocated_mem, a);
}
int exec(const instruction_vec &i_vec, const label_table <able,
const builtin_functions_map &builtin_functions) {
typedef int (*Func)(void);
asmjit::CodeHolder code;
asmjit::JitRuntime rt;
gen_x64(i_vec, rt, code, ltable, builtin_functions);
Func fn;
asmjit::Error err = rt.add(&fn, &code);
if (err)
return -1;
int result = fn();
// no longer needed
(void)result;
return 0;
}
void dump_x86_64(const instruction_vec &i_vec, const label_table <able,
const builtin_functions_map &builtin_functions) {
typedef int (*Func)(void);
asmjit::CodeHolder code;
asmjit::JitRuntime rt;
gen_x64(i_vec, rt, code, ltable, builtin_functions);
dump_x86_64(code);
}