[279] | 1 | /*
|
---|
| 2 | * CIL code generator for TCC
|
---|
| 3 | *
|
---|
| 4 | * Copyright (c) 2002 Fabrice Bellard
|
---|
| 5 | *
|
---|
| 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.
|
---|
| 10 | *
|
---|
| 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.
|
---|
| 15 | *
|
---|
| 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
|
---|
| 19 | */
|
---|
| 20 |
|
---|
[331] | 21 | #error this code has bit-rotted since 2003
|
---|
| 22 |
|
---|
[279] | 23 | /* number of available registers */
|
---|
| 24 | #define NB_REGS 3
|
---|
| 25 |
|
---|
| 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 */
|
---|
| 32 |
|
---|
| 33 | #define RC_INT RC_ST
|
---|
| 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 */
|
---|
| 38 |
|
---|
| 39 | /* pretty names for the registers */
|
---|
| 40 | enum {
|
---|
| 41 | REG_ST0 = 0,
|
---|
| 42 | REG_ST1,
|
---|
| 43 | REG_ST2,
|
---|
| 44 | };
|
---|
| 45 |
|
---|
[331] | 46 | const int reg_classes[NB_REGS] = {
|
---|
| 47 | /* ST0 */ RC_ST | RC_ST0,
|
---|
| 48 | /* ST1 */ RC_ST | RC_ST1,
|
---|
| 49 | /* ST2 */ RC_ST,
|
---|
| 50 | };
|
---|
| 51 |
|
---|
[279] | 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 */
|
---|
| 56 |
|
---|
| 57 | /* defined if function parameters must be evaluated in reverse order */
|
---|
[331] | 58 | /* #define INVERT_FUNC_PARAMS */
|
---|
[279] | 59 |
|
---|
| 60 | /* defined if structures are passed as pointers. Otherwise structures
|
---|
| 61 | are directly pushed on stack. */
|
---|
[331] | 62 | /* #define FUNC_STRUCT_PARAM_AS_PTR */
|
---|
[279] | 63 |
|
---|
| 64 | /* pointer size, in bytes */
|
---|
| 65 | #define PTR_SIZE 4
|
---|
| 66 |
|
---|
| 67 | /* long double size and alignment, in bytes */
|
---|
| 68 | #define LDOUBLE_SIZE 8
|
---|
| 69 | #define LDOUBLE_ALIGN 8
|
---|
| 70 |
|
---|
| 71 | /* function call context */
|
---|
| 72 | typedef struct GFuncContext {
|
---|
| 73 | int func_call; /* func call type (FUNC_STDCALL or FUNC_CDECL) */
|
---|
| 74 | } GFuncContext;
|
---|
| 75 |
|
---|
| 76 | /******************************************************/
|
---|
| 77 | /* opcode definitions */
|
---|
| 78 |
|
---|
| 79 | #define IL_OP_PREFIX 0xFE
|
---|
| 80 |
|
---|
| 81 | enum ILOPCodes {
|
---|
| 82 | #define OP(name, str, n) IL_OP_ ## name = n,
|
---|
| 83 | #include "il-opcodes.h"
|
---|
| 84 | #undef OP
|
---|
| 85 | };
|
---|
| 86 |
|
---|
| 87 | char *il_opcodes_str[] = {
|
---|
[331] | 88 | #define OP(name, str, n) [n] = str,
|
---|
[279] | 89 | #include "il-opcodes.h"
|
---|
| 90 | #undef OP
|
---|
| 91 | };
|
---|
| 92 |
|
---|
| 93 | /******************************************************/
|
---|
| 94 |
|
---|
| 95 | /* arguments variable numbers start from there */
|
---|
| 96 | #define ARG_BASE 0x70000000
|
---|
| 97 |
|
---|
| 98 | static FILE *il_outfile;
|
---|
| 99 |
|
---|
| 100 | static void out_byte(int c)
|
---|
| 101 | {
|
---|
[331] | 102 | *(char *)ind++ = c;
|
---|
[279] | 103 | }
|
---|
| 104 |
|
---|
| 105 | static void out_le32(int c)
|
---|
| 106 | {
|
---|
| 107 | out_byte(c);
|
---|
| 108 | out_byte(c >> 8);
|
---|
| 109 | out_byte(c >> 16);
|
---|
| 110 | out_byte(c >> 24);
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | static void init_outfile(void)
|
---|
| 114 | {
|
---|
| 115 | if (!il_outfile) {
|
---|
| 116 | il_outfile = stdout;
|
---|
| 117 | fprintf(il_outfile,
|
---|
| 118 | ".assembly extern mscorlib\n"
|
---|
| 119 | "{\n"
|
---|
| 120 | ".ver 1:0:2411:0\n"
|
---|
| 121 | "}\n\n");
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | static void out_op1(int op)
|
---|
| 126 | {
|
---|
| 127 | if (op & 0x100)
|
---|
| 128 | out_byte(IL_OP_PREFIX);
|
---|
| 129 | out_byte(op & 0xff);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | /* output an opcode with prefix */
|
---|
| 133 | static void out_op(int op)
|
---|
| 134 | {
|
---|
| 135 | out_op1(op);
|
---|
| 136 | fprintf(il_outfile, " %s\n", il_opcodes_str[op]);
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | static void out_opb(int op, int c)
|
---|
| 140 | {
|
---|
| 141 | out_op1(op);
|
---|
| 142 | out_byte(c);
|
---|
| 143 | fprintf(il_outfile, " %s %d\n", il_opcodes_str[op], c);
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | static void out_opi(int op, int c)
|
---|
| 147 | {
|
---|
| 148 | out_op1(op);
|
---|
| 149 | out_le32(c);
|
---|
| 150 | fprintf(il_outfile, " %s 0x%x\n", il_opcodes_str[op], c);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | /* XXX: not complete */
|
---|
| 154 | static void il_type_to_str(char *buf, int buf_size,
|
---|
| 155 | int t, const char *varstr)
|
---|
| 156 | {
|
---|
| 157 | int bt;
|
---|
| 158 | Sym *s, *sa;
|
---|
| 159 | char buf1[256];
|
---|
| 160 | const char *tstr;
|
---|
| 161 |
|
---|
| 162 | t = t & VT_TYPE;
|
---|
| 163 | bt = t & VT_BTYPE;
|
---|
| 164 | buf[0] = '\0';
|
---|
| 165 | if (t & VT_UNSIGNED)
|
---|
| 166 | pstrcat(buf, buf_size, "unsigned ");
|
---|
| 167 | switch(bt) {
|
---|
| 168 | case VT_VOID:
|
---|
| 169 | tstr = "void";
|
---|
| 170 | goto add_tstr;
|
---|
| 171 | case VT_BOOL:
|
---|
| 172 | tstr = "bool";
|
---|
| 173 | goto add_tstr;
|
---|
| 174 | case VT_BYTE:
|
---|
| 175 | tstr = "int8";
|
---|
| 176 | goto add_tstr;
|
---|
| 177 | case VT_SHORT:
|
---|
| 178 | tstr = "int16";
|
---|
| 179 | goto add_tstr;
|
---|
| 180 | case VT_ENUM:
|
---|
| 181 | case VT_INT:
|
---|
| 182 | case VT_LONG:
|
---|
| 183 | tstr = "int32";
|
---|
| 184 | goto add_tstr;
|
---|
| 185 | case VT_LLONG:
|
---|
| 186 | tstr = "int64";
|
---|
| 187 | goto add_tstr;
|
---|
| 188 | case VT_FLOAT:
|
---|
| 189 | tstr = "float32";
|
---|
| 190 | goto add_tstr;
|
---|
| 191 | case VT_DOUBLE:
|
---|
| 192 | case VT_LDOUBLE:
|
---|
| 193 | tstr = "float64";
|
---|
| 194 | add_tstr:
|
---|
| 195 | pstrcat(buf, buf_size, tstr);
|
---|
| 196 | break;
|
---|
| 197 | case VT_STRUCT:
|
---|
| 198 | tcc_error("structures not handled yet");
|
---|
| 199 | break;
|
---|
| 200 | case VT_FUNC:
|
---|
| 201 | s = sym_find((unsigned)t >> VT_STRUCT_SHIFT);
|
---|
[331] | 202 | il_type_to_str(buf, buf_size, s->t, varstr);
|
---|
[279] | 203 | pstrcat(buf, buf_size, "(");
|
---|
| 204 | sa = s->next;
|
---|
| 205 | while (sa != NULL) {
|
---|
[331] | 206 | il_type_to_str(buf1, sizeof(buf1), sa->t, NULL);
|
---|
[279] | 207 | pstrcat(buf, buf_size, buf1);
|
---|
| 208 | sa = sa->next;
|
---|
| 209 | if (sa)
|
---|
| 210 | pstrcat(buf, buf_size, ", ");
|
---|
| 211 | }
|
---|
| 212 | pstrcat(buf, buf_size, ")");
|
---|
| 213 | goto no_var;
|
---|
| 214 | case VT_PTR:
|
---|
| 215 | s = sym_find((unsigned)t >> VT_STRUCT_SHIFT);
|
---|
| 216 | pstrcpy(buf1, sizeof(buf1), "*");
|
---|
| 217 | if (varstr)
|
---|
| 218 | pstrcat(buf1, sizeof(buf1), varstr);
|
---|
[331] | 219 | il_type_to_str(buf, buf_size, s->t, buf1);
|
---|
[279] | 220 | goto no_var;
|
---|
| 221 | }
|
---|
| 222 | if (varstr) {
|
---|
| 223 | pstrcat(buf, buf_size, " ");
|
---|
| 224 | pstrcat(buf, buf_size, varstr);
|
---|
| 225 | }
|
---|
| 226 | no_var: ;
|
---|
| 227 | }
|
---|
| 228 |
|
---|
[331] | 229 |
|
---|
[279] | 230 | /* patch relocation entry with value 'val' */
|
---|
| 231 | void greloc_patch1(Reloc *p, int val)
|
---|
| 232 | {
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | /* output a symbol and patch all calls to it */
|
---|
| 236 | void gsym_addr(t, a)
|
---|
| 237 | {
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | /* output jump and return symbol */
|
---|
| 241 | static int out_opj(int op, int c)
|
---|
| 242 | {
|
---|
| 243 | out_op1(op);
|
---|
| 244 | out_le32(0);
|
---|
| 245 | if (c == 0) {
|
---|
| 246 | c = ind - (int)cur_text_section->data;
|
---|
| 247 | }
|
---|
| 248 | fprintf(il_outfile, " %s L%d\n", il_opcodes_str[op], c);
|
---|
| 249 | return c;
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | void gsym(int t)
|
---|
| 253 | {
|
---|
| 254 | fprintf(il_outfile, "L%d:\n", t);
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | /* load 'r' from value 'sv' */
|
---|
| 258 | void load(int r, SValue *sv)
|
---|
| 259 | {
|
---|
| 260 | int v, fc, ft;
|
---|
| 261 |
|
---|
| 262 | v = sv->r & VT_VALMASK;
|
---|
| 263 | fc = sv->c.i;
|
---|
[331] | 264 | ft = sv->t;
|
---|
[279] | 265 |
|
---|
| 266 | if (sv->r & VT_LVAL) {
|
---|
| 267 | if (v == VT_LOCAL) {
|
---|
| 268 | if (fc >= ARG_BASE) {
|
---|
| 269 | 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);
|
---|
| 274 | } else {
|
---|
| 275 | out_opi(IL_OP_LDARG, fc);
|
---|
| 276 | }
|
---|
| 277 | } else {
|
---|
| 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);
|
---|
| 282 | } else {
|
---|
| 283 | out_opi(IL_OP_LDLOC, fc);
|
---|
| 284 | }
|
---|
| 285 | }
|
---|
| 286 | } else if (v == VT_CONST) {
|
---|
| 287 | /* XXX: handle globals */
|
---|
| 288 | out_opi(IL_OP_LDSFLD, 0);
|
---|
| 289 | } else {
|
---|
| 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);
|
---|
| 304 | else
|
---|
| 305 | out_op(IL_OP_LDIND_I4);
|
---|
| 306 | }
|
---|
| 307 | } else {
|
---|
| 308 | if (v == VT_CONST) {
|
---|
| 309 | /* XXX: handle globals */
|
---|
| 310 | if (fc >= -1 && fc <= 8) {
|
---|
| 311 | out_op(IL_OP_LDC_I4_M1 + fc + 1);
|
---|
| 312 | } else {
|
---|
| 313 | out_opi(IL_OP_LDC_I4, fc);
|
---|
| 314 | }
|
---|
| 315 | } else if (v == VT_LOCAL) {
|
---|
| 316 | if (fc >= ARG_BASE) {
|
---|
| 317 | fc -= ARG_BASE;
|
---|
| 318 | if (fc <= 0xff) {
|
---|
| 319 | out_opb(IL_OP_LDARGA_S, fc);
|
---|
| 320 | } else {
|
---|
| 321 | out_opi(IL_OP_LDARGA, fc);
|
---|
| 322 | }
|
---|
| 323 | } else {
|
---|
| 324 | if (fc <= 0xff) {
|
---|
| 325 | out_opb(IL_OP_LDLOCA_S, fc);
|
---|
| 326 | } else {
|
---|
| 327 | out_opi(IL_OP_LDLOCA, fc);
|
---|
| 328 | }
|
---|
| 329 | }
|
---|
| 330 | } else {
|
---|
| 331 | /* XXX: do it */
|
---|
| 332 | }
|
---|
| 333 | }
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 | /* store register 'r' in lvalue 'v' */
|
---|
| 337 | void store(int r, SValue *sv)
|
---|
| 338 | {
|
---|
| 339 | int v, fc, ft;
|
---|
| 340 |
|
---|
| 341 | v = sv->r & VT_VALMASK;
|
---|
| 342 | fc = sv->c.i;
|
---|
[331] | 343 | ft = sv->t;
|
---|
[279] | 344 | if (v == VT_LOCAL) {
|
---|
| 345 | if (fc >= ARG_BASE) {
|
---|
| 346 | fc -= ARG_BASE;
|
---|
| 347 | /* XXX: check IL arg store semantics */
|
---|
| 348 | if (fc <= 0xff) {
|
---|
| 349 | out_opb(IL_OP_STARG_S, fc);
|
---|
| 350 | } else {
|
---|
| 351 | out_opi(IL_OP_STARG, fc);
|
---|
| 352 | }
|
---|
| 353 | } else {
|
---|
| 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);
|
---|
| 358 | } else {
|
---|
| 359 | out_opi(IL_OP_STLOC, fc);
|
---|
| 360 | }
|
---|
| 361 | }
|
---|
| 362 | } else if (v == VT_CONST) {
|
---|
| 363 | /* XXX: handle globals */
|
---|
| 364 | out_opi(IL_OP_STSFLD, 0);
|
---|
| 365 | } else {
|
---|
| 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);
|
---|
| 376 | else
|
---|
| 377 | out_op(IL_OP_STIND_I4);
|
---|
| 378 | }
|
---|
| 379 | }
|
---|
| 380 |
|
---|
| 381 | /* start function call and return function call context */
|
---|
| 382 | void gfunc_start(GFuncContext *c, int func_call)
|
---|
| 383 | {
|
---|
| 384 | c->func_call = func_call;
|
---|
| 385 | }
|
---|
| 386 |
|
---|
| 387 | /* push function parameter which is in (vtop->t, vtop->c). Stack entry
|
---|
| 388 | is then popped. */
|
---|
| 389 | void gfunc_param(GFuncContext *c)
|
---|
| 390 | {
|
---|
[331] | 391 | if ((vtop->t & VT_BTYPE) == VT_STRUCT) {
|
---|
[279] | 392 | tcc_error("structures passed as value not handled yet");
|
---|
| 393 | } else {
|
---|
| 394 | /* simply push on stack */
|
---|
| 395 | gv(RC_ST0);
|
---|
| 396 | }
|
---|
| 397 | vtop--;
|
---|
| 398 | }
|
---|
| 399 |
|
---|
| 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)
|
---|
| 403 | {
|
---|
| 404 | char buf[1024];
|
---|
| 405 |
|
---|
| 406 | if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
|
---|
| 407 | /* XXX: more info needed from tcc */
|
---|
[331] | 408 | il_type_to_str(buf, sizeof(buf), vtop->t, "xxx");
|
---|
[279] | 409 | fprintf(il_outfile, " call %s\n", buf);
|
---|
| 410 | } else {
|
---|
| 411 | /* indirect call */
|
---|
| 412 | gv(RC_INT);
|
---|
[331] | 413 | il_type_to_str(buf, sizeof(buf), vtop->t, NULL);
|
---|
[279] | 414 | fprintf(il_outfile, " calli %s\n", buf);
|
---|
| 415 | }
|
---|
| 416 | vtop--;
|
---|
| 417 | }
|
---|
| 418 |
|
---|
| 419 | /* generate function prolog of type 't' */
|
---|
| 420 | void gfunc_prolog(int t)
|
---|
| 421 | {
|
---|
| 422 | int addr, u, func_call;
|
---|
| 423 | Sym *sym;
|
---|
| 424 | char buf[1024];
|
---|
| 425 |
|
---|
| 426 | init_outfile();
|
---|
| 427 |
|
---|
| 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");
|
---|
| 435 |
|
---|
| 436 | if (!strcmp(funcname, "main"))
|
---|
| 437 | fprintf(il_outfile, " .entrypoint\n");
|
---|
| 438 |
|
---|
| 439 | sym = sym_find((unsigned)t >> VT_STRUCT_SHIFT);
|
---|
[331] | 440 | func_call = sym->r;
|
---|
[279] | 441 |
|
---|
| 442 | addr = ARG_BASE;
|
---|
| 443 | /* if the function returns a structure, then add an
|
---|
| 444 | implicit pointer parameter */
|
---|
[331] | 445 | func_vt = sym->t;
|
---|
| 446 | func_var = (sym->c == FUNC_ELLIPSIS);
|
---|
| 447 | if ((func_vt & VT_BTYPE) == VT_STRUCT) {
|
---|
[279] | 448 | func_vc = addr;
|
---|
| 449 | addr++;
|
---|
| 450 | }
|
---|
| 451 | /* define parameters */
|
---|
| 452 | while ((sym = sym->next) != NULL) {
|
---|
[331] | 453 | u = sym->t;
|
---|
[279] | 454 | sym_push(sym->v & ~SYM_FIELD, u,
|
---|
| 455 | VT_LOCAL | lvalue_type(sym->type.t), addr);
|
---|
| 456 | addr++;
|
---|
| 457 | }
|
---|
| 458 | }
|
---|
| 459 |
|
---|
| 460 | /* generate function epilog */
|
---|
| 461 | void gfunc_epilog(void)
|
---|
| 462 | {
|
---|
| 463 | out_op(IL_OP_RET);
|
---|
| 464 | fprintf(il_outfile, "}\n\n");
|
---|
| 465 | }
|
---|
| 466 |
|
---|
| 467 | /* generate a jump to a label */
|
---|
| 468 | int gjmp(int t)
|
---|
| 469 | {
|
---|
| 470 | return out_opj(IL_OP_BR, t);
|
---|
| 471 | }
|
---|
| 472 |
|
---|
| 473 | /* generate a jump to a fixed address */
|
---|
| 474 | void gjmp_addr(int a)
|
---|
| 475 | {
|
---|
| 476 | /* XXX: handle syms */
|
---|
| 477 | out_opi(IL_OP_BR, a);
|
---|
| 478 | }
|
---|
| 479 |
|
---|
| 480 | /* generate a test. set 'inv' to invert test. Stack entry is popped */
|
---|
| 481 | int gtst(int inv, int t)
|
---|
| 482 | {
|
---|
| 483 | int v, *p, c;
|
---|
| 484 |
|
---|
| 485 | v = vtop->r & VT_VALMASK;
|
---|
| 486 | if (v == VT_CMP) {
|
---|
| 487 | c = vtop->c.i ^ inv;
|
---|
| 488 | switch(c) {
|
---|
| 489 | case TOK_EQ:
|
---|
| 490 | c = IL_OP_BEQ;
|
---|
| 491 | break;
|
---|
| 492 | case TOK_NE:
|
---|
| 493 | c = IL_OP_BNE_UN;
|
---|
| 494 | break;
|
---|
| 495 | case TOK_LT:
|
---|
| 496 | c = IL_OP_BLT;
|
---|
| 497 | break;
|
---|
| 498 | case TOK_LE:
|
---|
| 499 | c = IL_OP_BLE;
|
---|
| 500 | break;
|
---|
| 501 | case TOK_GT:
|
---|
| 502 | c = IL_OP_BGT;
|
---|
| 503 | break;
|
---|
| 504 | case TOK_GE:
|
---|
| 505 | c = IL_OP_BGE;
|
---|
| 506 | break;
|
---|
| 507 | case TOK_ULT:
|
---|
| 508 | c = IL_OP_BLT_UN;
|
---|
| 509 | break;
|
---|
| 510 | case TOK_ULE:
|
---|
| 511 | c = IL_OP_BLE_UN;
|
---|
| 512 | break;
|
---|
| 513 | case TOK_UGT:
|
---|
| 514 | c = IL_OP_BGT_UN;
|
---|
| 515 | break;
|
---|
| 516 | case TOK_UGE:
|
---|
| 517 | c = IL_OP_BGE_UN;
|
---|
| 518 | break;
|
---|
| 519 | }
|
---|
| 520 | t = out_opj(c, t);
|
---|
| 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 */
|
---|
| 525 | p = &vtop->c.i;
|
---|
| 526 | while (*p != 0)
|
---|
| 527 | p = (int *)*p;
|
---|
| 528 | *p = t;
|
---|
| 529 | t = vtop->c.i;
|
---|
| 530 | } else {
|
---|
| 531 | t = gjmp(t);
|
---|
| 532 | gsym(vtop->c.i);
|
---|
| 533 | }
|
---|
| 534 | }
|
---|
| 535 | vtop--;
|
---|
| 536 | return t;
|
---|
| 537 | }
|
---|
| 538 |
|
---|
| 539 | /* generate an integer binary operation */
|
---|
| 540 | void gen_opi(int op)
|
---|
| 541 | {
|
---|
| 542 | gv2(RC_ST1, RC_ST0);
|
---|
| 543 | switch(op) {
|
---|
| 544 | case '+':
|
---|
| 545 | out_op(IL_OP_ADD);
|
---|
| 546 | goto std_op;
|
---|
| 547 | case '-':
|
---|
| 548 | out_op(IL_OP_SUB);
|
---|
| 549 | goto std_op;
|
---|
| 550 | case '&':
|
---|
| 551 | out_op(IL_OP_AND);
|
---|
| 552 | goto std_op;
|
---|
| 553 | case '^':
|
---|
| 554 | out_op(IL_OP_XOR);
|
---|
| 555 | goto std_op;
|
---|
| 556 | case '|':
|
---|
| 557 | out_op(IL_OP_OR);
|
---|
| 558 | goto std_op;
|
---|
| 559 | case '*':
|
---|
| 560 | out_op(IL_OP_MUL);
|
---|
| 561 | goto std_op;
|
---|
| 562 | case TOK_SHL:
|
---|
| 563 | out_op(IL_OP_SHL);
|
---|
| 564 | goto std_op;
|
---|
| 565 | case TOK_SHR:
|
---|
| 566 | out_op(IL_OP_SHR_UN);
|
---|
| 567 | goto std_op;
|
---|
| 568 | case TOK_SAR:
|
---|
| 569 | out_op(IL_OP_SHR);
|
---|
| 570 | goto std_op;
|
---|
| 571 | case '/':
|
---|
| 572 | case TOK_PDIV:
|
---|
| 573 | out_op(IL_OP_DIV);
|
---|
| 574 | goto std_op;
|
---|
| 575 | case TOK_UDIV:
|
---|
| 576 | out_op(IL_OP_DIV_UN);
|
---|
| 577 | goto std_op;
|
---|
| 578 | case '%':
|
---|
| 579 | out_op(IL_OP_REM);
|
---|
| 580 | goto std_op;
|
---|
| 581 | case TOK_UMOD:
|
---|
| 582 | out_op(IL_OP_REM_UN);
|
---|
| 583 | std_op:
|
---|
| 584 | vtop--;
|
---|
| 585 | vtop[0].r = REG_ST0;
|
---|
| 586 | break;
|
---|
| 587 | case TOK_EQ:
|
---|
| 588 | case TOK_NE:
|
---|
| 589 | case TOK_LT:
|
---|
| 590 | case TOK_LE:
|
---|
| 591 | case TOK_GT:
|
---|
| 592 | case TOK_GE:
|
---|
| 593 | case TOK_ULT:
|
---|
| 594 | case TOK_ULE:
|
---|
| 595 | case TOK_UGT:
|
---|
| 596 | case TOK_UGE:
|
---|
| 597 | vtop--;
|
---|
| 598 | vtop[0].r = VT_CMP;
|
---|
| 599 | vtop[0].c.i = op;
|
---|
| 600 | break;
|
---|
| 601 | }
|
---|
| 602 | }
|
---|
| 603 |
|
---|
| 604 | /* generate a floating point operation 'v = t1 op t2' instruction. The
|
---|
[331] | 605 | two operands are guaranteed to have the same floating point type */
|
---|
[279] | 606 | void gen_opf(int op)
|
---|
| 607 | {
|
---|
| 608 | /* same as integer */
|
---|
| 609 | gen_opi(op);
|
---|
| 610 | }
|
---|
| 611 |
|
---|
| 612 | /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
|
---|
| 613 | and 'long long' cases. */
|
---|
| 614 | void gen_cvt_itof(int t)
|
---|
| 615 | {
|
---|
| 616 | gv(RC_ST0);
|
---|
| 617 | if (t == VT_FLOAT)
|
---|
| 618 | out_op(IL_OP_CONV_R4);
|
---|
| 619 | else
|
---|
| 620 | out_op(IL_OP_CONV_R8);
|
---|
| 621 | }
|
---|
| 622 |
|
---|
| 623 | /* convert fp to int 't' type */
|
---|
| 624 | /* XXX: handle long long case */
|
---|
| 625 | void gen_cvt_ftoi(int t)
|
---|
| 626 | {
|
---|
| 627 | gv(RC_ST0);
|
---|
| 628 | switch(t) {
|
---|
| 629 | case VT_INT | VT_UNSIGNED:
|
---|
| 630 | out_op(IL_OP_CONV_U4);
|
---|
| 631 | break;
|
---|
| 632 | case VT_LLONG:
|
---|
| 633 | out_op(IL_OP_CONV_I8);
|
---|
| 634 | break;
|
---|
| 635 | case VT_LLONG | VT_UNSIGNED:
|
---|
| 636 | out_op(IL_OP_CONV_U8);
|
---|
| 637 | break;
|
---|
| 638 | default:
|
---|
| 639 | out_op(IL_OP_CONV_I4);
|
---|
| 640 | break;
|
---|
| 641 | }
|
---|
| 642 | }
|
---|
| 643 |
|
---|
| 644 | /* convert from one floating point type to another */
|
---|
| 645 | void gen_cvt_ftof(int t)
|
---|
| 646 | {
|
---|
| 647 | gv(RC_ST0);
|
---|
| 648 | if (t == VT_FLOAT) {
|
---|
| 649 | out_op(IL_OP_CONV_R4);
|
---|
| 650 | } else {
|
---|
| 651 | out_op(IL_OP_CONV_R8);
|
---|
| 652 | }
|
---|
| 653 | }
|
---|
| 654 |
|
---|
| 655 | /* end of CIL code generator */
|
---|
| 656 | /*************************************************************/
|
---|
[331] | 657 |
|
---|