narmcnv.pas 17 KB

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