do it anyway!
[unix-history] / usr / src / old / as.tahoe / asparse.c
CommitLineData
f7cb25e0
SL
1/*
2 * Copyright (c) 1982 Regents of the University of California
3 */
4#ifndef lint
5static char sccsid[] = "@(#)asparse.c 4.17 7/1/83";
6#endif not lint
7
8#include <stdio.h>
9#include "as.h"
10#include "asscan.h"
11#include "assyms.h"
12#include "asexpr.h"
13
14int lgensym[10];
15char genref[10];
16
17long bitfield;
18int bitoff;
19int curlen; /* current length of literals */
20int printblank;
21
22/*
23 * The following three variables are communication between various
24 * modules to special case a number of things. They are properly
25 * categorized as hacks.
26 */
27extern struct symtab *lastnam;/*last name seen by the lexical analyzer*/
28int exprisname; /*last factor in an expression was a name*/
29int droppedLP; /*one is analyzing an expression beginning with*/
30 /*a left parenthesis, which has already been*/
31 /*shifted. (Used to parse (<expr>)(rn)*/
32
33char yytext[NCPName+2]; /*the lexical image*/
34int yylval; /*the lexical value; sloppy typing*/
35u_char yyopcode; /* lexical value for an opcode */
36Bignum yybignum; /* lexical value for a big number */
37int num_type; /* type of bignums */
38/*
39 * Expression and argument managers
40 */
41struct exp *xp; /*next free expression slot, used by expr.c*/
42struct exp explist[NEXP]; /*max of 20 expressions in one opcode*/
43struct arg arglist[NARG]; /*building up operands in instructions*/
44/*
45 * Sets to accelerate token discrimination
46 */
47char tokensets[(LASTTOKEN) - (FIRSTTOKEN) + 1];
48
49static char UDotsname[64]; /*name of the assembly source*/
50
51yyparse()
52{
53 reg struct exp *locxp;
54 /*
55 * loc1xp and ptrloc1xp are used in the
56 * expression lookahead
57 */
58 struct exp *loc1xp; /*must be non register*/
59 struct exp **ptrloc1xp = & loc1xp;
60 struct exp *pval; /*hacking expr:expr*/
61
62 reg struct symtab *np;
63 reg int argcnt;
64
65 reg inttoktype val; /*what yylex gives*/
66 reg inttoktype auxval; /*saves val*/
67
68 reg struct arg *ap; /*first free argument*/
69
70 reg struct symtab *p;
71 reg struct symtab *stpt;
72
73 struct strdesc *stringp; /*handles string lists*/
74
75 int regno; /*handles arguments*/
76 int *ptrregno = &regno;
77 int sawmul; /*saw * */
78 int sawindex; /*saw [rn]*/
79 int sawsize;
80 int seg_type; /*the kind of segment: data or text*/
81 int seg_number; /*the segment number*/
82 int space_value; /*how much .space needs*/
83 int fill_rep; /*how many reps for .fill */
84 int rep_fill; /*the same - temprary */
85 int fill_size; /*how many bytes for .fill */
86
87 int field_width; /*how wide a field is to be*/
88 int field_value; /*the value to stuff in a field*/
89 char *stabname; /*name of stab dealing with*/
90 ptrall stabstart; /*where the stab starts in the buffer*/
91 int reloc_how; /* how to relocate expressions */
92 int incasetable; /* set if in a case table */
93 int j, k;
94 char ch;
95 int length; /* for printout */
96 union twolong
97 {
98 long lpart[2];
99 char strpart [8];
100 }fillval;
101
102
103 incasetable = 0;
104 xp = explist;
105 ap = arglist;
106
107 val = yylex();
108
109 while (val != PARSEEOF){ /* primary loop */
110
111 while (INTOKSET(val, LINSTBEGIN)){
112 if (val == INT) {
113 int i = ((struct exp *)yylval)->e_xvalue;
114 shift;
115 if (val != COLON){
116 yyerror("Local label %d is not followed by a ':' for a label definition",
117 i);
118 goto errorfix;
119 }
120 if (i < 0 || i > 9) {
121 yyerror("Local labels are 0-9");
122 goto errorfix;
123 }
124 (void)sprintf(yytext, "L%d\001%d", i, lgensym[i]);
125 lgensym[i]++;
126 genref[i] = 0;
127 yylval = (int)*lookup(passno == 1);
128 val = NAME;
129 np = (struct symtab *)yylval;
130 goto restlab;
131 }
132 if (val == NL){
133 lineno++;
134 if (liston && (passno == 2) && (! endofsource))
135 {
136 /* printing previous line & layout */
137 length = strlen (layout);
138 fprintf (listfile, "%*.*s", LHEAD, LHEAD, layout);
139 if (length <= LHEAD+LLEN)
140 j = LLEN;
141 else { /* break line at last blank */
142 j = LHEAD+LLEN;
143 while(j>LHEAD && layout[j]!= ' ')
144 j--;
145 if(j == LHEAD)
146 j = LLEN;
147 else
148 j -= LHEAD;
149 }
150 k = LHEAD+j;
151 fprintf (listfile, "%-*.*s", LLEN, j, &layout[LHEAD]);
152 fprintf (listfile, " ");
153 do {
154 ch = getc (source);
155 putc (ch, listfile);
156 } while (ch != '\n');
157 while (k < length)
158 {
159 fprintf (listfile, "%*s", LHEAD, "");
160 /* break line at last blank */
161 if(layout[k] == ' ')
162 k++;
163 if((j = k+LLEN) >= length)
164 j = length;
165 else
166 while(j>k && layout[j]!= ' ')
167 j--;
168 if(j == k)
169 j = LLEN;
170 else
171 j -= k;
172 fprintf (listfile, "%-*.*s\n", j, j, &layout[k]);
173 k += j;
174 }
175 k = 0;
176 while (layout[k] != '\0')
177 layout[k++] = '\0';
178 ch = getc (source);
179 if (ch == EOF)
180 {
181 if (ind == ninfiles)
182 endofsource = 1;
183 else
184 {
185 source = fopen (innames[ind++], "r");
186 lineno = 1;
187 }
188 }
189 else
190 ungetc (ch, source);
191 layoutpos = layout;
192 sprintf (layoutpos, "%4ld ", lineno);
193 layoutpos += 6;
194 long_out (dotp->e_xvalue);
195 if (dotp->e_xvalue >= datbase)
196 sprintf (layoutpos," * ");
197 else
198 sprintf (layoutpos," ");
199 layoutpos += 4;
200 }
201 shift;
202 } else
203 if (val == SEMI)
204 shift;
205 else { /*its a name, so we have a label or def */
206 if (val != NAME){
207 ERROR("Name expected for a label");
208 }
209 np = (struct symtab *)yylval;
210 shiftover(NAME);
211 if (val != COLON) {
212 yyerror("\"%s\" is not followed by a ':' for a label definition",
213 FETCHNAME(np));
214 goto errorfix;
215 }
216restlab:
217 shift;
218 flushfield(NBPW/4);
219 if ((np->s_type&XTYPE)!=XUNDEF) {
220 if( (np->s_type&XTYPE)!=dotp->e_xtype
221 || np->s_value!=dotp->e_xvalue
222 || ( (passno==1)
223 &&(np->s_index != dotp->e_xloc)
224 )
225 ){
226#ifndef DEBUG
227 if (FETCHNAME(np)[0] != 'L')
228#endif not DEBUG
229 {
230 if (passno == 1)
231 yyerror("%s redefined",
232 FETCHNAME(np));
233 else
234 yyerror("%s redefined: PHASE ERROR, 1st: %d, 2nd: %d",
235 FETCHNAME(np),
236 np->s_value,
237 dotp->e_xvalue);
238 }
239 }
240 }
241 np->s_type &= ~(XTYPE|XFORW);
242 np->s_type |= dotp->e_xtype;
243 np->s_value = dotp->e_xvalue;
244 if (passno == 1){
245 np->s_index = dotp-usedot;
246 if (FETCHNAME(np)[0] == 'L'){
247 nlabels++;
248 }
249 np->s_tag = LABELID;
250 }
251 } /*end of this being a label*/
252 } /*end of to consuming all labels, NLs and SEMIS */
253
254 xp = explist;
255 ap = arglist;
256
257 /*
258 * process the INSTRUCTION body
259 */
260 switch(val){
261
262 default:
263 ERROR("Unrecognized instruction or directive");
264
265 case IABORT:
266 shift;
267 sawabort();
268 /*NOTREACHED*/
269 break;
270
271 case PARSEEOF:
272 tokptr -= sizeof(bytetoktype);
273 *tokptr++ = VOID;
274 tokptr[1] = VOID;
275 tokptr[2] = PARSEEOF;
276 break;
277
278 case IFILE:
279 shift;
280 stringp = (struct strdesc *)yylval;
281 shiftover(STRING);
282 dotsname = &UDotsname[0];
283 movestr(dotsname, stringp->sd_string,
284 min(stringp->sd_strlen, sizeof(UDotsname)));
285 break;
286
287 case ILINENO:
288 shift; /*over the ILINENO*/
289 expr(locxp, val);
290 lineno = locxp->e_xvalue;
291 break;
292
293 case ISET: /* .set <name> , <expr> */
294 shift;
295 np = (struct symtab *)yylval;
296 shiftover(NAME);
297 shiftover(CM);
298 expr(locxp, val);
299 np->s_type &= (XXTRN|XFORW);
300 np->s_type |= locxp->e_xtype&(XTYPE|XFORW);
301 np->s_value = locxp->e_xvalue;
302 if (passno==1)
303 np->s_index = locxp->e_xloc;
304 if ((locxp->e_xtype&XTYPE) == XUNDEF)
305 yyerror("Illegal set?");
306 break;
307
308 case ILSYM: /*.lsym name , expr */
309 shift;
310 np = (struct symtab *)yylval;
311 shiftover(NAME);
312 shiftover(CM);
313 expr(locxp, val);
314 /*
315 * Build the unique occurance of the
316 * symbol.
317 * The character scanner will have
318 * already entered it into the symbol
319 * table, but we should remove it
320 */
321 if (passno == 1){
322 stpt = (struct symtab *)symalloc();
323 stpt->s_name = np->s_name;
324 np->s_tag = OBSOLETE; /*invalidate original */
325 nforgotten++;
326 np = stpt;
327 if ( (locxp->e_xtype & XTYPE) != XABS)
328 yyerror("Illegal second argument to lsym");
329 np->s_value = locxp->e_xvalue;
330 np->s_type = XABS;
331 np->s_tag = ILSYM;
332 }
333 break;
334
335 case IGLOBAL: /*.globl <name> */
336 shift;
337 np = (struct symtab *)yylval;
338 shiftover(NAME);
339 np->s_type |= XXTRN;
340 break;
341
342 case IDATA: /*.data [ <expr> ] */
343 case ITEXT: /*.text [ <expr> ] */
344 incasetable = 0;
345 seg_type = -val;
346 shift;
347 if (INTOKSET(val, EBEGOPS+YUKKYEXPRBEG+SAFEEXPRBEG)){
348 expr(locxp, val);
349 seg_type = -seg_type; /*now, it is positive*/
350 }
351
352 if (seg_type < 0) { /*there wasn't an associated expr*/
353 seg_number = 0;
354 seg_type = -seg_type;
355 } else {
356 if ( ((locxp->e_xtype & XTYPE) != XABS) /* tekmdp */
357 || (seg_number = locxp->e_xvalue) >= NLOC) {
358 yyerror("illegal location counter");
359 seg_number = 0;
360 }
361 }
362 if (seg_type == IDATA)
363 seg_number += NLOC;
364 flushfield(NBPW/4);
365 dotp = &usedot[seg_number];
366 if (passno==2) { /* go salt away in pass 2*/
367 txtfil = usefile[seg_number];
368 relfil = rusefile[seg_number];
369 }
370 break;
371
372 /*
373 * Storage filler directives:
374 *
375 * .byte [<exprlist>]
376 *
377 * exprlist: empty | exprlist outexpr
378 * outexpr: <expr> | <expr> : <expr>
379 */
380 case IBYTE: curlen = NBPW/4; goto elist;
381 case IWORD: curlen = NBPW/2; goto elist;
382 case IINT: curlen = NBPW; goto elist;
383 case ILONG: curlen = NBPW; goto elist;
384
385 elist:
386 seg_type = val;
387 shift;
388
389 /*
390 * Expression List processing
391 */
392 if (INTOKSET(val, EBEGOPS+YUKKYEXPRBEG+SAFEEXPRBEG)){
393 do{
394 /*
395 * expression list consists of a list of :
396 * <expr>
397 * <expr> : <expr>
398 * (pack expr2 into expr1 bits
399 */
400 expr(locxp, val);
401 /*
402 * now, pointing at the next token
403 */
404 /* if (val == COLON){ */
405 /* shiftover(COLON); */
406 /* expr(pval, val); */
407 /* if ((locxp->e_xtype & XTYPE) != XABS) */
408 /* yyerror("Width not absolute"); */
409 /* field_width = locxp->e_xvalue; */
410 /* locxp = pval; */
411 /* if (bitoff + field_width > curlen) */
412 /* flushfield(curlen); */
413 /* if (field_width > curlen) */
414 /* yyerror("Expression crosses field boundary"); */
415 /* } else { */
416 field_width = curlen;
417 if (bitoff == 0) printblank = 0;
418 else printblank = 1;
419 flushfield(curlen);
420 if (liston && (passno == 2) && printblank)
421 *layoutpos++ = ' ';
422 /* } */
423
424 if ((locxp->e_xtype & XTYPE) != XABS) {
425 if (bitoff)
426 yyerror("Illegal relocation in field");
427 switch(curlen){
428 case NBPW/4: reloc_how = TYPB; break;
429 case NBPW/2: reloc_how = TYPW; break;
430 case NBPW: reloc_how = TYPL; break;
431 }
432 if (passno == 1){
433 dotp->e_xvalue += ty_nbyte[reloc_how];
434 } else {
435 outrel(locxp, reloc_how);
436 if (liston)
437 *layoutpos++ = ' ';
438 }
439 } else {
440 /*
441 *
442 * See if we are doing a case instruction.
443 * If so, then see if the branch distance,
444 * stored as a word,
445 * is going to loose sig bits.
446 */
447 if (passno == 2 && incasetable){
448 if ( !(ISWORD(locxp->e_xvalue)))
449 yyerror("Case will branch too far");
450 }
451 field_value = locxp->e_xvalue & ( (1L << field_width)-1);
452 bitfield |= field_value << bitoff;
453 bitoff += field_width;
454 }
455 xp = explist;
456 if (auxval = (val == CM))
457 shift;
458 } while (auxval);
459 } /* there existed an expression at all */
460
461 flushfield(curlen);
462 if ( ( curlen == NBPW/4) && bitoff)
463 dotp->e_xvalue ++;
464 break;
465 /*end of case IBYTE, IWORD, ILONG, IINT*/
466
467 case ISPACE: /* .space <expr> */
468 shift;
469 expr(locxp, val);
470 if ((locxp->e_xtype & XTYPE) != XABS) /* tekmdp */
471 yyerror("Space size not absolute");
472 if (locxp->e_xvalue < 0)
473 yyerror("Space size not positive");
474 space_value = locxp->e_xvalue;
475 ospace:
476 flushfield(NBPW/4);
477 {
478 static char spacebuf[128];
479 while (space_value > sizeof(spacebuf)){
480 outs(spacebuf, sizeof(spacebuf));
481 space_value -= sizeof(spacebuf);
482 }
483 outs(spacebuf, space_value);
484 }
485 if (liston && (passno == 2))
486 sprintf (layoutpos, "****");
487 break;
488
489 /*
490 * .fill rep, size, value
491 * repeat rep times: fill size bytes with (truncated) value
492 * size must be between 1 and 8
493 */
494 case IFILL:
495 shift;
496 expr(locxp, val);
497 if ( (locxp->e_xtype & XTYPE) != XABS) /* tekmdp */
498 yyerror("Fill repetition count not absolute");
499 rep_fill = fill_rep = locxp->e_xvalue;
500 shiftover(CM);
501 expr(locxp, val);
502 if ( (locxp->e_xtype & XTYPE) != XABS) /* tekmdp */
503 yyerror("Fill size not absolute");
504 fill_size = locxp->e_xvalue;
505 if (fill_size <= 0 || fill_size > 8)
506 yyerror("Fill count not in in 1..8");
507 shiftover(CM);
508 expr(locxp, val);
509 if (passno == 2 && (locxp->e_xtype & XTYPE) != XABS) /* tekmdp */
510 yyerror("Fill value not absolute");
511 flushfield(NBPW/4);
512 dotp->e_xvalue += fill_rep * fill_size;
513 if (passno == 1) {
514 locxp->e_xvalue += fill_rep * fill_size;
515 } else {
516 fillval.lpart[0] = locxp->e_yvalue;
517 fillval.lpart[1] = locxp->e_xvalue;
518 while (fill_rep-- > 0)
519 bwrite(&(fillval.strpart[8-fill_size]),fill_size,txtfil);
520 if (liston) {
521 while (rep_fill-- > 0)
522 {
523 switch (fill_size)
524 {
525 case 1:
526 byte_out (locxp->e_xvalue);
527 *layoutpos++ = ' ';
528 break;
529 case 2:
530 word_out (locxp->e_xvalue);
531 *layoutpos++ = ' ';
532 break;
533 case 3:
534 byte_out (locxp->e_xvalue >> 16);
535 byte_out (locxp->e_xvalue >> 8);
536 byte_out (locxp->e_xvalue);
537 *layoutpos++ = ' ';
538 break;
539 case 4:
540 long_out (locxp->e_xvalue);
541 *layoutpos++ = ' ';
542 break;
543 case 5:
544 byte_out (locxp->e_yvalue);
545 long_out (locxp->e_xvalue);
546 *layoutpos++ = ' ';
547 break;
548 case 6:
549 word_out (locxp->e_yvalue);
550 long_out (locxp->e_xvalue);
551 *layoutpos++ = ' ';
552 break;
553 case 7:
554 byte_out (locxp->e_yvalue >> 16);
555 byte_out (locxp->e_yvalue >> 8);
556 byte_out (locxp->e_yvalue);
557 long_out (locxp->e_xvalue);
558 *layoutpos++ = ' ';
559 break;
560 case 8:
561 long_out (locxp->e_yvalue);
562 long_out (locxp->e_xvalue);
563 *layoutpos++ = ' ';
564 break;
565 }
566 }
567 }
568 }
569 break;
570
571 case IASCII: /* .ascii [ <stringlist> ] */
572 case IASCIZ: /* .asciz [ <stringlist> ] */
573 auxval = val;
574 shift;
575 /*
576 * Code to consume a string list
577 *
578 * stringlist: empty | STRING | stringlist STRING
579 */
580 while (val == STRING){
581 int mystrlen;
582 flushfield(NBPW/4);
583 if (bitoff)
584 dotp->e_xvalue++;
585 stringp = (struct strdesc *)yylval;
586 /*
587 * utilize the string scanner cheat;
588 * the scanner appended a null byte on the string,
589 * but didn't charge it to sd_strlen
590 */
591 mystrlen = stringp->sd_strlen;
592 mystrlen += (auxval == IASCIZ) ? 1 : 0;
593 if (passno == 2){
594 if (stringp->sd_place & STR_CORE){
595 outs(stringp->sd_string, mystrlen);
596 if (liston)
597 {
598 int i;
599 for (i = 0;i < mystrlen; i++)
600 {
601 sprintf (layoutpos, "%02x",
602 stringp->sd_string[i]);
603 layoutpos += 2;
604 }
605 }
606 } else {
607 int i, nread;
608 fseek(strfile, stringp->sd_stroff, 0);
609 for (i = 0; i < mystrlen;/*VOID*/){
610 nread = fread(yytext, 1,
611 min(mystrlen - i,
612 sizeof(yytext)), strfile);
613 outs(yytext, nread);
614 if (liston)
615 {
616 int k;
617 for (k = 0;k < nread; k++)
618 {
619 sprintf (layoutpos,
620 "%02x", yytext[k]);
621 layoutpos += 2;
622 }
623 }
624 i += nread;
625 }
626 }
627 } else {
628 dotp->e_xvalue += mystrlen;
629 }
630 shift; /*over the STRING*/
631 if (val == CM) /*could be a split string*/
632 shift;
633 }
634 break;
635
636 case IORG: /* .org <expr> */
637 shift;
638 expr(locxp, val);
639
640 if ((locxp->e_xtype & XTYPE) == XABS) /* tekmdp */
641 orgwarn++;
642 else if ((locxp->e_xtype & ~XXTRN) != dotp->e_xtype)
643 yyerror("Illegal expression to set origin");
644 if ((unsigned)locxp->e_xvalue < (unsigned)dotp->e_xvalue)
645 {
646 ERROR("Backwards 'org'");
647 }
648 space_value = locxp->e_xvalue - dotp->e_xvalue;
649 goto ospace;
650 break;
651
652/*
653 *
654 * Process stabs. Stabs are created only by the f77
655 * and the C compiler with the -g flag set.
656 * We only look at the stab ONCE, during pass 1, and
657 * virtually remove the stab from the intermediate file
658 * so it isn't seen during pass2. This makes for some
659 * hairy processing to handle labels occuring in
660 * stab entries, but since most expressions in the
661 * stab are integral we save lots of time in the second
662 * pass by not looking at the stabs.
663 * A stab that is tagged floating will be bumped during
664 * the jxxx resolution phase. A stab tagged fixed will
665 * not be be bumped.
666 *
667 * .stab: Old fashioned stabs
668 * .stabn: For stabs without names
669 * .stabs: For stabs with string names
670 * .stabd: For stabs for line numbers or bracketing,
671 * without a string name, without
672 * a final expression. The value of the
673 * final expression is taken to be the current
674 * location counter, and is patched by the 2nd pass
675 *
676 * .stab{<expr>,}*NCPName,<expr>, <expr>, <expr>, <expr>
677 * .stabn <expr>, <expr>, <expr>, <expr>
678 * .stabs STRING, <expr>, <expr>, <expr>, <expr>
679 * .stabd <expr>, <expr>, <expr> # .
680 */
681 case ISTAB:
682 yyerror(".stab directive no longer supported");
683 goto errorfix;
684
685 tailstab:
686 expr(locxp, val);
687 if (! (locxp->e_xvalue & STABTYPS)){
688 yyerror("Invalid type in %s", stabname);
689 goto errorfix;
690 }
691 stpt->s_ptype = locxp->e_xvalue;
692 shiftover(CM);
693 expr(locxp, val);
694 stpt->s_other = locxp->e_xvalue;
695 shiftover(CM);
696 expr(locxp, val);
697 stpt->s_desc = locxp->e_xvalue;
698 shiftover(CM);
699 exprisname = 0;
700 expr(locxp, val);
701 p = locxp->e_xname;
702 if (p == NULL) { /*absolute expr to begin with*/
703 stpt->s_value = locxp->e_xvalue;
704 stpt->s_index = dotp - usedot;
705 if (exprisname){
706 switch(stpt->s_ptype){
707 case N_GSYM:
708 case N_FNAME:
709 case N_RSYM:
710 case N_SSYM:
711 case N_LSYM:
712 case N_PSYM:
713 case N_BCOMM:
714 case N_ECOMM:
715 case N_LENG:
716 stpt->s_tag = STABFIXED;
717 break;
718 default:
719 stpt->s_tag = STABFLOATING;
720 break;
721 }
722 } else
723 stpt->s_tag = STABFIXED;
724 }
725 else { /*really have a name*/
726 stpt->s_dest = locxp->e_xname;
727 stpt->s_index = p->s_index;
728 stpt->s_type = p->s_type | STABFLAG;
729 /*
730 * We will assign a more accruate
731 * guess of locxp's location when
732 * we sort the symbol table
733 * The final value of value is
734 * given by stabfix()
735 */
736/*
737 * For exprs of the form (name + value) one needs to remember locxp->e_xvalue
738 * for use in stabfix. The right place to keep this is in stpt->s_value
739 * however this gets corrupted at an unknown point.
740 * As a bandaid hack the value is preserved in s_desc and s_other (a
741 * short and a char). This destroys these two values and will
742 * be fixed. May 19 ,1983 Alastair Fyfe
743 */
744 if(locxp->e_xvalue) {
745 stpt->s_other = (locxp->e_xvalue >> 16);
746 stpt->s_desc = (locxp->e_xvalue & 0x0000ffff);
747 stpt->s_tag = STABFLOATING;
748 }
749 }
750 /*
751 * tokptr now points at one token beyond
752 * the current token stored in val and yylval,
753 * which are the next tokens after the end of
754 * this .stab directive. This next token must
755 * be either a SEMI or NL, so is of width just
756 * one. Therefore, to point to the next token
757 * after the end of this stab, just back up one..
758 */
759 buildskip(stabstart, (bytetoktype *)tokptr - sizeof(bytetoktype));
760 break; /*end of the .stab*/
761
762 case ISTABDOT:
763 stabname = ".stabd";
764 stpt = (struct symtab *)yylval;
765 /*
766 * We clobber everything after the
767 * .stabd and its pointer... we MUST
768 * be able to get back to this .stabd
769 * so that we can resolve its final value
770 */
771 stabstart = tokptr;
772 shift; /*over the ISTABDOT*/
773 if (passno == 1){
774 expr(locxp, val);
775 if (! (locxp->e_xvalue & STABTYPS)){
776 yyerror("Invalid type in .stabd");
777 goto errorfix;
778 }
779 stpt->s_ptype = locxp->e_xvalue;
780 shiftover(CM);
781 expr(locxp, val);
782 stpt->s_other = locxp->e_xvalue;
783 shiftover(CM);
784 expr(locxp, val);
785 stpt->s_desc = locxp->e_xvalue;
786 /*
787 *
788 * Now, clobber everything but the
789 * .stabd pseudo and the pointer
790 * to its symbol table entry
791 * tokptr points to the next token,
792 * build the skip up to this
793 */
794 buildskip(stabstart, (bytetoktype *)tokptr - sizeof(bytetoktype));
795 }
796 /*
797 * pass 1: Assign a good guess for its position
798 * (ensures they are sorted into right place)/
799 * pass 2: Fix the actual value
800 */
801 stpt->s_value = dotp->e_xvalue;
802 stpt->s_index = dotp - usedot;
803 stpt->s_tag = STABFLOATING; /*although it has no effect in pass 2*/
804 break;
805
806 case ISTABNONE: stabname = ".stabn"; goto shortstab;
807
808 case ISTABSTR: stabname = ".stabs";
809 shortstab:
810 auxval = val;
811 if (passno == 2) goto errorfix;
812 stpt = (struct symtab *)yylval;
813 stabstart = tokptr;
814 (bytetoktype *)stabstart -= sizeof(struct symtab *);
815 (bytetoktype *)stabstart -= sizeof(bytetoktype);
816 shift;
817 if (auxval == ISTABSTR){
818 stringp = (struct strdesc *)yylval;
819 shiftover(STRING);
820 stpt->s_name = (char *)stringp;
821 /*
822 * We want the trailing null included in this string.
823 * We utilize the cheat the string scanner used,
824 * and merely increment the string length
825 */
826 stringp->sd_strlen += 1;
827 shiftover(CM);
828 } else {
829 stpt->s_name = (char *)savestr("\0", 0, STR_BOTH);
830 }
831 goto tailstab;
832 break;
833
834 case ICOMM: /* .comm <name> , <expr> */
835 case ILCOMM: /* .lcomm <name> , <expr> */
836 auxval = val;
837 shift;
838 np = (struct symtab *)yylval;
839 shiftover(NAME);
840 shiftover(CM);
841 expr(locxp, val);
842
843 if ( (locxp->e_xtype & XTYPE) != XABS) /* tekmdp */
844 yyerror("comm size not absolute");
845 if (passno == 1 && (np->s_type&XTYPE) != XUNDEF)
846 yyerror("Redefinition of %s", FETCHNAME(np));
847 if (passno==1) {
848 np->s_value = locxp->e_xvalue;
849 if (auxval == ICOMM)
850 np->s_type |= XXTRN;
851 else {
852 np->s_type &= ~XTYPE;
853 np->s_type |= XBSS;
854 }
855 }
856 break;
857
858 case IALIGN: /* .align <expr> */
859 stpt = (struct symtab *)yylval;
860 shift;
861 expr(locxp, val);
862 if ((dotp->e_xtype & XTYPE) == XDATA)
863 djalign(locxp, stpt);
864 else
865 jalign(locxp, stpt);
866 break;
867
868 case INST0: /* instructions w/o arguments*/
869 incasetable = 0;
870 insout(yyopcode, (struct arg *)0, 0);
871 shift;
872 break;
873
874 case INSTn: /* instructions with arguments*/
875 case IJXXX: /* UNIX style jump instructions */
876 auxval = val;
877 /*
878 * Code to process an argument list
879 */
880 ap = arglist;
881 xp = explist;
882
883 shift; /* bring in the first token for the arg list*/
884
885 for (argcnt = 1; argcnt <= 6; argcnt++, ap++){
886 /*
887 * code to process an argument proper
888 */
889 sawindex = sawmul = sawsize = 0;
890 {
891 switch(val) {
892
893 default:
894 disp:
895 if( !(INTOKSET(val,
896 EBEGOPS
897 +YUKKYEXPRBEG
898 +SAFEEXPRBEG)) ) {
899 ERROR("expression expected");
900 }
901 expr(ap->a_xp,val);
902 overdisp:
903 if ( val == LP || sawsize){
904 shiftover(LP);
905 findreg(regno);
906 shiftover(RP);
907 ap->a_atype = ADISP;
908 ap->a_areg1 = regno;
909 } else {
910 ap->a_atype = AEXP;
911 ap->a_areg1 = 0;
912 }
913 goto index;
914
915 case SIZESPEC:
916 sizespec:
917 sawsize = yylval;
918 shift;
919 goto disp;
920
921 case REG:
922 case REGOP:
923 findreg(regno);
924 ap->a_atype = AREG;
925 ap->a_areg1 = regno;
926 break;
927
928 case MUL:
929 sawmul = 1;
930 shift;
931 if (val == LP) goto base;
932 if (val == LITOP) goto imm;
933 if (val == SIZESPEC) goto sizespec;
934 if (INTOKSET(val,
935 EBEGOPS
936 +YUKKYEXPRBEG
937 +SAFEEXPRBEG)) goto disp;
938 ERROR("expression, '(' or '$' expected");
939 break;
940
941 case LP:
942 base:
943 shift; /*consume the LP*/
944 /*
945 * hack the ambiguity of
946 * movl (expr) (rn), ...
947 * note that (expr) could also
948 * be (rn) (by special hole in the
949 * grammar), which we ensure
950 * means register indirection, instead
951 * of an expression with value n
952 */
953 if (val != REG && val != REGOP){
954 droppedLP = 1;
955 val = exprparse(val, &(ap->a_xp));
956 droppedLP = 0;
957 goto overdisp;
958 }
959 findreg(regno);
960 shiftover(RP);
961 if (val == PLUS){
962 shift;
963 ap->a_atype = AINCR;
964 if (sawmul && regno != 0xE)
965 yyerror ("Autoincrement deferred register must be SP");
966 if (!(sawmul || regno == 0xE))
967 yyerror ("Autoincrement register must be SP");
968 } else
969 ap->a_atype = ABASE;
970 ap->a_areg1 = regno;
971 goto index;
972
973 case LITOP:
974 imm:
975 shift;
976 expr(locxp, val);
977 ap->a_atype = AIMM;
978 ap->a_areg1 = 0;
979 ap->a_xp = locxp;
980 goto index;
981
982 case MP:
983 shift; /* -(reg) */
984 findreg(regno);
985 if (regno != 0xE)
986 yyerror ("Autodecrement register must be SP");
987 shiftover(RP);
988 ap->a_atype = ADECR;
989 ap->a_areg1 = regno;
990 index: /*look for [reg] */
991 if (val == LB){
992 shift;
993 findreg(regno);
994 shiftover(RB);
995 sawindex = 1;
996 ap->a_areg2 = regno;
997 }
998 break;
999
1000 } /*end of the switch to process an arg*/
1001 } /*end of processing an argument*/
1002
1003 if (sawmul){
1004 /*
1005 * Make a concession for *(%r)
1006 * meaning *0(%r)
1007 */
1008 if (ap->a_atype == ABASE) {
1009 ap->a_atype = ADISP;
1010 xp->e_xtype = XABS;
1011 xp->e_number = Znumber;
1012 xp->e_number.num_tag = TYPL;
1013 xp->e_xloc = 0;
1014 ap->a_xp = xp++;
1015 }
1016 ap->a_atype |= ASTAR;
1017 sawmul = 0;
1018 }
1019 if (sawindex){
1020 ap->a_atype |= AINDX;
1021 sawindex = 0;
1022 }
1023 ap->a_dispsize = sawsize == 0 ? d124 : sawsize;
1024 if (val != CM) break;
1025 shiftover(CM);
1026 } /*processing all the arguments*/
1027
1028 if (argcnt > 6){
1029 yyerror("More than 6 arguments");
1030 goto errorfix;
1031 }
1032
1033 /*
1034 * See if this is a case instruction,
1035 * so we can set up tests on the following
1036 * vector of branch displacements
1037 */
1038 if (yyopcode == 0xfc) /* 'casel' instruction */
1039 incasetable++;
1040 else
1041 incasetable = 0;
1042
1043 insout(yyopcode, arglist,
1044 auxval == INSTn ? argcnt : - argcnt);
1045 break;
1046
1047 case IQUAD: num_type = TYPQ; goto bignumlist;
1048 case IFFLOAT: num_type = TYPF; goto bignumlist;
1049 case IDFLOAT: num_type = TYPD;
1050 bignumlist:
1051 /*
1052 * eat a list of non 32 bit numbers.
1053 * IQUAD can, possibly, return
1054 * INT's, if the numbers are "small".
1055 *
1056 * The value of the numbers is coming back
1057 * as an expression, NOT in yybignum.
1058 */
1059 shift; /* over the opener */
1060 if ((val == BIGNUM) || (val == INT)){
1061 do{
1062 if ((val != BIGNUM) && (val != INT)){
1063 ERROR(ty_float[num_type]
1064 ? "floating number expected"
1065 : "integer number expected" );
1066 }
1067 dotp->e_xvalue += ty_nbyte[num_type];
1068 if (passno == 2){
1069 switch (num_type) {
1070 case TYPF:
1071 bwrite(&((struct exp *)yylval)->e_number.num_num.numFf_float.Ff_ulong,
1072 ty_nbyte[num_type], txtfil);
1073 if (liston)
1074 {
1075 long_out(((struct exp *)yylval)->e_number.num_num.numFf_float.Ff_ulong[0]);
1076 *layoutpos++ = ' ';
1077 }
1078 break;
1079 case TYPD:
1080 bwrite(&((struct exp *)yylval)->e_number.num_num.numFd_float.Fd_ulong[0],
1081 sizeof (long), txtfil);
1082 bwrite(&((struct exp *)yylval)->e_number.num_num.numFd_float.Fd_ulong[1],
1083 sizeof (long), txtfil);
1084 if (liston)
1085 {
1086 long_out(((struct exp *)yylval)->e_number.num_num.numIq_int.Iq_ulong[0]);
1087 long_out(((struct exp *)yylval)->e_number.num_num.numIq_int.Iq_ulong[1]);
1088 *layoutpos++ = ' ';
1089 }
1090 break;
1091 case TYPQ:
1092 bwrite(&((struct exp *)yylval)->e_number.num_num.numIq_int.Iq_ulong[1],
1093 sizeof (long), txtfil);
1094 bwrite(&((struct exp *)yylval)->e_number.num_num.numIq_int.Iq_ulong[0],
1095 sizeof (long), txtfil);
1096 if (liston)
1097 {
1098 long_out(((struct exp *)yylval)->e_number.num_num.numIq_int.Iq_ulong[1]);
1099 long_out(((struct exp *)yylval)->e_number.num_num.numIq_int.Iq_ulong[0]);
1100 *layoutpos++ = ' ';
1101 }
1102 break;
1103 }
1104 }
1105 xp = explist;
1106 shift; /* over this number */
1107 if (auxval = (val == CM))
1108 shift; /* over the comma */
1109 } while (auxval); /* as long as there are commas */
1110 }
1111 break;
1112 /* end of the case for initialized big numbers */
1113 } /*end of the switch for looking at each reserved word*/
1114
1115 continue;
1116
1117 errorfix:
1118 /*
1119 * got here by either requesting to skip to the
1120 * end of this statement, or by erroring out and
1121 * wanting to apply panic mode recovery
1122 */
1123 while ( (val != NL)
1124 && (val != SEMI)
1125 && (val != PARSEEOF)
1126 ){
1127 shift;
1128 }
1129 if (val == NL)
1130 lineno++;
1131 shift;
1132
1133 } /*end of the loop to read the entire file, line by line*/
1134
1135} /*end of yyparse*/
1136
1137/*
1138 * Process a register declaration of the form
1139 * % <expr>
1140 *
1141 * Note:
1142 * The scanner has already processed funny registers of the form
1143 * %dd[+-]*, where dd is a decimal number in the range 00 to 15 (optional
1144 * preceding zero digit). If there was any space between the % and
1145 * the digit, the scanner wouldn't have recognized it, so we
1146 * hack it out here.
1147 */
1148inttoktype funnyreg(val, regnoback) /*what the read head will sit on*/
1149 inttoktype val; /*what the read head is sitting on*/
1150 int *regnoback; /*call by return*/
1151{
1152 reg struct exp *locxp;
1153 struct exp *loc1xp;
1154 struct exp **ptrloc1xp = & loc1xp;
1155
1156 expr(locxp, val); /*and leave the current read head with value*/
1157 if ( (passno == 2) &&
1158 ( (locxp->e_xtype & XTYPE) != XABS
1159 || (locxp->e_xvalue < 0)
1160 || (locxp->e_xvalue >= 16)
1161 )
1162 ){
1163 yyerror("Illegal register");
1164 return(0);
1165 }
1166 *regnoback = locxp->e_xvalue;
1167 return(val);
1168}
1169/*
1170 * Shift over error
1171 */
1172shiftoerror(token)
1173 int token;
1174{
1175 char *tok_to_name();
1176 yyerror("%s expected", tok_to_name(token));
1177}
1178
1179/*VARARGS1*/
1180yyerror(s, a1, a2,a3,a4,a5)
1181 char *s;
1182{
1183
1184#define sink stdout
1185
1186 if (anyerrs == 0 && anywarnings == 0 && ! silent)
1187 fprintf(sink, "Assembler:\n");
1188 anyerrs++;
1189 if (silent)
1190 return;
1191 fprintf(sink, "\"%s\", line %d: ", dotsname, lineno);
1192 fprintf(sink, s, a1, a2,a3,a4,a5);
1193 fprintf(sink, "\n");
1194#undef sink
1195}
1196
1197/*VARARGS1*/
1198yywarning(s, a1, a2,a3,a4,a5)
1199 char *s;
1200{
1201#define sink stdout
1202 if (anyerrs == 0 && anywarnings == 0 && ! silent)
1203 fprintf(sink, "Assembler:\n");
1204 anywarnings++;
1205 if (silent)
1206 return;
1207 fprintf(sink, "\"%s\", line %d: WARNING: ", dotsname, lineno);
1208 fprintf(sink, s, a1, a2,a3,a4,a5);
1209 fprintf(sink, "\n");
1210#undef sink
1211}