narmcnv.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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,defcmp;
  22. type
  23. tarmtypeconvnode = class(tcgtypeconvnode)
  24. protected
  25. function first_int_to_real: tnode;override;
  26. { procedure second_int_to_int;override; }
  27. { procedure second_string_to_string;override; }
  28. { procedure second_cstring_to_pchar;override; }
  29. { procedure second_string_to_chararray;override; }
  30. { procedure second_array_to_pointer;override; }
  31. // function first_int_to_real: tnode; override;
  32. { procedure second_pointer_to_array;override; }
  33. { procedure second_chararray_to_string;override; }
  34. { procedure second_char_to_string;override; }
  35. procedure second_int_to_real;override;
  36. // procedure second_real_to_real;override;
  37. { procedure second_cord_to_pointer;override; }
  38. { procedure second_proc_to_procvar;override; }
  39. { procedure second_bool_to_int;override; }
  40. procedure second_int_to_bool;override;
  41. { procedure second_load_smallset;override; }
  42. { procedure second_ansistring_to_pchar;override; }
  43. { procedure second_pchar_to_string;override; }
  44. { procedure second_class_to_intf;override; }
  45. { procedure second_char_to_char;override; }
  46. end;
  47. implementation
  48. uses
  49. verbose,globtype,globals,systems,
  50. symconst,symdef,aasmbase,aasmtai,aasmdata,
  51. defutil,
  52. cgbase,cgutils,
  53. pass_1,pass_2,procinfo,
  54. ncon,ncal,
  55. ncgutil,
  56. cpubase,cpuinfo,aasmcpu,
  57. rgobj,tgobj,cgobj,cgcpu;
  58. {*****************************************************************************
  59. FirstTypeConv
  60. *****************************************************************************}
  61. function tarmtypeconvnode.first_int_to_real: tnode;
  62. var
  63. fname: string[19];
  64. begin
  65. if cs_fp_emulation in current_settings.moduleswitches then
  66. result:=inherited first_int_to_real
  67. else
  68. begin
  69. { converting a 64bit integer to a float requires a helper }
  70. if is_64bitint(left.resultdef) or
  71. is_currency(left.resultdef) then
  72. begin
  73. { hack to avoid double division by 10000, as it's
  74. already done by typecheckpass.resultdef_int_to_real }
  75. if is_currency(left.resultdef) then
  76. left.resultdef := s64inttype;
  77. if is_signed(left.resultdef) then
  78. fname := 'fpc_int64_to_double'
  79. else
  80. fname := 'fpc_qword_to_double';
  81. result := ccallnode.createintern(fname,ccallparanode.create(
  82. left,nil));
  83. left:=nil;
  84. if (tfloatdef(resultdef).floattype=s32real) then
  85. inserttypeconv(result,s32floattype);
  86. firstpass(result);
  87. exit;
  88. end
  89. else
  90. { other integers are supposed to be 32 bit }
  91. begin
  92. if is_signed(left.resultdef) then
  93. inserttypeconv(left,s32inttype)
  94. else
  95. inserttypeconv(left,u32inttype);
  96. firstpass(left);
  97. end;
  98. result := nil;
  99. case current_settings.fputype of
  100. fpu_fpa,
  101. fpu_fpa10,
  102. fpu_fpa11:
  103. expectloc:=LOC_FPUREGISTER;
  104. fpu_vfpv2,
  105. fpu_vfpv3:
  106. expectloc:=LOC_MMREGISTER;
  107. else
  108. internalerror(2009112702);
  109. end;
  110. end;
  111. end;
  112. procedure tarmtypeconvnode.second_int_to_real;
  113. const
  114. signedprec2vfpop: array[boolean,OS_F32..OS_F64] of tasmop =
  115. ((A_FUITOS,A_FUITOD),
  116. (A_FSITOS,A_FSITOD));
  117. var
  118. instr : taicpu;
  119. href : treference;
  120. l1,l2 : tasmlabel;
  121. hregister : tregister;
  122. signed : boolean;
  123. begin
  124. case current_settings.fputype of
  125. fpu_fpa,
  126. fpu_fpa10,
  127. fpu_fpa11:
  128. begin
  129. { convert first to double to avoid precision loss }
  130. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  131. location_force_reg(current_asmdata.CurrAsmList,left.location,OS_32,true);
  132. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  133. instr:=taicpu.op_reg_reg(A_FLT,location.register,left.location.register);
  134. if is_signed(left.resultdef) then
  135. begin
  136. instr.oppostfix:=cgsize2fpuoppostfix[def_cgsize(resultdef)];
  137. current_asmdata.CurrAsmList.concat(instr);
  138. end
  139. else
  140. begin
  141. { flt does a signed load, fix this }
  142. case tfloatdef(resultdef).floattype of
  143. s32real,
  144. s64real:
  145. begin
  146. { converting dword to s64real first and cut off at the end avoids precision loss }
  147. instr.oppostfix:=PF_D;
  148. current_asmdata.CurrAsmList.concat(instr);
  149. current_asmdata.getdatalabel(l1);
  150. current_asmdata.getjumplabel(l2);
  151. reference_reset_symbol(href,l1,0,const_align(8));
  152. current_asmdata.CurrAsmList.concat(Taicpu.op_reg_const(A_CMP,left.location.register,0));
  153. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_GE,l2);
  154. hregister:=cg.getfpuregister(current_asmdata.CurrAsmList,OS_F64);
  155. new_section(current_asmdata.asmlists[al_typedconsts],sec_rodata_norel,l1.name,const_align(8));
  156. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(l1));
  157. { I got this constant from a test program (FK) }
  158. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_32bit($41f00000));
  159. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_32bit(0));
  160. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList,OS_F64,OS_F64,href,hregister);
  161. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADF,location.register,hregister,location.register),PF_D));
  162. cg.a_label(current_asmdata.CurrAsmList,l2);
  163. { cut off if we should convert to single }
  164. if tfloatdef(resultdef).floattype=s32real then
  165. begin
  166. hregister:=location.register;
  167. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  168. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_MVF,location.register,hregister),PF_S));
  169. end;
  170. end;
  171. else
  172. internalerror(200410031);
  173. end;
  174. end;
  175. end;
  176. fpu_vfpv2,
  177. fpu_vfpv3:
  178. begin
  179. location_reset(location,LOC_MMREGISTER,def_cgsize(resultdef));
  180. signed:=left.location.size=OS_S32;
  181. location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,false);
  182. if (left.location.size<>OS_F32) then
  183. internalerror(2009112703);
  184. if left.location.size<>location.size then
  185. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size)
  186. else
  187. location.register:=left.location.register;
  188. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(
  189. signedprec2vfpop[signed,location.size],location.register,left.location.register));
  190. end;
  191. end;
  192. end;
  193. procedure tarmtypeconvnode.second_int_to_bool;
  194. var
  195. hreg1,
  196. hregister : tregister;
  197. href : treference;
  198. resflags : tresflags;
  199. hlabel,oldTrueLabel,oldFalseLabel : tasmlabel;
  200. newsize : tcgsize;
  201. begin
  202. oldTrueLabel:=current_procinfo.CurrTrueLabel;
  203. oldFalseLabel:=current_procinfo.CurrFalseLabel;
  204. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  205. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  206. secondpass(left);
  207. if codegenerror then
  208. exit;
  209. { Explicit typecasts from any ordinal type to a boolean type }
  210. { must not change the ordinal value }
  211. if (nf_explicit in flags) and
  212. not(left.location.loc in [LOC_FLAGS,LOC_JUMP]) then
  213. begin
  214. location_copy(location,left.location);
  215. newsize:=def_cgsize(resultdef);
  216. { change of size? change sign only if location is LOC_(C)REGISTER? Then we have to sign/zero-extend }
  217. if (tcgsize2size[newsize]<>tcgsize2size[left.location.size]) or
  218. ((newsize<>left.location.size) and (location.loc in [LOC_REGISTER,LOC_CREGISTER])) then
  219. location_force_reg(current_asmdata.CurrAsmList,location,newsize,true)
  220. else
  221. location.size:=newsize;
  222. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  223. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  224. exit;
  225. end;
  226. { Load left node into flag F_NE/F_E }
  227. resflags:=F_NE;
  228. case left.location.loc of
  229. LOC_CREFERENCE,
  230. LOC_REFERENCE :
  231. begin
  232. if left.location.size in [OS_64,OS_S64] then
  233. begin
  234. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  235. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.reference,hregister);
  236. href:=left.location.reference;
  237. inc(href.offset,4);
  238. tcgarm(cg).cgsetflags:=true;
  239. cg.a_op_ref_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,href,hregister);
  240. tcgarm(cg).cgsetflags:=false;
  241. end
  242. else
  243. begin
  244. location_force_reg(current_asmdata.CurrAsmList,left.location,left.location.size,true);
  245. tcgarm(cg).cgsetflags:=true;
  246. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,left.location.size,left.location.register,left.location.register);
  247. tcgarm(cg).cgsetflags:=false;
  248. end;
  249. end;
  250. LOC_FLAGS :
  251. begin
  252. resflags:=left.location.resflags;
  253. end;
  254. LOC_REGISTER,LOC_CREGISTER :
  255. begin
  256. if left.location.size in [OS_64,OS_S64] then
  257. begin
  258. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  259. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.register64.reglo,hregister);
  260. tcgarm(cg).cgsetflags:=true;
  261. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,left.location.register64.reghi,hregister);
  262. tcgarm(cg).cgsetflags:=false;
  263. end
  264. else
  265. begin
  266. tcgarm(cg).cgsetflags:=true;
  267. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,left.location.size,left.location.register,left.location.register);
  268. tcgarm(cg).cgsetflags:=false;
  269. end;
  270. end;
  271. LOC_JUMP :
  272. begin
  273. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  274. current_asmdata.getjumplabel(hlabel);
  275. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  276. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,1,hregister);
  277. cg.a_jmp_always(current_asmdata.CurrAsmList,hlabel);
  278. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  279. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,0,hregister);
  280. cg.a_label(current_asmdata.CurrAsmList,hlabel);
  281. tcgarm(cg).cgsetflags:=true;
  282. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_INT,hregister,hregister);
  283. tcgarm(cg).cgsetflags:=false;
  284. end;
  285. else
  286. internalerror(200311301);
  287. end;
  288. { load flags to register }
  289. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  290. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  291. cg.g_flags2reg(current_asmdata.CurrAsmList,location.size,resflags,hreg1);
  292. if (is_cbool(resultdef)) then
  293. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,location.size,hreg1,hreg1);
  294. {$ifndef cpu64bitalu}
  295. if (location.size in [OS_64,OS_S64]) then
  296. begin
  297. location.register64.reglo:=hreg1;
  298. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  299. if (is_cbool(resultdef)) then
  300. { reglo is either 0 or -1 -> reghi has to become the same }
  301. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,location.register64.reglo,location.register64.reghi)
  302. else
  303. { unsigned }
  304. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,location.register64.reghi);
  305. end
  306. else
  307. {$endif cpu64bitalu}
  308. location.register:=hreg1;
  309. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  310. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  311. end;
  312. begin
  313. ctypeconvnode:=tarmtypeconvnode;
  314. end.