Mercurial > hg > Members > kono > os9 > sbc09
annotate a09.c @ 26:51a35f0347f6
fix label
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 11 Jul 2018 01:00:18 +0900 |
parents | c4e3abe97e2c |
children | 3c14d647bb51 |
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; |
26 | 183 struct symrecord * prevlp = 0; |
0 | 184 |
8 | 185 /* expression categories... |
186 ECORD all zeros is ordinary constant. | |
187 ECADR bit 1 indicates address within module. | |
188 ECEXT bit 2 indicates external address. | |
189 ECLBL bit 3 public label | |
190 ECABS bit 4 indicates this can't be relocated if it's an address. | |
191 ECNEG bit 5 indicates address (if any) is negative. | |
192 */ | |
193 | |
194 | |
195 /* Symbol categories. exprcat ( symcat & 0xe ) | |
196 0 SCC Constant value (from equ). ECORD | |
197 1 SCV Variable value (from set) ECORD | |
198 2 SCC__ADR Address within program module (label). ECADR | |
199 3 SCV__ADR Variable containing address. ECADR | |
200 4 SC_E_ADR Adress in other program module (extern) ECEXT | |
201 5 SCVE_ADR Variable containing external address. ECEXT | |
202 6 SCU _ADR Unresolved address. ECEXT+ECADR | |
203 7 SCV_UADR Variable containing unresolved address. ECEXT+ECADR | |
204 8 SC___LBL Public label. ECLBL | |
205 9 SCMACRO Macro definition. xxx | |
206 10 SCU__LBL Public label (yet undefined). ECADR+ECLBL | |
207 11 SCPARAM parameter name. ECADR+ECLBL | |
208 12 SCLOCAL local label. ECEXT+ECLBL | |
209 13 SCEMPTY empty. xxx | |
0 | 210 */ |
211 | |
212 struct symrecord symtable[NLABELS]; | |
213 | |
214 void processfile(char *name); | |
215 | |
216 struct oprecord * findop(char * nm) | |
217 /* Find operation (mnemonic) in table using binary search */ | |
218 { | |
219 int lo,hi,i,s; | |
220 lo=0;hi=sizeof(optable)/sizeof(optable[0])-1; | |
221 do { | |
222 i=(lo+hi)/2; | |
223 s=strcmp(optable[i].name,nm); | |
224 if(s<0) lo=i+1; | |
225 else if(s>0) hi=i-1; | |
226 else break; | |
227 } while(hi>=lo); | |
228 if (s) return NULL; | |
229 return optable+i; | |
230 } | |
231 | |
232 struct symrecord * findsym(char * nm) | |
233 /* finds symbol table record; inserts if not found | |
234 uses binary search, maintains sorted table */ | |
235 { | |
236 int lo,hi,i,j,s; | |
237 lo=0;hi=symcounter-1; | |
238 s=1;i=0; | |
239 while (hi>=lo) { | |
240 i=(lo+hi)/2; | |
241 s=strcmp(symtable[i].name,nm); | |
242 if(s<0) lo=i+1; | |
243 else if(s>0) hi=i-1; | |
244 else break; | |
245 } | |
246 if(s) { | |
247 i=(s<0?i+1:i); | |
248 if(symcounter==NLABELS) { | |
249 fprintf(stderr,"Sorry, no storage for symbols!!!"); | |
250 exit(4); | |
251 } | |
252 for(j=symcounter;j>i;j--) symtable[j]=symtable[j-1]; | |
253 symcounter++; | |
254 strcpy(symtable[i].name,nm); | |
255 symtable[i].cat=13; | |
256 } | |
257 return symtable+i; | |
258 } | |
259 | |
260 FILE *listfile,*objfile; | |
261 char *listname,*objname,*srcname,*curname; | |
262 int lineno; | |
263 | |
264 void | |
265 outsymtable() | |
266 { | |
267 int i,j=0; | |
268 fprintf(listfile,"\nSYMBOL TABLE"); | |
269 for(i=0;i<symcounter;i++) | |
270 if(symtable[i].cat!=13) { | |
271 if(j%4==0)fprintf(listfile,"\n"); | |
272 fprintf(listfile,"%10s %02d %04x",symtable[i].name,symtable[i].cat, | |
273 symtable[i].value); | |
274 j++; | |
275 } | |
276 fprintf(listfile,"\n"); | |
277 } | |
278 | |
279 struct regrecord{char *name;unsigned char tfr,psh;}; | |
280 struct regrecord regtable[]= | |
281 {{"D",0x00,0x06},{"X",0x01,0x10},{"Y",0x02,0x20}, | |
282 {"U",0x03,0x40},{"S",0x04,0x40},{"PC",0x05,0x80}, | |
283 {"A",0x08,0x02},{"B",0x09,0x04},{"CC",0x0a,0x01}, | |
284 {"CCR",0x0a,0x01},{"DP",0x0b,0x08},{"DPR",0x0b,0x08}}; | |
285 | |
286 struct regrecord * findreg(char *nm) | |
287 { | |
288 int i; | |
289 for(i=0;i<12;i++) { | |
290 if(strcmp(regtable[i].name,nm)==0) return regtable+i; | |
291 } | |
292 return 0; | |
293 } | |
294 | |
295 | |
296 char pass; /* Assembler pass=1 or 2 */ | |
297 char listing; /* flag to indicate listing */ | |
298 char relocatable; /* flag to indicate relocatable object. */ | |
299 char terminate; /* flag to indicate termination. */ | |
300 char generating; /* flag to indicate that we generate code */ | |
301 unsigned short loccounter,oldlc; /* Location counter */ | |
302 | |
303 char inpline[128]; /* Current input line (not expanded)*/ | |
304 char srcline[128]; /* Current source line */ | |
305 char * srcptr; /* Pointer to line being parsed */ | |
306 | |
307 char unknown; /* flag to indicate value unknown */ | |
308 char certain; /* flag to indicate value is certain at pass 1*/ | |
309 int error; /* flags indicating errors in current line. */ | |
310 int errors; /* number of errors */ | |
311 char exprcat; /* category of expression being parsed, eg. | |
312 label or constant, this is important when | |
313 generating relocatable object code. */ | |
314 | |
6 | 315 void generate() |
316 { | |
317 generating = 1; | |
318 if (prevloc) { | |
7
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
319 oldlc = loccounter = prevloc-1 ; |
25 | 320 os9 = prevloc = 0; |
6 | 321 } |
322 } | |
323 | |
0 | 324 |
325 char namebuf[MAXIDLEN+1]; | |
326 | |
327 void | |
328 err(int er) { | |
329 error |= er ; | |
330 } | |
331 | |
332 void | |
333 scanname() | |
334 { | |
335 int i=0; | |
336 char c; | |
337 while(1) { | |
338 c=*srcptr++; | |
339 if(c>='a'&&c<='z')c-=32; | |
340 if(c!='.'&&c!='$'&&(c<'0'||c>'9')&&(c<'A'||c>'Z'))break; | |
341 if(i<MAXIDLEN)namebuf[i++]=c; | |
342 } | |
343 namebuf[i]=0; | |
344 srcptr--; | |
345 } | |
346 | |
347 void | |
348 skipspace() | |
349 { | |
350 char c; | |
351 do { | |
352 c=*srcptr++; | |
353 } while(c==' '||c=='\t'); | |
354 srcptr--; | |
355 } | |
356 | |
357 short scanexpr(int); | |
358 | |
359 short scandecimal() | |
360 { | |
361 char c; | |
362 short t=0; | |
363 c=*srcptr++; | |
364 while(isdigit(c)) { | |
365 t=t*10+c-'0'; | |
366 c=*srcptr++; | |
367 } | |
368 srcptr--; | |
369 return t; | |
370 } | |
371 | |
372 short scanhex() | |
373 { | |
374 short t=0,i=0; | |
375 srcptr++; | |
376 scanname(); | |
377 while(namebuf[i]>='0'&&namebuf[i]<='F') { | |
378 t=t*16+namebuf[i]-'0'; | |
379 if(namebuf[i]>'9')t-=7; | |
380 i++; | |
381 } | |
382 if(i==0)error|=1; | |
383 return t; | |
384 } | |
385 | |
386 short scanchar() | |
387 { | |
388 short t; | |
389 srcptr++; | |
390 t=*srcptr; | |
391 if(t)srcptr++; | |
392 if (*srcptr=='\'')srcptr++; | |
393 return t; | |
394 } | |
395 | |
396 short scanbin() | |
397 { | |
398 char c; | |
399 short t=0; | |
400 srcptr++; | |
401 c=*srcptr++; | |
402 while(c=='0'||c=='1') { | |
403 t=t*2+c-'0'; | |
404 c=*srcptr++; | |
405 } | |
406 srcptr--; | |
407 return t; | |
408 } | |
409 | |
410 short scanoct() | |
411 { | |
412 char c; | |
413 short t=0; | |
414 srcptr++; | |
415 c=*srcptr++; | |
416 while(c>='0'&&c<='7') { | |
417 t=t*8+c-'0'; | |
418 c=*srcptr++; | |
419 } | |
420 srcptr--; | |
421 return t; | |
422 } | |
423 | |
424 | |
425 short scanlabel() | |
426 { | |
427 struct symrecord * p; | |
428 scanname(); | |
429 p=findsym(namebuf); | |
430 if(p->cat==13) { | |
431 p->cat=6; | |
432 p->value=0; | |
433 } | |
434 if(p->cat==9||p->cat==11)error|=1; | |
435 exprcat=p->cat&14; | |
436 if(exprcat==6||exprcat==10)unknown=1; | |
437 if(((exprcat==2||exprcat==8) | |
438 && (unsigned short)(p->value)>(unsigned short)loccounter)|| | |
439 exprcat==4) | |
440 certain=0; | |
441 if(exprcat==8||exprcat==6||exprcat==10)exprcat=2; | |
442 return p->value; | |
443 } | |
444 | |
445 | |
446 short scanfactor() | |
447 { | |
448 char c; | |
449 short t; | |
450 skipspace(); | |
451 c=*srcptr; | |
452 if(isalpha(c))return scanlabel(); | |
453 else if(isdigit(c))return scandecimal(); | |
454 else switch(c) { | |
11 | 455 case '*' : if(prevloc) { srcptr++;exprcat|=2;return prevloc-1; } |
456 case '.' : srcptr++;exprcat|=2;return loccounter; | |
0 | 457 case '$' : return scanhex(); |
458 case '%' : return scanbin(); | |
459 case '&' : /* compatibility */ | |
460 case '@' : return scanoct(); | |
461 case '\'' : return scanchar(); | |
462 case '(' : srcptr++;t=scanexpr(0);skipspace(); | |
463 if(*srcptr==')')srcptr++;else error|=1; | |
464 return t; | |
465 case '-' : srcptr++;exprcat^=32;return -scanfactor(); | |
466 case '+' : srcptr++;return scanfactor(); | |
467 case '!' : srcptr++;exprcat|=16;return !scanfactor(); | |
468 case '^' : | |
469 case '~' : srcptr++;exprcat|=16;return ~scanfactor(); | |
470 } | |
471 error|=1; | |
472 return 0; | |
473 } | |
474 | |
475 #define EXITEVAL {srcptr--;return t;} | |
476 | |
477 #define RESOLVECAT if((oldcat&15)==0)oldcat=0;\ | |
478 if((exprcat&15)==0)exprcat=0;\ | |
479 if((exprcat==2&&oldcat==34)||(exprcat==34&&oldcat==2)) {\ | |
480 exprcat=0;\ | |
481 oldcat=0;}\ | |
482 exprcat|=oldcat;\ | |
483 /* resolve such cases as constant added to address or difference between | |
484 two addresses in same module */ | |
485 | |
486 | |
487 short scanexpr(int level) /* This is what you call _recursive_ descent!!!*/ | |
488 { | |
489 short t,u; | |
490 char oldcat,c; | |
491 exprcat=0; | |
492 if(level==10)return scanfactor(); | |
493 t=scanexpr(level+1); | |
494 while(1) { | |
5 | 495 // skipspace(); |
0 | 496 c=*srcptr++; |
497 switch(c) { | |
498 case '*':oldcat=exprcat; | |
499 t*=scanexpr(10); | |
500 exprcat|=oldcat|16; | |
501 break; | |
502 case '/':oldcat=exprcat; | |
503 u=scanexpr(10); | |
504 if(u)t/=u;else error|=1; | |
505 exprcat|=oldcat|16; | |
506 break; | |
507 case '%':oldcat=exprcat; | |
508 u=scanexpr(10); | |
509 if(u)t%=u;else error|=1; | |
510 exprcat|=oldcat|16; | |
511 break; | |
512 case '+':if(level==9)EXITEVAL | |
513 oldcat=exprcat; | |
514 t+=scanexpr(9); | |
515 RESOLVECAT | |
516 break; | |
517 case '-':if(level==9)EXITEVAL | |
518 oldcat=exprcat; | |
519 t-=scanexpr(9); | |
520 exprcat^=32; | |
521 RESOLVECAT | |
522 break; | |
523 case '<':if(*(srcptr)=='<') { | |
524 if(level>=8)EXITEVAL | |
525 srcptr++; | |
526 oldcat=exprcat; | |
527 t<<=scanexpr(8); | |
528 exprcat|=oldcat|16; | |
529 break; | |
530 } else if(*(srcptr)=='=') { | |
531 if(level>=7)EXITEVAL | |
532 srcptr++; | |
533 oldcat=exprcat; | |
534 t=t<=scanexpr(7); | |
535 exprcat|=oldcat|16; | |
536 break; | |
537 } else { | |
538 if(level>=7)EXITEVAL | |
539 oldcat=exprcat; | |
540 t=t<scanexpr(7); | |
541 exprcat|=oldcat|16; | |
542 break; | |
543 } | |
544 case '>':if(*(srcptr)=='>') { | |
545 if(level>=8)EXITEVAL | |
546 srcptr++; | |
547 oldcat=exprcat; | |
548 t>>=scanexpr(8); | |
549 exprcat|=oldcat|16; | |
550 break; | |
551 } else if(*(srcptr)=='=') { | |
552 if(level>=7)EXITEVAL | |
553 srcptr++; | |
554 oldcat=exprcat; | |
555 t=t>=scanexpr(7); | |
556 exprcat|=oldcat|16; | |
557 break; | |
558 } else { | |
559 if(level>=7)EXITEVAL | |
560 oldcat=exprcat; | |
561 t=t>scanexpr(7); | |
562 exprcat|=oldcat|16; | |
563 break; | |
564 } | |
5 | 565 case '!':if(level>=6) { |
566 if (*srcptr=='=') { | |
567 srcptr++; | |
568 oldcat=exprcat; | |
569 t=t!=scanexpr(6); | |
570 exprcat|=oldcat|16; | |
571 } else { | |
572 oldcat=exprcat; | |
573 t|=scanexpr(6); | |
574 exprcat|=oldcat|16; | |
575 } | |
576 } | |
0 | 577 break; |
578 case '=':if(level>=6)EXITEVAL | |
579 if(*srcptr=='=')srcptr++; | |
580 oldcat=exprcat; | |
581 t=t==scanexpr(6); | |
582 exprcat|=oldcat|16; | |
583 break; | |
584 case '&':if(level>=5)EXITEVAL | |
585 oldcat=exprcat; | |
586 t&=scanexpr(5); | |
587 exprcat|=oldcat|16; | |
588 break; | |
589 case '^':if(level>=4)EXITEVAL | |
590 oldcat=exprcat; | |
591 t^=scanexpr(4); | |
592 exprcat|=oldcat|16; | |
593 break; | |
594 case '|':if(level>=3)EXITEVAL | |
595 oldcat=exprcat; | |
596 t|=scanexpr(3); | |
597 exprcat|=oldcat|16; | |
598 default: EXITEVAL | |
599 } | |
600 } | |
601 } | |
602 | |
603 char mode; /* addressing mode 0=immediate,1=direct,2=extended,3=postbyte | |
604 4=pcrelative(with postbyte) 5=indirect 6=pcrel&indirect*/ | |
605 char opsize; /*desired operand size 0=dunno,1=5,2=8,3=16*/ | |
606 short operand; | |
607 unsigned char postbyte; | |
608 | |
609 int dpsetting; | |
610 | |
611 | |
612 int scanindexreg() | |
613 { | |
614 char c; | |
615 c=*srcptr; | |
616 if(islower(c))c-=32; | |
617 if (debug) fprintf(stderr,"DEBUG: scanindexreg: indexreg=%d, mode=%d, opsize=%d, error=%d, postbyte=%02X\n",c,mode,opsize,error,postbyte); | |
618 switch(c) { | |
619 case 'X':return 1; | |
620 case 'Y':postbyte|=0x20;return 1; | |
621 case 'U':postbyte|=0x40;return 1; | |
622 case 'S':postbyte|=0x60;return 1; | |
623 default: return 0; | |
624 } | |
625 } | |
626 | |
627 void | |
628 set3() | |
629 { | |
630 if(mode<3)mode=3; | |
631 } | |
632 | |
633 void | |
634 scanspecial() | |
635 { | |
636 set3(); | |
637 skipspace(); | |
638 if(*srcptr=='-') { | |
639 srcptr++; | |
640 if(*srcptr=='-') { | |
641 srcptr++; | |
642 postbyte=0x83; | |
643 } else postbyte=0x82; | |
644 if(!scanindexreg())error|=2;else srcptr++; | |
645 } else { | |
646 postbyte=0x80; | |
647 if(!scanindexreg())error|=2;else srcptr++; | |
648 if(*srcptr=='+') { | |
649 srcptr++; | |
650 if(*srcptr=='+') { | |
651 srcptr++; | |
652 postbyte+=1; | |
653 } | |
654 } else postbyte+=4; | |
655 } | |
656 } | |
657 | |
658 void | |
659 scanindexed() | |
660 { | |
661 set3(); | |
662 postbyte=0; | |
663 if(scanindexreg()) { | |
664 srcptr++; | |
665 if(opsize==0) { | |
666 if(unknown||!certain)opsize=3; | |
667 else if(operand>=-16&&operand<16&&mode==3)opsize=1; | |
668 else if(operand>=-128&&operand<128)opsize=2; | |
669 else opsize=3; | |
670 } | |
671 switch(opsize) { | |
672 case 1:postbyte+=(operand&31);opsize=0;break; | |
673 case 2:postbyte+=0x88;break; | |
674 case 3:postbyte+=0x89;break; | |
675 } | |
676 } else { /*pc relative*/ | |
677 if(toupper(*srcptr)!='P')error|=2; | |
678 else { | |
679 srcptr++; | |
680 if(toupper(*srcptr)!='C')error|=2; | |
681 else { | |
682 srcptr++; | |
683 if(toupper(*srcptr)=='R')srcptr++; | |
684 } | |
685 } | |
686 mode++;postbyte+=0x8c; | |
687 if(opsize==1)opsize=2; | |
688 } | |
689 } | |
690 | |
691 #define RESTORE {srcptr=oldsrcptr;c=*srcptr;goto dodefault;} | |
692 | |
693 void | |
694 scanoperands() | |
695 { | |
696 char c,d,*oldsrcptr; | |
697 unknown=0; | |
698 opsize=0; | |
699 certain=1; | |
700 skipspace(); | |
701 c=*srcptr; | |
702 mode=0; | |
703 if(c=='[') { | |
704 srcptr++; | |
705 c=*srcptr; | |
706 mode=5; | |
707 } | |
708 if (debug) fprintf(stderr,"DEBUG: scanoperands: c=%c (%02X)\n",c,c); | |
709 switch(c) { | |
710 case 'D': case 'd': | |
711 oldsrcptr=srcptr; | |
712 srcptr++; | |
713 skipspace(); | |
714 if(*srcptr!=',')RESTORE else { | |
715 postbyte=0x8b; | |
716 srcptr++; | |
717 if(!scanindexreg())RESTORE else {srcptr++;set3();} | |
718 } | |
719 break; | |
720 case 'A': case 'a': | |
721 oldsrcptr=srcptr; | |
722 srcptr++; | |
723 skipspace(); | |
724 if(*srcptr!=',')RESTORE else { | |
725 postbyte=0x86; | |
726 srcptr++; | |
727 if(!scanindexreg())RESTORE else {srcptr++;set3();} | |
728 } | |
729 break; | |
730 case 'B': case 'b': | |
731 oldsrcptr=srcptr; | |
732 srcptr++; | |
733 skipspace(); | |
734 if(*srcptr!=',')RESTORE else { | |
735 postbyte=0x85; | |
736 srcptr++; | |
737 if (debug) fprintf(stderr,"DEBUG: scanoperands: breg preindex: c=%c (%02X)\n",*srcptr,*srcptr); | |
738 if(!scanindexreg())RESTORE else {srcptr++;set3();} | |
739 if (debug) fprintf(stderr,"DEBUG: scanoperands: breg: postindex c=%c (%02X)\n",*srcptr,*srcptr); | |
740 } | |
741 break; | |
742 case ',': | |
743 srcptr++; | |
744 scanspecial(); | |
745 break; | |
746 case '#': | |
747 if(mode==5)error|=2;else mode=0; | |
748 srcptr++; | |
5 | 749 if (*srcptr=='"') { |
750 operand = (srcptr[1]<<8) + srcptr[2] ; | |
751 srcptr += 3; | |
752 break; | |
753 } | |
0 | 754 operand=scanexpr(0); |
755 break; | |
756 case '<': | |
757 srcptr++; | |
758 if(*srcptr=='<') { | |
759 srcptr++; | |
760 opsize=1; | |
761 } else opsize=2; | |
762 goto dodefault; | |
763 case '>': | |
764 srcptr++; | |
765 opsize=3; | |
766 default: dodefault: | |
767 operand=scanexpr(0); | |
768 skipspace(); | |
769 if(*srcptr==',') { | |
770 srcptr++; | |
771 scanindexed(); | |
772 } else { | |
773 if(opsize==0) { | |
774 if(unknown||!certain||dpsetting==-1|| | |
775 (unsigned short)(operand-dpsetting*256)>=256) | |
776 opsize=3; else opsize=2; | |
777 } | |
778 if(opsize==1)opsize=2; | |
779 if(mode==5){ | |
780 postbyte=0x8f; | |
781 opsize=3; | |
782 } else mode=opsize-1; | |
783 } | |
784 } | |
785 if (debug) fprintf(stderr,"DEBUG: scanoperands: mode=%d, error=%d, postbyte=%02X\n",mode,error,postbyte); | |
786 if(mode>=5) { | |
787 skipspace(); | |
788 postbyte|=0x10; | |
789 if(*srcptr!=']')error|=2;else srcptr++; | |
790 } | |
791 if(pass==2&&unknown)error|=4; | |
792 } | |
793 | |
794 unsigned char codebuf[128]; | |
795 int codeptr; /* byte offset within instruction */ | |
796 int suppress; /* 0=no suppress 1=until ENDIF 2=until ELSE 3=until ENDM */ | |
797 int ifcount; /* count of nested IFs within suppressed text */ | |
798 | |
799 unsigned char outmode; /* 0 is binary, 1 is s-records */ | |
800 | |
801 unsigned short hexaddr; | |
802 int hexcount; | |
803 unsigned char hexbuffer[16]; | |
804 unsigned int chksum; | |
805 | |
806 extern int os9crc(unsigned char c, int crcp); | |
807 int crc; | |
808 | |
809 void | |
810 reset_crc() | |
811 { | |
812 crc = -1; | |
813 } | |
814 | |
815 | |
816 void | |
817 flushhex() | |
818 { | |
819 int i; | |
820 if(hexcount){ | |
821 fprintf(objfile,"S1%02X%04X",(hexcount+3)&0xff,hexaddr&0xffff); | |
822 for(i=0;i<hexcount;i++)fprintf(objfile,"%02X",hexbuffer[i]); | |
823 chksum+=(hexaddr&0xff)+((hexaddr>>8)&0xff)+hexcount+3; | |
824 fprintf(objfile,"%02X\n",0xff-(chksum&0xff)); | |
825 hexaddr+=hexcount; | |
826 hexcount=0; | |
827 chksum=0; | |
828 } | |
829 } | |
830 | |
831 void | |
832 outhex(unsigned char x) | |
833 { | |
834 if(hexcount==16)flushhex(); | |
835 hexbuffer[hexcount++]=x; | |
836 chksum+=x; | |
837 } | |
838 | |
839 void | |
840 outbuffer() | |
841 { | |
842 int i; | |
843 for(i=0;i<codeptr;i++) { | |
844 crc = os9crc(codebuf[i],crc); | |
845 if(!outmode)fputc(codebuf[i],objfile);else outhex(codebuf[i]); | |
846 } | |
847 } | |
848 | |
849 char *errormsg[]={"Error in expression", | |
850 "Illegal addressing mode", | |
851 "Undefined label", | |
852 "Multiple definitions of label", | |
853 "Relative branch out of range", | |
854 "Missing label", | |
855 "","","","","","","","","", | |
856 "Illegal mnemonic" | |
857 }; | |
858 | |
859 void | |
860 report() | |
861 { | |
862 int i; | |
863 fprintf(stderr,"File %s, line %d:%s\n",curname,lineno,srcline); | |
864 for(i=0;i<16;i++) { | |
865 if(error&1) { | |
866 fprintf(stderr,"%s\n",errormsg[i]); | |
867 if(pass==2&&listing)fprintf(listfile,"**** %s\n",errormsg[i]); | |
868 } | |
869 error>>=1; | |
870 } | |
5 | 871 error = 0; |
0 | 872 errors++; |
873 } | |
874 | |
875 void | |
876 outlist() | |
877 { | |
878 int i; | |
879 fprintf(listfile,"%04X: ",oldlc); | |
880 for(i=0;i<codeptr&&i<MAXLISTBYTES;i++) | |
881 fprintf(listfile,"%02X",codebuf[i]); | |
882 for(;i<=MAXLISTBYTES;i++) | |
883 fprintf(listfile," "); | |
884 fprintf(listfile,"%s\n",srcline); | |
885 while(i<codeptr) { | |
886 fprintf(listfile,"%04X: ",oldlc + i); | |
887 for(int j=0;i<codeptr&&j<MAXLISTBYTES;j++) { | |
888 fprintf(listfile,"%02X",codebuf[i]); i++; | |
889 } | |
890 fprintf(listfile,"\n"); | |
891 } | |
892 } | |
893 | |
894 void | |
895 setlabel(struct symrecord * lp) | |
896 { | |
26 | 897 if (prevlp) { |
898 struct symrecord *l = prevlp; | |
899 prevlp = 0; | |
900 setlabel(l); | |
901 } | |
0 | 902 if(lp) { |
903 if(lp->cat!=13&&lp->cat!=6) { | |
904 if(lp->cat!=2||lp->value!=loccounter) | |
905 error|=8; | |
906 } else { | |
907 lp->cat=2; | |
908 lp->value=loccounter; | |
909 } | |
910 } | |
911 } | |
912 | |
913 void | |
914 putbyte(unsigned char b) | |
915 { | |
916 codebuf[codeptr++]=b; | |
917 } | |
918 | |
919 void | |
920 putword(unsigned short w) | |
921 { | |
922 codebuf[codeptr++]=w>>8; | |
923 codebuf[codeptr++]=w&0x0ff; | |
924 } | |
925 | |
926 void | |
927 doaddress() /* assemble the right addressing bytes for an instruction */ | |
928 { | |
929 int offs; | |
930 switch(mode) { | |
931 case 0: if(opsize==2)putbyte(operand);else putword(operand);break; | |
932 case 1: putbyte(operand);break; | |
933 case 2: putword(operand);break; | |
934 case 3: case 5: putbyte(postbyte); | |
935 switch(opsize) { | |
936 case 2: putbyte(operand);break; | |
937 case 3: putword(operand); | |
938 } | |
939 break; | |
940 case 4: case 6: offs=(unsigned short)operand-loccounter-codeptr-2; | |
941 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
|
942 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
|
943 error|=16; |
111e5defb8ab
boot is called, rti failed
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
11
diff
changeset
|
944 offs--; |
111e5defb8ab
boot is called, rti failed
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
11
diff
changeset
|
945 opsize=3; |
111e5defb8ab
boot is called, rti failed
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
11
diff
changeset
|
946 postbyte++; |
0 | 947 } |
948 putbyte(postbyte); | |
949 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); | |
950 if(opsize==3)putword(offs); | |
951 else putbyte(offs); | |
952 } | |
953 } | |
954 | |
955 void | |
956 onebyte(int co) | |
957 { | |
958 putbyte(co); | |
959 } | |
960 | |
961 void | |
962 twobyte(int co) | |
963 { | |
964 putword(co); | |
965 } | |
966 | |
967 void | |
968 oneimm(int co) | |
969 { | |
970 scanoperands(); | |
5 | 971 if(mode>=3) |
972 error|=2; | |
0 | 973 putbyte(co); |
974 putbyte(operand); | |
975 } | |
976 | |
977 void | |
978 lea(int co) | |
979 { | |
980 putbyte(co); | |
981 scanoperands(); | |
982 if(mode==0) error|=2; | |
983 if(mode<3) { | |
984 opsize=3; | |
985 postbyte=0x8f; | |
986 mode=3; | |
987 } | |
988 if (debug) fprintf(stderr,"DEBUG: lea: mode=%d, opsize=%d, error=%d, postbyte=%02X, *src=%c\n",mode,opsize,error,postbyte,*srcptr); | |
989 doaddress(); | |
990 } | |
991 | |
992 void | |
993 sbranch(int co) | |
994 { | |
995 int offs; | |
996 scanoperands(); | |
997 if(mode!=1&&mode!=2)error|=2; | |
998 offs=(unsigned short)operand-loccounter-2; | |
999 if(!unknown&&(offs<-128||offs>=128))error|=16; | |
1000 if(pass==2&&unknown)error|=4; | |
1001 putbyte(co); | |
1002 putbyte(offs); | |
1003 } | |
1004 | |
1005 void | |
1006 lbra(int co) | |
1007 { | |
1008 scanoperands(); | |
1009 if(mode!=1&&mode!=2)error|=2; | |
1010 putbyte(co); | |
1011 putword(operand-loccounter-3); | |
1012 } | |
1013 | |
1014 void | |
1015 lbranch(int co) | |
1016 { | |
1017 scanoperands(); | |
1018 if(mode!=1&&mode!=2)error|=2; | |
1019 putword(co); | |
1020 putword(operand-loccounter-4); | |
1021 } | |
1022 | |
1023 void | |
1024 arith(int co) | |
1025 { | |
1026 scanoperands(); | |
1027 switch(mode) { | |
1028 case 0:opsize=2;putbyte(co);break; | |
1029 case 1:putbyte(co+0x010);break; | |
1030 case 2:putbyte(co+0x030);break; | |
1031 default:putbyte(co+0x020); | |
1032 } | |
1033 doaddress(); | |
1034 } | |
1035 | |
1036 void | |
1037 darith(int co) | |
1038 { | |
1039 scanoperands(); | |
1040 switch(mode) { | |
1041 case 0:opsize=3;putbyte(co);break; | |
1042 case 1:putbyte(co+0x010);break; | |
1043 case 2:putbyte(co+0x030);break; | |
1044 default:putbyte(co+0x020); | |
1045 } | |
1046 doaddress(); | |
1047 } | |
1048 | |
1049 void | |
1050 d2arith(int co) | |
1051 { | |
1052 scanoperands(); | |
1053 switch(mode) { | |
1054 case 0:opsize=3;putword(co);break; | |
1055 case 1:putword(co+0x010);break; | |
1056 case 2:putword(co+0x030);break; | |
1057 default:putword(co+0x020); | |
1058 } | |
1059 doaddress(); | |
1060 } | |
1061 | |
1062 void | |
1063 oneaddr(int co) | |
1064 { | |
1065 scanoperands(); | |
1066 switch(mode) { | |
1067 case 0: error|=2;break; | |
1068 case 1: putbyte(co);break; | |
1069 case 2: putbyte(co+0x70);break; | |
1070 default: putbyte(co+0x60);break; | |
1071 } | |
1072 doaddress(); | |
1073 } | |
1074 | |
1075 void | |
1076 tfrexg(int co) | |
1077 { | |
1078 struct regrecord * p; | |
1079 putbyte(co); | |
1080 skipspace(); | |
1081 scanname(); | |
1082 if((p=findreg(namebuf))==0)error|=2; | |
1083 else postbyte=(p->tfr)<<4; | |
1084 skipspace(); | |
1085 if(*srcptr==',')srcptr++;else error|=2; | |
1086 skipspace(); | |
1087 scanname(); | |
1088 if((p=findreg(namebuf))==0)error|=2; | |
1089 else postbyte|=p->tfr; | |
1090 putbyte(postbyte); | |
1091 } | |
1092 | |
1093 void | |
1094 pshpul(int co) | |
1095 { | |
1096 struct regrecord *p; | |
1097 putbyte(co); | |
1098 postbyte=0; | |
1099 do { | |
1100 if(*srcptr==',')srcptr++; | |
1101 skipspace(); | |
1102 scanname(); | |
1103 if((p=findreg(namebuf))==0)error|=2; | |
1104 else postbyte|=p->psh; | |
1105 skipspace(); | |
1106 }while (*srcptr==','); | |
1107 putbyte(postbyte); | |
1108 } | |
1109 | |
1110 void | |
1111 skipComma() | |
1112 { | |
1113 while(*srcptr && *srcptr!='\n' && *srcptr!=',')srcptr++; | |
1114 if (*srcptr==',') { | |
1115 srcptr++; | |
1116 } else { | |
1117 error|=1; | |
1118 } | |
1119 } | |
1120 | |
1121 int modStart; | |
1122 | |
1123 void os9begin() | |
1124 { | |
6 | 1125 generate(); |
3 | 1126 os9=1; // contiguous code generation ( i.e. ignore org nor rmb ) |
0 | 1127 modStart = loccounter; |
1128 reset_crc(); | |
1129 putword(0x87cd); | |
1130 putword(scanexpr(0)-loccounter); // module size | |
1131 skipComma(); | |
1132 putword(scanexpr(0)-loccounter); // offset to module name | |
1133 skipComma(); | |
1134 putbyte(scanexpr(0)); // type / language | |
1135 skipComma(); | |
1136 putbyte(scanexpr(0)); // attribute | |
1137 int parity=0; | |
1138 for(int i=0; i< 8; i++) parity^=codebuf[i]; | |
1139 putbyte(parity^0xff); // header parity | |
1140 skipspace(); | |
1141 while (*srcptr==',') { // there are some more | |
1142 srcptr++; | |
1143 putword(scanexpr(0)); | |
1144 skipspace(); | |
1145 } | |
1146 } | |
1147 | |
1148 void os9end() | |
1149 { | |
1150 crc = crc ^ 0xffffff; | |
1151 | |
1152 putbyte((crc>>16)&0xff); | |
1153 putbyte((crc>>8)&0xff); | |
1154 putbyte(crc&0xff); | |
1155 } | |
1156 | |
1157 | |
1158 void | |
1159 pseudoop(int co,struct symrecord * lp) | |
1160 { | |
1161 int i; | |
1162 char c; | |
1163 char *fname; | |
1164 int locsave; | |
1165 | |
1166 switch(co) { | |
1167 case 0:/* RMB */ | |
3 | 1168 // in OS9 mode, this generates no data |
1169 // loccounter will be reset after any code to the current code generation | |
0 | 1170 setlabel(lp); |
1171 operand=scanexpr(0); | |
1172 if(unknown)error|=4; | |
1173 loccounter+=operand; | |
1174 if(generating&&pass==2) { | |
6 | 1175 if(!outmode && !os9 ) { |
1176 for(i=0;i<operand;i++) { fputc(0,objfile); } | |
1177 } else | |
1178 flushhex(); | |
0 | 1179 } |
1180 hexaddr=loccounter; | |
1181 break; | |
1182 case 5:/* EQU */ | |
1183 operand=scanexpr(0); | |
1184 if(!lp)error|=32; | |
1185 else { | |
1186 if(lp->cat==13||lp->cat==6|| | |
1187 (lp->value==(unsigned short)operand&&pass==2)) { | |
1188 if(exprcat==2)lp->cat=2; | |
1189 else lp->cat=0; | |
1190 lp->value=operand; | |
1191 } else error|=8; | |
1192 } | |
1193 break; | |
1194 case 7:/* FCB */ | |
6 | 1195 generate(); |
0 | 1196 setlabel(lp); |
1197 do { | |
1198 if(*srcptr==',')srcptr++; | |
1199 skipspace(); | |
1200 if(*srcptr=='\"') { | |
1201 srcptr++; | |
1202 while(*srcptr!='\"'&&*srcptr) | |
1203 putbyte(*srcptr++); | |
1204 if(*srcptr=='\"')srcptr++; | |
1205 } else { | |
1206 putbyte(scanexpr(0)); | |
1207 if(unknown&&pass==2)error|=4; | |
1208 } | |
1209 skipspace(); | |
1210 } while(*srcptr==','); | |
1211 break; | |
1212 case 8:/* FCC */ | |
6 | 1213 generate(); |
0 | 1214 setlabel(lp); |
1215 skipspace(); | |
1216 c=*srcptr++; | |
1217 while(*srcptr!=c&&*srcptr) | |
1218 putbyte(*srcptr++); | |
1219 if(*srcptr==c)srcptr++; | |
1220 break; | |
1221 case 9:/* FDB */ | |
6 | 1222 generate(); |
0 | 1223 setlabel(lp); |
1224 do { | |
1225 if(*srcptr==',')srcptr++; | |
1226 skipspace(); | |
1227 putword(scanexpr(0)); | |
1228 if(unknown&&pass==2)error|=4; | |
1229 skipspace(); | |
1230 } while(*srcptr==','); | |
1231 break; | |
1232 case 23 :/* FCS */ | |
6 | 1233 generate(); |
0 | 1234 setlabel(lp); |
1235 skipspace(); | |
1236 int sep = *srcptr; | |
1237 if(sep=='\"' || sep=='/') { | |
1238 srcptr++; | |
1239 while(*srcptr!=sep&&*srcptr) | |
1240 putbyte(*srcptr++); | |
1241 if(*srcptr==sep)srcptr++; | |
1242 codebuf[codeptr-1] |= 0x80; // os9 string termination | |
1243 } | |
1244 break; | |
1245 case 1: /* ELSE */ | |
1246 suppress=1; | |
1247 break; | |
1248 case 21: /* IFP1 */ | |
1249 if(pass==2)suppress=2; | |
1250 break; | |
1251 case 29: /* IFGT */ | |
1252 operand=scanexpr(0); | |
1253 if(unknown)error|=4; | |
1254 if(operand>0)suppress=2; | |
1255 break; | |
1256 case 31: /* IFLT */ | |
1257 operand=scanexpr(0); | |
1258 if(unknown)error|=4; | |
1259 if(operand<0)suppress=2; | |
1260 break; | |
3 | 1261 case 30: /* IFEQ */ |
0 | 1262 operand=scanexpr(0); |
1263 if(unknown)error|=4; | |
1264 if(operand==0)suppress=2; | |
1265 break; | |
3 | 1266 case 28: /* IFNE */ |
0 | 1267 case 10: /* IF */ |
1268 operand=scanexpr(0); | |
1269 if(unknown)error|=4; | |
1270 if(!operand)suppress=2; | |
1271 break; | |
1272 case 12: /* ORG */ | |
1273 operand=scanexpr(0); | |
1274 if(unknown)error|=4; | |
3 | 1275 if(generating&&pass==2&&!outmode&&!os9) { |
0 | 1276 for(i=0;i<(unsigned short)operand-loccounter;i++) |
3 | 1277 fputc(0,objfile); |
1278 } else flushhex(); | |
0 | 1279 loccounter=operand; |
1280 hexaddr=loccounter; | |
1281 break; | |
1282 case 14: /* SETDP */ | |
1283 operand=scanexpr(0); | |
1284 if(unknown)error|=4; | |
1285 if(!(operand&255))operand=(unsigned short)operand>>8; | |
1286 if((unsigned)operand>255)operand=-1; | |
1287 dpsetting=operand; | |
1288 break; | |
1289 case 15: /* SET */ | |
1290 operand=scanexpr(0); | |
1291 if(!lp)error|=32; | |
1292 else { | |
1293 if(lp->cat&1||lp->cat==6) { | |
1294 if(exprcat==2)lp->cat=3; | |
1295 else lp->cat=1; | |
1296 lp->value=operand; | |
1297 } else error|=8; | |
1298 } | |
1299 break; | |
1300 case 2: /* END */ | |
1301 terminate=1; | |
1302 break; | |
1303 case 27: /* USE */ | |
6 | 1304 // locsave = loccounter ; |
0 | 1305 case 16: /* INCLUDE */ |
1306 skipspace(); | |
1307 if(*srcptr=='"')srcptr++; | |
1308 i = 0; | |
1309 for(i=0; !(srcptr[i]==0||srcptr[i]=='"'); i++); | |
1310 int len = i; | |
1311 fname = calloc(1,len); | |
1312 for(i=0;i<len;i++) { | |
1313 if(*srcptr==0||*srcptr=='"')break; | |
1314 fname[i]=*srcptr++; | |
1315 } | |
1316 fname[i]=0; | |
1317 processfile(fname); | |
1318 codeptr=0; | |
1319 srcline[0]=0; | |
3 | 1320 // if (co==27) loccounter = locsave; |
0 | 1321 break; |
1322 case 24: /* MOD */ | |
6 | 1323 oldlc = loccounter = 0; |
0 | 1324 setlabel(lp); |
1325 os9begin(); | |
1326 break; | |
1327 case 25: /* EMOD */ | |
1328 os9end(); | |
1329 break; | |
1330 case 32: /* OS9 */ | |
6 | 1331 generate(); |
0 | 1332 setlabel(lp); |
3 | 1333 putword(0x103f); // SWI2 |
0 | 1334 putbyte(scanexpr(0)); |
15 | 1335 if(unknown&&pass==2)error|=4; |
0 | 1336 break; |
1337 case 18: /* TTL */ | |
1338 break; | |
1339 case 19: /* OPT */ | |
1340 case 26: /* NAM */ | |
1341 case 20: /* PAG */ | |
1342 case 3: /* ENDIF/ENDC */ | |
1343 break; | |
1344 } | |
1345 } | |
1346 | |
1347 | |
1348 void | |
1349 processline() | |
1350 { | |
1351 struct symrecord * lp; | |
1352 struct oprecord * op; | |
1353 int co; | |
1354 char c; | |
1355 srcptr=srcline; | |
1356 oldlc=loccounter; | |
5 | 1357 // error=0; |
0 | 1358 unknown=0;certain=1; |
1359 lp=0; | |
1360 codeptr=0; | |
1361 if(isalnum(*srcptr)) { | |
1362 scanname();lp=findsym(namebuf); | |
1363 if(*srcptr==':') srcptr++; | |
26 | 1364 if(lp && pass==2) { |
1365 oldlc = lp->value; | |
1366 } | |
0 | 1367 } |
1368 skipspace(); | |
1369 if(isalnum(*srcptr)) { | |
1370 scanname(); | |
1371 op=findop(namebuf); | |
1372 if(op) { | |
1373 if(op->cat!=13){ | |
6 | 1374 generate(); |
0 | 1375 setlabel(lp); |
1376 } | |
1377 co=op->code; | |
1378 switch(op->cat) { | |
1379 case 0:onebyte(co);break; | |
1380 case 1:twobyte(co);break; | |
1381 case 2:oneimm(co);break; | |
1382 case 3:lea(co);break; | |
1383 case 4:sbranch(co);break; | |
1384 case 5:lbranch(co);break; | |
1385 case 6:lbra(co);break; | |
1386 case 7:arith(co);break; | |
1387 case 8:darith(co);break; | |
1388 case 9:d2arith(co);break; | |
1389 case 10:oneaddr(co);break; | |
1390 case 11:tfrexg(co);break; | |
1391 case 12:pshpul(co);break; | |
1392 case 13:pseudoop(co,lp); | |
1393 } | |
1394 c=*srcptr; | |
1395 if (debug) fprintf(stderr,"DEBUG: processline: mode=%d, opsize=%d, error=%d, postbyte=%02X c=%c\n",mode,opsize,error,postbyte,c); | |
1396 if(c!=' '&&*(srcptr-1)!=' '&&c!=0&&c!=';')error|=2; | |
1397 } | |
1398 else error|=0x8000; | |
26 | 1399 } else { |
1400 prevlp = lp; // os9 mode label can be data or code | |
1401 } | |
0 | 1402 if(pass==2) { |
1403 outbuffer(); | |
1404 if(listing)outlist(); | |
1405 } | |
1406 if(error)report(); | |
1407 loccounter+=codeptr; | |
25 | 1408 if (os9 && prevloc==0 ) { |
1409 prevloc = loccounter+1; | |
1410 oldlc = loccounter = 0; | |
1411 } | |
0 | 1412 } |
1413 | |
1414 void | |
1415 suppressline() | |
1416 { | |
1417 struct oprecord * op; | |
1418 srcptr=srcline; | |
1419 oldlc=loccounter; | |
1420 codeptr=0; | |
1421 if(isalnum(*srcptr)) { | |
1422 scanname(); | |
1423 if(*srcptr==':')srcptr++; | |
1424 } | |
1425 skipspace(); | |
1426 scanname();op=findop(namebuf); | |
1427 if(op && op->cat==13) { | |
1428 if(op->code==10||op->code==13||op->code==29||op->code==28||op->code==21||op->code==30) ifcount++; | |
1429 else if(op->code==3) { | |
1430 if(ifcount>0)ifcount--;else if(suppress==1|suppress==2)suppress=0; | |
1431 } else if(op->code==1) { | |
1432 if(ifcount==0 && suppress==2)suppress=0; | |
1433 } | |
1434 } | |
6 | 1435 if(pass==2&&listing)outlist(); } |
0 | 1436 |
1437 void | |
1438 usage(char*nm) | |
1439 { | |
1440 fprintf(stderr,"Usage: %s [-o objname] [-l listname] [-s srecord-file] srcname\n",nm); | |
1441 exit(2); | |
1442 } | |
1443 | |
1444 char * | |
1445 strconcat(char *s,int spos,char *d) | |
1446 { | |
1447 int slen = strlen(s); | |
1448 int dlen = strlen(d); | |
1449 if ( spos == 0) spos = slen; | |
1450 char *out = calloc(1,spos+dlen+1); | |
1451 int i = 0; | |
1452 for(; i< spos; i++ ) out[i] = s[i]; | |
1453 for(; i< spos+dlen+1; i++ ) out[i] = *d++; | |
1454 return out; | |
1455 } | |
1456 | |
1457 | |
1458 void | |
1459 getoptions(int c,char*v[]) | |
1460 { | |
6 | 1461 int i=1; |
0 | 1462 if(c==1)usage(v[0]); |
6 | 1463 while(v[i]) { |
1464 if(strcmp(v[i],"-d")==0) { | |
1465 debug=1; | |
1466 i++; | |
1467 } else if(strcmp(v[i],"-o")==0) { | |
1468 objname = v[i+1]; | |
1469 i+=2; | |
1470 } else if(strcmp(v[i],"-s")==0) { | |
1471 objname=v[i+1]; | |
1472 outmode=1; | |
1473 i+=2; | |
1474 } else if(strcmp(v[i],"-l")==0) { | |
1475 listname=v[i+1]; | |
1476 i+=2; | |
1477 } else if(*v[i]=='-') { | |
1478 usage(v[0]); | |
1479 } else { | |
1480 if (srcname) usage(v[0]); | |
1481 srcname=v[i]; | |
1482 i++; | |
1483 } | |
0 | 1484 } |
1485 if(objname==0) { | |
1486 for(i=0;srcname[i]!='.' && srcname[i]!=0 ;i++) ; | |
1487 objname = strconcat(srcname,i,".b"); | |
1488 } | |
1489 listing=(listname!=0); | |
1490 } | |
1491 | |
1492 void | |
1493 expandline() | |
1494 { | |
1495 int i=0,j=0,k,j1; | |
1496 for(i=0;i<128&&j<128;i++) | |
1497 { | |
1498 if(inpline[i]=='\n') { | |
1499 srcline[j]=0;break; | |
1500 } | |
1501 if(inpline[i]=='\t') { | |
1502 j1=j; | |
1503 for(k=0;k<8-j1%8 && j<128;k++)srcline[j++]=' '; | |
1504 }else srcline[j++]=inpline[i]; | |
1505 } | |
1506 srcline[127]=0; | |
1507 } | |
1508 | |
1509 | |
1510 void | |
1511 processfile(char *name) | |
1512 { | |
1513 char *oldname; | |
1514 int oldno; | |
1515 FILE *srcfile; | |
1516 oldname=curname; | |
1517 curname=name; | |
1518 oldno=lineno; | |
1519 lineno=0; | |
1520 if((srcfile=fopen(name,"r"))==0) { | |
6 | 1521 int i = 0; |
1522 if (oldname) { | |
1523 i = strlen(oldname); | |
1524 while(i>0 && oldname[i]!='/') i--; | |
1525 } | |
0 | 1526 if (i>0) { |
1527 char *next = strconcat(oldname,i+1,name); | |
1528 if((srcfile=fopen(next,"r"))==0) { | |
1529 fprintf(stderr,"Cannot open source file %s\n",next); | |
1530 exit(4); | |
1531 } | |
1532 curname = next; | |
1533 } else { | |
1534 fprintf(stderr,"Cannot open source file %s\n",name); | |
1535 exit(4); | |
1536 } | |
1537 } | |
1538 while(!terminate&&fgets(inpline,128,srcfile)) { | |
1539 expandline(); | |
1540 lineno++; | |
1541 srcptr=srcline; | |
1542 if(suppress)suppressline(); else processline(); | |
1543 } | |
26 | 1544 setlabel(0); // process prevlp |
0 | 1545 fclose(srcfile); |
1546 if(suppress) { | |
1547 fprintf(stderr,"improperly nested IF statements in %s",curname); | |
1548 errors++; | |
1549 suppress=0; | |
1550 } | |
1551 lineno=oldno; | |
1552 curname=oldname; | |
1553 } | |
1554 | |
1555 int | |
1556 main(int argc,char *argv[]) | |
1557 { | |
1558 char c; | |
1559 getoptions(argc,argv); | |
1560 pass=1; | |
1561 errors=0; | |
1562 generating=0; | |
1563 terminate=0; | |
1564 processfile(srcname); | |
1565 if(errors) { | |
1566 fprintf(stderr,"%d Pass 1 Errors, Continue?",errors); | |
1567 c=getchar(); | |
1568 if(c=='n'||c=='N') exit(3); | |
1569 } | |
1570 pass=2; | |
6 | 1571 prevloc = 0; |
0 | 1572 loccounter=0; |
1573 errors=0; | |
1574 generating=0; | |
1575 terminate=0; | |
1576 if(listing&&((listfile=fopen(listname,"w"))==0)) { | |
1577 fprintf(stderr,"Cannot open list file"); | |
1578 exit(4); | |
1579 } | |
1580 if((objfile=fopen(objname,outmode?"w":"wb"))==0) { | |
1581 fprintf(stderr,"Cannot write object file\n"); | |
1582 exit(4); | |
1583 } | |
1584 processfile(srcname); | |
1585 fprintf(stderr,"%d Pass 2 errors.\n",errors); | |
1586 if(listing) { | |
1587 fprintf(listfile,"%d Pass 2 errors.\n",errors); | |
1588 outsymtable(); | |
1589 fclose(listfile); | |
1590 } | |
1591 if(outmode){ | |
1592 flushhex(); | |
1593 fprintf(objfile,"S9030000FC\n"); | |
1594 } | |
1595 fclose(objfile); | |
1596 return 0; | |
1597 } | |
1598 |