narmcnv.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. (current_settings.fputype=fpu_fpv4_s16) then
  65. result:=inherited first_int_to_real
  66. else
  67. begin
  68. { converting a 64bit integer to a float requires a helper }
  69. if is_64bitint(left.resultdef) or
  70. is_currency(left.resultdef) then
  71. begin
  72. { hack to avoid double division by 10000, as it's
  73. already done by typecheckpass.resultdef_int_to_real }
  74. if is_currency(left.resultdef) then
  75. left.resultdef := s64inttype;
  76. if is_signed(left.resultdef) then
  77. fname := 'fpc_int64_to_double'
  78. else
  79. fname := 'fpc_qword_to_double';
  80. result := ccallnode.createintern(fname,ccallparanode.create(
  81. left,nil));
  82. left:=nil;
  83. if (tfloatdef(resultdef).floattype=s32real) then
  84. inserttypeconv(result,s32floattype);
  85. firstpass(result);
  86. exit;
  87. end
  88. else
  89. { other integers are supposed to be 32 bit }
  90. begin
  91. if is_signed(left.resultdef) then
  92. inserttypeconv(left,s32inttype)
  93. else
  94. inserttypeconv(left,u32inttype);
  95. firstpass(left);
  96. end;
  97. result := nil;
  98. case current_settings.fputype of
  99. fpu_fpa,
  100. fpu_fpa10,
  101. fpu_fpa11:
  102. expectloc:=LOC_FPUREGISTER;
  103. fpu_vfpv2,
  104. fpu_vfpv3,
  105. fpu_vfpv3_d16,
  106. fpu_fpv4_s16:
  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 (current_settings.fputype=fpu_fpv4_s16) 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. fpu_vfpv2,
  222. fpu_vfpv3,
  223. fpu_vfpv3_d16:
  224. begin
  225. location_reset(location,LOC_MMREGISTER,def_cgsize(resultdef));
  226. signed:=left.location.size=OS_S32;
  227. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,false);
  228. if (left.location.size<>OS_F32) then
  229. internalerror(2009112703);
  230. if left.location.size<>location.size then
  231. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size)
  232. else
  233. location.register:=left.location.register;
  234. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_VCVT,
  235. location.register,left.location.register),
  236. signedprec2vfppf[signed,location.size]));
  237. end;
  238. fpu_fpv4_s16:
  239. begin
  240. location_reset(location,LOC_MMREGISTER,def_cgsize(resultdef));
  241. signed:=left.location.size=OS_S32;
  242. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,false);
  243. if (left.location.size<>OS_F32) then
  244. internalerror(2009112703);
  245. if left.location.size<>location.size then
  246. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size)
  247. else
  248. location.register:=left.location.register;
  249. if signed then
  250. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_VCVT,location.register,left.location.register), PF_F32S32))
  251. else
  252. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_VCVT,location.register,left.location.register), PF_F32U32));
  253. end;
  254. end;
  255. end;
  256. procedure tarmtypeconvnode.second_int_to_bool;
  257. var
  258. hreg1,
  259. hregister : tregister;
  260. href : treference;
  261. resflags : tresflags;
  262. hlabel,oldTrueLabel,oldFalseLabel : tasmlabel;
  263. newsize : tcgsize;
  264. begin
  265. oldTrueLabel:=current_procinfo.CurrTrueLabel;
  266. oldFalseLabel:=current_procinfo.CurrFalseLabel;
  267. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  268. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  269. secondpass(left);
  270. if codegenerror then
  271. exit;
  272. { Explicit typecasts from any ordinal type to a boolean type }
  273. { must not change the ordinal value }
  274. if (nf_explicit in flags) and
  275. not(left.location.loc in [LOC_FLAGS,LOC_JUMP]) then
  276. begin
  277. location_copy(location,left.location);
  278. newsize:=def_cgsize(resultdef);
  279. { change of size? change sign only if location is LOC_(C)REGISTER? Then we have to sign/zero-extend }
  280. if (tcgsize2size[newsize]<>tcgsize2size[left.location.size]) or
  281. ((newsize<>left.location.size) and (location.loc in [LOC_REGISTER,LOC_CREGISTER])) then
  282. hlcg.location_force_reg(current_asmdata.CurrAsmList,location,left.resultdef,resultdef,true)
  283. else
  284. location.size:=newsize;
  285. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  286. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  287. exit;
  288. end;
  289. { Load left node into flag F_NE/F_E }
  290. resflags:=F_NE;
  291. if (left.location.loc in [LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF]) then
  292. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  293. case left.location.loc of
  294. LOC_CREFERENCE,
  295. LOC_REFERENCE :
  296. begin
  297. if left.location.size in [OS_64,OS_S64] then
  298. begin
  299. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  300. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.reference,hregister);
  301. href:=left.location.reference;
  302. inc(href.offset,4);
  303. tbasecgarm(cg).cgsetflags:=true;
  304. cg.a_op_ref_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,href,hregister);
  305. tbasecgarm(cg).cgsetflags:=false;
  306. end
  307. else
  308. begin
  309. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  310. tbasecgarm(cg).cgsetflags:=true;
  311. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,left.location.size,left.location.register,left.location.register);
  312. tbasecgarm(cg).cgsetflags:=false;
  313. end;
  314. end;
  315. LOC_FLAGS :
  316. begin
  317. resflags:=left.location.resflags;
  318. end;
  319. LOC_REGISTER,LOC_CREGISTER :
  320. begin
  321. if left.location.size in [OS_64,OS_S64] then
  322. begin
  323. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  324. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.register64.reglo,hregister);
  325. tbasecgarm(cg).cgsetflags:=true;
  326. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,left.location.register64.reghi,hregister);
  327. tbasecgarm(cg).cgsetflags:=false;
  328. end
  329. else
  330. begin
  331. tbasecgarm(cg).cgsetflags:=true;
  332. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,left.location.size,left.location.register,left.location.register);
  333. tbasecgarm(cg).cgsetflags:=false;
  334. end;
  335. end;
  336. LOC_JUMP :
  337. begin
  338. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  339. current_asmdata.getjumplabel(hlabel);
  340. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  341. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,1,hregister);
  342. cg.a_jmp_always(current_asmdata.CurrAsmList,hlabel);
  343. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  344. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,0,hregister);
  345. cg.a_label(current_asmdata.CurrAsmList,hlabel);
  346. tbasecgarm(cg).cgsetflags:=true;
  347. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_INT,hregister,hregister);
  348. tbasecgarm(cg).cgsetflags:=false;
  349. end;
  350. else
  351. internalerror(200311301);
  352. end;
  353. { load flags to register }
  354. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  355. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  356. cg.g_flags2reg(current_asmdata.CurrAsmList,location.size,resflags,hreg1);
  357. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  358. if (is_cbool(resultdef)) then
  359. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,location.size,hreg1,hreg1);
  360. {$ifndef cpu64bitalu}
  361. if (location.size in [OS_64,OS_S64]) then
  362. begin
  363. location.register64.reglo:=hreg1;
  364. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  365. if (is_cbool(resultdef)) then
  366. { reglo is either 0 or -1 -> reghi has to become the same }
  367. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,location.register64.reglo,location.register64.reghi)
  368. else
  369. { unsigned }
  370. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,location.register64.reghi);
  371. end
  372. else
  373. {$endif cpu64bitalu}
  374. location.register:=hreg1;
  375. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  376. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  377. end;
  378. begin
  379. ctypeconvnode:=tarmtypeconvnode;
  380. end.