|
|
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:
193: #define STACK 32*1024L;
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:
301: do_define("fpiar", "fpi");
302:
303: do_define("XREF", ".globl");
304:
305: do_define("XDEF", ".globl");
306:
307: do_define("TEXT", ".text");
308:
309: do_define("DATA", ".data");
310:
311: /* gas doesn't have a .bss directive */
312:
313: do_define("BSS", ".data");
314:
315: do_define("END", "| END");
316:
317: do_define("dc.l", ".long");
318:
319: do_define("dc.w", ".word");
320:
321: do_define("dc.b", ".byte");
322:
323: } else if (syntax == ASM) {
324:
325: do_define("TEXT", "SECTION TEXT");
326:
327: do_define("DATA", "SECTION DATA");
328:
329: do_define("BSS", "SECTION BSS");
330:
331: }
332:
333: }
334:
335:
336:
337: int
338:
339: main (argc, argv)
340:
341: int argc; char **argv;
342:
343: {
344:
345: FILE *f;
346:
347: #ifdef NATIVEATARI
348:
349: if (!argv[0] || !argv[0][0]) /* run from desktop? */
350:
351: atexit(hit_return);
352:
353: #endif
354:
355: argv++;
356:
357: outfile = stdout;
358:
359:
360:
361: while (*argv) {
362:
363: if (!strcmp(*argv, "-o")) {
364:
365: argv++;
366:
367: if (*argv == 0) {
368:
369: fprintf(stderr, "missing argument to -o\n");
370:
371: usage();
372:
373: }
374:
375: f = fopen(*argv, "w");
376:
377: if (!f)
378:
379: perror(*argv);
380:
381: else
382:
383: outfile = f;
384:
385: argv++;
386:
387: } else if (!strcmp(*argv, "-gas")) {
388:
389: argv++;
390:
391: syntax = GAS;
392:
393: } else if (!strcmp(*argv, "-asm")) {
394:
395: argv++;
396:
397: syntax = ASM;
398:
399: } else if (!strcmp(*argv, "-purec")) {
400:
401: argv++;
402:
403: syntax = PUREC;
404:
405: } else if (!strncmp(*argv, "-D", 2)) {
406:
407: char *word, *defn;
408:
409: word = *argv+2;
410:
411: defn = index(word,'=');
412:
413: if (defn)
414:
415: *defn++ = '\0';
416:
417: else
418:
419: defn = "1";
420:
421: if (*word) do_define(word,defn);
422:
423: argv++;
424:
425: } else if (!strcmp(*argv, "--")) {
426:
427: argv++;
428:
429: break;
430:
431: } else {
432:
433: if (**argv == '-') {
434:
435: fprintf(stderr, "unknown option: %s\n",
436:
437: *argv);
438:
439: usage();
440:
441: }
442:
443: break;
444:
445: }
446:
447: }
448:
449:
450:
451: do_initial_defs();
452:
453:
454:
455: if (*argv == 0) {
456:
457: setjmp(start);
458:
459: infile = stdin;
460:
461: yyparse();
462:
463: } else {
464:
465: while(*argv) {
466:
467: if (!(f = fopen(*argv, "r")))
468:
469: perror(*argv);
470:
471: else {
472:
473: infile = f;
474:
475: setjmp(start);
476:
477: yyparse();
478:
479: fclose(f);
480:
481: }
482:
483: argv++;
484:
485: }
486:
487: }
488:
489:
490:
491: if (ifstkptr != 0) {
492:
493: fputs("%ifdef without matching %endif\n", stderr);
494:
495: errors++;
496:
497: }
498:
499: return errors;
500:
501: }
502:
503:
504:
505: void
506:
507: yyerror (s) /* Called by yyparse on error */
508:
509: char *s;
510:
511: {
512:
513: errors++;
514:
515: printf("%s\n", s);
516:
517: longjmp(start, 1);
518:
519: }
520:
521:
522:
523: void dbgmsg(s) char *s;
524:
525: {
526:
527: fprintf(stderr, "%s\n", s);
528:
529: }
530:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.