cgcpu.pas 8.5 KB

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