cgcpu.pas 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. {
  2. $Id$
  3. Copyright (c) 2002 by Florian Klaempfl
  4. This unit implements the code generator for the x86-64.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit cgcpu;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cgbase,cgobj,cgx86,
  23. aasmbase,aasmtai,aasmcpu,
  24. cpubase,cpuinfo,cpupara,parabase,
  25. symdef,
  26. node,symconst,rgx86,procinfo;
  27. type
  28. tcgx86_64 = class(tcgx86)
  29. procedure init_register_allocators;override;
  30. procedure g_proc_exit(list : taasmoutput;parasize:longint;nostackframe:boolean);override;
  31. procedure g_intf_wrapper(list: TAAsmoutput; procdef: tprocdef; const labelname: string; ioffset: longint);override;
  32. end;
  33. implementation
  34. uses
  35. globtype,globals,verbose,systems,cutils,
  36. symsym,defutil,paramgr,fmodule,cgutils,
  37. rgobj,tgobj,rgcpu;
  38. procedure Tcgx86_64.init_register_allocators;
  39. begin
  40. inherited init_register_allocators;
  41. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,[RS_RAX,RS_RDX,RS_RCX,RS_RBX,RS_RSI,RS_RDI,
  42. RS_R8,RS_R9,RS_R10,RS_R11,RS_R12,RS_R13,RS_R14,RS_R15],first_int_imreg,[RS_RBP]);
  43. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBNONE,[RS_XMM0,RS_XMM1,RS_XMM2,RS_XMM3,RS_XMM4,RS_XMM5,RS_XMM6,RS_XMM7,
  44. RS_XMM8,RS_XMM9,RS_XMM10,RS_XMM11,RS_XMM12,RS_XMM13,RS_XMM14,RS_XMM15],first_mm_imreg,[]);
  45. rgfpu:=Trgx86fpu.create;
  46. end;
  47. procedure tcgx86_64.g_proc_exit(list : taasmoutput;parasize:longint;nostackframe:boolean);
  48. var
  49. stacksize : longint;
  50. begin
  51. { Release PIC register }
  52. if cs_create_pic in aktmoduleswitches then
  53. list.concat(tai_regalloc.dealloc(NR_PIC_OFFSET_REG,nil));
  54. { remove stackframe }
  55. if not nostackframe then
  56. begin
  57. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) then
  58. begin
  59. stacksize:=current_procinfo.calc_stackframe_size;
  60. if (stacksize<>0) then
  61. cg.a_op_const_reg(list,OP_ADD,OS_ADDR,stacksize,current_procinfo.framepointer);
  62. end
  63. else
  64. list.concat(Taicpu.op_none(A_LEAVE,S_NO));
  65. list.concat(tai_regalloc.dealloc(NR_FRAME_POINTER_REG,nil));
  66. end;
  67. list.concat(Taicpu.Op_none(A_RET,S_NO));
  68. end;
  69. procedure tcgx86_64.g_intf_wrapper(list: TAAsmoutput; procdef: tprocdef; const labelname: string; ioffset: longint);
  70. var
  71. make_global : boolean;
  72. href : treference;
  73. begin
  74. if procdef.proctypeoption<>potype_none then
  75. Internalerror(200006137);
  76. if not assigned(procdef._class) or
  77. (procdef.procoptions*[po_classmethod, po_staticmethod,
  78. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  79. Internalerror(200006138);
  80. if procdef.owner.symtabletype<>objectsymtable then
  81. Internalerror(200109191);
  82. make_global:=false;
  83. if (not current_module.is_unit) or
  84. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  85. make_global:=true;
  86. if make_global then
  87. List.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0))
  88. else
  89. List.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0));
  90. { set param1 interface to self }
  91. g_adjust_self_value(list,procdef,ioffset);
  92. if po_virtualmethod in procdef.procoptions then
  93. begin
  94. if (procdef.extnumber=$ffff) then
  95. Internalerror(200006139);
  96. { mov 0(%rdi),%rax ; load vmt}
  97. reference_reset_base(href,NR_RDI,0);
  98. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_RAX);
  99. { jmp *vmtoffs(%eax) ; method offs }
  100. reference_reset_base(href,NR_RAX,procdef._class.vmtmethodoffset(procdef.extnumber));
  101. list.concat(taicpu.op_ref_reg(A_MOV,S_Q,href,NR_RAX));
  102. list.concat(taicpu.op_reg(A_JMP,S_Q,NR_RAX));
  103. end
  104. else
  105. list.concat(taicpu.op_sym(A_JMP,S_NO,objectlibrary.newasmsymbol(procdef.mangledname,AB_EXTERNAL,AT_FUNCTION)));
  106. List.concat(Tai_symbol_end.Createname(labelname));
  107. end;
  108. begin
  109. cg:=tcgx86_64.create;
  110. {$ifndef cpu64bit}
  111. cg64:=tcg64f64.create;
  112. {$endif cpu64bit}
  113. end.
  114. {
  115. $Log$
  116. Revision 1.22 2005-02-14 17:13:10 peter
  117. * truncate log
  118. Revision 1.21 2005/02/06 00:05:56 florian
  119. + x86_64 pic draft
  120. Revision 1.20 2005/01/24 22:08:33 peter
  121. * interface wrapper generation moved to cgobj
  122. * generate interface wrappers after the module is parsed
  123. }