nllvmcnv.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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,verbose,
  57. aasmbase,aasmdata,
  58. llvmbase,aasmllvm,
  59. procinfo,
  60. ncal,
  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. result:=
  67. (fromdef<>todef) and
  68. { two procdefs that are structurally the same but semantically different
  69. still need a conversion }
  70. (
  71. ((fromdef.typ=procvardef) and
  72. (todef.typ=procvardef)) or
  73. { same for two different specialisations }
  74. ((df_specialization in fromdef.defoptions) and
  75. (df_specialization in todef.defoptions)) or
  76. { external objc classes referring to the same type }
  77. (is_objc_class_or_protocol(fromdef) and
  78. is_objc_class_or_protocol(todef)) or
  79. { typed from/to untyped filedef in ISO mode: have to keep because of
  80. the get/put buffer }
  81. ((fromdef.typ=filedef) and
  82. (tfiledef(fromdef).filetyp=ft_typed) and
  83. (todef.typ=filedef) and
  84. (tfiledef(todef).filetyp=ft_typed) and
  85. (not equal_defs(tfiledef(fromdef).typedfiledef, tfiledef(todef).typedfiledef) or
  86. target_specific_need_equal_typeconv(tfiledef(fromdef).typedfiledef, tfiledef(todef).typedfiledef))
  87. )
  88. );
  89. end;
  90. function tllvmtypeconvnode.first_int_to_real: tnode;
  91. begin
  92. expectloc:=LOC_FPUREGISTER;
  93. result:=nil;
  94. end;
  95. function tllvmtypeconvnode.first_int_to_bool: tnode;
  96. begin
  97. result:=inherited;
  98. if not assigned(result) then
  99. begin
  100. if not((nf_explicit in flags) and
  101. not(left.location.loc in [LOC_FLAGS,LOC_JUMP])) then
  102. expectloc:=LOC_JUMP;
  103. end;
  104. end;
  105. function tllvmtypeconvnode.first_nil_to_methodprocvar: tnode;
  106. begin
  107. result:=inherited;
  108. if assigned(result) then
  109. exit;
  110. expectloc:=LOC_REFERENCE;
  111. end;
  112. function tllvmtypeconvnode.first_real_to_real: tnode;
  113. begin
  114. result:=inherited;
  115. if assigned(result) then
  116. exit;
  117. { fptosui always uses round to zero, while we have to use the current
  118. rounding mode when converting from another floating point type to
  119. currency/comp to be compatible with the regular code generators ->
  120. call round() instead }
  121. if (tfloatdef(resultdef).floattype in [s64currency,s64comp]) and
  122. not(tfloatdef(left.resultdef).floattype in [s64currency,s64comp]) then
  123. begin
  124. result:=ccallnode.createinternfromunit('SYSTEM','ROUND',
  125. ccallparanode.create(left,nil));
  126. left:=nil;
  127. { left was already been multiplied by 10000 by typecheck_real_to_real
  128. -> ensure we don't do that again with the result of round }
  129. result:=ctypeconvnode.create_internal(result,resultdef);
  130. end;
  131. end;
  132. procedure tllvmtypeconvnode.second_pointer_to_array;
  133. var
  134. hreg: tregister;
  135. begin
  136. inherited;
  137. { insert type conversion }
  138. hreg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,cpointerdef.getreusable(resultdef));
  139. hlcg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,tpointerdef(left.resultdef).pointeddef,cpointerdef.getreusable(resultdef),location.reference,hreg);
  140. reference_reset_base(location.reference,hreg,0,location.reference.temppos,location.reference.alignment,location.reference.volatility);
  141. end;
  142. procedure tllvmtypeconvnode.second_int_to_real;
  143. var
  144. op: tllvmop;
  145. llvmtodef: tdef;
  146. begin
  147. if is_signed(left.resultdef) then
  148. op:=la_sitofp
  149. else
  150. op:=la_uitofp;
  151. { see comment about currency in thlcgllvm.a_loadfpu_ref_reg }
  152. if not(tfloatdef(resultdef).floattype in [s64comp,s64currency]) then
  153. llvmtodef:=resultdef
  154. else
  155. llvmtodef:=s80floattype;
  156. location_reset(location,LOC_FPUREGISTER,def_cgsize(llvmtodef));
  157. location.register:=hlcg.getfpuregister(current_asmdata.CurrAsmList,llvmtodef);
  158. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  159. current_asmdata.CurrAsmList.concat(taillvm.op_reg_size_reg_size(op,location.register,left.resultdef,left.location.register,llvmtodef));
  160. end;
  161. procedure tllvmtypeconvnode.second_proc_to_procvar;
  162. begin
  163. inherited;
  164. if not tabstractprocdef(resultdef).is_addressonly and
  165. not tabstractprocdef(left.resultdef).is_addressonly then
  166. begin
  167. if location.loc<>LOC_REFERENCE then
  168. internalerror(2015111902);
  169. hlcg.g_ptrtypecast_ref(current_asmdata.CurrAsmList,
  170. cpointerdef.getreusable(tprocdef(left.resultdef).getcopyas(procvardef,pc_normal,'')),
  171. cpointerdef.getreusable(resultdef),
  172. location.reference);
  173. end;
  174. end;
  175. procedure tllvmtypeconvnode.second_nil_to_methodprocvar;
  176. var
  177. href: treference;
  178. begin
  179. tg.gethltemp(current_asmdata.CurrAsmList,resultdef,resultdef.size,tt_normal,href);
  180. location_reset_ref(location,LOC_REFERENCE,def_cgsize(resultdef),href.alignment,href.volatility);
  181. location.reference:=href;
  182. hlcg.g_ptrtypecast_ref(current_asmdata.CurrAsmList,cpointerdef.getreusable(resultdef),cpointerdef.getreusable(methodpointertype),href);
  183. hlcg.g_load_const_field_by_name(current_asmdata.CurrAsmList,trecorddef(methodpointertype),0,'proc',href);
  184. hlcg.g_load_const_field_by_name(current_asmdata.CurrAsmList,trecorddef(methodpointertype),0,'self',href);
  185. end;
  186. procedure tllvmtypeconvnode.second_int_to_bool;
  187. var
  188. truelabel,
  189. falselabel: tasmlabel;
  190. newsize : tcgsize;
  191. begin
  192. secondpass(left);
  193. if codegenerror then
  194. exit;
  195. { Explicit typecasts from any ordinal type to a boolean type }
  196. { must not change the ordinal value }
  197. if (nf_explicit in flags) and
  198. not(left.location.loc in [LOC_FLAGS,LOC_JUMP]) then
  199. begin
  200. location_copy(location,left.location);
  201. newsize:=def_cgsize(resultdef);
  202. { change of size? change sign only if location is LOC_(C)REGISTER? Then we have to sign/zero-extend }
  203. if (tcgsize2size[newsize]<>tcgsize2size[left.location.size]) or
  204. ((newsize<>left.location.size) and (location.loc in [LOC_REGISTER,LOC_CREGISTER])) then
  205. hlcg.location_force_reg(current_asmdata.CurrAsmList,location,left.resultdef,resultdef,true)
  206. else
  207. location.size:=newsize;
  208. exit;
  209. end;
  210. case left.location.loc of
  211. LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF,
  212. LOC_CREFERENCE,LOC_REFERENCE,LOC_REGISTER,LOC_CREGISTER:
  213. begin
  214. current_asmdata.getjumplabel(truelabel);
  215. current_asmdata.getjumplabel(falselabel);
  216. location_reset_jump(location,truelabel,falselabel);
  217. hlcg.a_cmp_const_loc_label(current_asmdata.CurrAsmList,left.resultdef,OC_EQ,0,left.location,location.falselabel);
  218. hlcg.a_jmp_always(current_asmdata.CurrAsmList,location.truelabel);
  219. end;
  220. LOC_JUMP :
  221. begin
  222. location:=left.location;
  223. end;
  224. else
  225. internalerror(10062);
  226. end;
  227. end;
  228. procedure tllvmtypeconvnode.second_nothing;
  229. var
  230. hreg: tregister;
  231. begin
  232. { insert LLVM-level type conversions for same-sized entities that are
  233. nevertheless different types }
  234. if left.resultdef<>resultdef then
  235. begin
  236. { handle sometype(voidptr^) and "absolute" }
  237. if not is_void(left.resultdef) and
  238. not(nf_absolute in flags) and
  239. (left.resultdef.typ<>formaldef) and
  240. (resultdef.typ<>formaldef) and
  241. { can't get/check the size of open arrays, and they are allowed to
  242. change sizesduring conversions }
  243. not is_open_array(resultdef) and
  244. not is_open_array(left.resultdef) and
  245. { TP-style child objects can be cast to parent objects, and their
  246. sizes may differ }
  247. (not is_object(resultdef) or
  248. not def_is_related(left.resultdef,resultdef)) and
  249. { in case of ISO-like I/O, the typed file def includes a
  250. get/put buffer of the size of the file's elements }
  251. not(
  252. (m_isolike_io in current_settings.modeswitches) and
  253. (left.resultdef.typ=filedef) and
  254. (tfiledef(left.resultdef).filetyp=ft_typed) and
  255. (resultdef.typ=filedef) and
  256. (tfiledef(resultdef).filetyp in [ft_untyped,ft_typed]) and
  257. (resultdef.size<left.resultdef.size)
  258. ) and
  259. { anything else with different size that ends up here is an error }
  260. (left.resultdef.size<>resultdef.size) then
  261. internalerror(2014012216);
  262. hlcg.location_force_mem(current_asmdata.CurrAsmList,left.location,left.resultdef);
  263. hreg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,cpointerdef.getreusable(resultdef));
  264. hlcg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.resultdef,cpointerdef.getreusable(resultdef),left.location.reference,hreg);
  265. location_reset_ref(location,left.location.loc,def_cgsize(resultdef),left.location.reference.alignment,left.location.reference.volatility);
  266. reference_reset_base(location.reference,hreg,0,location.reference.temppos,location.reference.alignment,location.reference.volatility);
  267. end
  268. else
  269. location_copy(location,left.location);
  270. end;
  271. begin
  272. ctypeconvnode:=tllvmtypeconvnode;
  273. end.