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
|
/*
casm - Simple, portable assembler
Copyright (C) 2003-2015 Ian Cowburn (ianc@noddybox.co.uk)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-------------------------------------------------------------------------
Listing
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include "global.h"
#include "state.h"
#include "label.h"
#include "macro.h"
#include "expr.h"
#include "varchar.h"
#include "listing.h"
/* ---------------------------------------- PRIVATE TYPES
*/
enum option_t
{
OPT_LIST,
OPT_LISTFILE,
OPT_LISTPC,
OPT_LISTHEX,
OPT_LISTMACROS,
OPT_LISTLABELS,
OPT_LISTRMBLANK
};
static const ValueTable option_set[] =
{
{"list", OPT_LIST},
{"list-file", OPT_LISTFILE},
{"list-pc", OPT_LISTPC},
{"list-hex", OPT_LISTHEX},
{"list-macros", OPT_LISTMACROS},
{"list-labels", OPT_LISTLABELS},
{"list-rm-blank", OPT_LISTRMBLANK},
{NULL}
};
typedef enum
{
LabelsOff = 0,
LabelsDump = 1,
LabelsDumpPrivate = 2
} LabelMode;
typedef enum
{
MacrosOff = 0,
MacrosInvoke = 1,
MacrosDump = 2
} MacroMode;
typedef struct
{
int enabled;
int dump_PC;
int dump_bytes;
int rm_blank;
LabelMode labels;
MacroMode macros;
} Options;
/* ---------------------------------------- PRIVATE DATA
*/
static int line_PC;
static int last_line_blank;
static FILE *output;
static Options options =
{
FALSE,
FALSE,
FALSE,
TRUE,
LabelsOff,
MacrosOff,
};
static ValueTable labels_table[] =
{
{"off", LabelsOff},
{"no", LabelsOff},
{"on", LabelsDump},
{"yes", LabelsDump},
{"all", LabelsDump | LabelsDumpPrivate},
{NULL}
};
static ValueTable macros_table[] =
{
{"off", MacrosOff},
{"no", MacrosOff},
{"yes", MacrosInvoke},
{"exec", MacrosInvoke},
{"dump", MacrosDump},
{"all", MacrosInvoke | MacrosDump},
{NULL}
};
/* ---------------------------------------- PRIVATE FUNCTIONS
*/
static void BuildArgs(Varchar *str, int argc, char *argv[], int quoted[])
{
int f;
for(f = 0; f < argc; f++)
{
if (f > 0)
{
VarcharAdd(str, ", ");
}
if (quoted[f] && quoted[f] == '(')
{
VarcharPrintf(str, "(%s)", argv[f]);
}
else if (quoted[f])
{
VarcharPrintf(str, "%c%s%c", quoted[f], argv[f], quoted[f]);
}
else
{
VarcharAdd(str, argv[f]);
}
}
}
static void Output(const char *fmt, ...)
{
if (IsFinalPass() && options.enabled)
{
va_list va;
va_start(va, fmt);
if (output)
{
vfprintf(output, fmt, va);
}
else
{
vfprintf(stdout, fmt, va);
}
va_end(va);
}
}
static FILE *GetOutput(void)
{
return output ? output : stdout;
}
static int IsBlankLine(const char *p)
{
while(*p)
{
if (!isspace((unsigned char)*p++))
{
return FALSE;
}
}
return TRUE;
}
/* ---------------------------------------- INTERFACES
*/
const ValueTable *ListOptions(void)
{
return option_set;
}
CommandStatus ListSetOption(int opt, int argc, char *argv[],
int quoted[], char *err, size_t errsize)
{
const ValueTable *val;
CommandStatus stat = CMD_OK;
if (!IsFinalPass())
{
return CMD_OK;
}
CMD_ARGC_CHECK(1);
switch(opt)
{
case OPT_LIST:
options.enabled = ParseTrueFalse(argv[0], FALSE);
break;
case OPT_LISTFILE:
if (output)
{
snprintf(err, errsize, "output file already set");
stat = CMD_FAILED;
}
else
{
output = fopen(argv[0], "w");
if (!output)
{
snprintf(err, errsize, "couldn't open \"%s\"", argv[0]);
stat = CMD_FAILED;
}
}
break;
case OPT_LISTPC:
options.dump_PC = ParseTrueFalse(argv[0], FALSE);
break;
case OPT_LISTHEX:
options.dump_bytes = ParseTrueFalse(argv[0], FALSE);
break;
case OPT_LISTMACROS:
CMD_TABLE(argv[0], macros_table, val);
options.macros = val->value;
break;
case OPT_LISTLABELS:
CMD_TABLE(argv[0], labels_table, val);
options.labels = val->value;
break;
case OPT_LISTRMBLANK:
options.rm_blank = ParseTrueFalse(argv[0], FALSE);
break;
default:
break;
}
return stat;
}
void ListStartLine(void)
{
line_PC = PC();
}
void ListLine(const char *line)
{
if (IsFinalPass() && options.enabled)
{
if (options.rm_blank && last_line_blank && IsBlankLine(line))
{
return;
}
last_line_blank = IsBlankLine(line);
Output("%s\n", line);
/* Generate PC and hex dump and add to comment
*/
if ((options.dump_PC || options.dump_bytes) && (PC() != line_PC))
{
Varchar *hex;
int f;
hex = VarcharCreate(NULL);
VarcharAdd(hex, ";");
if (options.dump_PC)
{
VarcharPrintf(hex, " $%4.4X:", (unsigned)line_PC);
}
if (options.dump_bytes && (PC() - line_PC < 256))
{
for(f = line_PC; f < PC(); f++)
{
VarcharPrintf(hex, " $%2.2X", (unsigned)ReadByte(f));
}
}
Output("%s\n", VarcharContents(hex));
VarcharFree(hex);
}
}
}
void ListMacroInvokeStart(int argc, char *argv[], int quoted[])
{
if (IsFinalPass() && options.enabled && options.macros & MacrosInvoke)
{
Varchar *s;
s = VarcharCreate(NULL);
VarcharPrintf(s, "; START MACRO %s ", argv[0]);
BuildArgs(s, argc - 1, argv + 1, quoted + 1);
Output("%s\n", VarcharContents(s));
VarcharFree(s);
}
}
void ListMacroInvokeEnd(const char *name)
{
if (IsFinalPass() && options.enabled && options.macros & MacrosInvoke)
{
Varchar *s;
s = VarcharCreate(NULL);
VarcharAdd(s, "; END MACRO ");
VarcharAdd(s, name);
Output("%s\n", VarcharContents(s));
VarcharFree(s);
}
}
void ListError(const char *fmt, ...)
{
char buff[4096];
va_list va;
va_start(va, fmt);
vsnprintf(buff, sizeof buff, fmt, va);
va_end(va);
fprintf(stderr, "%s\n", buff);
if (IsFinalPass() && options.enabled && output)
{
Output("%s\n", buff);
}
}
void ListFinish(void)
{
if (IsFinalPass() && options.enabled)
{
if (options.labels)
{
Output("\n;\n; LABELS:\n;\n");
LabelDump(GetOutput(), options.labels & LabelsDumpPrivate);
}
if (options.macros & MacrosDump)
{
Output("\n;\n; MACROS:\n;\n");
MacroDump(GetOutput());
}
}
if (output)
{
if (output != stdout)
{
fclose(output);
}
output = NULL;
}
options.enabled = FALSE;
}
/*
vim: ai sw=4 ts=8 expandtab
*/
|