hlcgcpu.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. aasmbase, aasmdata,
  25. cgbase, cgutils,
  26. symtype,symdef,
  27. parabase, hlcgobj, hlcg2ll;
  28. type
  29. thlcgmips = class(thlcg2ll)
  30. function a_call_name(list: TAsmList; pd: tprocdef; const s: TSymStr; const paras: array of pcgpara; forceresdef: tdef; weak: boolean): tcgpara; override;
  31. procedure a_load_subsetreg_reg(list: TAsmList; subsetsize, tosize: tdef; const sreg: tsubsetregister; destreg: tregister);override;
  32. protected
  33. procedure a_load_regconst_subsetreg_intern(list: TAsmList; fromsize, subsetsize: tdef; fromreg: tregister; const sreg: tsubsetregister; slopt: tsubsetloadopt); override;
  34. public
  35. procedure g_intf_wrapper(list: tasmlist; procdef: tprocdef; const labelname: string; ioffset: longint); override;
  36. procedure a_jmp_external_name(list: TAsmList; const externalname: TSymStr);override;
  37. end;
  38. implementation
  39. uses
  40. verbose,globals,
  41. fmodule,
  42. aasmtai,aasmcpu,
  43. cutils,
  44. symconst,symsym,defutil,
  45. cgobj,
  46. cpubase,
  47. cpuinfo,
  48. cgcpu;
  49. function thlcgmips.a_call_name(list: TAsmList; pd: tprocdef; const s: TSymStr; const paras: array of pcgpara; forceresdef: tdef; weak: boolean): tcgpara;
  50. var
  51. ref: treference;
  52. sym: tasmsymbol;
  53. begin
  54. if (cs_create_pic in current_settings.moduleswitches) then begin
  55. {
  56. !!!!!!!!!!!!!!!!1
  57. I'm not shure this code is correct
  58. see line 86 -> if NOT pic then do pic code
  59. }
  60. if weak then
  61. sym:=current_asmdata.WeakRefAsmSymbol(s,AT_FUNCTION)
  62. else
  63. sym:=current_asmdata.RefAsmSymbol(s,AT_FUNCTION);
  64. if (po_external in pd.procoptions) then
  65. begin
  66. if not (cs_create_pic in current_settings.moduleswitches) then
  67. begin
  68. reference_reset_symbol(ref,current_asmdata.RefAsmSymbol('_gp',AT_DATA),0,sizeof(aint),[]);
  69. list.concat(tai_comment.create(strpnew('Using PIC code for a_call_name')));
  70. cg.a_loadaddr_ref_reg(list,ref,NR_GP);
  71. end;
  72. TCGMIPS(cg).a_call_sym_pic(list,sym);
  73. end
  74. else
  75. cg.a_call_name(list,s,weak);
  76. end else
  77. cg.a_call_name(list,s,weak);
  78. { set the result location }
  79. result:=get_call_result_cgpara(pd,forceresdef);
  80. end;
  81. procedure thlcgmips.a_load_subsetreg_reg(list: TAsmList; subsetsize, tosize: tdef; const sreg: tsubsetregister; destreg: tregister);
  82. var
  83. cgsubsetsize,
  84. cgtosize: tcgsize;
  85. begin
  86. cgsubsetsize:=def_cgsize(subsetsize);
  87. cgtosize:=def_cgsize(tosize);
  88. if (current_settings.cputype<>cpu_mips32r2) and (current_settings.cputype<>cpu_pic32mx) then
  89. inherited a_load_subsetreg_reg(list,subsetsize,tosize,sreg,destreg)
  90. else if (sreg.bitlen>32) then
  91. InternalError(2013070201)
  92. else if (sreg.bitlen<>32) then
  93. begin
  94. list.concat(taicpu.op_reg_reg_const_const(A_EXT,destreg,sreg.subsetreg,
  95. sreg.startbit,sreg.bitlen));
  96. { types with a negative lower bound are always a base type (8, 16, 32 bits) }
  97. if (cgsubsetsize in [OS_S8..OS_S128]) then
  98. if ((sreg.bitlen mod 8) = 0) then
  99. begin
  100. cg.a_load_reg_reg(list,tcgsize2unsigned[cgsubsetsize],cgsubsetsize,destreg,destreg);
  101. cg.a_load_reg_reg(list,cgsubsetsize,cgtosize,destreg,destreg);
  102. end
  103. else
  104. begin
  105. cg.a_op_const_reg(list,OP_SHL,OS_INT,32-sreg.bitlen,destreg);
  106. cg.a_op_const_reg(list,OP_SAR,OS_INT,32-sreg.bitlen,destreg);
  107. end;
  108. end
  109. else
  110. cg.a_load_reg_reg(list,cgsubsetsize,cgtosize,sreg.subsetreg,destreg);
  111. end;
  112. procedure thlcgmips.a_load_regconst_subsetreg_intern(list: TAsmList; fromsize, subsetsize: tdef; fromreg: tregister; const sreg: tsubsetregister; slopt: tsubsetloadopt);
  113. begin
  114. if (current_settings.cputype<>cpu_mips32r2) and (current_settings.cputype<>cpu_pic32mx) then
  115. inherited a_load_regconst_subsetreg_intern(list,fromsize,subsetsize,fromreg,sreg,slopt)
  116. else if (sreg.bitlen>32) then
  117. InternalError(2013070202)
  118. else if (sreg.bitlen<>32) then
  119. begin
  120. case slopt of
  121. SL_SETZERO:
  122. fromreg:=NR_R0;
  123. SL_SETMAX:
  124. begin
  125. fromreg:=cg.getintregister(list,OS_INT);
  126. cg.a_load_const_reg(list,OS_INT,-1,fromreg);
  127. end;
  128. else
  129. ;
  130. end;
  131. list.concat(taicpu.op_reg_reg_const_const(A_INS,sreg.subsetreg,fromreg,
  132. sreg.startbit,sreg.bitlen));
  133. end
  134. else if not (slopt in [SL_SETZERO,SL_SETMAX]) then
  135. cg.a_load_reg_reg(list,def_cgsize(fromsize),def_cgsize(subsetsize),fromreg,sreg.subsetreg)
  136. else
  137. inherited a_load_regconst_subsetreg_intern(list,fromsize,subsetsize,fromreg,sreg,slopt);
  138. end;
  139. procedure thlcgmips.a_jmp_external_name(list: TAsmList; const externalname: TSymStr);
  140. var
  141. href: treference;
  142. begin
  143. reference_reset_symbol(href,current_asmdata.RefAsmSymbol(externalname,AT_DATA),0,sizeof(aint),[]);
  144. { Always do indirect jump using $t9, it won't harm in non-PIC mode }
  145. if (cs_create_pic in current_settings.moduleswitches) then
  146. begin
  147. list.concat(taicpu.op_none(A_P_SET_NOREORDER));
  148. list.concat(taicpu.op_reg(A_P_CPLOAD,NR_PIC_FUNC));
  149. href.base:=NR_GP;
  150. href.refaddr:=addr_pic_call16;
  151. list.concat(taicpu.op_reg_ref(A_LW,NR_PIC_FUNC,href));
  152. list.concat(taicpu.op_reg(A_JR,NR_PIC_FUNC));
  153. { Delay slot }
  154. list.Concat(taicpu.op_none(A_NOP));
  155. list.Concat(taicpu.op_none(A_P_SET_REORDER));
  156. end
  157. else
  158. begin
  159. href.refaddr:=addr_high;
  160. list.concat(taicpu.op_reg_ref(A_LUI,NR_PIC_FUNC,href));
  161. href.refaddr:=addr_low;
  162. list.concat(taicpu.op_reg_ref(A_ADDIU,NR_PIC_FUNC,href));
  163. list.concat(taicpu.op_reg(A_JR,NR_PIC_FUNC));
  164. { Delay slot }
  165. list.Concat(taicpu.op_none(A_NOP));
  166. end;
  167. end;
  168. procedure thlcgmips.g_intf_wrapper(list: tasmlist; procdef: tprocdef; const labelname: string; ioffset: longint);
  169. var
  170. make_global: boolean;
  171. hsym: tsym;
  172. href: treference;
  173. paraloc: Pcgparalocation;
  174. IsVirtual: boolean;
  175. begin
  176. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  177. Internalerror(200006137);
  178. if not assigned(procdef.struct) or
  179. (procdef.procoptions * [po_classmethod, po_staticmethod,
  180. po_methodpointer, po_interrupt, po_iocheck] <> []) then
  181. Internalerror(200006138);
  182. if procdef.owner.symtabletype <> objectsymtable then
  183. Internalerror(200109191);
  184. make_global := False;
  185. if (not current_module.is_unit) or create_smartlink or
  186. (procdef.owner.defowner.owner.symtabletype = globalsymtable) then
  187. make_global := True;
  188. if make_global then
  189. List.concat(Tai_symbol.Createname_global(labelname, AT_FUNCTION, 0, procdef))
  190. else
  191. List.concat(Tai_symbol.Createname_hidden(labelname, AT_FUNCTION, 0, procdef));
  192. IsVirtual:=(po_virtualmethod in procdef.procoptions) and
  193. not is_objectpascal_helper(procdef.struct);
  194. if (cs_create_pic in current_settings.moduleswitches) and
  195. (not IsVirtual) then
  196. begin
  197. list.concat(Taicpu.op_none(A_P_SET_NOREORDER));
  198. list.concat(Taicpu.op_reg(A_P_CPLOAD,NR_PIC_FUNC));
  199. list.concat(Taicpu.op_none(A_P_SET_REORDER));
  200. end;
  201. { set param1 interface to self }
  202. procdef.init_paraloc_info(callerside);
  203. hsym:=tsym(procdef.parast.Find('self'));
  204. if not(assigned(hsym) and
  205. (hsym.typ=paravarsym)) then
  206. internalerror(2010103101);
  207. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  208. if assigned(paraloc^.next) then
  209. InternalError(2013020101);
  210. case paraloc^.loc of
  211. LOC_REGISTER:
  212. begin
  213. if ((ioffset>=simm16lo) and (ioffset<=simm16hi)) then
  214. cg.a_op_const_reg(list,OP_SUB, paraloc^.size,ioffset,paraloc^.register)
  215. else
  216. begin
  217. cg.a_load_const_reg(list, paraloc^.size, ioffset, NR_R1);
  218. cg.a_op_reg_reg(list, OP_SUB, paraloc^.size, NR_R1, paraloc^.register);
  219. end;
  220. end;
  221. else
  222. internalerror(2010103102);
  223. end;
  224. if IsVirtual then
  225. begin
  226. { load VMT pointer }
  227. reference_reset_base(href,voidpointertype,paraloc^.register,0,ctempposinvalid,sizeof(aint),[]);
  228. list.concat(taicpu.op_reg_ref(A_LW,NR_VMT,href));
  229. if (procdef.extnumber=$ffff) then
  230. Internalerror(2000061303);
  231. { TODO: case of large VMT is not handled }
  232. { We have no reason not to use $t9 even in non-PIC mode. }
  233. reference_reset_base(href, voidpointertype, NR_VMT, tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber), ctempposinvalid, sizeof(aint), []);
  234. list.concat(taicpu.op_reg_ref(A_LW,NR_PIC_FUNC,href));
  235. list.concat(taicpu.op_reg(A_JR, NR_PIC_FUNC));
  236. end
  237. else if not (cs_create_pic in current_settings.moduleswitches) then
  238. list.concat(taicpu.op_sym(A_J,current_asmdata.RefAsmSymbol(procdef.mangledname,AT_FUNCTION)))
  239. else
  240. begin
  241. { GAS does not expand "J symbol" into PIC sequence }
  242. reference_reset_symbol(href,current_asmdata.RefAsmSymbol(procdef.mangledname,AT_FUNCTION),0,sizeof(pint),[]);
  243. href.base:=NR_GP;
  244. href.refaddr:=addr_pic_call16;
  245. list.concat(taicpu.op_reg_ref(A_LW,NR_PIC_FUNC,href));
  246. list.concat(taicpu.op_reg(A_JR,NR_PIC_FUNC));
  247. end;
  248. { Delay slot }
  249. list.Concat(TAiCpu.Op_none(A_NOP));
  250. List.concat(Tai_symbol_end.Createname(labelname));
  251. end;
  252. procedure create_hlcodegen_cpu;
  253. begin
  254. hlcg:=thlcgmips.create;
  255. create_codegen;
  256. end;
  257. begin
  258. chlcgobj:=thlcgmips;
  259. create_hlcodegen:=@create_hlcodegen_cpu;
  260. end.