il-gen.c 16 KB

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