scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

as.h (3475B)


      1 /*
      2  * First 3 bits of flags in segments and symbols are for the
      3  * type of segment
      4  */
      5 enum symflags {
      6 	FREG    = 1 << 0,
      7 	FSECT   = 1 << 1,
      8 	FSYM    = 1 << 2,
      9 	FCOMMON = 1 << 3,
     10 	FEXTERN = 1 << 4,
     11 	FDEF    = 1 << 5,
     12 	FGLOBAL = 1 << 6,
     13 	FABS    = 1 << 7,
     14 };
     15 
     16 enum secflags {
     17 	SREAD  = 1 << 0,
     18 	SWRITE = 1 << 1,
     19 	SEXEC  = 1 << 2,
     20 	SLOAD  = 1 << 3,
     21 	SFILE  = 1 << 4,
     22 	SABS   = 1 << 5,
     23 };
     24 
     25 enum endianess {
     26 	BIG_ENDIAN    = -1,
     27 	LITTLE_ENDIAN = 1
     28 };
     29 
     30 enum common_args {
     31 	AIMM = 1,
     32 	ASTR,
     33 	AREG,
     34 	ANUMBER,
     35 	AIMM2,
     36 	AIMM3,
     37 	AIMM5,
     38 	AIMM8,
     39 	AIMM16,
     40 	AIMM32,
     41 	AIMM64,
     42 	AINDIR,
     43 	AINDEX,
     44 	ADIRECT,
     45 	AREG_OFF,
     46 	ASYM,
     47 	AOPT,
     48 	AREP,
     49 	AMAX,
     50 };
     51 
     52 enum tokens {
     53 	EOS = -1,
     54 	IDEN = 1,
     55 	NUMBER,
     56 	REG,
     57 	STRING,
     58 	MINUS,
     59 	SHL,
     60 	SHR,
     61 	GE,
     62 	LE,
     63 };
     64 
     65 #define MAXSYM 63
     66 
     67 typedef struct reloc Reloc;
     68 typedef struct ins Ins;
     69 typedef struct op Op;
     70 typedef struct section Section;
     71 typedef struct symbol Symbol;
     72 typedef struct node Node;
     73 typedef struct string String;
     74 typedef void Format(Op *, Node **);
     75 
     76 struct string {
     77 	char *buf;
     78 	size_t offset;
     79 };
     80 
     81 struct line {
     82 	char *label;
     83 	char *op;
     84 	char *args;
     85 };
     86 
     87 struct ins {
     88 	int begin, end;
     89 	char *str;
     90 };
     91 
     92 struct reloc {
     93 	size_t offset;
     94 	Symbol *sym;
     95 	unsigned char flags;
     96 	unsigned char size;
     97 	unsigned char nbits;
     98 	unsigned char shift;
     99 };
    100 
    101 struct op {
    102 	unsigned char flags;
    103 	unsigned char size;
    104 	void (*format)(Op *, Node **);
    105 	unsigned char *bytes;
    106 	unsigned char *args;
    107 };
    108 
    109 struct section {
    110 	Symbol *sym;
    111 	char *mem;
    112 	unsigned char flags;
    113 	unsigned char fill;
    114 	unsigned char aligment;
    115 	unsigned id;
    116 	TUINT base;
    117 	TUINT max;
    118 	TUINT curpc;
    119 	TUINT pc;
    120 	struct section *next;
    121 };
    122 
    123 struct symbol {
    124 	String name;
    125 	String type;
    126 	unsigned char flags;
    127 	unsigned char pass;
    128 	TUINT value;
    129 	TUINT size;
    130 	Section *section;
    131 	struct symbol *next;
    132 	struct symbol *hash;
    133 };
    134 
    135 struct node {
    136 	unsigned char op;
    137 	unsigned char addr;
    138 	struct symbol *sym;
    139 	struct node *left;
    140 	struct node *right;
    141 };
    142 
    143 union yylval {
    144 	TUINT val;
    145 	Symbol *sym;
    146 };
    147 
    148 
    149 /* symbol.c */
    150 extern void cleansecs(void);
    151 extern void isecs(void);
    152 extern void emit(char *bytes, int nbytes);
    153 extern Section *setsec(char *name, char *attr);
    154 extern Symbol *tmpsym(TUINT val);
    155 extern void killtmp(void);
    156 extern int toobig(Node *np, int type);
    157 extern void dumpstab(char *msg);
    158 extern String newstring(char *s);
    159 
    160 /* main.c */
    161 extern Symbol *lookup(char *name);
    162 extern Symbol *deflabel(char *name);
    163 
    164 /* parser.c */
    165 extern Node **getargs(char *s);
    166 extern void error(char *msg, ...);
    167 /* Avoid errors in files where stdio is not included */
    168 #ifdef stdin
    169 extern int nextline(struct line *linep);
    170 #endif
    171 extern void unexpected(void);
    172 extern void expect(int token);
    173 int next(void);
    174 #define accept(t) (yytoken == (t) ? next() : 0)
    175 extern void regctx(int mode);
    176 extern Node *getreg(void);
    177 extern Node *operand(char **s);
    178 extern void addinput(char *fname);
    179 extern int delinput(void);
    180 
    181 /* expr.c */
    182 extern Node *expr(void);
    183 extern void deltree(Node *np);
    184 extern Node *node(int op, Node *l, Node *r);
    185 
    186 /* proc.c */
    187 extern void iarch(void);
    188 extern int match(Op *op, Node **args);
    189 extern Node *moperand(void);
    190 
    191 /* ins.c */
    192 extern char *tobytes(TUINT v, int n, int inc);
    193 
    194 /*
    195  * Definition of global variables
    196  */
    197 extern Section *cursec, *seclist;
    198 extern int nr_ins;
    199 extern Ins instab[];
    200 extern Op optab[];
    201 extern int pass;
    202 extern TUINT maxaddr;
    203 extern int endian;
    204 extern Symbol *linesym, *symlist;
    205 extern char *infile;
    206 extern int endpass;
    207 extern int yytoken;
    208 extern size_t yylen;
    209 extern union yylval yylval;
    210 extern char yytext[];