x86_64-gen.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  1. /*
  2. * x86-64 code generator for TCC
  3. *
  4. * Copyright (c) 2008 Shinichiro Hamaji
  5. *
  6. * Based on i386-gen.c by Fabrice Bellard
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <assert.h>
  23. /* number of available registers */
  24. #define NB_REGS 5
  25. /* a register can belong to several classes. The classes must be
  26. sorted from more general to more precise (see gv2() code which does
  27. assumptions on it). */
  28. #define RC_INT 0x0001 /* generic integer register */
  29. #define RC_FLOAT 0x0002 /* generic float register */
  30. #define RC_RAX 0x0004
  31. #define RC_RCX 0x0008
  32. #define RC_RDX 0x0010
  33. #define RC_XMM0 0x0020
  34. #define RC_ST0 0x0040 /* only for long double */
  35. #define RC_IRET RC_RAX /* function return: integer register */
  36. #define RC_LRET RC_RDX /* function return: second integer register */
  37. #define RC_FRET RC_XMM0 /* function return: float register */
  38. /* pretty names for the registers */
  39. enum {
  40. TREG_RAX = 0,
  41. TREG_RCX = 1,
  42. TREG_RDX = 2,
  43. TREG_RSI = 6,
  44. TREG_RDI = 7,
  45. TREG_R8 = 8,
  46. TREG_R9 = 9,
  47. TREG_R10 = 10,
  48. TREG_R11 = 11,
  49. TREG_XMM0 = 3,
  50. TREG_ST0 = 4,
  51. TREG_MEM = 0x10,
  52. };
  53. #define REX_BASE(reg) (((reg) >> 3) & 1)
  54. #define REG_VALUE(reg) ((reg) & 7)
  55. int reg_classes[NB_REGS] = {
  56. /* eax */ RC_INT | RC_RAX,
  57. /* ecx */ RC_INT | RC_RCX,
  58. /* edx */ RC_INT | RC_RDX,
  59. /* xmm0 */ RC_FLOAT | RC_XMM0,
  60. /* st0 */ RC_ST0,
  61. };
  62. /* return registers for function */
  63. #define REG_IRET TREG_RAX /* single word int return register */
  64. #define REG_LRET TREG_RDX /* second word return register (for long long) */
  65. #define REG_FRET TREG_XMM0 /* float return register */
  66. /* defined if function parameters must be evaluated in reverse order */
  67. #define INVERT_FUNC_PARAMS
  68. /* pointer size, in bytes */
  69. #define PTR_SIZE 8
  70. /* long double size and alignment, in bytes */
  71. #define LDOUBLE_SIZE 16
  72. #define LDOUBLE_ALIGN 8
  73. /* maximum alignment (for aligned attribute support) */
  74. #define MAX_ALIGN 8
  75. /******************************************************/
  76. /* ELF defines */
  77. #define EM_TCC_TARGET EM_X86_64
  78. /* relocation type for 32 bit data relocation */
  79. #define R_DATA_32 R_X86_64_64
  80. #define R_JMP_SLOT R_X86_64_JUMP_SLOT
  81. #define R_COPY R_X86_64_COPY
  82. #define ELF_START_ADDR 0x08048000
  83. #define ELF_PAGE_SIZE 0x1000
  84. /******************************************************/
  85. static unsigned long func_sub_sp_offset;
  86. static int func_ret_sub;
  87. /* XXX: make it faster ? */
  88. void g(int c)
  89. {
  90. int ind1;
  91. ind1 = ind + 1;
  92. if (ind1 > cur_text_section->data_allocated)
  93. section_realloc(cur_text_section, ind1);
  94. cur_text_section->data[ind] = c;
  95. ind = ind1;
  96. }
  97. void o(unsigned int c)
  98. {
  99. while (c) {
  100. g(c);
  101. c = c >> 8;
  102. }
  103. }
  104. void gen_le32(int c)
  105. {
  106. g(c);
  107. g(c >> 8);
  108. g(c >> 16);
  109. g(c >> 24);
  110. }
  111. void gen_le64(int64_t c)
  112. {
  113. g(c);
  114. g(c >> 8);
  115. g(c >> 16);
  116. g(c >> 24);
  117. g(c >> 32);
  118. g(c >> 40);
  119. g(c >> 48);
  120. g(c >> 56);
  121. }
  122. /* output a symbol and patch all calls to it */
  123. void gsym_addr(int t, int a)
  124. {
  125. int n, *ptr;
  126. while (t) {
  127. ptr = (int *)(cur_text_section->data + t);
  128. n = *ptr; /* next value */
  129. *ptr = a - t - 4;
  130. t = n;
  131. }
  132. }
  133. void gsym(int t)
  134. {
  135. gsym_addr(t, ind);
  136. }
  137. /* psym is used to put an instruction with a data field which is a
  138. reference to a symbol. It is in fact the same as oad ! */
  139. #define psym oad
  140. static int is64_type(int t)
  141. {
  142. return ((t & VT_BTYPE) == VT_PTR ||
  143. (t & VT_BTYPE) == VT_FUNC ||
  144. (t & VT_BTYPE) == VT_LLONG);
  145. }
  146. static int is_sse_float(int t) {
  147. int bt;
  148. bt = t & VT_BTYPE;
  149. return bt == VT_DOUBLE || bt == VT_FLOAT;
  150. }
  151. /* instruction + 4 bytes data. Return the address of the data */
  152. static int oad(int c, int s)
  153. {
  154. int ind1;
  155. o(c);
  156. ind1 = ind + 4;
  157. if (ind1 > cur_text_section->data_allocated)
  158. section_realloc(cur_text_section, ind1);
  159. *(int *)(cur_text_section->data + ind) = s;
  160. s = ind;
  161. ind = ind1;
  162. return s;
  163. }
  164. /* output constant with relocation if 'r & VT_SYM' is true */
  165. static void gen_addr64(int r, Sym *sym, int64_t c)
  166. {
  167. if (r & VT_SYM)
  168. greloc(cur_text_section, sym, ind, R_X86_64_64);
  169. gen_le64(c);
  170. }
  171. /* output constant with relocation if 'r & VT_SYM' is true */
  172. static void gen_addrpc32(int r, Sym *sym, int c)
  173. {
  174. if (r & VT_SYM)
  175. greloc(cur_text_section, sym, ind, R_X86_64_PC32);
  176. gen_le32(c-4);
  177. }
  178. /* output got address with relocation */
  179. static void gen_gotpcrel(int r, Sym *sym, int c)
  180. {
  181. Section *sr;
  182. ElfW(Rela) *rel;
  183. greloc(cur_text_section, sym, ind, R_X86_64_GOTPCREL);
  184. sr = cur_text_section->reloc;
  185. rel = (ElfW(Rela) *)(sr->data + sr->data_offset - sizeof(ElfW(Rela)));
  186. rel->r_addend = -4;
  187. gen_le32(0);
  188. if (c) {
  189. /* we use add c, %xxx for displacement */
  190. o(0x48 + REX_BASE(r));
  191. o(0x81);
  192. o(0xc0 + REG_VALUE(r));
  193. gen_le32(c);
  194. }
  195. }
  196. static void gen_modrm_impl(int op_reg, int r, Sym *sym, int c, int is_got)
  197. {
  198. op_reg = REG_VALUE(op_reg) << 3;
  199. if ((r & VT_VALMASK) == VT_CONST) {
  200. /* constant memory reference */
  201. o(0x05 | op_reg);
  202. if (is_got) {
  203. gen_gotpcrel(r, sym, c);
  204. } else {
  205. gen_addrpc32(r, sym, c);
  206. }
  207. } else if ((r & VT_VALMASK) == VT_LOCAL) {
  208. /* currently, we use only ebp as base */
  209. if (c == (char)c) {
  210. /* short reference */
  211. o(0x45 | op_reg);
  212. g(c);
  213. } else {
  214. oad(0x85 | op_reg, c);
  215. }
  216. } else if ((r & VT_VALMASK) >= TREG_MEM) {
  217. if (c) {
  218. g(0x80 | op_reg | REG_VALUE(r));
  219. gen_le32(c);
  220. } else {
  221. g(0x00 | op_reg | REG_VALUE(r));
  222. }
  223. } else {
  224. g(0x00 | op_reg | (r & VT_VALMASK));
  225. }
  226. }
  227. /* generate a modrm reference. 'op_reg' contains the addtionnal 3
  228. opcode bits */
  229. static void gen_modrm(int op_reg, int r, Sym *sym, int c)
  230. {
  231. gen_modrm_impl(op_reg, r, sym, c, 0);
  232. }
  233. /* generate a modrm reference. 'op_reg' contains the addtionnal 3
  234. opcode bits */
  235. static void gen_modrm64(int opcode, int op_reg, int r, Sym *sym, int c)
  236. {
  237. int is_got;
  238. int rex = 0x48 | (REX_BASE(op_reg) << 2);
  239. if ((r & VT_VALMASK) != VT_CONST &&
  240. (r & VT_VALMASK) != VT_LOCAL) {
  241. rex |= REX_BASE(VT_VALMASK & r);
  242. }
  243. o(rex);
  244. o(opcode);
  245. is_got = (op_reg & TREG_MEM) && !(sym->type.t & VT_STATIC);
  246. gen_modrm_impl(op_reg, r, sym, c, is_got);
  247. }
  248. /* load 'r' from value 'sv' */
  249. void load(int r, SValue *sv)
  250. {
  251. int v, t, ft, fc, fr;
  252. SValue v1;
  253. fr = sv->r;
  254. ft = sv->type.t;
  255. fc = sv->c.ul;
  256. /* we use indirect access via got */
  257. if ((fr & VT_VALMASK) == VT_CONST && (fr & VT_SYM) &&
  258. (fr & VT_LVAL) && !(sv->sym->type.t & VT_STATIC)) {
  259. /* use the result register as a temporal register */
  260. int tr = r | TREG_MEM;
  261. if (is_float(ft)) {
  262. /* we cannot use float registers as a temporal register */
  263. tr = get_reg(RC_INT) | TREG_MEM;
  264. }
  265. gen_modrm64(0x8b, tr, fr, sv->sym, 0);
  266. /* load from the temporal register */
  267. fr = tr | VT_LVAL;
  268. }
  269. v = fr & VT_VALMASK;
  270. if (fr & VT_LVAL) {
  271. if (v == VT_LLOCAL) {
  272. v1.type.t = VT_PTR;
  273. v1.r = VT_LOCAL | VT_LVAL;
  274. v1.c.ul = fc;
  275. load(r, &v1);
  276. fr = r;
  277. }
  278. if ((ft & VT_BTYPE) == VT_FLOAT) {
  279. o(0x6e0f66); /* movd */
  280. r = 0;
  281. } else if ((ft & VT_BTYPE) == VT_DOUBLE) {
  282. o(0x7e0ff3); /* movq */
  283. r = 0;
  284. } else if ((ft & VT_BTYPE) == VT_LDOUBLE) {
  285. o(0xdb); /* fldt */
  286. r = 5;
  287. } else if ((ft & VT_TYPE) == VT_BYTE) {
  288. o(0xbe0f); /* movsbl */
  289. } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) {
  290. o(0xb60f); /* movzbl */
  291. } else if ((ft & VT_TYPE) == VT_SHORT) {
  292. o(0xbf0f); /* movswl */
  293. } else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) {
  294. o(0xb70f); /* movzwl */
  295. } else if (is64_type(ft)) {
  296. gen_modrm64(0x8b, r, fr, sv->sym, fc);
  297. return;
  298. } else {
  299. o(0x8b); /* movl */
  300. }
  301. gen_modrm(r, fr, sv->sym, fc);
  302. } else {
  303. if (v == VT_CONST) {
  304. if ((ft & VT_BTYPE) == VT_LLONG) {
  305. assert(!(fr & VT_SYM));
  306. o(0x48);
  307. o(0xb8 + REG_VALUE(r)); /* mov $xx, r */
  308. gen_addr64(fr, sv->sym, sv->c.ull);
  309. } else {
  310. if (fr & VT_SYM) {
  311. if (sv->sym->type.t & VT_STATIC) {
  312. o(0x8d48);
  313. o(0x05 + REG_VALUE(r) * 8); /* lea xx(%rip), r */
  314. gen_addrpc32(fr, sv->sym, fc);
  315. } else {
  316. o(0x8b48);
  317. o(0x05 + REG_VALUE(r) * 8); /* mov xx(%rip), r */
  318. gen_gotpcrel(r, sv->sym, fc);
  319. }
  320. } else {
  321. o(0xb8 + REG_VALUE(r)); /* mov $xx, r */
  322. gen_le32(fc);
  323. }
  324. }
  325. } else if (v == VT_LOCAL) {
  326. o(0x48 | REX_BASE(r));
  327. o(0x8d); /* lea xxx(%ebp), r */
  328. gen_modrm(r, VT_LOCAL, sv->sym, fc);
  329. } else if (v == VT_CMP) {
  330. oad(0xb8 + r, 0); /* mov $0, r */
  331. o(0x0f); /* setxx %br */
  332. o(fc);
  333. o(0xc0 + r);
  334. } else if (v == VT_JMP || v == VT_JMPI) {
  335. t = v & 1;
  336. oad(0xb8 + r, t); /* mov $1, r */
  337. o(0x05eb); /* jmp after */
  338. gsym(fc);
  339. oad(0xb8 + r, t ^ 1); /* mov $0, r */
  340. } else if (v != r) {
  341. if (r == TREG_XMM0) {
  342. assert(v == TREG_ST0);
  343. /* gen_cvt_ftof(VT_DOUBLE); */
  344. o(0xf0245cdd); /* fstpl -0x10(%rsp) */
  345. /* movsd -0x10(%rsp),%xmm0 */
  346. o(0x44100ff2);
  347. o(0xf024);
  348. } else if (r == TREG_ST0) {
  349. assert(v == TREG_XMM0);
  350. /* gen_cvt_ftof(VT_LDOUBLE); */
  351. /* movsd %xmm0,-0x10(%rsp) */
  352. o(0x44110ff2);
  353. o(0xf024);
  354. o(0xf02444dd); /* fldl -0x10(%rsp) */
  355. } else {
  356. o(0x48 | REX_BASE(r) | (REX_BASE(v) << 2));
  357. o(0x89);
  358. o(0xc0 + r + v * 8); /* mov v, r */
  359. }
  360. }
  361. }
  362. }
  363. /* store register 'r' in lvalue 'v' */
  364. void store(int r, SValue *v)
  365. {
  366. int fr, bt, ft, fc;
  367. int op64 = 0;
  368. /* store the REX prefix in this variable when PIC is enabled */
  369. int pic = 0;
  370. ft = v->type.t;
  371. fc = v->c.ul;
  372. fr = v->r & VT_VALMASK;
  373. bt = ft & VT_BTYPE;
  374. /* we need to access the variable via got */
  375. if (fr == VT_CONST && (v->r & VT_SYM)) {
  376. /* mov xx(%rip), %r11 */
  377. o(0x1d8b4c);
  378. gen_gotpcrel(TREG_R11, v->sym, v->c.ul);
  379. pic = is64_type(bt) ? 0x49 : 0x41;
  380. }
  381. /* XXX: incorrect if float reg to reg */
  382. if (bt == VT_FLOAT) {
  383. o(0x66);
  384. o(pic);
  385. o(0x7e0f); /* movd */
  386. r = 0;
  387. } else if (bt == VT_DOUBLE) {
  388. o(0x66);
  389. o(pic);
  390. o(0xd60f); /* movq */
  391. r = 0;
  392. } else if (bt == VT_LDOUBLE) {
  393. o(0xc0d9); /* fld %st(0) */
  394. o(pic);
  395. o(0xdb); /* fstpt */
  396. r = 7;
  397. } else {
  398. if (bt == VT_SHORT)
  399. o(0x66);
  400. o(pic);
  401. if (bt == VT_BYTE || bt == VT_BOOL)
  402. o(0x88);
  403. else if (is64_type(bt))
  404. op64 = 0x89;
  405. else
  406. o(0x89);
  407. }
  408. if (pic) {
  409. /* xxx r, (%r11) where xxx is mov, movq, fld, or etc */
  410. if (op64)
  411. o(op64);
  412. o(3 + (r << 3));
  413. } else if (op64) {
  414. if (fr == VT_CONST ||
  415. fr == VT_LOCAL ||
  416. (v->r & VT_LVAL)) {
  417. gen_modrm64(op64, r, v->r, v->sym, fc);
  418. } else if (fr != r) {
  419. /* XXX: don't we really come here? */
  420. abort();
  421. o(0xc0 + fr + r * 8); /* mov r, fr */
  422. }
  423. } else {
  424. if (fr == VT_CONST ||
  425. fr == VT_LOCAL ||
  426. (v->r & VT_LVAL)) {
  427. gen_modrm(r, v->r, v->sym, fc);
  428. } else if (fr != r) {
  429. /* XXX: don't we really come here? */
  430. abort();
  431. o(0xc0 + fr + r * 8); /* mov r, fr */
  432. }
  433. }
  434. }
  435. static void gadd_sp(int val)
  436. {
  437. if (val == (char)val) {
  438. o(0xc48348);
  439. g(val);
  440. } else {
  441. oad(0xc48148, val); /* add $xxx, %rsp */
  442. }
  443. }
  444. /* 'is_jmp' is '1' if it is a jump */
  445. static void gcall_or_jmp(int is_jmp)
  446. {
  447. int r;
  448. if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
  449. /* constant case */
  450. if (vtop->r & VT_SYM) {
  451. /* relocation case */
  452. greloc(cur_text_section, vtop->sym,
  453. ind + 1, R_X86_64_PC32);
  454. } else {
  455. /* put an empty PC32 relocation */
  456. put_elf_reloc(symtab_section, cur_text_section,
  457. ind + 1, R_X86_64_PC32, 0);
  458. }
  459. oad(0xe8 + is_jmp, vtop->c.ul - 4); /* call/jmp im */
  460. } else {
  461. /* otherwise, indirect call */
  462. r = TREG_R11;
  463. load(r, vtop);
  464. o(0x41); /* REX */
  465. o(0xff); /* call/jmp *r */
  466. o(0xd0 + REG_VALUE(r) + (is_jmp << 4));
  467. }
  468. }
  469. static uint8_t arg_regs[6] = {
  470. TREG_RDI, TREG_RSI, TREG_RDX, TREG_RCX, TREG_R8, TREG_R9
  471. };
  472. /* Generate function call. The function address is pushed first, then
  473. all the parameters in call order. This functions pops all the
  474. parameters and the function address. */
  475. void gfunc_call(int nb_args)
  476. {
  477. int size, align, r, args_size, i, func_call;
  478. Sym *func_sym;
  479. SValue *orig_vtop;
  480. int nb_reg_args = 0;
  481. int nb_sse_args = 0;
  482. int sse_reg, gen_reg;
  483. /* calculate the number of integer/float arguments */
  484. args_size = 0;
  485. for(i = 0; i < nb_args; i++) {
  486. if ((vtop[-i].type.t & VT_BTYPE) == VT_STRUCT) {
  487. args_size += type_size(&vtop->type, &align);
  488. } else if ((vtop[-i].type.t & VT_BTYPE) == VT_LDOUBLE) {
  489. args_size += 16;
  490. } else if (is_sse_float(vtop[-i].type.t)) {
  491. nb_sse_args++;
  492. if (nb_sse_args > 8) args_size += 8;
  493. } else {
  494. nb_reg_args++;
  495. if (nb_reg_args > 6) args_size += 8;
  496. }
  497. }
  498. /* for struct arguments, we need to call memcpy and the function
  499. call breaks register passing arguments we are preparing.
  500. So, we process arguments which will be passed by stack first. */
  501. orig_vtop = vtop;
  502. gen_reg = nb_reg_args;
  503. sse_reg = nb_sse_args;
  504. /* adjust stack to align SSE boundary */
  505. if (args_size &= 8) {
  506. o(0x50); /* push $rax */
  507. }
  508. for(i = 0; i < nb_args; i++) {
  509. if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
  510. size = type_size(&vtop->type, &align);
  511. /* align to stack align size */
  512. size = (size + 3) & ~3;
  513. /* allocate the necessary size on stack */
  514. o(0x48);
  515. oad(0xec81, size); /* sub $xxx, %rsp */
  516. /* generate structure store */
  517. r = get_reg(RC_INT);
  518. o(0x48 + REX_BASE(r));
  519. o(0x89); /* mov %rsp, r */
  520. o(0xe0 + r);
  521. {
  522. /* following code breaks vtop[1] */
  523. SValue tmp = vtop[1];
  524. vset(&vtop->type, r | VT_LVAL, 0);
  525. vswap();
  526. vstore();
  527. vtop[1] = tmp;
  528. }
  529. args_size += size;
  530. } else if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
  531. gv(RC_ST0);
  532. size = LDOUBLE_SIZE;
  533. oad(0xec8148, size); /* sub $xxx, %rsp */
  534. o(0x7cdb); /* fstpt 0(%rsp) */
  535. g(0x24);
  536. g(0x00);
  537. args_size += size;
  538. } else if (is_sse_float(vtop->type.t)) {
  539. int j = --sse_reg;
  540. if (j >= 8) {
  541. gv(RC_FLOAT);
  542. o(0x50); /* push $rax */
  543. /* movq %xmm0, (%rsp) */
  544. o(0x04d60f66);
  545. o(0x24);
  546. args_size += 8;
  547. }
  548. } else {
  549. int j = --gen_reg;
  550. /* simple type */
  551. /* XXX: implicit cast ? */
  552. if (j >= 6) {
  553. r = gv(RC_INT);
  554. o(0x50 + r); /* push r */
  555. args_size += 8;
  556. }
  557. }
  558. vtop--;
  559. }
  560. vtop = orig_vtop;
  561. /* then, we prepare register passing arguments.
  562. Note that we cannot set RDX and RCX in this loop because gv()
  563. may break these temporary registers. Let's use R10 and R11
  564. instead of them */
  565. gen_reg = nb_reg_args;
  566. sse_reg = nb_sse_args;
  567. for(i = 0; i < nb_args; i++) {
  568. if ((vtop->type.t & VT_BTYPE) == VT_STRUCT ||
  569. (vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
  570. } else if (is_sse_float(vtop->type.t)) {
  571. int j = --sse_reg;
  572. if (j < 8) {
  573. gv(RC_FLOAT); /* only one float register */
  574. /* movaps %xmm0, %xmmN */
  575. o(0x280f);
  576. o(0xc0 + (sse_reg << 3));
  577. }
  578. } else {
  579. int j = --gen_reg;
  580. /* simple type */
  581. /* XXX: implicit cast ? */
  582. if (j < 6) {
  583. r = gv(RC_INT);
  584. if (j < 2) {
  585. o(0x8948); /* mov */
  586. o(0xc0 + r * 8 + arg_regs[j]);
  587. } else if (j < 4) {
  588. o(0x8949); /* mov */
  589. /* j=2: r10, j=3: r11 */
  590. o(0xc0 + r * 8 + j);
  591. } else {
  592. o(0x8949); /* mov */
  593. /* j=4: r8, j=5: r9 */
  594. o(0xc0 + r * 8 + j - 4);
  595. }
  596. }
  597. }
  598. vtop--;
  599. }
  600. save_regs(0); /* save used temporary registers */
  601. /* Copy R10 and R11 into RDX and RCX, respectively */
  602. if (nb_reg_args > 2) {
  603. o(0xd2894c); /* mov %r10, %rdx */
  604. if (nb_reg_args > 3) {
  605. o(0xd9894c); /* mov %r11, %rcx */
  606. }
  607. }
  608. func_sym = vtop->type.ref;
  609. func_call = FUNC_CALL(func_sym->r);
  610. oad(0xb8, nb_sse_args < 8 ? nb_sse_args : 8); /* mov nb_sse_args, %eax */
  611. gcall_or_jmp(0);
  612. if (args_size)
  613. gadd_sp(args_size);
  614. vtop--;
  615. }
  616. #ifdef TCC_TARGET_PE
  617. /* XXX: support PE? */
  618. #warning "PE isn't tested at all"
  619. #define FUNC_PROLOG_SIZE 12
  620. #else
  621. #define FUNC_PROLOG_SIZE 11
  622. #endif
  623. static void push_arg_reg(int i) {
  624. loc -= 8;
  625. gen_modrm64(0x89, arg_regs[i], VT_LOCAL, NULL, loc);
  626. }
  627. /* generate function prolog of type 't' */
  628. void gfunc_prolog(CType *func_type)
  629. {
  630. int i, addr, align, size, func_call;
  631. int param_index, param_addr, reg_param_index, sse_param_index;
  632. Sym *sym;
  633. CType *type;
  634. func_ret_sub = 0;
  635. sym = func_type->ref;
  636. func_call = FUNC_CALL(sym->r);
  637. addr = PTR_SIZE * 2;
  638. loc = 0;
  639. ind += FUNC_PROLOG_SIZE;
  640. func_sub_sp_offset = ind;
  641. if (func_type->ref->c == FUNC_ELLIPSIS) {
  642. int seen_reg_num, seen_sse_num, seen_stack_size;
  643. seen_reg_num = seen_sse_num = 0;
  644. /* frame pointer and return address */
  645. seen_stack_size = PTR_SIZE * 2;
  646. /* count the number of seen parameters */
  647. sym = func_type->ref;
  648. while ((sym = sym->next) != NULL) {
  649. type = &sym->type;
  650. if (is_sse_float(type->t)) {
  651. if (seen_sse_num < 8) {
  652. seen_sse_num++;
  653. } else {
  654. seen_stack_size += 8;
  655. }
  656. } else if ((type->t & VT_BTYPE) == VT_STRUCT) {
  657. size = type_size(type, &align);
  658. size = (size + 3) & ~3;
  659. seen_stack_size += size;
  660. } else if ((type->t & VT_BTYPE) == VT_LDOUBLE) {
  661. seen_stack_size += LDOUBLE_SIZE;
  662. } else {
  663. if (seen_reg_num < 6) {
  664. seen_reg_num++;
  665. } else {
  666. seen_stack_size += 8;
  667. }
  668. }
  669. }
  670. loc -= 16;
  671. /* movl $0x????????, -0x10(%rbp) */
  672. o(0xf045c7);
  673. gen_le32(seen_reg_num * 8);
  674. /* movl $0x????????, -0xc(%rbp) */
  675. o(0xf445c7);
  676. gen_le32(seen_sse_num * 16 + 48);
  677. /* movl $0x????????, -0x8(%rbp) */
  678. o(0xf845c7);
  679. gen_le32(seen_stack_size);
  680. /* save all register passing arguments */
  681. for (i = 0; i < 8; i++) {
  682. loc -= 16;
  683. o(0xd60f66); /* movq */
  684. gen_modrm(7 - i, VT_LOCAL, NULL, loc);
  685. /* movq $0, loc+8(%rbp) */
  686. o(0x85c748);
  687. gen_le32(loc + 8);
  688. gen_le32(0);
  689. }
  690. for (i = 0; i < 6; i++) {
  691. push_arg_reg(5 - i);
  692. }
  693. }
  694. sym = func_type->ref;
  695. param_index = 0;
  696. reg_param_index = 0;
  697. sse_param_index = 0;
  698. /* if the function returns a structure, then add an
  699. implicit pointer parameter */
  700. func_vt = sym->type;
  701. if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
  702. push_arg_reg(reg_param_index);
  703. param_addr = loc;
  704. func_vc = loc;
  705. param_index++;
  706. reg_param_index++;
  707. }
  708. /* define parameters */
  709. while ((sym = sym->next) != NULL) {
  710. type = &sym->type;
  711. size = type_size(type, &align);
  712. size = (size + 3) & ~3;
  713. if (is_sse_float(type->t)) {
  714. if (sse_param_index < 8) {
  715. /* save arguments passed by register */
  716. loc -= 8;
  717. o(0xd60f66); /* movq */
  718. gen_modrm(sse_param_index, VT_LOCAL, NULL, loc);
  719. param_addr = loc;
  720. } else {
  721. param_addr = addr;
  722. addr += size;
  723. }
  724. sse_param_index++;
  725. } else if ((type->t & VT_BTYPE) == VT_STRUCT ||
  726. (type->t & VT_BTYPE) == VT_LDOUBLE) {
  727. param_addr = addr;
  728. addr += size;
  729. } else {
  730. if (reg_param_index < 6) {
  731. /* save arguments passed by register */
  732. push_arg_reg(reg_param_index);
  733. param_addr = loc;
  734. } else {
  735. param_addr = addr;
  736. addr += 8;
  737. }
  738. reg_param_index++;
  739. }
  740. sym_push(sym->v & ~SYM_FIELD, type,
  741. VT_LOCAL | VT_LVAL, param_addr);
  742. param_index++;
  743. }
  744. }
  745. /* generate function epilog */
  746. void gfunc_epilog(void)
  747. {
  748. int v, saved_ind;
  749. o(0xc9); /* leave */
  750. if (func_ret_sub == 0) {
  751. o(0xc3); /* ret */
  752. } else {
  753. o(0xc2); /* ret n */
  754. g(func_ret_sub);
  755. g(func_ret_sub >> 8);
  756. }
  757. /* align local size to word & save local variables */
  758. v = (-loc + 15) & -16;
  759. saved_ind = ind;
  760. ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
  761. #ifdef TCC_TARGET_PE
  762. if (v >= 4096) {
  763. Sym *sym = external_global_sym(TOK___chkstk, &func_old_type, 0);
  764. oad(0xb8, v); /* mov stacksize, %eax */
  765. oad(0xe8, -4); /* call __chkstk, (does the stackframe too) */
  766. greloc(cur_text_section, sym, ind-4, R_X86_64_PC32);
  767. } else
  768. #endif
  769. {
  770. o(0xe5894855); /* push %rbp, mov %rsp, %rbp */
  771. o(0xec8148); /* sub rsp, stacksize */
  772. gen_le32(v);
  773. #if FUNC_PROLOG_SIZE == 12
  774. o(0x90); /* adjust to FUNC_PROLOG_SIZE */
  775. #endif
  776. }
  777. ind = saved_ind;
  778. }
  779. /* generate a jump to a label */
  780. int gjmp(int t)
  781. {
  782. return psym(0xe9, t);
  783. }
  784. /* generate a jump to a fixed address */
  785. void gjmp_addr(int a)
  786. {
  787. int r;
  788. r = a - ind - 2;
  789. if (r == (char)r) {
  790. g(0xeb);
  791. g(r);
  792. } else {
  793. oad(0xe9, a - ind - 5);
  794. }
  795. }
  796. /* generate a test. set 'inv' to invert test. Stack entry is popped */
  797. int gtst(int inv, int t)
  798. {
  799. int v, *p;
  800. v = vtop->r & VT_VALMASK;
  801. if (v == VT_CMP) {
  802. /* fast case : can jump directly since flags are set */
  803. g(0x0f);
  804. t = psym((vtop->c.i - 16) ^ inv, t);
  805. } else if (v == VT_JMP || v == VT_JMPI) {
  806. /* && or || optimization */
  807. if ((v & 1) == inv) {
  808. /* insert vtop->c jump list in t */
  809. p = &vtop->c.i;
  810. while (*p != 0)
  811. p = (int *)(cur_text_section->data + *p);
  812. *p = t;
  813. t = vtop->c.i;
  814. } else {
  815. t = gjmp(t);
  816. gsym(vtop->c.i);
  817. }
  818. } else {
  819. if (is_float(vtop->type.t) ||
  820. (vtop->type.t & VT_BTYPE) == VT_LLONG) {
  821. vpushi(0);
  822. gen_op(TOK_NE);
  823. }
  824. if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
  825. /* constant jmp optimization */
  826. if ((vtop->c.i != 0) != inv)
  827. t = gjmp(t);
  828. } else {
  829. v = gv(RC_INT);
  830. o(0x85);
  831. o(0xc0 + v * 9);
  832. g(0x0f);
  833. t = psym(0x85 ^ inv, t);
  834. }
  835. }
  836. vtop--;
  837. return t;
  838. }
  839. /* generate an integer binary operation */
  840. void gen_opi(int op)
  841. {
  842. int r, fr, opc, c;
  843. switch(op) {
  844. case '+':
  845. case TOK_ADDC1: /* add with carry generation */
  846. opc = 0;
  847. gen_op8:
  848. if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST &&
  849. !is64_type(vtop->type.t)) {
  850. /* constant case */
  851. vswap();
  852. r = gv(RC_INT);
  853. if (is64_type(vtop->type.t)) {
  854. o(0x48 | REX_BASE(r));
  855. }
  856. vswap();
  857. c = vtop->c.i;
  858. if (c == (char)c) {
  859. /* XXX: generate inc and dec for smaller code ? */
  860. o(0x83);
  861. o(0xc0 | (opc << 3) | REG_VALUE(r));
  862. g(c);
  863. } else {
  864. o(0x81);
  865. oad(0xc0 | (opc << 3) | REG_VALUE(r), c);
  866. }
  867. } else {
  868. gv2(RC_INT, RC_INT);
  869. r = vtop[-1].r;
  870. fr = vtop[0].r;
  871. if (opc != 7 ||
  872. is64_type(vtop[0].type.t) || (vtop[0].type.t & VT_UNSIGNED) ||
  873. is64_type(vtop[-1].type.t) || (vtop[-1].type.t & VT_UNSIGNED)) {
  874. o(0x48 | REX_BASE(r) | (REX_BASE(fr) << 2));
  875. }
  876. o((opc << 3) | 0x01);
  877. o(0xc0 + REG_VALUE(r) + REG_VALUE(fr) * 8);
  878. }
  879. vtop--;
  880. if (op >= TOK_ULT && op <= TOK_GT) {
  881. vtop->r = VT_CMP;
  882. vtop->c.i = op;
  883. }
  884. break;
  885. case '-':
  886. case TOK_SUBC1: /* sub with carry generation */
  887. opc = 5;
  888. goto gen_op8;
  889. case TOK_ADDC2: /* add with carry use */
  890. opc = 2;
  891. goto gen_op8;
  892. case TOK_SUBC2: /* sub with carry use */
  893. opc = 3;
  894. goto gen_op8;
  895. case '&':
  896. opc = 4;
  897. goto gen_op8;
  898. case '^':
  899. opc = 6;
  900. goto gen_op8;
  901. case '|':
  902. opc = 1;
  903. goto gen_op8;
  904. case '*':
  905. gv2(RC_INT, RC_INT);
  906. r = vtop[-1].r;
  907. fr = vtop[0].r;
  908. if (is64_type(vtop[0].type.t) || (vtop[0].type.t & VT_UNSIGNED) ||
  909. is64_type(vtop[-1].type.t) || (vtop[-1].type.t & VT_UNSIGNED)) {
  910. o(0x48 | REX_BASE(fr) | (REX_BASE(r) << 2));
  911. }
  912. vtop--;
  913. o(0xaf0f); /* imul fr, r */
  914. o(0xc0 + fr + r * 8);
  915. break;
  916. case TOK_SHL:
  917. opc = 4;
  918. goto gen_shift;
  919. case TOK_SHR:
  920. opc = 5;
  921. goto gen_shift;
  922. case TOK_SAR:
  923. opc = 7;
  924. gen_shift:
  925. opc = 0xc0 | (opc << 3);
  926. if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
  927. /* constant case */
  928. vswap();
  929. r = gv(RC_INT);
  930. if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
  931. o(0x48 | REX_BASE(r));
  932. c = 0x3f;
  933. } else {
  934. c = 0x1f;
  935. }
  936. vswap();
  937. c &= vtop->c.i;
  938. o(0xc1); /* shl/shr/sar $xxx, r */
  939. o(opc | r);
  940. g(c);
  941. } else {
  942. /* we generate the shift in ecx */
  943. gv2(RC_INT, RC_RCX);
  944. r = vtop[-1].r;
  945. if ((vtop[-1].type.t & VT_BTYPE) == VT_LLONG) {
  946. o(0x48 | REX_BASE(r));
  947. }
  948. o(0xd3); /* shl/shr/sar %cl, r */
  949. o(opc | r);
  950. }
  951. vtop--;
  952. break;
  953. case '/':
  954. case TOK_UDIV:
  955. case TOK_PDIV:
  956. case '%':
  957. case TOK_UMOD:
  958. case TOK_UMULL:
  959. /* first operand must be in eax */
  960. /* XXX: need better constraint for second operand */
  961. gv2(RC_RAX, RC_RCX);
  962. r = vtop[-1].r;
  963. fr = vtop[0].r;
  964. vtop--;
  965. save_reg(TREG_RDX);
  966. if (op == TOK_UMULL) {
  967. o(0xf7); /* mul fr */
  968. o(0xe0 + fr);
  969. vtop->r2 = TREG_RDX;
  970. r = TREG_RAX;
  971. } else {
  972. if (op == TOK_UDIV || op == TOK_UMOD) {
  973. o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
  974. o(0xf0 + fr);
  975. } else {
  976. if ((vtop->type.t & VT_BTYPE) & VT_LLONG) {
  977. o(0x9948); /* cqto */
  978. o(0x48 + REX_BASE(fr));
  979. } else {
  980. o(0x99); /* cltd */
  981. }
  982. o(0xf7); /* idiv fr, %eax */
  983. o(0xf8 + fr);
  984. }
  985. if (op == '%' || op == TOK_UMOD)
  986. r = TREG_RDX;
  987. else
  988. r = TREG_RAX;
  989. }
  990. vtop->r = r;
  991. break;
  992. default:
  993. opc = 7;
  994. goto gen_op8;
  995. }
  996. }
  997. void gen_opl(int op)
  998. {
  999. gen_opi(op);
  1000. }
  1001. /* generate a floating point operation 'v = t1 op t2' instruction. The
  1002. two operands are guaranted to have the same floating point type */
  1003. /* XXX: need to use ST1 too */
  1004. void gen_opf(int op)
  1005. {
  1006. int a, ft, fc, swapped, r;
  1007. int float_type =
  1008. (vtop->type.t & VT_BTYPE) == VT_LDOUBLE ? RC_ST0 : RC_FLOAT;
  1009. /* convert constants to memory references */
  1010. if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
  1011. vswap();
  1012. gv(float_type);
  1013. vswap();
  1014. }
  1015. if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
  1016. gv(float_type);
  1017. /* must put at least one value in the floating point register */
  1018. if ((vtop[-1].r & VT_LVAL) &&
  1019. (vtop[0].r & VT_LVAL)) {
  1020. vswap();
  1021. gv(float_type);
  1022. vswap();
  1023. }
  1024. swapped = 0;
  1025. /* swap the stack if needed so that t1 is the register and t2 is
  1026. the memory reference */
  1027. if (vtop[-1].r & VT_LVAL) {
  1028. vswap();
  1029. swapped = 1;
  1030. }
  1031. if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
  1032. if (op >= TOK_ULT && op <= TOK_GT) {
  1033. /* load on stack second operand */
  1034. load(TREG_ST0, vtop);
  1035. save_reg(TREG_RAX); /* eax is used by FP comparison code */
  1036. if (op == TOK_GE || op == TOK_GT)
  1037. swapped = !swapped;
  1038. else if (op == TOK_EQ || op == TOK_NE)
  1039. swapped = 0;
  1040. if (swapped)
  1041. o(0xc9d9); /* fxch %st(1) */
  1042. o(0xe9da); /* fucompp */
  1043. o(0xe0df); /* fnstsw %ax */
  1044. if (op == TOK_EQ) {
  1045. o(0x45e480); /* and $0x45, %ah */
  1046. o(0x40fC80); /* cmp $0x40, %ah */
  1047. } else if (op == TOK_NE) {
  1048. o(0x45e480); /* and $0x45, %ah */
  1049. o(0x40f480); /* xor $0x40, %ah */
  1050. op = TOK_NE;
  1051. } else if (op == TOK_GE || op == TOK_LE) {
  1052. o(0x05c4f6); /* test $0x05, %ah */
  1053. op = TOK_EQ;
  1054. } else {
  1055. o(0x45c4f6); /* test $0x45, %ah */
  1056. op = TOK_EQ;
  1057. }
  1058. vtop--;
  1059. vtop->r = VT_CMP;
  1060. vtop->c.i = op;
  1061. } else {
  1062. /* no memory reference possible for long double operations */
  1063. load(TREG_ST0, vtop);
  1064. swapped = !swapped;
  1065. switch(op) {
  1066. default:
  1067. case '+':
  1068. a = 0;
  1069. break;
  1070. case '-':
  1071. a = 4;
  1072. if (swapped)
  1073. a++;
  1074. break;
  1075. case '*':
  1076. a = 1;
  1077. break;
  1078. case '/':
  1079. a = 6;
  1080. if (swapped)
  1081. a++;
  1082. break;
  1083. }
  1084. ft = vtop->type.t;
  1085. fc = vtop->c.ul;
  1086. o(0xde); /* fxxxp %st, %st(1) */
  1087. o(0xc1 + (a << 3));
  1088. vtop--;
  1089. }
  1090. } else {
  1091. if (op >= TOK_ULT && op <= TOK_GT) {
  1092. /* if saved lvalue, then we must reload it */
  1093. r = vtop->r;
  1094. fc = vtop->c.ul;
  1095. if ((r & VT_VALMASK) == VT_LLOCAL) {
  1096. SValue v1;
  1097. r = get_reg(RC_INT);
  1098. v1.type.t = VT_INT;
  1099. v1.r = VT_LOCAL | VT_LVAL;
  1100. v1.c.ul = fc;
  1101. load(r, &v1);
  1102. fc = 0;
  1103. }
  1104. if (op == TOK_EQ || op == TOK_NE) {
  1105. swapped = 0;
  1106. } else {
  1107. if (op == TOK_LE || op == TOK_LT)
  1108. swapped = !swapped;
  1109. if (op == TOK_LE || op == TOK_GE) {
  1110. op = 0x93; /* setae */
  1111. } else {
  1112. op = 0x97; /* seta */
  1113. }
  1114. }
  1115. if (swapped) {
  1116. o(0x7e0ff3); /* movq */
  1117. gen_modrm(1, r, vtop->sym, fc);
  1118. if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE) {
  1119. o(0x66);
  1120. }
  1121. o(0x2e0f); /* ucomisd %xmm0, %xmm1 */
  1122. o(0xc8);
  1123. } else {
  1124. if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE) {
  1125. o(0x66);
  1126. }
  1127. o(0x2e0f); /* ucomisd */
  1128. gen_modrm(0, r, vtop->sym, fc);
  1129. }
  1130. vtop--;
  1131. vtop->r = VT_CMP;
  1132. vtop->c.i = op;
  1133. } else {
  1134. /* no memory reference possible for long double operations */
  1135. if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
  1136. load(TREG_XMM0, vtop);
  1137. swapped = !swapped;
  1138. }
  1139. switch(op) {
  1140. default:
  1141. case '+':
  1142. a = 0;
  1143. break;
  1144. case '-':
  1145. a = 4;
  1146. break;
  1147. case '*':
  1148. a = 1;
  1149. break;
  1150. case '/':
  1151. a = 6;
  1152. break;
  1153. }
  1154. ft = vtop->type.t;
  1155. fc = vtop->c.ul;
  1156. if ((ft & VT_BTYPE) == VT_LDOUBLE) {
  1157. o(0xde); /* fxxxp %st, %st(1) */
  1158. o(0xc1 + (a << 3));
  1159. } else {
  1160. /* if saved lvalue, then we must reload it */
  1161. r = vtop->r;
  1162. if ((r & VT_VALMASK) == VT_LLOCAL) {
  1163. SValue v1;
  1164. r = get_reg(RC_INT);
  1165. v1.type.t = VT_INT;
  1166. v1.r = VT_LOCAL | VT_LVAL;
  1167. v1.c.ul = fc;
  1168. load(r, &v1);
  1169. fc = 0;
  1170. }
  1171. if (swapped) {
  1172. /* movq %xmm0,%xmm1 */
  1173. o(0x7e0ff3);
  1174. o(0xc8);
  1175. load(TREG_XMM0, vtop);
  1176. /* subsd %xmm1,%xmm0 (f2 0f 5c c1) */
  1177. if ((ft & VT_BTYPE) == VT_DOUBLE) {
  1178. o(0xf2);
  1179. } else {
  1180. o(0xf3);
  1181. }
  1182. o(0x0f);
  1183. o(0x58 + a);
  1184. o(0xc1);
  1185. } else {
  1186. if ((ft & VT_BTYPE) == VT_DOUBLE) {
  1187. o(0xf2);
  1188. } else {
  1189. o(0xf3);
  1190. }
  1191. o(0x0f);
  1192. o(0x58 + a);
  1193. gen_modrm(0, r, vtop->sym, fc);
  1194. }
  1195. }
  1196. vtop--;
  1197. }
  1198. }
  1199. }
  1200. /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
  1201. and 'long long' cases. */
  1202. void gen_cvt_itof(int t)
  1203. {
  1204. if ((t & VT_BTYPE) == VT_LDOUBLE) {
  1205. save_reg(TREG_ST0);
  1206. gv(RC_INT);
  1207. if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
  1208. /* signed long long to float/double/long double (unsigned case
  1209. is handled generically) */
  1210. o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
  1211. o(0x242cdf); /* fildll (%rsp) */
  1212. o(0x08c48348); /* add $8, %rsp */
  1213. } else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
  1214. (VT_INT | VT_UNSIGNED)) {
  1215. /* unsigned int to float/double/long double */
  1216. o(0x6a); /* push $0 */
  1217. g(0x00);
  1218. o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
  1219. o(0x242cdf); /* fildll (%rsp) */
  1220. o(0x10c48348); /* add $16, %rsp */
  1221. } else {
  1222. /* int to float/double/long double */
  1223. o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
  1224. o(0x2404db); /* fildl (%rsp) */
  1225. o(0x08c48348); /* add $8, %rsp */
  1226. }
  1227. vtop->r = TREG_ST0;
  1228. } else {
  1229. save_reg(TREG_XMM0);
  1230. gv(RC_INT);
  1231. o(0xf2 + ((t & VT_BTYPE) == VT_FLOAT));
  1232. if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
  1233. (VT_INT | VT_UNSIGNED) ||
  1234. (vtop->type.t & VT_BTYPE) == VT_LLONG) {
  1235. o(0x48); /* REX */
  1236. }
  1237. o(0x2a0f);
  1238. o(0xc0 + (vtop->r & VT_VALMASK)); /* cvtsi2sd */
  1239. vtop->r = TREG_XMM0;
  1240. }
  1241. }
  1242. /* convert from one floating point type to another */
  1243. void gen_cvt_ftof(int t)
  1244. {
  1245. int ft, bt, tbt;
  1246. ft = vtop->type.t;
  1247. bt = ft & VT_BTYPE;
  1248. tbt = t & VT_BTYPE;
  1249. if (bt == VT_FLOAT) {
  1250. gv(RC_FLOAT);
  1251. if (tbt == VT_DOUBLE) {
  1252. o(0xc0140f); /* unpcklps */
  1253. o(0xc05a0f); /* cvtps2pd */
  1254. } else if (tbt == VT_LDOUBLE) {
  1255. /* movss %xmm0,-0x10(%rsp) */
  1256. o(0x44110ff3);
  1257. o(0xf024);
  1258. o(0xf02444d9); /* flds -0x10(%rsp) */
  1259. vtop->r = TREG_ST0;
  1260. }
  1261. } else if (bt == VT_DOUBLE) {
  1262. gv(RC_FLOAT);
  1263. if (tbt == VT_FLOAT) {
  1264. o(0xc0140f66); /* unpcklpd */
  1265. o(0xc05a0f66); /* cvtpd2ps */
  1266. } else if (tbt == VT_LDOUBLE) {
  1267. /* movsd %xmm0,-0x10(%rsp) */
  1268. o(0x44110ff2);
  1269. o(0xf024);
  1270. o(0xf02444dd); /* fldl -0x10(%rsp) */
  1271. vtop->r = TREG_ST0;
  1272. }
  1273. } else {
  1274. gv(RC_ST0);
  1275. if (tbt == VT_DOUBLE) {
  1276. o(0xf0245cdd); /* fstpl -0x10(%rsp) */
  1277. /* movsd -0x10(%rsp),%xmm0 */
  1278. o(0x44100ff2);
  1279. o(0xf024);
  1280. vtop->r = TREG_XMM0;
  1281. } else if (tbt == VT_FLOAT) {
  1282. o(0xf0245cd9); /* fstps -0x10(%rsp) */
  1283. /* movss -0x10(%rsp),%xmm0 */
  1284. o(0x44100ff3);
  1285. o(0xf024);
  1286. vtop->r = TREG_XMM0;
  1287. }
  1288. }
  1289. }
  1290. /* convert fp to int 't' type */
  1291. void gen_cvt_ftoi(int t)
  1292. {
  1293. int ft, bt, size, r;
  1294. ft = vtop->type.t;
  1295. bt = ft & VT_BTYPE;
  1296. if (bt == VT_LDOUBLE) {
  1297. gen_cvt_ftof(VT_DOUBLE);
  1298. bt = VT_DOUBLE;
  1299. }
  1300. gv(RC_FLOAT);
  1301. if (t != VT_INT)
  1302. size = 8;
  1303. else
  1304. size = 4;
  1305. r = get_reg(RC_INT);
  1306. if (bt == VT_FLOAT) {
  1307. o(0xf3);
  1308. } else if (bt == VT_DOUBLE) {
  1309. o(0xf2);
  1310. } else {
  1311. assert(0);
  1312. }
  1313. if (size == 8) {
  1314. o(0x48 + REX_BASE(r));
  1315. }
  1316. o(0x2c0f); /* cvttss2si or cvttsd2si */
  1317. o(0xc0 + (REG_VALUE(r) << 3));
  1318. vtop->r = r;
  1319. }
  1320. /* computed goto support */
  1321. void ggoto(void)
  1322. {
  1323. gcall_or_jmp(1);
  1324. vtop--;
  1325. }
  1326. /* end of x86-64 code generator */
  1327. /*************************************************************/