il-gen.c 16 KB

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