File: main.c

package info (click to toggle)
a56 1.3%2Bdfsg-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 352 kB
  • sloc: ansic: 3,471; yacc: 1,888; makefile: 120; awk: 4
file content (466 lines) | stat: -rwxr-xr-x 8,269 bytes parent folder | download | duplicates (3)
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
466
/*******************************************************
 *
 *  a56 - a DSP56001 assembler
 *
 *  Written by Quinn C. Jensen
 *  July 1990
 *
 *******************************************************\

/*
 * Copyright (C) 1990-1994 Quinn C. Jensen
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  The author makes no representations
 * about the suitability of this software for any purpose.  It is
 * provided "as is" without express or implied warranty.
 *
 */
static char *Copyright = "Copyright (C) 1990-1994 Quinn C. Jensen";

/*
 *  main.c - The "main" code for the assembler.
 *
 */

#include "a56.h"

#define MAX 1024

int pass;
int error, warning;
extern unsigned int pc;
extern int seg;
BOOL binary_listing = FALSE;
BOOL list_includes = FALSE;
FILE *obj = NULL;
extern BOOL list_on;
BOOL list_on_next = TRUE;

main(argc,argv)
int argc;
char *argv[];
{
	int i;
	extern char *optarg;
	extern int optind;
	int c;
	char *output_file = "a56.out";
	char *input_file;
	char *usage = "usage: a56  [-b]  [-l]  [-d]  [-o output-file]  input-file\n";

	while((c = getopt(argc, argv, "bldo:")) != EOF) switch (c) {
		case 'b':
			binary_listing++;
			break;
		case 'l':
			list_includes++;
			break;
		case 'd':
			ldebug++;
			break;
		case 'o':
			output_file = optarg;
			break;
		case '?':
		default:
			fatal(usage);
	}
	input_file = argv[optind++];
	if(input_file == NULL) fatal(usage);
	obj = open_write(output_file);

	pc = 0;
	seg = 0;
	pass = 1;
	reset_psects();
	include(input_file);

	pc = 0;
	seg = 0;
	pass = 2;
	reset_psects();
	include(input_file);

	psect_summary();
	dump_symtab();
	fclose(obj);
	printf("errors=%d\n", error);
	printf("warnings=%d\n", warning);
	return error ? 1 : 0;
}

struct inc inc[MAX_NEST];
int inc_p = 0;
FILE *yyin;

include(file)
char *file;
{
	FILE *fp = open_read(file);

	inc_p++;
	if(inc_p >= MAX_NEST)
		fatal("%s: include nesting too deep\n", file);

	inc[inc_p].file = file;
	inc[inc_p].fp = fp;
	inc[inc_p].line = 1;

	list_on_next = TRUE;
	if(inc_p > 1 && NOT list_includes)
		list_on_next = FALSE;

	yyin = inc[inc_p].fp;
	if(inc_p == 1)
#ifdef FLEX
	{
		static int started = 0;
		if(started)
			yyrestart(yyin);
		else
			started = 1;
		yyparse();
	}
#else
		yyparse();
#endif
}

yywrap()
{
	fclose(inc[inc_p].fp);
	inc_p--;
	list_on = list_on_next = TRUE;
	if(inc_p > 1)
		list_on = list_on_next = FALSE;
	if(inc_p) {
		yyin = inc[inc_p].fp;
		return 0;
	} else {
		return 1;
	}
}

struct n
sym_ref(sym)		/* return symbol value or UNDEF if not defined yet */
char *sym;
{
	struct sym *sp, *find_sym();
	struct n result;

	result.type = UNDEF;

	sp = find_sym(sym);
	if(NOT sp) {
		if(pass == 2) {
			yyerror("%s: undefined symbol", sym);
		}		   
		return result;
	}

	return sp->n;
}

#define HASHSIZE 128

#define HASH(sym) (((sym)[0] ^ sym[1]) % HASHSIZE)

struct sym *symtab[HASHSIZE];

sym_def(sym, type, seg, i, f)
char *sym;
int type;
int seg;
int i;
double f;
{
	struct sym *sp, **stop, *find_sym();

	if(pass == 1) {
		if(find_sym(sym)) {
			pass = 2;				/* what a kludge */
			yyerror("%s: multiply defined symbol", sym);
			pass = 1;
			return 1;
		}
		stop = &symtab[HASH(sym)];
		sp = NEW(struct sym);
		sp->next = *stop;
		*stop = sp;
		sp->name = strsave(sym);
		sp->n.type = type;
		sp->n.seg = seg;
		if(type == INT)
			sp->n.val.i = i & 0xFFFFFF;
		else
			sp->n.val.f = f;
	} else {
		sp = find_sym(sym);
		if(NOT sp)
			fatal("internal error 304\n");
		if(sp->n.type != type ||
			type == INT && sp->n.val.i != (i & 0xFFFFFF) ||
			type == FLT && sp->n.val.f != f)
			yyerror("%s: assembler phase error", sym);
	}		
}

struct sym *find_sym(sym)
char *sym;
{
	struct sym *sp, **stop;

	stop = &symtab[HASH(sym)];
	for(sp = *stop; sp; sp = sp->next)
		if(strcmp(sp->name, sym) == 0)
			return sp;
		
	return NULL;
}

