rspasm.c
15.2 KB
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
/**************************************************************************
* *
* Copyright (C) 1994, Silicon Graphics, Inc. *
* *
* These coded instructions, statements, and computer programs contain *
* unpublished proprietary information of Silicon Graphics, Inc., and *
* are protected by Federal copyright law. They may not be disclosed *
* to third parties or copied or duplicated in any form, in whole or *
* in part, without the prior written consent of Silicon Graphics, Inc. *
* *
*************************************************************************/
/*
* File: rspasm.c
* Creator: hsa@sgi.com
* Create Date: Thu Feb 24 15:18:11 PST 1994
*
* This file is the top-level of the RSP microcode assembler.
* The parsing and basic structure of this program was stolen from
* Henry Moreton and the VICE group.
*
* A simple two-pass assembler:
* - parse source file, converting assembly language to machine language
* - go back and fix up jump/branch addresses
*
*/
#include <stdio.h>
#include <time.h>
#include <string.h>
#include "../rspsim/rsp.h"
#include "../rspsim/opcode.h"
#include "../rspsim/disasm.h"
#include "globals.h"
#include "defines.h"
#include "types.h"
#include "macros.h"
Instruction InstructionSpace[MAX_INSTRUCTIONS*2];
unsigned char DataSpace[MAX_DATA_BYTES];
Symbol *DataSymbols[MAX_DATA_BYTES]; /* for symbols */
Symbol *LinearSymbols[MAX_DATA_BYTES];
int symbol_count = 0;
unsigned int DataBase = 0;
unsigned int AssembleBase = 0;
int AssembleBaseSet = FALSE; /* AssembleBase set flag */
unsigned int PC = 0;
unsigned int DataCount = 0;
unsigned int OpcodeTable[MAX_OPCODES];
char *(OpcodeStrings[64]);
char *(SpecialStrings[64]);
char *(RegimmStrings[32]);
char *(Cop2Strings[32]);
int ExternSymCount = 0;
char ExternSymFiles[32][256];
extern unsigned int ExternSymbolSearch(char *sym);
/* either RCP or MME instruction set... */
int ins_set = RCP_INS_SET;
FILE *symbol_fp = NULL;
char input_file[256], output_file[256], listing_file[255], data_file[255];
char symbol_file[256];
char curr_file[256];
char line_buffer[4096];
int mylineno = 1;
boolean is_error = FALSE;
int dry_run = FALSE;
#define MAXQ 8
int sym_lineq[MAXQ];
char sym_fileq[MAXQ][256];
int sym_qhead, sym_qtail, sym_qlen;
#define PrintUsage() fprintf(stderr,"usage : rspasm [options] srcfile\n");
static void
do_help(void)
{
fprintf(stderr,"\nCommand Options:\n\n");
fprintf(stderr,
"\t-b <baseaddr> : provide a base address for assembly.\n");
fprintf(stderr,
"\t-c : don't run source through /usr/lib/cpp.\n");
fprintf(stderr,
"\t-D<name> : define symbol <name> to /usr/lib/cpp.\n");
fprintf(stderr,
"\t-d : turn on yacc debugging.\n");
fprintf(stderr,
"\t-E : only run the pre-processor(s); don't assemble.\n");
fprintf(stderr,
"\t-h : print this help message.\n");
fprintf(stderr,
"\t-I<dir> : add directory to find include files.\n");
fprintf(stderr,
"\t-l : don't generate a listing file.\n");
fprintf(stderr,
"\t-m : don't run source through m4 macro pre-processor.\n");
fprintf(stderr,
"\t-mme : use the MME instruction set (default is Nintendo).\n");
fprintf(stderr,
"\t-o <file> : name of output file.\n");
fprintf(stderr,
"\t-S <file> : object file to search for external symbols.\n");
fprintf(stderr,"\n");
}
void
sym_setthisline (void)
{
if (sym_qlen == MAXQ) {
fprintf (stderr, "sym_setthisline(): queue length exceeded.\n");
} else {
sym_lineq[sym_qhead] = mylineno;
strcpy (sym_fileq[sym_qhead], curr_file);
sym_qlen++;
sym_qhead = (sym_qhead + 1) % MAXQ;
}
}
void
sym_output(u32 addr, int two)
{
if (symbol_fp != (FILE *)NULL) {
if (sym_qlen == 0) {
fprintf (stderr, "sym_output(): queue is empty.\n");
} else {
fprintf(symbol_fp, "line 0x%08x %s %d\n",
(addr << 2) + AssembleBase,
sym_fileq[sym_qtail],
sym_lineq[sym_qtail]);
if (!two) {
/* Don't update queue if first of double instructions */
sym_qlen--;
sym_qtail = (sym_qtail + 1) % MAXQ;
}
}
}
}
void
sym_namescalar (char *name, int reg)
{
if (symbol_fp != (FILE *)NULL) {
fprintf(symbol_fp, "sname %s %d\n", name, reg);
}
}
void
sym_namevector (char *name, int reg)
{
if (symbol_fp != (FILE *)NULL) {
fprintf(symbol_fp, "vname %s %d\n", name, reg);
}
}
void
sym_namecontrol (char *name, int reg)
{
if (symbol_fp != (FILE *)NULL) {
/* fprintf(symbol_fp, "cname %s %d\n", name, reg);*/
}
}
void
sym_unname (char *name)
{
if (symbol_fp != (FILE *)NULL) {
fprintf(symbol_fp, "unname %s\n", name);
}
}
void
sym_entry (char *name)
{
if (symbol_fp != (FILE *)NULL) {
fprintf(symbol_fp, "entry %s\n", name);
}
}
void
sym_end (char *name)
{
if (symbol_fp != (FILE *)NULL) {
fprintf(symbol_fp, "end %s\n", name);
}
}
int
main(int argc, char *argv[])
{
boolean docpp = TRUE, dom4 = TRUE, eflag = FALSE;
int i, offset, debug = FALSE, dolisting = TRUE, tmp;
FILE *output, *listing, *data;
char buf[255], cppcmd[1024], cppdefs[512];
time_t this_time;
Symbol *symPtr;
int value;
strcpy(output_file, "a.out"); /* default executable */
strcpy(cppdefs, "");
while ((argc > 1) && (argv[1][0] == '-')) {
switch(argv[1][1]) {
case 'b': /* base */
strcpy(buf, argv[2]);
sscanf(buf,"%i",&AssembleBase);
AssembleBaseSet = TRUE;
argv++;
argc--;
break;
case 'c':
docpp = FALSE;
break;
case 'D':
sprintf(cppdefs, "%s %s", cppdefs, argv[1]);
break;
case 'd':
debug = TRUE;
break;
case 'E':
eflag = TRUE;
break;
case 'g': /* outupt symbols for debugging */
break;
case 'h':
PrintUsage();
do_help();
exit(0);
break;
case 'I':
sprintf(cppdefs, "%s %s", cppdefs, argv[1]);
break;
case 'l':
dolisting = FALSE;
break;
case 'm':
if (argv[1][2] == 'm' && argv[1][3] == 'e') {
ins_set = MME_INS_SET;
} else {
dom4 = FALSE;
}
break;
case 'O': /* optimize. Ha! */
break;
case 'o':
strcpy(output_file, argv[2]);
argv++;
argc--;
break;
case 'S':
if (ExternSymCount < 32) {
sprintf(ExternSymFiles[ExternSymCount++],"%s.dbg",argv[2]);
} else {
fprintf(stderr,"ERROR : too many external symbol files.\n");
}
argv++;
argc--;
break;
case 't':
dry_run = TRUE;
break;
default:
fprintf(stderr,"ERROR : unrecognized option %s\n",argv[1]);
PrintUsage();
do_help();
exit(-1);
break;
}
argc--;
argv++;
}
/*
* make sure source file specified.
*/
if (argc < 2) {
fprintf(stderr,"ERROR : no source file specified.\n");
PrintUsage();
do_help();
exit(-1);
}
strcpy(input_file, argv[1]);
strcpy(curr_file, input_file);
/*
* make sure we don't blow away the source file...
*/
if (strcmp(output_file, input_file)==0) {
fprintf(stderr,"ERROR : output file [%s] same as input file [%s].\n",
output_file, input_file);
exit(-1);
}
/*
* open source file.
*/
if ((yyin=fopen(input_file,"r")) == NULL) {
fprintf(stderr,"ERROR : cannot open source file [%s]\n",
input_file);
exit(-1);
}
sprintf(symbol_file,"%s.sym",output_file);
if ((symbol_fp=fopen(symbol_file,"w")) == NULL) {
fprintf(stderr,"ERROR : cannot open symbol file [%s]\n",
symbol_file);
exit(-1);
}
/*
* run through the C preprocessor
*/
if (!docpp && !dom4) {
/* skip cpp step */
} else {
fclose(yyin);
if (docpp) {
#ifdef __sgi__
sprintf(cppcmd,"/usr/bin/cc -E -D_LANGUAGE_ASSEMBLY %s %s ",
cppdefs, input_file);
#else
sprintf(cppcmd,"%s/usr/sbin/cpp -lang-asm -D_LANGUAGE_ASSEMBLY %s %s ",
getenv("ROOT"), cppdefs, input_file);
#endif
} else {
sprintf(cppcmd,"cat %s ", input_file);
}
if (dom4) {
sprintf(cppcmd,"%s | m4 ",cppcmd);
}
if ((yyin = (FILE *) popen(cppcmd, "r")) == NULL) {
fprintf(stderr,"ERROR : could not run cpp on [%s]\n",
input_file);
exit(-1);
}
}
if (eflag) {
if ((output=fopen(output_file,"w")) == NULL) {
fprintf(stderr,"ERROR : cannot open output file [%s]\n",
output_file);
exit(-1);
}
while (fgets(buf, 80, yyin) != NULL) {
fprintf(output,"%s",buf);
}
exit(0);
}
hcreate(SYMBOL_TABLE_SIZE); /* init hash() functions */
BuildOpcodeTable();
/*
* First pass: parse input.
*/
yydebug = debug;
if (yyparse())
exit(-1);
fclose(yyin);
/*
* Second pass: fixup forward references.
*/
/* Check for IMEM over flow before proceeding. */
if (PC > MAX_INSTRUCTIONS) {
fprintf(stderr,"WARNING : number of assembled instructions exceeds IMEM 1K size.\n");
}
for (i=0; i<PC; i++) {
symPtr = InstructionSpace[i].symPtr;
if (symPtr != NULL) {
value = 0xffffffff;
if (!IsSymDefined(symPtr)) {
value = ExternSymbolSearch(symPtr->id);
symPtr->value = value;
}
if (!IsSymDefined(symPtr) && value == 0xffffffff) {
fprintf(stderr,"ERROR : symbol %s undefined.\n",symPtr->id);
} else {
/*
* fix up reference
*/
if (symPtr->flag & SYM_LA) {
/* this is the 'la' macro/op, which generates two
* instructions, an lui and an ori. The symPtr
* is used to fix both...
*/
tmp = ((unsigned)(symPtr->value & 0xffff0000) >> 16);
LoadField(InstructionSpace[i].instruction, 15, 0, tmp);
i++;
tmp = symPtr->value & 0x0000ffff;
LoadField(InstructionSpace[i].instruction, 15, 0, tmp);
} else if (ExtractBits(InstructionSpace[i].instruction,31,27)
== 0x01) {
/*
* J & JAL instruction
*/
if (symPtr->value & 0x03) {
yyerror("targeting nonword boundary");
} else if ((symPtr->value & 0xf0000000) !=
((i+AssembleBase) & 0xf0000000)) {
yyerror("target high order bits disagree with PC");
}
tmp = (symPtr->value & 0x0fffffff) >> 2;
LoadField(InstructionSpace[i].instruction, 25, 0, tmp);
} else {
/*
* branch relative offset instruction
* symPtr->value is a byte address.
*/
tmp = ((i+1)<<2) + AssembleBase;
offset = symPtr->value - tmp;
if (symPtr->value & 0x3) {
yyerror("target to non word boundary");
} else if ((offset>32767) || (offset<-32768)) {
yyerror("target out of range of current PC");
}
LoadField(InstructionSpace[i].instruction, 15, 0, (offset>>2));
}
}
}
}
/*
* Second pass for data space: fixup forward references.
*/
/* Check for DMEM over flow before proceeding. */
if (DataCount > MAX_DATA_BYTES) {
fprintf(stderr,"ERROR : data exceeds DMEM 4KB size.\n");
exit(-1);
}
for (i=0; i<(DataCount /*>>2*/ ); i++) {
symPtr = DataSymbols[i];
if (symPtr != NULL) {
value = 0xffffffff;
if (!IsSymDefined(symPtr)) {
value = ExternSymbolSearch(symPtr->id);
symPtr->value = value;
}
if (!IsSymDefined(symPtr) && value == 0xffffffff) {
fprintf(stderr,"ERROR : data symbol %s undefined.\n",symPtr->id);
is_error = TRUE;
} else {
symPtr->value;
if (IsSymHalf(symPtr)) {
#ifdef __sgi__
value = (symPtr->value&0xffff)<<16;
bcopy(&(value), &(DataSpace[(i /*<<2*/ )]),
sizeof(unsigned short int));
#else
unsigned short value = (symPtr->value&0xffff);
*(unsigned short*)&DataSpace[i] = htons(value);
#endif
} else {
#ifdef __sgi__
bcopy(&(symPtr->value), &(DataSpace[(i /*<<2*/ )]),
sizeof(unsigned long int));
#else
*(unsigned long int*)&DataSpace[i] = htonl(symPtr->value);
#endif
}
}
}
}
if (is_error) {
if (dry_run)
fprintf(stderr,"%s : object size: %4d instructions (%d bytes)\n",
output_file, PC, PC << 2);
exit(-1);
}
/*
* write out executable.
*/
if ((output=fopen(output_file,"w")) == NULL) {
fprintf(stderr,"ERROR : cannot open output file [%s]\n",
output_file);
exit(-1);
}
for (i=0; i<PC; i++) {
unsigned int tmp = htonl(InstructionSpace[i].instruction);
fwrite(&tmp, sizeof(unsigned int), 1,
output);
}
fclose(output);
/*
* write out data.
*/
sprintf(data_file,"%s.dat",output_file);
if ((data=fopen(data_file,"w")) == NULL) {
fprintf(stderr,"ERROR : cannot open data file [%s]\n",
data_file);
exit(-1);
}
for (i=0; i<DataCount; i++) {
fwrite(&(DataSpace[i]), sizeof(unsigned char), 1, data);
}
fclose(data);
fprintf(stderr,"%s : object size: %4d instructions (%d bytes)\n",
output_file, PC, PC << 2);
fprintf(stderr,"%s : data size: %4d bytes, based at 0x%08x.\n",
data_file, DataCount, DataBase);
/*
* write out symbols for debugger:
*/
sprintf(data_file,"%s.dbg",output_file);
if ((data=fopen(data_file,"w")) == NULL) {
fprintf(stderr,"ERROR : cannot open debugger symbol file [%s]\n",
data_file);
exit(-1);
}
for (i=0; i<symbol_count; i++) {
if (IsSymLabel(LinearSymbols[i])) {
fprintf(data,"%s %08x I\n",
LinearSymbols[i]->id,
LinearSymbols[i]->value);
}
if (IsSymData(LinearSymbols[i])) {
fprintf(data,"%s %08x D\n",
LinearSymbols[i]->id,
LinearSymbols[i]->value);
}
}
fclose(data);
/*
* write out listing (if requested).
*/
if (dolisting) {
sprintf(listing_file,"%s.lst",output_file);
if ((listing=fopen(listing_file,"w")) == NULL) {
fprintf(stderr,"ERROR : cannot open listing file [%s]\n",
listing_file);
exit(-1);
}
this_time = time(NULL);
fprintf(listing,"\n***\n");
fprintf(listing,"*** Source file : %s\n",input_file);
fprintf(listing,"*** Object file : %s\n",output_file);
fprintf(listing,"*** Listing file : %s\n",listing_file);
fprintf(listing,"*** Created by : %s\n",getenv("USER"));
fprintf(listing,"*** Create time : %s", asctime(localtime(&this_time)));
fprintf(listing,"*** Assembled at : 0x%08x\n",AssembleBase);
fprintf(listing,"*** Data Seg Base : 0x%08x\n",DataBase);
fprintf(listing,"***\n\n");
for (i=0; i<PC; i++) {
fprintf(listing,"0x%08x : 0x%08x ",
(i<<2)+AssembleBase, InstructionSpace[i].instruction);
rsp_DisasmInst(InstructionSpace[i].instruction, buf, (i<<2)+AssembleBase);
fprintf(listing,"%s\n",buf);
}
fclose(listing);
}
return(0);
}
unsigned int
ExternSymbolSearch(char *sym)
{
char junk1[80], junk2[80];
FILE *fp;
int i, j;
unsigned int value;
for (i=0; i<ExternSymCount; i++) {
if ((fp=fopen(ExternSymFiles[i],"r"))==NULL) {
fprintf(stderr,"ERROR : could not open symbol file [%s] to find symbol [%s].\n",
ExternSymFiles[i], sym);
} else {
value = 0xffffffff;
while (fgets(junk2, 80, fp) != NULL) {
sscanf(junk2,"%s %x",&(junk1[0]),&j);
if (strcmp(junk1,sym)==0) {
value = j;
fclose(fp);
return(value);
}
}
fclose(fp);
}
}
fprintf(stderr,"ERROR : could not find symbol [%s] in external symbol tables.\n",sym);
return(0xffffffff);
}