cgcpu.pas 9.7 KB

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