-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
frontend_custom.c
267 lines (219 loc) · 5.91 KB
/
frontend_custom.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
/*
* 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 <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "clowncommon/clowncommon.h"
#include "semantic.h"
#define ERROR(message) do { fputs("Error: " message "\n", stderr); exit_code = EXIT_FAILURE;} while (0)
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] == '-' && 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
{
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;
cc_bool print_usage;
const char *input_file_path;
const char *output_file_path;
const char *listing_file_path;
const char *symbol_file_path;
cc_bool case_insensitive;
cc_bool debug;
cc_bool equ_set_descope_local_labels;
cc_bool output_local_labels_to_sym_file;
cc_bool warnings_enabled;
int i;
total_arguments = argc;
arguments = argv;
print_usage = cc_false;
input_file_path = NULL;
output_file_path = NULL;
listing_file_path = NULL;
symbol_file_path = NULL;
case_insensitive = cc_false;
debug = cc_false;
equ_set_descope_local_labels = cc_false;
output_local_labels_to_sym_file = cc_false;
warnings_enabled = cc_true;
for (i = 1; i < argc; ++i)
{
if (argv[i][0] == '-')
{
switch (argv[i][1])
{
case 'h':
print_usage = cc_true;
continue;
case 'i':
if (i < argc && argv[i + 1][0] != '-')
{
++i;
input_file_path = argv[i];
}
continue;
case 'o':
if (i < argc && argv[i + 1][0] != '-')
{
++i;
output_file_path = argv[i];
}
continue;
case 'l':
if (i < argc && argv[i + 1][0] != '-')
{
++i;
listing_file_path = argv[i];
}
continue;
case 's':
if (i < argc && argv[i + 1][0] != '-')
{
++i;
symbol_file_path = argv[i];
}
continue;
case 'c':
case_insensitive = cc_true;
continue;
case 'b':
debug = cc_true;
continue;
case 'd':
equ_set_descope_local_labels = cc_true;
continue;
case 'e':
/* We'll deal with this later, in DefinitionCallback. */
if (i < argc && argv[i + 1][0] != '-')
++i;
continue;
case 'v':
output_local_labels_to_sym_file = cc_true;
continue;
case 'w':
warnings_enabled = cc_false;
continue;
}
}
fprintf(stderr, "Error: Unrecognised option '%s'.\n", argv[i]);
exit_code = EXIT_FAILURE;
}
if (argc < 2 || print_usage)
{
fputs(
"clownassembler - An assembler for the Motorola 68000.\n"
"\n"
"Options:\n"
" -i [path] - Input file. If not specified, STDIN is used instead.\n"
" -o [path] - Output file.\n"
" -l [path] - Listing file. Optional.\n"
" -s [path] - asm68k-style symbol file. Optional.\n"
" -c - Enable case-insensitive mode.\n"
" -b - Enable Bison's debug output.\n"
" -d - Allow EQU/SET to descope local labels.\n"
" -e X=Y - Defines symbol X to value Y.\n"
" -v - Include local labels in symbol file.\n"
" -w - Silence warnings.\n"
, stdout);
}
else
{
if (output_file_path == NULL)
{
ERROR("Output file path must be specified with '-o'.");
}
else
{
FILE *input_file;
if (input_file_path == NULL)
input_file = stdin;
else
input_file = fopen(input_file_path, "r");
if (input_file == NULL)
{
ERROR("Could not open input file.");
}
else
{
FILE *output_file;
output_file = fopen(output_file_path, "wb");
if (output_file == NULL)
{
ERROR("Could not open output file.");
}
else
{
FILE *listing_file;
FILE *symbol_file;
if (listing_file_path == NULL)
{
listing_file = NULL;
}
else
{
listing_file = fopen(listing_file_path, "w");
if (listing_file == NULL)
ERROR("Could not open listing file.");
}
if (symbol_file_path == NULL)
{
symbol_file = NULL;
}
else
{
symbol_file = fopen(symbol_file_path, "wb");
if (symbol_file == NULL)
ERROR("Could not open symbol file.");
}
if (!ClownAssembler_AssembleFile(input_file, output_file, stderr, listing_file, symbol_file, input_file_path != NULL ? input_file_path : "STDIN", debug, case_insensitive, equ_set_descope_local_labels, output_local_labels_to_sym_file, warnings_enabled, DefinitionCallback, NULL))
ERROR("Could not assemble.");
if (listing_file != NULL)
fclose(listing_file);
fclose(output_file);
}
fclose(input_file);
}
}
}
return exit_code;
}