-
Notifications
You must be signed in to change notification settings - Fork 0
/
biffle.c
240 lines (225 loc) · 4.92 KB
/
biffle.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "biffle.h"
#include "hashmap.h"
#include "ops.h"
#define match(s1, s2) strncmp(s1, s2, sizeof(s2)) == 0
typedef struct label_s {
char label_name[256];
int pos;
} label_t;
FILE* fp = 0;
map_t label_map;
int arg_value(char* arg)
{
trimwhitespace(arg);
if (arg[0] == '.') {
int* pos = malloc(sizeof(int) + 1);
*pos = 0;
int error = hashmap_get(label_map, arg, (void**)(&pos));
int ret = 0;
if (error == MAP_OK) ret = *pos;
return ret;
}
if (arg[0] == '\'')
return (int) arg[1];
if (arg[0] == '%')
return 30 + atoi(arg++);
if (arg[0] == '"')
return 0;
if (match(arg, "zero"))
return zero;
if (match(arg, "hlt"))
return hlt;
if (match(arg, "pc"))
return pc;
if (match(arg, "jcmp"))
return jcmp;
if (match(arg, "res"))
return res;
if (match(arg, "ein"))
return ein;
if (match(arg, "zwei"))
return zwei;
if (match(arg, "sc"))
return sc;
if (match(arg, "sp"))
return sp;
if (match(arg, "tmp1"))
return tmp1;
if (match(arg, "tmp2"))
return tmp2;
if (match(arg, "tmp3"))
return tmp3;
if (match(arg, "tmp4"))
return tmp4;
if (match(arg, "tmp5"))
return tmp5;
if (match(arg, "tmp6"))
return tmp6;
if (match(arg, "tmp7"))
return tmp7;
if (match(arg, "tmp8"))
return tmp8;
return atoi(arg);
}
void assemble_op(char* opstr)
{
char* opcpy = malloc(256);
strncpy(opcpy, opstr, 256);
char* opname = strtok(opstr, " \n");
char* starget1 = strtok(NULL, " \n");
char* starget2 = starget1 ? strtok(NULL, " \n") : NULL;
int target1 = 0;
int target2 = 0;
if (starget1 != NULL)
target1 = arg_value(starget1);
if (starget2 != NULL)
target2 = arg_value(starget2);
if (match(opname, "add"))
op_ADD(target1, target2);
else if (match(opname, "sub"))
op_SUB(target1, target2);
else if (match(opname, "addi"))
op_ADDI(target1, target2);
else if (match(opname, "subi"))
op_SUBI(target1, target2);
else if (match(opname, "print")) {
op_PRINT(opcpy + 6);
}
else if (match(opname, "debug"))
op_DEBUG(target1);
else if (match(opname, "printn"))
op_PRINTN(target1);
else if (match(opname, "load"))
op_LOAD(target1, target2);
else if (match(opname, "store"))
op_STORE(target1, target2);
else if (match(opname, "set"))
op_SET(target1, target2);
else if (match(opname, "mult"))
op_MULT(target1, target2);
else if (match(opname, "div"))
op_DIV(target1, target2);
else if (match(opname, "mod"))
op_MOD(target1, target2);
else if (match(opname, "getc"))
op_GETC(target1);
else if (match(opname, "mov"))
op_MOV(target1, target2);
else if (match(opname, "comp"))
op_COMP(target1, target2);
else if (match(opname, "bif"))
op_BIF(target1, target2);
else if (match(opname, "cne"))
op_CNE(target1, target2);
else if (match(opname, "jump"))
op_JUMP(target1);
else if (match(opname, "put"))
op_PUT(target1);
else if (match(opname, "inc"))
op_INC(target1);
else if (match(opname, "dec"))
op_DEC(target1);
else if (match(opname, "hlt"))
op_hlt();
else if (match(opname, "ret"))
op_RET();
else if (match(opname, "noop"))
op_NOOP();
else if (match(opname, "call"))
op_CALL(target1);
else if (match(opname, "push"))
op_PUSH(target1);
else if (match(opname, "pop"))
op_POP(target1);
op_idx++;
}
void assemble_program()
{
size_t len = 0;
char * line = NULL;
ssize_t read;
int idx = 0;
while ((read = getline(&line, &len, fp)) != -1) {
if (read == 1) continue;
line = trimwhitespace(line);
if (line[0] == ';') continue;
if (line[0] == '.') {
char* lcpy = malloc(len);
int* midx = malloc(sizeof(int));
*midx = idx;
strncpy(lcpy, line, len);
hashmap_put(label_map, lcpy, midx);
} else {
idx ++;
}
}
rewind(fp);
printf(">>>");
MEM_INIT();
SET(hlt, 1);
cell_while();
while ((read = getline(&line, &len, fp)) != -1) {
if (read == 1) continue;
line = trimwhitespace(line);
if (line[0] == ';') continue;
if (line[0] == '.') continue;
assemble_op(line);
}
move(hlt);
cell_end();
}
char *trimwhitespace(char *str)
{
char *end;
// Trim leading space
while(isspace(*str)) str++;
if(*str == 0) // All spaces?
return str;
// Trim trailing space
end = str + strlen(str) - 1;
while(end > str && isspace(*end)) end--;
// Write new null terminator
*(end+1) = 0;
return str;
}
int main(int argc, char** argv)
{
if (argc > 1) {
chdir(get_dir_name(argv[1]));
fp = fopen(get_file_name(argv[1]), "r");
} else {
fp = stdin;
}
label_map = hashmap_new();
assemble_program();
}
char* get_dir_name(char* file)
{
char* fcpy = malloc(256);
strncpy(fcpy, file, 256);
size_t lastc = strlen(fcpy) - 1;
while(fcpy[lastc] != '/' && lastc >= 0) {
fcpy[lastc] = 0;
lastc--;
}
return fcpy;
}
char* get_file_name(char* file)
{
char* fcpy = malloc(256);
strncpy(fcpy, file, 256);
size_t c = 0;
size_t slen = strlen(fcpy);
char* name = fcpy;
while(fcpy[c] != '/') {
name++;
c++;
if (c == slen)
return fcpy;
}
name++;
return name;
}