1 /****************************************************************************
2     Copyright (C) 1987-2015 by Jeffery P. Hansen
3 
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8 
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License along
15     with this program; if not, write to the Free Software Foundation, Inc.,
16     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 
18     Last edit by hansen on Thu Apr 13 17:59:12 2000
19 ****************************************************************************/
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdarg.h>
24 #include <sys/types.h>
25 #include <pwd.h>
26 #include "hash.h"
27 #include "parser.h"
28 #include "grammar.h"
29 #include "gmac.h"
30 
31 extern Code *mcode;
32 extern Code *ucode;
33 extern Code *map;
34 
35 int ycLineNumber;
36 char ycFileName[1024];
37 int yyErrorCount = 0;
38 
39 #ifndef yyrestart
40 void yyrestart(FILE*);
41 #endif
42 
43 /*
44   Current objects (used by parser)
45  */
46 struct {
47   MemProg	*mb;			/* The current memory program */
48   UField	*field;			/* The most recently defined field */
49   SHash		*eset;			/* The current enum set */
50   OpDef		*opdef;			/* The current operator */
51   OprDef	*oprdef;		/* The current operand */
52   OprGroup	*ogdef;			/* The current operand group */
53   int		dtype;			/* Current data type for .short, .byte, etc.*/
54   int		noprs;			/* Number of operands */
55   int		opr_type[MAXOPRS];	/* Operand types */
56   int		opr_regs[MAXOPRS];	/* Operand registers */
57   Number	*opr_nums[MAXOPRS];	/* Operand numbers */
58   char		*proc_name;		/* Current procedure name */
59 } cur;
60 
yywrap()61 int yywrap()
62 {
63   return 1;
64 }
65 
yyerror(char * s,...)66 int yyerror(char *s,...)
67 {
68   va_list ap;
69 
70   va_start(ap,s);
71 
72   fprintf(stderr,"<\"%s\",%d> ",ycFileName,ycLineNumber);
73   vfprintf(stderr,s,ap);
74   fprintf(stderr,"\n");
75 
76   va_end(ap);
77 
78   yyErrorCount++;
79 
80   return 0;
81 }
82 
83 
ycBank(int w,struct range * br,char * name,struct range * ar)84 void ycBank(int w,struct range *br,char *name,struct range *ar)
85 {
86   Bank *B;
87 
88   if (br->msb < br->lsb) {
89     yyerror("Msb can not be less than lsb in bank declaration.");
90     return;
91   }
92 
93   if ((br->msb-br->lsb+1) > 32) {
94     yyerror("Banks can not be more than 32 bits.");
95     return;
96   }
97 
98   B = new_Bank(name,br->msb,br->lsb,ar ? ar->msb : -1,ar ? ar->lsb : -1);
99 
100   switch (w) {
101   case MICROCODE :
102     Code_addBank(ucode,B);
103     break;
104   case MACROCODE :
105     Code_addBank(mcode,B);
106     break;
107   case MAP :
108     Code_addBank(map,B);
109     break;
110   }
111 }
112 
ycField(char * name,struct range * r,int iscomp)113 void ycField(char *name,struct range *r,int iscomp)
114 {
115   if (iscomp && r->msb != r->lsb) {
116     yyerror("Compliment operator not allowed on multi-bit fields.");
117     return;
118   }
119 
120   cur.field = new_UField(name,r->msb,r->lsb,iscomp);
121   Code_addField(ucode,cur.field);
122   cur.eset = &cur.field->uf_codes;
123 }
124 
ycEnum(char * name,int value)125 void ycEnum(char *name,int value)
126 {
127   int *n = new(int);
128   *n = value;
129 
130   SHash_insert(cur.eset,name,n);
131 }
132 
ycBeginUCode(int addr)133 void ycBeginUCode(int addr)
134 {
135   cur.mb = new_MemProg(Code_nbits(ucode),addr);
136 
137   Code_addMemProg(ucode,cur.mb);
138 }
139 
ycEndUCode()140 void ycEndUCode()
141 {
142 }
143 
ycULabel(char * name)144 void ycULabel(char *name)
145 {
146   if (Code_findLabel(ucode,name)) {
147     yyerror("Redefinition of target '%s'.",name);
148     return;
149   }
150   Code_addLabel(ucode,name,MemProg_current(cur.mb));
151 }
152 
ycUNext()153 void ycUNext()
154 {
155   MemProg_incAddr(cur.mb);
156 }
157 
ycUSpec(struct number * n1,struct number * n2)158 void ycUSpec(struct number *n1,struct number *n2)
159 {
160   UField *f = 0;
161   int dmsb,dlsb;
162   MemInst *mi;
163 
164   switch (n1->ntype) {
165   case NT_VALUE:
166     dmsb = dlsb = n1->v.d;
167     break;
168   case NT_SYMBOL :
169     f = Code_findField(ucode,n1->v.s);
170     if (!f) {
171       yyerror("Undefined field '%s' in micro-instruction.",n1->v.s);
172       return;
173     }
174     dmsb = UField_msb(f);
175     dlsb = UField_lsb(f);
176     break;
177   default:
178     yyerror("Unexpected relative symbol in micro-instruction.");
179     return;
180   }
181 
182   if (dmsb < dlsb) {
183     int x = dmsb;
184     dmsb = dlsb;
185     dlsb = x;
186   }
187 
188 
189   mi = new_MemInst(MemProg_current(cur.mb),dmsb,dlsb);
190   MemProg_addInst(cur.mb,mi);
191 
192   if (n2) {
193     switch (n2->ntype) {
194     case NT_VALUE:
195       MemInst_setNumValue(mi,n2->v.d);
196       break;
197     case NT_SYMBOL :
198       {
199 	int *n = 0;
200 	if (f)
201 	  n = UField_findEnum(f,n2->v.s);
202 	if (!n)
203 	  n = Code_findLabel(ucode,n2->v.s);
204 	if (n) {
205 	  MemInst_setNumValue(mi,*n);
206 	} else {
207 	  MemInst_setSymValue(mi,n2->v.s,0,dmsb-dlsb,0);
208 	}
209       }
210       break;
211     default:
212       yyerror("Unexpected relative symbol in micro-instruction.");
213       return;
214     }
215   } else
216     MemInst_setNumValue(mi,1);
217 }
218 
ycMap(struct number * val,struct number * addr)219 void ycMap(struct number *val,struct number *addr)
220 {
221   MemProg *mb;
222   MemInst *mi;
223 
224   if (Code_numMBlocks(map) == 0) {
225     mb = new_MemProg(Code_nbits(ucode),0);
226     Code_addMemProg(map,mb);
227   } else
228     mb = Code_getMBlock(map,0);
229 
230   if (addr->ntype  != NT_VALUE) {
231     yyerror("Symbolic value not allowed for map address.");
232     return;
233   }
234 
235   mi = new_MemInst(addr->v.d,31,0);
236   MemProg_addInst(mb,mi);
237 
238   if (val->ntype == NT_VALUE)
239     MemInst_setNumValue(mi,val->v.d);
240   else
241     MemInst_setSymValue(mi,val->v.s,0,31,0);
242 }
243 
ycBeginOp(char * name)244 void ycBeginOp(char *name)
245 {
246   if (SHash_find(&mcode->c_ops,name)) {
247     yyerror("Redefinition of operator '%s'.",name);
248     return;
249   }
250 
251   cur.opdef = new_OpDef(name,Code_nbits(mcode));
252   SHash_insert(&mcode->c_ops,name,cur.opdef);
253 }
254 
ycEndOp()255 void ycEndOp()
256 {
257   /*  OpDef_print(cur.opdef,stdout);*/
258   cur.opdef = 0;
259 }
260 
ycAddOprGroup(char * name)261 void ycAddOprGroup(char *name)
262 {
263   OprGroup *og = (OprGroup *) SHash_find(&mcode->c_oprs,name);
264   if (!name) {
265     yyerror("Undefined operand group '%s'\n",name);
266     return;
267   }
268   OpDef_addGroup(cur.opdef,og);
269 }
270 
ycBeginOprGroup(char * name)271 void ycBeginOprGroup(char *name)
272 {
273   if (name) {
274     if (SHash_find(&mcode->c_oprs,name)) {
275       yyerror("Redefinition of operator '%s'.",name);
276       return;
277     }
278   }
279 
280   cur.ogdef = new_OprGroup(name ? name : "*local*",Code_nbits(mcode));
281   cur.oprdef = 0;
282 
283   if (name && !cur.opdef)
284     SHash_insert(&mcode->c_oprs,name,cur.ogdef);
285 
286   if (cur.opdef)
287     OpDef_addGroup(cur.opdef,cur.ogdef);
288 }
289 
ycEndOprGroup()290 void ycEndOprGroup()
291 {
292   cur.ogdef = 0;
293   cur.oprdef = 0;
294 }
295 
ycBeginOperand()296 void ycBeginOperand()
297 {
298   cur.oprdef = OprGroup_addOprDef(cur.ogdef);
299 }
300 
ycEndOperand()301 void ycEndOperand()
302 {
303   int r = OprDef_check(cur.oprdef);
304 
305   switch (r) {
306   case GE_IDRANGE :
307     yyerror("Id number(s) in operand declaration out of range.");
308     break;
309   case GE_REDECID :
310     yyerror("Id numbers in operand declaration must be unique.");
311     break;
312   case GE_UNDEFID :
313     yyerror("Undefined Id number used in operand declaration.");
314     break;
315   case GE_BADTYPE :
316     yyerror("Type mismatch in operand declaration.");
317     break;
318   case GE_OK :
319     break;
320   default :
321     yyerror("Bogonic internal code %d in ycEndOperand.",r);
322     break;
323   }
324 
325   cur.oprdef = 0;
326 }
327 
ycOLHS(int type,int n1,int n2)328 void ycOLHS(int type,int n1,int n2)
329 {
330   OprDef_addOpr(cur.oprdef,type,n1,n2);
331 }
332 
ycORHS(int type,int bnum,struct range * br,int vnum,struct range * vr,int offset)333 void ycORHS(int type,int bnum,struct range *br,int vnum,struct range *vr,int offset)
334 {
335   OpInst *oi;
336 
337   oi = new_OpInst(type,bnum,br->msb,br->lsb,vnum,vr ? vr->msb : -1,vr ? vr->lsb : -1,offset);
338 
339   if (cur.oprdef)
340     OprDef_addOpInst(cur.oprdef,oi);
341   else
342     OpDef_addOpInst(cur.opdef,oi);
343 }
344 
ycMLabel(char * name)345 void ycMLabel(char *name)
346 {
347   char *ename = 0;
348   char buf[1024];
349 
350   if (cur.proc_name) {
351 
352     sprintf(buf,"%s.%s",cur.proc_name,name);
353     ename = name;
354     name = buf;
355   } else
356     ename = name;
357 
358   if (Code_findLabel(mcode,name)) {
359     yyerror("Redefinition of target '%s'.",ename);
360     return;
361   }
362   Code_addLabel(mcode,name,MemProg_current(cur.mb));
363 }
364 
ycMSymbol(char * name,int value)365 void ycMSymbol(char *name,int value)
366 {
367   char *ename = 0;
368   char buf[1024];
369 
370   if (cur.proc_name) {
371 
372     sprintf(buf,"%s.%s",cur.proc_name,name);
373     ename = name;
374     name = buf;
375   } else
376     ename = name;
377 
378   if (Code_findLabel(mcode,name)) {
379     yyerror("Redefinition of target '%s'.",ename);
380     return;
381   }
382   Code_addLabel(mcode,name,value);
383 }
384 
ycBeginMCode(int addr)385 void ycBeginMCode(int addr)
386 {
387   cur.mb = new_MemProg(Code_nbits(mcode),addr);
388   Code_addMemProg(mcode,cur.mb);
389 
390   cur.opdef = 0;
391   cur.proc_name = 0;
392 }
393 
ycEndMCode()394 void ycEndMCode()
395 {
396 }
397 
ycMOp(char * name)398 void ycMOp(char *name)
399 {
400   cur.opdef = (OpDef*) SHash_find(&mcode->c_ops,name);
401   if (!cur.opdef) {
402     yyerror("Undefined operator '%s'",name);
403     return;
404   }
405   cur.noprs = 0;
406 }
407 
ycMOperand(int am,struct number * n,int r)408 void ycMOperand(int am,struct number *n,int r)
409 {
410   if (cur.noprs == MAXOPRS) {
411     yyerror("Too many operands for instruction.");
412     return;
413   }
414 
415   if (cur.proc_name && n && n->ntype == NT_SYMBOL) {
416     char buf[1024];
417 
418     sprintf(buf,"%s.%s",cur.proc_name,n->v.s);
419     n->v.s = strdup(buf);
420   }
421 
422   cur.opr_type[cur.noprs] = am;
423   cur.opr_nums[cur.noprs] = n;
424   cur.opr_regs[cur.noprs] = r;
425 
426   cur.noprs++;
427 }
428 
ycMNext()429 void ycMNext()
430 {
431   if (cur.opdef) {
432     OprDef *od = OpDef_findOpr(cur.opdef,cur.opr_type,cur.noprs);
433     if (!od) {
434       yyerror("Illegal operands for '%s' instruction.",cur.opdef->od_name);
435     } else {
436       int i;
437       int r[MAXOPRS*2];
438       Number *n[MAXOPRS*2];
439       MemInst *mi;
440       int maxAddr = 0;
441 
442       for (i = 0;i < od->od_numOprs;i++) {
443 	Opr *p = od->od_oprs[i];
444 	switch (p->od_type) {
445 	case AM_IMMEDIATE :
446 	case AM_IMMIND :
447 	  n[p->od_val] = cur.opr_nums[i];
448 	  break;
449 	case AM_DIRECT :
450 	case AM_INDIRECT :
451 	  r[p->od_reg] = cur.opr_regs[i];
452 	  break;
453 	case AM_INDEXED :
454 	  n[p->od_val] = cur.opr_nums[i];
455 	  r[p->od_reg] = cur.opr_regs[i];
456 	  break;
457 	}
458       }
459 
460       for (i = 0;i < cur.opdef->od_numInsts;i++) {
461 	if ((mi = OpInst_genMemInst(cur.opdef->od_insts[i],cur.mb,0,0))) {
462 	  if (mi->mi_addr > maxAddr) maxAddr = mi->mi_addr;
463 	  MemProg_addInst(cur.mb,mi);
464 	} else
465 	  yyerror("Illegal memory instruction in definition of '%s'.",cur.opdef->od_name);
466       }
467 
468       for (i = 0;i < od->od_numInsts;i++) {
469 	if ((mi = OpInst_genMemInst(od->od_insts[i],cur.mb,r,n))) {
470 	  if (mi->mi_addr > maxAddr) maxAddr = mi->mi_addr;
471 	  MemProg_addInst(cur.mb,mi);
472 	}
473       }
474       while (MemProg_current(cur.mb) <= maxAddr)
475 	MemProg_incAddr(cur.mb);
476     }
477     cur.opdef = 0;
478   }
479 }
480 
ycBss(int n)481 void ycBss(int n)
482 {
483   cur.mb->mb_current += n;
484 }
485 
ycData(int dt)486 void ycData(int dt)
487 {
488   cur.dtype = dt;
489 }
490 
ycDataNum(int v)491 void ycDataNum(int v)
492 {
493   MemInst *mi;
494 
495   switch (cur.dtype) {
496   case DBYTE :
497     mi = new_MemInst(MemProg_current(cur.mb),7,0);
498     MemInst_setNumValue(mi,v);
499     MemProg_addInst(cur.mb,mi);
500     MemProg_incAddr(cur.mb);
501     break;
502   case DSHORT :
503     if ((MemProg_current(cur.mb) & 0x1)) {
504       mi = new_MemInst(MemProg_current(cur.mb),7,0);
505       MemInst_setNumValue(mi,0);
506       MemProg_addInst(cur.mb,mi);
507       MemProg_incAddr(cur.mb);
508     }
509 
510     mi = new_MemInst(MemProg_current(cur.mb),7,0);
511     MemInst_setNumValue(mi,(v&0xff));
512     MemProg_addInst(cur.mb,mi);
513     MemProg_incAddr(cur.mb);
514 
515     mi = new_MemInst(MemProg_current(cur.mb),7,0);
516     MemInst_setNumValue(mi,((v>>8)&0xff));
517     MemProg_addInst(cur.mb,mi);
518     MemProg_incAddr(cur.mb);
519     break;
520   case DLONG :
521     while ((MemProg_current(cur.mb) & 0x3)) {
522       mi = new_MemInst(MemProg_current(cur.mb),7,0);
523       MemInst_setNumValue(mi,0);
524       MemProg_addInst(cur.mb,mi);
525       MemProg_incAddr(cur.mb);
526     }
527 
528     mi = new_MemInst(MemProg_current(cur.mb),7,0);
529     MemInst_setNumValue(mi,(v&0xff));
530     MemProg_addInst(cur.mb,mi);
531     MemProg_incAddr(cur.mb);
532 
533     mi = new_MemInst(MemProg_current(cur.mb),7,0);
534     MemInst_setNumValue(mi,((v>>8)&0xff));
535     MemProg_addInst(cur.mb,mi);
536     MemProg_incAddr(cur.mb);
537 
538     mi = new_MemInst(MemProg_current(cur.mb),7,0);
539     MemInst_setNumValue(mi,((v>>16)&0xff));
540     MemProg_addInst(cur.mb,mi);
541     MemProg_incAddr(cur.mb);
542 
543     mi = new_MemInst(MemProg_current(cur.mb),7,0);
544     MemInst_setNumValue(mi,((v>>24)&0xff));
545     MemProg_addInst(cur.mb,mi);
546     MemProg_incAddr(cur.mb);
547     break;
548   default :
549     yyerror("Bogonic internal code %d in ycDataNum",cur.dtype);
550     break;
551   }
552 
553 }
554 
ycDataLit(char * v)555 void ycDataLit(char *v)
556 {
557   yyerror("Unsupported use of literal in data value.");
558 }
559 
ycDataStr(char * v)560 void ycDataStr(char *v)
561 {
562   for (;*v;v++) {
563     ycDataNum(*v);
564   }
565 }
566 
ycBeginProc(char * pname)567 void ycBeginProc(char *pname)
568 {
569   if (cur.proc_name)
570     yyerror("Nested procedures not allowed.  Currently in '%s'",cur.proc_name);
571 
572   ycMLabel(pname);
573   cur.proc_name = pname;
574 }
575 
ycEndProc()576 void ycEndProc()
577 {
578   cur.proc_name = 0;
579 }
580 
ycRegRecord(char * name,int value)581 void ycRegRecord(char *name,int value)
582 {
583   extern struct lex_keywordentry lex_regs[];
584   extern int num_lex_regs;
585   int i,j;
586 
587   for (i = 0;i < num_lex_regs;i++) {
588     if (ycKeyCmp(lex_regs[i].Keyword,name) >= 0) break;
589   }
590 
591   for (j = num_lex_regs;j > i;j--)
592     lex_regs[j] = lex_regs[j-1];
593   num_lex_regs++;
594 
595   lex_regs[i].Keyword = strdup(name);
596   lex_regs[i].Value = value;
597 }
598 
ParseFile(char * fileName)599 void ParseFile(char *fileName)
600 {
601   FILE *f;
602 
603   if (!(f = fopen(fileName,"r"))) {
604     fprintf(stderr,"Failed to open input file '%s'\n",fileName);
605     exit(1);
606   }
607 
608   strcpy(ycFileName,fileName);
609   ycLineNumber = 1;
610 
611   yyrestart(f);
612   BeginBA();
613   yyparse();
614   fclose(f);
615 
616   if (yyErrorCount)
617     exit(1);
618 }
619 
Number_print(Number * N,FILE * f)620 void Number_print(Number *N,FILE *f)
621 {
622   switch (N->ntype) {
623   case NT_VALUE :
624     printf("<%d>",N->v.d);
625     break;
626   case NT_SYMBOL :
627     printf("<%s>",N->v.s);
628     break;
629   case NT_RELSYMBOL :
630     printf("<%s+%d>",N->v.s,N->offset);
631     break;
632   }
633 }
634 
Number_copy(Number * sn)635 Number *Number_copy(Number *sn)
636 {
637   Number *dn = new(Number);
638 
639   *dn = *sn;
640 
641   return dn;
642 }
643 
644