x86_64-gen.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230
  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. #ifdef TARGET_DEFS_ONLY
  23. /* number of available registers */
  24. #define NB_REGS 25
  25. #define NB_ASM_REGS 16
  26. #define CONFIG_TCC_ASM
  27. /* a register can belong to several classes. The classes must be
  28. sorted from more general to more precise (see gv2() code which does
  29. assumptions on it). */
  30. #define RC_INT 0x0001 /* generic integer register */
  31. #define RC_FLOAT 0x0002 /* generic float register */
  32. #define RC_RAX 0x0004
  33. #define RC_RCX 0x0008
  34. #define RC_RDX 0x0010
  35. #define RC_ST0 0x0080 /* only for long double */
  36. #define RC_R8 0x0100
  37. #define RC_R9 0x0200
  38. #define RC_R10 0x0400
  39. #define RC_R11 0x0800
  40. #define RC_XMM0 0x1000
  41. #define RC_XMM1 0x2000
  42. #define RC_XMM2 0x4000
  43. #define RC_XMM3 0x8000
  44. #define RC_XMM4 0x10000
  45. #define RC_XMM5 0x20000
  46. #define RC_XMM6 0x40000
  47. #define RC_XMM7 0x80000
  48. #define RC_IRET RC_RAX /* function return: integer register */
  49. #define RC_LRET RC_RDX /* function return: second integer register */
  50. #define RC_FRET RC_XMM0 /* function return: float register */
  51. #define RC_QRET RC_XMM1 /* function return: second float register */
  52. /* pretty names for the registers */
  53. enum {
  54. TREG_RAX = 0,
  55. TREG_RCX = 1,
  56. TREG_RDX = 2,
  57. TREG_RSP = 4,
  58. TREG_RSI = 6,
  59. TREG_RDI = 7,
  60. TREG_R8 = 8,
  61. TREG_R9 = 9,
  62. TREG_R10 = 10,
  63. TREG_R11 = 11,
  64. TREG_XMM0 = 16,
  65. TREG_XMM1 = 17,
  66. TREG_XMM2 = 18,
  67. TREG_XMM3 = 19,
  68. TREG_XMM4 = 20,
  69. TREG_XMM5 = 21,
  70. TREG_XMM6 = 22,
  71. TREG_XMM7 = 23,
  72. TREG_ST0 = 24,
  73. TREG_MEM = 0x20
  74. };
  75. #define REX_BASE(reg) (((reg) >> 3) & 1)
  76. #define REG_VALUE(reg) ((reg) & 7)
  77. /* return registers for function */
  78. #define REG_IRET TREG_RAX /* single word int return register */
  79. #define REG_LRET TREG_RDX /* second word return register (for long long) */
  80. #define REG_FRET TREG_XMM0 /* float return register */
  81. #define REG_QRET TREG_XMM1 /* second float return register */
  82. /* defined if function parameters must be evaluated in reverse order */
  83. #define INVERT_FUNC_PARAMS
  84. /* pointer size, in bytes */
  85. #define PTR_SIZE 8
  86. /* long double size and alignment, in bytes */
  87. #define LDOUBLE_SIZE 16
  88. #define LDOUBLE_ALIGN 16
  89. /* maximum alignment (for aligned attribute support) */
  90. #define MAX_ALIGN 16
  91. /******************************************************/
  92. #else /* ! TARGET_DEFS_ONLY */
  93. /******************************************************/
  94. #include "tcc.h"
  95. #include <assert.h>
  96. ST_DATA const int reg_classes[NB_REGS] = {
  97. /* eax */ RC_INT | RC_RAX,
  98. /* ecx */ RC_INT | RC_RCX,
  99. /* edx */ RC_INT | RC_RDX,
  100. 0,
  101. 0,
  102. 0,
  103. 0,
  104. 0,
  105. RC_R8,
  106. RC_R9,
  107. RC_R10,
  108. RC_R11,
  109. 0,
  110. 0,
  111. 0,
  112. 0,
  113. /* xmm0 */ RC_FLOAT | RC_XMM0,
  114. /* xmm1 */ RC_FLOAT | RC_XMM1,
  115. /* xmm2 */ RC_FLOAT | RC_XMM2,
  116. /* xmm3 */ RC_FLOAT | RC_XMM3,
  117. /* xmm4 */ RC_FLOAT | RC_XMM4,
  118. /* xmm5 */ RC_FLOAT | RC_XMM5,
  119. /* xmm6 an xmm7 are included so gv() can be used on them,
  120. but they are not tagged with RC_FLOAT because they are
  121. callee saved on Windows */
  122. RC_XMM6,
  123. RC_XMM7,
  124. /* st0 */ RC_ST0
  125. };
  126. static unsigned long func_sub_sp_offset;
  127. static int func_ret_sub;
  128. /* XXX: make it faster ? */
  129. ST_FUNC void g(int c)
  130. {
  131. int ind1;
  132. if (nocode_wanted)
  133. return;
  134. ind1 = ind + 1;
  135. if (ind1 > cur_text_section->data_allocated)
  136. section_realloc(cur_text_section, ind1);
  137. cur_text_section->data[ind] = c;
  138. ind = ind1;
  139. }
  140. ST_FUNC void o(unsigned int c)
  141. {
  142. while (c) {
  143. g(c);
  144. c = c >> 8;
  145. }
  146. }
  147. ST_FUNC void gen_le16(int v)
  148. {
  149. g(v);
  150. g(v >> 8);
  151. }
  152. ST_FUNC void gen_le32(int c)
  153. {
  154. g(c);
  155. g(c >> 8);
  156. g(c >> 16);
  157. g(c >> 24);
  158. }
  159. ST_FUNC void gen_le64(int64_t c)
  160. {
  161. g(c);
  162. g(c >> 8);
  163. g(c >> 16);
  164. g(c >> 24);
  165. g(c >> 32);
  166. g(c >> 40);
  167. g(c >> 48);
  168. g(c >> 56);
  169. }
  170. static void orex(int ll, int r, int r2, int b)
  171. {
  172. if ((r & VT_VALMASK) >= VT_CONST)
  173. r = 0;
  174. if ((r2 & VT_VALMASK) >= VT_CONST)
  175. r2 = 0;
  176. if (ll || REX_BASE(r) || REX_BASE(r2))
  177. o(0x40 | REX_BASE(r) | (REX_BASE(r2) << 2) | (ll << 3));
  178. o(b);
  179. }
  180. /* output a symbol and patch all calls to it */
  181. ST_FUNC void gsym_addr(int t, int a)
  182. {
  183. while (t) {
  184. unsigned char *ptr = cur_text_section->data + t;
  185. uint32_t n = read32le(ptr); /* next value */
  186. write32le(ptr, a - t - 4);
  187. t = n;
  188. }
  189. }
  190. void gsym(int t)
  191. {
  192. gsym_addr(t, ind);
  193. }
  194. static int is64_type(int t)
  195. {
  196. return ((t & VT_BTYPE) == VT_PTR ||
  197. (t & VT_BTYPE) == VT_FUNC ||
  198. (t & VT_BTYPE) == VT_LLONG);
  199. }
  200. /* instruction + 4 bytes data. Return the address of the data */
  201. static int oad(int c, int s)
  202. {
  203. int t;
  204. if (nocode_wanted)
  205. return s;
  206. o(c);
  207. t = ind;
  208. gen_le32(s);
  209. return t;
  210. }
  211. /* generate jmp to a label */
  212. #define gjmp2(instr,lbl) oad(instr,lbl)
  213. ST_FUNC void gen_addr32(int r, Sym *sym, int c)
  214. {
  215. if (r & VT_SYM)
  216. greloca(cur_text_section, sym, ind, R_X86_64_32S, c), c=0;
  217. gen_le32(c);
  218. }
  219. /* output constant with relocation if 'r & VT_SYM' is true */
  220. ST_FUNC void gen_addr64(int r, Sym *sym, int64_t c)
  221. {
  222. if (r & VT_SYM)
  223. greloca(cur_text_section, sym, ind, R_X86_64_64, c), c=0;
  224. gen_le64(c);
  225. }
  226. /* output constant with relocation if 'r & VT_SYM' is true */
  227. ST_FUNC void gen_addrpc32(int r, Sym *sym, int c)
  228. {
  229. greloca(cur_text_section, sym, ind, R_X86_64_PC32, 0);
  230. gen_le32(c-4);
  231. }
  232. /* output got address with relocation */
  233. static void gen_gotpcrel(int r, Sym *sym, int c)
  234. {
  235. #ifdef TCC_TARGET_PE
  236. tcc_error("internal error: no GOT on PE: %s %x %x | %02x %02x %02x\n",
  237. get_tok_str(sym->v, NULL), c, r,
  238. cur_text_section->data[ind-3],
  239. cur_text_section->data[ind-2],
  240. cur_text_section->data[ind-1]
  241. );
  242. #endif
  243. greloca(cur_text_section, sym, ind, R_X86_64_GOTPCREL, -4);
  244. gen_le32(0);
  245. if (c) {
  246. /* we use add c, %xxx for displacement */
  247. orex(1, r, 0, 0x81);
  248. o(0xc0 + REG_VALUE(r));
  249. gen_le32(c);
  250. }
  251. }
  252. static void gen_modrm_impl(int op_reg, int r, Sym *sym, int c, int is_got)
  253. {
  254. op_reg = REG_VALUE(op_reg) << 3;
  255. if ((r & VT_VALMASK) == VT_CONST) {
  256. /* constant memory reference */
  257. o(0x05 | op_reg);
  258. if (is_got) {
  259. gen_gotpcrel(r, sym, c);
  260. } else {
  261. gen_addrpc32(r, sym, c);
  262. }
  263. } else if ((r & VT_VALMASK) == VT_LOCAL) {
  264. /* currently, we use only ebp as base */
  265. if (c == (char)c) {
  266. /* short reference */
  267. o(0x45 | op_reg);
  268. g(c);
  269. } else {
  270. oad(0x85 | op_reg, c);
  271. }
  272. } else if ((r & VT_VALMASK) >= TREG_MEM) {
  273. if (c) {
  274. g(0x80 | op_reg | REG_VALUE(r));
  275. gen_le32(c);
  276. } else {
  277. g(0x00 | op_reg | REG_VALUE(r));
  278. }
  279. } else {
  280. g(0x00 | op_reg | REG_VALUE(r));
  281. }
  282. }
  283. /* generate a modrm reference. 'op_reg' contains the additional 3
  284. opcode bits */
  285. static void gen_modrm(int op_reg, int r, Sym *sym, int c)
  286. {
  287. gen_modrm_impl(op_reg, r, sym, c, 0);
  288. }
  289. /* generate a modrm reference. 'op_reg' contains the additional 3
  290. opcode bits */
  291. static void gen_modrm64(int opcode, int op_reg, int r, Sym *sym, int c)
  292. {
  293. int is_got;
  294. is_got = (op_reg & TREG_MEM) && !(sym->type.t & VT_STATIC);
  295. orex(1, r, op_reg, opcode);
  296. gen_modrm_impl(op_reg, r, sym, c, is_got);
  297. }
  298. /* load 'r' from value 'sv' */
  299. void load(int r, SValue *sv)
  300. {
  301. int v, t, ft, fc, fr;
  302. SValue v1;
  303. #ifdef TCC_TARGET_PE
  304. SValue v2;
  305. sv = pe_getimport(sv, &v2);
  306. #endif
  307. fr = sv->r;
  308. ft = sv->type.t & ~VT_DEFSIGN;
  309. fc = sv->c.i;
  310. if (fc != sv->c.i && (fr & VT_SYM))
  311. tcc_error("64 bit addend in load");
  312. ft &= ~(VT_VOLATILE | VT_CONSTANT);
  313. #ifndef TCC_TARGET_PE
  314. /* we use indirect access via got */
  315. if ((fr & VT_VALMASK) == VT_CONST && (fr & VT_SYM) &&
  316. (fr & VT_LVAL) && !(sv->sym->type.t & VT_STATIC)) {
  317. /* use the result register as a temporal register */
  318. int tr = r | TREG_MEM;
  319. if (is_float(ft)) {
  320. /* we cannot use float registers as a temporal register */
  321. tr = get_reg(RC_INT) | TREG_MEM;
  322. }
  323. gen_modrm64(0x8b, tr, fr, sv->sym, 0);
  324. /* load from the temporal register */
  325. fr = tr | VT_LVAL;
  326. }
  327. #endif
  328. v = fr & VT_VALMASK;
  329. if (fr & VT_LVAL) {
  330. int b, ll;
  331. if (v == VT_LLOCAL) {
  332. v1.type.t = VT_PTR;
  333. v1.r = VT_LOCAL | VT_LVAL;
  334. v1.c.i = fc;
  335. fr = r;
  336. if (!(reg_classes[fr] & (RC_INT|RC_R11)))
  337. fr = get_reg(RC_INT);
  338. load(fr, &v1);
  339. }
  340. ll = 0;
  341. /* Like GCC we can load from small enough properly sized
  342. structs and unions as well.
  343. XXX maybe move to generic operand handling, but should
  344. occur only with asm, so tccasm.c might also be a better place */
  345. if ((ft & VT_BTYPE) == VT_STRUCT) {
  346. int align;
  347. switch (type_size(&sv->type, &align)) {
  348. case 1: ft = VT_BYTE; break;
  349. case 2: ft = VT_SHORT; break;
  350. case 4: ft = VT_INT; break;
  351. case 8: ft = VT_LLONG; break;
  352. default:
  353. tcc_error("invalid aggregate type for register load");
  354. break;
  355. }
  356. }
  357. if ((ft & VT_BTYPE) == VT_FLOAT) {
  358. b = 0x6e0f66;
  359. r = REG_VALUE(r); /* movd */
  360. } else if ((ft & VT_BTYPE) == VT_DOUBLE) {
  361. b = 0x7e0ff3; /* movq */
  362. r = REG_VALUE(r);
  363. } else if ((ft & VT_BTYPE) == VT_LDOUBLE) {
  364. b = 0xdb, r = 5; /* fldt */
  365. } else if ((ft & VT_TYPE) == VT_BYTE || (ft & VT_TYPE) == VT_BOOL) {
  366. b = 0xbe0f; /* movsbl */
  367. } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) {
  368. b = 0xb60f; /* movzbl */
  369. } else if ((ft & VT_TYPE) == VT_SHORT) {
  370. b = 0xbf0f; /* movswl */
  371. } else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) {
  372. b = 0xb70f; /* movzwl */
  373. } else {
  374. assert(((ft & VT_BTYPE) == VT_INT)
  375. || ((ft & VT_BTYPE) == VT_LLONG)
  376. || ((ft & VT_BTYPE) == VT_PTR)
  377. || ((ft & VT_BTYPE) == VT_FUNC)
  378. );
  379. ll = is64_type(ft);
  380. b = 0x8b;
  381. }
  382. if (ll) {
  383. gen_modrm64(b, r, fr, sv->sym, fc);
  384. } else {
  385. orex(ll, fr, r, b);
  386. gen_modrm(r, fr, sv->sym, fc);
  387. }
  388. } else {
  389. if (v == VT_CONST) {
  390. if (fr & VT_SYM) {
  391. #ifdef TCC_TARGET_PE
  392. orex(1,0,r,0x8d);
  393. o(0x05 + REG_VALUE(r) * 8); /* lea xx(%rip), r */
  394. gen_addrpc32(fr, sv->sym, fc);
  395. #else
  396. if (sv->sym->type.t & VT_STATIC) {
  397. orex(1,0,r,0x8d);
  398. o(0x05 + REG_VALUE(r) * 8); /* lea xx(%rip), r */
  399. gen_addrpc32(fr, sv->sym, fc);
  400. } else {
  401. orex(1,0,r,0x8b);
  402. o(0x05 + REG_VALUE(r) * 8); /* mov xx(%rip), r */
  403. gen_gotpcrel(r, sv->sym, fc);
  404. }
  405. #endif
  406. } else if (is64_type(ft)) {
  407. orex(1,r,0, 0xb8 + REG_VALUE(r)); /* mov $xx, r */
  408. gen_le64(sv->c.i);
  409. } else {
  410. orex(0,r,0, 0xb8 + REG_VALUE(r)); /* mov $xx, r */
  411. gen_le32(fc);
  412. }
  413. } else if (v == VT_LOCAL) {
  414. orex(1,0,r,0x8d); /* lea xxx(%ebp), r */
  415. gen_modrm(r, VT_LOCAL, sv->sym, fc);
  416. } else if (v == VT_CMP) {
  417. orex(0,r,0,0);
  418. if ((fc & ~0x100) != TOK_NE)
  419. oad(0xb8 + REG_VALUE(r), 0); /* mov $0, r */
  420. else
  421. oad(0xb8 + REG_VALUE(r), 1); /* mov $1, r */
  422. if (fc & 0x100)
  423. {
  424. /* This was a float compare. If the parity bit is
  425. set the result was unordered, meaning false for everything
  426. except TOK_NE, and true for TOK_NE. */
  427. fc &= ~0x100;
  428. o(0x037a + (REX_BASE(r) << 8));
  429. }
  430. orex(0,r,0, 0x0f); /* setxx %br */
  431. o(fc);
  432. o(0xc0 + REG_VALUE(r));
  433. } else if (v == VT_JMP || v == VT_JMPI) {
  434. t = v & 1;
  435. orex(0,r,0,0);
  436. oad(0xb8 + REG_VALUE(r), t); /* mov $1, r */
  437. o(0x05eb + (REX_BASE(r) << 8)); /* jmp after */
  438. gsym(fc);
  439. orex(0,r,0,0);
  440. oad(0xb8 + REG_VALUE(r), t ^ 1); /* mov $0, r */
  441. } else if (v != r) {
  442. if ((r >= TREG_XMM0) && (r <= TREG_XMM7)) {
  443. if (v == TREG_ST0) {
  444. /* gen_cvt_ftof(VT_DOUBLE); */
  445. o(0xf0245cdd); /* fstpl -0x10(%rsp) */
  446. /* movsd -0x10(%rsp),%xmmN */
  447. o(0x100ff2);
  448. o(0x44 + REG_VALUE(r)*8); /* %xmmN */
  449. o(0xf024);
  450. } else {
  451. assert((v >= TREG_XMM0) && (v <= TREG_XMM7));
  452. if ((ft & VT_BTYPE) == VT_FLOAT) {
  453. o(0x100ff3);
  454. } else {
  455. assert((ft & VT_BTYPE) == VT_DOUBLE);
  456. o(0x100ff2);
  457. }
  458. o(0xc0 + REG_VALUE(v) + REG_VALUE(r)*8);
  459. }
  460. } else if (r == TREG_ST0) {
  461. assert((v >= TREG_XMM0) && (v <= TREG_XMM7));
  462. /* gen_cvt_ftof(VT_LDOUBLE); */
  463. /* movsd %xmmN,-0x10(%rsp) */
  464. o(0x110ff2);
  465. o(0x44 + REG_VALUE(r)*8); /* %xmmN */
  466. o(0xf024);
  467. o(0xf02444dd); /* fldl -0x10(%rsp) */
  468. } else {
  469. orex(1,r,v, 0x89);
  470. o(0xc0 + REG_VALUE(r) + REG_VALUE(v) * 8); /* mov v, r */
  471. }
  472. }
  473. }
  474. }
  475. /* store register 'r' in lvalue 'v' */
  476. void store(int r, SValue *v)
  477. {
  478. int fr, bt, ft, fc;
  479. int op64 = 0;
  480. /* store the REX prefix in this variable when PIC is enabled */
  481. int pic = 0;
  482. #ifdef TCC_TARGET_PE
  483. SValue v2;
  484. v = pe_getimport(v, &v2);
  485. #endif
  486. fr = v->r & VT_VALMASK;
  487. ft = v->type.t;
  488. fc = v->c.i;
  489. if (fc != v->c.i && (fr & VT_SYM))
  490. tcc_error("64 bit addend in store");
  491. ft &= ~(VT_VOLATILE | VT_CONSTANT);
  492. bt = ft & VT_BTYPE;
  493. #ifndef TCC_TARGET_PE
  494. /* we need to access the variable via got */
  495. if (fr == VT_CONST && (v->r & VT_SYM)) {
  496. /* mov xx(%rip), %r11 */
  497. o(0x1d8b4c);
  498. gen_gotpcrel(TREG_R11, v->sym, v->c.i);
  499. pic = is64_type(bt) ? 0x49 : 0x41;
  500. }
  501. #endif
  502. /* XXX: incorrect if float reg to reg */
  503. if (bt == VT_FLOAT) {
  504. o(0x66);
  505. o(pic);
  506. o(0x7e0f); /* movd */
  507. r = REG_VALUE(r);
  508. } else if (bt == VT_DOUBLE) {
  509. o(0x66);
  510. o(pic);
  511. o(0xd60f); /* movq */
  512. r = REG_VALUE(r);
  513. } else if (bt == VT_LDOUBLE) {
  514. o(0xc0d9); /* fld %st(0) */
  515. o(pic);
  516. o(0xdb); /* fstpt */
  517. r = 7;
  518. } else {
  519. if (bt == VT_SHORT)
  520. o(0x66);
  521. o(pic);
  522. if (bt == VT_BYTE || bt == VT_BOOL)
  523. orex(0, 0, r, 0x88);
  524. else if (is64_type(bt))
  525. op64 = 0x89;
  526. else
  527. orex(0, 0, r, 0x89);
  528. }
  529. if (pic) {
  530. /* xxx r, (%r11) where xxx is mov, movq, fld, or etc */
  531. if (op64)
  532. o(op64);
  533. o(3 + (r << 3));
  534. } else if (op64) {
  535. if (fr == VT_CONST || fr == VT_LOCAL || (v->r & VT_LVAL)) {
  536. gen_modrm64(op64, r, v->r, v->sym, fc);
  537. } else if (fr != r) {
  538. /* XXX: don't we really come here? */
  539. abort();
  540. o(0xc0 + fr + r * 8); /* mov r, fr */
  541. }
  542. } else {
  543. if (fr == VT_CONST || fr == VT_LOCAL || (v->r & VT_LVAL)) {
  544. gen_modrm(r, v->r, v->sym, fc);
  545. } else if (fr != r) {
  546. /* XXX: don't we really come here? */
  547. abort();
  548. o(0xc0 + fr + r * 8); /* mov r, fr */
  549. }
  550. }
  551. }
  552. /* 'is_jmp' is '1' if it is a jump */
  553. static void gcall_or_jmp(int is_jmp)
  554. {
  555. int r;
  556. if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST &&
  557. ((vtop->r & VT_SYM) || (vtop->c.i-4) == (int)(vtop->c.i-4))) {
  558. /* constant case */
  559. if (vtop->r & VT_SYM) {
  560. /* relocation case */
  561. #ifdef TCC_TARGET_PE
  562. greloca(cur_text_section, vtop->sym, ind + 1, R_X86_64_PC32, (int)(vtop->c.i-4));
  563. #else
  564. greloca(cur_text_section, vtop->sym, ind + 1, R_X86_64_PLT32, (int)(vtop->c.i-4));
  565. #endif
  566. } else {
  567. /* put an empty PC32 relocation */
  568. put_elf_reloca(symtab_section, cur_text_section,
  569. ind + 1, R_X86_64_PC32, 0, (int)(vtop->c.i-4));
  570. }
  571. oad(0xe8 + is_jmp, 0); /* call/jmp im */
  572. } else {
  573. /* otherwise, indirect call */
  574. r = TREG_R11;
  575. load(r, vtop);
  576. o(0x41); /* REX */
  577. o(0xff); /* call/jmp *r */
  578. o(0xd0 + REG_VALUE(r) + (is_jmp << 4));
  579. }
  580. }
  581. #if defined(CONFIG_TCC_BCHECK)
  582. #ifndef TCC_TARGET_PE
  583. static addr_t func_bound_offset;
  584. static unsigned long func_bound_ind;
  585. #endif
  586. static void gen_static_call(int v)
  587. {
  588. Sym *sym = external_global_sym(v, &func_old_type, 0);
  589. oad(0xe8, 0);
  590. greloca(cur_text_section, sym, ind-4, R_X86_64_PC32, -4);
  591. }
  592. /* generate a bounded pointer addition */
  593. ST_FUNC void gen_bounded_ptr_add(void)
  594. {
  595. /* save all temporary registers */
  596. save_regs(0);
  597. /* prepare fast x86_64 function call */
  598. gv(RC_RAX);
  599. o(0xc68948); // mov %rax,%rsi ## second arg in %rsi, this must be size
  600. vtop--;
  601. gv(RC_RAX);
  602. o(0xc78948); // mov %rax,%rdi ## first arg in %rdi, this must be ptr
  603. vtop--;
  604. /* do a fast function call */
  605. gen_static_call(TOK___bound_ptr_add);
  606. /* returned pointer is in rax */
  607. vtop++;
  608. vtop->r = TREG_RAX | VT_BOUNDED;
  609. /* relocation offset of the bounding function call point */
  610. vtop->c.i = (cur_text_section->reloc->data_offset - sizeof(ElfW(Rela)));
  611. }
  612. /* patch pointer addition in vtop so that pointer dereferencing is
  613. also tested */
  614. ST_FUNC void gen_bounded_ptr_deref(void)
  615. {
  616. addr_t func;
  617. int size, align;
  618. ElfW(Rela) *rel;
  619. Sym *sym;
  620. size = 0;
  621. /* XXX: put that code in generic part of tcc */
  622. if (!is_float(vtop->type.t)) {
  623. if (vtop->r & VT_LVAL_BYTE)
  624. size = 1;
  625. else if (vtop->r & VT_LVAL_SHORT)
  626. size = 2;
  627. }
  628. if (!size)
  629. size = type_size(&vtop->type, &align);
  630. switch(size) {
  631. case 1: func = TOK___bound_ptr_indir1; break;
  632. case 2: func = TOK___bound_ptr_indir2; break;
  633. case 4: func = TOK___bound_ptr_indir4; break;
  634. case 8: func = TOK___bound_ptr_indir8; break;
  635. case 12: func = TOK___bound_ptr_indir12; break;
  636. case 16: func = TOK___bound_ptr_indir16; break;
  637. default:
  638. tcc_error("unhandled size when dereferencing bounded pointer");
  639. func = 0;
  640. break;
  641. }
  642. sym = external_global_sym(func, &func_old_type, 0);
  643. if (!sym->c)
  644. put_extern_sym(sym, NULL, 0, 0);
  645. /* patch relocation */
  646. /* XXX: find a better solution ? */
  647. rel = (ElfW(Rela) *)(cur_text_section->reloc->data + vtop->c.i);
  648. rel->r_info = ELF64_R_INFO(sym->c, ELF64_R_TYPE(rel->r_info));
  649. }
  650. #endif
  651. #ifdef TCC_TARGET_PE
  652. #define REGN 4
  653. static const uint8_t arg_regs[REGN] = {
  654. TREG_RCX, TREG_RDX, TREG_R8, TREG_R9
  655. };
  656. /* Prepare arguments in R10 and R11 rather than RCX and RDX
  657. because gv() will not ever use these */
  658. static int arg_prepare_reg(int idx) {
  659. if (idx == 0 || idx == 1)
  660. /* idx=0: r10, idx=1: r11 */
  661. return idx + 10;
  662. else
  663. return arg_regs[idx];
  664. }
  665. static int func_scratch, func_alloca;
  666. /* Generate function call. The function address is pushed first, then
  667. all the parameters in call order. This functions pops all the
  668. parameters and the function address. */
  669. static void gen_offs_sp(int b, int r, int d)
  670. {
  671. orex(1,0,r & 0x100 ? 0 : r, b);
  672. if (d == (char)d) {
  673. o(0x2444 | (REG_VALUE(r) << 3));
  674. g(d);
  675. } else {
  676. o(0x2484 | (REG_VALUE(r) << 3));
  677. gen_le32(d);
  678. }
  679. }
  680. static int using_regs(int size)
  681. {
  682. return !(size > 8 || (size & (size - 1)));
  683. }
  684. /* Return the number of registers needed to return the struct, or 0 if
  685. returning via struct pointer. */
  686. ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align, int *regsize)
  687. {
  688. int size, align;
  689. *ret_align = 1; // Never have to re-align return values for x86-64
  690. *regsize = 8;
  691. size = type_size(vt, &align);
  692. if (!using_regs(size))
  693. return 0;
  694. if (size == 8)
  695. ret->t = VT_LLONG;
  696. else if (size == 4)
  697. ret->t = VT_INT;
  698. else if (size == 2)
  699. ret->t = VT_SHORT;
  700. else
  701. ret->t = VT_BYTE;
  702. ret->ref = NULL;
  703. return 1;
  704. }
  705. static int is_sse_float(int t) {
  706. int bt;
  707. bt = t & VT_BTYPE;
  708. return bt == VT_DOUBLE || bt == VT_FLOAT;
  709. }
  710. static int gfunc_arg_size(CType *type) {
  711. int align;
  712. if (type->t & (VT_ARRAY|VT_BITFIELD))
  713. return 8;
  714. return type_size(type, &align);
  715. }
  716. void gfunc_call(int nb_args)
  717. {
  718. int size, r, args_size, i, d, bt, struct_size;
  719. int arg;
  720. args_size = (nb_args < REGN ? REGN : nb_args) * PTR_SIZE;
  721. arg = nb_args;
  722. /* for struct arguments, we need to call memcpy and the function
  723. call breaks register passing arguments we are preparing.
  724. So, we process arguments which will be passed by stack first. */
  725. struct_size = args_size;
  726. for(i = 0; i < nb_args; i++) {
  727. SValue *sv;
  728. --arg;
  729. sv = &vtop[-i];
  730. bt = (sv->type.t & VT_BTYPE);
  731. size = gfunc_arg_size(&sv->type);
  732. if (using_regs(size))
  733. continue; /* arguments smaller than 8 bytes passed in registers or on stack */
  734. if (bt == VT_STRUCT) {
  735. /* align to stack align size */
  736. size = (size + 15) & ~15;
  737. /* generate structure store */
  738. r = get_reg(RC_INT);
  739. gen_offs_sp(0x8d, r, struct_size);
  740. struct_size += size;
  741. /* generate memcpy call */
  742. vset(&sv->type, r | VT_LVAL, 0);
  743. vpushv(sv);
  744. vstore();
  745. --vtop;
  746. } else if (bt == VT_LDOUBLE) {
  747. gv(RC_ST0);
  748. gen_offs_sp(0xdb, 0x107, struct_size);
  749. struct_size += 16;
  750. }
  751. }
  752. if (func_scratch < struct_size)
  753. func_scratch = struct_size;
  754. arg = nb_args;
  755. struct_size = args_size;
  756. for(i = 0; i < nb_args; i++) {
  757. --arg;
  758. bt = (vtop->type.t & VT_BTYPE);
  759. size = gfunc_arg_size(&vtop->type);
  760. if (!using_regs(size)) {
  761. /* align to stack align size */
  762. size = (size + 15) & ~15;
  763. if (arg >= REGN) {
  764. d = get_reg(RC_INT);
  765. gen_offs_sp(0x8d, d, struct_size);
  766. gen_offs_sp(0x89, d, arg*8);
  767. } else {
  768. d = arg_prepare_reg(arg);
  769. gen_offs_sp(0x8d, d, struct_size);
  770. }
  771. struct_size += size;
  772. } else {
  773. if (is_sse_float(vtop->type.t)) {
  774. if (tcc_state->nosse)
  775. tcc_error("SSE disabled");
  776. gv(RC_XMM0); /* only use one float register */
  777. if (arg >= REGN) {
  778. /* movq %xmm0, j*8(%rsp) */
  779. gen_offs_sp(0xd60f66, 0x100, arg*8);
  780. } else {
  781. /* movaps %xmm0, %xmmN */
  782. o(0x280f);
  783. o(0xc0 + (arg << 3));
  784. d = arg_prepare_reg(arg);
  785. /* mov %xmm0, %rxx */
  786. o(0x66);
  787. orex(1,d,0, 0x7e0f);
  788. o(0xc0 + REG_VALUE(d));
  789. }
  790. } else {
  791. if (bt == VT_STRUCT) {
  792. vtop->type.ref = NULL;
  793. vtop->type.t = size > 4 ? VT_LLONG : size > 2 ? VT_INT
  794. : size > 1 ? VT_SHORT : VT_BYTE;
  795. }
  796. r = gv(RC_INT);
  797. if (arg >= REGN) {
  798. gen_offs_sp(0x89, r, arg*8);
  799. } else {
  800. d = arg_prepare_reg(arg);
  801. orex(1,d,r,0x89); /* mov */
  802. o(0xc0 + REG_VALUE(r) * 8 + REG_VALUE(d));
  803. }
  804. }
  805. }
  806. vtop--;
  807. }
  808. save_regs(0);
  809. /* Copy R10 and R11 into RCX and RDX, respectively */
  810. if (nb_args > 0) {
  811. o(0xd1894c); /* mov %r10, %rcx */
  812. if (nb_args > 1) {
  813. o(0xda894c); /* mov %r11, %rdx */
  814. }
  815. }
  816. gcall_or_jmp(0);
  817. if ((vtop->r & VT_SYM) && vtop->sym->v == TOK_alloca) {
  818. /* need to add the "func_scratch" area after alloca */
  819. o(0x0548), gen_le32(func_alloca), func_alloca = ind - 4;
  820. }
  821. /* other compilers don't clear the upper bits when returning char/short */
  822. bt = vtop->type.ref->type.t & (VT_BTYPE | VT_UNSIGNED);
  823. if (bt == (VT_BYTE | VT_UNSIGNED))
  824. o(0xc0b60f); /* movzbl %al, %eax */
  825. else if (bt == VT_BYTE)
  826. o(0xc0be0f); /* movsbl %al, %eax */
  827. else if (bt == VT_SHORT)
  828. o(0x98); /* cwtl */
  829. else if (bt == (VT_SHORT | VT_UNSIGNED))
  830. o(0xc0b70f); /* movzbl %al, %eax */
  831. #if 0 /* handled in gen_cast() */
  832. else if (bt == VT_INT)
  833. o(0x9848); /* cltq */
  834. else if (bt == (VT_INT | VT_UNSIGNED))
  835. o(0xc089); /* mov %eax,%eax */
  836. #endif
  837. vtop--;
  838. }
  839. #define FUNC_PROLOG_SIZE 11
  840. /* generate function prolog of type 't' */
  841. void gfunc_prolog(CType *func_type)
  842. {
  843. int addr, reg_param_index, bt, size;
  844. Sym *sym;
  845. CType *type;
  846. func_ret_sub = 0;
  847. func_scratch = 0;
  848. func_alloca = 0;
  849. loc = 0;
  850. addr = PTR_SIZE * 2;
  851. ind += FUNC_PROLOG_SIZE;
  852. func_sub_sp_offset = ind;
  853. reg_param_index = 0;
  854. sym = func_type->ref;
  855. /* if the function returns a structure, then add an
  856. implicit pointer parameter */
  857. func_vt = sym->type;
  858. func_var = (sym->f.func_type == FUNC_ELLIPSIS);
  859. size = gfunc_arg_size(&func_vt);
  860. if (!using_regs(size)) {
  861. gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr);
  862. func_vc = addr;
  863. reg_param_index++;
  864. addr += 8;
  865. }
  866. /* define parameters */
  867. while ((sym = sym->next) != NULL) {
  868. type = &sym->type;
  869. bt = type->t & VT_BTYPE;
  870. size = gfunc_arg_size(type);
  871. if (!using_regs(size)) {
  872. if (reg_param_index < REGN) {
  873. gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr);
  874. }
  875. sym_push(sym->v & ~SYM_FIELD, type, VT_LLOCAL | VT_LVAL, addr);
  876. } else {
  877. if (reg_param_index < REGN) {
  878. /* save arguments passed by register */
  879. if ((bt == VT_FLOAT) || (bt == VT_DOUBLE)) {
  880. if (tcc_state->nosse)
  881. tcc_error("SSE disabled");
  882. o(0xd60f66); /* movq */
  883. gen_modrm(reg_param_index, VT_LOCAL, NULL, addr);
  884. } else {
  885. gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr);
  886. }
  887. }
  888. sym_push(sym->v & ~SYM_FIELD, type, VT_LOCAL | VT_LVAL, addr);
  889. }
  890. addr += 8;
  891. reg_param_index++;
  892. }
  893. while (reg_param_index < REGN) {
  894. if (func_type->ref->f.func_type == FUNC_ELLIPSIS) {
  895. gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr);
  896. addr += 8;
  897. }
  898. reg_param_index++;
  899. }
  900. }
  901. /* generate function epilog */
  902. void gfunc_epilog(void)
  903. {
  904. int v, saved_ind;
  905. o(0xc9); /* leave */
  906. if (func_ret_sub == 0) {
  907. o(0xc3); /* ret */
  908. } else {
  909. o(0xc2); /* ret n */
  910. g(func_ret_sub);
  911. g(func_ret_sub >> 8);
  912. }
  913. saved_ind = ind;
  914. ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
  915. /* align local size to word & save local variables */
  916. v = (func_scratch + -loc + 15) & -16;
  917. if (v >= 4096) {
  918. Sym *sym = external_global_sym(TOK___chkstk, &func_old_type, 0);
  919. oad(0xb8, v); /* mov stacksize, %eax */
  920. oad(0xe8, 0); /* call __chkstk, (does the stackframe too) */
  921. greloca(cur_text_section, sym, ind-4, R_X86_64_PC32, -4);
  922. o(0x90); /* fill for FUNC_PROLOG_SIZE = 11 bytes */
  923. } else {
  924. o(0xe5894855); /* push %rbp, mov %rsp, %rbp */
  925. o(0xec8148); /* sub rsp, stacksize */
  926. gen_le32(v);
  927. }
  928. /* add the "func_scratch" area after each alloca seen */
  929. while (func_alloca) {
  930. unsigned char *ptr = cur_text_section->data + func_alloca;
  931. func_alloca = read32le(ptr);
  932. write32le(ptr, func_scratch);
  933. }
  934. cur_text_section->data_offset = saved_ind;
  935. pe_add_unwind_data(ind, saved_ind, v);
  936. ind = cur_text_section->data_offset;
  937. }
  938. #else
  939. static void gadd_sp(int val)
  940. {
  941. if (val == (char)val) {
  942. o(0xc48348);
  943. g(val);
  944. } else {
  945. oad(0xc48148, val); /* add $xxx, %rsp */
  946. }
  947. }
  948. typedef enum X86_64_Mode {
  949. x86_64_mode_none,
  950. x86_64_mode_memory,
  951. x86_64_mode_integer,
  952. x86_64_mode_sse,
  953. x86_64_mode_x87
  954. } X86_64_Mode;
  955. static X86_64_Mode classify_x86_64_merge(X86_64_Mode a, X86_64_Mode b)
  956. {
  957. if (a == b)
  958. return a;
  959. else if (a == x86_64_mode_none)
  960. return b;
  961. else if (b == x86_64_mode_none)
  962. return a;
  963. else if ((a == x86_64_mode_memory) || (b == x86_64_mode_memory))
  964. return x86_64_mode_memory;
  965. else if ((a == x86_64_mode_integer) || (b == x86_64_mode_integer))
  966. return x86_64_mode_integer;
  967. else if ((a == x86_64_mode_x87) || (b == x86_64_mode_x87))
  968. return x86_64_mode_memory;
  969. else
  970. return x86_64_mode_sse;
  971. }
  972. static X86_64_Mode classify_x86_64_inner(CType *ty)
  973. {
  974. X86_64_Mode mode;
  975. Sym *f;
  976. switch (ty->t & VT_BTYPE) {
  977. case VT_VOID: return x86_64_mode_none;
  978. case VT_INT:
  979. case VT_BYTE:
  980. case VT_SHORT:
  981. case VT_LLONG:
  982. case VT_BOOL:
  983. case VT_PTR:
  984. case VT_FUNC:
  985. return x86_64_mode_integer;
  986. case VT_FLOAT:
  987. case VT_DOUBLE: return x86_64_mode_sse;
  988. case VT_LDOUBLE: return x86_64_mode_x87;
  989. case VT_STRUCT:
  990. f = ty->ref;
  991. mode = x86_64_mode_none;
  992. for (f = f->next; f; f = f->next)
  993. mode = classify_x86_64_merge(mode, classify_x86_64_inner(&f->type));
  994. return mode;
  995. }
  996. assert(0);
  997. return 0;
  998. }
  999. static X86_64_Mode classify_x86_64_arg(CType *ty, CType *ret, int *psize, int *palign, int *reg_count)
  1000. {
  1001. X86_64_Mode mode;
  1002. int size, align, ret_t = 0;
  1003. if (ty->t & (VT_BITFIELD|VT_ARRAY)) {
  1004. *psize = 8;
  1005. *palign = 8;
  1006. *reg_count = 1;
  1007. ret_t = ty->t;
  1008. mode = x86_64_mode_integer;
  1009. } else {
  1010. size = type_size(ty, &align);
  1011. *psize = (size + 7) & ~7;
  1012. *palign = (align + 7) & ~7;
  1013. if (size > 16) {
  1014. mode = x86_64_mode_memory;
  1015. } else {
  1016. mode = classify_x86_64_inner(ty);
  1017. switch (mode) {
  1018. case x86_64_mode_integer:
  1019. if (size > 8) {
  1020. *reg_count = 2;
  1021. ret_t = VT_QLONG;
  1022. } else {
  1023. *reg_count = 1;
  1024. ret_t = (size > 4) ? VT_LLONG : VT_INT;
  1025. }
  1026. break;
  1027. case x86_64_mode_x87:
  1028. *reg_count = 1;
  1029. ret_t = VT_LDOUBLE;
  1030. break;
  1031. case x86_64_mode_sse:
  1032. if (size > 8) {
  1033. *reg_count = 2;
  1034. ret_t = VT_QFLOAT;
  1035. } else {
  1036. *reg_count = 1;
  1037. ret_t = (size > 4) ? VT_DOUBLE : VT_FLOAT;
  1038. }
  1039. break;
  1040. default: break; /* nothing to be done for x86_64_mode_memory and x86_64_mode_none*/
  1041. }
  1042. }
  1043. }
  1044. if (ret) {
  1045. ret->ref = NULL;
  1046. ret->t = ret_t;
  1047. }
  1048. return mode;
  1049. }
  1050. ST_FUNC int classify_x86_64_va_arg(CType *ty)
  1051. {
  1052. /* This definition must be synced with stdarg.h */
  1053. enum __va_arg_type {
  1054. __va_gen_reg, __va_float_reg, __va_stack
  1055. };
  1056. int size, align, reg_count;
  1057. X86_64_Mode mode = classify_x86_64_arg(ty, NULL, &size, &align, &reg_count);
  1058. switch (mode) {
  1059. default: return __va_stack;
  1060. case x86_64_mode_integer: return __va_gen_reg;
  1061. case x86_64_mode_sse: return __va_float_reg;
  1062. }
  1063. }
  1064. /* Return the number of registers needed to return the struct, or 0 if
  1065. returning via struct pointer. */
  1066. ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align, int *regsize)
  1067. {
  1068. int size, align, reg_count;
  1069. *ret_align = 1; // Never have to re-align return values for x86-64
  1070. *regsize = 8;
  1071. return (classify_x86_64_arg(vt, ret, &size, &align, &reg_count) != x86_64_mode_memory);
  1072. }
  1073. #define REGN 6
  1074. static const uint8_t arg_regs[REGN] = {
  1075. TREG_RDI, TREG_RSI, TREG_RDX, TREG_RCX, TREG_R8, TREG_R9
  1076. };
  1077. static int arg_prepare_reg(int idx) {
  1078. if (idx == 2 || idx == 3)
  1079. /* idx=2: r10, idx=3: r11 */
  1080. return idx + 8;
  1081. else
  1082. return arg_regs[idx];
  1083. }
  1084. /* Generate function call. The function address is pushed first, then
  1085. all the parameters in call order. This functions pops all the
  1086. parameters and the function address. */
  1087. void gfunc_call(int nb_args)
  1088. {
  1089. X86_64_Mode mode;
  1090. CType type;
  1091. int size, align, r, args_size, stack_adjust, i, reg_count;
  1092. int nb_reg_args = 0;
  1093. int nb_sse_args = 0;
  1094. int sse_reg, gen_reg;
  1095. char _onstack[nb_args], *onstack = _onstack;
  1096. /* calculate the number of integer/float register arguments, remember
  1097. arguments to be passed via stack (in onstack[]), and also remember
  1098. if we have to align the stack pointer to 16 (onstack[i] == 2). Needs
  1099. to be done in a left-to-right pass over arguments. */
  1100. stack_adjust = 0;
  1101. for(i = nb_args - 1; i >= 0; i--) {
  1102. mode = classify_x86_64_arg(&vtop[-i].type, NULL, &size, &align, &reg_count);
  1103. if (mode == x86_64_mode_sse && nb_sse_args + reg_count <= 8) {
  1104. nb_sse_args += reg_count;
  1105. onstack[i] = 0;
  1106. } else if (mode == x86_64_mode_integer && nb_reg_args + reg_count <= REGN) {
  1107. nb_reg_args += reg_count;
  1108. onstack[i] = 0;
  1109. } else if (mode == x86_64_mode_none) {
  1110. onstack[i] = 0;
  1111. } else {
  1112. if (align == 16 && (stack_adjust &= 15)) {
  1113. onstack[i] = 2;
  1114. stack_adjust = 0;
  1115. } else
  1116. onstack[i] = 1;
  1117. stack_adjust += size;
  1118. }
  1119. }
  1120. if (nb_sse_args && tcc_state->nosse)
  1121. tcc_error("SSE disabled but floating point arguments passed");
  1122. /* fetch cpu flag before generating any code */
  1123. if (vtop >= vstack && (vtop->r & VT_VALMASK) == VT_CMP)
  1124. gv(RC_INT);
  1125. /* for struct arguments, we need to call memcpy and the function
  1126. call breaks register passing arguments we are preparing.
  1127. So, we process arguments which will be passed by stack first. */
  1128. gen_reg = nb_reg_args;
  1129. sse_reg = nb_sse_args;
  1130. args_size = 0;
  1131. stack_adjust &= 15;
  1132. for (i = 0; i < nb_args;) {
  1133. mode = classify_x86_64_arg(&vtop[-i].type, NULL, &size, &align, &reg_count);
  1134. if (!onstack[i]) {
  1135. ++i;
  1136. continue;
  1137. }
  1138. /* Possibly adjust stack to align SSE boundary. We're processing
  1139. args from right to left while allocating happens left to right
  1140. (stack grows down), so the adjustment needs to happen _after_
  1141. an argument that requires it. */
  1142. if (stack_adjust) {
  1143. o(0x50); /* push %rax; aka sub $8,%rsp */
  1144. args_size += 8;
  1145. stack_adjust = 0;
  1146. }
  1147. if (onstack[i] == 2)
  1148. stack_adjust = 1;
  1149. vrotb(i+1);
  1150. switch (vtop->type.t & VT_BTYPE) {
  1151. case VT_STRUCT:
  1152. /* allocate the necessary size on stack */
  1153. o(0x48);
  1154. oad(0xec81, size); /* sub $xxx, %rsp */
  1155. /* generate structure store */
  1156. r = get_reg(RC_INT);
  1157. orex(1, r, 0, 0x89); /* mov %rsp, r */
  1158. o(0xe0 + REG_VALUE(r));
  1159. vset(&vtop->type, r | VT_LVAL, 0);
  1160. vswap();
  1161. vstore();
  1162. break;
  1163. case VT_LDOUBLE:
  1164. gv(RC_ST0);
  1165. oad(0xec8148, size); /* sub $xxx, %rsp */
  1166. o(0x7cdb); /* fstpt 0(%rsp) */
  1167. g(0x24);
  1168. g(0x00);
  1169. break;
  1170. case VT_FLOAT:
  1171. case VT_DOUBLE:
  1172. assert(mode == x86_64_mode_sse);
  1173. r = gv(RC_FLOAT);
  1174. o(0x50); /* push $rax */
  1175. /* movq %xmmN, (%rsp) */
  1176. o(0xd60f66);
  1177. o(0x04 + REG_VALUE(r)*8);
  1178. o(0x24);
  1179. break;
  1180. default:
  1181. assert(mode == x86_64_mode_integer);
  1182. /* simple type */
  1183. /* XXX: implicit cast ? */
  1184. r = gv(RC_INT);
  1185. orex(0,r,0,0x50 + REG_VALUE(r)); /* push r */
  1186. break;
  1187. }
  1188. args_size += size;
  1189. vpop();
  1190. --nb_args;
  1191. onstack++;
  1192. }
  1193. /* XXX This should be superfluous. */
  1194. save_regs(0); /* save used temporary registers */
  1195. /* then, we prepare register passing arguments.
  1196. Note that we cannot set RDX and RCX in this loop because gv()
  1197. may break these temporary registers. Let's use R10 and R11
  1198. instead of them */
  1199. assert(gen_reg <= REGN);
  1200. assert(sse_reg <= 8);
  1201. for(i = 0; i < nb_args; i++) {
  1202. mode = classify_x86_64_arg(&vtop->type, &type, &size, &align, &reg_count);
  1203. /* Alter stack entry type so that gv() knows how to treat it */
  1204. vtop->type = type;
  1205. if (mode == x86_64_mode_sse) {
  1206. if (reg_count == 2) {
  1207. sse_reg -= 2;
  1208. gv(RC_FRET); /* Use pair load into xmm0 & xmm1 */
  1209. if (sse_reg) { /* avoid redundant movaps %xmm0, %xmm0 */
  1210. /* movaps %xmm0, %xmmN */
  1211. o(0x280f);
  1212. o(0xc0 + (sse_reg << 3));
  1213. /* movaps %xmm1, %xmmN */
  1214. o(0x280f);
  1215. o(0xc1 + ((sse_reg+1) << 3));
  1216. }
  1217. } else {
  1218. assert(reg_count == 1);
  1219. --sse_reg;
  1220. /* Load directly to register */
  1221. gv(RC_XMM0 << sse_reg);
  1222. }
  1223. } else if (mode == x86_64_mode_integer) {
  1224. /* simple type */
  1225. /* XXX: implicit cast ? */
  1226. int d;
  1227. gen_reg -= reg_count;
  1228. r = gv(RC_INT);
  1229. d = arg_prepare_reg(gen_reg);
  1230. orex(1,d,r,0x89); /* mov */
  1231. o(0xc0 + REG_VALUE(r) * 8 + REG_VALUE(d));
  1232. if (reg_count == 2) {
  1233. d = arg_prepare_reg(gen_reg+1);
  1234. orex(1,d,vtop->r2,0x89); /* mov */
  1235. o(0xc0 + REG_VALUE(vtop->r2) * 8 + REG_VALUE(d));
  1236. }
  1237. }
  1238. vtop--;
  1239. }
  1240. assert(gen_reg == 0);
  1241. assert(sse_reg == 0);
  1242. /* We shouldn't have many operands on the stack anymore, but the
  1243. call address itself is still there, and it might be in %eax
  1244. (or edx/ecx) currently, which the below writes would clobber.
  1245. So evict all remaining operands here. */
  1246. save_regs(0);
  1247. /* Copy R10 and R11 into RDX and RCX, respectively */
  1248. if (nb_reg_args > 2) {
  1249. o(0xd2894c); /* mov %r10, %rdx */
  1250. if (nb_reg_args > 3) {
  1251. o(0xd9894c); /* mov %r11, %rcx */
  1252. }
  1253. }
  1254. if (vtop->type.ref->f.func_type != FUNC_NEW) /* implies FUNC_OLD or FUNC_ELLIPSIS */
  1255. oad(0xb8, nb_sse_args < 8 ? nb_sse_args : 8); /* mov nb_sse_args, %eax */
  1256. gcall_or_jmp(0);
  1257. if (args_size)
  1258. gadd_sp(args_size);
  1259. vtop--;
  1260. }
  1261. #define FUNC_PROLOG_SIZE 11
  1262. static void push_arg_reg(int i) {
  1263. loc -= 8;
  1264. gen_modrm64(0x89, arg_regs[i], VT_LOCAL, NULL, loc);
  1265. }
  1266. /* generate function prolog of type 't' */
  1267. void gfunc_prolog(CType *func_type)
  1268. {
  1269. X86_64_Mode mode;
  1270. int i, addr, align, size, reg_count;
  1271. int param_addr = 0, reg_param_index, sse_param_index;
  1272. Sym *sym;
  1273. CType *type;
  1274. sym = func_type->ref;
  1275. addr = PTR_SIZE * 2;
  1276. loc = 0;
  1277. ind += FUNC_PROLOG_SIZE;
  1278. func_sub_sp_offset = ind;
  1279. func_ret_sub = 0;
  1280. if (sym->f.func_type == FUNC_ELLIPSIS) {
  1281. int seen_reg_num, seen_sse_num, seen_stack_size;
  1282. seen_reg_num = seen_sse_num = 0;
  1283. /* frame pointer and return address */
  1284. seen_stack_size = PTR_SIZE * 2;
  1285. /* count the number of seen parameters */
  1286. sym = func_type->ref;
  1287. while ((sym = sym->next) != NULL) {
  1288. type = &sym->type;
  1289. mode = classify_x86_64_arg(type, NULL, &size, &align, &reg_count);
  1290. switch (mode) {
  1291. default:
  1292. stack_arg:
  1293. seen_stack_size = ((seen_stack_size + align - 1) & -align) + size;
  1294. break;
  1295. case x86_64_mode_integer:
  1296. if (seen_reg_num + reg_count > REGN)
  1297. goto stack_arg;
  1298. seen_reg_num += reg_count;
  1299. break;
  1300. case x86_64_mode_sse:
  1301. if (seen_sse_num + reg_count > 8)
  1302. goto stack_arg;
  1303. seen_sse_num += reg_count;
  1304. break;
  1305. }
  1306. }
  1307. loc -= 16;
  1308. /* movl $0x????????, -0x10(%rbp) */
  1309. o(0xf045c7);
  1310. gen_le32(seen_reg_num * 8);
  1311. /* movl $0x????????, -0xc(%rbp) */
  1312. o(0xf445c7);
  1313. gen_le32(seen_sse_num * 16 + 48);
  1314. /* movl $0x????????, -0x8(%rbp) */
  1315. o(0xf845c7);
  1316. gen_le32(seen_stack_size);
  1317. /* save all register passing arguments */
  1318. for (i = 0; i < 8; i++) {
  1319. loc -= 16;
  1320. if (!tcc_state->nosse) {
  1321. o(0xd60f66); /* movq */
  1322. gen_modrm(7 - i, VT_LOCAL, NULL, loc);
  1323. }
  1324. /* movq $0, loc+8(%rbp) */
  1325. o(0x85c748);
  1326. gen_le32(loc + 8);
  1327. gen_le32(0);
  1328. }
  1329. for (i = 0; i < REGN; i++) {
  1330. push_arg_reg(REGN-1-i);
  1331. }
  1332. }
  1333. sym = func_type->ref;
  1334. reg_param_index = 0;
  1335. sse_param_index = 0;
  1336. /* if the function returns a structure, then add an
  1337. implicit pointer parameter */
  1338. func_vt = sym->type;
  1339. mode = classify_x86_64_arg(&func_vt, NULL, &size, &align, &reg_count);
  1340. if (mode == x86_64_mode_memory) {
  1341. push_arg_reg(reg_param_index);
  1342. func_vc = loc;
  1343. reg_param_index++;
  1344. }
  1345. /* define parameters */
  1346. while ((sym = sym->next) != NULL) {
  1347. type = &sym->type;
  1348. mode = classify_x86_64_arg(type, NULL, &size, &align, &reg_count);
  1349. switch (mode) {
  1350. case x86_64_mode_sse:
  1351. if (tcc_state->nosse)
  1352. tcc_error("SSE disabled but floating point arguments used");
  1353. if (sse_param_index + reg_count <= 8) {
  1354. /* save arguments passed by register */
  1355. loc -= reg_count * 8;
  1356. param_addr = loc;
  1357. for (i = 0; i < reg_count; ++i) {
  1358. o(0xd60f66); /* movq */
  1359. gen_modrm(sse_param_index, VT_LOCAL, NULL, param_addr + i*8);
  1360. ++sse_param_index;
  1361. }
  1362. } else {
  1363. addr = (addr + align - 1) & -align;
  1364. param_addr = addr;
  1365. addr += size;
  1366. }
  1367. break;
  1368. case x86_64_mode_memory:
  1369. case x86_64_mode_x87:
  1370. addr = (addr + align - 1) & -align;
  1371. param_addr = addr;
  1372. addr += size;
  1373. break;
  1374. case x86_64_mode_integer: {
  1375. if (reg_param_index + reg_count <= REGN) {
  1376. /* save arguments passed by register */
  1377. loc -= reg_count * 8;
  1378. param_addr = loc;
  1379. for (i = 0; i < reg_count; ++i) {
  1380. gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, param_addr + i*8);
  1381. ++reg_param_index;
  1382. }
  1383. } else {
  1384. addr = (addr + align - 1) & -align;
  1385. param_addr = addr;
  1386. addr += size;
  1387. }
  1388. break;
  1389. }
  1390. default: break; /* nothing to be done for x86_64_mode_none */
  1391. }
  1392. sym_push(sym->v & ~SYM_FIELD, type,
  1393. VT_LOCAL | VT_LVAL, param_addr);
  1394. }
  1395. #ifdef CONFIG_TCC_BCHECK
  1396. /* leave some room for bound checking code */
  1397. if (tcc_state->do_bounds_check) {
  1398. func_bound_offset = lbounds_section->data_offset;
  1399. func_bound_ind = ind;
  1400. oad(0xb8, 0); /* lbound section pointer */
  1401. o(0xc78948); /* mov %rax,%rdi ## first arg in %rdi, this must be ptr */
  1402. oad(0xb8, 0); /* call to function */
  1403. }
  1404. #endif
  1405. }
  1406. /* generate function epilog */
  1407. void gfunc_epilog(void)
  1408. {
  1409. int v, saved_ind;
  1410. #ifdef CONFIG_TCC_BCHECK
  1411. if (tcc_state->do_bounds_check
  1412. && func_bound_offset != lbounds_section->data_offset)
  1413. {
  1414. addr_t saved_ind;
  1415. addr_t *bounds_ptr;
  1416. Sym *sym_data;
  1417. /* add end of table info */
  1418. bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t));
  1419. *bounds_ptr = 0;
  1420. /* generate bound local allocation */
  1421. sym_data = get_sym_ref(&char_pointer_type, lbounds_section,
  1422. func_bound_offset, lbounds_section->data_offset);
  1423. saved_ind = ind;
  1424. ind = func_bound_ind;
  1425. greloca(cur_text_section, sym_data, ind + 1, R_X86_64_64, 0);
  1426. ind = ind + 5 + 3;
  1427. gen_static_call(TOK___bound_local_new);
  1428. ind = saved_ind;
  1429. /* generate bound check local freeing */
  1430. o(0x5250); /* save returned value, if any */
  1431. greloca(cur_text_section, sym_data, ind + 1, R_X86_64_64, 0);
  1432. oad(0xb8, 0); /* mov xxx, %rax */
  1433. o(0xc78948); /* mov %rax,%rdi # first arg in %rdi, this must be ptr */
  1434. gen_static_call(TOK___bound_local_delete);
  1435. o(0x585a); /* restore returned value, if any */
  1436. }
  1437. #endif
  1438. o(0xc9); /* leave */
  1439. if (func_ret_sub == 0) {
  1440. o(0xc3); /* ret */
  1441. } else {
  1442. o(0xc2); /* ret n */
  1443. g(func_ret_sub);
  1444. g(func_ret_sub >> 8);
  1445. }
  1446. /* align local size to word & save local variables */
  1447. v = (-loc + 15) & -16;
  1448. saved_ind = ind;
  1449. ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
  1450. o(0xe5894855); /* push %rbp, mov %rsp, %rbp */
  1451. o(0xec8148); /* sub rsp, stacksize */
  1452. gen_le32(v);
  1453. ind = saved_ind;
  1454. }
  1455. #endif /* not PE */
  1456. /* generate a jump to a label */
  1457. int gjmp(int t)
  1458. {
  1459. return gjmp2(0xe9, t);
  1460. }
  1461. /* generate a jump to a fixed address */
  1462. void gjmp_addr(int a)
  1463. {
  1464. int r;
  1465. r = a - ind - 2;
  1466. if (r == (char)r) {
  1467. g(0xeb);
  1468. g(r);
  1469. } else {
  1470. oad(0xe9, a - ind - 5);
  1471. }
  1472. }
  1473. ST_FUNC void gtst_addr(int inv, int a)
  1474. {
  1475. int v = vtop->r & VT_VALMASK;
  1476. if (v == VT_CMP) {
  1477. inv ^= (vtop--)->c.i;
  1478. a -= ind + 2;
  1479. if (a == (char)a) {
  1480. g(inv - 32);
  1481. g(a);
  1482. } else {
  1483. g(0x0f);
  1484. oad(inv - 16, a - 4);
  1485. }
  1486. } else if ((v & ~1) == VT_JMP) {
  1487. if ((v & 1) != inv) {
  1488. gjmp_addr(a);
  1489. gsym(vtop->c.i);
  1490. } else {
  1491. gsym(vtop->c.i);
  1492. o(0x05eb);
  1493. gjmp_addr(a);
  1494. }
  1495. vtop--;
  1496. }
  1497. }
  1498. /* generate a test. set 'inv' to invert test. Stack entry is popped */
  1499. ST_FUNC int gtst(int inv, int t)
  1500. {
  1501. int v = vtop->r & VT_VALMASK;
  1502. if (nocode_wanted) {
  1503. ;
  1504. } else if (v == VT_CMP) {
  1505. /* fast case : can jump directly since flags are set */
  1506. if (vtop->c.i & 0x100)
  1507. {
  1508. /* This was a float compare. If the parity flag is set
  1509. the result was unordered. For anything except != this
  1510. means false and we don't jump (anding both conditions).
  1511. For != this means true (oring both).
  1512. Take care about inverting the test. We need to jump
  1513. to our target if the result was unordered and test wasn't NE,
  1514. otherwise if unordered we don't want to jump. */
  1515. vtop->c.i &= ~0x100;
  1516. if (inv == (vtop->c.i == TOK_NE))
  1517. o(0x067a); /* jp +6 */
  1518. else
  1519. {
  1520. g(0x0f);
  1521. t = gjmp2(0x8a, t); /* jp t */
  1522. }
  1523. }
  1524. g(0x0f);
  1525. t = gjmp2((vtop->c.i - 16) ^ inv, t);
  1526. } else if (v == VT_JMP || v == VT_JMPI) {
  1527. /* && or || optimization */
  1528. if ((v & 1) == inv) {
  1529. /* insert vtop->c jump list in t */
  1530. uint32_t n1, n = vtop->c.i;
  1531. if (n) {
  1532. while ((n1 = read32le(cur_text_section->data + n)))
  1533. n = n1;
  1534. write32le(cur_text_section->data + n, t);
  1535. t = vtop->c.i;
  1536. }
  1537. } else {
  1538. t = gjmp(t);
  1539. gsym(vtop->c.i);
  1540. }
  1541. }
  1542. vtop--;
  1543. return t;
  1544. }
  1545. /* generate an integer binary operation */
  1546. void gen_opi(int op)
  1547. {
  1548. int r, fr, opc, c;
  1549. int ll, uu, cc;
  1550. ll = is64_type(vtop[-1].type.t);
  1551. uu = (vtop[-1].type.t & VT_UNSIGNED) != 0;
  1552. cc = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
  1553. switch(op) {
  1554. case '+':
  1555. case TOK_ADDC1: /* add with carry generation */
  1556. opc = 0;
  1557. gen_op8:
  1558. if (cc && (!ll || (int)vtop->c.i == vtop->c.i)) {
  1559. /* constant case */
  1560. vswap();
  1561. r = gv(RC_INT);
  1562. vswap();
  1563. c = vtop->c.i;
  1564. if (c == (char)c) {
  1565. /* XXX: generate inc and dec for smaller code ? */
  1566. orex(ll, r, 0, 0x83);
  1567. o(0xc0 | (opc << 3) | REG_VALUE(r));
  1568. g(c);
  1569. } else {
  1570. orex(ll, r, 0, 0x81);
  1571. oad(0xc0 | (opc << 3) | REG_VALUE(r), c);
  1572. }
  1573. } else {
  1574. gv2(RC_INT, RC_INT);
  1575. r = vtop[-1].r;
  1576. fr = vtop[0].r;
  1577. orex(ll, r, fr, (opc << 3) | 0x01);
  1578. o(0xc0 + REG_VALUE(r) + REG_VALUE(fr) * 8);
  1579. }
  1580. vtop--;
  1581. if (op >= TOK_ULT && op <= TOK_GT) {
  1582. vtop->r = VT_CMP;
  1583. vtop->c.i = op;
  1584. }
  1585. break;
  1586. case '-':
  1587. case TOK_SUBC1: /* sub with carry generation */
  1588. opc = 5;
  1589. goto gen_op8;
  1590. case TOK_ADDC2: /* add with carry use */
  1591. opc = 2;
  1592. goto gen_op8;
  1593. case TOK_SUBC2: /* sub with carry use */
  1594. opc = 3;
  1595. goto gen_op8;
  1596. case '&':
  1597. opc = 4;
  1598. goto gen_op8;
  1599. case '^':
  1600. opc = 6;
  1601. goto gen_op8;
  1602. case '|':
  1603. opc = 1;
  1604. goto gen_op8;
  1605. case '*':
  1606. gv2(RC_INT, RC_INT);
  1607. r = vtop[-1].r;
  1608. fr = vtop[0].r;
  1609. orex(ll, fr, r, 0xaf0f); /* imul fr, r */
  1610. o(0xc0 + REG_VALUE(fr) + REG_VALUE(r) * 8);
  1611. vtop--;
  1612. break;
  1613. case TOK_SHL:
  1614. opc = 4;
  1615. goto gen_shift;
  1616. case TOK_SHR:
  1617. opc = 5;
  1618. goto gen_shift;
  1619. case TOK_SAR:
  1620. opc = 7;
  1621. gen_shift:
  1622. opc = 0xc0 | (opc << 3);
  1623. if (cc) {
  1624. /* constant case */
  1625. vswap();
  1626. r = gv(RC_INT);
  1627. vswap();
  1628. orex(ll, r, 0, 0xc1); /* shl/shr/sar $xxx, r */
  1629. o(opc | REG_VALUE(r));
  1630. g(vtop->c.i & (ll ? 63 : 31));
  1631. } else {
  1632. /* we generate the shift in ecx */
  1633. gv2(RC_INT, RC_RCX);
  1634. r = vtop[-1].r;
  1635. orex(ll, r, 0, 0xd3); /* shl/shr/sar %cl, r */
  1636. o(opc | REG_VALUE(r));
  1637. }
  1638. vtop--;
  1639. break;
  1640. case TOK_UDIV:
  1641. case TOK_UMOD:
  1642. uu = 1;
  1643. goto divmod;
  1644. case '/':
  1645. case '%':
  1646. case TOK_PDIV:
  1647. uu = 0;
  1648. divmod:
  1649. /* first operand must be in eax */
  1650. /* XXX: need better constraint for second operand */
  1651. gv2(RC_RAX, RC_RCX);
  1652. r = vtop[-1].r;
  1653. fr = vtop[0].r;
  1654. vtop--;
  1655. save_reg(TREG_RDX);
  1656. orex(ll, 0, 0, uu ? 0xd231 : 0x99); /* xor %edx,%edx : cqto */
  1657. orex(ll, fr, 0, 0xf7); /* div fr, %eax */
  1658. o((uu ? 0xf0 : 0xf8) + REG_VALUE(fr));
  1659. if (op == '%' || op == TOK_UMOD)
  1660. r = TREG_RDX;
  1661. else
  1662. r = TREG_RAX;
  1663. vtop->r = r;
  1664. break;
  1665. default:
  1666. opc = 7;
  1667. goto gen_op8;
  1668. }
  1669. }
  1670. void gen_opl(int op)
  1671. {
  1672. gen_opi(op);
  1673. }
  1674. /* generate a floating point operation 'v = t1 op t2' instruction. The
  1675. two operands are guaranteed to have the same floating point type */
  1676. /* XXX: need to use ST1 too */
  1677. void gen_opf(int op)
  1678. {
  1679. int a, ft, fc, swapped, r;
  1680. int float_type =
  1681. (vtop->type.t & VT_BTYPE) == VT_LDOUBLE ? RC_ST0 : RC_FLOAT;
  1682. /* convert constants to memory references */
  1683. if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
  1684. vswap();
  1685. gv(float_type);
  1686. vswap();
  1687. }
  1688. if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
  1689. gv(float_type);
  1690. /* must put at least one value in the floating point register */
  1691. if ((vtop[-1].r & VT_LVAL) &&
  1692. (vtop[0].r & VT_LVAL)) {
  1693. vswap();
  1694. gv(float_type);
  1695. vswap();
  1696. }
  1697. swapped = 0;
  1698. /* swap the stack if needed so that t1 is the register and t2 is
  1699. the memory reference */
  1700. if (vtop[-1].r & VT_LVAL) {
  1701. vswap();
  1702. swapped = 1;
  1703. }
  1704. if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
  1705. if (op >= TOK_ULT && op <= TOK_GT) {
  1706. /* load on stack second operand */
  1707. load(TREG_ST0, vtop);
  1708. save_reg(TREG_RAX); /* eax is used by FP comparison code */
  1709. if (op == TOK_GE || op == TOK_GT)
  1710. swapped = !swapped;
  1711. else if (op == TOK_EQ || op == TOK_NE)
  1712. swapped = 0;
  1713. if (swapped)
  1714. o(0xc9d9); /* fxch %st(1) */
  1715. if (op == TOK_EQ || op == TOK_NE)
  1716. o(0xe9da); /* fucompp */
  1717. else
  1718. o(0xd9de); /* fcompp */
  1719. o(0xe0df); /* fnstsw %ax */
  1720. if (op == TOK_EQ) {
  1721. o(0x45e480); /* and $0x45, %ah */
  1722. o(0x40fC80); /* cmp $0x40, %ah */
  1723. } else if (op == TOK_NE) {
  1724. o(0x45e480); /* and $0x45, %ah */
  1725. o(0x40f480); /* xor $0x40, %ah */
  1726. op = TOK_NE;
  1727. } else if (op == TOK_GE || op == TOK_LE) {
  1728. o(0x05c4f6); /* test $0x05, %ah */
  1729. op = TOK_EQ;
  1730. } else {
  1731. o(0x45c4f6); /* test $0x45, %ah */
  1732. op = TOK_EQ;
  1733. }
  1734. vtop--;
  1735. vtop->r = VT_CMP;
  1736. vtop->c.i = op;
  1737. } else {
  1738. /* no memory reference possible for long double operations */
  1739. load(TREG_ST0, vtop);
  1740. swapped = !swapped;
  1741. switch(op) {
  1742. default:
  1743. case '+':
  1744. a = 0;
  1745. break;
  1746. case '-':
  1747. a = 4;
  1748. if (swapped)
  1749. a++;
  1750. break;
  1751. case '*':
  1752. a = 1;
  1753. break;
  1754. case '/':
  1755. a = 6;
  1756. if (swapped)
  1757. a++;
  1758. break;
  1759. }
  1760. ft = vtop->type.t;
  1761. fc = vtop->c.i;
  1762. o(0xde); /* fxxxp %st, %st(1) */
  1763. o(0xc1 + (a << 3));
  1764. vtop--;
  1765. }
  1766. } else {
  1767. if (op >= TOK_ULT && op <= TOK_GT) {
  1768. /* if saved lvalue, then we must reload it */
  1769. r = vtop->r;
  1770. fc = vtop->c.i;
  1771. if ((r & VT_VALMASK) == VT_LLOCAL) {
  1772. SValue v1;
  1773. r = get_reg(RC_INT);
  1774. v1.type.t = VT_PTR;
  1775. v1.r = VT_LOCAL | VT_LVAL;
  1776. v1.c.i = fc;
  1777. load(r, &v1);
  1778. fc = 0;
  1779. }
  1780. if (op == TOK_EQ || op == TOK_NE) {
  1781. swapped = 0;
  1782. } else {
  1783. if (op == TOK_LE || op == TOK_LT)
  1784. swapped = !swapped;
  1785. if (op == TOK_LE || op == TOK_GE) {
  1786. op = 0x93; /* setae */
  1787. } else {
  1788. op = 0x97; /* seta */
  1789. }
  1790. }
  1791. if (swapped) {
  1792. gv(RC_FLOAT);
  1793. vswap();
  1794. }
  1795. assert(!(vtop[-1].r & VT_LVAL));
  1796. if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
  1797. o(0x66);
  1798. if (op == TOK_EQ || op == TOK_NE)
  1799. o(0x2e0f); /* ucomisd */
  1800. else
  1801. o(0x2f0f); /* comisd */
  1802. if (vtop->r & VT_LVAL) {
  1803. gen_modrm(vtop[-1].r, r, vtop->sym, fc);
  1804. } else {
  1805. o(0xc0 + REG_VALUE(vtop[0].r) + REG_VALUE(vtop[-1].r)*8);
  1806. }
  1807. vtop--;
  1808. vtop->r = VT_CMP;
  1809. vtop->c.i = op | 0x100;
  1810. } else {
  1811. assert((vtop->type.t & VT_BTYPE) != VT_LDOUBLE);
  1812. switch(op) {
  1813. default:
  1814. case '+':
  1815. a = 0;
  1816. break;
  1817. case '-':
  1818. a = 4;
  1819. break;
  1820. case '*':
  1821. a = 1;
  1822. break;
  1823. case '/':
  1824. a = 6;
  1825. break;
  1826. }
  1827. ft = vtop->type.t;
  1828. fc = vtop->c.i;
  1829. assert((ft & VT_BTYPE) != VT_LDOUBLE);
  1830. r = vtop->r;
  1831. /* if saved lvalue, then we must reload it */
  1832. if ((vtop->r & VT_VALMASK) == VT_LLOCAL) {
  1833. SValue v1;
  1834. r = get_reg(RC_INT);
  1835. v1.type.t = VT_PTR;
  1836. v1.r = VT_LOCAL | VT_LVAL;
  1837. v1.c.i = fc;
  1838. load(r, &v1);
  1839. fc = 0;
  1840. }
  1841. assert(!(vtop[-1].r & VT_LVAL));
  1842. if (swapped) {
  1843. assert(vtop->r & VT_LVAL);
  1844. gv(RC_FLOAT);
  1845. vswap();
  1846. }
  1847. if ((ft & VT_BTYPE) == VT_DOUBLE) {
  1848. o(0xf2);
  1849. } else {
  1850. o(0xf3);
  1851. }
  1852. o(0x0f);
  1853. o(0x58 + a);
  1854. if (vtop->r & VT_LVAL) {
  1855. gen_modrm(vtop[-1].r, r, vtop->sym, fc);
  1856. } else {
  1857. o(0xc0 + REG_VALUE(vtop[0].r) + REG_VALUE(vtop[-1].r)*8);
  1858. }
  1859. vtop--;
  1860. }
  1861. }
  1862. }
  1863. /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
  1864. and 'long long' cases. */
  1865. void gen_cvt_itof(int t)
  1866. {
  1867. if ((t & VT_BTYPE) == VT_LDOUBLE) {
  1868. save_reg(TREG_ST0);
  1869. gv(RC_INT);
  1870. if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
  1871. /* signed long long to float/double/long double (unsigned case
  1872. is handled generically) */
  1873. o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
  1874. o(0x242cdf); /* fildll (%rsp) */
  1875. o(0x08c48348); /* add $8, %rsp */
  1876. } else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
  1877. (VT_INT | VT_UNSIGNED)) {
  1878. /* unsigned int to float/double/long double */
  1879. o(0x6a); /* push $0 */
  1880. g(0x00);
  1881. o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
  1882. o(0x242cdf); /* fildll (%rsp) */
  1883. o(0x10c48348); /* add $16, %rsp */
  1884. } else {
  1885. /* int to float/double/long double */
  1886. o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
  1887. o(0x2404db); /* fildl (%rsp) */
  1888. o(0x08c48348); /* add $8, %rsp */
  1889. }
  1890. vtop->r = TREG_ST0;
  1891. } else {
  1892. int r = get_reg(RC_FLOAT);
  1893. gv(RC_INT);
  1894. o(0xf2 + ((t & VT_BTYPE) == VT_FLOAT?1:0));
  1895. if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
  1896. (VT_INT | VT_UNSIGNED) ||
  1897. (vtop->type.t & VT_BTYPE) == VT_LLONG) {
  1898. o(0x48); /* REX */
  1899. }
  1900. o(0x2a0f);
  1901. o(0xc0 + (vtop->r & VT_VALMASK) + REG_VALUE(r)*8); /* cvtsi2sd */
  1902. vtop->r = r;
  1903. }
  1904. }
  1905. /* convert from one floating point type to another */
  1906. void gen_cvt_ftof(int t)
  1907. {
  1908. int ft, bt, tbt;
  1909. ft = vtop->type.t;
  1910. bt = ft & VT_BTYPE;
  1911. tbt = t & VT_BTYPE;
  1912. if (bt == VT_FLOAT) {
  1913. gv(RC_FLOAT);
  1914. if (tbt == VT_DOUBLE) {
  1915. o(0x140f); /* unpcklps */
  1916. o(0xc0 + REG_VALUE(vtop->r)*9);
  1917. o(0x5a0f); /* cvtps2pd */
  1918. o(0xc0 + REG_VALUE(vtop->r)*9);
  1919. } else if (tbt == VT_LDOUBLE) {
  1920. save_reg(RC_ST0);
  1921. /* movss %xmm0,-0x10(%rsp) */
  1922. o(0x110ff3);
  1923. o(0x44 + REG_VALUE(vtop->r)*8);
  1924. o(0xf024);
  1925. o(0xf02444d9); /* flds -0x10(%rsp) */
  1926. vtop->r = TREG_ST0;
  1927. }
  1928. } else if (bt == VT_DOUBLE) {
  1929. gv(RC_FLOAT);
  1930. if (tbt == VT_FLOAT) {
  1931. o(0x140f66); /* unpcklpd */
  1932. o(0xc0 + REG_VALUE(vtop->r)*9);
  1933. o(0x5a0f66); /* cvtpd2ps */
  1934. o(0xc0 + REG_VALUE(vtop->r)*9);
  1935. } else if (tbt == VT_LDOUBLE) {
  1936. save_reg(RC_ST0);
  1937. /* movsd %xmm0,-0x10(%rsp) */
  1938. o(0x110ff2);
  1939. o(0x44 + REG_VALUE(vtop->r)*8);
  1940. o(0xf024);
  1941. o(0xf02444dd); /* fldl -0x10(%rsp) */
  1942. vtop->r = TREG_ST0;
  1943. }
  1944. } else {
  1945. int r;
  1946. gv(RC_ST0);
  1947. r = get_reg(RC_FLOAT);
  1948. if (tbt == VT_DOUBLE) {
  1949. o(0xf0245cdd); /* fstpl -0x10(%rsp) */
  1950. /* movsd -0x10(%rsp),%xmm0 */
  1951. o(0x100ff2);
  1952. o(0x44 + REG_VALUE(r)*8);
  1953. o(0xf024);
  1954. vtop->r = r;
  1955. } else if (tbt == VT_FLOAT) {
  1956. o(0xf0245cd9); /* fstps -0x10(%rsp) */
  1957. /* movss -0x10(%rsp),%xmm0 */
  1958. o(0x100ff3);
  1959. o(0x44 + REG_VALUE(r)*8);
  1960. o(0xf024);
  1961. vtop->r = r;
  1962. }
  1963. }
  1964. }
  1965. /* convert fp to int 't' type */
  1966. void gen_cvt_ftoi(int t)
  1967. {
  1968. int ft, bt, size, r;
  1969. ft = vtop->type.t;
  1970. bt = ft & VT_BTYPE;
  1971. if (bt == VT_LDOUBLE) {
  1972. gen_cvt_ftof(VT_DOUBLE);
  1973. bt = VT_DOUBLE;
  1974. }
  1975. gv(RC_FLOAT);
  1976. if (t != VT_INT)
  1977. size = 8;
  1978. else
  1979. size = 4;
  1980. r = get_reg(RC_INT);
  1981. if (bt == VT_FLOAT) {
  1982. o(0xf3);
  1983. } else if (bt == VT_DOUBLE) {
  1984. o(0xf2);
  1985. } else {
  1986. assert(0);
  1987. }
  1988. orex(size == 8, r, 0, 0x2c0f); /* cvttss2si or cvttsd2si */
  1989. o(0xc0 + REG_VALUE(vtop->r) + REG_VALUE(r)*8);
  1990. vtop->r = r;
  1991. }
  1992. /* computed goto support */
  1993. void ggoto(void)
  1994. {
  1995. gcall_or_jmp(1);
  1996. vtop--;
  1997. }
  1998. /* Save the stack pointer onto the stack and return the location of its address */
  1999. ST_FUNC void gen_vla_sp_save(int addr) {
  2000. /* mov %rsp,addr(%rbp)*/
  2001. gen_modrm64(0x89, TREG_RSP, VT_LOCAL, NULL, addr);
  2002. }
  2003. /* Restore the SP from a location on the stack */
  2004. ST_FUNC void gen_vla_sp_restore(int addr) {
  2005. gen_modrm64(0x8b, TREG_RSP, VT_LOCAL, NULL, addr);
  2006. }
  2007. /* Subtract from the stack pointer, and push the resulting value onto the stack */
  2008. ST_FUNC void gen_vla_alloc(CType *type, int align) {
  2009. #ifdef TCC_TARGET_PE
  2010. /* alloca does more than just adjust %rsp on Windows */
  2011. vpush_global_sym(&func_old_type, TOK_alloca);
  2012. vswap(); /* Move alloca ref past allocation size */
  2013. gfunc_call(1);
  2014. #else
  2015. int r;
  2016. r = gv(RC_INT); /* allocation size */
  2017. /* sub r,%rsp */
  2018. o(0x2b48);
  2019. o(0xe0 | REG_VALUE(r));
  2020. /* We align to 16 bytes rather than align */
  2021. /* and ~15, %rsp */
  2022. o(0xf0e48348);
  2023. vpop();
  2024. #endif
  2025. }
  2026. /* end of x86-64 code generator */
  2027. /*************************************************************/
  2028. #endif /* ! TARGET_DEFS_ONLY */
  2029. /******************************************************/