narmcnv.pas 17 KB

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