hlcgcpu.pas 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. end;
  30. procedure create_hlcodegen;
  31. implementation
  32. uses
  33. globtype,verbose,
  34. procinfo,fmodule,
  35. symconst,
  36. aasmbase,aasmtai,aasmcpu,
  37. hlcgobj,
  38. cgbase, cgutils, cpubase, cgobj, cgcpu;
  39. procedure thlcgcpu.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  40. procedure loadvmttor12;
  41. var
  42. tmpref,
  43. href : treference;
  44. extrareg : boolean;
  45. l : TAsmLabel;
  46. begin
  47. reference_reset_base(href,voidpointertype,NR_R0,0,sizeof(pint));
  48. if GenerateThumbCode then
  49. begin
  50. if (href.offset in [0..124]) and ((href.offset mod 4)=0) then
  51. begin
  52. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  53. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R0);
  54. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
  55. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  56. end
  57. else
  58. begin
  59. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0,RS_R1]));
  60. { create consts entry }
  61. reference_reset(tmpref,4);
  62. current_asmdata.getjumplabel(l);
  63. current_procinfo.aktlocaldata.Concat(tai_align.Create(4));
  64. cg.a_label(current_procinfo.aktlocaldata,l);
  65. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  66. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(href.offset));
  67. tmpref.symbol:=l;
  68. tmpref.base:=NR_PC;
  69. list.concat(taicpu.op_reg_ref(A_LDR,NR_R1,tmpref));
  70. href.offset:=0;
  71. href.index:=NR_R1;
  72. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R0);
  73. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
  74. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0,RS_R1]));
  75. end;
  76. end
  77. else
  78. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R12);
  79. end;
  80. procedure op_onr12methodaddr;
  81. var
  82. tmpref,
  83. href : treference;
  84. l : TAsmLabel;
  85. begin
  86. if (procdef.extnumber=$ffff) then
  87. Internalerror(200006139);
  88. if GenerateThumbCode then
  89. begin
  90. reference_reset_base(href,voidpointertype,NR_R0,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber),sizeof(pint));
  91. if (href.offset in [0..124]) and ((href.offset mod 4)=0) then
  92. begin
  93. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  94. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R0);
  95. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
  96. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  97. end
  98. else
  99. begin
  100. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0,RS_R1]));
  101. { create consts entry }
  102. reference_reset(tmpref,4);
  103. current_asmdata.getjumplabel(l);
  104. current_procinfo.aktlocaldata.Concat(tai_align.Create(4));
  105. cg.a_label(current_procinfo.aktlocaldata,l);
  106. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  107. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(href.offset));
  108. tmpref.symbol:=l;
  109. tmpref.base:=NR_PC;
  110. list.concat(taicpu.op_reg_ref(A_LDR,NR_R1,tmpref));
  111. href.offset:=0;
  112. href.index:=NR_R1;
  113. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R0);
  114. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
  115. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0,RS_R1]));
  116. end;
  117. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R12));
  118. end
  119. else
  120. begin
  121. reference_reset_base(href,voidpointertype,NR_R12,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber),sizeof(pint));
  122. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R12);
  123. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R12));
  124. end;
  125. end;
  126. var
  127. make_global : boolean;
  128. tmpref : treference;
  129. l : TAsmLabel;
  130. begin
  131. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  132. Internalerror(200006137);
  133. if not assigned(procdef.struct) or
  134. (procdef.procoptions*[po_classmethod, po_staticmethod,
  135. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  136. Internalerror(200006138);
  137. if procdef.owner.symtabletype<>ObjectSymtable then
  138. Internalerror(200109191);
  139. make_global:=false;
  140. if (not current_module.is_unit) or
  141. create_smartlink or
  142. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  143. make_global:=true;
  144. if make_global then
  145. list.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0))
  146. else
  147. list.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0));
  148. { the wrapper might need aktlocaldata for the additional data to
  149. load the constant }
  150. current_procinfo:=cprocinfo.create(nil);
  151. { set param1 interface to self }
  152. g_adjust_self_value(list,procdef,ioffset);
  153. { case 4 }
  154. if (po_virtualmethod in procdef.procoptions) and
  155. not is_objectpascal_helper(procdef.struct) then
  156. begin
  157. loadvmttor12;
  158. op_onr12methodaddr;
  159. end
  160. { case 0 }
  161. else if GenerateThumbCode then
  162. begin
  163. { bl cannot be used here because it destroys lr }
  164. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  165. { create consts entry }
  166. reference_reset(tmpref,4);
  167. current_asmdata.getjumplabel(l);
  168. current_procinfo.aktlocaldata.Concat(tai_align.Create(4));
  169. cg.a_label(current_procinfo.aktlocaldata,l);
  170. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  171. current_procinfo.aktlocaldata.concat(tai_const.Create_sym(current_asmdata.RefAsmSymbol(procdef.mangledname)));
  172. tmpref.symbol:=l;
  173. tmpref.base:=NR_PC;
  174. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,tmpref,NR_R0);
  175. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
  176. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  177. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R12));
  178. end
  179. else
  180. list.concat(taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(procdef.mangledname)));
  181. list.concatlist(current_procinfo.aktlocaldata);
  182. current_procinfo.Free;
  183. current_procinfo:=nil;
  184. list.concat(Tai_symbol_end.Createname(labelname));
  185. end;
  186. procedure create_hlcodegen;
  187. begin
  188. hlcg:=thlcgcpu.create;
  189. create_codegen;
  190. end;
  191. begin
  192. chlcgobj:=thlcgcpu;
  193. end.