forked from grigory-rechistov/interpreters-comparison
-
Notifications
You must be signed in to change notification settings - Fork 2
/
translated-inline.c
592 lines (558 loc) · 27.7 KB
/
translated-inline.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
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
/* translated-inline.c - a inline binary translation sample engine
for a stack virtual machine.
Copyright (c) 2015, 2016 Valery Konychev. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of interpreters-comparison nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
#ifndef __x86_64__
/* The program generates machine code, only specific platforms are supported */
#error This program is designed to compile only on Intel64/AMD64 platform.
#error Sorry.
#endif
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <assert.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <string.h>
#include <sys/mman.h>
#include <setjmp.h>
#include <math.h>
#include "common.h"
/* Capsules and diassemble */
#include "inline_data.h"
/* setjmp/longjmp context buffer to be reachable from within generated code */
static jmp_buf return_buf;
/* Global pointer to be accessible from generated code.
Uses GNU extension to statically occupy host R15 register. */
register cpu_t * pcpu asm("r15");
/* Area for generated code. It is put into the .text section to be reachable
from the rest of the code (relative branch to fit in 32 bits) */
/* For explanation of '#' character,
see https://gcc.gnu.org/ml/gcc-help/2010-09/msg00088.html */
char gen_code[JIT_CODE_SIZE] __attribute__ ((section (".text#")))
__attribute__ ((aligned(4096)));
/* TODO:a global - not good. Should be moved into cpu state or somewhere else */
static uint64_t steplimit = LLONG_MAX;
/* Strings for capsules. It's used for substitution address. Before call
printf or puts fuction address have been placed in edi register. */
/* For printf("[%d]\n", tmp1); in sr_Print capsule. */
const char str_printf[] = "[%d]\n";
/* For printf("Stack underflow\n"); in pop function. */
const char str_pop[] = "Stack underflow\n";
/* For printf("Stack overflow\n"); in push function. */
const char str_push[] = "Stack overflow\n";
static inline decode_t decode_at_address(const Instr_t* prog, uint32_t addr) {
assert(addr < PROGRAM_SIZE);
decode_t result = {0};
Instr_t raw_instr = prog[addr];
result.opcode = raw_instr;
switch (raw_instr) {
case Instr_Nop:
case Instr_Halt:
case Instr_Print:
case Instr_Swap:
case Instr_Dup:
case Instr_Inc:
case Instr_Add:
case Instr_Sub:
case Instr_Mul:
case Instr_Rand:
case Instr_Dec:
case Instr_Drop:
case Instr_Over:
case Instr_Mod:
case Instr_And:
case Instr_Or:
case Instr_Xor:
case Instr_SHL:
case Instr_SHR:
case Instr_Rot:
case Instr_SQRT:
case Instr_Pick:
result.length = 1;
break;
case Instr_Push:
case Instr_JNE:
case Instr_JE:
case Instr_Jump:
result.length = 2;
assert(addr+1 < PROGRAM_SIZE);
result.immediate = (int32_t)prog[addr+1];
break;
case Instr_Break:
default: /* Undefined instructions equal to Break */
result.length = 1;
result.opcode = Instr_Break;
break;
}
return result;
}
/* Supporting functions for in-lined service routines */
static void enter_generated_code(void* addr) {
__asm__ __volatile__ ( "jmp *%0"::"r"(addr):);
}
static void exit_generated_code() {
longjmp(return_buf, 1);
}
static inline void push(cpu_t *pcpu, uint32_t v) {
assert(pcpu);
if (pcpu->sp >= STACK_CAPACITY-1) {
printf("Stack overflow\n");
pcpu->state = Cpu_Break;
exit_generated_code();
}
pcpu->stack[++pcpu->sp] = v;
}
static void inline_translate_program(const Instr_t *prog,
char *out_code, void **entrypoints, int len) {
assert(prog);
assert(out_code);
assert(entrypoints);
/* An IA-32 instruction "CALL rel32" is used as a trampoline to invoke
service routines. A template for it is "call .+0x00000005" */
const char call_template_code[] = { 0xe8, 0x00, 0x00, 0x00, 0x00 };
const int call_template_size = sizeof(call_template_code);
int i = 0; /* Address of current guest instruction */
char* cur = out_code; /* Where to put new code */
while (i < len) {
decode_t decoded = decode_at_address(prog, i);
entrypoints[i] = (void*) cur;
/* Address of function relative of the end of call.
Exaple: addr_exit1 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)call_addr_in_capsule;
cur - address of start curent binary capsules in translated code.
call_addr_in_capsule - address of call function in binary capsule. Look in the inline_data.h
Need to copy relative address in (cur + call_addr_in_capsule + 1),
because first byte is opcode of call instruction */
int addr_exit1 = 0;
int addr_exit2 = 0;
int addr_exit3 = 0;
int addr_puts1 = 0;
int addr_puts2 = 0;
int addr_push1 = 0;
int addr_push2 = 0;
int addr_push3 = 0;
int addr_abort = 0;
int addr_printf = 0;
int addr_rand = 0;
/* Absolute address of strings. To printf or puts function work need to move absolute address in
edi register. */
int addr_str1 = (intptr_t)&str_printf;
int addr_str2 = (intptr_t)&str_pop;
int addr_str3 = (intptr_t)&str_push;
/* Length of current binary capsule. */
int len = 0;
/* For instruction with immediate. If it's branch instruction in imm need to place immediate + 2,
2 is a length of inctruction. */
int imm = 0;
switch(decoded.opcode) {
case Instr_Nop:
addr_exit1 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0x2b;
len = sizeof(bin_sr_Nop);
assert(cur + len - out_code < JIT_CODE_SIZE);
memcpy(cur, bin_sr_Nop, len);
memcpy(cur+0x2c, &addr_exit1, 4);
memcpy(cur+0x1a, &steplimit, 8);
break;
case Instr_Halt:
addr_exit1 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0x11;
len = sizeof(bin_sr_Halt);
assert(cur + len - out_code < JIT_CODE_SIZE);
memcpy(cur, bin_sr_Halt, len);
memcpy(cur+0x12,&addr_exit1, 4);
break;
case Instr_Print:
addr_exit1 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0x57;
addr_exit2 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0x6f;
addr_puts1 = (intptr_t)&puts - (intptr_t)cur - call_template_size - (intptr_t)0x61;
addr_printf = (intptr_t)&printf - (intptr_t)cur - call_template_size - (intptr_t)0x24;
addr_abort = (intptr_t)&abort - (intptr_t)cur - call_template_size - (intptr_t)0x74;
len = sizeof(bin_sr_Print);
assert(cur + len - out_code < JIT_CODE_SIZE);
memcpy(cur, bin_sr_Print, len);
memcpy(cur+0x58, &addr_exit1, 4);
memcpy(cur+0x70, &addr_exit2, 4);
memcpy(cur+0x25, &addr_printf, 4);
memcpy(cur+0x62, &addr_puts1, 4);
memcpy(cur+0x75, &addr_abort, 4);
memcpy(cur+0x1a, &addr_str1, 4);
memcpy(cur+0x5d, &addr_str2, 4);
memcpy(cur+0x43, &steplimit, 8);
break;
case Instr_Swap:
addr_exit1 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0x8a;
addr_exit2 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0x91;
addr_exit3 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0xa9;
addr_puts1 = (intptr_t)&puts - (intptr_t)cur - call_template_size - (intptr_t)0x7c;
addr_puts2 = (intptr_t)&puts - (intptr_t)cur - call_template_size - (intptr_t)0x9b;
addr_abort = (intptr_t)&abort - (intptr_t)cur - call_template_size - (intptr_t)0xae;
len = sizeof(bin_sr_Swap);
assert(cur + len - out_code < JIT_CODE_SIZE);
memcpy(cur, bin_sr_Swap, len);
memcpy(cur+0x7d, &addr_puts1, 4);
memcpy(cur+0x9c, &addr_puts2, 4);
memcpy(cur+0xaf, &addr_abort, 4);
memcpy(cur+0x8b, &addr_exit1, 4);
memcpy(cur+0x92, &addr_exit2, 4);
memcpy(cur+0xaa, &addr_exit3, 4);
memcpy(cur+0x78, &addr_str2, 4);
memcpy(cur+0x97, &addr_str3, 4);
memcpy(cur+0x68, &steplimit, 8);
break;
case Instr_Dup:
addr_exit1 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0x79;
addr_exit2 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0x80;
addr_exit3 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0x98;
addr_puts1 = (intptr_t)&puts - (intptr_t)cur - call_template_size - (intptr_t)0x6b;
addr_puts2 = (intptr_t)&puts - (intptr_t)cur - call_template_size - (intptr_t)0x8a;
addr_abort = (intptr_t)&abort - (intptr_t)cur - call_template_size - (intptr_t)0x9d;
len = sizeof(bin_sr_Dup);
assert(cur + len - out_code < JIT_CODE_SIZE);
memcpy(cur, bin_sr_Dup, len);
memcpy(cur+0x7a, &addr_exit1, 4);
memcpy(cur+0x81, &addr_exit2, 4);
memcpy(cur+0x99, &addr_exit3, 4);
memcpy(cur+0x6c, &addr_puts1, 4);
memcpy(cur+0x8b, &addr_puts2, 4);
memcpy(cur+0x9e, &addr_abort, 4);
memcpy(cur+0x86, &addr_str2, 4);
memcpy(cur+0x67, &addr_str3, 4);
memcpy(cur+0x57, &steplimit, 8);
break;
case Instr_Inc:
addr_exit1 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0x5f;
addr_exit2 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0x77;
addr_exit3 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0x8f;
addr_puts1 = (intptr_t)&puts - (intptr_t)cur - call_template_size - (intptr_t)0x69;
addr_puts2 = (intptr_t)&puts - (intptr_t)cur - call_template_size - (intptr_t)0x81;
addr_abort = (intptr_t)&abort - (intptr_t)cur - call_template_size - (intptr_t)0x94;
len = sizeof(bin_sr_Inc);
assert(cur + len - out_code < JIT_CODE_SIZE);
memcpy(cur, bin_sr_Inc, len);
memcpy(cur+0x60, &addr_exit1, 4);
memcpy(cur+0x78, &addr_exit2, 4);
memcpy(cur+0x90, &addr_exit3, 4);
memcpy(cur+0x6a, &addr_puts1, 4);
memcpy(cur+0x82, &addr_puts2, 4);
memcpy(cur+0x95, &addr_abort, 4);
memcpy(cur+0x7d, &addr_str2, 4);
memcpy(cur+0x65, &addr_str3, 4);
memcpy(cur+0x4e, &steplimit, 8);
break;
case Instr_Add:
addr_exit1 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0x7c;
addr_exit2 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0x83;
addr_exit3 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0xa0;
addr_puts1 = (intptr_t)&puts - (intptr_t)cur - call_template_size - (intptr_t)0x6e;
addr_puts2 = (intptr_t)&puts - (intptr_t)cur - call_template_size - (intptr_t)0x92;
addr_abort = (intptr_t)&abort - (intptr_t)cur - call_template_size - (intptr_t)0x88;
len = sizeof(bin_sr_Add);
assert(cur + len - out_code < JIT_CODE_SIZE);
memcpy(cur, bin_sr_Add, len);
memcpy(cur+0x7d, &addr_exit1, 4);
memcpy(cur+0x84, &addr_exit2, 4);
memcpy(cur+0xa1, &addr_exit3, 4);
memcpy(cur+0x6f, &addr_puts1, 4);
memcpy(cur+0x93, &addr_puts2, 4);
memcpy(cur+0x89, &addr_abort, 4);
memcpy(cur+0x6a, &addr_str2, 4);
memcpy(cur+0x8e, &addr_str3, 4);
memcpy(cur+0x5a, &steplimit, 8);
break;
case Instr_Sub:
addr_exit1 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0x7c;
addr_exit2 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0x83;
addr_exit3 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0xa0;
addr_puts1 = (intptr_t)&puts - (intptr_t)cur - call_template_size - (intptr_t)0x6e;
addr_puts2 = (intptr_t)&puts - (intptr_t)cur - call_template_size - (intptr_t)0x92;
addr_abort = (intptr_t)&abort - (intptr_t)cur - call_template_size - (intptr_t)0x88;
len = sizeof(bin_sr_Sub);
assert(cur + len - out_code < JIT_CODE_SIZE);
memcpy(cur, bin_sr_Sub, len);
memcpy(cur+0x7d, &addr_exit1, 4);
memcpy(cur+0x84, &addr_exit2, 4);
memcpy(cur+0xa1, &addr_exit3, 4);
memcpy(cur+0x6f, &addr_puts1, 4);
memcpy(cur+0x93, &addr_puts2, 4);
memcpy(cur+0x89, &addr_abort, 4);
memcpy(cur+0x6a, &addr_str2, 4);
memcpy(cur+0x8e, &addr_str3, 4);
memcpy(cur+0x5a, &steplimit, 8);
break;
case Instr_Mul:
addr_exit1 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0x7d;
addr_exit2 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0x84;
addr_exit3 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0xa1;
addr_puts1 = (intptr_t)&puts - (intptr_t)cur - call_template_size - (intptr_t)0x6f;
addr_puts2 = (intptr_t)&puts - (intptr_t)cur - call_template_size - (intptr_t)0x93;
addr_abort = (intptr_t)&abort - (intptr_t)cur - call_template_size - (intptr_t)0x89;
len = sizeof(bin_sr_Mul);
assert(cur + len - out_code < JIT_CODE_SIZE);
memcpy(cur, bin_sr_Mul, len);
memcpy(cur+0x7e, &addr_exit1, 4);
memcpy(cur+0x85, &addr_exit2, 4);
memcpy(cur+0xa2, &addr_exit3, 4);
memcpy(cur+0x70, &addr_puts1, 4);
memcpy(cur+0x94, &addr_puts2, 4);
memcpy(cur+0x8a, &addr_abort, 4);
memcpy(cur+0x6b, &addr_str2, 4);
memcpy(cur+0x8f, &addr_str3, 4);
memcpy(cur+0x5b, &steplimit, 8);
break;
case Instr_Rand:
addr_exit1 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0x50;
addr_exit2 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0x68;
addr_puts1 = (intptr_t)&puts - (intptr_t)cur - call_template_size - (intptr_t)0x5a;
addr_abort = (intptr_t)&abort - (intptr_t)cur - call_template_size - (intptr_t)0x6d;
addr_rand = (intptr_t)&rand - (intptr_t)cur - call_template_size;
len = sizeof(bin_sr_Rand);
assert(cur + len - out_code < JIT_CODE_SIZE);
memcpy(cur, bin_sr_Rand, len);
memcpy(cur+0x51, &addr_exit1, 4);
memcpy(cur+0x69, &addr_exit2, 4);
memcpy(cur+0x5b, &addr_puts1, 4);
memcpy(cur+0x6e, &addr_abort, 4);
memcpy(cur+0x56, &addr_str3, 4);
memcpy(cur+0x01, &addr_rand, 4);
memcpy(cur+0x3f, &steplimit, 8);
break;
case Instr_Dec:
addr_exit1 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0x5f;
addr_exit2 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0x77;
addr_exit3 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0x8f;
addr_puts1 = (intptr_t)&puts - (intptr_t)cur - call_template_size - (intptr_t)0x69;
addr_puts2 = (intptr_t)&puts - (intptr_t)cur - call_template_size - (intptr_t)0x81;
addr_abort = (intptr_t)&abort - (intptr_t)cur - call_template_size - (intptr_t)0x94;
len = sizeof(bin_sr_Dec);
assert(cur + len - out_code < JIT_CODE_SIZE);
memcpy(cur, bin_sr_Dec, len);
memcpy(cur+0x60, &addr_exit1, 4);
memcpy(cur+0x78, &addr_exit2, 4);
memcpy(cur+0x90, &addr_exit3, 4);
memcpy(cur+0x6a, &addr_puts1, 4);
memcpy(cur+0x82, &addr_puts2, 4);
memcpy(cur+0x95, &addr_abort, 4);
memcpy(cur+0x7d, &addr_str2, 4);
memcpy(cur+0x65, &addr_str3, 4);
memcpy(cur+0x4e, &steplimit, 8);
break;
case Instr_Drop:
addr_exit1 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0x42;
addr_exit2 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0x5a;
addr_puts1 = (intptr_t)&puts - (intptr_t)cur - call_template_size - (intptr_t)0x4c;
addr_abort = (intptr_t)&abort - (intptr_t)cur - call_template_size - (intptr_t)0x5f;
len = sizeof(bin_sr_Drop);
assert(cur + len - out_code < JIT_CODE_SIZE);
memcpy(cur, bin_sr_Drop, len);
memcpy(cur+0x43, &addr_exit1, 4);
memcpy(cur+0x5b, &addr_exit2, 4);
memcpy(cur+0x4d, &addr_puts1, 4);
memcpy(cur+0x60, &addr_abort, 4);
memcpy(cur+0x48, &addr_str2, 4);
memcpy(cur+0x31, &steplimit, 8);
break;
case Instr_Over:
addr_exit1 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0x91;
addr_exit2 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0x98;
addr_puts1 = (intptr_t)&puts - (intptr_t)cur - call_template_size - (intptr_t)0x83;
addr_push1 = (intptr_t)&push - (intptr_t)cur - call_template_size - (intptr_t)0x3b;
addr_push2 = (intptr_t)&push - (intptr_t)cur - call_template_size - (intptr_t)0x45;
addr_push3 = (intptr_t)&push - (intptr_t)cur - call_template_size - (intptr_t)0x50;
addr_abort = (intptr_t)&abort - (intptr_t)cur - call_template_size - (intptr_t)0x9d;
len = sizeof(bin_sr_Over);
assert(cur + len - out_code < JIT_CODE_SIZE);
memcpy(cur, bin_sr_Over, len);
memcpy(cur+0x3c, &addr_push1, 4);
memcpy(cur+0x46, &addr_push2, 4);
memcpy(cur+0x51, &addr_push3, 4);
memcpy(cur+0x84, &addr_puts1, 4);
memcpy(cur+0x9e, &addr_abort, 4);
memcpy(cur+0x92, &addr_exit1, 4);
memcpy(cur+0x99, &addr_exit2, 4);
memcpy(cur+0x7f, &addr_str2, 4);
memcpy(cur+0x6f, &steplimit, 8);
break;
case Instr_Mod:
addr_exit1 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0x87;
addr_exit2 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0x9f;
addr_puts1 = (intptr_t)&puts - (intptr_t)cur - call_template_size - (intptr_t)0x79;
addr_puts2 = (intptr_t)&puts - (intptr_t)cur - call_template_size - (intptr_t)0x91;
addr_abort = (intptr_t)&abort - (intptr_t)cur - call_template_size - (intptr_t)0xa4;
len = sizeof(bin_sr_Mod);
assert(cur + len - out_code < JIT_CODE_SIZE);
memcpy(cur, bin_sr_Mod, len);
memcpy(cur+0x88, &addr_exit1, 4);
memcpy(cur+0xa0, &addr_exit2, 4);
memcpy(cur+0x7a, &addr_puts1, 4);
memcpy(cur+0x92, &addr_puts2, 4);
memcpy(cur+0xa5, &addr_abort, 4);
memcpy(cur+0x8d, &addr_str2, 4);
memcpy(cur+0x75, &addr_str3, 4);
memcpy(cur+0x65, &steplimit, 8);
break;
case Instr_Push:
addr_exit1 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0x4e;
addr_exit2 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0x66;
addr_puts1 = (intptr_t)&puts - (intptr_t)cur - call_template_size - (intptr_t)0x58;
addr_abort = (intptr_t)&abort - (intptr_t)cur - call_template_size - (intptr_t)0x6b;
imm = decoded.immediate;
len = sizeof(bin_sr_Push);
assert(cur + len - out_code < JIT_CODE_SIZE);
memcpy(cur, bin_sr_Push, len);
memcpy(cur+0x4f, &addr_exit1, 4);
memcpy(cur+0x67, &addr_exit2, 4);
memcpy(cur+0x59, &addr_puts1, 4);
memcpy(cur+0x6c, &addr_abort, 4);
memcpy(cur+0x54, &addr_str3, 4);
memcpy(cur+0x23, &imm, 4);
memcpy(cur+0x3d, &steplimit, 8);
break;
case Instr_JNE:
addr_exit1 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0x56;
addr_exit2 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0x6e;
addr_puts1 = (intptr_t)&puts - (intptr_t)cur - call_template_size - (intptr_t)0x60;
addr_abort = (intptr_t)&abort - (intptr_t)cur - call_template_size - (intptr_t)0x73;
imm = decoded.immediate + 2;
len = sizeof(bin_sr_Jne);
assert(cur + len - out_code < JIT_CODE_SIZE);
memcpy(cur, bin_sr_Jne, len);
memcpy(cur+0x57, &addr_exit1, 4);
memcpy(cur+0x6f, &addr_exit2, 4);
memcpy(cur+0x61, &addr_puts1, 4);
memcpy(cur+0x74, &addr_abort, 4);
memcpy(cur+0x5c, &addr_str2, 4);
memcpy(cur+0x4b, &imm, 4);
memcpy(cur+0x39, &steplimit, 8);
break;
case Instr_JE:
addr_exit1 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0x56;
addr_exit2 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0x6e;
addr_puts1 = (intptr_t)&puts - (intptr_t)cur - call_template_size - (intptr_t)0x60;
addr_abort = (intptr_t)&abort - (intptr_t)cur - call_template_size - (intptr_t)0x73;
imm = decoded.immediate + 2;
len = sizeof(bin_sr_Je);
assert(cur + len - out_code < JIT_CODE_SIZE);
memcpy(cur, bin_sr_Je, len);
memcpy(cur+0x57, &addr_exit1, 4);
memcpy(cur+0x6f, &addr_exit2, 4);
memcpy(cur+0x61, &addr_puts1, 4);
memcpy(cur+0x74, &addr_abort, 4);
memcpy(cur+0x5c, &addr_str2, 4);
memcpy(cur+0x4b, &imm, 4);
memcpy(cur+0x39, &steplimit, 8);
break;
case Instr_Jump:
addr_exit1 = (intptr_t)&exit_generated_code - (intptr_t)cur - call_template_size
- (intptr_t)0x0e;
len = sizeof(bin_sr_Jump);
imm = decoded.immediate + 2;
assert(cur + len - out_code < JIT_CODE_SIZE);
memcpy(cur, bin_sr_Jump, len);
memcpy(cur+0x0f, &addr_exit1, 4);
memcpy(cur+0x03, &imm, 4);
break;
case Instr_Break:
default:
break;
}
i += decoded.length;
cur += len;
}
}
int main(int argc, char **argv) {
steplimit = parse_args(argc, argv);
cpu_t cpu = init_cpu();
pcpu = &cpu;
/* Code section is protected from writes by default, un-protect it */
if (mprotect(gen_code, JIT_CODE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC)) {
perror("mprotect");
exit(2);
}
/* Pre-populate resulting code buffer with INT3 (machine code 0xCC).
This will help to catch jumps to wrong locations */
memset(gen_code, 0xcc, JIT_CODE_SIZE);
void* entrypoints[PROGRAM_SIZE] = {0}; /* a map of guest PCs to capsules */
inline_translate_program(cpu.pmem, gen_code, entrypoints, PROGRAM_SIZE);
setjmp(return_buf); /* Will get here from generated code. */
while (cpu.state == Cpu_Running && cpu.steps < steplimit) {
if (cpu.pc > PROGRAM_SIZE) {
cpu.state = Cpu_Break;
break;
}
enter_generated_code(entrypoints[cpu.pc]); /* Will not return */
}
assert(cpu.state != Cpu_Running || cpu.steps == steplimit);
/* Print CPU state */
printf("CPU executed %ld steps. End state \"%s\".\n",
cpu.steps, cpu.state == Cpu_Halted? "Halted":
cpu.state == Cpu_Running? "Running": "Break");
printf("PC = %#x, SP = %d\n", cpu.pc, cpu.sp);
printf("Stack: ");
for (int32_t i=cpu.sp; i >= 0 ; i--) {
printf("%#10x ", cpu.stack[i]);
}
printf("%s\n", cpu.sp == -1? "(empty)": "");
free(LoadedProgram);
return cpu.state == Cpu_Halted ||
(cpu.state == Cpu_Running &&
cpu.steps == steplimit)?0:1;
}