nllvmcnv.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. {
  2. Copyright (c) 2014 by Jonas Maebe
  3. Generate LLVM IR for type converting nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit nllvmcnv;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. symtype,
  22. node,ncnv,ncgcnv,defcmp;
  23. type
  24. tllvmtypeconvnode = class(tcgtypeconvnode)
  25. public
  26. class function target_specific_need_equal_typeconv(fromdef, todef: tdef): boolean; override;
  27. protected
  28. function first_int_to_real: tnode; override;
  29. function first_int_to_bool: tnode; override;
  30. function first_nil_to_methodprocvar: tnode; override;
  31. function first_real_to_real: tnode; override;
  32. { procedure second_int_to_int;override; }
  33. { procedure second_string_to_string;override; }
  34. { procedure second_cstring_to_pchar;override; }
  35. { procedure second_string_to_chararray;override; }
  36. { procedure second_array_to_pointer;override; }
  37. procedure second_pointer_to_array;override;
  38. { procedure second_chararray_to_string;override; }
  39. { procedure second_char_to_string;override; }
  40. procedure second_int_to_real;override;
  41. { procedure second_real_to_real;override; }
  42. { procedure second_cord_to_pointer;override; }
  43. procedure second_proc_to_procvar;override;
  44. procedure second_nil_to_methodprocvar; override;
  45. { procedure second_bool_to_int;override; }
  46. procedure second_int_to_bool;override;
  47. { procedure second_load_smallset;override; }
  48. { procedure second_ansistring_to_pchar;override; }
  49. { procedure second_pchar_to_string;override; }
  50. { procedure second_class_to_intf;override; }
  51. { procedure second_char_to_char;override; }
  52. procedure second_nothing; override;
  53. end;
  54. implementation
  55. uses
  56. globtype,globals,cutils,verbose,
  57. aasmbase,aasmdata,
  58. llvmbase,llvminfo,llvmfeatures,aasmllvm,aasmllvmmetadata,llvmdef,
  59. procinfo,
  60. ncal,ncon,
  61. symconst,symdef,defutil,
  62. cgbase,cgutils,tgobj,hlcgobj,pass_2;
  63. { tllvmtypeconvnode }
  64. class function tllvmtypeconvnode.target_specific_need_equal_typeconv(fromdef, todef: tdef): boolean;
  65. begin
  66. if (llvmflag_opaque_ptr in llvmversion_properties[current_settings.llvmversion]) and
  67. is_address(fromdef) and
  68. is_address(todef) then
  69. begin
  70. result:=false;
  71. exit;
  72. end;
  73. result:=
  74. (fromdef<>todef) and
  75. { two procdefs that are structurally the same but semantically different
  76. still need a conversion }
  77. (
  78. ((fromdef.typ=procvardef) and
  79. (todef.typ=procvardef)) or
  80. { same for two different specialisations }
  81. ((df_specialization in fromdef.defoptions) and
  82. (df_specialization in todef.defoptions)) or
  83. { external objc classes referring to the same type }
  84. (is_objc_class_or_protocol(fromdef) and
  85. is_objc_class_or_protocol(todef)) or
  86. { typed from/to untyped filedef in ISO mode: have to keep because of
  87. the get/put buffer }
  88. ((fromdef.typ=filedef) and
  89. (tfiledef(fromdef).filetyp=ft_typed) and
  90. (todef.typ=filedef) and
  91. (tfiledef(todef).filetyp=ft_typed) and
  92. (not equal_defs(tfiledef(fromdef).typedfiledef, tfiledef(todef).typedfiledef) or
  93. target_specific_need_equal_typeconv(tfiledef(fromdef).typedfiledef, tfiledef(todef).typedfiledef))
  94. )
  95. );
  96. end;
  97. function tllvmtypeconvnode.first_int_to_real: tnode;
  98. {$push}{$j+}
  99. const
  100. intrinfix: array[boolean] of string[7] =
  101. ('uitofp','sitofp');
  102. {$pop}
  103. var
  104. exceptmode: ansistring;
  105. begin
  106. if (llvmflag_constrained_fptoi_itofp in llvmversion_properties[current_settings.llvmversion]) and
  107. { these are converted to 80 bits first in any case }
  108. not(tfloatdef(resultdef).floattype in [s64currency,s64comp]) and
  109. { no actuual int -> floating point conversion }
  110. (torddef(left.resultdef).ordtype<>scurrency) and
  111. ((left.resultdef.size>=resultdef.size) or
  112. ((torddef(left.resultdef).ordtype=u64bit) and
  113. (tfloatdef(resultdef).floattype=s80real))) and
  114. (llvm_constrained_si64tofp_support or
  115. (torddef(left.resultdef).ordtype<>s64bit) or
  116. (tfloatdef(resultdef).floattype<>s64real)) then
  117. begin
  118. { in case rounding may have to be applied, use the intrinsic }
  119. exceptmode:=llvm_constrainedexceptmodestring;
  120. result:=ccallnode.createintern('llvm_experimental_constrained_'+intrinfix[is_signed(left.resultdef)]+llvmfloatintrinsicsuffix(tfloatdef(resultdef))+'_i'+tostr(left.resultdef.size*8),
  121. ccallparanode.create(cstringconstnode.createpchar(ansistring2pchar(exceptmode),length(exceptmode),llvm_metadatatype),
  122. ccallparanode.create(cstringconstnode.createpchar(ansistring2pchar('round.dynamic'),length('round.dynamic'),llvm_metadatatype),
  123. ccallparanode.create(left,nil)
  124. )
  125. )
  126. );
  127. left:=nil;
  128. end
  129. else
  130. begin
  131. expectloc:=LOC_FPUREGISTER;
  132. result:=nil;
  133. end;
  134. end;
  135. function tllvmtypeconvnode.first_int_to_bool: tnode;
  136. begin
  137. result:=inherited;
  138. if not assigned(result) then
  139. begin
  140. if not((nf_explicit in flags) and
  141. not(left.location.loc in [LOC_FLAGS,LOC_JUMP])) then
  142. expectloc:=LOC_JUMP;
  143. end;
  144. end;
  145. function tllvmtypeconvnode.first_nil_to_methodprocvar: tnode;
  146. begin
  147. result:=inherited;
  148. if assigned(result) then
  149. exit;
  150. expectloc:=LOC_REFERENCE;
  151. end;
  152. function tllvmtypeconvnode.first_real_to_real: tnode;
  153. begin
  154. result:=inherited;
  155. if assigned(result) then
  156. exit;
  157. { fptosui always uses round to zero, while we have to use the current
  158. rounding mode when converting from another floating point type to
  159. currency/comp to be compatible with the regular code generators ->
  160. call round() instead }
  161. if (tfloatdef(resultdef).floattype in [s64currency,s64comp]) and
  162. not(tfloatdef(left.resultdef).floattype in [s64currency,s64comp]) and
  163. not(nf_internal in flags) then
  164. begin
  165. result:=ccallnode.createinternfromunit('SYSTEM','ROUND',
  166. ccallparanode.create(left,nil));
  167. left:=nil;
  168. { left was already been multiplied by 10000 by typecheck_real_to_real
  169. -> ensure we don't do that again with the result of round }
  170. result:=ctypeconvnode.create_internal(result,resultdef);
  171. end;
  172. end;
  173. procedure tllvmtypeconvnode.second_pointer_to_array;
  174. var
  175. hreg: tregister;
  176. begin
  177. inherited;
  178. { insert type conversion }
  179. hreg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,cpointerdef.getreusable(resultdef));
  180. hlcg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,tpointerdef(left.resultdef).pointeddef,cpointerdef.getreusable(resultdef),location.reference,hreg);
  181. reference_reset_base(location.reference,hreg,0,location.reference.temppos,location.reference.alignment,location.reference.volatility);
  182. end;
  183. procedure tllvmtypeconvnode.second_int_to_real;
  184. var
  185. op: tllvmop;
  186. llvmtodef: tdef;
  187. begin
  188. if is_signed(left.resultdef) then
  189. op:=la_sitofp
  190. else
  191. op:=la_uitofp;
  192. { see comment about currency in thlcgllvm.a_loadfpu_ref_reg }
  193. if not(tfloatdef(resultdef).floattype in [s64comp,s64currency]) then
  194. llvmtodef:=resultdef
  195. else
  196. llvmtodef:=s80floattype;
  197. location_reset(location,LOC_FPUREGISTER,def_cgsize(llvmtodef));
  198. location.register:=hlcg.getfpuregister(current_asmdata.CurrAsmList,llvmtodef);
  199. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  200. current_asmdata.CurrAsmList.concat(taillvm.op_reg_size_reg_size(op,location.register,left.resultdef,left.location.register,llvmtodef));
  201. end;
  202. procedure tllvmtypeconvnode.second_proc_to_procvar;
  203. begin
  204. inherited;
  205. if not tabstractprocdef(resultdef).is_addressonly and
  206. not tabstractprocdef(left.resultdef).is_addressonly then
  207. begin
  208. if location.loc<>LOC_REFERENCE then
  209. internalerror(2015111902);
  210. hlcg.g_ptrtypecast_ref(current_asmdata.CurrAsmList,
  211. cpointerdef.getreusable(cprocvardef.getreusableprocaddr(tprocdef(left.resultdef),pc_normal)),
  212. cpointerdef.getreusable(resultdef),
  213. location.reference);
  214. end;
  215. end;
  216. procedure tllvmtypeconvnode.second_nil_to_methodprocvar;
  217. var
  218. href: treference;
  219. begin
  220. tg.gethltemp(current_asmdata.CurrAsmList,resultdef,resultdef.size,tt_normal,href);
  221. location_reset_ref(location,LOC_REFERENCE,def_cgsize(resultdef),href.alignment,href.volatility);
  222. location.reference:=href;
  223. hlcg.g_ptrtypecast_ref(current_asmdata.CurrAsmList,cpointerdef.getreusable(resultdef),cpointerdef.getreusable(methodpointertype),href);
  224. hlcg.g_load_const_field_by_name(current_asmdata.CurrAsmList,trecorddef(methodpointertype),0,'proc',href);
  225. hlcg.g_load_const_field_by_name(current_asmdata.CurrAsmList,trecorddef(methodpointertype),0,'self',href);
  226. end;
  227. procedure tllvmtypeconvnode.second_int_to_bool;
  228. var
  229. truelabel,
  230. falselabel: tasmlabel;
  231. newsize : tcgsize;
  232. begin
  233. secondpass(left);
  234. if codegenerror then
  235. exit;
  236. { Explicit typecasts from any ordinal type to a boolean type }
  237. { must not change the ordinal value }
  238. if (nf_explicit in flags) and
  239. not(left.location.loc in [LOC_FLAGS,LOC_JUMP]) then
  240. begin
  241. location_copy(location,left.location);
  242. newsize:=def_cgsize(resultdef);
  243. { change of size? change sign only if location is LOC_(C)REGISTER? Then we have to sign/zero-extend }
  244. if (tcgsize2size[newsize]<>tcgsize2size[left.location.size]) or
  245. ((newsize<>left.location.size) and (location.loc in [LOC_REGISTER,LOC_CREGISTER])) then
  246. hlcg.location_force_reg(current_asmdata.CurrAsmList,location,left.resultdef,resultdef,true)
  247. else
  248. location.size:=newsize;
  249. exit;
  250. end;
  251. case left.location.loc of
  252. LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF,
  253. LOC_CREFERENCE,LOC_REFERENCE,LOC_REGISTER,LOC_CREGISTER:
  254. begin
  255. current_asmdata.getjumplabel(truelabel);
  256. current_asmdata.getjumplabel(falselabel);
  257. location_reset_jump(location,truelabel,falselabel);
  258. hlcg.a_cmp_const_loc_label(current_asmdata.CurrAsmList,left.resultdef,OC_EQ,0,left.location,location.falselabel);
  259. hlcg.a_jmp_always(current_asmdata.CurrAsmList,location.truelabel);
  260. end;
  261. LOC_JUMP :
  262. begin
  263. location:=left.location;
  264. end;
  265. else
  266. internalerror(10062);
  267. end;
  268. end;
  269. procedure tllvmtypeconvnode.second_nothing;
  270. var
  271. hreg: tregister;
  272. begin
  273. { insert LLVM-level type conversions for same-sized entities that are
  274. nevertheless different types }
  275. if (left.resultdef<>resultdef) and
  276. (not(llvmflag_opaque_ptr in llvmversion_properties[current_settings.llvmversion]) or
  277. not(is_address(left.resultdef) and
  278. is_address(resultdef))) then
  279. begin
  280. { handle sometype(voidptr^) and "absolute" }
  281. if not is_void(left.resultdef) and
  282. not(nf_absolute in flags) and
  283. (left.resultdef.typ<>formaldef) and
  284. (resultdef.typ<>formaldef) and
  285. { can't get/check the size of open arrays, and they are allowed to
  286. change sizesduring conversions }
  287. not is_open_array(resultdef) and
  288. not is_open_array(left.resultdef) and
  289. { TP-style child objects can be cast to parent objects, and their
  290. sizes may differ }
  291. (not is_object(resultdef) or
  292. not def_is_related(left.resultdef,resultdef)) and
  293. { in case of ISO-like I/O, the typed file def includes a
  294. get/put buffer of the size of the file's elements }
  295. not(
  296. (m_isolike_io in current_settings.modeswitches) and
  297. (left.resultdef.typ=filedef) and
  298. (tfiledef(left.resultdef).filetyp=ft_typed) and
  299. (resultdef.typ=filedef) and
  300. (tfiledef(resultdef).filetyp in [ft_untyped,ft_typed]) and
  301. (resultdef.size<left.resultdef.size)
  302. ) and
  303. { anything else with different size that ends up here is an error }
  304. (left.resultdef.size<>resultdef.size) then
  305. internalerror(2014012216);
  306. hlcg.location_force_mem(current_asmdata.CurrAsmList,left.location,left.resultdef);
  307. hreg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,cpointerdef.getreusable(resultdef));
  308. hlcg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.resultdef,cpointerdef.getreusable(resultdef),left.location.reference,hreg);
  309. location_reset_ref(location,left.location.loc,def_cgsize(resultdef),left.location.reference.alignment,left.location.reference.volatility);
  310. reference_reset_base(location.reference,hreg,0,location.reference.temppos,location.reference.alignment,location.reference.volatility);
  311. end
  312. else
  313. location_copy(location,left.location);
  314. end;
  315. begin
  316. ctypeconvnode:=tllvmtypeconvnode;
  317. end.