forked from sysprog21/jit-construct
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jit-arm.dasc
94 lines (86 loc) · 1.86 KB
/
jit-arm.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
#include <stdint.h>
#include "util.h"
|.arch arm
|.actionlist actions
|
|// Use r4 as our cell pointer.
|// Since r4 is a callee-save register, it will be preserved
|// across our calls to getchar and putchar.
|.define PTR, r4
#define Dst &state
#define MAX_NESTING 256
int main(int argc, char *argv[])
{
if (argc < 2) err("Usage: jit-arm <inputfile>");
dasm_State *state;
initjit(&state, actions);
unsigned int maxpc = 0;
int pcstack[MAX_NESTING];
int *top = pcstack, *limit = pcstack + MAX_NESTING;
// Function prologue.
| push {PTR, r5, r7, lr}
| mov PTR, r0
for (char *p = read_file(argv[1]); *p; p++) {
switch (*p) {
case '>':
| add PTR, PTR, #1
break;
case '<':
| sub PTR, PTR, #1
break;
case '+':
| ldrb r5, [PTR]
| add r5, r5, #1
| strb r5, [PTR]
break;
case '-':
| ldrb r5, [PTR]
| sub r5, r5, #1
| strb r5, [PTR]
break;
case '.':
| mov r0, #1 // stdout
| mov r1, PTR
| mov r2, #1
| mov r7, #4 // sys_write
| svc #0
break;
case ',':
| mov r0, #0 // stdin
| mov r1, PTR
| mov r2, #1
| mov r7, #3 // sys_read
| svc #0
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.
maxpc += 2; // add two labels
*top++ = maxpc;
dasm_growpc(&state, maxpc);
| ldrb r5, [PTR]
| cmp r5, #0
| beq =>(maxpc-2)
|=>(maxpc-1):
break;
case ']':
if (top == pcstack) err("Unmatched ']'");
top--;
| ldrb r5, [PTR]
| cmp r5, #0
| bne =>(*top-1)
|=>(*top-2):
break;
}
}
// Function epilogue.
| pop {PTR, r5, r7, pc}
void (*fptr)(char*) = jitcode(&state);
char *mem = calloc(30000, 1);
fptr(mem);
free(mem);
free_jitcode(fptr);
return 0;
}