-
Notifications
You must be signed in to change notification settings - Fork 140
/
forth.c
219 lines (193 loc) · 4.91 KB
/
forth.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
#include <ir/ir.h>
#include <target/util.h>
#define FORTH_MEM_SIZE_STR "16777216"
#define FORTH_ADDR_MASK UINT_MAX
static const char* FORTH_REG_NAMES[] = {
"reg-a", "reg-b", "reg-c", "reg-d", "reg-bp", "reg-sp", "reg-pc"
};
static void init_state_forth(Data* data) {
reg_names = FORTH_REG_NAMES;
for (int i = 0; i < 7; i++) {
emit_line("variable %s", reg_names[i]);
emit_line("0 %s !", reg_names[i]);
}
emit_line("variable mem");
emit_line(FORTH_MEM_SIZE_STR " cells allocate throw mem !");
for (int mp = 0; data; data = data->next, mp++) {
if (data->v) {
emit_line("%d mem @ %d cells + !", data->v, mp);
}
}
emit_line("");
emit_line("variable tmpchar");
emit_line(": getc");
inc_indent();
emit_line("tmpchar 1 stdin read-file");
emit_line("0= swap 1 = and if tmpchar @ else 0 then");
dec_indent();
emit_line(";");
}
static void forth_emit_func_prologue(int func_id) {
emit_line("");
emit_line(": func%d", func_id);
inc_indent();
emit_line("begin");
inc_indent();
emit_line("reg-pc @ dup %d >= swap %d < and",
func_id * CHUNKED_FUNC_SIZE, (func_id + 1) * CHUNKED_FUNC_SIZE);
dec_indent();
emit_line("while");
inc_indent();
emit_line("reg-pc @");
emit_line("dup -1 = if");
inc_indent();
}
static void forth_emit_func_epilogue(void) {
dec_indent();
emit_line("then");
emit_line("drop");
emit_line("1 reg-pc +!");
dec_indent();
emit_line("repeat");
dec_indent();
emit_line(";");
}
static void forth_emit_pc_change(int pc) {
dec_indent();
emit_line("then");
emit_line("dup %d = if", pc);
inc_indent();
}
static const char* forth_value_str(Value* v) {
if (v->type == REG) {
return format("%s @", reg_names[v->reg]);
} else if (v->type == IMM) {
return format("%d", v->imm);
} else {
error("invalid value");
}
}
static const char* forth_src_str(Inst* inst) {
return forth_value_str(&inst->src);
}
static void emit_cmp(Inst* inst, const char* cmp_op) {
emit_line("%s @ %s %s if 1 else 0 then %s !",
reg_names[inst->dst.reg], forth_src_str(inst),
cmp_op, reg_names[inst->dst.reg]);
}
static void emit_jump(Inst* inst) {
emit_line("%s 1 - reg-pc !", forth_value_str(&inst->jmp));
}
static void emit_cond_jump(Inst* inst, const char* cmp_op) {
emit_line("%s @ %s %s if",
reg_names[inst->dst.reg], forth_src_str(inst), cmp_op);
inc_indent();
emit_jump(inst);
dec_indent();
emit_line("then");
}
static void forth_emit_inst(Inst* inst) {
switch (inst->op) {
case MOV:
emit_line("%s %s !", forth_src_str(inst), reg_names[inst->dst.reg]);
break;
case ADD:
emit_line("%s @ %s + %d and",
reg_names[inst->dst.reg], forth_src_str(inst), FORTH_ADDR_MASK);
emit_line("%s !", reg_names[inst->dst.reg]);
break;
case SUB:
emit_line("%s @ %s - %d and",
reg_names[inst->dst.reg], forth_src_str(inst), FORTH_ADDR_MASK);
emit_line("%s !", reg_names[inst->dst.reg]);
break;
case LOAD:
emit_line("mem @ %s cells + @ %s !",
forth_src_str(inst), reg_names[inst->dst.reg]);
break;
case STORE:
emit_line("%s @ mem @ %s cells + !",
reg_names[inst->dst.reg], forth_src_str(inst));
break;
case PUTC:
emit_line("%s emit", forth_src_str(inst));
break;
case GETC:
emit_line("getc %s !", reg_names[inst->dst.reg]);
break;
case EXIT:
emit_line("bye");
break;
case DUMP:
break;
case EQ:
emit_cmp(inst, "=");
break;
case NE:
emit_cmp(inst, "<>");
break;
case LT:
emit_cmp(inst, "<");
break;
case GT:
emit_cmp(inst, ">");
break;
case LE:
emit_cmp(inst, "<=");
break;
case GE:
emit_cmp(inst, ">=");
break;
case JEQ:
emit_cond_jump(inst, "=");
break;
case JNE:
emit_cond_jump(inst, "<>");
break;
case JLT:
emit_cond_jump(inst, "<");
break;
case JGT:
emit_cond_jump(inst, ">");
break;
case JLE:
emit_cond_jump(inst, "<=");
break;
case JGE:
emit_cond_jump(inst, ">=");
break;
case JMP:
emit_jump(inst);
break;
default:
error("oops");
}
}
void target_forth(Module* module) {
init_state_forth(module->data);
emit_line("");
int num_funcs = emit_chunked_main_loop(module->text,
forth_emit_func_prologue,
forth_emit_func_epilogue,
forth_emit_pc_change,
forth_emit_inst);
emit_line("");
emit_line(": main");
inc_indent();
emit_line("begin");
inc_indent();
emit_line("reg-pc @ %d /", CHUNKED_FUNC_SIZE);
for (int i = 0; i < num_funcs; i++) {
emit_line("dup %d = if func%d else", i, i);
}
for (int i = 0; i < num_funcs; i++) {
emit_line("then");
}
emit_line("drop");
dec_indent();
emit_line("0 until");
dec_indent();
emit_line(";");
emit_line("");
emit_line("main");
}