extern char segs[];
dump_symtab()
{
	struct sym *sp, **stop;
	int i;

	printf("\n\
Symbol Table\n\
-------------------------------------\n");
/*
SSSSSSSSSSSSSSSS S XXXXXX
SSSSSSSSSSSSSSSS S DDDDDDDDD.DDDDDDDDDD
*/

	for(i = 0, stop = symtab; i < HASHSIZE; i++, stop++) {
		for(sp = *stop; sp; sp = sp->next) {
			if(sp->n.type == INT) {
				printf("%16s %c %06X\n", sp->name, segs[sp->n.seg], sp->n.val.i);
				fprintf(obj, "I %06X %s\n", sp->n.val.i, sp->name);
			} else {
				printf("%16s %c %.10f\n", sp->name, segs[sp->n.seg], sp->n.val.f);
				fprintf(obj, "F %.10f %s\n", sp->n.val.f, sp->name);
			}
		}
	}   
}

char *printcode(word)
int word;
{
	static char list[MAX], *lp;
	int i;

	word &= 0xFFFFFF;

	if(binary_listing) {
		sprintf(list, "%06X<", word);
		for(i = 0, lp = &list[7]; i < 24; i++, lp++) {
			*lp = word & 1 << 23 - i ? '1' : '0';
			if(i && i % 4 == 3)
				*++lp = i % 8 == 7 ? ' ' : ',';
		}
		lp[-1] = '>';
		lp[0] = '\0';
	} else {
		sprintf(list, "%06X", word);
	}
	return list;
}

char *spacespace[2] = {
/*P:XXXX_XXXXXX_*/
 "              ",
/*P:XXXX_XXXXXX(XXXX_XXXX_XXXX_XXXX_XXXX_XXXX)_*/
 "                                             "};
char *spaces(n)
int n;
{
	return spacespace[binary_listing ? 1 : 0] + n;
}

extern char segs[];

gencode(seg, pc, word)
int seg, pc, word;
{
	fprintf(obj, "%c %04X %06X\n", segs[seg], pc, word & 0xFFFFFF);
}

char fixbuf[1024];

char *fixstring(s)
char *s;
{
	char *bp = fixbuf;
	int ival;

	while(*s) {
		switch (*s) {
			case '\'':
			case '\"':
				s++;
				break;
			case '\\':
				switch (*++s) {
					case 'b': *bp++ = '\b'; break;
					case 'r': *bp++ = '\r'; break;
					case 'f': *bp++ = '\f'; break;
					case 'n': *bp++ = '\n'; break;
					case 't': *bp++ = '\t'; break;
					case '\\': *bp++ = '\\'; break;
					case '0':
						ival = 0;
						while(*s >= '0' && *s <= '9') {
							ival <<= 3;
							ival += *s++ - '0';
						}
						*bp++ = ival;
						break;
				}
				break;
			default:
				*bp++ = *s++;
				break;
		}
	}
	*bp = '\0';
	return fixbuf;
}

#define ONE 0x4000000

makefrac(s)
char *s;
{
	int frac = 0, div = 1;
	int scale = 1;

	while(*s) {
		switch(*s) {
			case '-':
				scale = -1;
				break;
			case '.':
				div = 10;
				break;
			default:
				frac += (*s - '0') * scale * ONE / div;
				div *= 10;
				break;
		}
		s++;
	}

	return frac + scale * 4 >> 3 & 0xFFFFFF;
}

/***************** psect stuff ************************/

struct psect *ptop = NULL, *cur_psect = NULL;

reset_psects()
{
	struct psect *pp;

	for(pp = ptop; pp; pp = pp->next) {
		pp->pc = pp->bottom;
	}

	set_psect(NULL);
}

psect_summary()
{
	printf("\nSummary of psect usage\n\n");

	printf("\
                 section seg base last top      used       avail    total\n\
-------------------------------------------------------------------------\n");
/*
SSSSSSSSSSSSSSSSSSSSSSSS  X  FFFF FFFF FFFF 99999 100%  99999 100%  99999
*/

	summarize(ptop);	/* do it recursively to place back in order */
	printf("\n");
}

summarize(pp)
struct psect *pp;
{
	int used, avail, of;

	if(pp == NULL)
		return 1;

	used = pp->pc - pp->bottom;
	avail = pp->top - pp->pc;
	of = pp->top - pp->bottom;

	summarize(pp->next);

	printf("%24.24s  %c  %04X %04X %04X %5d %3d%%  %5d %3d%%  %5d\n",
		pp->name, segs[pp->seg], pp->bottom, pp->pc, pp->top,
		used, of ? used * 100 / of : 0, avail, of ? avail * 100 / of : 0,
		of);
}

struct psect *find_psect(name)
char *name;
{
	struct psect *pp;

	for(pp = ptop; pp; pp = pp->next)
		if(strcmp(pp->name, name) == 0)
			return pp;

	return NULL;
}

set_psect(pp)
struct psect *pp;
{
	cur_psect = pp;
}

check_psect(seg, pc)
int seg;
unsigned int pc;
{
	if(cur_psect) {
		if(seg == cur_psect->seg && pc >= cur_psect->bottom && 
			pc <= cur_psect->top) {
			cur_psect->pc = pc;
			return TRUE;
		} else {
			return FALSE;
		}
	} else {
		return TRUE;
	}
}

struct psect *new_psect(name, seg, bottom, top)
char *name;
int seg;
unsigned int bottom, top;
{
	struct psect *pp = find_psect(name);

	if(NOT pp) {
		pp = (struct psect *)alloc(sizeof *pp);
		pp->next = ptop;
		ptop = pp;
		pp->name = strsave(name);
		pp->seg = seg;
		pp->pc = bottom;
	}
	pp->bottom = bottom;
	pp->top = top;

	return pp;
}