Mercurial > hg > Members > kono > os9 > sbc09
annotate a09.c @ 25:c4e3abe97e2c
rmb in os9 mode
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 10 Jul 2018 15:38:58 +0900 |
parents | 7104ad38bed3 |
children | 51a35f0347f6 |
rev | line source |
---|---|
0 | 1 /* A09, 6809 Assembler. |
2 | |
3 created 1993,1994 by L.C. Benschop. | |
4 copyleft (c) 1994-2014 by the sbc09 team, see AUTHORS for more details. | |
5 license: GNU General Public License version 2, see LICENSE for more details. | |
6 THERE IS NO WARRANTY ON THIS PROGRAM. | |
7 | |
8 Generates binary image file from the lowest to | |
9 the highest address with actually assembled data. | |
10 | |
5 | 11 Machin edependencies: |
0 | 12 char is 8 bits. |
13 short is 16 bits. | |
14 integer arithmetic is twos complement. | |
15 | |
16 syntax a09 [-o filename] [-l filename] sourcefile. | |
17 | |
18 Options | |
19 -o filename name of the output file (default name minus a09 suffix) | |
20 -s filename name of the s-record output file (default its a binary file) | |
21 -l filename list file name (default no listing) | |
22 -d enable debugging | |
23 | |
24 recognized pseudoops: | |
25 extern public | |
26 macro endm if else endif | |
27 org equ set setdp | |
28 fcb fcw fdb fcc rmb | |
29 end include title | |
30 | |
31 Not all of these are actually IMPLEMENTED!!!!!! | |
32 | |
33 Revisions: | |
34 1993-11-03 v0.1 | |
35 Initial version. | |
36 1994/03/21 v0.2 | |
37 Fixed PC relative addressing bug | |
38 Added SET, SETDP, INCLUDE. IF/ELSE/ENDIF | |
39 No macros yet, and no separate linkable modules. | |
40 2012-06-04 j at klasek at | |
41 New: debugging parameter/option. | |
42 Fixed additional possible issue PC relative addressing. | |
43 Compatibility: Octal number prefix "&". | |
44 2014-07-15 j at klasek at | |
45 Fixed usage message. | |
46 */ | |
47 | |
48 #include <stdlib.h> | |
49 #include <stdio.h> | |
50 #include <string.h> | |
51 #include <ctype.h> | |
52 | |
53 #define NLABELS 2048 | |
54 #define MAXIDLEN 16 | |
55 #define MAXLISTBYTES 8 | |
56 #define FNLEN 30 | |
57 #define LINELEN 128 | |
58 | |
59 static int debug=0; | |
60 | |
61 struct oprecord{char * name; | |
62 unsigned char cat; | |
63 unsigned short code;}; | |
64 | |
65 /* Instruction categories: | |
66 0 one byte oprcodes NOP | |
67 1 two byte opcodes SWI2 | |
68 2 opcodes w. imm byte ANDCC | |
69 3 LEAX etc. | |
70 4 short branches. BGE | |
71 5 long branches 2byte opc LBGE | |
72 6 long branches 1byte opc LBRA | |
73 7 accumulator instr. ADDA | |
74 8 double reg instr 1byte opc LDX | |
75 9 double reg instr 2 byte opc LDY | |
76 10 single address instrs NEG | |
77 11 TFR, EXG | |
78 12 push,pull | |
79 13 pseudoops | |
80 */ | |
81 | |
82 struct oprecord optable[]={ | |
83 {"ABX",0,0x3a},{"ADCA",7,0x89},{"ADCB",7,0xc9}, | |
84 {"ADDA",7,0x8b},{"ADDB",7,0xcb},{"ADDD",8,0xc3}, | |
85 {"ANDA",7,0X84},{"ANDB",7,0xc4},{"ANDCC",2,0x1c}, | |
86 {"ASL",10,0x08},{"ASLA",0,0x48},{"ASLB",0,0x58}, | |
87 {"ASR",10,0x07},{"ASRA",0,0x47},{"ASRB",0,0x57}, | |
88 {"BCC",4,0x24},{"BCS",4,0x25},{"BEQ",4,0x27}, | |
89 {"BGE",4,0x2c},{"BGT",4,0x2e},{"BHI",4,0x22}, | |
90 {"BHS",4,0x24},{"BITA",7,0x85},{"BITB",7,0xc5}, | |
91 {"BLE",4,0x2f},{"BLO",4,0x25},{"BLS",4,0x23}, | |
92 {"BLT",4,0x2d},{"BMI",4,0x2b},{"BNE",4,0x26}, | |
93 {"BPL",4,0x2a},{"BRA",4,0x20},{"BRN",4,0x21}, | |
94 {"BSR",4,0x8d}, | |
95 {"BVC",4,0x28},{"BVS",4,0x29}, | |
96 {"CLC",1,0x1cfe},{"CLF",1,0x1cbf},{"CLI",1,0x1cef}, | |
97 {"CLIF",1,0x1caf}, | |
98 {"CLR",10,0x0f},{"CLRA",0,0x4f},{"CLRB",0,0x5f}, | |
99 {"CLV",1,0x1cfd}, | |
100 {"CMPA",7,0x81},{"CMPB",7,0xc1},{"CMPD",9,0x1083}, | |
101 {"CMPS",9,0x118c},{"CMPU",9,0x1183},{"CMPX",8,0x8c}, | |
102 {"CMPY",9,0x108c}, | |
103 {"COM",10,0x03},{"COMA",0,0x43},{"COMB",0,0x53}, | |
104 {"CWAI",2,0x3c},{"DAA",0,0x19}, | |
105 {"DEC",10,0x0a},{"DECA",0,0x4a},{"DECB",0,0x5a}, | |
106 {"DES",1,0x327f},{"DEU",1,0x335f},{"DEX",1,0x301f}, | |
107 {"DEY",1,0x313f}, | |
108 {"ELSE",13,1}, | |
109 {"EMOD",13,25}, | |
110 {"END",13,2}, | |
111 {"ENDC",13,3}, | |
112 {"ENDIF",13,3}, | |
113 {"ENDM",13,4}, | |
114 {"EORA",7,0x88},{"EORB",7,0xc8}, | |
115 {"EQU",13,5},{"EXG",11,0x1e},{"EXTERN",13,6}, | |
116 {"FCB",13,7},{"FCC",13,8}, | |
117 {"FCS",13,23}, | |
118 {"FCW",13,9}, | |
119 {"FDB",13,9}, | |
120 {"IF",13,10}, | |
121 {"IFEQ",13,29}, | |
122 {"IFGT",13,30}, | |
123 {"IFNE",13,28}, | |
124 {"IFP1",13,21}, | |
125 {"INC",10,0x0c},{"INCA",0,0x4c},{"INCB",0,0x5c}, | |
126 {"INCLUDE",13,16}, | |
127 {"INS",1,0x3261},{"INU",1,0x3341},{"INX",1,0x3001}, | |
128 {"INY",1,0x3121},{"JMP",10,0x0e},{"JSR",8,0x8d}, | |
129 {"LBCC",5,0x1024},{"LBCS",5,0x1025},{"LBEQ",5,0x1027}, | |
130 {"LBGE",5,0x102c},{"LBGT",5,0x102e},{"LBHI",5,0x1022}, | |
131 {"LBHS",5,0x1024}, | |
132 {"LBLE",5,0x102f},{"LBLO",5,0x1025},{"LBLS",5,0x1023}, | |
133 {"LBLT",5,0x102d},{"LBMI",5,0x102b},{"LBNE",5,0x1026}, | |
134 {"LBPL",5,0x102a},{"LBRA",6,0x16},{"LBRN",5,0x1021}, | |
135 {"LBSR",6,0x17}, | |
136 {"LBVC",5,0x1028},{"LBVS",5,0x1029}, | |
137 {"LDA",7,0x86},{"LDB",7,0xc6},{"LDD",8,0xcc}, | |
138 {"LDS",9,0x10ce},{"LDU",8,0xce},{"LDX",8,0x8e}, | |
139 {"LDY",9,0x108e},{"LEAS",3,0x32}, | |
140 {"LEAU",3,0x33},{"LEAX",3,0x30},{"LEAY",3,0x31}, | |
141 {"LSL",10,0x08},{"LSLA",0,0x48},{"LSLB",0,0x58}, | |
142 {"LSR",10,0x04},{"LSRA",0,0x44},{"LSRB",0,0x54}, | |
143 {"MACRO",13,11}, | |
144 {"MOD",13,24}, | |
145 {"MUL",0,0x3d}, | |
146 {"NAM",13,26}, | |
147 {"NEG",10,0x00},{"NEGA",0,0x40},{"NEGB",0,0x50}, | |
148 {"NOP",0,0x12}, | |
149 {"OPT",13,19}, | |
150 {"ORA",7,0x8a},{"ORB",7,0xca},{"ORCC",2,0x1a}, | |
151 {"ORG",13,12}, | |
152 {"OS9",13,32}, | |
153 {"PAG",13,20}, {"PAGE",13,20}, | |
154 {"PSHS",12,0x34},{"PSHU",12,0x36},{"PUBLIC",13,13}, | |
155 {"PULS",12,0x35},{"PULU",12,0x37},{"RMB",13,0}, | |
156 {"ROL",10,0x09},{"ROLA",0,0x49},{"ROLB",0,0x59}, | |
157 {"ROR",10,0x06},{"RORA",0,0x46},{"RORB",0,0x56}, | |
158 {"RTI",0,0x3b},{"RTS",0,0x39}, | |
159 {"SBCA",7,0x82},{"SBCB",7,0xc2}, | |
160 {"SEC",1,0x1a01},{"SEF",1,0x1a40},{"SEI",1,0x1a10}, | |
161 {"SEIF",1,0x1a50},{"SET",13,15}, | |
162 {"SETDP",13,14},{"SEV",1,0x1a02},{"SEX",0,0x1d}, | |
163 {"STA",7,0x87},{"STB",7,0xc7},{"STD",8,0xcd}, | |
164 {"STS",9,0x10cf},{"STU",8,0xcf},{"STX",8,0x8f}, | |
165 {"STY",9,0x108f}, | |
166 {"SUBA",7,0x80},{"SUBB",7,0xc0},{"SUBD",8,0x83}, | |
167 {"SWI",0,0x3f},{"SWI2",1,0x103f},{"SWI3",1,0x113f}, | |
168 {"SYNC",0,0x13},{"TFR",11,0x1f}, | |
169 {"TITLE",13,18}, | |
170 {"TST",10,0x0d},{"TSTA",0,0x4d},{"TSTB",0,0x5d}, | |
171 {"TTL",13,18}, | |
172 {"USE",13,27}, | |
173 }; | |
174 | |
175 struct symrecord{char name[MAXIDLEN+1]; | |
176 char cat; | |
177 unsigned short value; | |
178 }; | |
179 | |
180 int symcounter=0; | |
3 | 181 int os9 = 0; // os9 flag |
6 | 182 int prevloc = 0; |
0 | 183 |
8 | 184 /* expression categories... |
185 ECORD all zeros is ordinary constant. | |
186 ECADR bit 1 indicates address within module. | |
187 ECEXT bit 2 indicates external address. | |
188 ECLBL bit 3 public label | |
189 ECABS bit 4 indicates this can't be relocated if it's an address. | |
190 ECNEG bit 5 indicates address (if any) is negative. | |
191 */ | |
192 | |
193 | |
194 /* Symbol categories. exprcat ( symcat & 0xe ) | |
195 0 SCC Constant value (from equ). ECORD | |
196 1 SCV Variable value (from set) ECORD | |
197 2 SCC__ADR Address within program module (label). ECADR | |
198 3 SCV__ADR Variable containing address. ECADR | |
199 4 SC_E_ADR Adress in other program module (extern) ECEXT | |
200 5 SCVE_ADR Variable containing external address. ECEXT | |
201 6 SCU _ADR Unresolved address. ECEXT+ECADR | |
202 7 SCV_UADR Variable containing unresolved address. ECEXT+ECADR | |
203 8 SC___LBL Public label. ECLBL | |
204 9 SCMACRO Macro definition. xxx | |
205 10 SCU__LBL Public label (yet undefined). ECADR+ECLBL | |
206 11 SCPARAM parameter name. ECADR+ECLBL | |
207 12 SCLOCAL local label. ECEXT+ECLBL | |
208 13 SCEMPTY empty. xxx | |
0 | 209 */ |
210 | |
211 struct symrecord symtable[NLABELS]; | |
212 | |
213 void processfile(char *name); | |
214 | |
215 struct oprecord * findop(char * nm) | |
216 /* Find operation (mnemonic) in table using binary search */ | |
217 { | |
218 int lo,hi,i,s; | |
219 lo=0;hi=sizeof(optable)/sizeof(optable[0])-1; | |
220 do { | |
221 i=(lo+hi)/2; | |
222 s=strcmp(optable[i].name,nm); | |
223 if(s<0) lo=i+1; | |
224 else if(s>0) hi=i-1; | |
225 else break; | |
226 } while(hi>=lo); | |
227 if (s) return NULL; | |
228 return optable+i; | |
229 } | |
230 | |
231 struct symrecord * findsym(char * nm) | |
232 /* finds symbol table record; inserts if not found | |
233 uses binary search, maintains sorted table */ | |
234 { | |
235 int lo,hi,i,j,s; | |
236 lo=0;hi=symcounter-1; | |
237 s=1;i=0; | |
238 while (hi>=lo) { | |
239 i=(lo+hi)/2; | |
240 s=strcmp(symtable[i].name,nm); | |
241 if(s<0) lo=i+1; | |
242 else if(s>0) hi=i-1; | |
243 else break; | |
244 } | |
245 if(s) { | |
246 i=(s<0?i+1:i); | |
247 if(symcounter==NLABELS) { | |
248 fprintf(stderr,"Sorry, no storage for symbols!!!"); | |
249 exit(4); | |
250 } | |
251 for(j=symcounter;j>i;j--) symtable[j]=symtable[j-1]; | |
252 symcounter++; | |
253 strcpy(symtable[i].name,nm); | |
254 symtable[i].cat=13; | |
255 } | |
256 return symtable+i; | |
257 } | |
258 | |
259 FILE *listfile,*objfile; | |
260 char *listname,*objname,*srcname,*curname; | |
261 int lineno; | |
262 | |
263 void | |
264 outsymtable() | |
265 { | |
266 int i,j=0; | |
267 fprintf(listfile,"\nSYMBOL TABLE"); | |
268 for(i=0;i<symcounter;i++) | |
269 if(symtable[i].cat!=13) { | |
270 if(j%4==0)fprintf(listfile,"\n"); | |
271 fprintf(listfile,"%10s %02d %04x",symtable[i].name,symtable[i].cat, | |
272 symtable[i].value); | |
273 j++; | |
274 } | |
275 fprintf(listfile,"\n"); | |
276 } | |
277 | |
278 struct regrecord{char *name;unsigned char tfr,psh;}; | |
279 struct regrecord regtable[]= | |
280 {{"D",0x00,0x06},{"X",0x01,0x10},{"Y",0x02,0x20}, | |
281 {"U",0x03,0x40},{"S",0x04,0x40},{"PC",0x05,0x80}, | |
282 {"A",0x08,0x02},{"B",0x09,0x04},{"CC",0x0a,0x01}, | |
283 {"CCR",0x0a,0x01},{"DP",0x0b,0x08},{"DPR",0x0b,0x08}}; | |
284 | |
285 struct regrecord * findreg(char *nm) | |
286 { | |
287 int i; | |
288 for(i=0;i<12;i++) { | |
289 if(strcmp(regtable[i].name,nm)==0) return regtable+i; | |
290 } | |
291 return 0; | |
292 } | |
293 | |
294 | |
295 char pass; /* Assembler pass=1 or 2 */ | |
296 char listing; /* flag to indicate listing */ | |
297 char relocatable; /* flag to indicate relocatable object. */ | |
298 char terminate; /* flag to indicate termination. */ | |
299 char generating; /* flag to indicate that we generate code */ | |
300 unsigned short loccounter,oldlc; /* Location counter */ | |
301 | |
302 char inpline[128]; /* Current input line (not expanded)*/ | |
303 char srcline[128]; /* Current source line */ | |
304 char * srcptr; /* Pointer to line being parsed */ | |
305 | |
306 char unknown; /* flag to indicate value unknown */ | |
307 char certain; /* flag to indicate value is certain at pass 1*/ | |
308 int error; /* flags indicating errors in current line. */ | |
309 int errors; /* number of errors */ | |
310 char exprcat; /* category of expression being parsed, eg. | |
311 label or constant, this is important when | |
312 generating relocatable object code. */ | |
313 | |
6 | 314 void generate() |
315 { | |
316 generating = 1; | |
317 if (prevloc) { | |
7
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
318 oldlc = loccounter = prevloc-1 ; |
25 | 319 os9 = prevloc = 0; |
6 | 320 } |
321 } | |
322 | |
0 | 323 |
324 char namebuf[MAXIDLEN+1]; | |
325 | |
326 void | |
327 err(int er) { | |
328 error |= er ; | |
329 } | |
330 | |
331 void | |
332 scanname() | |
333 { | |
334 int i=0; | |
335 char c; | |
336 while(1) { | |
337 c=*srcptr++; | |
338 if(c>='a'&&c<='z')c-=32; | |
339 if(c!='.'&&c!='$'&&(c<'0'||c>'9')&&(c<'A'||c>'Z'))break; | |
340 if(i<MAXIDLEN)namebuf[i++]=c; | |
341 } | |
342 namebuf[i]=0; | |
343 srcptr--; | |
344 } | |
345 | |
346 void | |
347 skipspace() | |
348 { | |
349 char c; | |
350 do { | |
351 c=*srcptr++; | |
352 } while(c==' '||c=='\t'); | |
353 srcptr--; | |
354 } | |
355 | |
356 short scanexpr(int); | |
357 | |
358 short scandecimal() | |
359 { | |
360 char c; | |
361 short t=0; | |
362 c=*srcptr++; | |
363 while(isdigit(c)) { | |
364 t=t*10+c-'0'; | |
365 c=*srcptr++; | |
366 } | |
367 srcptr--; | |
368 return t; | |
369 } | |
370 | |
371 short scanhex() | |
372 { | |
373 short t=0,i=0; | |
374 srcptr++; | |
375 scanname(); | |
376 while(namebuf[i]>='0'&&namebuf[i]<='F') { | |
377 t=t*16+namebuf[i]-'0'; | |
378 if(namebuf[i]>'9')t-=7; | |
379 i++; | |
380 } | |
381 if(i==0)error|=1; | |
382 return t; | |
383 } | |
384 | |
385 short scanchar() | |
386 { | |
387 short t; | |
388 srcptr++; | |
389 t=*srcptr; | |
390 if(t)srcptr++; | |
391 if (*srcptr=='\'')srcptr++; | |
392 return t; | |
393 } | |
394 | |
395 short scanbin() | |
396 { | |
397 char c; | |
398 short t=0; | |
399 srcptr++; | |
400 c=*srcptr++; | |
401 while(c=='0'||c=='1') { | |
402 t=t*2+c-'0'; | |
403 c=*srcptr++; | |
404 } | |
405 srcptr--; | |
406 return t; | |
407 } | |
408 | |
409 short scanoct() | |
410 { | |
411 char c; | |
412 short t=0; | |
413 srcptr++; | |
414 c=*srcptr++; | |
415 while(c>='0'&&c<='7') { | |
416 t=t*8+c-'0'; | |
417 c=*srcptr++; | |
418 } | |
419 srcptr--; | |
420 return t; | |
421 } | |
422 | |
423 | |
424 short scanlabel() | |
425 { | |
426 struct symrecord * p; | |
427 scanname(); | |
428 p=findsym(namebuf); | |
429 if(p->cat==13) { | |
430 p->cat=6; | |
431 p->value=0; | |
432 } | |
433 if(p->cat==9||p->cat==11)error|=1; | |
434 exprcat=p->cat&14; | |
435 if(exprcat==6||exprcat==10)unknown=1; | |
436 if(((exprcat==2||exprcat==8) | |
437 && (unsigned short)(p->value)>(unsigned short)loccounter)|| | |
438 exprcat==4) | |
439 certain=0; | |
440 if(exprcat==8||exprcat==6||exprcat==10)exprcat=2; | |
441 return p->value; | |
442 } | |
443 | |
444 | |
445 short scanfactor() | |
446 { | |
447 char c; | |
448 short t; | |
449 skipspace(); | |
450 c=*srcptr; | |
451 if(isalpha(c))return scanlabel(); | |
452 else if(isdigit(c))return scandecimal(); | |
453 else switch(c) { | |
11 | 454 case '*' : if(prevloc) { srcptr++;exprcat|=2;return prevloc-1; } |
455 case '.' : srcptr++;exprcat|=2;return loccounter; | |
0 | 456 case '$' : return scanhex(); |
457 case '%' : return scanbin(); | |
458 case '&' : /* compatibility */ | |
459 case '@' : return scanoct(); | |
460 case '\'' : return scanchar(); | |
461 case '(' : srcptr++;t=scanexpr(0);skipspace(); | |
462 if(*srcptr==')')srcptr++;else error|=1; | |
463 return t; | |
464 case '-' : srcptr++;exprcat^=32;return -scanfactor(); | |
465 case '+' : srcptr++;return scanfactor(); | |
466 case '!' : srcptr++;exprcat|=16;return !scanfactor(); | |
467 case '^' : | |
468 case '~' : srcptr++;exprcat|=16;return ~scanfactor(); | |
469 } | |
470 error|=1; | |
471 return 0; | |
472 } | |
473 | |
474 #define EXITEVAL {srcptr--;return t;} | |
475 | |
476 #define RESOLVECAT if((oldcat&15)==0)oldcat=0;\ | |
477 if((exprcat&15)==0)exprcat=0;\ | |
478 if((exprcat==2&&oldcat==34)||(exprcat==34&&oldcat==2)) {\ | |
479 exprcat=0;\ | |
480 oldcat=0;}\ | |
481 exprcat|=oldcat;\ | |
482 /* resolve such cases as constant added to address or difference between | |
483 two addresses in same module */ | |
484 | |
485 | |
486 short scanexpr(int level) /* This is what you call _recursive_ descent!!!*/ | |
487 { | |
488 short t,u; | |
489 char oldcat,c; | |
490 exprcat=0; | |
491 if(level==10)return scanfactor(); | |
492 t=scanexpr(level+1); | |
493 while(1) { | |
5 | 494 // skipspace(); |
0 | 495 c=*srcptr++; |
496 switch(c) { | |
497 case '*':oldcat=exprcat; | |
498 t*=scanexpr(10); | |
499 exprcat|=oldcat|16; | |
500 break; | |
501 case '/':oldcat=exprcat; | |
502 u=scanexpr(10); | |
503 if(u)t/=u;else error|=1; | |
504 exprcat|=oldcat|16; | |
505 break; | |
506 case '%':oldcat=exprcat; | |
507 u=scanexpr(10); | |
508 if(u)t%=u;else error|=1; | |
509 exprcat|=oldcat|16; | |
510 break; | |
511 case '+':if(level==9)EXITEVAL | |
512 oldcat=exprcat; | |
513 t+=scanexpr(9); | |
514 RESOLVECAT | |
515 break; | |
516 case '-':if(level==9)EXITEVAL | |
517 oldcat=exprcat; | |
518 t-=scanexpr(9); | |
519 exprcat^=32; | |
520 RESOLVECAT | |
521 break; | |
522 case '<':if(*(srcptr)=='<') { | |
523 if(level>=8)EXITEVAL | |
524 srcptr++; | |
525 oldcat=exprcat; | |
526 t<<=scanexpr(8); | |
527 exprcat|=oldcat|16; | |
528 break; | |
529 } else if(*(srcptr)=='=') { | |
530 if(level>=7)EXITEVAL | |
531 srcptr++; | |
532 oldcat=exprcat; | |
533 t=t<=scanexpr(7); | |
534 exprcat|=oldcat|16; | |
535 break; | |
536 } else { | |
537 if(level>=7)EXITEVAL | |
538 oldcat=exprcat; | |
539 t=t<scanexpr(7); | |
540 exprcat|=oldcat|16; | |
541 break; | |
542 } | |
543 case '>':if(*(srcptr)=='>') { | |
544 if(level>=8)EXITEVAL | |
545 srcptr++; | |
546 oldcat=exprcat; | |
547 t>>=scanexpr(8); | |
548 exprcat|=oldcat|16; | |
549 break; | |
550 } else if(*(srcptr)=='=') { | |
551 if(level>=7)EXITEVAL | |
552 srcptr++; | |
553 oldcat=exprcat; | |
554 t=t>=scanexpr(7); | |
555 exprcat|=oldcat|16; | |
556 break; | |
557 } else { | |
558 if(level>=7)EXITEVAL | |
559 oldcat=exprcat; | |
560 t=t>scanexpr(7); | |
561 exprcat|=oldcat|16; | |
562 break; | |
563 } | |
5 | 564 case '!':if(level>=6) { |
565 if (*srcptr=='=') { | |
566 srcptr++; | |
567 oldcat=exprcat; | |
568 t=t!=scanexpr(6); | |
569 exprcat|=oldcat|16; | |
570 } else { | |
571 oldcat=exprcat; | |
572 t|=scanexpr(6); | |
573 exprcat|=oldcat|16; | |
574 } | |
575 } | |
0 | 576 break; |
577 case '=':if(level>=6)EXITEVAL | |
578 if(*srcptr=='=')srcptr++; | |
579 oldcat=exprcat; | |
580 t=t==scanexpr(6); | |
581 exprcat|=oldcat|16; | |
582 break; | |
583 case '&':if(level>=5)EXITEVAL | |
584 oldcat=exprcat; | |
585 t&=scanexpr(5); | |
586 exprcat|=oldcat|16; | |
587 break; | |
588 case '^':if(level>=4)EXITEVAL | |
589 oldcat=exprcat; | |
590 t^=scanexpr(4); | |
591 exprcat|=oldcat|16; | |
592 break; | |
593 case '|':if(level>=3)EXITEVAL | |
594 oldcat=exprcat; | |
595 t|=scanexpr(3); | |
596 exprcat|=oldcat|16; | |
597 default: EXITEVAL | |
598 } | |
599 } | |
600 } | |
601 | |
602 char mode; /* addressing mode 0=immediate,1=direct,2=extended,3=postbyte | |
603 4=pcrelative(with postbyte) 5=indirect 6=pcrel&indirect*/ | |
604 char opsize; /*desired operand size 0=dunno,1=5,2=8,3=16*/ | |
605 short operand; | |
606 unsigned char postbyte; | |
607 | |
608 int dpsetting; | |
609 | |
610 | |
611 int scanindexreg() | |
612 { | |
613 char c; | |
614 c=*srcptr; | |
615 if(islower(c))c-=32; | |
616 if (debug) fprintf(stderr,"DEBUG: scanindexreg: indexreg=%d, mode=%d, opsize=%d, error=%d, postbyte=%02X\n",c,mode,opsize,error,postbyte); | |
617 switch(c) { | |
618 case 'X':return 1; | |
619 case 'Y':postbyte|=0x20;return 1; | |
620 case 'U':postbyte|=0x40;return 1; | |
621 case 'S':postbyte|=0x60;return 1; | |
622 default: return 0; | |
623 } | |
624 } | |
625 | |
626 void | |
627 set3() | |
628 { | |
629 if(mode<3)mode=3; | |
630 } | |
631 | |
632 void | |
633 scanspecial() | |
634 { | |
635 set3(); | |
636 skipspace(); | |
637 if(*srcptr=='-') { | |
638 srcptr++; | |
639 if(*srcptr=='-') { | |
640 srcptr++; | |
641 postbyte=0x83; | |
642 } else postbyte=0x82; | |
643 if(!scanindexreg())error|=2;else srcptr++; | |
644 } else { | |
645 postbyte=0x80; | |
646 if(!scanindexreg())error|=2;else srcptr++; | |
647 if(*srcptr=='+') { | |
648 srcptr++; | |
649 if(*srcptr=='+') { | |
650 srcptr++; | |
651 postbyte+=1; | |
652 } | |
653 } else postbyte+=4; | |
654 } | |
655 } | |
656 | |
657 void | |
658 scanindexed() | |
659 { | |
660 set3(); | |
661 postbyte=0; | |
662 if(scanindexreg()) { | |
663 srcptr++; | |
664 if(opsize==0) { | |
665 if(unknown||!certain)opsize=3; | |
666 else if(operand>=-16&&operand<16&&mode==3)opsize=1; | |
667 else if(operand>=-128&&operand<128)opsize=2; | |
668 else opsize=3; | |
669 } | |
670 switch(opsize) { | |
671 case 1:postbyte+=(operand&31);opsize=0;break; | |
672 case 2:postbyte+=0x88;break; | |
673 case 3:postbyte+=0x89;break; | |
674 } | |
675 } else { /*pc relative*/ | |
676 if(toupper(*srcptr)!='P')error|=2; | |
677 else { | |
678 srcptr++; | |
679 if(toupper(*srcptr)!='C')error|=2; | |
680 else { | |
681 srcptr++; | |
682 if(toupper(*srcptr)=='R')srcptr++; | |
683 } | |
684 } | |
685 mode++;postbyte+=0x8c; | |
686 if(opsize==1)opsize=2; | |
687 } | |
688 } | |
689 | |
690 #define RESTORE {srcptr=oldsrcptr;c=*srcptr;goto dodefault;} | |
691 | |
692 void | |
693 scanoperands() | |
694 { | |
695 char c,d,*oldsrcptr; | |
696 unknown=0; | |
697 opsize=0; | |
698 certain=1; | |
699 skipspace(); | |
700 c=*srcptr; | |
701 mode=0; | |
702 if(c=='[') { | |
703 srcptr++; | |
704 c=*srcptr; | |
705 mode=5; | |
706 } | |
707 if (debug) fprintf(stderr,"DEBUG: scanoperands: c=%c (%02X)\n",c,c); | |
708 switch(c) { | |
709 case 'D': case 'd': | |
710 oldsrcptr=srcptr; | |
711 srcptr++; | |
712 skipspace(); | |
713 if(*srcptr!=',')RESTORE else { | |
714 postbyte=0x8b; | |
715 srcptr++; | |
716 if(!scanindexreg())RESTORE else {srcptr++;set3();} | |
717 } | |
718 break; | |
719 case 'A': case 'a': | |
720 oldsrcptr=srcptr; | |
721 srcptr++; | |
722 skipspace(); | |
723 if(*srcptr!=',')RESTORE else { | |
724 postbyte=0x86; | |
725 srcptr++; | |
726 if(!scanindexreg())RESTORE else {srcptr++;set3();} | |
727 } | |
728 break; | |
729 case 'B': case 'b': | |
730 oldsrcptr=srcptr; | |
731 srcptr++; | |
732 skipspace(); | |
733 if(*srcptr!=',')RESTORE else { | |
734 postbyte=0x85; | |
735 srcptr++; | |
736 if (debug) fprintf(stderr,"DEBUG: scanoperands: breg preindex: c=%c (%02X)\n",*srcptr,*srcptr); | |
737 if(!scanindexreg())RESTORE else {srcptr++;set3();} | |
738 if (debug) fprintf(stderr,"DEBUG: scanoperands: breg: postindex c=%c (%02X)\n",*srcptr,*srcptr); | |
739 } | |
740 break; | |
741 case ',': | |
742 srcptr++; | |
743 scanspecial(); | |
744 break; | |
745 case '#': | |
746 if(mode==5)error|=2;else mode=0; | |
747 srcptr++; | |
5 | 748 if (*srcptr=='"') { |
749 operand = (srcptr[1]<<8) + srcptr[2] ; | |
750 srcptr += 3; | |
751 break; | |
752 } | |
0 | 753 operand=scanexpr(0); |
754 break; | |
755 case '<': | |
756 srcptr++; | |
757 if(*srcptr=='<') { | |
758 srcptr++; | |
759 opsize=1; | |
760 } else opsize=2; | |
761 goto dodefault; | |
762 case '>': | |
763 srcptr++; | |
764 opsize=3; | |
765 default: dodefault: | |
766 operand=scanexpr(0); | |
767 skipspace(); | |
768 if(*srcptr==',') { | |
769 srcptr++; | |
770 scanindexed(); | |
771 } else { | |
772 if(opsize==0) { | |
773 if(unknown||!certain||dpsetting==-1|| | |
774 (unsigned short)(operand-dpsetting*256)>=256) | |
775 opsize=3; else opsize=2; | |
776 } | |
777 if(opsize==1)opsize=2; | |
778 if(mode==5){ | |
779 postbyte=0x8f; | |
780 opsize=3; | |
781 } else mode=opsize-1; | |
782 } | |
783 } | |
784 if (debug) fprintf(stderr,"DEBUG: scanoperands: mode=%d, error=%d, postbyte=%02X\n",mode,error,postbyte); | |
785 if(mode>=5) { | |
786 skipspace(); | |
787 postbyte|=0x10; | |
788 if(*srcptr!=']')error|=2;else srcptr++; | |
789 } | |
790 if(pass==2&&unknown)error|=4; | |
791 } | |
792 | |
793 unsigned char codebuf[128]; | |
794 int codeptr; /* byte offset within instruction */ | |
795 int suppress; /* 0=no suppress 1=until ENDIF 2=until ELSE 3=until ENDM */ | |
796 int ifcount; /* count of nested IFs within suppressed text */ | |
797 | |
798 unsigned char outmode; /* 0 is binary, 1 is s-records */ | |
799 | |
800 unsigned short hexaddr; | |
801 int hexcount; | |
802 unsigned char hexbuffer[16]; | |
803 unsigned int chksum; | |
804 | |
805 extern int os9crc(unsigned char c, int crcp); | |
806 int crc; | |
807 | |
808 void | |
809 reset_crc() | |
810 { | |
811 crc = -1; | |
812 } | |
813 | |
814 | |
815 void | |
816 flushhex() | |
817 { | |
818 int i; | |
819 if(hexcount){ | |
820 fprintf(objfile,"S1%02X%04X",(hexcount+3)&0xff,hexaddr&0xffff); | |
821 for(i=0;i<hexcount;i++)fprintf(objfile,"%02X",hexbuffer[i]); | |
822 chksum+=(hexaddr&0xff)+((hexaddr>>8)&0xff)+hexcount+3; | |
823 fprintf(objfile,"%02X\n",0xff-(chksum&0xff)); | |
824 hexaddr+=hexcount; | |
825 hexcount=0; | |
826 chksum=0; | |
827 } | |
828 } | |
829 | |
830 void | |
831 outhex(unsigned char x) | |
832 { | |
833 if(hexcount==16)flushhex(); | |
834 hexbuffer[hexcount++]=x; | |
835 chksum+=x; | |
836 } | |
837 | |
838 void | |
839 outbuffer() | |
840 { | |
841 int i; | |
842 for(i=0;i<codeptr;i++) { | |
843 crc = os9crc(codebuf[i],crc); | |
844 if(!outmode)fputc(codebuf[i],objfile);else outhex(codebuf[i]); | |
845 } | |
846 } | |
847 | |
848 char *errormsg[]={"Error in expression", | |
849 "Illegal addressing mode", | |
850 "Undefined label", | |
851 "Multiple definitions of label", | |
852 "Relative branch out of range", | |
853 "Missing label", | |
854 "","","","","","","","","", | |
855 "Illegal mnemonic" | |
856 }; | |
857 | |
858 void | |
859 report() | |
860 { | |
861 int i; | |
862 fprintf(stderr,"File %s, line %d:%s\n",curname,lineno,srcline); | |
863 for(i=0;i<16;i++) { | |
864 if(error&1) { | |
865 fprintf(stderr,"%s\n",errormsg[i]); | |
866 if(pass==2&&listing)fprintf(listfile,"**** %s\n",errormsg[i]); | |
867 } | |
868 error>>=1; | |
869 } | |
5 | 870 error = 0; |
0 | 871 errors++; |
872 } | |
873 | |
874 void | |
875 outlist() | |
876 { | |
877 int i; | |
878 fprintf(listfile,"%04X: ",oldlc); | |
879 for(i=0;i<codeptr&&i<MAXLISTBYTES;i++) | |
880 fprintf(listfile,"%02X",codebuf[i]); | |
881 for(;i<=MAXLISTBYTES;i++) | |
882 fprintf(listfile," "); | |
883 fprintf(listfile,"%s\n",srcline); | |
884 while(i<codeptr) { | |
885 fprintf(listfile,"%04X: ",oldlc + i); | |
886 for(int j=0;i<codeptr&&j<MAXLISTBYTES;j++) { | |
887 fprintf(listfile,"%02X",codebuf[i]); i++; | |
888 } | |
889 fprintf(listfile,"\n"); | |
890 } | |
891 } | |
892 | |
893 void | |
894 setlabel(struct symrecord * lp) | |
895 { | |
896 if(lp) { | |
897 if(lp->cat!=13&&lp->cat!=6) { | |
898 if(lp->cat!=2||lp->value!=loccounter) | |
899 error|=8; | |
900 } else { | |
901 lp->cat=2; | |
902 lp->value=loccounter; | |
903 } | |
904 } | |
905 } | |
906 | |
907 void | |
908 putbyte(unsigned char b) | |
909 { | |
910 codebuf[codeptr++]=b; | |
911 } | |
912 | |
913 void | |
914 putword(unsigned short w) | |
915 { | |
916 codebuf[codeptr++]=w>>8; | |
917 codebuf[codeptr++]=w&0x0ff; | |
918 } | |
919 | |
920 void | |
921 doaddress() /* assemble the right addressing bytes for an instruction */ | |
922 { | |
923 int offs; | |
924 switch(mode) { | |
925 case 0: if(opsize==2)putbyte(operand);else putword(operand);break; | |
926 case 1: putbyte(operand);break; | |
927 case 2: putword(operand);break; | |
928 case 3: case 5: putbyte(postbyte); | |
929 switch(opsize) { | |
930 case 2: putbyte(operand);break; | |
931 case 3: putword(operand); | |
932 } | |
933 break; | |
934 case 4: case 6: offs=(unsigned short)operand-loccounter-codeptr-2; | |
935 if(offs<-128||offs>=128||opsize==3||unknown||!certain) { | |
12
111e5defb8ab
boot is called, rti failed
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
11
diff
changeset
|
936 if((!unknown)&&opsize==2&&(offs<-128||offs>=128) ) |
111e5defb8ab
boot is called, rti failed
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
11
diff
changeset
|
937 error|=16; |
111e5defb8ab
boot is called, rti failed
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
11
diff
changeset
|
938 offs--; |
111e5defb8ab
boot is called, rti failed
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
11
diff
changeset
|
939 opsize=3; |
111e5defb8ab
boot is called, rti failed
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
11
diff
changeset
|
940 postbyte++; |
0 | 941 } |
942 putbyte(postbyte); | |
943 if (debug) fprintf(stderr,"DEBUG: doaddress: mode=%d, opsize=%d, error=%d, postbyte=%02X, operand=%04X offs=%d\n",mode,opsize,error,postbyte,operand,offs); | |
944 if(opsize==3)putword(offs); | |
945 else putbyte(offs); | |
946 } | |
947 } | |
948 | |
949 void | |
950 onebyte(int co) | |
951 { | |
952 putbyte(co); | |
953 } | |
954 | |
955 void | |
956 twobyte(int co) | |
957 { | |
958 putword(co); | |
959 } | |
960 | |
961 void | |
962 oneimm(int co) | |
963 { | |
964 scanoperands(); | |
5 | 965 if(mode>=3) |
966 error|=2; | |
0 | 967 putbyte(co); |
968 putbyte(operand); | |
969 } | |
970 | |
971 void | |
972 lea(int co) | |
973 { | |
974 putbyte(co); | |
975 scanoperands(); | |
976 if(mode==0) error|=2; | |
977 if(mode<3) { | |
978 opsize=3; | |
979 postbyte=0x8f; | |
980 mode=3; | |
981 } | |
982 if (debug) fprintf(stderr,"DEBUG: lea: mode=%d, opsize=%d, error=%d, postbyte=%02X, *src=%c\n",mode,opsize,error,postbyte,*srcptr); | |
983 doaddress(); | |
984 } | |
985 | |
986 void | |
987 sbranch(int co) | |
988 { | |
989 int offs; | |
990 scanoperands(); | |
991 if(mode!=1&&mode!=2)error|=2; | |
992 offs=(unsigned short)operand-loccounter-2; | |
993 if(!unknown&&(offs<-128||offs>=128))error|=16; | |
994 if(pass==2&&unknown)error|=4; | |
995 putbyte(co); | |
996 putbyte(offs); | |
997 } | |
998 | |
999 void | |
1000 lbra(int co) | |
1001 { | |
1002 scanoperands(); | |
1003 if(mode!=1&&mode!=2)error|=2; | |
1004 putbyte(co); | |
1005 putword(operand-loccounter-3); | |
1006 } | |
1007 | |
1008 void | |
1009 lbranch(int co) | |
1010 { | |
1011 scanoperands(); | |
1012 if(mode!=1&&mode!=2)error|=2; | |
1013 putword(co); | |
1014 putword(operand-loccounter-4); | |
1015 } | |
1016 | |
1017 void | |
1018 arith(int co) | |
1019 { | |
1020 scanoperands(); | |
1021 switch(mode) { | |
1022 case 0:opsize=2;putbyte(co);break; | |
1023 case 1:putbyte(co+0x010);break; | |
1024 case 2:putbyte(co+0x030);break; | |
1025 default:putbyte(co+0x020); | |
1026 } | |
1027 doaddress(); | |
1028 } | |
1029 | |
1030 void | |
1031 darith(int co) | |
1032 { | |
1033 scanoperands(); | |
1034 switch(mode) { | |
1035 case 0:opsize=3;putbyte(co);break; | |
1036 case 1:putbyte(co+0x010);break; | |
1037 case 2:putbyte(co+0x030);break; | |
1038 default:putbyte(co+0x020); | |
1039 } | |
1040 doaddress(); | |
1041 } | |
1042 | |
1043 void | |
1044 d2arith(int co) | |
1045 { | |
1046 scanoperands(); | |
1047 switch(mode) { | |
1048 case 0:opsize=3;putword(co);break; | |
1049 case 1:putword(co+0x010);break; | |
1050 case 2:putword(co+0x030);break; | |
1051 default:putword(co+0x020); | |
1052 } | |
1053 doaddress(); | |
1054 } | |
1055 | |
1056 void | |
1057 oneaddr(int co) | |
1058 { | |
1059 scanoperands(); | |
1060 switch(mode) { | |
1061 case 0: error|=2;break; | |
1062 case 1: putbyte(co);break; | |
1063 case 2: putbyte(co+0x70);break; | |
1064 default: putbyte(co+0x60);break; | |
1065 } | |
1066 doaddress(); | |
1067 } | |
1068 | |
1069 void | |
1070 tfrexg(int co) | |
1071 { | |
1072 struct regrecord * p; | |
1073 putbyte(co); | |
1074 skipspace(); | |
1075 scanname(); | |
1076 if((p=findreg(namebuf))==0)error|=2; | |
1077 else postbyte=(p->tfr)<<4; | |
1078 skipspace(); | |
1079 if(*srcptr==',')srcptr++;else error|=2; | |
1080 skipspace(); | |
1081 scanname(); | |
1082 if((p=findreg(namebuf))==0)error|=2; | |
1083 else postbyte|=p->tfr; | |
1084 putbyte(postbyte); | |
1085 } | |
1086 | |
1087 void | |
1088 pshpul(int co) | |
1089 { | |
1090 struct regrecord *p; | |
1091 putbyte(co); | |
1092 postbyte=0; | |
1093 do { | |
1094 if(*srcptr==',')srcptr++; | |
1095 skipspace(); | |
1096 scanname(); | |
1097 if((p=findreg(namebuf))==0)error|=2; | |
1098 else postbyte|=p->psh; | |
1099 skipspace(); | |
1100 }while (*srcptr==','); | |
1101 putbyte(postbyte); | |
1102 } | |
1103 | |
1104 void | |
1105 skipComma() | |
1106 { | |
1107 while(*srcptr && *srcptr!='\n' && *srcptr!=',')srcptr++; | |
1108 if (*srcptr==',') { | |
1109 srcptr++; | |
1110 } else { | |
1111 error|=1; | |
1112 } | |
1113 } | |
1114 | |
1115 int modStart; | |
1116 | |
1117 void os9begin() | |
1118 { | |
6 | 1119 generate(); |
3 | 1120 os9=1; // contiguous code generation ( i.e. ignore org nor rmb ) |
0 | 1121 modStart = loccounter; |
1122 reset_crc(); | |
1123 putword(0x87cd); | |
1124 putword(scanexpr(0)-loccounter); // module size | |
1125 skipComma(); | |
1126 putword(scanexpr(0)-loccounter); // offset to module name | |
1127 skipComma(); | |
1128 putbyte(scanexpr(0)); // type / language | |
1129 skipComma(); | |
1130 putbyte(scanexpr(0)); // attribute | |
1131 int parity=0; | |
1132 for(int i=0; i< 8; i++) parity^=codebuf[i]; | |
1133 putbyte(parity^0xff); // header parity | |
1134 skipspace(); | |
1135 while (*srcptr==',') { // there are some more | |
1136 srcptr++; | |
1137 putword(scanexpr(0)); | |
1138 skipspace(); | |
1139 } | |
1140 } | |
1141 | |
1142 void os9end() | |
1143 { | |
1144 crc = crc ^ 0xffffff; | |
1145 | |
1146 putbyte((crc>>16)&0xff); | |
1147 putbyte((crc>>8)&0xff); | |
1148 putbyte(crc&0xff); | |
1149 } | |
1150 | |
1151 | |
1152 void | |
1153 pseudoop(int co,struct symrecord * lp) | |
1154 { | |
1155 int i; | |
1156 char c; | |
1157 char *fname; | |
1158 int locsave; | |
1159 | |
1160 switch(co) { | |
1161 case 0:/* RMB */ | |
3 | 1162 // in OS9 mode, this generates no data |
1163 // loccounter will be reset after any code to the current code generation | |
0 | 1164 setlabel(lp); |
1165 operand=scanexpr(0); | |
1166 if(unknown)error|=4; | |
1167 loccounter+=operand; | |
1168 if(generating&&pass==2) { | |
6 | 1169 if(!outmode && !os9 ) { |
1170 for(i=0;i<operand;i++) { fputc(0,objfile); } | |
1171 } else | |
1172 flushhex(); | |
0 | 1173 } |
1174 hexaddr=loccounter; | |
1175 break; | |
1176 case 5:/* EQU */ | |
1177 operand=scanexpr(0); | |
1178 if(!lp)error|=32; | |
1179 else { | |
1180 if(lp->cat==13||lp->cat==6|| | |
1181 (lp->value==(unsigned short)operand&&pass==2)) { | |
1182 if(exprcat==2)lp->cat=2; | |
1183 else lp->cat=0; | |
1184 lp->value=operand; | |
1185 } else error|=8; | |
1186 } | |
1187 break; | |
1188 case 7:/* FCB */ | |
6 | 1189 generate(); |
0 | 1190 setlabel(lp); |
1191 do { | |
1192 if(*srcptr==',')srcptr++; | |
1193 skipspace(); | |
1194 if(*srcptr=='\"') { | |
1195 srcptr++; | |
1196 while(*srcptr!='\"'&&*srcptr) | |
1197 putbyte(*srcptr++); | |
1198 if(*srcptr=='\"')srcptr++; | |
1199 } else { | |
1200 putbyte(scanexpr(0)); | |
1201 if(unknown&&pass==2)error|=4; | |
1202 } | |
1203 skipspace(); | |
1204 } while(*srcptr==','); | |
1205 break; | |
1206 case 8:/* FCC */ | |
6 | 1207 generate(); |
0 | 1208 setlabel(lp); |
1209 skipspace(); | |
1210 c=*srcptr++; | |
1211 while(*srcptr!=c&&*srcptr) | |
1212 putbyte(*srcptr++); | |
1213 if(*srcptr==c)srcptr++; | |
1214 break; | |
1215 case 9:/* FDB */ | |
6 | 1216 generate(); |
0 | 1217 setlabel(lp); |
1218 do { | |
1219 if(*srcptr==',')srcptr++; | |
1220 skipspace(); | |
1221 putword(scanexpr(0)); | |
1222 if(unknown&&pass==2)error|=4; | |
1223 skipspace(); | |
1224 } while(*srcptr==','); | |
1225 break; | |
1226 case 23 :/* FCS */ | |
6 | 1227 generate(); |
0 | 1228 setlabel(lp); |
1229 skipspace(); | |
1230 int sep = *srcptr; | |
1231 if(sep=='\"' || sep=='/') { | |
1232 srcptr++; | |
1233 while(*srcptr!=sep&&*srcptr) | |
1234 putbyte(*srcptr++); | |
1235 if(*srcptr==sep)srcptr++; | |
1236 codebuf[codeptr-1] |= 0x80; // os9 string termination | |
1237 } | |
1238 break; | |
1239 case 1: /* ELSE */ | |
1240 suppress=1; | |
1241 break; | |
1242 case 21: /* IFP1 */ | |
1243 if(pass==2)suppress=2; | |
1244 break; | |
1245 case 29: /* IFGT */ | |
1246 operand=scanexpr(0); | |
1247 if(unknown)error|=4; | |
1248 if(operand>0)suppress=2; | |
1249 break; | |
1250 case 31: /* IFLT */ | |
1251 operand=scanexpr(0); | |
1252 if(unknown)error|=4; | |
1253 if(operand<0)suppress=2; | |
1254 break; | |
3 | 1255 case 30: /* IFEQ */ |
0 | 1256 operand=scanexpr(0); |
1257 if(unknown)error|=4; | |
1258 if(operand==0)suppress=2; | |
1259 break; | |
3 | 1260 case 28: /* IFNE */ |
0 | 1261 case 10: /* IF */ |
1262 operand=scanexpr(0); | |
1263 if(unknown)error|=4; | |
1264 if(!operand)suppress=2; | |
1265 break; | |
1266 case 12: /* ORG */ | |
1267 operand=scanexpr(0); | |
1268 if(unknown)error|=4; | |
3 | 1269 if(generating&&pass==2&&!outmode&&!os9) { |
0 | 1270 for(i=0;i<(unsigned short)operand-loccounter;i++) |
3 | 1271 fputc(0,objfile); |
1272 } else flushhex(); | |
0 | 1273 loccounter=operand; |
1274 hexaddr=loccounter; | |
1275 break; | |
1276 case 14: /* SETDP */ | |
1277 operand=scanexpr(0); | |
1278 if(unknown)error|=4; | |
1279 if(!(operand&255))operand=(unsigned short)operand>>8; | |
1280 if((unsigned)operand>255)operand=-1; | |
1281 dpsetting=operand; | |
1282 break; | |
1283 case 15: /* SET */ | |
1284 operand=scanexpr(0); | |
1285 if(!lp)error|=32; | |
1286 else { | |
1287 if(lp->cat&1||lp->cat==6) { | |
1288 if(exprcat==2)lp->cat=3; | |
1289 else lp->cat=1; | |
1290 lp->value=operand; | |
1291 } else error|=8; | |
1292 } | |
1293 break; | |
1294 case 2: /* END */ | |
1295 terminate=1; | |
1296 break; | |
1297 case 27: /* USE */ | |
6 | 1298 // locsave = loccounter ; |
0 | 1299 case 16: /* INCLUDE */ |
1300 skipspace(); | |
1301 if(*srcptr=='"')srcptr++; | |
1302 i = 0; | |
1303 for(i=0; !(srcptr[i]==0||srcptr[i]=='"'); i++); | |
1304 int len = i; | |
1305 fname = calloc(1,len); | |
1306 for(i=0;i<len;i++) { | |
1307 if(*srcptr==0||*srcptr=='"')break; | |
1308 fname[i]=*srcptr++; | |
1309 } | |
1310 fname[i]=0; | |
1311 processfile(fname); | |
1312 codeptr=0; | |
1313 srcline[0]=0; | |
3 | 1314 // if (co==27) loccounter = locsave; |
0 | 1315 break; |
1316 case 24: /* MOD */ | |
6 | 1317 oldlc = loccounter = 0; |
0 | 1318 setlabel(lp); |
1319 os9begin(); | |
1320 break; | |
1321 case 25: /* EMOD */ | |
1322 os9end(); | |
1323 break; | |
1324 case 32: /* OS9 */ | |
6 | 1325 generate(); |
0 | 1326 setlabel(lp); |
3 | 1327 putword(0x103f); // SWI2 |
0 | 1328 putbyte(scanexpr(0)); |
15 | 1329 if(unknown&&pass==2)error|=4; |
0 | 1330 break; |
1331 case 18: /* TTL */ | |
1332 break; | |
1333 case 19: /* OPT */ | |
1334 case 26: /* NAM */ | |
1335 case 20: /* PAG */ | |
1336 case 3: /* ENDIF/ENDC */ | |
1337 break; | |
1338 } | |
1339 } | |
1340 | |
1341 | |
1342 void | |
1343 processline() | |
1344 { | |
1345 struct symrecord * lp; | |
1346 struct oprecord * op; | |
1347 int co; | |
1348 char c; | |
1349 srcptr=srcline; | |
1350 oldlc=loccounter; | |
5 | 1351 // error=0; |
0 | 1352 unknown=0;certain=1; |
1353 lp=0; | |
1354 codeptr=0; | |
1355 if(isalnum(*srcptr)) { | |
1356 scanname();lp=findsym(namebuf); | |
1357 if(*srcptr==':') srcptr++; | |
1358 } | |
1359 skipspace(); | |
1360 if(isalnum(*srcptr)) { | |
1361 scanname(); | |
1362 op=findop(namebuf); | |
1363 if(op) { | |
1364 if(op->cat!=13){ | |
6 | 1365 generate(); |
0 | 1366 setlabel(lp); |
1367 } | |
1368 co=op->code; | |
1369 switch(op->cat) { | |
1370 case 0:onebyte(co);break; | |
1371 case 1:twobyte(co);break; | |
1372 case 2:oneimm(co);break; | |
1373 case 3:lea(co);break; | |
1374 case 4:sbranch(co);break; | |
1375 case 5:lbranch(co);break; | |
1376 case 6:lbra(co);break; | |
1377 case 7:arith(co);break; | |
1378 case 8:darith(co);break; | |
1379 case 9:d2arith(co);break; | |
1380 case 10:oneaddr(co);break; | |
1381 case 11:tfrexg(co);break; | |
1382 case 12:pshpul(co);break; | |
1383 case 13:pseudoop(co,lp); | |
1384 } | |
1385 c=*srcptr; | |
1386 if (debug) fprintf(stderr,"DEBUG: processline: mode=%d, opsize=%d, error=%d, postbyte=%02X c=%c\n",mode,opsize,error,postbyte,c); | |
1387 if(c!=' '&&*(srcptr-1)!=' '&&c!=0&&c!=';')error|=2; | |
1388 } | |
1389 else error|=0x8000; | |
1390 }else setlabel(lp); | |
1391 if(pass==2) { | |
1392 outbuffer(); | |
1393 if(listing)outlist(); | |
1394 } | |
1395 if(error)report(); | |
1396 loccounter+=codeptr; | |
25 | 1397 if (os9 && prevloc==0 ) { |
1398 prevloc = loccounter+1; | |
1399 oldlc = loccounter = 0; | |
1400 } | |
0 | 1401 } |
1402 | |
1403 void | |
1404 suppressline() | |
1405 { | |
1406 struct oprecord * op; | |
1407 srcptr=srcline; | |
1408 oldlc=loccounter; | |
1409 codeptr=0; | |
1410 if(isalnum(*srcptr)) { | |
1411 scanname(); | |
1412 if(*srcptr==':')srcptr++; | |
1413 } | |
1414 skipspace(); | |
1415 scanname();op=findop(namebuf); | |
1416 if(op && op->cat==13) { | |
1417 if(op->code==10||op->code==13||op->code==29||op->code==28||op->code==21||op->code==30) ifcount++; | |
1418 else if(op->code==3) { | |
1419 if(ifcount>0)ifcount--;else if(suppress==1|suppress==2)suppress=0; | |
1420 } else if(op->code==1) { | |
1421 if(ifcount==0 && suppress==2)suppress=0; | |
1422 } | |
1423 } | |
6 | 1424 if(pass==2&&listing)outlist(); } |
0 | 1425 |
1426 void | |
1427 usage(char*nm) | |
1428 { | |
1429 fprintf(stderr,"Usage: %s [-o objname] [-l listname] [-s srecord-file] srcname\n",nm); | |
1430 exit(2); | |
1431 } | |
1432 | |
1433 char * | |
1434 strconcat(char *s,int spos,char *d) | |
1435 { | |
1436 int slen = strlen(s); | |
1437 int dlen = strlen(d); | |
1438 if ( spos == 0) spos = slen; | |
1439 char *out = calloc(1,spos+dlen+1); | |
1440 int i = 0; | |
1441 for(; i< spos; i++ ) out[i] = s[i]; | |
1442 for(; i< spos+dlen+1; i++ ) out[i] = *d++; | |
1443 return out; | |
1444 } | |
1445 | |
1446 | |
1447 void | |
1448 getoptions(int c,char*v[]) | |
1449 { | |
6 | 1450 int i=1; |
0 | 1451 if(c==1)usage(v[0]); |
6 | 1452 while(v[i]) { |
1453 if(strcmp(v[i],"-d")==0) { | |
1454 debug=1; | |
1455 i++; | |
1456 } else if(strcmp(v[i],"-o")==0) { | |
1457 objname = v[i+1]; | |
1458 i+=2; | |
1459 } else if(strcmp(v[i],"-s")==0) { | |
1460 objname=v[i+1]; | |
1461 outmode=1; | |
1462 i+=2; | |
1463 } else if(strcmp(v[i],"-l")==0) { | |
1464 listname=v[i+1]; | |
1465 i+=2; | |
1466 } else if(*v[i]=='-') { | |
1467 usage(v[0]); | |
1468 } else { | |
1469 if (srcname) usage(v[0]); | |
1470 srcname=v[i]; | |
1471 i++; | |
1472 } | |
0 | 1473 } |
1474 if(objname==0) { | |
1475 for(i=0;srcname[i]!='.' && srcname[i]!=0 ;i++) ; | |
1476 objname = strconcat(srcname,i,".b"); | |
1477 } | |
1478 listing=(listname!=0); | |
1479 } | |
1480 | |
1481 void | |
1482 expandline() | |
1483 { | |
1484 int i=0,j=0,k,j1; | |
1485 for(i=0;i<128&&j<128;i++) | |
1486 { | |
1487 if(inpline[i]=='\n') { | |
1488 srcline[j]=0;break; | |
1489 } | |
1490 if(inpline[i]=='\t') { | |
1491 j1=j; | |
1492 for(k=0;k<8-j1%8 && j<128;k++)srcline[j++]=' '; | |
1493 }else srcline[j++]=inpline[i]; | |
1494 } | |
1495 srcline[127]=0; | |
1496 } | |
1497 | |
1498 | |
1499 void | |
1500 processfile(char *name) | |
1501 { | |
1502 char *oldname; | |
1503 int oldno; | |
1504 FILE *srcfile; | |
1505 oldname=curname; | |
1506 curname=name; | |
1507 oldno=lineno; | |
1508 lineno=0; | |
1509 if((srcfile=fopen(name,"r"))==0) { | |
6 | 1510 int i = 0; |
1511 if (oldname) { | |
1512 i = strlen(oldname); | |
1513 while(i>0 && oldname[i]!='/') i--; | |
1514 } | |
0 | 1515 if (i>0) { |
1516 char *next = strconcat(oldname,i+1,name); | |
1517 if((srcfile=fopen(next,"r"))==0) { | |
1518 fprintf(stderr,"Cannot open source file %s\n",next); | |
1519 exit(4); | |
1520 } | |
1521 curname = next; | |
1522 } else { | |
1523 fprintf(stderr,"Cannot open source file %s\n",name); | |
1524 exit(4); | |
1525 } | |
1526 } | |
1527 while(!terminate&&fgets(inpline,128,srcfile)) { | |
1528 expandline(); | |
1529 lineno++; | |
1530 srcptr=srcline; | |
1531 if(suppress)suppressline(); else processline(); | |
1532 } | |
1533 fclose(srcfile); | |
1534 if(suppress) { | |
1535 fprintf(stderr,"improperly nested IF statements in %s",curname); | |
1536 errors++; | |
1537 suppress=0; | |
1538 } | |
1539 lineno=oldno; | |
1540 curname=oldname; | |
1541 } | |
1542 | |
1543 int | |
1544 main(int argc,char *argv[]) | |
1545 { | |
1546 char c; | |
1547 getoptions(argc,argv); | |
1548 pass=1; | |
1549 errors=0; | |
1550 generating=0; | |
1551 terminate=0; | |
1552 processfile(srcname); | |
1553 if(errors) { | |
1554 fprintf(stderr,"%d Pass 1 Errors, Continue?",errors); | |
1555 c=getchar(); | |
1556 if(c=='n'||c=='N') exit(3); | |
1557 } | |
1558 pass=2; | |
6 | 1559 prevloc = 0; |
0 | 1560 loccounter=0; |
1561 errors=0; | |
1562 generating=0; | |
1563 terminate=0; | |
1564 if(listing&&((listfile=fopen(listname,"w"))==0)) { | |
1565 fprintf(stderr,"Cannot open list file"); | |
1566 exit(4); | |
1567 } | |
1568 if((objfile=fopen(objname,outmode?"w":"wb"))==0) { | |
1569 fprintf(stderr,"Cannot write object file\n"); | |
1570 exit(4); | |
1571 } | |
1572 processfile(srcname); | |
1573 fprintf(stderr,"%d Pass 2 errors.\n",errors); | |
1574 if(listing) { | |
1575 fprintf(listfile,"%d Pass 2 errors.\n",errors); | |
1576 outsymtable(); | |
1577 fclose(listfile); | |
1578 } | |
1579 if(outmode){ | |
1580 flushhex(); | |
1581 fprintf(objfile,"S9030000FC\n"); | |
1582 } | |
1583 fclose(objfile); | |
1584 return 0; | |
1585 } | |
1586 |