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