2
0

hlcgcpu.pas 4.9 KB

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