hlcgcpu.pas 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. symtype,
  24. aasmdata,
  25. symdef,
  26. cgbase,cgutils,
  27. hlcgobj, hlcg2ll;
  28. type
  29. thlcgaarch64 = class(thlcg2ll)
  30. procedure a_load_subsetreg_reg(list: TAsmList; subsetsize, tosize: tdef; const sreg: tsubsetregister; destreg: tregister); override;
  31. procedure a_load_subsetreg_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize: tdef; const fromsreg, tosreg: tsubsetregister); override;
  32. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);override;
  33. protected
  34. procedure a_load_regconst_subsetreg_intern(list: TAsmList; fromsize, subsetsize: tdef; fromreg: tregister; const sreg: tsubsetregister; slopt: tsubsetloadopt); override;
  35. end;
  36. implementation
  37. uses
  38. verbose,globtype,fmodule,
  39. aasmbase,aasmtai,
  40. symconst,symsym,defutil,
  41. cpubase,aasmcpu,parabase,
  42. cgobj,cgcpu;
  43. procedure thlcgaarch64.a_load_subsetreg_reg(list: TAsmList; subsetsize, tosize: tdef; const sreg: tsubsetregister; destreg: tregister);
  44. var
  45. op: tasmop;
  46. tocgsize: tcgsize;
  47. tmpdestreg: tregister;
  48. begin
  49. tocgsize:=def_cgsize(tosize);
  50. if (sreg.startbit<>0) or
  51. not((sreg.subsetregsize in [OS_32,OS_S32]) and
  52. (sreg.bitlen=32)) or
  53. not((sreg.subsetregsize in [OS_64,OS_S64]) and
  54. (sreg.bitlen=64)) then
  55. begin
  56. if is_signed(subsetsize) then
  57. op:=A_SBFX
  58. else
  59. op:=A_UBFX;
  60. { source and destination register of SBFX/UBFX have to be the same size }
  61. if (sreg.subsetregsize in [OS_64,OS_S64]) and
  62. not(tocgsize in [OS_64,OS_S64]) then
  63. tmpdestreg:=cg.getintregister(list,OS_64)
  64. else if not(sreg.subsetregsize in [OS_64,OS_S64]) and
  65. (tocgsize in [OS_64,OS_S64]) then
  66. tmpdestreg:=cg.getintregister(list,OS_32)
  67. else
  68. tmpdestreg:=destreg;
  69. list.concat(taicpu.op_reg_reg_const_const(op,tmpdestreg,sreg.subsetreg,sreg.startbit,sreg.bitlen));
  70. { need to sign extend further or truncate? }
  71. if (sreg.subsetregsize=OS_S64) and
  72. not(tocgsize in [OS_64,OS_S64]) then
  73. cg.a_load_reg_reg(list,OS_S64,tocgsize,tmpdestreg,destreg)
  74. else if is_signed(subsetsize) and
  75. (tocgsize in [OS_8,OS_16]) then
  76. cg.a_load_reg_reg(list,OS_32,tocgsize,tmpdestreg,destreg)
  77. else if tmpdestreg<>destreg then
  78. cg.a_load_reg_reg(list,def_cgsize(subsetsize),tocgsize,tmpdestreg,destreg)
  79. end
  80. else
  81. cg.a_load_reg_reg(list,def_cgsize(subsetsize),tocgsize,sreg.subsetreg,destreg);
  82. end;
  83. procedure makeregssamesize(list: tasmlist; fromsize, tosize: tcgsize; orgfromreg, orgtoreg: tregister; out newfromreg, newtoreg: tregister);
  84. begin
  85. if (fromsize in [OS_S64,OS_64])<>
  86. (tosize in [OS_S64,OS_64]) then
  87. begin
  88. newfromreg:=cg.makeregsize(list,orgfromreg,OS_64);
  89. newtoreg:=cg.makeregsize(list,orgtoreg,OS_64);
  90. end
  91. else
  92. begin
  93. newfromreg:=orgfromreg;
  94. newtoreg:=orgtoreg;
  95. end;
  96. end;
  97. procedure thlcgaarch64.a_load_subsetreg_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize: tdef; const fromsreg, tosreg: tsubsetregister);
  98. var
  99. fromreg, toreg: tregister;
  100. begin
  101. { BFM can only insert a bitfield that starts at position 0 in the source
  102. source or destination register }
  103. if (tosreg.startbit=0) and
  104. (fromsreg.bitlen>=tosreg.bitlen) then
  105. begin
  106. makeregssamesize(list,fromsreg.subsetregsize,tosreg.subsetregsize,fromsreg.subsetreg,tosreg.subsetreg,fromreg,toreg);
  107. list.concat(taicpu.op_reg_reg_const_const(A_BFXIL,toreg,fromreg,fromsreg.startbit,tosreg.bitlen))
  108. end
  109. else if (fromsreg.startbit=0) and
  110. (fromsreg.bitlen>=tosreg.bitlen) then
  111. begin
  112. makeregssamesize(list,fromsreg.subsetregsize,tosreg.subsetregsize,fromsreg.subsetreg,tosreg.subsetreg,fromreg,toreg);
  113. list.concat(taicpu.op_reg_reg_const_const(A_BFI,toreg,fromreg,tosreg.startbit,tosreg.bitlen))
  114. end
  115. else
  116. inherited;
  117. end;
  118. procedure thlcgaarch64.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  119. var
  120. make_global: boolean;
  121. href: treference;
  122. hsym: tsym;
  123. paraloc: pcgparalocation;
  124. op: tasmop;
  125. begin
  126. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  127. Internalerror(200006137);
  128. if not assigned(procdef.struct) or
  129. (procdef.procoptions*[po_classmethod, po_staticmethod,
  130. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  131. Internalerror(200006138);
  132. if procdef.owner.symtabletype<>ObjectSymtable then
  133. Internalerror(200109191);
  134. make_global:=false;
  135. if (not current_module.is_unit) or create_smartlink_library or
  136. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  137. make_global:=true;
  138. if make_global then
  139. list.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0,procdef))
  140. else
  141. list.concat(Tai_symbol.Createname_hidden(labelname,AT_FUNCTION,0,procdef));
  142. { set param1 interface to self }
  143. procdef.init_paraloc_info(callerside);
  144. hsym:=tsym(procdef.parast.Find('self'));
  145. if not(assigned(hsym) and
  146. (hsym.typ=paravarsym)) then
  147. internalerror(2010103101);
  148. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  149. if assigned(paraloc^.next) then
  150. InternalError(2013020101);
  151. case paraloc^.loc of
  152. LOC_REGISTER:
  153. tcgaarch64(cg).handle_reg_imm12_reg(list,A_SUB,paraloc^.size,paraloc^.register,ioffset,paraloc^.register,NR_IP0,false,true);
  154. else
  155. internalerror(2010103102);
  156. end;
  157. if (po_virtualmethod in procdef.procoptions) and
  158. not is_objectpascal_helper(procdef.struct) then
  159. begin
  160. if (procdef.extnumber=$ffff) then
  161. Internalerror(2000061302);
  162. { mov 0(%rdi),%rax ; load vmt}
  163. reference_reset_base(href,voidpointertype,paraloc^.register,0,ctempposinvalid,sizeof(pint),[]);
  164. getcpuregister(list,NR_IP0);
  165. a_load_ref_reg(list,voidpointertype,voidpointertype,href,NR_IP0);
  166. { jmp *vmtoffs(%eax) ; method offs }
  167. reference_reset_base(href,voidpointertype,NR_IP0,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber),ctempposinvalid,sizeof(pint),[]);
  168. op:=A_LDR;
  169. tcgaarch64(cg).make_simple_ref(list,op,OS_ADDR,PF_None,href,NR_IP0);
  170. list.concat(taicpu.op_reg_ref(op,NR_IP0,href));
  171. ungetcpuregister(list,NR_IP0);
  172. list.concat(taicpu.op_reg(A_BR,NR_IP0));
  173. end
  174. else
  175. cg.a_jmp_name(list,procdef.mangledname);
  176. list.concat(Tai_symbol_end.Createname(labelname));
  177. end;
  178. procedure thlcgaarch64.a_load_regconst_subsetreg_intern(list: TAsmList; fromsize, subsetsize: tdef; fromreg: tregister; const sreg: tsubsetregister; slopt: tsubsetloadopt);
  179. var
  180. toreg: tregister;
  181. begin
  182. if slopt in [SL_SETZERO,SL_SETMAX] then
  183. inherited
  184. else if not(sreg.bitlen in [32,64]) or
  185. (sreg.startbit<>0) or
  186. (getsubreg(fromreg)<getsubreg(sreg.subsetreg)) then
  187. begin
  188. makeregssamesize(list,def_cgsize(fromsize),sreg.subsetregsize,fromreg,sreg.subsetreg,fromreg,toreg);
  189. list.concat(taicpu.op_reg_reg_const_const(A_BFI,toreg,fromreg,sreg.startbit,sreg.bitlen))
  190. end
  191. else
  192. a_load_reg_reg(list,fromsize,subsetsize,fromreg,sreg.subsetreg);
  193. end;
  194. procedure create_hlcodegen_cpu;
  195. begin
  196. hlcg:=thlcgaarch64.create;
  197. create_codegen;
  198. end;
  199. begin
  200. chlcgobj:=thlcgaarch64;
  201. create_hlcodegen:=@create_hlcodegen_cpu;
  202. end.