scc

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

cc2.h (5528B)


      1 #define ASLABEL 0
      2 
      3 enum iflags {
      4 	BBENTRY =    1,        /* basic block entry */
      5 };
      6 
      7 enum tflags {
      8 	SIGNF   =     1 << 0,  /* Signed type */
      9 	INTF    =     1 << 1,  /* integer type */
     10 	FLOATF  =     1 << 2,  /* float type */
     11 	STRF    =     1 << 3,  /* string */
     12 	AGGRF   =     1 << 4,  /* aggregate */
     13 	FUNF    =     1 << 5,  /* function */
     14 	PARF    =     1 << 6,  /* parameter */
     15 	INITF   =     1 << 7,  /* initializer flag */
     16 	ELLIPS  =     1 << 8,  /* vararg function */
     17 	ARRF    =     1 << 9,  /* array flag */
     18 	PTRF    =     1 << 10, /* pointer flag */
     19 };
     20 
     21 enum sclass {
     22 	SAUTO     = 'A',
     23 	SREG      = 'R',
     24 	SLABEL    = 'L',
     25 	SINDEX    = 'I',
     26 	STMP      = 'N',
     27 	SGLOB     = 'G',
     28 	SEXTRN    = 'X',
     29 	SPRIV     = 'Y',
     30 	SLOCAL    = 'T',
     31 	SMEMB     = 'M',
     32 	SCONST    = '#',
     33 	STRING    = '"',
     34 	SNONE     = 0 /* cc2 relies on SNONE being 0 in nextpc() */
     35 };
     36 
     37 enum types {
     38 	ELLIPSIS = 'E',
     39 	INT8     = 'C',
     40 	INT16    = 'I',
     41 	INT32    = 'W',
     42 	INT64    = 'Q',
     43 	UINT8    = 'K',
     44 	UINT16   = 'N',
     45 	UINT32   = 'Z',
     46 	UINT64   = 'O',
     47 	POINTER  = 'P',
     48 	FUNCTION = 'F',
     49 	VECTOR   = 'V',
     50 	UNION    = 'U',
     51 	STRUCT   = 'S',
     52 	BOOL     = 'B',
     53 	FLOAT    = 'J',
     54 	DOUBLE   = 'D',
     55 	LDOUBLE  = 'H',
     56 	VOID     = '0'
     57 };
     58 
     59 enum op {
     60 	/* kind of operand */
     61 	/* operands */
     62 	OMEM     = 'M',
     63 	OINDEX   = 'I',
     64 	OTMP     = 'N',
     65 	OAUTO    = 'A',
     66 	OREG     = 'R',
     67 	OMREG    = 'G',
     68 	OCONST   = '#',
     69 	OSTRING  = '"',
     70 	OLOAD    = 'D',
     71 	OLABEL   = 'L',
     72 	OADD     = '+',
     73 	OSUB     = '-',
     74 	OMUL     = '*',
     75 	OMOD     = '%',
     76 	ODIV     = '/',
     77 	OSHL     = 'l',
     78 	OSHR     = 'r',
     79 	OLT      = '<',
     80 	OGT      = '>',
     81 	OLE      = '[',
     82 	OGE      = ']',
     83 	OEQ      = '=',
     84 	ONE      = '!',
     85 	OBAND    = '&',
     86 	OBOR     = '|',
     87 	OBXOR    = '^',
     88 	OCPL     = '~',
     89 	OASSIG   = ':',
     90 	OSNEG    = '_',
     91 	OCALL    = 'c',
     92 	OCALLE   = 'z',
     93 	OPAR     = 'p',
     94 	OFIELD   = '.',
     95 	OCOMMA   = ',',
     96 	OASK     = '?',
     97 	OCOLON   = ' ',
     98 	OADDR    = '\'',
     99 	OAND     = 'a',
    100 	OOR      = 'o',
    101 	ONEG     = 'n',
    102 	OPTR     = '@',
    103 	OCAST    = 'g',
    104 	OINC     = 'i',
    105 	ODEC     = 'd',
    106 	OBUILTIN = 'm',
    107 	/*statements */
    108 	ONOP     = 'q',
    109 	OJMP     = 'j',
    110 	OBRANCH  = 'y',
    111 	ORET     = 'h',
    112 	OBLOOP   = 'b',
    113 	OELOOP   = 'e',
    114 	OCASE    = 'v',
    115 	ODEFAULT = 'f',
    116 	OBSWITCH = 's',
    117 	OESWITCH = 't',
    118 	OBFUN    = 'x',
    119 	OEFUN    = 'k',
    120 };
    121 
    122 enum builtins {
    123 	BVA_START = 's',
    124 	BVA_END   = 'e',
    125 	BVA_ARG   = 'a',
    126 	BVA_COPY  = 'c',
    127 };
    128 
    129 enum nerrors {
    130 	EEOFFUN,       /* EOF while parsing function */
    131 	ENLABEL,       /* label without statement */
    132 	EIDOVER,       /* identifier overflow */
    133 	EOUTPAR,       /* out pf params */
    134 	ESYNTAX,       /* syntax error */
    135 	ESTACKA,       /* stack unaligned */
    136 	ESTACKO,       /* stack overflow */
    137 	ESTACKU,       /* stack underflow */
    138 	ELNLINE,       /* line too long */
    139 	ELNBLNE,       /* line without new line */
    140 	EFERROR,       /* error reading from file:%s */
    141 	EBADID,        /* incorrect symbol id */
    142 	EWTACKO,       /* switch stack overflow */
    143 	EWTACKU,       /* switch stack underflow */
    144 	ENOSWTC,       /* Out of switch statement */
    145 	EBBUILT,       /* Unknown builtin */
    146 	EOVERFL,       /* Numerical overflow */
    147 	ENUMERR
    148 };
    149 
    150 typedef struct node Node;
    151 typedef struct type Type;
    152 typedef struct symbol Symbol;
    153 typedef struct addr Addr;
    154 typedef struct inst Inst;
    155 
    156 struct type {
    157 	unsigned long size;
    158 	unsigned align;
    159 	unsigned short id;
    160 	short flags;
    161 };
    162 
    163 struct symbol {
    164 	Type type;
    165 	Type rtype;
    166 	unsigned short id;
    167 	unsigned short numid;
    168 	char *name;
    169 	char kind;
    170 	union {
    171 		long off;
    172 		Node *stmt;
    173 		Inst *inst;
    174 	} u;
    175 	Symbol *next, *prev;
    176 	Symbol *h_next;
    177 };
    178 
    179 struct node {
    180 	char op;
    181 	Type type;
    182 	int complex;
    183 	int address;
    184 	unsigned flags;
    185 	union {
    186 		TUINT i;
    187 		TFLOAT f;
    188 		char *s;
    189 		Symbol *sym;
    190 		int reg;
    191 		int subop;
    192 		long off;
    193 	} u;
    194 	Symbol *label;
    195 	Node *left, *right;
    196 	Node *next, *prev;
    197 };
    198 
    199 struct addr {
    200 	char kind;
    201 	union {
    202 		int reg;
    203 		TUINT i;
    204 		Symbol *sym;
    205 		long off;
    206 	} u;
    207 };
    208 
    209 struct inst {
    210 	unsigned char op;
    211 	unsigned char flags;
    212 	Symbol *label;
    213 	Inst *next, *prev;
    214 	Addr from1, from2, to;
    215 };
    216 
    217 /* main.c */
    218 extern void error(unsigned nerror, ...);
    219 
    220 /* parse.c */
    221 extern void parse(void);
    222 
    223 /* cgen.c */
    224 extern Node *sethi(Node *np);
    225 extern void genasm(void);
    226 
    227 /* peep.c */
    228 extern void peephole(void);
    229 
    230 /* code.c */
    231 extern void data(Node *np);
    232 extern void writeout(void), endinit(void);
    233 extern void code(int op, Node *to, Node *from1, Node *from2);
    234 extern void defvar(Symbol *), defpar(Symbol *), defglobal(Symbol *);
    235 extern void setlabel(Symbol *);
    236 extern Node *label2node(Node *np, Symbol *sym);
    237 extern Node *constnode(Node *np, TUINT n, Type *tp);
    238 extern Node *tmpnode(Type *);
    239 extern Node *idxnode(Node *, long);
    240 extern Symbol *newlabel(void);
    241 extern void pprint(char *s);
    242 extern void deftype(Type *);
    243 
    244 /* node.c */
    245 #define SETCUR  1
    246 #define KEEPCUR 0
    247 extern void apply(Node *(*fun)(Node *));
    248 extern void cleannodes(void);
    249 extern void delnode(Node *np);
    250 extern void deltree(Node *np);
    251 extern void prtree(Node *np), prforest(char *msg);
    252 extern Node *node(int op);
    253 extern Node *addstmt(Node *np, int flags);
    254 extern Node *delstmt(void);
    255 extern Node *nextstmt(void);
    256 
    257 /* symbol.c */
    258 #define TMPSYM  0
    259 extern Symbol *getsym(unsigned id);
    260 extern void popctx(void);
    261 extern void pushctx(void);
    262 extern void freesym(Symbol *sym);
    263 
    264 /* globals */
    265 extern Symbol *curfun;
    266 extern Symbol *locals;
    267 extern Inst *pc, *prog;
    268 
    269 /* target */
    270 extern Type int8type, int16type, int32type, int64type,
    271             uint8type, uint16type, uint32type, uint64type,
    272             float32type, float64type, float80type,
    273             booltype,
    274             ptrtype,
    275             voidtype,
    276             arg_type;