-
Notifications
You must be signed in to change notification settings - Fork 5
/
lc3.f
747 lines (661 loc) · 20.6 KB
/
lc3.f
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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
/* tab:8
*
* lc3.f - lexer for the LC-3 assembler
*
* "Copyright (c) 2003 by Steven S. Lumetta."
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose, without fee, and without written
* agreement is hereby granted, provided that the above copyright notice
* and the following two paragraphs appear in all copies of this software,
* that the files COPYING and NO_WARRANTY are included verbatim with
* any distribution, and that the contents of the file README are included
* verbatim as part of a file named README with any distribution.
*
* IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT,
* INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE AUTHOR
* HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
* BASIS, AND THE AUTHOR NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
* UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
*
* Author: Steve Lumetta
* Version: 1
* Creation Date: 18 October 2003
* Filename: lc3.f
* History:
* SSL 1 18 October 2003
* Copyright notices and Gnu Public License marker added.
*
* April 2011: fixed signedness and comment errors that were causing warnings
* and worrying students. SWS, Dartmouth
*
*/
%option noyywrap nounput
%{
/* questions...
should the assembler allow colons after label names? are the colons
part of the label? Currently I allow only alpha followed by alphanum and _.
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "symbol.h"
typedef enum opcode_t opcode_t;
enum opcode_t {
/* no opcode seen (yet) */
OP_NONE,
/* real instruction opcodes */
OP_ADD, OP_AND, OP_BR, OP_JMP, OP_JSR, OP_JSRR, OP_LD, OP_LDI, OP_LDR,
OP_LEA, OP_NOT, OP_RTI, OP_ST, OP_STI, OP_STR, OP_TRAP,
/* trap pseudo-ops */
OP_GETC, OP_HALT, OP_IN, OP_OUT, OP_PUTS, OP_PUTSP,
/* non-trap pseudo-ops */
OP_FILL, OP_RET, OP_STRINGZ,
/* directives */
OP_BLKW, OP_END, OP_ORIG,
NUM_OPS
};
static const char* const opnames[NUM_OPS] = {
/* no opcode seen (yet) */
"missing opcode",
/* real instruction opcodes */
"ADD", "AND", "BR", "JMP", "JSR", "JSRR", "LD", "LDI", "LDR", "LEA",
"NOT", "RTI", "ST", "STI", "STR", "TRAP",
/* trap pseudo-ops */
"GETC", "HALT", "IN", "OUT", "PUTS", "PUTSP",
/* non-trap pseudo-ops */
".FILL", "RET", ".STRINGZ",
/* directives */
".BLKW", ".END", ".ORIG",
};
typedef enum ccode_t ccode_t;
enum ccode_t {
CC_ = 0,
CC_P = 0x0200,
CC_Z = 0x0400,
CC_N = 0x0800
};
typedef enum operands_t operands_t;
enum operands_t {
O_RRR, O_RRI,
O_RR, O_RI, O_RL,
O_R, O_I, O_L, O_S,
O_,
NUM_OPERANDS
};
static const int op_format_ok[NUM_OPS] = {
/* no opcode seen (yet) */
0x200, /* no opcode, no operands */
/* real instruction formats */
0x003, /* ADD: RRR or RRI formats only */
0x003, /* AND: RRR or RRI formats only */
0x0C0, /* BR: I or L formats only */
0x020, /* JMP: R format only */
0x0C0, /* JSR: I or L formats only */
0x020, /* JSRR: R format only */
0x018, /* LD: RI or RL formats only */
0x018, /* LDI: RI or RL formats only */
0x002, /* LDR: RRI format only */
0x018, /* LEA: RI or RL formats only */
0x004, /* NOT: RR format only */
0x200, /* RTI: no operands allowed */
0x018, /* ST: RI or RL formats only */
0x018, /* STI: RI or RL formats only */
0x002, /* STR: RRI format only */
0x040, /* TRAP: I format only */
/* trap pseudo-op formats (no operands) */
0x200, /* GETC: no operands allowed */
0x200, /* HALT: no operands allowed */
0x200, /* IN: no operands allowed */
0x200, /* OUT: no operands allowed */
0x200, /* PUTS: no operands allowed */
0x200, /* PUTSP: no operands allowed */
/* non-trap pseudo-op formats */
0x0C0, /* .FILL: I or L formats only */
0x200, /* RET: no operands allowed */
0x100, /* .STRINGZ: S format only */
/* directive formats */
0x040, /* .BLKW: I format only */
0x200, /* .END: no operands allowed */
0x040 /* .ORIG: I format only */
};
typedef enum pre_parse_t pre_parse_t;
enum pre_parse_t {
NO_PP = 0,
PP_R1 = 1,
PP_R2 = 2,
PP_R3 = 4,
PP_I2 = 8,
PP_L2 = 16
};
static const pre_parse_t pre_parse[NUM_OPERANDS] = {
(PP_R1 | PP_R2 | PP_R3), /* O_RRR */
(PP_R1 | PP_R2), /* O_RRI */
(PP_R1 | PP_R2), /* O_RR */
(PP_R1 | PP_I2), /* O_RI */
(PP_R1 | PP_L2), /* O_RL */
PP_R1, /* O_R */
NO_PP, /* O_I */
NO_PP, /* O_L */
NO_PP, /* O_S */
NO_PP /* O_ */
};
typedef struct inst_t inst_t;
struct inst_t {
opcode_t op;
ccode_t ccode;
};
static int pass, line_num, num_errors, saw_orig, code_loc;
static inst_t inst;
static FILE* symout;
static FILE* objout;
static void new_inst_line ();
static void bad_operands ();
static void bad_line ();
static void parse_ccode (const char*);
static void generate_instruction (operands_t, const char*);
static void found_label (const char* lname);
%}
/* condition code specification */
CCODE [Nn]?[Zz]?[Pp]?
/* operand types */
REGISTER [rR][0-7]
HEX [xX][-]?[0-9a-fA-F]+
DECIMAL [#]?[-]?[0-9]+
IMMED {HEX}|{DECIMAL}
LABEL [A-Za-z][A-Za-z_0-9]*
/* strings with no carriage returns */
STRING \"([^"\n\r\\]*\\[^\n\r])*[^"\n\r\\]*\"
/* strings with carriage returns
STRLINE ([^"\n\r\\]*\\[^\n\r])*[^"\n\r\\]*
This needs to move down into the next section...
\" {BEGIN (ls_string);}
<ls_string>STRLINE {yymore ();}
<ls_string>[\\]?[\n][\r]? {line_num++; yymore ();}
<ls_string>\" {}
*/
/* operand and white space specification */
SPACE [ \t]
OP_SEP {SPACE}*,{SPACE}*
COMMENT [;][^\n\r]*
ENDLINE {SPACE}*{COMMENT}?\r?\n\r?
/* operand formats */
O_RRR {SPACE}+{REGISTER}{OP_SEP}{REGISTER}{OP_SEP}{REGISTER}{ENDLINE}
O_RRI {SPACE}+{REGISTER}{OP_SEP}{REGISTER}{OP_SEP}{IMMED}{ENDLINE}
O_RR {SPACE}+{REGISTER}{OP_SEP}{REGISTER}{ENDLINE}
O_RI {SPACE}+{REGISTER}{OP_SEP}{IMMED}{ENDLINE}
O_RL {SPACE}+{REGISTER}{OP_SEP}{LABEL}{ENDLINE}
O_R {SPACE}+{REGISTER}{ENDLINE}
O_I {SPACE}+{IMMED}{ENDLINE}
O_L {SPACE}+{LABEL}{ENDLINE}
O_S {SPACE}+{STRING}{ENDLINE}
O_ {ENDLINE}
/* need to define YY_INPUT... */
/* exclusive lexing states to read operands and eat garbage lines */
%x ls_operands ls_garbage
%%
/* rules for real instruction opcodes */
ADD {inst.op = OP_ADD; BEGIN (ls_operands);}
AND {inst.op = OP_AND; BEGIN (ls_operands);}
BR{CCODE} {inst.op = OP_BR; parse_ccode (yytext + 2); BEGIN (ls_operands);}
JMP {inst.op = OP_JMP; BEGIN (ls_operands);}
JSRR {inst.op = OP_JSRR; BEGIN (ls_operands);}
JSR {inst.op = OP_JSR; BEGIN (ls_operands);}
LDI {inst.op = OP_LDI; BEGIN (ls_operands);}
LDR {inst.op = OP_LDR; BEGIN (ls_operands);}
LD {inst.op = OP_LD; BEGIN (ls_operands);}
LEA {inst.op = OP_LEA; BEGIN (ls_operands);}
NOT {inst.op = OP_NOT; BEGIN (ls_operands);}
RTI {inst.op = OP_RTI; BEGIN (ls_operands);}
STI {inst.op = OP_STI; BEGIN (ls_operands);}
STR {inst.op = OP_STR; BEGIN (ls_operands);}
ST {inst.op = OP_ST; BEGIN (ls_operands);}
TRAP {inst.op = OP_TRAP; BEGIN (ls_operands);}
/* rules for trap pseudo-ols */
GETC {inst.op = OP_GETC; BEGIN (ls_operands);}
HALT {inst.op = OP_HALT; BEGIN (ls_operands);}
IN {inst.op = OP_IN; BEGIN (ls_operands);}
OUT {inst.op = OP_OUT; BEGIN (ls_operands);}
PUTS {inst.op = OP_PUTS; BEGIN (ls_operands);}
PUTSP {inst.op = OP_PUTSP; BEGIN (ls_operands);}
/* rules for non-trap pseudo-ops */
\.FILL {inst.op = OP_FILL; BEGIN (ls_operands);}
RET {inst.op = OP_RET; BEGIN (ls_operands);}
\.STRINGZ {inst.op = OP_STRINGZ; BEGIN (ls_operands);}
/* rules for directives */
\.BLKW {inst.op = OP_BLKW; BEGIN (ls_operands);}
\.END {return 0;}
\.ORIG {inst.op = OP_ORIG; BEGIN (ls_operands);}
/* rules for operand formats */
<ls_operands>{O_RRR} {generate_instruction (O_RRR, yytext); BEGIN (0);}
<ls_operands>{O_RRI} {generate_instruction (O_RRI, yytext); BEGIN (0);}
<ls_operands>{O_RR} {generate_instruction (O_RR, yytext); BEGIN (0);}
<ls_operands>{O_RI} {generate_instruction (O_RI, yytext); BEGIN (0);}
<ls_operands>{O_RL} {generate_instruction (O_RL, yytext); BEGIN (0);}
<ls_operands>{O_R} {generate_instruction (O_R, yytext); BEGIN (0);}
<ls_operands>{O_I} {generate_instruction (O_I, yytext); BEGIN (0);}
<ls_operands>{O_L} {generate_instruction (O_L, yytext); BEGIN (0);}
<ls_operands>{O_S} {generate_instruction (O_S, yytext); BEGIN (0);}
<ls_operands>{O_} {generate_instruction (O_, yytext); BEGIN (0);}
/* eat excess white space */
{SPACE}+ {}
{ENDLINE} {new_inst_line (); /* a blank line */ }
/* labels, with or without subsequent colons */\
/*
the colon form is used in some examples in the second edition
of the book, but may be removed in the third; it also allows
labels to use opcode and pseudo-op names, etc., however.
*/
{LABEL} {found_label (yytext);}
{LABEL}{SPACE}*: {found_label (yytext);}
/* error handling??? */
<ls_operands>[^\n\r]*<ENDLINE> {bad_operands (); BEGIN (0);}
{O_RRR}|{O_RRI}|{O_RR}|{O_RI}|{O_RL}|{O_R}|{O_I}|{O_S} {bad_operands ();}
. {BEGIN (ls_garbage);}
<ls_garbage>[^\n\r]*{ENDLINE} {bad_line (); BEGIN (0);}
%%
int
main (int argc, char** argv)
{
int len;
char* ext;
char* fname;
if (argc != 2) {
fprintf (stderr, "usage: %s <ASM filename>\n", argv[0]);
return 1;
}
/* Make our own copy of the filename. */
len = strlen (argv[1]);
if ((fname = malloc (len + 5)) == NULL) {
perror ("malloc");
return 3;
}
strcpy (fname, argv[1]);
/* Check for .asm extension; if not found, add it. */
if ((ext = strrchr (fname, '.')) == NULL || strcmp (ext, ".asm") != 0) {
ext = fname + len;
strcpy (ext, ".asm");
}
/* Open input file. */
if ((lc3in = fopen (fname, "r")) == NULL) {
fprintf (stderr, "Could not open %s for reading.\n", fname);
return 2;
}
/* Open output files. */
strcpy (ext, ".obj");
if ((objout = fopen (fname, "w")) == NULL) {
fprintf (stderr, "Could not open %s for writing.\n", fname);
return 2;
}
strcpy (ext, ".sym");
if ((symout = fopen (fname, "w")) == NULL) {
fprintf (stderr, "Could not open %s for writing.\n", fname);
return 2;
}
/* FIXME: Do we really need to exactly match old format for compatibility
with Windows simulator? */
fprintf (symout, "// Symbol table\n");
fprintf (symout, "// Scope level 0:\n");
fprintf (symout, "//\tSymbol Name Page Address\n");
fprintf (symout, "//\t---------------- ------------\n");
puts ("STARTING PASS 1");
pass = 1;
line_num = 0;
num_errors = 0;
saw_orig = 0;
code_loc = 0x3000;
new_inst_line ();
yylex ();
printf ("%d errors found in first pass.\n", num_errors);
if (num_errors > 0)
return 1;
if (fseek (lc3in, 0, SEEK_SET) != 0) {
perror ("fseek to start of ASM file");
return 3;
}
yyrestart (lc3in);
puts ("STARTING PASS 2");
pass = 2;
line_num = 0;
num_errors = 0;
saw_orig = 0;
code_loc = 0x3000;
new_inst_line ();
yylex ();
printf ("%d errors found in second pass.\n", num_errors);
if (num_errors > 0)
return 1;
fprintf (symout, "\n");
fclose (symout);
fclose (objout);
return 0;
}
static void
new_inst_line ()
{
inst.op = OP_NONE;
inst.ccode = CC_;
line_num++;
}
static void
bad_operands ()
{
fprintf (stderr, "%3d: illegal operands for %s\n",
line_num, opnames[inst.op]);
num_errors++;
new_inst_line ();
}
static void
bad_line ()
{
fprintf (stderr, "%3d: contains unrecognizable characters\n",
line_num);
num_errors++;
new_inst_line ();
}
static int
read_val (const char* s, int* vptr, int bits)
{
char* trash;
long v;
if (*s == 'x' || *s == 'X')
v = strtol (s + 1, &trash, 16);
else {
if (*s == '#')
s++;
v = strtol (s, &trash, 10);
}
if (v >= 0x8000)
v |= 0xFFFF0000;
if (v < -(1L << (bits - 1)) || v >= (1L << bits)) {
fprintf (stderr, "%3d: constant outside of allowed range\n", line_num);
num_errors++;
return -1;
}
if ((v & (1UL << (bits - 1))) != 0)
v |= ~((1UL << bits) - 1);
*vptr = v;
return 0;
}
static void
write_value (int val)
{
unsigned char out[2];
code_loc = (code_loc + 1) & 0xFFFF;
if (pass == 1)
return;
/* FIXME: just htons... */
out[0] = (val >> 8);
out[1] = (val & 0xFF);
fwrite (out, 2, 1, objout);
}
static char*
sym_name (const char* name)
{
unsigned char* local = (unsigned char *)strdup (name);
unsigned char* cut;
/* Not fast, but no limit on label length...who cares? */
for (cut = local; *cut != 0 && !isspace (*cut) && *cut != ':'; cut++);
*cut = 0;
return (char *)local;
}
static int
find_label (const char* optarg, int bits)
{
unsigned char* local;
symbol_t* label;
int limit, value;
if (pass == 1)
return 0;
local = (unsigned char *)sym_name (optarg);
label = find_symbol ((char *)local, NULL);
if (label != NULL) {
value = label->addr;
if (bits != 16) { /* Everything except 16 bits is PC-relative. */
limit = (1L << (bits - 1));
value -= code_loc + 1;
if (value < -limit || value >= limit) {
fprintf (stderr, "%3d: label \"%s\" at distance %d (allowed "
"range is %d to %d)\n", line_num, local, value,
-limit, limit - 1);
goto bad_label;
}
return value;
}
free (local);
return label->addr;
}
fprintf (stderr, "%3d: unknown label \"%s\"\n", line_num, local);
bad_label:
num_errors++;
free (local);
return 0;
}
static void
generate_instruction (operands_t operands, const char* opstr)
{
int val, r1, r2, r3;
const unsigned char* o1;
const unsigned char* o2;
const unsigned char* o3;
const unsigned char* str;
if ((op_format_ok[inst.op] & (1UL << operands)) == 0) {
bad_operands ();
return;
}
o1 = (unsigned char *)opstr;
while (isspace (*o1)) o1++;
if ((o2 = (unsigned char *)strchr ((char *)o1, ',')) != NULL) {
o2++;
while (isspace (*o2)) o2++;
if ((o3 = (unsigned char *)strchr ((char *)o2, ',')) != NULL) {
o3++;
while (isspace (*o3)) o3++;
}
} else
o3 = NULL;
if (inst.op == OP_ORIG) {
if (saw_orig == 0) {
if (read_val ((char *)o1, &code_loc, 16) == -1)
/* Pick a value; the error prevents code generation. */
code_loc = 0x3000;
else {
write_value (code_loc);
code_loc--; /* Starting point doesn't count as code. */
}
saw_orig = 1;
} else if (saw_orig == 1) {
fprintf (stderr, "%3d: multiple .ORIG directives found\n",
line_num);
saw_orig = 2;
}
new_inst_line ();
return;
}
if (saw_orig == 0) {
fprintf (stderr, "%3d: instruction appears before .ORIG\n",
line_num);
num_errors++;
new_inst_line ();
saw_orig = 2;
return;
}
if ((pre_parse[operands] & PP_R1) != 0)
r1 = o1[1] - '0';
if ((pre_parse[operands] & PP_R2) != 0)
r2 = o2[1] - '0';
if ((pre_parse[operands] & PP_R3) != 0)
r3 = o3[1] - '0';
if ((pre_parse[operands] & PP_I2) != 0)
(void)read_val ((char *)o2, &val, 9);
if ((pre_parse[operands] & PP_L2) != 0)
val = find_label ((char *)o2, 9);
switch (inst.op) {
/* Generate real instruction opcodes. */
case OP_ADD:
if (operands == O_RRI) {
/* Check or read immediate range (error in first pass
prevents execution of second, so never fails). */
(void)read_val ((char *)o3, &val, 5);
write_value (0x1020 | (r1 << 9) | (r2 << 6) | (val & 0x1F));
} else
write_value (0x1000 | (r1 << 9) | (r2 << 6) | r3);
break;
case OP_AND:
if (operands == O_RRI) {
/* Check or read immediate range (error in first pass
prevents execution of second, so never fails). */
(void)read_val ((char *)o3, &val, 5);
write_value (0x5020 | (r1 << 9) | (r2 << 6) | (val & 0x1F));
} else
write_value (0x5000 | (r1 << 9) | (r2 << 6) | r3);
break;
case OP_BR:
if (operands == O_I)
(void)read_val ((char *)o1, &val, 9);
else /* O_L */
val = find_label ((char *)o1, 9);
write_value (inst.ccode | (val & 0x1FF));
break;
case OP_JMP:
write_value (0xC000 | (r1 << 6));
break;
case OP_JSR:
if (operands == O_I)
(void)read_val ((char *)o1, &val, 11);
else /* O_L */
val = find_label ((char *)o1, 11);
write_value (0x4800 | (val & 0x7FF));
break;
case OP_JSRR:
write_value (0x4000 | (r1 << 6));
break;
case OP_LD:
write_value (0x2000 | (r1 << 9) | (val & 0x1FF));
break;
case OP_LDI:
write_value (0xA000 | (r1 << 9) | (val & 0x1FF));
break;
case OP_LDR:
(void)read_val ((char *)o3, &val, 6);
write_value (0x6000 | (r1 << 9) | (r2 << 6) | (val & 0x3F));
break;
case OP_LEA:
write_value (0xE000 | (r1 << 9) | (val & 0x1FF));
break;
case OP_NOT:
write_value (0x903F | (r1 << 9) | (r2 << 6));
break;
case OP_RTI:
write_value (0x8000);
break;
case OP_ST:
write_value (0x3000 | (r1 << 9) | (val & 0x1FF));
break;
case OP_STI:
write_value (0xB000 | (r1 << 9) | (val & 0x1FF));
break;
case OP_STR:
(void)read_val ((char *)o3, &val, 6);
write_value (0x7000 | (r1 << 9) | (r2 << 6) | (val & 0x3F));
break;
case OP_TRAP:
(void)read_val ((char *)o1, &val, 8);
write_value (0xF000 | (val & 0xFF));
break;
/* Generate trap pseudo-ops. */
case OP_GETC: write_value (0xF020); break;
case OP_HALT: write_value (0xF025); break;
case OP_IN: write_value (0xF023); break;
case OP_OUT: write_value (0xF021); break;
case OP_PUTS: write_value (0xF022); break;
case OP_PUTSP: write_value (0xF024); break;
/* Generate non-trap pseudo-ops. */
case OP_FILL:
if (operands == O_I) {
(void)read_val ((char *)o1, &val, 16);
val &= 0xFFFF;
} else /* O_L */
val = find_label ((char *)o1, 16);
write_value (val);
break;
case OP_RET:
write_value (0xC1C0);
break;
case OP_STRINGZ:
/* We must count locations written in pass 1;
write_value squashes the writes. */
for (str = o1 + 1; str[0] != '\"'; str++) {
if (str[0] == '\\') {
switch (str[1]) {
case 'a': write_value ('\a'); str++; break;
case 'b': write_value ('\b'); str++; break;
case 'e': write_value ('\e'); str++; break;
case 'f': write_value ('\f'); str++; break;
case 'n': write_value ('\n'); str++; break;
case 'r': write_value ('\r'); str++; break;
case 't': write_value ('\t'); str++; break;
case 'v': write_value ('\v'); str++; break;
case '\\': write_value ('\\'); str++; break;
case '\"': write_value ('\"'); str++; break;
/* FIXME: support others too? */
default: write_value (str[1]); str++; break;
}
} else
write_value (*str);
}
write_value (0);
break;
case OP_BLKW:
(void)read_val ((char *)o1, &val, 16);
val &= 0xFFFF;
while (val-- > 0)
write_value (0x0000);
break;
/* Handled earlier or never used, so never seen here. */
case OP_NONE:
case OP_ORIG:
case OP_END:
case NUM_OPS:
break;
}
new_inst_line ();
}
static void
parse_ccode (const char* ccstr)
{
if (*ccstr == 'N' || *ccstr == 'n') {
inst.ccode |= CC_N;
ccstr++;
}
if (*ccstr == 'Z' || *ccstr == 'z') {
inst.ccode |= CC_Z;
ccstr++;
}
if (*ccstr == 'P' || *ccstr == 'p')
inst.ccode |= CC_P;
/* special case: map BR to BRnzp */
if (inst.ccode == CC_)
inst.ccode = CC_P | CC_Z | CC_N;
}
static void
found_label (const char* lname)
{
unsigned char* local = (unsigned char *)sym_name (lname);
if (pass == 1) {
if (add_symbol ((char *)local, code_loc, 0) == -1) {
fprintf (stderr, "%3d: label %s has already appeared\n",
line_num, local);
num_errors++;
} else
fprintf (symout, "//\t%-16s %04X\n", local, code_loc);
}
free (local);
}