hlcgcpu.pas 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. {
  2. Copyright (c) 1998-2010 by Florian Klaempfl and Jonas Maebe
  3. Member of the Free Pascal development team
  4. This unit contains routines to create a pass-through high-level code
  5. generator. This is used by most regular code generators.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit hlcgcpu;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. aasmdata,
  24. symdef,
  25. hlcg2ll;
  26. type
  27. thlcgcpu = class(thlcg2ll)
  28. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);override;
  29. procedure g_external_wrapper(list : TAsmList; procdef: tprocdef; const externalname: string);override;
  30. end;
  31. procedure create_hlcodegen;
  32. implementation
  33. uses
  34. verbose,globtype,fmodule,
  35. aasmbase,aasmtai,aasmcpu,
  36. parabase,
  37. symconst,symtype,symsym,
  38. cgbase,cgutils,cgobj,hlcgobj,cpubase,cgcpu;
  39. procedure thlcgcpu.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  40. var
  41. make_global : boolean;
  42. href : treference;
  43. hsym : tsym;
  44. paraloc : pcgparalocation;
  45. begin
  46. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  47. Internalerror(200006137);
  48. if not assigned(procdef.struct) or
  49. (procdef.procoptions*[po_classmethod, po_staticmethod,
  50. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  51. Internalerror(200006138);
  52. if procdef.owner.symtabletype<>ObjectSymtable then
  53. Internalerror(200109191);
  54. make_global:=false;
  55. if (not current_module.is_unit) or create_smartlink or
  56. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  57. make_global:=true;
  58. if make_global then
  59. List.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0))
  60. else
  61. List.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0));
  62. { set param1 interface to self }
  63. procdef.init_paraloc_info(callerside);
  64. hsym:=tsym(procdef.parast.Find('self'));
  65. if not(assigned(hsym) and
  66. (hsym.typ=paravarsym)) then
  67. internalerror(2010103101);
  68. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  69. if assigned(paraloc^.next) then
  70. InternalError(2013020101);
  71. case paraloc^.loc of
  72. LOC_REGISTER:
  73. begin
  74. if ((ioffset>=simm13lo) and (ioffset<=simm13hi)) then
  75. cg.a_op_const_reg(list,OP_SUB,paraloc^.size,ioffset,paraloc^.register)
  76. else
  77. begin
  78. cg.a_load_const_reg(list,paraloc^.size,ioffset,NR_G1);
  79. cg.a_op_reg_reg(list,OP_SUB,paraloc^.size,NR_G1,paraloc^.register);
  80. end;
  81. end;
  82. else
  83. internalerror(2010103102);
  84. end;
  85. if (po_virtualmethod in procdef.procoptions) and
  86. not is_objectpascal_helper(procdef.struct) then
  87. begin
  88. if (procdef.extnumber=$ffff) then
  89. Internalerror(200006139);
  90. { mov 0(%rdi),%rax ; load vmt}
  91. reference_reset_base(href,voidpointertype,paraloc^.register,0,sizeof(pint));
  92. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_G1);
  93. { jmp *vmtoffs(%eax) ; method offs }
  94. reference_reset_base(href,voidpointertype,NR_G1,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber),sizeof(pint));
  95. list.concat(taicpu.op_ref_reg(A_LD,href,NR_G1));
  96. list.concat(taicpu.op_reg(A_JMP,NR_G1));
  97. { Delay slot }
  98. list.Concat(TAiCpu.Op_none(A_NOP));
  99. end
  100. else
  101. g_external_wrapper(list,procdef,procdef.mangledname);
  102. List.concat(Tai_symbol_end.Createname(labelname));
  103. end;
  104. procedure thlcgcpu.g_external_wrapper(list : TAsmList; procdef: tprocdef; const externalname: string);
  105. begin
  106. { CALL overwrites %o7 with its own address, we use delay slot to restore it. }
  107. list.concat(taicpu.op_reg_reg(A_MOV,NR_O7,NR_G1));
  108. list.concat(taicpu.op_sym(A_CALL,current_asmdata.RefAsmSymbol(externalname)));
  109. list.concat(taicpu.op_reg_reg(A_MOV,NR_G1,NR_O7));
  110. end;
  111. procedure create_hlcodegen;
  112. begin
  113. hlcg:=thlcgcpu.create;
  114. create_codegen;
  115. end;
  116. begin
  117. chlcgobj:=thlcgcpu;
  118. end.