-
Notifications
You must be signed in to change notification settings - Fork 0
/
lbf.c
198 lines (169 loc) · 3.34 KB
/
lbf.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
/*
* LBF: A JIT-compiled version of the ever-popular programming language whose
* real name cannot be given in polite company.
*
* Written from scratch by Pete Elmore ([email protected]), Halloween 2009,
* specifically so he could play with lightning some more.
*
* It's public domain.
*/
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <lightning.h>
#include "lbf.h"
int (*f)();
char cells[MAX_CELLS];
int celli = 0;
jit_insn code_buffer[MAX_CODE_SIZE];
bf_jump *head = 0;
void compile_begin()
{
f = (int (*)())(jit_set_ip(code_buffer).vptr);
jit_prolog(0);
}
void compile_end()
{
if(head) {
fprintf(stderr, "Syntax error: Unmatched '['!\n");
exit(1);
}
jit_movi_i(JIT_RET, 0);
jit_ret();
jit_flush_code(code_buffer, jit_get_ip().ptr);
}
void compile_inc()
{
jit_ldi_p(JIT_R0, &celli);
jit_addi_p(JIT_R0, JIT_R0, cells);
jit_ldr_uc(JIT_R1, JIT_R0);
jit_addi_ui(JIT_R1, JIT_R1, 1);
jit_str_uc(JIT_R0, JIT_R1);
}
void compile_dec()
{
jit_ldi_p(JIT_R0, &celli);
jit_addi_p(JIT_R0, JIT_R0, cells);
jit_ldr_uc(JIT_R1, JIT_R0);
jit_subi_ui(JIT_R1, JIT_R1, 1);
jit_str_uc(JIT_R0, JIT_R1);
}
void compile_while()
{
bf_jump *m;
m = malloc(sizeof(bf_jump));
if(!m) {
fprintf(stderr,
"Somehow, we ran out of memory compiling your code?\n");
abort();
}
m->link = head;
head = m;
jit_ldi_p(JIT_R0, &celli);
jit_addi_p(JIT_R0, JIT_R0, cells);
jit_ldr_uc(JIT_R1, JIT_R0);
m->fwd = jit_beqi_ui(jit_forward(), JIT_R1, 0);
m->back = jit_get_ip().ptr;
}
void compile_done()
{
bf_jump *m = head;
if(!m) {
fprintf(stderr, "Syntax error: Unmatched ']'!\n");
exit(2);
}
head = head->link;
jit_ldi_p(JIT_R0, &celli);
jit_addi_p(JIT_R0, JIT_R0, cells);
jit_ldr_uc(JIT_R1, JIT_R0);
jit_bnei_ui(m->back, JIT_R1, 0);
jit_patch(m->fwd);
free(m);
}
// TODO: Wrap-around when we hit a boundary?
void compile_next()
{
jit_ldi_p(JIT_R0, &celli);
jit_addi_i(JIT_R0, JIT_R0, 1);
jit_sti_p(&celli, JIT_R0);
}
// TODO: Wrap-around when we hit a boundary?
void compile_prev()
{
jit_ldi_p(JIT_R0, &celli);
jit_subi_i(JIT_R0, JIT_R0, 1);
jit_sti_p(&celli, JIT_R0);
}
void compile_read()
{
jit_ldi_p(JIT_R0, &celli);
jit_addi_p(JIT_R0, JIT_R0, cells);
jit_movi_i(JIT_R1, 1);
jit_movi_i(JIT_R2, 0);
jit_str_c(JIT_R0, JIT_R2);
jit_prepare(3);
jit_pusharg_i(JIT_R1);
jit_pusharg_p(JIT_R0);
jit_pusharg_i(JIT_R2);
jit_finish(read);
}
void compile_write()
{
jit_ldi_p(JIT_R0, &celli);
jit_addi_p(JIT_R0, JIT_R0, cells);
jit_movi_i(JIT_R1, 1);
jit_prepare(3);
jit_pusharg_i(JIT_R1);
jit_pusharg_p(JIT_R0);
jit_pusharg_i(JIT_R1);
jit_finish(write);
}
int main(int argc, char **argv)
{
int fd;
char c;
if(argc != 2) {
fprintf(stderr, "A JIT-compiled interpeter for BF.\n"
"Usage: %s filename.bf\n", argv[0]);
exit(1);
}
fd = open(argv[1], O_RDONLY);
if(fd < 0) {
fprintf(stderr, "Couldn't open %s.\n", argv[1]);
exit(2);
}
compile_begin();
while(read(fd, &c, 1)) {
switch (c) {
case '+':
compile_inc();
break;
case '-':
compile_dec();
break;
case '[':
compile_while();
break;
case ']':
compile_done();
break;
case '>':
compile_next();
break;
case '<':
compile_prev();
break;
case ',':
compile_read();
break;
case '.':
compile_write();
break;
}
}
close(fd);
compile_end();
return f();
}