hlcgcpu.pas 9.9 KB

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