2 * CIL code generator for TCC
4 * Copyright (c) 2002 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #error this code has bit-rotted since 2003
23 /* number of available registers */
26 /* a register can belong to several classes. The classes must be
27 sorted from more general to more precise (see gv2() code which does
28 assumptions on it). */
29 #define RC_ST 0x0001 /* any stack entry */
30 #define RC_ST0 0x0002 /* top of stack */
31 #define RC_ST1 0x0004 /* top - 1 */
34 #define RC_FLOAT RC_ST
35 #define RC_IRET RC_ST0 /* function return: integer register */
36 #define RC_LRET RC_ST0 /* function return: second integer register */
37 #define RC_FRET RC_ST0 /* function return: float register */
39 /* pretty names for the registers */
46 const int reg_classes
[NB_REGS
] = {
47 /* ST0 */ RC_ST
| RC_ST0
,
48 /* ST1 */ RC_ST
| RC_ST1
,
52 /* return registers for function */
53 #define REG_IRET REG_ST0 /* single word int return register */
54 #define REG_LRET REG_ST0 /* second word return register (for long long) */
55 #define REG_FRET REG_ST0 /* float return register */
57 /* defined if function parameters must be evaluated in reverse order */
58 /* #define INVERT_FUNC_PARAMS */
60 /* defined if structures are passed as pointers. Otherwise structures
61 are directly pushed on stack. */
62 /* #define FUNC_STRUCT_PARAM_AS_PTR */
64 /* pointer size, in bytes */
67 /* long double size and alignment, in bytes */
68 #define LDOUBLE_SIZE 8
69 #define LDOUBLE_ALIGN 8
71 /* function call context */
72 typedef struct GFuncContext
{
73 int func_call
; /* func call type (FUNC_STDCALL or FUNC_CDECL) */
76 /******************************************************/
77 /* opcode definitions */
79 #define IL_OP_PREFIX 0xFE
82 #define OP(name, str, n) IL_OP_ ## name = n,
83 #include "il-opcodes.h"
87 char *il_opcodes_str
[] = {
88 #define OP(name, str, n) [n] = str,
89 #include "il-opcodes.h"
93 /******************************************************/
95 /* arguments variable numbers start from there */
96 #define ARG_BASE 0x70000000
98 static FILE *il_outfile
;
100 static void out_byte(int c
)
105 static void out_le32(int c
)
113 static void init_outfile(void)
118 ".assembly extern mscorlib\n"
125 static void out_op1(int op
)
128 out_byte(IL_OP_PREFIX
);
132 /* output an opcode with prefix */
133 static void out_op(int op
)
136 fprintf(il_outfile
, " %s\n", il_opcodes_str
[op
]);
139 static void out_opb(int op
, int c
)
143 fprintf(il_outfile
, " %s %d\n", il_opcodes_str
[op
], c
);
146 static void out_opi(int op
, int c
)
150 fprintf(il_outfile
, " %s 0x%x\n", il_opcodes_str
[op
], c
);
153 /* XXX: not complete */
154 static void il_type_to_str(char *buf
, int buf_size
,
155 int t
, const char *varstr
)
166 pstrcat(buf
, buf_size
, "unsigned ");
195 pstrcat(buf
, buf_size
, tstr
);
198 tcc_error("structures not handled yet");
201 s
= sym_find((unsigned)t
>> VT_STRUCT_SHIFT
);
202 il_type_to_str(buf
, buf_size
, s
->t
, varstr
);
203 pstrcat(buf
, buf_size
, "(");
206 il_type_to_str(buf1
, sizeof(buf1
), sa
->t
, NULL
);
207 pstrcat(buf
, buf_size
, buf1
);
210 pstrcat(buf
, buf_size
, ", ");
212 pstrcat(buf
, buf_size
, ")");
215 s
= sym_find((unsigned)t
>> VT_STRUCT_SHIFT
);
216 pstrcpy(buf1
, sizeof(buf1
), "*");
218 pstrcat(buf1
, sizeof(buf1
), varstr
);
219 il_type_to_str(buf
, buf_size
, s
->t
, buf1
);
223 pstrcat(buf
, buf_size
, " ");
224 pstrcat(buf
, buf_size
, varstr
);
230 /* patch relocation entry with value 'val' */
231 void greloc_patch1(Reloc
*p
, int val
)
235 /* output a symbol and patch all calls to it */
240 /* output jump and return symbol */
241 static int out_opj(int op
, int c
)
246 c
= ind
- (int)cur_text_section
->data
;
248 fprintf(il_outfile
, " %s L%d\n", il_opcodes_str
[op
], c
);
254 fprintf(il_outfile
, "L%d:\n", t
);
257 /* load 'r' from value 'sv' */
258 void load(int r
, SValue
*sv
)
262 v
= sv
->r
& VT_VALMASK
;
266 if (sv
->r
& VT_LVAL
) {
268 if (fc
>= ARG_BASE
) {
270 if (fc
>= 0 && fc
<= 4) {
271 out_op(IL_OP_LDARG_0
+ fc
);
272 } else if (fc
<= 0xff) {
273 out_opb(IL_OP_LDARG_S
, fc
);
275 out_opi(IL_OP_LDARG
, fc
);
278 if (fc
>= 0 && fc
<= 4) {
279 out_op(IL_OP_LDLOC_0
+ fc
);
280 } else if (fc
<= 0xff) {
281 out_opb(IL_OP_LDLOC_S
, fc
);
283 out_opi(IL_OP_LDLOC
, fc
);
286 } else if (v
== VT_CONST
) {
287 /* XXX: handle globals */
288 out_opi(IL_OP_LDSFLD
, 0);
290 if ((ft
& VT_BTYPE
) == VT_FLOAT
) {
291 out_op(IL_OP_LDIND_R4
);
292 } else if ((ft
& VT_BTYPE
) == VT_DOUBLE
) {
293 out_op(IL_OP_LDIND_R8
);
294 } else if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
295 out_op(IL_OP_LDIND_R8
);
296 } else if ((ft
& VT_TYPE
) == VT_BYTE
)
297 out_op(IL_OP_LDIND_I1
);
298 else if ((ft
& VT_TYPE
) == (VT_BYTE
| VT_UNSIGNED
))
299 out_op(IL_OP_LDIND_U1
);
300 else if ((ft
& VT_TYPE
) == VT_SHORT
)
301 out_op(IL_OP_LDIND_I2
);
302 else if ((ft
& VT_TYPE
) == (VT_SHORT
| VT_UNSIGNED
))
303 out_op(IL_OP_LDIND_U2
);
305 out_op(IL_OP_LDIND_I4
);
309 /* XXX: handle globals */
310 if (fc
>= -1 && fc
<= 8) {
311 out_op(IL_OP_LDC_I4_M1
+ fc
+ 1);
313 out_opi(IL_OP_LDC_I4
, fc
);
315 } else if (v
== VT_LOCAL
) {
316 if (fc
>= ARG_BASE
) {
319 out_opb(IL_OP_LDARGA_S
, fc
);
321 out_opi(IL_OP_LDARGA
, fc
);
325 out_opb(IL_OP_LDLOCA_S
, fc
);
327 out_opi(IL_OP_LDLOCA
, fc
);
336 /* store register 'r' in lvalue 'v' */
337 void store(int r
, SValue
*sv
)
341 v
= sv
->r
& VT_VALMASK
;
345 if (fc
>= ARG_BASE
) {
347 /* XXX: check IL arg store semantics */
349 out_opb(IL_OP_STARG_S
, fc
);
351 out_opi(IL_OP_STARG
, fc
);
354 if (fc
>= 0 && fc
<= 4) {
355 out_op(IL_OP_STLOC_0
+ fc
);
356 } else if (fc
<= 0xff) {
357 out_opb(IL_OP_STLOC_S
, fc
);
359 out_opi(IL_OP_STLOC
, fc
);
362 } else if (v
== VT_CONST
) {
363 /* XXX: handle globals */
364 out_opi(IL_OP_STSFLD
, 0);
366 if ((ft
& VT_BTYPE
) == VT_FLOAT
)
367 out_op(IL_OP_STIND_R4
);
368 else if ((ft
& VT_BTYPE
) == VT_DOUBLE
)
369 out_op(IL_OP_STIND_R8
);
370 else if ((ft
& VT_BTYPE
) == VT_LDOUBLE
)
371 out_op(IL_OP_STIND_R8
);
372 else if ((ft
& VT_BTYPE
) == VT_BYTE
)
373 out_op(IL_OP_STIND_I1
);
374 else if ((ft
& VT_BTYPE
) == VT_SHORT
)
375 out_op(IL_OP_STIND_I2
);
377 out_op(IL_OP_STIND_I4
);
381 /* start function call and return function call context */
382 void gfunc_start(GFuncContext
*c
, int func_call
)
384 c
->func_call
= func_call
;
387 /* push function parameter which is in (vtop->t, vtop->c). Stack entry
389 void gfunc_param(GFuncContext
*c
)
391 if ((vtop
->t
& VT_BTYPE
) == VT_STRUCT
) {
392 tcc_error("structures passed as value not handled yet");
394 /* simply push on stack */
400 /* generate function call with address in (vtop->t, vtop->c) and free function
401 context. Stack entry is popped */
402 void gfunc_call(GFuncContext
*c
)
406 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
407 /* XXX: more info needed from tcc */
408 il_type_to_str(buf
, sizeof(buf
), vtop
->t
, "xxx");
409 fprintf(il_outfile
, " call %s\n", buf
);
413 il_type_to_str(buf
, sizeof(buf
), vtop
->t
, NULL
);
414 fprintf(il_outfile
, " calli %s\n", buf
);
419 /* generate function prolog of type 't' */
420 void gfunc_prolog(int t
)
422 int addr
, u
, func_call
;
428 /* XXX: pass function name to gfunc_prolog */
429 il_type_to_str(buf
, sizeof(buf
), t
, funcname
);
430 fprintf(il_outfile
, ".method static %s il managed\n", buf
);
431 fprintf(il_outfile
, "{\n");
432 /* XXX: cannot do better now */
433 fprintf(il_outfile
, " .maxstack %d\n", NB_REGS
);
434 fprintf(il_outfile
, " .locals (int32, int32, int32, int32, int32, int32, int32, int32)\n");
436 if (!strcmp(funcname
, "main"))
437 fprintf(il_outfile
, " .entrypoint\n");
439 sym
= sym_find((unsigned)t
>> VT_STRUCT_SHIFT
);
443 /* if the function returns a structure, then add an
444 implicit pointer parameter */
446 func_var
= (sym
->c
== FUNC_ELLIPSIS
);
447 if ((func_vt
& VT_BTYPE
) == VT_STRUCT
) {
451 /* define parameters */
452 while ((sym
= sym
->next
) != NULL
) {
454 sym_push(sym
->v
& ~SYM_FIELD
, u
,
455 VT_LOCAL
| lvalue_type(sym
->type
.t
), addr
);
460 /* generate function epilog */
461 void gfunc_epilog(void)
464 fprintf(il_outfile
, "}\n\n");
467 /* generate a jump to a label */
470 return out_opj(IL_OP_BR
, t
);
473 /* generate a jump to a fixed address */
474 void gjmp_addr(int a
)
476 /* XXX: handle syms */
477 out_opi(IL_OP_BR
, a
);
480 /* generate a test. set 'inv' to invert test. Stack entry is popped */
481 int gtst(int inv
, int t
)
485 v
= vtop
->r
& VT_VALMASK
;
521 } else if (v
== VT_JMP
|| v
== VT_JMPI
) {
522 /* && or || optimization */
523 if ((v
& 1) == inv
) {
524 /* insert vtop->c jump list in t */
539 /* generate an integer binary operation */
566 out_op(IL_OP_SHR_UN
);
576 out_op(IL_OP_DIV_UN
);
582 out_op(IL_OP_REM_UN
);
604 /* generate a floating point operation 'v = t1 op t2' instruction. The
605 two operands are guaranteed to have the same floating point type */
608 /* same as integer */
612 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
613 and 'long long' cases. */
614 void gen_cvt_itof(int t
)
618 out_op(IL_OP_CONV_R4
);
620 out_op(IL_OP_CONV_R8
);
623 /* convert fp to int 't' type */
624 /* XXX: handle long long case */
625 void gen_cvt_ftoi(int t
)
629 case VT_INT
| VT_UNSIGNED
:
630 out_op(IL_OP_CONV_U4
);
633 out_op(IL_OP_CONV_I8
);
635 case VT_LLONG
| VT_UNSIGNED
:
636 out_op(IL_OP_CONV_U8
);
639 out_op(IL_OP_CONV_I4
);
644 /* convert from one floating point type to another */
645 void gen_cvt_ftof(int t
)
649 out_op(IL_OP_CONV_R4
);
651 out_op(IL_OP_CONV_R8
);
655 /* end of CIL code generator */
656 /*************************************************************/