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