cgcpu.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. {
  2. Copyright (c) 2002 by Florian Klaempfl
  3. This unit implements the code generator for the x86-64.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit cgcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cgbase,cgutils,cgobj,cgx86,
  22. aasmbase,aasmtai,aasmdata,aasmcpu,
  23. cpubase,cpuinfo,cpupara,parabase,
  24. symdef,
  25. node,symconst,rgx86,procinfo;
  26. type
  27. tcgx86_64 = class(tcgx86)
  28. procedure init_register_allocators;override;
  29. procedure a_loadfpu_ref_cgpara(list: TAsmList; size: tcgsize; const ref: treference; const cgpara: TCGPara); override;
  30. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference); override;
  31. procedure g_proc_entry(list : TAsmList;localsize:longint; nostackframe:boolean);override;
  32. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);override;
  33. procedure g_local_unwind(list: TAsmList; l: TAsmLabel);override;
  34. procedure g_save_registers(list: TAsmList);override;
  35. procedure g_restore_registers(list: TAsmList);override;
  36. procedure a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize : tcgsize;intreg, mmreg: tregister; shuffle: pmmshuffle); override;
  37. procedure a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize : tcgsize;mmreg, intreg: tregister;shuffle : pmmshuffle); override;
  38. private
  39. function use_push: boolean;
  40. function saved_xmm_reg_size: longint;
  41. end;
  42. procedure create_codegen;
  43. implementation
  44. uses
  45. globtype,globals,verbose,systems,cutils,cclasses,
  46. symsym,symtable,defutil,paramgr,fmodule,cpupi,
  47. rgobj,tgobj,rgcpu,ncgutil;
  48. procedure Tcgx86_64.init_register_allocators;
  49. const
  50. win64_saved_std_regs : array[0..7] of tsuperregister = (RS_RBX,RS_RDI,RS_RSI,RS_R12,RS_R13,RS_R14,RS_R15,RS_RBP);
  51. others_saved_std_regs : array[0..4] of tsuperregister = (RS_RBX,RS_R12,RS_R13,RS_R14,RS_R15);
  52. saved_regs_length : array[boolean] of longint = (5,7);
  53. win64_saved_xmm_regs : array[0..9] of tsuperregister = (RS_XMM6,RS_XMM7,
  54. RS_XMM8,RS_XMM9,RS_XMM10,RS_XMM11,RS_XMM12,RS_XMM13,RS_XMM14,RS_XMM15);
  55. var
  56. i : longint;
  57. begin
  58. inherited init_register_allocators;
  59. if (length(saved_standard_registers)<>saved_regs_length[target_info.system=system_x86_64_win64]) then
  60. begin
  61. if target_info.system=system_x86_64_win64 then
  62. begin
  63. SetLength(saved_standard_registers,Length(win64_saved_std_regs));
  64. SetLength(saved_mm_registers,Length(win64_saved_xmm_regs));
  65. for i:=low(win64_saved_std_regs) to high(win64_saved_std_regs) do
  66. saved_standard_registers[i]:=win64_saved_std_regs[i];
  67. for i:=low(win64_saved_xmm_regs) to high(win64_saved_xmm_regs) do
  68. saved_mm_registers[i]:=win64_saved_xmm_regs[i];
  69. end
  70. else
  71. begin
  72. SetLength(saved_standard_registers,Length(others_saved_std_regs));
  73. SetLength(saved_mm_registers,0);
  74. for i:=low(others_saved_std_regs) to high(others_saved_std_regs) do
  75. saved_standard_registers[i]:=others_saved_std_regs[i];
  76. end;
  77. end;
  78. if target_info.system=system_x86_64_win64 then
  79. begin
  80. if (cs_userbp in current_settings.optimizerswitches) and assigned(current_procinfo) and (current_procinfo.framepointer=NR_STACK_POINTER_REG) then
  81. begin
  82. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,[RS_RAX,RS_RDX,RS_RCX,RS_R8,RS_R9,RS_R10,
  83. RS_R11,RS_RBX,RS_RSI,RS_RDI,RS_R12,RS_R13,RS_R14,RS_R15,RS_RBP],first_int_imreg,[]);
  84. end
  85. else
  86. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,[RS_RAX,RS_RDX,RS_RCX,RS_R8,RS_R9,RS_R10,
  87. RS_R11,RS_RBX,RS_RSI,RS_RDI,RS_R12,RS_R13,RS_R14,RS_R15],first_int_imreg,[])
  88. end
  89. else
  90. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,[RS_RAX,RS_RDX,RS_RCX,RS_RSI,RS_RDI,RS_R8,
  91. RS_R9,RS_R10,RS_R11,RS_RBX,RS_R12,RS_R13,RS_R14,RS_R15],first_int_imreg,[]);
  92. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBWHOLE,[RS_XMM0,RS_XMM1,RS_XMM2,RS_XMM3,RS_XMM4,RS_XMM5,RS_XMM6,RS_XMM7,
  93. RS_XMM8,RS_XMM9,RS_XMM10,RS_XMM11,RS_XMM12,RS_XMM13,RS_XMM14,RS_XMM15],first_mm_imreg,[]);
  94. rgfpu:=Trgx86fpu.create;
  95. end;
  96. procedure tcgx86_64.a_loadfpu_ref_cgpara(list: TAsmList; size: tcgsize; const ref: treference; const cgpara: TCGPara);
  97. begin
  98. { a record containing an extended value is returned on the x87 stack
  99. -> size will be OS_F128 (if not packed), while cgpara.paraloc^.size
  100. contains the proper size
  101. In the future we should probably always use cgpara.location^.size, but
  102. that should only be tested/done after 2.8 is branched }
  103. if size in [OS_128,OS_F128] then
  104. size:=cgpara.location^.size;
  105. inherited;
  106. end;
  107. procedure tcgx86_64.a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference);
  108. begin
  109. { same as with a_loadfpu_ref_cgpara() above, but on the callee side
  110. when the value is moved from the fpu register into a memory location }
  111. if tosize in [OS_128,OS_F128] then
  112. tosize:=OS_F80;
  113. inherited;
  114. end;
  115. function tcgx86_64.use_push: boolean;
  116. begin
  117. result:=(current_procinfo.framepointer=NR_STACK_POINTER_REG) or
  118. (current_procinfo.procdef.proctypeoption=potype_exceptfilter);
  119. end;
  120. function tcgx86_64.saved_xmm_reg_size: longint;
  121. var
  122. i: longint;
  123. begin
  124. result:=0;
  125. if (target_info.system<>system_x86_64_win64) or
  126. (not uses_registers(R_MMREGISTER)) then
  127. exit;
  128. for i:=low(saved_mm_registers) to high(saved_mm_registers) do
  129. begin
  130. if (saved_mm_registers[i] in rg[R_MMREGISTER].used_in_proc) then
  131. inc(result,tcgsize2size[OS_VECTOR]);
  132. end;
  133. end;
  134. procedure tcgx86_64.g_proc_entry(list : TAsmList;localsize:longint;nostackframe:boolean);
  135. var
  136. hitem: tlinkedlistitem;
  137. r: integer;
  138. href: treference;
  139. templist: TAsmList;
  140. frame_offset: longint;
  141. suppress_endprologue: boolean;
  142. stackmisalignment: longint;
  143. xmmsize: longint;
  144. procedure push_one_reg(reg: tregister);
  145. begin
  146. list.concat(taicpu.op_reg(A_PUSH,tcgsize2opsize[OS_ADDR],reg));
  147. if (target_info.system=system_x86_64_win64) then
  148. begin
  149. list.concat(cai_seh_directive.create_reg(ash_pushreg,reg));
  150. include(current_procinfo.flags,pi_has_unwind_info);
  151. end;
  152. end;
  153. procedure push_regs;
  154. var
  155. r: longint;
  156. begin
  157. for r := low(saved_standard_registers) to high(saved_standard_registers) do
  158. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  159. begin
  160. inc(stackmisalignment,sizeof(pint));
  161. push_one_reg(newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE));
  162. end;
  163. end;
  164. begin
  165. hitem:=list.last;
  166. { pi_has_unwind_info may already be set at this point if there are
  167. SEH directives in assembler body. In this case, .seh_endprologue
  168. is expected to be one of those directives, and not generated here. }
  169. suppress_endprologue:=(pi_has_unwind_info in current_procinfo.flags);
  170. { save old framepointer }
  171. if not nostackframe then
  172. begin
  173. { return address }
  174. stackmisalignment := sizeof(pint);
  175. list.concat(tai_regalloc.alloc(current_procinfo.framepointer,nil));
  176. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  177. begin
  178. push_regs;
  179. CGmessage(cg_d_stackframe_omited);
  180. end
  181. else
  182. begin
  183. { push <frame_pointer> }
  184. inc(stackmisalignment,sizeof(pint));
  185. push_one_reg(NR_FRAME_POINTER_REG);
  186. { Return address and FP are both on stack }
  187. current_asmdata.asmcfi.cfa_def_cfa_offset(list,2*sizeof(pint));
  188. current_asmdata.asmcfi.cfa_offset(list,NR_FRAME_POINTER_REG,-(2*sizeof(pint)));
  189. if current_procinfo.procdef.proctypeoption<>potype_exceptfilter then
  190. list.concat(Taicpu.op_reg_reg(A_MOV,tcgsize2opsize[OS_ADDR],NR_STACK_POINTER_REG,NR_FRAME_POINTER_REG))
  191. else
  192. begin
  193. push_regs;
  194. gen_load_frame_for_exceptfilter(list);
  195. { Need only as much stack space as necessary to do the calls.
  196. Exception filters don't have own local vars, and temps are 'mapped'
  197. to the parent procedure.
  198. maxpushedparasize is already aligned at least on x86_64. }
  199. localsize:=current_procinfo.maxpushedparasize;
  200. end;
  201. current_asmdata.asmcfi.cfa_def_cfa_register(list,NR_FRAME_POINTER_REG);
  202. {
  203. TODO: current framepointer handling is not compatible with Win64 at all:
  204. Win64 expects FP to point to the top or into the middle of local area.
  205. In FPC it points to the bottom, making it impossible to generate
  206. UWOP_SET_FPREG unwind code if local area is > 240 bytes.
  207. So for now pretend we never have a framepointer.
  208. }
  209. end;
  210. xmmsize:=saved_xmm_reg_size;
  211. if use_push and (xmmsize<>0) then
  212. begin
  213. localsize:=align(localsize,target_info.stackalign)+xmmsize;
  214. reference_reset_base(current_procinfo.save_regs_ref,NR_STACK_POINTER_REG,
  215. localsize-xmmsize,tcgsize2size[OS_VECTOR]);
  216. end;
  217. { allocate stackframe space }
  218. if (localsize<>0) or
  219. ((target_info.stackalign>sizeof(pint)) and
  220. (stackmisalignment <> 0) and
  221. ((pi_do_call in current_procinfo.flags) or
  222. (po_assembler in current_procinfo.procdef.procoptions))) then
  223. begin
  224. if target_info.stackalign>sizeof(pint) then
  225. localsize := align(localsize+stackmisalignment,target_info.stackalign)-stackmisalignment;
  226. cg.g_stackpointer_alloc(list,localsize);
  227. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  228. current_asmdata.asmcfi.cfa_def_cfa_offset(list,localsize+sizeof(pint));
  229. current_procinfo.final_localsize:=localsize;
  230. if (target_info.system=system_x86_64_win64) then
  231. begin
  232. if localsize<>0 then
  233. list.concat(cai_seh_directive.create_offset(ash_stackalloc,localsize));
  234. include(current_procinfo.flags,pi_has_unwind_info);
  235. if use_push and (xmmsize<>0) then
  236. begin
  237. href:=current_procinfo.save_regs_ref;
  238. for r:=low(saved_mm_registers) to high(saved_mm_registers) do
  239. if saved_mm_registers[r] in rg[R_MMREGISTER].used_in_proc then
  240. begin
  241. a_loadmm_reg_ref(list,OS_VECTOR,OS_VECTOR,newreg(R_MMREGISTER,saved_mm_registers[r],R_SUBMMWHOLE),href,nil);
  242. inc(href.offset,tcgsize2size[OS_VECTOR]);
  243. end;
  244. end;
  245. end;
  246. end;
  247. end;
  248. if not (pi_has_unwind_info in current_procinfo.flags) then
  249. exit;
  250. { Generate unwind data for x86_64-win64 }
  251. list.insertafter(cai_seh_directive.create_name(ash_proc,current_procinfo.procdef.mangledname),hitem);
  252. templist:=TAsmList.Create;
  253. { We need to record postive offsets from RSP; if registers are saved
  254. at negative offsets from RBP we need to account for it. }
  255. if (not use_push) then
  256. frame_offset:=current_procinfo.final_localsize
  257. else
  258. frame_offset:=0;
  259. { There's no need to describe position of register saves precisely;
  260. since registers are not modified before they are saved, and saves do not
  261. change RSP, 'logically' all saves can happen at the end of prologue. }
  262. href:=current_procinfo.save_regs_ref;
  263. if (not use_push) then
  264. begin
  265. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  266. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  267. begin
  268. templist.concat(cai_seh_directive.create_reg_offset(ash_savereg,
  269. newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE),
  270. href.offset+frame_offset));
  271. inc(href.offset,sizeof(aint));
  272. end;
  273. end;
  274. if uses_registers(R_MMREGISTER) then
  275. begin
  276. if (href.offset mod tcgsize2size[OS_VECTOR])<>0 then
  277. inc(href.offset,tcgsize2size[OS_VECTOR]-(href.offset mod tcgsize2size[OS_VECTOR]));
  278. for r:=low(saved_mm_registers) to high(saved_mm_registers) do
  279. begin
  280. if saved_mm_registers[r] in rg[R_MMREGISTER].used_in_proc then
  281. begin
  282. templist.concat(cai_seh_directive.create_reg_offset(ash_savexmm,
  283. newreg(R_MMREGISTER,saved_mm_registers[r],R_SUBMMWHOLE),
  284. href.offset+frame_offset));
  285. inc(href.offset,tcgsize2size[OS_VECTOR]);
  286. end;
  287. end;
  288. end;
  289. if not suppress_endprologue then
  290. templist.concat(cai_seh_directive.create(ash_endprologue));
  291. if assigned(current_procinfo.endprologue_ai) then
  292. current_procinfo.aktproccode.insertlistafter(current_procinfo.endprologue_ai,templist)
  293. else
  294. list.concatlist(templist);
  295. templist.free;
  296. end;
  297. procedure tcgx86_64.g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);
  298. procedure increase_sp(a : tcgint);
  299. var
  300. href : treference;
  301. begin
  302. reference_reset_base(href,NR_STACK_POINTER_REG,a,0);
  303. { normally, lea is a better choice than an add }
  304. list.concat(Taicpu.op_ref_reg(A_LEA,TCGSize2OpSize[OS_ADDR],href,NR_STACK_POINTER_REG));
  305. end;
  306. var
  307. href : treference;
  308. hreg : tregister;
  309. r : longint;
  310. begin
  311. { Prevent return address from a possible call from ending up in the epilogue }
  312. { (restoring registers happens before epilogue, providing necessary padding) }
  313. if (current_procinfo.flags*[pi_has_unwind_info,pi_do_call,pi_has_saved_regs])=[pi_has_unwind_info,pi_do_call] then
  314. list.concat(Taicpu.op_none(A_NOP));
  315. { remove stackframe }
  316. if not nostackframe then
  317. begin
  318. if use_push then
  319. begin
  320. if (saved_xmm_reg_size<>0) then
  321. begin
  322. href:=current_procinfo.save_regs_ref;
  323. for r:=low(saved_mm_registers) to high(saved_mm_registers) do
  324. if saved_mm_registers[r] in rg[R_MMREGISTER].used_in_proc then
  325. begin
  326. { Allocate register so the optimizer does not remove the load }
  327. hreg:=newreg(R_MMREGISTER,saved_mm_registers[r],R_SUBMMWHOLE);
  328. a_reg_alloc(list,hreg);
  329. a_loadmm_ref_reg(list,OS_VECTOR,OS_VECTOR,href,hreg,nil);
  330. inc(href.offset,tcgsize2size[OS_VECTOR]);
  331. end;
  332. end;
  333. if (current_procinfo.final_localsize<>0) then
  334. increase_sp(current_procinfo.final_localsize);
  335. internal_restore_regs(list,true);
  336. if (current_procinfo.procdef.proctypeoption=potype_exceptfilter) then
  337. list.concat(Taicpu.op_reg(A_POP,tcgsize2opsize[OS_ADDR],NR_FRAME_POINTER_REG));
  338. end
  339. else if (target_info.system=system_x86_64_win64) then
  340. begin
  341. { Comply with Win64 unwinding mechanism, which only recognizes
  342. 'add $constant,%rsp' and 'lea offset(FPREG),%rsp' as belonging to
  343. the function epilog.
  344. Neither 'leave' nor even 'mov %FPREG,%rsp' are allowed. }
  345. reference_reset_base(href,current_procinfo.framepointer,0,sizeof(pint));
  346. list.concat(Taicpu.op_ref_reg(A_LEA,tcgsize2opsize[OS_ADDR],href,NR_STACK_POINTER_REG));
  347. list.concat(Taicpu.op_reg(A_POP,tcgsize2opsize[OS_ADDR],current_procinfo.framepointer));
  348. end
  349. else
  350. generate_leave(list);
  351. list.concat(tai_regalloc.dealloc(current_procinfo.framepointer,nil));
  352. end;
  353. list.concat(Taicpu.Op_none(A_RET,S_NO));
  354. if (pi_has_unwind_info in current_procinfo.flags) then
  355. begin
  356. tx86_64procinfo(current_procinfo).dump_scopes(list);
  357. list.concat(cai_seh_directive.create(ash_endproc));
  358. end;
  359. end;
  360. procedure tcgx86_64.g_save_registers(list: TAsmList);
  361. begin
  362. if (not use_push) then
  363. inherited g_save_registers(list);
  364. end;
  365. procedure tcgx86_64.g_restore_registers(list: TAsmList);
  366. begin
  367. if (not use_push) then
  368. inherited g_restore_registers(list);
  369. end;
  370. procedure tcgx86_64.g_local_unwind(list: TAsmList; l: TAsmLabel);
  371. var
  372. para1,para2: tcgpara;
  373. href: treference;
  374. pd: tprocdef;
  375. begin
  376. if (target_info.system<>system_x86_64_win64) then
  377. begin
  378. inherited g_local_unwind(list,l);
  379. exit;
  380. end;
  381. pd:=search_system_proc('_fpc_local_unwind');
  382. para1.init;
  383. para2.init;
  384. paramanager.getintparaloc(list,pd,1,para1);
  385. paramanager.getintparaloc(list,pd,2,para2);
  386. reference_reset_symbol(href,l,0,1);
  387. { TODO: using RSP is correct only while the stack is fixed!!
  388. (true now, but will change if/when allocating from stack is implemented) }
  389. a_load_reg_cgpara(list,OS_ADDR,NR_STACK_POINTER_REG,para1);
  390. a_loadaddr_ref_cgpara(list,href,para2);
  391. paramanager.freecgpara(list,para2);
  392. paramanager.freecgpara(list,para1);
  393. g_call(list,'_FPC_local_unwind');
  394. para2.done;
  395. para1.done;
  396. end;
  397. procedure tcgx86_64.a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize : tcgsize; intreg, mmreg: tregister; shuffle: pmmshuffle);
  398. var
  399. opc: tasmop;
  400. begin
  401. { this code can only be used to transfer raw data, not to perform
  402. conversions }
  403. if (tcgsize2size[fromsize]<>tcgsize2size[tosize]) or
  404. not(tosize in [OS_F32,OS_F64,OS_M64]) then
  405. internalerror(2009112505);
  406. case fromsize of
  407. OS_32,OS_S32:
  408. opc:=A_MOVD;
  409. OS_64,OS_S64:
  410. opc:=A_MOVQ;
  411. else
  412. internalerror(2009112506);
  413. end;
  414. if assigned(shuffle) and
  415. not shufflescalar(shuffle) then
  416. internalerror(2009112517);
  417. list.concat(taicpu.op_reg_reg(opc,S_NO,intreg,mmreg));
  418. end;
  419. procedure tcgx86_64.a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize : tcgsize; mmreg, intreg: tregister;shuffle : pmmshuffle);
  420. var
  421. opc: tasmop;
  422. begin
  423. { this code can only be used to transfer raw data, not to perform
  424. conversions }
  425. if (tcgsize2size[fromsize]<>tcgsize2size[tosize]) or
  426. not (fromsize in [OS_F32,OS_F64,OS_M64]) then
  427. internalerror(2009112507);
  428. case tosize of
  429. OS_32,OS_S32:
  430. opc:=A_MOVD;
  431. OS_64,OS_S64:
  432. opc:=A_MOVQ;
  433. else
  434. internalerror(2009112408);
  435. end;
  436. if assigned(shuffle) and
  437. not shufflescalar(shuffle) then
  438. internalerror(2009112515);
  439. list.concat(taicpu.op_reg_reg(opc,S_NO,mmreg,intreg));
  440. end;
  441. procedure create_codegen;
  442. begin
  443. cg:=tcgx86_64.create;
  444. cg128:=tcg128.create;
  445. end;
  446. end.