forked from embedded2015/jit-construct
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jit-x64.dasc
134 lines (126 loc) · 2.55 KB
/
jit-x64.dasc
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
#include <stdint.h>
#include "util.h"
|.arch x64
|.actionlist actions
|
|// Use rbx as our cell pointer.
|// Since rbx is a callee-save register, it will be preserved
|// across our calls to getchar and putchar.
|.define PTR, rbx
|
|// Macro for calling a function.
|// In cases where our target is <=2**32 away we can use
|// | call &addr
|// But since we don't know if it will be, we use this safe
|// sequence instead.
|.macro callp, addr
| mov64 rax, (uintptr_t)addr
| call rax
|.endmacro
#define Dst &state
#define MAX_NESTING 256
int main(int argc, char *argv[])
{
if (argc < 2) err("Usage: jit-x64 <inputfile>");
dasm_State *state;
initjit(&state, actions);
unsigned int maxpc = 0;
int pcstack[MAX_NESTING];
int *top = pcstack, *limit = pcstack + MAX_NESTING;
int cnt;
// Function prologue.
| push PTR
| mov PTR, rdi // rdi store 1st argument
for (char *p = read_file(argv[1]); *p; p++) {
switch (*p) {
case '>':
for (cnt=1, p++; *p == '>'; p++)
cnt++;
p--;
if(cnt>1) {
| add PTR, cnt
break;
}
| inc PTR
break;
case '<':
for (cnt=1, p++; *p == '<'; p++)
cnt++;
p--;
if(cnt>1) {
| sub PTR, cnt
break;
}
| dec PTR
break;
case '+':
for (cnt=1, p++; *p == '+'; p++)
cnt++;
p--;
if(cnt>1) {
| add byte [PTR], cnt
break;
}
| inc byte [PTR]
break;
case '-':
for (cnt=1, p++; *p == '-'; p++)
cnt++;
p--;
if(cnt>1) {
| sub byte [PTR], cnt
break;
}
| dec byte [PTR]
break;
case '.':
| movzx edi, byte [PTR]
| callp putchar
break;
case ',':
| callp getchar
| mov byte [PTR], al
break;
case '[':
if(*(p+1)=='-' && *(p+2)==']') {
| mov byte [PTR] ,0
p+=2;
break;
}
if (*(p+1)=='>' && *(p+2)=='+' && *(p+3)=='<'
&& *(p+4)=='-' && *(p+4)==']') {
cnt = *p;
| add byte [PTR+1], cnt
| mov byte [PTR], 0
break;
}
if (top == limit) err("Nesting too deep.");
// Each loop gets two pclabels: at the beginning and end.
// We store pclabel offsets in a stack to link the loop
// begin and end together.
maxpc += 2;
*top++ = maxpc;
dasm_growpc(&state, maxpc);
| cmp byte [PTR], 0
| je =>(maxpc-2)
|=>(maxpc-1):
break;
case ']':
if (top == pcstack) err("Unmatched ']'");
top--;
| cmp byte [PTR], 0
| jne =>(*top-1)
|=>(*top-2):
break;
}
}
// Function epilogue.
| pop PTR
| ret
void (*fptr)(char*) = jitcode(&state);
char *mem = calloc(30000, 1);
fptr(mem);
free(mem);
free_jitcode(fptr);
return 0;
}