narmcnv.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate ARM assembler 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 narmcnv;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,ncnv,ncgcnv;
  22. type
  23. tarmtypeconvnode = class(tcgtypeconvnode)
  24. protected
  25. function first_int_to_real: tnode;override;
  26. function first_real_to_real: tnode; override;
  27. { procedure second_int_to_int;override; }
  28. { procedure second_string_to_string;override; }
  29. { procedure second_cstring_to_pchar;override; }
  30. { procedure second_string_to_chararray;override; }
  31. { procedure second_array_to_pointer;override; }
  32. // function first_int_to_real: tnode; override;
  33. { procedure second_pointer_to_array;override; }
  34. { procedure second_chararray_to_string;override; }
  35. { procedure second_char_to_string;override; }
  36. procedure second_int_to_real;override;
  37. // procedure second_real_to_real;override;
  38. { procedure second_cord_to_pointer;override; }
  39. { procedure second_proc_to_procvar;override; }
  40. { procedure second_bool_to_int;override; }
  41. procedure second_int_to_bool;override;
  42. { procedure second_load_smallset;override; }
  43. { procedure second_ansistring_to_pchar;override; }
  44. { procedure second_pchar_to_string;override; }
  45. { procedure second_class_to_intf;override; }
  46. { procedure second_char_to_char;override; }
  47. end;
  48. implementation
  49. uses
  50. verbose,globtype,globals,symdef,aasmbase,aasmtai,aasmdata,symtable,
  51. defutil,
  52. cgbase,cgutils,
  53. pass_1,pass_2,procinfo,ncal,
  54. ncgutil,
  55. cpubase,cpuinfo,aasmcpu,cgobj,hlcgobj,cgcpu;
  56. {*****************************************************************************
  57. FirstTypeConv
  58. *****************************************************************************}
  59. function tarmtypeconvnode.first_int_to_real: tnode;
  60. var
  61. fname: string[19];
  62. begin
  63. if (cs_fp_emulation in current_settings.moduleswitches) or
  64. {$ifdef cpufpemu}
  65. (current_settings.fputype=fpu_soft) or
  66. {$endif cpufpemu}
  67. (FPUARM_HAS_VFP_SINGLE_ONLY in fpu_capabilities[current_settings.fputype]) then
  68. result:=inherited first_int_to_real
  69. else
  70. begin
  71. { converting a 64bit integer to a float requires a helper }
  72. if is_64bitint(left.resultdef) or
  73. is_currency(left.resultdef) then
  74. begin
  75. { hack to avoid double division by 10000, as it's
  76. already done by typecheckpass.resultdef_int_to_real }
  77. if is_currency(left.resultdef) then
  78. left.resultdef := s64inttype;
  79. if is_signed(left.resultdef) then
  80. fname := 'fpc_int64_to_double'
  81. else
  82. fname := 'fpc_qword_to_double';
  83. result := ccallnode.createintern(fname,ccallparanode.create(
  84. left,nil));
  85. left:=nil;
  86. if (tfloatdef(resultdef).floattype=s32real) then
  87. inserttypeconv(result,s32floattype);
  88. firstpass(result);
  89. exit;
  90. end
  91. else
  92. { other integers are supposed to be 32 bit }
  93. begin
  94. if is_signed(left.resultdef) then
  95. inserttypeconv(left,s32inttype)
  96. else
  97. inserttypeconv(left,u32inttype);
  98. firstpass(left);
  99. end;
  100. result := nil;
  101. case current_settings.fputype of
  102. fpu_fpa,
  103. fpu_fpa10,
  104. fpu_fpa11:
  105. expectloc:=LOC_FPUREGISTER;
  106. fpu_vfp_first..fpu_vfp_last:
  107. expectloc:=LOC_MMREGISTER;
  108. else
  109. internalerror(2009112702);
  110. end;
  111. end;
  112. end;
  113. function tarmtypeconvnode.first_real_to_real: tnode;
  114. begin
  115. if FPUARM_HAS_VFP_SINGLE_ONLY in fpu_capabilities[current_settings.fputype] then
  116. begin
  117. case tfloatdef(left.resultdef).floattype of
  118. s32real:
  119. case tfloatdef(resultdef).floattype of
  120. s64real:
  121. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float32_to_float64',ccallparanode.create(
  122. ctypeconvnode.create_internal(left,search_system_type('FLOAT32REC').typedef),nil)),resultdef);
  123. s32real:
  124. begin
  125. result:=left;
  126. left:=nil;
  127. end;
  128. else
  129. internalerror(200610151);
  130. end;
  131. s64real:
  132. case tfloatdef(resultdef).floattype of
  133. s32real:
  134. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float64_to_float32',ccallparanode.create(
  135. ctypeconvnode.create_internal(left,search_system_type('FLOAT64').typedef),nil)),resultdef);
  136. s64real:
  137. begin
  138. result:=left;
  139. left:=nil;
  140. end;
  141. else
  142. internalerror(200610152);
  143. end;
  144. else
  145. internalerror(200610153);
  146. end;
  147. left:=nil;
  148. firstpass(result);
  149. exit;
  150. end
  151. else
  152. Result := inherited first_real_to_real;
  153. end;
  154. procedure tarmtypeconvnode.second_int_to_real;
  155. const
  156. signedprec2vfppf: array[boolean,OS_F32..OS_F64] of toppostfix =
  157. ((PF_F32U32,PF_F64U32),
  158. (PF_F32S32,PF_F64S32));
  159. var
  160. instr : taicpu;
  161. href : treference;
  162. l1,l2 : tasmlabel;
  163. hregister : tregister;
  164. signed : boolean;
  165. begin
  166. case current_settings.fputype of
  167. fpu_fpa,
  168. fpu_fpa10,
  169. fpu_fpa11:
  170. begin
  171. { convert first to double to avoid precision loss }
  172. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  173. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,u32inttype,true);
  174. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  175. instr:=taicpu.op_reg_reg(A_FLT,location.register,left.location.register);
  176. if is_signed(left.resultdef) then
  177. begin
  178. instr.oppostfix:=cgsize2fpuoppostfix[def_cgsize(resultdef)];
  179. current_asmdata.CurrAsmList.concat(instr);
  180. end
  181. else
  182. begin
  183. { flt does a signed load, fix this }
  184. case tfloatdef(resultdef).floattype of
  185. s32real,
  186. s64real:
  187. begin
  188. { converting dword to s64real first and cut off at the end avoids precision loss }
  189. instr.oppostfix:=PF_D;
  190. current_asmdata.CurrAsmList.concat(instr);
  191. current_asmdata.getglobaldatalabel(l1);
  192. current_asmdata.getjumplabel(l2);
  193. reference_reset_symbol(href,l1,0,const_align(8),[]);
  194. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  195. current_asmdata.CurrAsmList.concat(Taicpu.op_reg_const(A_CMP,left.location.register,0));
  196. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_GE,l2);
  197. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  198. hregister:=cg.getfpuregister(current_asmdata.CurrAsmList,OS_F64);
  199. new_section(current_asmdata.asmlists[al_typedconsts],sec_rodata_norel,l1.name,const_align(8));
  200. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(l1));
  201. { I got this constant from a test program (FK) }
  202. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_32bit($41f00000));
  203. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_32bit(0));
  204. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList,OS_F64,OS_F64,href,hregister);
  205. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADF,location.register,hregister,location.register),PF_D));
  206. cg.a_label(current_asmdata.CurrAsmList,l2);
  207. { cut off if we should convert to single }
  208. if tfloatdef(resultdef).floattype=s32real then
  209. begin
  210. hregister:=location.register;
  211. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  212. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  213. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_MVF,location.register,hregister),PF_S));
  214. end;
  215. end;
  216. else
  217. internalerror(200410031);
  218. end;
  219. end;
  220. end;
  221. else if FPUARM_HAS_VFP_DOUBLE in fpu_capabilities[current_settings.fputype] then
  222. begin
  223. location_reset(location,LOC_MMREGISTER,def_cgsize(resultdef));
  224. signed:=left.location.size=OS_S32;
  225. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,false);
  226. if (left.location.size<>OS_F32) then
  227. internalerror(2009112703);
  228. if left.location.size<>location.size then
  229. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size)
  230. else
  231. location.register:=left.location.register;
  232. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_VCVT,
  233. location.register,left.location.register),
  234. signedprec2vfppf[signed,location.size]));
  235. end
  236. else if FPUARM_HAS_VFP_SINGLE_ONLY in fpu_capabilities[current_settings.fputype] then
  237. begin
  238. location_reset(location,LOC_MMREGISTER,def_cgsize(resultdef));
  239. signed:=left.location.size=OS_S32;
  240. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,false);
  241. if (left.location.size<>OS_F32) then
  242. internalerror(2009112703);
  243. if left.location.size<>location.size then
  244. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size)
  245. else
  246. location.register:=left.location.register;
  247. if signed then
  248. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_VCVT,location.register,left.location.register), PF_F32S32))
  249. else
  250. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_VCVT,location.register,left.location.register), PF_F32U32));
  251. end
  252. else
  253. { should be handled in pass 1 }
  254. internalerror(2019050934);
  255. end;
  256. end;
  257. procedure tarmtypeconvnode.second_int_to_bool;
  258. var
  259. hreg1,
  260. hregister : tregister;
  261. href : treference;
  262. resflags : tresflags;
  263. hlabel : tasmlabel;
  264. newsize : tcgsize;
  265. begin
  266. secondpass(left);
  267. if codegenerror then
  268. exit;
  269. { Explicit typecasts from any ordinal type to a boolean type }
  270. { must not change the ordinal value }
  271. if (nf_explicit in flags) and
  272. not(left.location.loc in [LOC_FLAGS,LOC_JUMP]) then
  273. begin
  274. location_copy(location,left.location);
  275. newsize:=def_cgsize(resultdef);
  276. { change of size? change sign only if location is LOC_(C)REGISTER? Then we have to sign/zero-extend }
  277. if (tcgsize2size[newsize]<>tcgsize2size[left.location.size]) or
  278. ((newsize<>left.location.size) and (location.loc in [LOC_REGISTER,LOC_CREGISTER])) then
  279. hlcg.location_force_reg(current_asmdata.CurrAsmList,location,left.resultdef,resultdef,true)
  280. else
  281. location.size:=newsize;
  282. exit;
  283. end;
  284. { Load left node into flag F_NE/F_E }
  285. resflags:=F_NE;
  286. if (left.location.loc in [LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF]) then
  287. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  288. case left.location.loc of
  289. LOC_CREFERENCE,
  290. LOC_REFERENCE :
  291. begin
  292. if left.location.size in [OS_64,OS_S64] then
  293. begin
  294. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  295. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.reference,hregister);
  296. href:=left.location.reference;
  297. inc(href.offset,4);
  298. tbasecgarm(cg).cgsetflags:=true;
  299. cg.a_op_ref_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,href,hregister);
  300. tbasecgarm(cg).cgsetflags:=false;
  301. end
  302. else
  303. begin
  304. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  305. tbasecgarm(cg).cgsetflags:=true;
  306. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,left.location.size,left.location.register,left.location.register);
  307. tbasecgarm(cg).cgsetflags:=false;
  308. end;
  309. end;
  310. LOC_FLAGS :
  311. begin
  312. resflags:=left.location.resflags;
  313. end;
  314. LOC_REGISTER,LOC_CREGISTER :
  315. begin
  316. if left.location.size in [OS_64,OS_S64] then
  317. begin
  318. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  319. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.register64.reglo,hregister);
  320. tbasecgarm(cg).cgsetflags:=true;
  321. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,left.location.register64.reghi,hregister);
  322. tbasecgarm(cg).cgsetflags:=false;
  323. end
  324. else
  325. begin
  326. tbasecgarm(cg).cgsetflags:=true;
  327. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,left.location.size,left.location.register,left.location.register);
  328. tbasecgarm(cg).cgsetflags:=false;
  329. end;
  330. end;
  331. LOC_JUMP :
  332. begin
  333. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  334. current_asmdata.getjumplabel(hlabel);
  335. cg.a_label(current_asmdata.CurrAsmList,left.location.truelabel);
  336. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,1,hregister);
  337. cg.a_jmp_always(current_asmdata.CurrAsmList,hlabel);
  338. cg.a_label(current_asmdata.CurrAsmList,left.location.falselabel);
  339. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,0,hregister);
  340. cg.a_label(current_asmdata.CurrAsmList,hlabel);
  341. tbasecgarm(cg).cgsetflags:=true;
  342. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_INT,hregister,hregister);
  343. tbasecgarm(cg).cgsetflags:=false;
  344. end;
  345. else
  346. internalerror(200311301);
  347. end;
  348. { load flags to register }
  349. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  350. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  351. cg.g_flags2reg(current_asmdata.CurrAsmList,location.size,resflags,hreg1);
  352. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  353. if (is_cbool(resultdef)) then
  354. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,location.size,hreg1,hreg1);
  355. {$ifndef cpu64bitalu}
  356. if (location.size in [OS_64,OS_S64]) then
  357. begin
  358. location.register64.reglo:=hreg1;
  359. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  360. if (is_cbool(resultdef)) then
  361. { reglo is either 0 or -1 -> reghi has to become the same }
  362. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,location.register64.reglo,location.register64.reghi)
  363. else
  364. { unsigned }
  365. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,location.register64.reghi);
  366. end
  367. else
  368. {$endif cpu64bitalu}
  369. location.register:=hreg1;
  370. end;
  371. begin
  372. ctypeconvnode:=tarmtypeconvnode;
  373. end.