2
0

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. (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. signedprec2vfpop: array[boolean,OS_F32..OS_F64] of tasmop =
  157. ((A_FUITOS,A_FUITOD),
  158. (A_FSITOS,A_FSITOD));
  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.getdatalabel(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(taicpu.op_reg_reg(
  235. signedprec2vfpop[signed,location.size],location.register,left.location.register));
  236. end;
  237. fpu_fpv4_s16:
  238. begin
  239. location_reset(location,LOC_MMREGISTER,def_cgsize(resultdef));
  240. signed:=left.location.size=OS_S32;
  241. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,false);
  242. if (left.location.size<>OS_F32) then
  243. internalerror(2009112703);
  244. if left.location.size<>location.size then
  245. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size)
  246. else
  247. location.register:=left.location.register;
  248. if signed then
  249. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_VCVT,location.register,left.location.register), PF_F32S32))
  250. else
  251. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_VCVT,location.register,left.location.register), PF_F32U32));
  252. end;
  253. end;
  254. end;
  255. procedure tarmtypeconvnode.second_int_to_bool;
  256. var
  257. hreg1,
  258. hregister : tregister;
  259. href : treference;
  260. resflags : tresflags;
  261. hlabel,oldTrueLabel,oldFalseLabel : tasmlabel;
  262. newsize : tcgsize;
  263. begin
  264. oldTrueLabel:=current_procinfo.CurrTrueLabel;
  265. oldFalseLabel:=current_procinfo.CurrFalseLabel;
  266. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  267. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  268. secondpass(left);
  269. if codegenerror then
  270. exit;
  271. { Explicit typecasts from any ordinal type to a boolean type }
  272. { must not change the ordinal value }
  273. if (nf_explicit in flags) and
  274. not(left.location.loc in [LOC_FLAGS,LOC_JUMP]) then
  275. begin
  276. location_copy(location,left.location);
  277. newsize:=def_cgsize(resultdef);
  278. { change of size? change sign only if location is LOC_(C)REGISTER? Then we have to sign/zero-extend }
  279. if (tcgsize2size[newsize]<>tcgsize2size[left.location.size]) or
  280. ((newsize<>left.location.size) and (location.loc in [LOC_REGISTER,LOC_CREGISTER])) then
  281. hlcg.location_force_reg(current_asmdata.CurrAsmList,location,left.resultdef,resultdef,true)
  282. else
  283. location.size:=newsize;
  284. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  285. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  286. exit;
  287. end;
  288. { Load left node into flag F_NE/F_E }
  289. resflags:=F_NE;
  290. if (left.location.loc in [LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF]) then
  291. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  292. case left.location.loc of
  293. LOC_CREFERENCE,
  294. LOC_REFERENCE :
  295. begin
  296. if left.location.size in [OS_64,OS_S64] then
  297. begin
  298. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  299. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.reference,hregister);
  300. href:=left.location.reference;
  301. inc(href.offset,4);
  302. tbasecgarm(cg).cgsetflags:=true;
  303. cg.a_op_ref_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,href,hregister);
  304. tbasecgarm(cg).cgsetflags:=false;
  305. end
  306. else
  307. begin
  308. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  309. tbasecgarm(cg).cgsetflags:=true;
  310. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,left.location.size,left.location.register,left.location.register);
  311. tbasecgarm(cg).cgsetflags:=false;
  312. end;
  313. end;
  314. LOC_FLAGS :
  315. begin
  316. resflags:=left.location.resflags;
  317. end;
  318. LOC_REGISTER,LOC_CREGISTER :
  319. begin
  320. if left.location.size in [OS_64,OS_S64] then
  321. begin
  322. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  323. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.register64.reglo,hregister);
  324. tbasecgarm(cg).cgsetflags:=true;
  325. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,left.location.register64.reghi,hregister);
  326. tbasecgarm(cg).cgsetflags:=false;
  327. end
  328. else
  329. begin
  330. tbasecgarm(cg).cgsetflags:=true;
  331. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,left.location.size,left.location.register,left.location.register);
  332. tbasecgarm(cg).cgsetflags:=false;
  333. end;
  334. end;
  335. LOC_JUMP :
  336. begin
  337. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  338. current_asmdata.getjumplabel(hlabel);
  339. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  340. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,1,hregister);
  341. cg.a_jmp_always(current_asmdata.CurrAsmList,hlabel);
  342. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  343. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,0,hregister);
  344. cg.a_label(current_asmdata.CurrAsmList,hlabel);
  345. tbasecgarm(cg).cgsetflags:=true;
  346. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_INT,hregister,hregister);
  347. tbasecgarm(cg).cgsetflags:=false;
  348. end;
  349. else
  350. internalerror(200311301);
  351. end;
  352. { load flags to register }
  353. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  354. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  355. cg.g_flags2reg(current_asmdata.CurrAsmList,location.size,resflags,hreg1);
  356. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  357. if (is_cbool(resultdef)) then
  358. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,location.size,hreg1,hreg1);
  359. {$ifndef cpu64bitalu}
  360. if (location.size in [OS_64,OS_S64]) then
  361. begin
  362. location.register64.reglo:=hreg1;
  363. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  364. if (is_cbool(resultdef)) then
  365. { reglo is either 0 or -1 -> reghi has to become the same }
  366. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,location.register64.reglo,location.register64.reghi)
  367. else
  368. { unsigned }
  369. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,location.register64.reghi);
  370. end
  371. else
  372. {$endif cpu64bitalu}
  373. location.register:=hreg1;
  374. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  375. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  376. end;
  377. begin
  378. ctypeconvnode:=tarmtypeconvnode;
  379. end.