1 /* 2 * Copyright (c) 1982 Regents of the University of California 3 * @(#)as.h 4.12 06/10/83 4 */ 5 #ifdef VMS 6 # define vax 1 7 # define VAX 1 8 #endif VMS 9 10 #define reg register 11 12 #include <sys/types.h> 13 #ifdef UNIX 14 15 #ifdef FLEXNAMES 16 # include <a.out.h> 17 # include <stab.h> 18 #else not FLEXNAMES 19 # define ONLIST 20 # include "a.out.h" 21 # include <stab.h> 22 #endif FLEXNAMES 23 24 #endif UNIX 25 #ifdef VMS 26 27 #ifdef UNIXDEVEL 28 # include <a.out.h> 29 #else not UNIXDEVEL 30 # include <aout.h> 31 #endif not UNIXDEVEL 32 33 #endif VMS 34 35 #define readonly 36 #define NINST 300 37 38 #define NEXP 20 /* max number of expr. terms per instruction */ 39 #define NARG 6 /* max number of args per instruction */ 40 #define NHASH 1103 /* hash table is dynamically extended */ 41 #define TNAMESIZE 32 /* maximum length of temporary file names */ 42 #define NLOC 4 /* number of location ctrs */ 43 44 #ifdef UNIX 45 # ifndef FLEXNAMES 46 # ifndef NCPS 47 # define NCPS 8 /* number of characters per symbol*/ 48 # endif 49 # else 50 # ifdef NCPS 51 # undef NCPS 52 # endif 53 # define NCPS 4096 /* needed to allocate yytext */ 54 # endif 55 # endif UNIX 56 57 # ifdef VMS 58 # ifdef NCPS 59 # undef NCPS 60 # endif NCPS 61 # define NCPS 15 62 # endif VMS 63 64 /* 65 * Symbol types 66 */ 67 #define XUNDEF 0x0 68 #define XABS 0x2 69 #define XTEXT 0x4 70 #define XDATA 0x6 71 #define XBSS 0x8 72 73 #define XXTRN 0x1 74 #define XTYPE 0x1E 75 76 #define XFORW 0x20 /* Was forward-referenced when undefined */ 77 78 #define ERR (-1) 79 #define NBPW 32 /* Bits per word */ 80 81 #define AMASK 017 82 83 /* 84 * Actual argument syntax types 85 */ 86 #define AREG 1 /* %r */ 87 #define ABASE 2 /* (%r) */ 88 #define ADECR 3 /* -(%r) */ 89 #define AINCR 4 /* (%r)+ */ 90 #define ADISP 5 /* expr(%r) */ 91 #define AEXP 6 /* expr */ 92 #define AIMM 7 /* $ expr */ 93 #define ASTAR 8 /* * */ 94 #define AINDX 16 /* [%r] */ 95 /* 96 * Definitions for the things found in ``instrs'' 97 */ 98 #define INSTTAB 1 99 #include "instrs.h" 100 101 /* 102 * Tells outrel what it is relocating 103 * RELOC_PCREL is an implicit argument to outrel; it is or'ed in 104 * with a TYPX 105 */ 106 #define RELOC_PCREL (1<<TYPLG) 107 /* 108 * reference types for loader 109 */ 110 #define PCREL 1 111 #define LEN1 2 112 #define LEN2 4 113 #define LEN4 6 114 #define LEN8 8 115 #define LEN16 10 116 117 extern int reflen[]; /* {LEN*+PCREL} ==> number of bytes */ 118 extern int lgreflen[]; /* {LEN*+PCREL} ==> lg number of bytes */ 119 extern int len124[]; /* {1,2,4,8,16} ==> {LEN1, LEN2, LEN4, LEN8} */ 120 extern char mod124[]; /* {1,2,4,8,16} ==> {bits to construct operands */ 121 extern int type_124[]; /* {1,2,4,8,16} ==> {TYPB,TYPW,TYPL,TYPQ,TYPO} */ 122 extern int ty_NORELOC[]; /* {TYPB..TYPH} ==> {1 if relocation not OK */ 123 extern int ty_float[]; /* {TYPB..TYPH} ==> {1 if floating number */ 124 extern int ty_LEN[]; /* {TYPB..TYPH} ==> {LEN1..LEN16} */ 125 extern int ty_nbyte[]; /* {TYPB..TYPH} ==> {1,2,4,8,16} */ 126 extern int ty_nlg[]; /* {TYPB..TYPH} ==> lg{1,2,4,8,16} */ 127 extern char *ty_string[]; /* {TYPB..TYPH} ==> printable */ 128 129 #define TMPC 7 130 #define HW 0x1 131 #define FW 0x3 132 #define DW 0x7 133 #define OW 0xF 134 135 #ifdef VMS 136 # define PAGRND 0x1FFL 137 #endif VMS 138 139 #define round(x,y) (((x)+(y)) & ~(y)) 140 141 #define STABTYPS 0340 142 #define STABFLAG 0200 143 144 /* 145 * Follows are the definitions for the symbol table tags, which are 146 * all unsigned characters.. 147 * High value tags are generated by the asembler for internal 148 * use. 149 * Low valued tags are the parser coded tokens the scanner returns. 150 * There are several pertinant bounds in this ordering: 151 * a) Symbols greater than JXQUESTIONABLE 152 * are used by the jxxx bumper, indicating that 153 * the symbol table entry is a jxxx entry 154 * that has yet to be bumped. 155 * b) Symbols greater than IGNOREBOUND are not 156 * bequeathed to the loader; they are truly 157 * for assembler internal use only. 158 * c) Symbols greater than OKTOBUMP represent 159 * indices into the program text that should 160 * be changed in preceeding jumps or aligns 161 * must get turned into their long form. 162 */ 163 164 #define TAGMASK 0xFF 165 166 # define JXACTIVE 0xFF /*jxxx size unknown*/ 167 # define JXNOTYET 0xFE /*jxxx size known, but not yet expanded*/ 168 # define JXALIGN 0xFD /*align jxxx entry*/ 169 # define JXINACTIVE 0xFC /*jxxx size known and expanded*/ 170 171 #define JXQUESTIONABLE 0xFB 172 173 # define JXTUNNEL 0xFA /*jxxx that jumps to another*/ 174 # define OBSOLETE 0xF9 /*erroneously entered symbol*/ 175 176 #define IGNOREBOUND 0xF8 /*symbols greater than this are ignored*/ 177 # define STABFLOATING 0xF7 178 # define LABELID 0xF6 179 180 #define OKTOBUMP 0xF5 181 # define STABFIXED 0xF4 182 183 /* 184 * astoks.h contains reserved word codings the parser should 185 * know about 186 */ 187 #include "astoks.h" 188 189 /* 190 * The structure for one symbol table entry. 191 * Symbol table entries are used for both user defined symbols, 192 * and symbol slots generated to create the jxxx jump from 193 * slots. 194 * Caution: the instructions are stored in a shorter version 195 * of the struct symtab, using all fields in sym_nm and 196 * tag. The fields used in sym_nm are carefully redeclared 197 * in struct Instab and struct instab (see below). 198 * If struct nlist gets changed, then Instab and instab may 199 * have to be changed. 200 */ 201 202 struct symtab{ 203 struct nlist s_nm; 204 u_char s_tag; /* assembler tag */ 205 u_char s_ptype; /* if tag == NAME */ 206 u_char s_jxoveralign; /* if a JXXX, jumped over align */ 207 short s_index; /* which segment */ 208 struct symtab *s_dest; /* if JXXX, where going to */ 209 #ifdef DJXXX 210 short s_jxline; /* source line of the jump from */ 211 #endif 212 }; 213 /* 214 * Redefinitions of the fields in symtab for 215 * use when the symbol table entry marks a jxxx instruction. 216 */ 217 #define s_jxbump s_ptype /* tag == JX..., how far to expand */ 218 #define s_jxfear s_desc /* how far needs to be bumped */ 219 /* 220 * Redefinitions of fields in the struct nlist for symbols so that 221 * one saves typing, and so that they conform 222 * with the old naming conventions. 223 */ 224 #ifdef FLEXNAMES 225 #define s_name s_nm.n_un.n_name /* name pointer */ 226 #define s_nmx s_nm.n_un.n_strx /* string table index */ 227 #else not FLEXNAMES 228 #define s_name s_nm.n_name 229 #endif 230 #define s_type s_nm.n_type /* type of the symbol */ 231 #define s_other s_nm.n_other /* other information for sdb */ 232 #define s_desc s_nm.n_desc /* type descriptor */ 233 #define s_value s_nm.n_value /* value of the symbol, or sdb delta */ 234 235 struct instab{ 236 struct nlist s_nm; /* instruction name, type (opcode) */ 237 u_char s_tag; 238 u_char s_eopcode; 239 char s_pad[2]; /* round to 20 bytes */ 240 }; 241 typedef struct instab *Iptr; 242 /* 243 * The fields nm.n_desc and nm.n_value total 6 bytes; this is 244 * just enough for the 6 bytes describing the argument types. 245 * We use a macro to define access to these 6 bytes, assuming that 246 * they are allocated adjacently. 247 * IF THE FORMAT OF STRUCT nlist CHANGES, THESE MAY HAVE TO BE CHANGED. 248 * 249 * Instab is cleverly declared to look very much like the combination of 250 * a struct symtab and a struct nlist. 251 */ 252 /* 253 * With the 1981 VAX architecture reference manual, 254 * DEC defined and named two byte opcodes. 255 * In addition, DEC defined four new one byte instructions for 256 * queue manipulation. 257 * The assembler was patched in 1982 to reflect this change. 258 * 259 * The two byte opcodes are preceded with an escape byte 260 * (usually an ESCD) and an opcode byte. 261 * For one byte opcodes, the opcode is called the primary opcode. 262 * For two byte opcodes, the second opcode is called the primary opcode. 263 * 264 * We store the primary opcode in I_popcode, 265 * and the escape opcode in I_eopcode. 266 * 267 * For one byte opcodes in the basic arhitecture, 268 * I_eopcode is CORE 269 * For one byte opcodes in the new architecture definition, 270 * I_eopcode is NEW 271 * For the two byte opcodes, I_eopcode is the escape byte. 272 * 273 * The assembler checks if a NEW or two byte opcode is used, 274 * and issues a warning diagnostic. 275 */ 276 /* 277 * For upward compatability reasons, we can't have the two opcodes 278 * forming an operator specifier byte(s) be physically adjacent 279 * in the instruction table. 280 * We define a structure and a constructor that is used in 281 * the instruction generator. 282 */ 283 struct Opcode{ 284 u_char Op_eopcode; 285 u_char Op_popcode; 286 }; 287 288 #define BADPOINT 0xAAAAAAAA 289 /* 290 * See if a structured opcode is bad 291 */ 292 #define ITABCHECK(o) ((itab[o.Op_eopcode] != (Iptr*)BADPOINT) && (itab[o.Op_eopcode][o.Op_popcode] != (Iptr)BADPOINT)) 293 /* 294 * Index the itab by a structured opcode 295 */ 296 #define ITABFETCH(o) itab[o.Op_eopcode][o.Op_popcode] 297 298 struct Instab{ 299 #ifdef FLEXNAMES 300 char *I_name; 301 #else not FLEXNAMES 302 char I_name[NCPS]; 303 #endif 304 u_char I_popcode; /* basic op code */ 305 char I_nargs; 306 char I_args[6]; 307 u_char I_s_tag; 308 u_char I_eopcode; 309 char I_pad[2]; /* round to 20 bytes */ 310 }; 311 /* 312 * Redefinitions of fields in the struct nlist for instructions so that 313 * one saves typing, and conforms to the old naming conventions 314 */ 315 #define i_popcode s_nm.n_type /* use the same field as symtab.type */ 316 #define i_eopcode s_eopcode 317 #define i_nargs s_nm.n_other /* number of arguments */ 318 #define fetcharg(ptr, n) ((struct Instab *)ptr)->I_args[n] 319 320 struct arg { /*one argument to an instruction*/ 321 char a_atype; 322 char a_areg1; 323 char a_areg2; 324 char a_dispsize; /*usually d124, unless have B^, etc*/ 325 struct exp *a_xp; 326 }; 327 /* 328 * Definitions for numbers and expressions. 329 */ 330 #include "asnumber.h" 331 struct exp { 332 Bignum e_number; /* 128 bits of #, plus tag */ 333 char e_xtype; 334 char e_xloc; 335 struct symtab *e_xname; 336 }; 337 #define e_xvalue e_number.num_num.numIl_int.Il_long 338 339 #define MINLIT 0 340 #define MAXLIT 63 341 342 #define MINBYTE -128 343 #define MAXBYTE 127 344 #define MINUBYTE 0 345 #define MAXUBYTE 255 346 347 #define MINWORD -32768 348 #define MAXWORD 32767 349 #define MINUWORD 0 350 #define MAXUWORD 65535 351 352 #define ISLIT(x) (((x) >= MINLIT) && ((x) <= MAXLIT)) 353 #define ISBYTE(x) (((x) >= MINBYTE) && ((x) <= MAXBYTE)) 354 #define ISUBYTE(x) (((x) >= MINUBYTE) && ((x) <= MAXUBYTE)) 355 #define ISWORD(x) (((x) >= MINWORD) && ((x) <= MAXWORD)) 356 #define ISUWORD(x) (((x) >= MINUWORD) && ((x) <= MAXUWORD)) 357 358 extern struct arg arglist[NARG]; /*building operands in instructions*/ 359 extern struct exp explist[NEXP]; /*building up a list of expressions*/ 360 extern struct exp *xp; /*current free expression*/ 361 /* 362 * Communication between the scanner and the jxxx handlers. 363 * lastnam: the last name seen on the input 364 * lastjxxx: pointer to the last symbol table entry for 365 * a jump from 366 */ 367 extern struct symtab *lastnam; 368 extern struct symtab *lastjxxx; 369 370 #ifdef VMS 371 extern char *vms_obj_ptr; /* object buffer pointer */ 372 extern char sobuf[]; /* object buffer */ 373 extern int objfil; /* VMS object file descriptor */ 374 #endif VMS 375 376 /* 377 * Lgensym is used to make up funny names for local labels. 378 * lgensym[i] is the current funny number to put after 379 * references to if, lgensym[i]-1 is for ib. 380 * genref[i] is set when the label is referenced before 381 * it is defined (i.e. 2f) so that we can be sure these 382 * labels are always defined to avoid weird diagnostics 383 * from the loader later. 384 */ 385 extern int lgensym[10]; 386 extern char genref[10]; 387 388 extern char tmpn1[TNAMESIZE]; /* Interpass temporary */ 389 extern struct exp *dotp; /* the current dot location */ 390 extern int loctr; 391 392 extern struct exec hdr; /* a.out header */ 393 extern u_long tsize; /* total text size */ 394 extern u_long dsize; /* total data size */ 395 extern u_long trsize; /* total text relocation size */ 396 extern u_long drsize; /* total data relocation size */ 397 extern u_long datbase; /* base of the data segment */ 398 /* 399 * Bitoff and bitfield keep track of the packing into 400 * bytes mandated by the expression syntax <expr> ':' <expr> 401 */ 402 extern int bitoff; 403 extern long bitfield; 404 405 /* 406 * The lexical analyzer builds up symbols in yytext. Lookup 407 * expects its argument in this buffer 408 */ 409 extern char yytext[NCPS+2]; /* text buffer for lexical */ 410 /* 411 * Variables to manage the input assembler source file 412 */ 413 extern int lineno; /*the line number*/ 414 extern char *dotsname; /*the name of the as source*/ 415 416 extern FILE *tmpfil; /* interpass communication*/ 417 418 extern int passno; /* 1 or 2 */ 419 420 extern int anyerrs; /*errors as'ing arguments*/ 421 extern int anywarnings; /*warnings as'ing arguments*/ 422 extern int silent; /*don't mention the errors*/ 423 extern int savelabels; /*save labels in a.out*/ 424 extern int orgwarn; /* questionable origin ? */ 425 extern int useVM; /*use virtual memory temp file*/ 426 extern int jxxxJUMP; /*use jmp instead of brw for jxxx */ 427 extern int readonlydata; /*initialized data into text space*/ 428 extern int nGHnumbers; /* GH numbers used */ 429 extern int nGHopcodes; /* GH opcodes used */ 430 extern int nnewopcodes; /* new opcodes used */ 431 #ifdef DEBUG 432 extern int debug; 433 extern int toktrace; 434 #endif 435 /* 436 * Information about the instructions 437 */ 438 extern struct instab **itab[NINST]; /*maps opcodes to instructions*/ 439 extern readonly struct Instab instab[]; 440 441 extern int curlen; /*current literal storage size*/ 442 extern int d124; /*current pointer storage size*/ 443 444 struct symtab **lookup(); /*argument in yytext*/ 445 struct symtab *symalloc(); 446 447 char *Calloc(); 448 char *ClearCalloc(); 449 450 #define outb(val) {dotp->e_xvalue++; if (passno==2) bputc((val), (txtfil));} 451 452 #define outs(cp, lg) dotp->e_xvalue += (lg); if (passno == 2) bwrite((cp), (lg), (txtfil)) 453 454 #ifdef UNIX 455 #define Outb(o) outb(o) 456 #endif UNIX 457 458 #ifdef VMS 459 #define Outb(o) {*vms_obj_ptr++=-1;*vms_obj_ptr++=(char)o;dotp->e_xvalue+=1;} 460 #endif VMS 461 462 /* 463 * Most of the time, the argument to flushfield is a power of two constant, 464 * the calculations involving it can be optimized to shifts. 465 */ 466 #define flushfield(n) if (bitoff != 0) Flushfield( ( (bitoff+n-1) /n ) * n) 467 468 /* 469 * The biobuf structure and associated routines are used to write 470 * into one file at several places concurrently. Calling bopen 471 * with a biobuf structure sets it up to write ``biofd'' starting 472 * at the specified offset. You can then use ``bwrite'' and/or ``bputc'' 473 * to stuff characters in the stream, much like ``fwrite'' and ``fputc''. 474 * Calling bflush drains all the buffers and MUST be done before exit. 475 */ 476 struct biobuf { 477 short b_nleft; /* Number free spaces left in b_buf */ 478 /* Initialize to be less than BUFSIZ initially, to boundary align in file */ 479 char *b_ptr; /* Next place to stuff characters */ 480 char b_buf[BUFSIZ]; /* The buffer itself */ 481 off_t b_off; /* Current file offset */ 482 struct biobuf *b_link; /* Link in chain for bflush() */ 483 }; 484 #define bputc(c,b) ((b)->b_nleft ? (--(b)->b_nleft, *(b)->b_ptr++ = (c)) \ 485 : bflushc(b, c)) 486 #define BFILE struct biobuf 487 488 extern BFILE *biobufs; /* head of the block I/O buffer chain */ 489 extern int biofd; /* file descriptor for block I/O file */ 490 extern off_t boffset; /* physical position in logical file */ 491 492 /* 493 * For each of the named .text .data segments 494 * (introduced by .text <expr>), we maintain 495 * the current value of the dot, and the BFILE where 496 * the information for each of the segments is placed 497 * during the second pass. 498 */ 499 extern struct exp usedot[NLOC + NLOC]; 500 extern BFILE *usefile[NLOC + NLOC]; 501 extern BFILE *txtfil;/* file for text and data: into usefile */ 502 /* 503 * Relocation information for each segment is accumulated 504 * seperately from the others. Writing the relocation 505 * information is logically viewed as writing to one 506 * relocation saving file for each segment; physically 507 * we have a bunch of buffers allocated internally that 508 * contain the relocation information. 509 */ 510 struct relbufdesc *rusefile[NLOC + NLOC]; 511 struct relbufdesc *relfil; 512