hlcgcpu.pas 9.9 KB

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