scc

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

as.h (3461B)


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