|
|
1.1 root 1: /* Infix notation calculator--calc */
2:
3:
4:
5: %{
6:
7: #define YYSTYPE char *
8:
9:
10:
11: #include "asmtrans.h"
12:
13: %}
14:
15:
16:
17: %token WORD
18:
19: %token WHITESP
20:
21: %token EOLN
22:
23: %token STRING
24:
25: %token DEFINECMD
26:
27: %token INCLUDECMD
28:
29: %token IFDEFCMD
30:
31: %token IFNDEFCMD
32:
33: %token ELSECMD
34:
35: %token ENDIFCMD
36:
37:
38:
39: /* Grammar follows */
40:
41: %%
42:
43: input:
44:
45: | input line
46:
47: ;
48:
49:
50:
51: line: EOLN { emit($1); }
52:
53: /* note that emit() will automatically free the memory,
54:
55: and will also check the hidecnt variable */
56:
57: | label EOLN { emit($1); emit($2); }
58:
59: | opline EOLN { emit($1); emit($2); }
60:
61: | label opline EOLN { emit($1); emit($2);
62:
63: emit($3); }
64:
65: | INCLUDECMD WHITESP STRING EOLN { if (!hidecnt) do_include($3); free($3); }
66:
67: | INCLUDECMD WHITESP WORD EOLN { if (!hidecnt) do_include($3); free($3); }
68:
69: | DEFINECMD WHITESP WORD WHITESP STRING EOLN {
70:
71: if (!hidecnt) do_define($3, $5); free($3); free($5); }
72:
73: | DEFINECMD WHITESP WORD WHITESP operand EOLN {
74:
75: if (!hidecnt) do_define($3, $5); free($3); free($5); }
76:
77: | IFDEFCMD WHITESP WORD EOLN { do_ifdef($3); free($3); }
78:
79: | IFNDEFCMD WHITESP WORD EOLN { do_ifndef($3); free($3); }
80:
81: | ELSECMD EOLN { do_else(); }
82:
83: | ENDIFCMD EOLN { do_endif(); }
84:
85: ;
86:
87:
88:
89: opline: WHITESP opcode { $$ = do_ops("", $2, "", ""); free($2); }
90:
91: | WHITESP opcode WHITESP ops
92:
93: { $$ = do_ops("", $2, $3, $4);
94:
95: free($2); free($3); free($4); }
96:
97: | WORD WHITESP opcode { $$ = do_ops($1, $3, "", ""); free($1); free($3); }
98:
99: | WORD WHITESP opcode WHITESP ops
100:
101: { $$ = do_ops($1, $3, $4, $5);
102:
103: free($1); free($3); free($4); free($5);}
104:
105: ;
106:
107:
108:
109: ops: operand { $$ = $1; }
110:
111: | operand ',' ops { $$ = concat3($1, ",", $3);
112:
113: free($1); free($3); }
114:
115: ;
116:
117:
118:
119: opcode: WORD { $$ = wordlookup($1); free($1); }
120:
121: ;
122:
123:
124:
125: label: WORD ':' { $$ = concat($1, ":"); free($1); }
126:
127:
128:
129: operand: basic {$$ = $1; }
130:
131: | '#' basic {$$ = immediate($2); free($2); }
132:
133: | '(' basic ')' {$$ = indirect($2); free($2); }
134:
135: | '(' basic ')' '+' {$$ = postinc($2); free($2); }
136:
137: | '-' '(' basic ')' {$$ = predec($3); free($3); }
138:
139: | basic '(' basic ')' {$$ = indexed($1, $3); free($1); free($3); }
140:
141: | '(' basic ')' WORD {$$ = sizedop($2, $4); free($2); free($4); }
142:
143: | basic '(' basic ',' basic ')' {$$ = twoindex($1, $3, $5);
144:
145: free($1); free($3); free($5); }
146:
147: | basic '{' basic ':' basic '}' {$$ = bitfield($1, $3, $5);
148:
149: free($1); free($3); free($5); }
150:
151: ;
152:
153:
154:
155: basic: basexpr { $$ = $1; }
156:
157: | basexpr op basic { $$ = concat3($1, $2, $3); free($1); free($2); free($3); }
158:
159: | '-' basic { $$ = concat("-", $2); free($2); }
160:
161:
162:
163: basexpr: WORD {$$ = wordlookup($1); free($1); }
164:
165: | '$' WORD {$$ = hexop($2); free($2);}
166:
167: ;
168:
169:
170:
171: op: '+' { $$ = strdup("+"); }
172:
173: | '-' { $$ = strdup("-"); }
174:
175: | '*' { $$ = strdup("*"); }
176:
177: | '/' { $$ = strdup("/"); }
178:
179: ;
180:
181: %%
182:
183: #include <setjmp.h>
184:
185:
186:
187: jmp_buf start;
188:
189:
190:
191: #ifdef NATIVEATARI
192:
1.1.1.2 ! root 193: #define STACK 32*1024L
1.1 root 194:
195: #ifdef LATTICE
196:
197: long _STACK = STACK;
198:
199: #endif
200:
201: #ifdef __GNUC__
202:
203: long _stksize = STACK;
204:
205: #endif
206:
207:
208:
209: static void
210:
211: hit_return()
212:
213: {
214:
215: printf("Hit return to continue\n");
216:
217: fflush(stdout);
218:
219: getchar();
220:
221: }
222:
223: #endif
224:
225:
226:
227: void usage()
228:
229: {
230:
231: fprintf(stderr, "Usage: asmtrans [-gas][-asm][-o outfile] infile\n");
232:
233: exit(2);
234:
235: }
236:
237:
238:
239: int errors = 0;
240:
241:
242:
243: void
244:
245: do_include(file)
246:
247: char *file;
248:
249: {
250:
251: jmp_buf save;
252:
253: FILE *oldin, *f;
254:
255:
256:
257: f = fopen(file, "r");
258:
259: if (!f) {
260:
261: perror(file);
262:
263: return;
264:
265: }
266:
267: bcopy(start, save, sizeof(jmp_buf));
268:
269: oldin = infile;
270:
271: infile = f;
272:
273: setjmp(start);
274:
275: yyparse();
276:
277: fclose(f);
278:
279: infile = oldin;
280:
281: bcopy(save, start, sizeof(jmp_buf));
282:
283: longjmp(start,1);
284:
285: }
286:
287:
288:
289: /* set up initial definitions based on syntax type */
290:
291:
292:
293: void
294:
295: do_initial_defs()
296:
297: {
298:
299: if (syntax == GAS) {
300:
1.1.1.2 ! root 301: do_define("mmusr", "psr");
! 302:
1.1 root 303: do_define("fpiar", "fpi");
304:
305: do_define("XREF", ".globl");
306:
307: do_define("XDEF", ".globl");
308:
309: do_define("TEXT", ".text");
310:
311: do_define("DATA", ".data");
312:
313: /* gas doesn't have a .bss directive */
314:
315: do_define("BSS", ".data");
316:
317: do_define("END", "| END");
318:
319: do_define("dc.l", ".long");
320:
321: do_define("dc.w", ".word");
322:
323: do_define("dc.b", ".byte");
324:
325: } else if (syntax == ASM) {
326:
327: do_define("TEXT", "SECTION TEXT");
328:
329: do_define("DATA", "SECTION DATA");
330:
331: do_define("BSS", "SECTION BSS");
332:
333: }
334:
335: }
336:
337:
338:
339: int
340:
341: main (argc, argv)
342:
343: int argc; char **argv;
344:
345: {
346:
347: FILE *f;
348:
349: #ifdef NATIVEATARI
350:
351: if (!argv[0] || !argv[0][0]) /* run from desktop? */
352:
353: atexit(hit_return);
354:
355: #endif
356:
357: argv++;
358:
359: outfile = stdout;
360:
361:
362:
363: while (*argv) {
364:
365: if (!strcmp(*argv, "-o")) {
366:
367: argv++;
368:
369: if (*argv == 0) {
370:
371: fprintf(stderr, "missing argument to -o\n");
372:
373: usage();
374:
375: }
376:
377: f = fopen(*argv, "w");
378:
379: if (!f)
380:
381: perror(*argv);
382:
383: else
384:
385: outfile = f;
386:
387: argv++;
388:
389: } else if (!strcmp(*argv, "-gas")) {
390:
391: argv++;
392:
393: syntax = GAS;
394:
395: } else if (!strcmp(*argv, "-asm")) {
396:
397: argv++;
398:
399: syntax = ASM;
400:
401: } else if (!strcmp(*argv, "-purec")) {
402:
403: argv++;
404:
405: syntax = PUREC;
406:
407: } else if (!strncmp(*argv, "-D", 2)) {
408:
409: char *word, *defn;
410:
411: word = *argv+2;
412:
413: defn = index(word,'=');
414:
415: if (defn)
416:
417: *defn++ = '\0';
418:
419: else
420:
421: defn = "1";
422:
423: if (*word) do_define(word,defn);
424:
425: argv++;
426:
427: } else if (!strcmp(*argv, "--")) {
428:
429: argv++;
430:
431: break;
432:
433: } else {
434:
435: if (**argv == '-') {
436:
437: fprintf(stderr, "unknown option: %s\n",
438:
439: *argv);
440:
441: usage();
442:
443: }
444:
445: break;
446:
447: }
448:
449: }
450:
451:
452:
453: do_initial_defs();
454:
455:
456:
457: if (*argv == 0) {
458:
459: setjmp(start);
460:
461: infile = stdin;
462:
463: yyparse();
464:
465: } else {
466:
467: while(*argv) {
468:
469: if (!(f = fopen(*argv, "r")))
470:
471: perror(*argv);
472:
473: else {
474:
475: infile = f;
476:
477: setjmp(start);
478:
479: yyparse();
480:
481: fclose(f);
482:
483: }
484:
485: argv++;
486:
487: }
488:
489: }
490:
491:
492:
493: if (ifstkptr != 0) {
494:
495: fputs("%ifdef without matching %endif\n", stderr);
496:
497: errors++;
498:
499: }
500:
501: return errors;
502:
503: }
504:
505:
506:
507: void
508:
509: yyerror (s) /* Called by yyparse on error */
510:
511: char *s;
512:
513: {
514:
515: errors++;
516:
517: printf("%s\n", s);
518:
519: longjmp(start, 1);
520:
521: }
522:
523:
524:
525: void dbgmsg(s) char *s;
526:
527: {
528:
529: fprintf(stderr, "%s\n", s);
530:
531: }
532:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.