cgcpu.pas 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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,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. end;
  32. implementation
  33. uses
  34. globtype,globals,verbose,systems,cutils,
  35. symsym,defutil,paramgr,fmodule,cgutils,
  36. rgobj,tgobj,rgcpu;
  37. procedure Tcgx86_64.init_register_allocators;
  38. begin
  39. inherited init_register_allocators;
  40. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,[RS_RAX,RS_RDX,RS_RCX,RS_RBX,RS_RSI,RS_RDI,
  41. RS_R8,RS_R9,RS_R10,RS_R11,RS_R12,RS_R13,RS_R14,RS_R15],first_int_imreg,[RS_RBP]);
  42. 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,
  43. RS_XMM8,RS_XMM9,RS_XMM10,RS_XMM11,RS_XMM12,RS_XMM13,RS_XMM14,RS_XMM15],first_mm_imreg,[]);
  44. rgfpu:=Trgx86fpu.create;
  45. end;
  46. procedure tcgx86_64.g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);
  47. var
  48. stacksize : longint;
  49. begin
  50. { Release PIC register }
  51. if cs_create_pic in aktmoduleswitches then
  52. list.concat(tai_regalloc.dealloc(NR_PIC_OFFSET_REG,nil));
  53. { remove stackframe }
  54. if not nostackframe then
  55. begin
  56. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) then
  57. begin
  58. stacksize:=current_procinfo.calc_stackframe_size;
  59. if (stacksize<>0) then
  60. cg.a_op_const_reg(list,OP_ADD,OS_ADDR,stacksize,current_procinfo.framepointer);
  61. end
  62. else
  63. list.concat(Taicpu.op_none(A_LEAVE,S_NO));
  64. list.concat(tai_regalloc.dealloc(NR_FRAME_POINTER_REG,nil));
  65. end;
  66. list.concat(Taicpu.Op_none(A_RET,S_NO));
  67. end;
  68. procedure tcgx86_64.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  69. var
  70. make_global : boolean;
  71. href : treference;
  72. sym : tasmsymbol;
  73. r : treference;
  74. begin
  75. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  76. Internalerror(200006137);
  77. if not assigned(procdef._class) or
  78. (procdef.procoptions*[po_classmethod, po_staticmethod,
  79. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  80. Internalerror(200006138);
  81. if procdef.owner.symtabletype<>objectsymtable then
  82. Internalerror(200109191);
  83. make_global:=false;
  84. if (not current_module.is_unit) or
  85. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  86. make_global:=true;
  87. if make_global then
  88. List.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0))
  89. else
  90. List.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0));
  91. { set param1 interface to self }
  92. g_adjust_self_value(list,procdef,ioffset);
  93. if po_virtualmethod in procdef.procoptions then
  94. begin
  95. if (procdef.extnumber=$ffff) then
  96. Internalerror(200006139);
  97. { mov 0(%rdi),%rax ; load vmt}
  98. reference_reset_base(href,NR_RDI,0);
  99. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_RAX);
  100. { jmp *vmtoffs(%eax) ; method offs }
  101. reference_reset_base(href,NR_RAX,procdef._class.vmtmethodoffset(procdef.extnumber));
  102. list.concat(taicpu.op_ref_reg(A_MOV,S_Q,href,NR_RAX));
  103. list.concat(taicpu.op_reg(A_JMP,S_Q,NR_RAX));
  104. end
  105. else
  106. begin
  107. sym:=current_asmdata.RefAsmSymbol(procdef.mangledname);
  108. reference_reset_symbol(r,sym,0);
  109. if cs_create_pic in aktmoduleswitches then
  110. r.refaddr:=addr_pic
  111. else
  112. r.refaddr:=addr_full;
  113. list.concat(taicpu.op_ref(A_JMP,S_NO,r));
  114. end;
  115. List.concat(Tai_symbol_end.Createname(labelname));
  116. end;
  117. begin
  118. cg:=tcgx86_64.create;
  119. {$ifndef cpu64bit}
  120. cg64:=tcg64f64.create;
  121. {$endif cpu64bit}
  122. end.