hlcgcpu.pas 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. globtype,
  24. aasmdata,
  25. symdef,
  26. hlcg2ll;
  27. type
  28. thlcgcpu = class(thlcg2ll)
  29. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);override;
  30. procedure a_jmp_external_name(list: TAsmList; const externalname: TSymStr);override;
  31. end;
  32. procedure create_hlcodegen;
  33. implementation
  34. uses
  35. verbose,fmodule,
  36. aasmbase,aasmtai,aasmcpu,
  37. parabase,
  38. symconst,symtype,symsym,
  39. cgbase,cgutils,cgobj,hlcgobj,cpubase,cgcpu;
  40. procedure thlcgcpu.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  41. var
  42. make_global : boolean;
  43. href : treference;
  44. hsym : tsym;
  45. paraloc : pcgparalocation;
  46. begin
  47. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  48. Internalerror(200006137);
  49. if not assigned(procdef.struct) or
  50. (procdef.procoptions*[po_classmethod, po_staticmethod,
  51. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  52. Internalerror(200006138);
  53. if procdef.owner.symtabletype<>ObjectSymtable then
  54. Internalerror(200109191);
  55. make_global:=false;
  56. if (not current_module.is_unit) or create_smartlink or
  57. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  58. make_global:=true;
  59. if make_global then
  60. List.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0,procdef))
  61. else
  62. List.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0,procdef));
  63. { set param1 interface to self }
  64. procdef.init_paraloc_info(callerside);
  65. hsym:=tsym(procdef.parast.Find('self'));
  66. if not(assigned(hsym) and
  67. (hsym.typ=paravarsym)) then
  68. internalerror(2010103101);
  69. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  70. if assigned(paraloc^.next) then
  71. InternalError(2013020101);
  72. case paraloc^.loc of
  73. LOC_REGISTER:
  74. begin
  75. if ((ioffset>=simm13lo) and (ioffset<=simm13hi)) then
  76. cg.a_op_const_reg(list,OP_SUB,paraloc^.size,ioffset,paraloc^.register)
  77. else
  78. begin
  79. cg.a_load_const_reg(list,paraloc^.size,ioffset,NR_G1);
  80. cg.a_op_reg_reg(list,OP_SUB,paraloc^.size,NR_G1,paraloc^.register);
  81. end;
  82. end;
  83. else
  84. internalerror(2010103102);
  85. end;
  86. if (po_virtualmethod in procdef.procoptions) and
  87. not is_objectpascal_helper(procdef.struct) then
  88. begin
  89. if (procdef.extnumber=$ffff) then
  90. Internalerror(200006139);
  91. { mov 0(%rdi),%rax ; load vmt}
  92. reference_reset_base(href,voidpointertype,paraloc^.register,0,sizeof(pint),[]);
  93. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_G1);
  94. { jmp *vmtoffs(%eax) ; method offs }
  95. reference_reset_base(href,voidpointertype,NR_G1,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber),sizeof(pint),[]);
  96. list.concat(taicpu.op_ref_reg(A_LD,href,NR_G1));
  97. list.concat(taicpu.op_reg(A_JMP,NR_G1));
  98. { Delay slot }
  99. list.Concat(TAiCpu.Op_none(A_NOP));
  100. end
  101. else
  102. a_jmp_external_name(list,procdef.mangledname);
  103. List.concat(Tai_symbol_end.Createname(labelname));
  104. end;
  105. procedure thlcgcpu.a_jmp_external_name(list: TAsmList; const externalname: TSymStr);
  106. begin
  107. { CALL overwrites %o7 with its own address, we use delay slot to restore it. }
  108. list.concat(taicpu.op_reg_reg(A_MOV,NR_O7,NR_G1));
  109. list.concat(taicpu.op_sym(A_CALL,current_asmdata.RefAsmSymbol(externalname,AT_FUNCTION)));
  110. list.concat(taicpu.op_reg_reg(A_MOV,NR_G1,NR_O7));
  111. end;
  112. procedure create_hlcodegen;
  113. begin
  114. hlcg:=thlcgcpu.create;
  115. create_codegen;
  116. end;
  117. begin
  118. chlcgobj:=thlcgcpu;
  119. end.