nllvmcnv.pas 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. { procedure second_int_to_int;override; }
  32. { procedure second_string_to_string;override; }
  33. { procedure second_cstring_to_pchar;override; }
  34. { procedure second_string_to_chararray;override; }
  35. { procedure second_array_to_pointer;override; }
  36. procedure second_pointer_to_array;override;
  37. { procedure second_chararray_to_string;override; }
  38. { procedure second_char_to_string;override; }
  39. procedure second_int_to_real;override;
  40. { procedure second_real_to_real;override; }
  41. { procedure second_cord_to_pointer;override; }
  42. procedure second_proc_to_procvar;override;
  43. procedure second_nil_to_methodprocvar; override;
  44. { procedure second_bool_to_int;override; }
  45. procedure second_int_to_bool;override;
  46. { procedure second_load_smallset;override; }
  47. { procedure second_ansistring_to_pchar;override; }
  48. { procedure second_pchar_to_string;override; }
  49. { procedure second_class_to_intf;override; }
  50. { procedure second_char_to_char;override; }
  51. procedure second_nothing; override;
  52. end;
  53. implementation
  54. uses
  55. globtype,globals,verbose,
  56. aasmbase,aasmdata,
  57. llvmbase,aasmllvm,
  58. procinfo,
  59. symconst,symdef,defutil,
  60. cgbase,cgutils,tgobj,hlcgobj,pass_2;
  61. { tllvmtypeconvnode }
  62. class function tllvmtypeconvnode.target_specific_need_equal_typeconv(fromdef, todef: tdef): boolean;
  63. begin
  64. result:=
  65. (fromdef<>todef) and
  66. { two procdefs that are structurally the same but semantically different
  67. still need a convertion }
  68. (
  69. ((fromdef.typ=procvardef) and
  70. (todef.typ=procvardef))
  71. );
  72. end;
  73. function tllvmtypeconvnode.first_int_to_real: tnode;
  74. begin
  75. expectloc:=LOC_FPUREGISTER;
  76. result:=nil;
  77. end;
  78. function tllvmtypeconvnode.first_int_to_bool: tnode;
  79. begin
  80. result:=inherited;
  81. if not assigned(result) then
  82. begin
  83. if not((nf_explicit in flags) and
  84. not(left.location.loc in [LOC_FLAGS,LOC_JUMP])) then
  85. expectloc:=LOC_JUMP;
  86. end;
  87. end;
  88. function tllvmtypeconvnode.first_nil_to_methodprocvar: tnode;
  89. begin
  90. result:=inherited;
  91. if assigned(result) then
  92. exit;
  93. expectloc:=LOC_REFERENCE;
  94. end;
  95. procedure tllvmtypeconvnode.second_pointer_to_array;
  96. var
  97. hreg: tregister;
  98. begin
  99. inherited;
  100. { insert type conversion }
  101. hreg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,cpointerdef.getreusable(resultdef));
  102. hlcg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,tpointerdef(left.resultdef).pointeddef,cpointerdef.getreusable(resultdef),location.reference,hreg);
  103. reference_reset_base(location.reference,hreg,0,location.reference.alignment,location.reference.volatility);
  104. end;
  105. procedure tllvmtypeconvnode.second_int_to_real;
  106. var
  107. op: tllvmop;
  108. llvmtodef: tdef;
  109. begin
  110. if is_signed(left.resultdef) then
  111. op:=la_sitofp
  112. else
  113. op:=la_uitofp;
  114. { see comment about currency in thlcgllvm.a_loadfpu_ref_reg }
  115. if not(tfloatdef(resultdef).floattype in [s64comp,s64currency]) then
  116. llvmtodef:=resultdef
  117. else
  118. llvmtodef:=s80floattype;
  119. location_reset(location,LOC_FPUREGISTER,def_cgsize(llvmtodef));
  120. location.register:=hlcg.getfpuregister(current_asmdata.CurrAsmList,llvmtodef);
  121. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  122. current_asmdata.CurrAsmList.concat(taillvm.op_reg_size_reg_size(op,location.register,left.resultdef,left.location.register,llvmtodef));
  123. end;
  124. procedure tllvmtypeconvnode.second_proc_to_procvar;
  125. begin
  126. inherited;
  127. if not tabstractprocdef(resultdef).is_addressonly and
  128. not tabstractprocdef(left.resultdef).is_addressonly then
  129. begin
  130. if location.loc<>LOC_REFERENCE then
  131. internalerror(2015111902);
  132. hlcg.g_ptrtypecast_ref(current_asmdata.CurrAsmList,
  133. cpointerdef.getreusable(tprocdef(left.resultdef).getcopyas(procvardef,pc_normal)),
  134. cpointerdef.getreusable(resultdef),
  135. location.reference);
  136. end;
  137. end;
  138. procedure tllvmtypeconvnode.second_nil_to_methodprocvar;
  139. var
  140. href: treference;
  141. begin
  142. tg.gethltemp(current_asmdata.CurrAsmList,resultdef,resultdef.size,tt_normal,href);
  143. location_reset_ref(location,LOC_REFERENCE,def_cgsize(resultdef),href.alignment,href.volatility);
  144. location.reference:=href;
  145. hlcg.g_ptrtypecast_ref(current_asmdata.CurrAsmList,cpointerdef.getreusable(resultdef),cpointerdef.getreusable(methodpointertype),href);
  146. hlcg.g_load_const_field_by_name(current_asmdata.CurrAsmList,trecorddef(methodpointertype),0,'proc',href);
  147. hlcg.g_load_const_field_by_name(current_asmdata.CurrAsmList,trecorddef(methodpointertype),0,'self',href);
  148. end;
  149. procedure tllvmtypeconvnode.second_int_to_bool;
  150. var
  151. truelabel,
  152. falselabel: tasmlabel;
  153. newsize : tcgsize;
  154. begin
  155. secondpass(left);
  156. if codegenerror then
  157. exit;
  158. { Explicit typecasts from any ordinal type to a boolean type }
  159. { must not change the ordinal value }
  160. if (nf_explicit in flags) and
  161. not(left.location.loc in [LOC_FLAGS,LOC_JUMP]) then
  162. begin
  163. location_copy(location,left.location);
  164. newsize:=def_cgsize(resultdef);
  165. { change of size? change sign only if location is LOC_(C)REGISTER? Then we have to sign/zero-extend }
  166. if (tcgsize2size[newsize]<>tcgsize2size[left.location.size]) or
  167. ((newsize<>left.location.size) and (location.loc in [LOC_REGISTER,LOC_CREGISTER])) then
  168. hlcg.location_force_reg(current_asmdata.CurrAsmList,location,left.resultdef,resultdef,true)
  169. else
  170. location.size:=newsize;
  171. exit;
  172. end;
  173. case left.location.loc of
  174. LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF,
  175. LOC_CREFERENCE,LOC_REFERENCE,LOC_REGISTER,LOC_CREGISTER:
  176. begin
  177. current_asmdata.getjumplabel(truelabel);
  178. current_asmdata.getjumplabel(falselabel);
  179. location_reset_jump(location,truelabel,falselabel);
  180. hlcg.a_cmp_const_loc_label(current_asmdata.CurrAsmList,left.resultdef,OC_EQ,0,left.location,location.falselabel);
  181. hlcg.a_jmp_always(current_asmdata.CurrAsmList,location.truelabel);
  182. end;
  183. LOC_JUMP :
  184. begin
  185. location:=left.location;
  186. end;
  187. else
  188. internalerror(10062);
  189. end;
  190. end;
  191. procedure tllvmtypeconvnode.second_nothing;
  192. var
  193. hreg: tregister;
  194. begin
  195. if left.resultdef<>resultdef then
  196. begin
  197. { handle sometype(voidptr^) and "absolute" }
  198. if not is_void(left.resultdef) and
  199. not(nf_absolute in flags) and
  200. (left.resultdef.typ<>formaldef) and
  201. (resultdef.typ<>formaldef) and
  202. not is_open_array(resultdef) and
  203. not is_open_array(left.resultdef) and
  204. (left.resultdef.size<>resultdef.size) and
  205. { in case of ISO-like I/O, the typed file def includes a
  206. get/put buffer of the size of the file's elements }
  207. not(
  208. (m_isolike_io in current_settings.modeswitches) and
  209. (left.resultdef.typ=filedef) and
  210. (tfiledef(left.resultdef).filetyp=ft_typed) and
  211. (resultdef.typ=filedef) and
  212. (tfiledef(resultdef).filetyp=ft_untyped)
  213. ) then
  214. internalerror(2014012216);
  215. hlcg.location_force_mem(current_asmdata.CurrAsmList,left.location,left.resultdef);
  216. hreg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,cpointerdef.getreusable(resultdef));
  217. hlcg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.resultdef,cpointerdef.getreusable(resultdef),left.location.reference,hreg);
  218. location_reset_ref(location,left.location.loc,left.location.size,left.location.reference.alignment,left.location.reference.volatility);
  219. reference_reset_base(location.reference,hreg,0,location.reference.alignment,location.reference.volatility);
  220. end
  221. else
  222. location_copy(location,left.location);
  223. end;
  224. begin
  225. ctypeconvnode:=tllvmtypeconvnode;
  226. end.