int: load assignment destination last if possible
[neatcc.git] / ncc.h
blob0558f27ed29808a0347abb424c72b512cff7d89f
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)
88 * Intermediate instruction operands
89 * R: register, N: immediate, S: symbol, L: local,
90 * D: displacement, G: label, C: arguments
92 /* Instruction rd r1 r2 r3 */
93 #define O_ADD 0x000010 /* R R RN - */
94 #define O_SHL 0x000020 /* R R RN - */
95 #define O_MUL 0x000040 /* R R RN - */
96 #define O_CMP 0x000080 /* R R RN - */
97 #define O_UOP 0x000100 /* R R - - */
98 #define O_CALL 0x000200 /* R RS D C */
99 #define O_MOV 0x000400 /* R RNSL D - */
100 #define O_MEM 0x000800 /* - R R R */
101 #define O_JMP 0x001000 /* - - - G */
102 #define O_JZ 0x002000 /* - R - G */
103 #define O_JCC 0x004000 /* - R RN G */
104 #define O_RET 0x008000 /* - R - - */
105 #define O_LD 0x010000 /* R RSL D - */
106 #define O_ST 0x020000 /* - R RSL D */
107 /* opcode flags: num, loc, sym */
108 #define O_NUM 0x100000 /* instruction immediate */
109 #define O_LOC 0x200000 /* local (frame pointer displacement) */
110 #define O_SYM 0x400000 /* symbols (relocations and offset) */
111 /* other members of instruction groups */
112 #define O_SUB (1 | O_ADD)
113 #define O_AND (2 | O_ADD)
114 #define O_OR (3 | O_ADD)
115 #define O_XOR (4 | O_ADD)
116 #define O_SHR (1 | O_SHL)
117 #define O_DIV (1 | O_MUL)
118 #define O_MOD (2 | O_MUL)
119 #define O_LT (0 | O_CMP)
120 #define O_GE (1 | O_CMP)
121 #define O_EQ (2 | O_CMP)
122 #define O_NE (3 | O_CMP)
123 #define O_LE (4 | O_CMP)
124 #define O_GT (5 | O_CMP)
125 #define O_NEG (0 | O_UOP)
126 #define O_NOT (1 | O_UOP)
127 #define O_LNOT (2 | O_UOP)
128 #define O_MSET (0 | O_MEM)
129 #define O_MCPY (1 | O_MEM)
130 #define O_JNZ (1 | O_JZ)
131 /* instruction masks */
132 #define O_BOP (O_ADD | O_MUL | O_CMP | O_SHL)
133 #define O_OUT (O_BOP | O_UOP | O_CALL | O_MOV | O_LD)
134 #define O_JXX (O_JMP | O_JZ | O_JCC)
135 /* instruction operand type */
136 #define O_C(op) ((op) & 0xffffff) /* operation code */
137 #define O_T(op) ((op) >> 24) /* instruction operand type */
138 #define O_MK(op, bt) ((op) | ((bt) << 24))
140 /* operations on the stack */
141 void o_bop(long op); /* binary operation */
142 void o_uop(long op); /* unary operation */
143 void o_cast(long bt);
144 void o_memcpy(void);
145 void o_memset(void);
146 void o_call(int argc, int ret);
147 void o_ret(int ret);
148 void o_assign(long bt);
149 void o_deref(long bt);
150 void o_load(void);
151 int o_popnum(long *num);
152 int o_popsym(long *sym, long *off);
153 /* pushing values to the stack */
154 void o_num(long n);
155 void o_local(long addr);
156 void o_sym(char *sym);
157 void o_tmpdrop(int n);
158 void o_tmpswap(void);
159 void o_tmpcopy(void);
160 /* handling locals */
161 long o_mklocal(long size);
162 void o_rmlocal(long addr, long sz);
163 long o_arg2loc(int i);
164 /* branches */
165 void o_label(long id);
166 void o_jmp(long id);
167 void o_jz(long id);
168 long o_mark(void);
169 void o_back(long mark);
170 /* data/bss sections */
171 long o_dsnew(char *name, long size, int global);
172 void o_dscpy(long addr, void *buf, long len);
173 void o_dsset(char *name, long off, long bt);
174 void o_bsnew(char *name, long size, int global);
175 /* functions */
176 void o_func_beg(char *name, int argc, int global, int vararg);
177 void o_func_end(void);
178 void o_code(char *name, char *c, long c_len);
179 /* output */
180 void o_write(int fd);
182 /* SECTION THREE: The Intermediate Code */
183 /* intermediate code instructions */
184 struct ic {
185 long op; /* instruction opcode */
186 long a1; /* first argument */
187 long a2; /* second argument */
188 long a3; /* more information, like jump target */
189 long *args; /* call arguments */
192 /* get the generated intermediate code */
193 void ic_get(struct ic **c, long *n);
194 int ic_num(struct ic *ic, long iv, long *num);
195 int ic_sym(struct ic *ic, long iv, long *sym, long *off);
196 long *ic_lastuse(struct ic *ic, long ic_n);
197 void ic_free(struct ic *ic);
198 int ic_regcnt(struct ic *ic);
200 /* global register allocation */
201 void reg_init(struct ic *ic, long ic_n);
202 long reg_mask(void);
203 int reg_lmap(long ic, long loc);
204 int reg_rmap(long ic, long reg);
205 int reg_safe(long loc);
206 void reg_done(void);
208 /* SECTION FOUR: Final Code Generation */
210 * To make maintaining different architectures easier and to unify the
211 * optimizations, I have merged the code generation for different
212 * architectures. The i_*() functions are now the low level
213 * architecture-specific code generation entry points. The
214 * differences between RISC and CISC architectures, actually the
215 * annoying asymmetry in CISC architecture, has made this interface
216 * more complex than it could have ideally been. Nevertheless,
217 * the benefits of extracting gen.c and the cleaner design,
218 * especially with the presence of the optimizations, outweighs the
219 * added complexity. Overall, there were many challenges for
220 * extracting gen.c including:
221 * + Different register sets; caller/callee saved and argument registers
222 * + CISC-style instructions that work on limited registers and parameters
223 * + Different instruction formats and immediate value limitations
224 * + Generating epilog, prolog, and local variable addresses when optimizing
226 * I tried to make this interface as small as possible. The key
227 * functions and macros described next.
229 * i_reg() returns the mask of allowed registers for each
230 * operand of an instruction. The first argument op, specifies
231 * the instruction (O_* macros); i_reg() sets the value r0, r1,
232 * and r2 to indicate the mask of acceptable registers for the
233 * first, second, and third operands of the instruction.
234 * The value of these masks may be changed to zero to indicate
235 * fewer than three operands. If md is zero while m1 is not,
236 * the destination register should be equal to the first register,
237 * as in CISC architectures. mt denotes the mask of registers
238 * that may lose their contents after the instruction.
240 * i_ins() generates code for the given instruction. The arguments
241 * indicate the instruction and its operands. The code is generated
242 * by calling os() and oi() functions and the current position in
243 * the code segment is obtained by calling opos(). For branch
244 * instructions, i_ins() returns the position of branch offset in
245 * code segment, to be filled later with i_fill().
247 * Some macros should be defined in architecture-dependent headers
248 * and a few variables should be defined for each architecture,
249 * such as tmpregs, which is an array of register numbers that
250 * can be used for holding temporaries and argregs, which is an
251 * array of register numbers for holding the first N_ARGS arguments.
252 * Consult x64.h as an example, for the macros defined for each
253 * architecture.
256 #ifdef NEATCC_ARM
257 #include "arm.h"
258 #endif
259 #ifdef NEATCC_X64
260 #include "x64.h"
261 #endif
262 #ifdef NEATCC_X86
263 #include "x86.h"
264 #endif
266 /* architecture-specific operations */
267 long i_reg(long op, long *rd, long *r1, long *r2, long *r3, long *mt);
268 long i_ins(long op, long rd, long r1, long r2, long r3);
269 int i_imm(long lim, long n);
270 void i_label(long id);
271 void i_wrap(int argc, long sargs, long spsub, int initfp, long sregs, long sregs_pos);
272 void i_code(char **c, long *c_len, long **rsym, long **rflg, long **roff, long *rcnt);
273 void i_done(void);
275 extern int tmpregs[];
276 extern int argregs[];
278 /* SECTION FIVE: Object File Generation */
279 #define OUT_CS 0x0001 /* code segment symbol */
280 #define OUT_DS 0x0002 /* data segment symbol */
281 #define OUT_BSS 0x0004 /* bss segment symbol */
283 #define OUT_GLOB 0x0010 /* global symbol */
285 #define OUT_RLREL 0x0020 /* relative relocation */
286 #define OUT_RLSX 0x0040 /* sign extend relocation */
287 #define OUT_RL24 0x0400 /* 3-byte relocation */
288 #define OUT_RL32 0x0800 /* 4-byte relocation */
290 #define OUT_ALIGNMENT 16 /* section alignment */
292 void out_init(long flags);
294 long out_sym(char *name);
295 void out_def(char *name, long flags, long off, long len);
296 void out_rel(long id, long flags, long off);
298 void out_write(int fd, char *cs, long cslen, char *ds, long dslen);