-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
frontend_asm68k.c
465 lines (412 loc) · 13.1 KB
/
frontend_asm68k.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*
* Copyright (C) 2022 Clownacy
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <ctype.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "semantic.h"
#include "strcmpci.h"
static int total_arguments;
static char **arguments;
static void DefinitionCallback(void *internal, void* const user_data, void (* const add_definition)(void *internal, const char *identifier, size_t identifier_length, unsigned long value))
{
int i;
(void)user_data;
for (i = 1; i < total_arguments; ++i)
{
if (arguments[i][0] == '/' && tolower(arguments[i][1]) == 'e')
{
const char* const identifier = arguments[i + 1];
const size_t identifier_length = strcspn(identifier, "=");
unsigned long value;
char *str_end;
if (identifier[identifier_length] != '=')
{
value = 0; /* Exactly what asm68k does... */
}
else
{
/* SN 68k (asm68k) supports XX, 0xXX, and $XX. */
if (identifier[identifier_length + 1] == '$')
value = strtoul(&identifier[identifier_length + 1 + 1], &str_end, 0x10);
else
value = strtoul(&identifier[identifier_length + 1], &str_end, 0);
if (str_end < strchr(identifier, '\0'))
fprintf(stderr, "Error: Value of argument '/e %s' is invalid.\n", arguments[i + 1]);
}
add_definition(internal, identifier, identifier_length, value);
++i; /* Skip argument. */
}
}
}
int main(int argc, char **argv)
{
int exit_code = EXIT_SUCCESS;
int i;
char *combined_arguments;
cc_bool case_sensitive;
cc_bool equ_set_descope_local_labels;
cc_bool output_local_labels_to_sym_file;
cc_bool warnings_enabled;
const char *source_file_path;
const char *object_file_path;
const char *symbol_file_path;
const char *listing_file_path;
FILE *source_file;
FILE *object_file;
FILE *symbol_file;
FILE *listing_file;
enum
{
OUTPUT_EXECUTABLE,
OUTPUT_FLAT_BINARY,
OUTPUT_S_RECORD
} output_mode;
total_arguments = argc;
arguments = argv;
combined_arguments = NULL;
case_sensitive = cc_false;
equ_set_descope_local_labels = cc_false;
output_local_labels_to_sym_file = cc_false;
warnings_enabled = cc_true;
source_file_path = object_file_path = symbol_file_path = listing_file_path = NULL;
source_file = object_file = symbol_file = listing_file = NULL;
output_mode = OUTPUT_EXECUTABLE;
for (i = 1; i < argc; ++i)
{
if (argv[i][0] == '/')
{
/* This is a switch. */
switch (tolower(argv[i][1]))
{
case 'e':
/* We'll deal with this later, in DefinitionCallback. */
++i;
break;
case 'j':
/* Skip argument. */
++i;
/* Fallthrough */
case 'c':
case 'd':
case 'g':
case 'k':
case 'l':
case 'm':
case 'w':
case 'z': /* And 'zd'. */
fprintf(stderr, "Warning: Unsupported option: '%s'.\n", argv[i]);
break;
case 'p': /* And 'ps'. */
if (tolower(argv[i][2]) == 's')
output_mode = OUTPUT_S_RECORD;
else
output_mode = OUTPUT_FLAT_BINARY;
break;
case 'o':
/* Parse the argument after this one. */
++i;
if (i < argc)
{
const char *option;
for (option = strtok(argv[i], ","); option != NULL; option = strtok(NULL, ","))
{
if (strcmpci(option, "ae+") == 0)
{
fprintf(stderr, "Error: Unsupported option: '%s'.\n", argv[i]);
exit_code = EXIT_FAILURE;
}
else if (strcmpci(option, "ae-") == 0)
{
/* TODO - AE should be enabled by default. */
/* Already disabled by default but okay. */
}
else if (strcmpci(option, "an+") == 0)
{
/* Quietly accept this even though it isn't configurable. */
}
else if (strcmpci(option, "an-") == 0)
{
fprintf(stderr, "Warning: Option '%s' has no effect, as alternate numeric specifiers are always enabled.\n", argv[i]);
}
else if (strcmpci(option, "c+") == 0)
{
case_sensitive = cc_true;
}
else if (strcmpci(option, "c-") == 0)
{
case_sensitive = cc_false;
}
else if (strcmpci(option, "d+") == 0)
{
equ_set_descope_local_labels = cc_true;
}
else if (strcmpci(option, "d-") == 0)
{
equ_set_descope_local_labels = cc_false;
}
else if (strcmpci(option, "e+") == 0)
{
fprintf(stderr, "Error: Unsupported option: '%s'.\n", argv[i]);
exit_code = EXIT_FAILURE;
}
else if (strcmpci(option, "e-") == 0)
{
fprintf(stderr, "Error: Unsupported option: '%s'.\n", argv[i]);
exit_code = EXIT_FAILURE;
}
else if (tolower(argv[i][0]) == 'l')
{
switch (tolower(argv[i][1]))
{
case '+':
case '-':
fprintf(stderr, "Warning: Option '%s' has no effect, as '@' and '.' always specify local labels.\n", argv[i]);
break;
default:
fprintf(stderr, "Error: Option '%s' is unsupported: local labels can only be specified with '@' or '.'.\n", argv[i]);
exit_code = EXIT_FAILURE;
break;
}
}
else if (strcmpci(option, "s+") == 0)
{
fprintf(stderr, "Error: Unsupported option: '%s'.\n", argv[i]);
exit_code = EXIT_FAILURE;
}
else if (strcmpci(option, "s-") == 0)
{
fprintf(stderr, "Error: Unsupported option: '%s'.\n", argv[i]);
exit_code = EXIT_FAILURE;
}
else if (strcmpci(option, "v+") == 0)
{
output_local_labels_to_sym_file = cc_true;
}
else if (strcmpci(option, "v-") == 0)
{
output_local_labels_to_sym_file = cc_false;
}
else if (strcmpci(option, "w+") == 0)
{
warnings_enabled = cc_true;
}
else if (strcmpci(option, "w-") == 0)
{
warnings_enabled = cc_false;
}
else if (strcmpci(option, "ws+") == 0)
{
/* This is always enabled. */
}
else if (strcmpci(option, "ws-") == 0)
{
fprintf(stderr, "Error: Unsupported option: '%s'.\n", argv[i]);
exit_code = EXIT_FAILURE;
}
else if (strcmpci(option, "x+") == 0)
{
fprintf(stderr, "Error: Unsupported option: '%s'.\n", argv[i]);
exit_code = EXIT_FAILURE;
}
else if (strcmpci(option, "x-") == 0)
{
fprintf(stderr, "Error: Unsupported option: '%s'.\n", argv[i]);
exit_code = EXIT_FAILURE;
}
else if (strcmpci(option, "op+") == 0)
{
fprintf(stderr, "Error: Unsupported option: '%s'.\n", argv[i]);
exit_code = EXIT_FAILURE;
}
else if (strcmpci(option, "op-") == 0)
{
fprintf(stderr, "Error: Unsupported option: '%s'.\n", argv[i]);
exit_code = EXIT_FAILURE;
}
else if (strcmpci(option, "os+") == 0)
{
fprintf(stderr, "Error: Unsupported option: '%s'.\n", argv[i]);
exit_code = EXIT_FAILURE;
}
else if (strcmpci(option, "os-") == 0)
{
fprintf(stderr, "Error: Unsupported option: '%s'.\n", argv[i]);
exit_code = EXIT_FAILURE;
}
else if (strcmpci(option, "ow+") == 0)
{
fprintf(stderr, "Error: Unsupported option: '%s'.\n", argv[i]);
exit_code = EXIT_FAILURE;
}
else if (strcmpci(option, "ow-") == 0)
{
fprintf(stderr, "Error: Unsupported option: '%s'.\n", argv[i]);
exit_code = EXIT_FAILURE;
}
else if (strcmpci(option, "oz+") == 0)
{
fprintf(stderr, "Error: Unsupported option: '%s'.\n", argv[i]);
exit_code = EXIT_FAILURE;
}
else if (strcmpci(option, "oz-") == 0)
{
fprintf(stderr, "Error: Unsupported option: '%s'.\n", argv[i]);
exit_code = EXIT_FAILURE;
}
else if (strcmpci(option, "oaq+") == 0)
{
fprintf(stderr, "Error: Unsupported option: '%s'.\n", argv[i]);
exit_code = EXIT_FAILURE;
}
else if (strcmpci(option, "oaq-") == 0)
{
fprintf(stderr, "Error: Unsupported option: '%s'.\n", argv[i]);
exit_code = EXIT_FAILURE;
}
else if (strcmpci(option, "osq+") == 0)
{
fprintf(stderr, "Error: Unsupported option: '%s'.\n", argv[i]);
exit_code = EXIT_FAILURE;
}
else if (strcmpci(option, "osq-") == 0)
{
fprintf(stderr, "Error: Unsupported option: '%s'.\n", argv[i]);
exit_code = EXIT_FAILURE;
}
else if (strcmpci(option, "omq+") == 0)
{
fprintf(stderr, "Error: Unsupported option: '%s'.\n", argv[i]);
exit_code = EXIT_FAILURE;
}
else if (strcmpci(option, "omq-") == 0)
{
fprintf(stderr, "Error: Unsupported option: '%s'.\n", argv[i]);
exit_code = EXIT_FAILURE;
}
else
{
fprintf(stderr, "Error: Unrecognised option: '%s'.\n", argv[i]);
exit_code = EXIT_FAILURE;
}
}
}
break;
default:
fprintf(stderr, "Error: Unrecognised switch: '%s'.\n", argv[i]);
exit_code = EXIT_FAILURE;
break;
}
}
else
{
size_t combined_arguments_size;
int j;
combined_arguments_size = 1; /* The '1' is for the null character. */
/* We've encountered the file list. */
for (j = i; j < argc; ++j)
combined_arguments_size += strlen(argv[j]);
combined_arguments = (char*)malloc(combined_arguments_size);
if (combined_arguments == NULL)
{
fprintf(stderr, "Error: Could not allocate memory for combined argument string.\n");
exit_code = EXIT_FAILURE;
}
else
{
char *comma;
combined_arguments[0] = '\0';
for (j = i; j < argc; ++j)
strcat(combined_arguments, argv[j]);
source_file_path = combined_arguments;
comma = strchr(source_file_path, ',');
if (comma != NULL)
{
comma[0] = '\0';
object_file_path = comma + 1;
comma = strchr(object_file_path, ',');
if (comma != NULL)
{
comma[0] = '\0';
symbol_file_path = comma + 1;
comma = strchr(symbol_file_path, ',');
if (comma != NULL)
{
comma[0] = '\0';
listing_file_path = comma + 1;
comma = strchr(listing_file_path, ',');
if (comma != NULL)
comma[0] = '\0';
}
}
}
}
break;
}
}
/* Open files. */
if (source_file_path == NULL || source_file_path[0] == '\0')
{
fprintf(stderr, "Error: Source file path must be specified.\n");
exit_code = EXIT_FAILURE;
}
else if ((source_file = fopen(source_file_path, "r")) == NULL)
{
fprintf(stderr, "Error: Source file could not be opened.\n");
exit_code = EXIT_FAILURE;
}
if (object_file_path == NULL || object_file_path[0] == '\0')
{
fprintf(stderr, "Error: Object file path must be specified.\n");
exit_code = EXIT_FAILURE;
}
else if ((object_file = fopen(object_file_path, "wb")) == NULL)
{
fprintf(stderr, "Error: Object file could not be opened.\n");
exit_code = EXIT_FAILURE;
}
if (symbol_file_path != NULL && symbol_file_path[0] != '\0')
symbol_file = fopen(symbol_file_path, "wb");
if (listing_file_path != NULL && listing_file_path[0] != '\0')
listing_file = fopen(listing_file_path, "w");
/* Make sure that we're using a supported output format. */
switch (output_mode)
{
case OUTPUT_EXECUTABLE:
fprintf(stderr, "Error: Executable output format is not supported: use '/p' for outputting a flat binary instead.\n");
exit_code = EXIT_FAILURE;
break;
case OUTPUT_FLAT_BINARY:
break;
case OUTPUT_S_RECORD:
fprintf(stderr, "Error: S-record output format is not supported: use '/p' for outputting a flat binary instead.\n");
exit_code = EXIT_FAILURE;
break;
}
/* If we've gotten this far without any errors, then finally run the assembler. */
if (exit_code != EXIT_FAILURE)
{
if (!ClownAssembler_AssembleFile(source_file, object_file, stdout, listing_file, symbol_file, source_file_path, cc_false, !case_sensitive, equ_set_descope_local_labels, output_local_labels_to_sym_file, warnings_enabled, DefinitionCallback, NULL))
{
fprintf(stderr, "Error: Failed to assemble.\n");
exit_code = EXIT_FAILURE;
}
}
free(combined_arguments);
return exit_code;
}