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