narmcnv.pas 17 KB

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