ncc: new intermediate code
[neatcc.git] / ncc.h
blobf325797e91c9467c2b9eb95691ea2f7bdf650a39
1 /*
2 * THE NEATCC C COMPILER
4 * This header file is organized as follows:
6 * 0. helper functions and data structures
7 * 1. ncc.c -> tok.c: the interface for reading tokens
8 * 2. ncc.c -> int.c: the interface for generating the intermediate code
9 * 3. int.c -> gen.c: the intermediate code
10 * 4. gen.c -> x64.c: the interface for generating the final code
11 * 5. gen.c -> out.c: the interface for generating object files
14 /* SECTION ZERO: Helper Functions */
15 /* predefined array limits; (p.f. means per function) */
16 #define NARGS 32 /* number of function/macro arguments */
17 #define NTMPS 64 /* number of expression temporaries */
18 #define NFIELDS 128 /* number of fields in structs */
19 #define NAMELEN 128 /* size of identifiers */
20 #define NDEFS 1024 /* number of macros */
21 #define MARGLEN 1024 /* size of macro arguments */
22 #define MDEFLEN 2048 /* size of macro definitions */
23 #define NBUFS 32 /* macro expansion stack depth */
24 #define NLOCS 1024 /* number of header search paths */
26 #define LEN(a) (sizeof(a) / sizeof((a)[0]))
27 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
28 #define MIN(a, b) ((a) < (b) ? (a) : (b))
29 #define MAX(a, b) ((a) < (b) ? (b) : (a))
31 void *mextend(void *old, long oldsz, long newsz, long memsz);
32 void die(char *msg, ...);
33 void err(char *fmt, ...);
35 /* variable length buffer */
36 struct mem {
37 char *s; /* allocated buffer */
38 long sz; /* buffer size */
39 long n; /* length of data stored in s */
42 void mem_init(struct mem *mem);
43 void mem_done(struct mem *mem);
44 void mem_cut(struct mem *mem, long pos);
45 void *mem_buf(struct mem *mem);
46 void mem_put(struct mem *mem, void *buf, long len);
47 void mem_putc(struct mem *mem, int c);
48 void mem_putz(struct mem *mem, long sz);
49 void mem_cpy(struct mem *mem, long off, void *buf, long len);
50 long mem_len(struct mem *mem);
51 void *mem_get(struct mem *mem);
53 /* SECTION ONE: Tokenisation */
54 void tok_init(char *path);
55 void tok_done(void);
56 char *tok_see(void); /* return the current token; a static buffer */
57 char *tok_get(void); /* return and consume the current token */
58 long tok_len(void); /* the length of the last token */
59 long tok_num(char *tok, long *n);
60 long tok_addr(void);
61 void tok_jump(long addr);
63 int cpp_init(char *path);
64 void cpp_path(char *s);
65 void cpp_define(char *name, char *def);
66 char *cpp_loc(long addr);
67 int cpp_read(char **buf, long *len);
69 /* SECTION TWO: Intermediate Code Generation */
70 /* basic type meaning */
71 #define T_MSIZE 0x000f
72 #define T_MSIGN 0x0010
73 #define T_SZ(bt) ((bt) & T_MSIZE)
74 #define T_MK(sign, size) (((sign) & T_MSIGN) | ((size) & T_MSIZE))
76 /* number of bytes in basic types */
77 #define ULNG (LONGSZ)
78 #define UINT (4)
79 #define USHT (2)
80 #define UCHR (1)
81 /* basic types */
82 #define SLNG (ULNG | T_MSIGN)
83 #define SINT (UINT | T_MSIGN)
84 #define SSHT (USHT | T_MSIGN)
85 #define SCHR (UCHR | T_MSIGN)
87 /* instructions macros */
88 #define O_ADD 0x000010 /* add r0, r1, r2(num) */
89 #define O_SHL 0x000020 /* shl r0, r1, r2(num) */
90 #define O_MUL 0x000040 /* mul r0, r1, r2(num) */
91 #define O_CMP 0x000080 /* cmp r0, r1, r2(num) */
92 #define O_UOP 0x000100 /* neg r0, r1 */
93 #define O_CALL 0x000200 /* cmp r0, r1(sym) */
94 #define O_MOV 0x000400 /* mov r0, r1(num,sym,loc) */
95 #define O_MEM 0x000800 /* mem* r0, r1, r2 */
96 #define O_JMP 0x001000 /* jmp num */
97 #define O_JZ 0x002000 /* jz r0, num */
98 #define O_JCC 0x004000 /* jcc r0, r1(num), num */
99 #define O_RET 0x008000 /* ret r0 */
100 #define O_LD 0x010000 /* ld r0, r1(sym,loc), r2(num) */
101 #define O_ST 0x020000 /* st r0, r1(sym,loc), r2(num) */
102 /* opcode flags: num, loc, sym */
103 #define O_NUM 0x100000 /* instruction immediate */
104 #define O_LOC 0x200000 /* local (frame pointer displacement) */
105 #define O_SYM 0x400000 /* symbols (relocations and offset) */
106 /* other members of instruction groups */
107 #define O_SUB (1 | O_ADD)
108 #define O_AND (2 | O_ADD)
109 #define O_OR (3 | O_ADD)
110 #define O_XOR (4 | O_ADD)
111 #define O_SHR (1 | O_SHL)
112 #define O_DIV (1 | O_MUL)
113 #define O_MOD (2 | O_MUL)
114 #define O_LT (0 | O_CMP)
115 #define O_GE (1 | O_CMP)
116 #define O_EQ (2 | O_CMP)
117 #define O_NE (3 | O_CMP)
118 #define O_LE (4 | O_CMP)
119 #define O_GT (5 | O_CMP)
120 #define O_NEG (0 | O_UOP)
121 #define O_NOT (1 | O_UOP)
122 #define O_LNOT (2 | O_UOP)
123 #define O_MSET (0 | O_MEM)
124 #define O_MCPY (1 | O_MEM)
125 #define O_JNZ (1 | O_JZ)
126 /* instruction masks */
127 #define O_BOP (O_ADD | O_MUL | O_CMP | O_SHL)
128 #define O_OUT (O_BOP | O_UOP | O_CALL | O_MOV | O_LD)
129 #define O_JXX (O_JMP | O_JZ | O_JCC)
130 /* instruction operand type */
131 #define O_C(op) ((op) & 0xffffff) /* operation code */
132 #define O_T(op) ((op) >> 24) /* instruction operand type */
133 #define O_MK(op, bt) ((op) | ((bt) << 24))
135 /* operations on the stack */
136 void o_bop(long op); /* binary operation */
137 void o_uop(long op); /* unary operation */
138 void o_cast(long bt);
139 void o_memcpy(void);
140 void o_memset(void);
141 void o_call(int argc, int ret);
142 void o_ret(int ret);
143 void o_assign(long bt);
144 void o_deref(long bt);
145 void o_load(void);
146 int o_popnum(long *num);
147 int o_popsym(long *sym, long *off);
148 /* pushing values to the stack */
149 void o_num(long n);
150 void o_local(long addr);
151 void o_sym(char *sym);
152 void o_tmpdrop(int n);
153 void o_tmpswap(void);
154 void o_tmpcopy(void);
155 /* handling locals */
156 long o_mklocal(long size);
157 void o_rmlocal(long addr, long sz);
158 long o_arg2loc(int i);
159 /* branches */
160 void o_label(long id);
161 void o_jmp(long id);
162 void o_jz(long id);
163 long o_mark(void);
164 void o_back(long mark);
165 /* data/bss sections */
166 long o_dsnew(char *name, long size, int global);
167 void o_dscpy(long addr, void *buf, long len);
168 void o_dsset(char *name, long off, long bt);
169 void o_bsnew(char *name, long size, int global);
170 /* functions */
171 void o_func_beg(char *name, int argc, int global, int vararg);
172 void o_func_end(void);
173 void o_code(char *name, char *c, long c_len);
174 /* output */
175 void o_write(int fd);
177 /* SECTION THREE: The Intermediate Code */
178 /* intermediate code instructions */
179 struct ic {
180 long op; /* instruction opcode */
181 long arg0; /* first argument; usually destination */
182 long arg1; /* second argument */
183 long arg2; /* more information, like jump target */
184 long *args; /* call arguments */
187 /* get the generated intermediate code */
188 void ic_get(struct ic **c, long *n);
189 int ic_num(struct ic *ic, long iv, long *num);
190 int ic_sym(struct ic *ic, long iv, long *sym, long *off);
192 /* SECTION FOUR: Final Code Generation */
194 * To make maintaining different architectures easier and to unify the
195 * optimizations, I have merged the code generation for different
196 * architectures. The i_*() functions are now the low level
197 * architecture-specific code generation entry points. The
198 * differences between RISC and CISC architectures, actually the
199 * annoying asymmetry in CISC architecture, has made this interface
200 * more complex than it could have ideally been. Nevertheless,
201 * the benefits of extracting gen.c and the cleaner design,
202 * especially with the presence of the optimizations, outweighs the
203 * added complexity. Overall, there were many challenges for
204 * extracting gen.c including:
205 * + Different register sets; caller/callee saved and argument registers
206 * + CISC-style instructions that work on limited registers and parameters
207 * + Different instruction formats and immediate value limitations
208 * + Generating epilog, prolog, and local variable addresses when optimizing
210 * I tried to make this interface as small as possible. The key
211 * functions and macros described next.
213 * i_reg() returns the mask of allowed registers for each
214 * operand of an instruction. The first argument op, specifies
215 * the instruction (O_* macros); i_reg() sets the value r0, r1,
216 * and r2 to indicate the mask of acceptable registers for the
217 * first, second, and third operands of the instruction.
218 * The value of these masks may be changed to zero to indicate
219 * fewer than three operands. If md is zero while m1 is not,
220 * the destination register should be equal to the first register,
221 * as in CISC architectures. mt denotes the mask of registers
222 * that may lose their contents after the instruction.
224 * i_ins() generates code for the given instruction. The arguments
225 * indicate the instruction and its operands. The code is generated
226 * by calling os() and oi() functions and the current position in
227 * the code segment is obtained by calling opos(). For branch
228 * instructions, i_ins() returns the position of branch offset in
229 * code segment, to be filled later with i_fill().
231 * Some macros should be defined in architecture-dependent headers
232 * and a few variables should be defined for each architecture,
233 * such as tmpregs, which is an array of register numbers that
234 * can be used for holding temporaries and argregs, which is an
235 * array of register numbers for holding the first N_ARGS arguments.
236 * Consult x64.h as an example, for the macros defined for each
237 * architecture.
240 #ifdef NEATCC_ARM
241 #include "arm.h"
242 #endif
243 #ifdef NEATCC_X64
244 #include "x64.h"
245 #endif
246 #ifdef NEATCC_X86
247 #include "x86.h"
248 #endif
250 /* architecture-specific operations */
251 long i_reg(long op, long *r0, long *r1, long *r2, long *mt);
252 long i_ins(long op, long r0, long r1, long r2);
253 int i_imm(long lim, long n);
254 void i_label(long id);
255 void i_wrap(int argc, long sargs, long spsub, int initfp, long sregs, long sregs_pos);
256 void i_code(char **c, long *c_len, long **rsym, long **rflg, long **roff, long *rcnt);
257 void i_done(void);
259 extern int tmpregs[];
260 extern int argregs[];
262 /* SECTION FIVE: Object File Generation */
263 #define OUT_CS 0x0001 /* code segment symbol */
264 #define OUT_DS 0x0002 /* data segment symbol */
265 #define OUT_BSS 0x0004 /* bss segment symbol */
267 #define OUT_GLOB 0x0010 /* global symbol */
269 #define OUT_RLREL 0x0020 /* relative relocation */
270 #define OUT_RLSX 0x0040 /* sign extend relocation */
271 #define OUT_RL24 0x0400 /* 3-byte relocation */
272 #define OUT_RL32 0x0800 /* 4-byte relocation */
274 #define OUT_ALIGNMENT 16 /* section alignment */
276 void out_init(long flags);
278 long out_sym(char *name);
279 void out_def(char *name, long flags, long off, long len);
280 void out_rel(long id, long flags, long off);
282 void out_write(int fd, char *cs, long cslen, char *ds, long dslen);