-
Notifications
You must be signed in to change notification settings - Fork 0
/
bytecode_generator.c
321 lines (273 loc) · 8.19 KB
/
bytecode_generator.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdbool.h>
#define ONE_CHAR 'X'
#define ZERO_CHAR '_'
void to_binary_representation(char* sink, char n)
{
for (size_t bit = 0; bit < 8; ++bit)
{
char c = (n >> bit) & 1 ? ONE_CHAR : ZERO_CHAR;
sink[7 - bit] = c;
}
}
bool is_whitespace(char c)
{
return c == ' '
|| c == '\t'
|| c == '\n';
}
size_t read_lines(char** lines, char* file_buffer, size_t file_buffer_size)
{
size_t lines_index = 0;
size_t file_buffer_index = 0;
char c = file_buffer[file_buffer_index];
while (file_buffer_index < file_buffer_size)
{
char line_buffer[256];
size_t line_buffer_index = 0;
while ((c = file_buffer[file_buffer_index++]) != '\n'
&& file_buffer_index < file_buffer_size)
{
line_buffer[line_buffer_index++] = c;
}
if (line_buffer_index)
{ // Not a completely empty line.
char* line = (char*)malloc(256 * sizeof(char));
if (line)
{
// Copy into allocated char array and remove comments.
size_t line_index = 0;
while (line_index < line_buffer_index
&& line_buffer[line_index] != ';')
{
line[line_index] = line_buffer[line_index];
// No line breaks in our lines array!
assert(line[line_index] != '\n');
++line_index;
}
if (line_index)
{
line[line_index] = 0;
lines[lines_index++] = line;
}
else
{
free(line);
}
}
else
{
printf("Memory allocation failure.\n");
}
}
}
// Remove lines with only whitespace.
for (size_t i = 0; i < lines_index; ++i)
{
bool contains_valid_char = false;
char* line = lines[i];
for (size_t j = 0; line[j]; ++j)
{
if (line[j] == ONE_CHAR || line[j] == ZERO_CHAR)
{
contains_valid_char = true;
}
}
if (!contains_valid_char)
{
free(line);
for (size_t j = i; j < lines_index - 1; ++j)
{
lines[j] = lines[j + 1];
}
--lines_index;
}
}
return lines_index;
}
int generate_bytecode(char* file_buffer, size_t file_buffer_size)
{
/* Preprocessing:
- Remove comments and empty lines
- Create label map, mapping labels to their instruction addresses
- Remove labels, whitespace
- Pad instructions and numeric constants
- Replace label references with mapped addresses
- Make sure we end up only with valid characters
*/
char** lines = (char**)malloc(2048 * sizeof(char*));
size_t lines_index = 0;
if (lines)
{
lines_index = read_lines(lines, file_buffer, file_buffer_size);
char labels[256][5];
char label_instruction_addresses[256][9];
size_t labels_index = 0;
// Map labels to their instruction addresses and remove labels.
for (size_t i = 0; i < lines_index; ++i)
{
if (!is_whitespace(lines[i][0]))
{
for (size_t j = 0; j < 4; ++j)
{
labels[labels_index][j] = lines[i][j];
// Remove label (set to spaces).
lines[i][j] = ' ';
}
labels[labels_index][4] = 0;
// Save the line number as the instruction address.
to_binary_representation(
label_instruction_addresses[labels_index],
(char)i);
label_instruction_addresses[labels_index][8] = 0;
++labels_index;
}
}
// Remove leading whitespace, pad tokens, replace label references.
for (size_t i = 0; i < lines_index; ++i)
{
char unpadded_token[9];
size_t token_length = 0;
for (size_t j = 0, c = lines[i][j]; c; ++j, c = lines[i][j])
{
if (!is_whitespace(c))
{
unpadded_token[token_length++] = c;
}
}
unpadded_token[token_length] = 0;
// Replace token if its a label.
if (token_length == 4)
{
for (size_t j = 0; j < labels_index; ++j)
{
if (strcmp(unpadded_token, labels[j]) == 0)
{
strcpy(unpadded_token, label_instruction_addresses[j]);
}
}
// Set token_length
token_length = 8;
}
for (size_t j = 0; j < 8; ++j)
{
lines[i][j] = ZERO_CHAR;
}
for (size_t j = 0; j < token_length; ++j)
{
lines[i][8 - token_length + j] = unpadded_token[j];
}
// Pad the numeric constants and label references with a 1 for push.
if (token_length == 7 || token_length == 8)
{
lines[i][0] = ONE_CHAR;
}
lines[i][8] = 0;
}
/*
for (int i = 0; i < labels_index; ++i)
{
printf("%s : %s\n",
labels[i],
label_instruction_addresses[i]);
}
*/
// Check if everything is valid.
for (size_t i = 0; i < lines_index; ++i)
{
size_t line_index = 0;
char c;
while ((c = lines[i][line_index++]))
{
if (c != ONE_CHAR && c != ZERO_CHAR)
{
printf("Bytecode generation error!\n Invalid character?\n");
return 0;
}
}
}
for (size_t i = 0; i < lines_index; ++i)
{
printf("%s\n", lines[i]);
free(lines[i]);
}
free(lines);
return 1;
}
else
{
printf("Memory allocation failure.\n");
return 0;
}
return 0;
}
int main(int argc, char* argv[])
{
if (argc != 2)
{
printf("Usage: %s file\n", argv[0]);
exit(1);
}
FILE* file;
file = fopen(argv[1], "r");
if (file)
{
// Get the filesize. Note: We still want to generate an empty bytecode
// file when reading an empty input file.
size_t filesize;
fseek(file, 0L, SEEK_END);
filesize = ftell(file);
// Reset filehandle to the beginning of the file.
fseek(file, 0L, SEEK_SET);
// Allocate file buffer
char* file_buffer = (char*)malloc(filesize);
if (file_buffer)
{
size_t bytes_read = 0;
while (!feof(file))
{
size_t count = fread(file_buffer, sizeof(char), filesize, file);
if (ferror(file))
{
printf("Error while reading the file.\n");
}
else
{
bytes_read += count;
}
}
if (bytes_read == filesize)
{
int result = generate_bytecode(file_buffer, bytes_read);
if (!result)
{
printf("Error during bytecode generation.\n");
exit(1);
}
}
else if (filesize == 0)
{
// Create empty output file
}
else
{
printf("Error while reading the file: %d\n", bytes_read);
}
free(file_buffer);
}
else
{
printf("Memory allocation failure.\nCould not allocate %d bytes.\n", filesize);
}
// Close the filehandle.
fclose(file);
}
else
{
printf("Could not open file %s.\n", argv[1]);
exit(1);
}
}