cgcpu.pas 9.0 KB

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