narmcnv.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. (current_settings.fputype=fpu_fpv4_s16) 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_vfpv2,
  107. fpu_vfpv3,
  108. fpu_vfpv4,
  109. fpu_vfpv3_d16,
  110. fpu_fpv4_s16:
  111. expectloc:=LOC_MMREGISTER;
  112. else
  113. internalerror(2009112702);
  114. end;
  115. end;
  116. end;
  117. function tarmtypeconvnode.first_real_to_real: tnode;
  118. begin
  119. if (current_settings.fputype=fpu_fpv4_s16) then
  120. begin
  121. case tfloatdef(left.resultdef).floattype of
  122. s32real:
  123. case tfloatdef(resultdef).floattype of
  124. s64real:
  125. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float32_to_float64',ccallparanode.create(
  126. ctypeconvnode.create_internal(left,search_system_type('FLOAT32REC').typedef),nil)),resultdef);
  127. s32real:
  128. begin
  129. result:=left;
  130. left:=nil;
  131. end;
  132. else
  133. internalerror(200610151);
  134. end;
  135. s64real:
  136. case tfloatdef(resultdef).floattype of
  137. s32real:
  138. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float64_to_float32',ccallparanode.create(
  139. ctypeconvnode.create_internal(left,search_system_type('FLOAT64').typedef),nil)),resultdef);
  140. s64real:
  141. begin
  142. result:=left;
  143. left:=nil;
  144. end;
  145. else
  146. internalerror(200610152);
  147. end;
  148. else
  149. internalerror(200610153);
  150. end;
  151. left:=nil;
  152. firstpass(result);
  153. exit;
  154. end
  155. else
  156. Result := inherited first_real_to_real;
  157. end;
  158. procedure tarmtypeconvnode.second_int_to_real;
  159. const
  160. signedprec2vfppf: array[boolean,OS_F32..OS_F64] of toppostfix =
  161. ((PF_F32U32,PF_F64U32),
  162. (PF_F32S32,PF_F64S32));
  163. var
  164. instr : taicpu;
  165. href : treference;
  166. l1,l2 : tasmlabel;
  167. hregister : tregister;
  168. signed : boolean;
  169. begin
  170. case current_settings.fputype of
  171. fpu_fpa,
  172. fpu_fpa10,
  173. fpu_fpa11:
  174. begin
  175. { convert first to double to avoid precision loss }
  176. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  177. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,u32inttype,true);
  178. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  179. instr:=taicpu.op_reg_reg(A_FLT,location.register,left.location.register);
  180. if is_signed(left.resultdef) then
  181. begin
  182. instr.oppostfix:=cgsize2fpuoppostfix[def_cgsize(resultdef)];
  183. current_asmdata.CurrAsmList.concat(instr);
  184. end
  185. else
  186. begin
  187. { flt does a signed load, fix this }
  188. case tfloatdef(resultdef).floattype of
  189. s32real,
  190. s64real:
  191. begin
  192. { converting dword to s64real first and cut off at the end avoids precision loss }
  193. instr.oppostfix:=PF_D;
  194. current_asmdata.CurrAsmList.concat(instr);
  195. current_asmdata.getglobaldatalabel(l1);
  196. current_asmdata.getjumplabel(l2);
  197. reference_reset_symbol(href,l1,0,const_align(8),[]);
  198. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  199. current_asmdata.CurrAsmList.concat(Taicpu.op_reg_const(A_CMP,left.location.register,0));
  200. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_GE,l2);
  201. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  202. hregister:=cg.getfpuregister(current_asmdata.CurrAsmList,OS_F64);
  203. new_section(current_asmdata.asmlists[al_typedconsts],sec_rodata_norel,l1.name,const_align(8));
  204. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(l1));
  205. { I got this constant from a test program (FK) }
  206. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_32bit($41f00000));
  207. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_32bit(0));
  208. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList,OS_F64,OS_F64,href,hregister);
  209. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADF,location.register,hregister,location.register),PF_D));
  210. cg.a_label(current_asmdata.CurrAsmList,l2);
  211. { cut off if we should convert to single }
  212. if tfloatdef(resultdef).floattype=s32real then
  213. begin
  214. hregister:=location.register;
  215. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  216. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  217. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_MVF,location.register,hregister),PF_S));
  218. end;
  219. end;
  220. else
  221. internalerror(200410031);
  222. end;
  223. end;
  224. end;
  225. fpu_vfpv2,
  226. fpu_vfpv3,
  227. fpu_vfpv4,
  228. fpu_vfpv3_d16:
  229. begin
  230. location_reset(location,LOC_MMREGISTER,def_cgsize(resultdef));
  231. signed:=left.location.size=OS_S32;
  232. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,false);
  233. if (left.location.size<>OS_F32) then
  234. internalerror(2009112703);
  235. if left.location.size<>location.size then
  236. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size)
  237. else
  238. location.register:=left.location.register;
  239. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_VCVT,
  240. location.register,left.location.register),
  241. signedprec2vfppf[signed,location.size]));
  242. end;
  243. fpu_fpv4_s16:
  244. begin
  245. location_reset(location,LOC_MMREGISTER,def_cgsize(resultdef));
  246. signed:=left.location.size=OS_S32;
  247. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,false);
  248. if (left.location.size<>OS_F32) then
  249. internalerror(2009112703);
  250. if left.location.size<>location.size then
  251. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size)
  252. else
  253. location.register:=left.location.register;
  254. if signed then
  255. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_VCVT,location.register,left.location.register), PF_F32S32))
  256. else
  257. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_VCVT,location.register,left.location.register), PF_F32U32));
  258. end;
  259. else
  260. { should be handled in pass 1 }
  261. internalerror(2019050934);
  262. end;
  263. end;
  264. procedure tarmtypeconvnode.second_int_to_bool;
  265. var
  266. hreg1,
  267. hregister : tregister;
  268. href : treference;
  269. resflags : tresflags;
  270. hlabel : tasmlabel;
  271. newsize : tcgsize;
  272. begin
  273. secondpass(left);
  274. if codegenerror then
  275. exit;
  276. { Explicit typecasts from any ordinal type to a boolean type }
  277. { must not change the ordinal value }
  278. if (nf_explicit in flags) and
  279. not(left.location.loc in [LOC_FLAGS,LOC_JUMP]) then
  280. begin
  281. location_copy(location,left.location);
  282. newsize:=def_cgsize(resultdef);
  283. { change of size? change sign only if location is LOC_(C)REGISTER? Then we have to sign/zero-extend }
  284. if (tcgsize2size[newsize]<>tcgsize2size[left.location.size]) or
  285. ((newsize<>left.location.size) and (location.loc in [LOC_REGISTER,LOC_CREGISTER])) then
  286. hlcg.location_force_reg(current_asmdata.CurrAsmList,location,left.resultdef,resultdef,true)
  287. else
  288. location.size:=newsize;
  289. exit;
  290. end;
  291. { Load left node into flag F_NE/F_E }
  292. resflags:=F_NE;
  293. if (left.location.loc in [LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF]) then
  294. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  295. case left.location.loc of
  296. LOC_CREFERENCE,
  297. LOC_REFERENCE :
  298. begin
  299. if left.location.size in [OS_64,OS_S64] then
  300. begin
  301. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  302. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.reference,hregister);
  303. href:=left.location.reference;
  304. inc(href.offset,4);
  305. tbasecgarm(cg).cgsetflags:=true;
  306. cg.a_op_ref_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,href,hregister);
  307. tbasecgarm(cg).cgsetflags:=false;
  308. end
  309. else
  310. begin
  311. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  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_FLAGS :
  318. begin
  319. resflags:=left.location.resflags;
  320. end;
  321. LOC_REGISTER,LOC_CREGISTER :
  322. begin
  323. if left.location.size in [OS_64,OS_S64] then
  324. begin
  325. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  326. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.register64.reglo,hregister);
  327. tbasecgarm(cg).cgsetflags:=true;
  328. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,left.location.register64.reghi,hregister);
  329. tbasecgarm(cg).cgsetflags:=false;
  330. end
  331. else
  332. begin
  333. tbasecgarm(cg).cgsetflags:=true;
  334. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,left.location.size,left.location.register,left.location.register);
  335. tbasecgarm(cg).cgsetflags:=false;
  336. end;
  337. end;
  338. LOC_JUMP :
  339. begin
  340. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  341. current_asmdata.getjumplabel(hlabel);
  342. cg.a_label(current_asmdata.CurrAsmList,left.location.truelabel);
  343. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,1,hregister);
  344. cg.a_jmp_always(current_asmdata.CurrAsmList,hlabel);
  345. cg.a_label(current_asmdata.CurrAsmList,left.location.falselabel);
  346. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,0,hregister);
  347. cg.a_label(current_asmdata.CurrAsmList,hlabel);
  348. tbasecgarm(cg).cgsetflags:=true;
  349. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_INT,hregister,hregister);
  350. tbasecgarm(cg).cgsetflags:=false;
  351. end;
  352. else
  353. internalerror(200311301);
  354. end;
  355. { load flags to register }
  356. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  357. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  358. cg.g_flags2reg(current_asmdata.CurrAsmList,location.size,resflags,hreg1);
  359. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  360. if (is_cbool(resultdef)) then
  361. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,location.size,hreg1,hreg1);
  362. {$ifndef cpu64bitalu}
  363. if (location.size in [OS_64,OS_S64]) then
  364. begin
  365. location.register64.reglo:=hreg1;
  366. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  367. if (is_cbool(resultdef)) then
  368. { reglo is either 0 or -1 -> reghi has to become the same }
  369. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,location.register64.reglo,location.register64.reghi)
  370. else
  371. { unsigned }
  372. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,location.register64.reghi);
  373. end
  374. else
  375. {$endif cpu64bitalu}
  376. location.register:=hreg1;
  377. end;
  378. begin
  379. ctypeconvnode:=tarmtypeconvnode;
  380. end.