cgcpu.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 done_register_allocators;override;
  30. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);override;
  31. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);override;
  32. procedure a_param_ref(list : TAsmList;size : tcgsize;const r : treference;const paraloc : TCGPara);override;
  33. procedure a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize : tcgsize;intreg, mmreg: tregister; shuffle: pmmshuffle); override;
  34. procedure a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize : tcgsize;mmreg, intreg: tregister;shuffle : pmmshuffle); override;
  35. end;
  36. procedure create_codegen;
  37. implementation
  38. uses
  39. globtype,globals,verbose,systems,cutils,
  40. symsym,defutil,paramgr,fmodule,
  41. rgobj,tgobj,rgcpu;
  42. procedure Tcgx86_64.init_register_allocators;
  43. const
  44. win64_saved_std_regs : array[0..6] of tsuperregister = (RS_RBX,RS_RDI,RS_RSI,RS_R12,RS_R13,RS_R14,RS_R15);
  45. others_saved_std_regs : array[0..4] of tsuperregister = (RS_RBX,RS_R12,RS_R13,RS_R14,RS_R15);
  46. win64_saved_xmm_regs : array[0..9] of tsuperregister = (RS_XMM6,RS_XMM7,
  47. RS_XMM8,RS_XMM9,RS_XMM10,RS_XMM11,RS_XMM12,RS_XMM13,RS_XMM14,RS_XMM15);
  48. var
  49. i : longint;
  50. framepointer : tsuperregister;
  51. begin
  52. inherited init_register_allocators;
  53. if target_info.system=system_x86_64_win64 then
  54. begin
  55. SetLength(saved_standard_registers,Length(win64_saved_std_regs));
  56. SetLength(saved_mm_registers,Length(win64_saved_xmm_regs));
  57. for i:=low(win64_saved_std_regs) to high(win64_saved_std_regs) do
  58. saved_standard_registers[i]:=win64_saved_std_regs[i];
  59. for i:=low(win64_saved_xmm_regs) to high(win64_saved_xmm_regs) do
  60. saved_mm_registers[i]:=win64_saved_xmm_regs[i];
  61. end
  62. else
  63. begin
  64. SetLength(saved_standard_registers,Length(others_saved_std_regs));
  65. SetLength(saved_mm_registers,0);
  66. for i:=low(others_saved_std_regs) to high(others_saved_std_regs) do
  67. saved_standard_registers[i]:=others_saved_std_regs[i];
  68. end;
  69. if assigned(current_procinfo) then
  70. framepointer:=getsupreg(current_procinfo.framepointer)
  71. else
  72. { in intf. wrapper code generation }
  73. framepointer:=RS_FRAME_POINTER_REG;
  74. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,[RS_RAX,RS_RDX,RS_RCX,RS_RBX,RS_RSI,RS_RDI,
  75. RS_R8,RS_R9,RS_R10,RS_R11,RS_R12,RS_R13,RS_R14,RS_R15],first_int_imreg,[framepointer]);
  76. 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,
  77. RS_XMM8,RS_XMM9,RS_XMM10,RS_XMM11,RS_XMM12,RS_XMM13,RS_XMM14,RS_XMM15],first_mm_imreg,[]);
  78. rgfpu:=Trgx86fpu.create;
  79. end;
  80. procedure Tcgx86_64.done_register_allocators;
  81. begin
  82. inherited done_register_allocators;
  83. setlength(saved_standard_registers,0);
  84. setlength(saved_mm_registers,0);
  85. end;
  86. procedure tcgx86_64.a_param_ref(list : TAsmList;size : tcgsize;const r : treference;const paraloc : TCGPara);
  87. var
  88. tmpref, ref: treference;
  89. location: pcgparalocation;
  90. sizeleft: aint;
  91. sourcesize: tcgsize;
  92. begin
  93. location := paraloc.location;
  94. tmpref := r;
  95. { make sure we handle passing a 32 bit value in memory to a }
  96. { 64 bit register location etc. correctly }
  97. if (size<>OS_NO) and
  98. (tcgsize2size[size]<paraloc.intsize) then
  99. begin
  100. paraloc.check_simple_location;
  101. if not(location^.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  102. internalerror(2008031801);
  103. sizeleft:=tcgsize2size[size]
  104. end
  105. else
  106. sizeleft:=paraloc.intsize;
  107. while assigned(location) do
  108. begin
  109. case location^.loc of
  110. LOC_REGISTER,LOC_CREGISTER:
  111. begin
  112. sourcesize:=int_cgsize(sizeleft);
  113. if (sourcesize=OS_NO) then
  114. sourcesize:=location^.size;
  115. a_load_ref_reg(list,sourcesize,location^.size,tmpref,location^.register);
  116. end;
  117. LOC_REFERENCE:
  118. begin
  119. reference_reset_base(ref,location^.reference.index,location^.reference.offset,paraloc.alignment);
  120. g_concatcopy(list,tmpref,ref,sizeleft);
  121. end;
  122. else
  123. internalerror(2002081103);
  124. end;
  125. inc(tmpref.offset,tcgsize2size[location^.size]);
  126. dec(sizeleft,tcgsize2size[location^.size]);
  127. location := location^.next;
  128. end;
  129. end;
  130. procedure tcgx86_64.g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);
  131. var
  132. stacksize : longint;
  133. begin
  134. { Release PIC register }
  135. if cs_create_pic in current_settings.moduleswitches then
  136. list.concat(tai_regalloc.dealloc(NR_PIC_OFFSET_REG,nil));
  137. { remove stackframe }
  138. if not nostackframe then
  139. begin
  140. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) then
  141. begin
  142. stacksize:=current_procinfo.calc_stackframe_size;
  143. if (target_info.system in system_needs_16_byte_stack_alignment) and
  144. ((stacksize <> 0) or
  145. (pi_do_call in current_procinfo.flags) or
  146. { can't detect if a call in this case -> use nostackframe }
  147. { if you (think you) know what you are doing }
  148. (po_assembler in current_procinfo.procdef.procoptions)) then
  149. stacksize := align(stacksize+sizeof(aint),16) - sizeof(aint);
  150. if (stacksize<>0) then
  151. cg.a_op_const_reg(list,OP_ADD,OS_ADDR,stacksize,current_procinfo.framepointer);
  152. end
  153. else
  154. list.concat(Taicpu.op_none(A_LEAVE,S_NO));
  155. list.concat(tai_regalloc.dealloc(NR_FRAME_POINTER_REG,nil));
  156. end;
  157. list.concat(Taicpu.Op_none(A_RET,S_NO));
  158. end;
  159. procedure tcgx86_64.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  160. var
  161. make_global : boolean;
  162. href : treference;
  163. sym : tasmsymbol;
  164. r : treference;
  165. begin
  166. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  167. Internalerror(200006137);
  168. if not assigned(procdef._class) or
  169. (procdef.procoptions*[po_classmethod, po_staticmethod,
  170. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  171. Internalerror(200006138);
  172. if procdef.owner.symtabletype<>ObjectSymtable then
  173. Internalerror(200109191);
  174. make_global:=false;
  175. if (not current_module.is_unit) or
  176. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  177. make_global:=true;
  178. if make_global then
  179. List.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0))
  180. else
  181. List.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0));
  182. { set param1 interface to self }
  183. g_adjust_self_value(list,procdef,ioffset);
  184. if po_virtualmethod in procdef.procoptions then
  185. begin
  186. if (procdef.extnumber=$ffff) then
  187. Internalerror(200006139);
  188. { load vmt from first paramter }
  189. { win64 uses a different abi }
  190. if target_info.system=system_x86_64_win64 then
  191. reference_reset_base(href,NR_RCX,0,sizeof(pint))
  192. else
  193. reference_reset_base(href,NR_RDI,0,sizeof(pint));
  194. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_RAX);
  195. { jmp *vmtoffs(%eax) ; method offs }
  196. reference_reset_base(href,NR_RAX,procdef._class.vmtmethodoffset(procdef.extnumber),sizeof(pint));
  197. list.concat(taicpu.op_ref_reg(A_MOV,S_Q,href,NR_RAX));
  198. list.concat(taicpu.op_reg(A_JMP,S_Q,NR_RAX));
  199. end
  200. else
  201. begin
  202. sym:=current_asmdata.RefAsmSymbol(procdef.mangledname);
  203. reference_reset_symbol(r,sym,0,sizeof(pint));
  204. if (cs_create_pic in current_settings.moduleswitches) and
  205. { darwin/x86_64's assembler doesn't want @PLT after call symbols }
  206. (target_info.system<>system_x86_64_darwin) then
  207. r.refaddr:=addr_pic
  208. else
  209. r.refaddr:=addr_full;
  210. list.concat(taicpu.op_ref(A_JMP,S_NO,r));
  211. end;
  212. List.concat(Tai_symbol_end.Createname(labelname));
  213. end;
  214. procedure tcgx86_64.a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize : tcgsize; intreg, mmreg: tregister; shuffle: pmmshuffle);
  215. begin
  216. { this code can only be used to transfer raw data, not to perform
  217. conversions }
  218. if (tosize<>OS_F64) then
  219. internalerror(2009112505);
  220. if not(fromsize in [OS_64,OS_S64]) then
  221. internalerror(2009112506);
  222. if assigned(shuffle) and
  223. not shufflescalar(shuffle) then
  224. internalerror(2009112517);
  225. list.concat(taicpu.op_reg_reg(A_MOVD,S_NO,intreg,mmreg));
  226. end;
  227. procedure tcgx86_64.a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize : tcgsize; mmreg, intreg: tregister;shuffle : pmmshuffle);
  228. begin
  229. { this code can only be used to transfer raw data, not to perform
  230. conversions }
  231. if (fromsize<>OS_F64) then
  232. internalerror(2009112507);
  233. if not(tosize in [OS_64,OS_S64]) then
  234. internalerror(2009112408);
  235. if assigned(shuffle) and
  236. not shufflescalar(shuffle) then
  237. internalerror(2009112515);
  238. list.concat(taicpu.op_reg_reg(A_MOVD,S_NO,mmreg,intreg));
  239. end;
  240. procedure create_codegen;
  241. begin
  242. cg:=tcgx86_64.create;
  243. end;
  244. end.