forked from sysprog21/jit-construct
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jit-x64.dasc
169 lines (156 loc) · 3.35 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
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
#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
#define MAX_LEN 256
int clear_loop(char *p){
if(*(p+1) == '-'&&*(p+2) == ']')
return 1;
else
return 0;
}
int continuous_count(char *p)
{
char *ptr = p;
int count = 0;
while (*ptr == *p) {
count++;
ptr++;
}
return count;
}
int check_loops(char *p,int *index,int *mult, int *p_offset)
{
int count=0;
int res,offset = 0,_index = 0;
if (*(p+1) != '-') return -1;
p += 2;
count+=2;
while (*p != ']') {
if (*p == '[' || *p == '-' ||
*p == '.' || *p == ',')
return -1;
res = continuous_count(p);
if (*p == '>') offset += res;
else if (*p == '<') offset -= res;
else if (*p == '+') {
index[_index] = offset;
mult[_index] = res;
_index++;
}
p += res;
count+=res;
}
if (offset != 0) return -1;
*p_offset = count;
return _index;
}
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 count;//
int index[MAX_LEN];
int mult[MAX_LEN];
int p_offset;
// Function prologue.
| push PTR
| mov PTR, rdi // rdi store 1st argument
for (char *p = read_file(argv[1]); *p; p++) {
switch (*p) {
case '>':
count=continuous_count(p);
p+=count-1;
| add PTR, count
break;
case '<':
count=continuous_count(p);
p+=count-1;
| sub PTR, count
break;
case '+':
count=continuous_count(p);
p+=count-1;
| add byte [PTR], count
break;
case '-':
count=continuous_count(p);
p+=count-1;
| sub byte [PTR], count
break;
case '.':
| movzx edi, byte [PTR]
| callp putchar
break;
case ',':
| callp getchar
| mov byte [PTR], al
break;
case '[':
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.
if( (count = check_loops(p, index, mult, &p_offset)) >=0 ){
for(int id=0; id<count; id++){
| mov cl, byte [PTR]
| mov ch, 0
| add PTR, index[id]
| mov ax, mult[id]
| imul ax, cx
| add byte [PTR], al
| sub PTR, index[id]
}
p+=p_offset;
| mov byte [PTR], 0
break;
}
else{
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;
}