narmcnv.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. hregister : tregister;
  196. href : treference;
  197. resflags : tresflags;
  198. hlabel,oldTrueLabel,oldFalseLabel : tasmlabel;
  199. newsize : tcgsize;
  200. begin
  201. oldTrueLabel:=current_procinfo.CurrTrueLabel;
  202. oldFalseLabel:=current_procinfo.CurrFalseLabel;
  203. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  204. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  205. secondpass(left);
  206. if codegenerror then
  207. exit;
  208. { Explicit typecasts from any ordinal type to a boolean type }
  209. { must not change the ordinal value }
  210. if (nf_explicit in flags) and
  211. not(left.location.loc in [LOC_FLAGS,LOC_JUMP]) then
  212. begin
  213. location_copy(location,left.location);
  214. newsize:=def_cgsize(resultdef);
  215. { change of size? change sign only if location is LOC_(C)REGISTER? Then we have to sign/zero-extend }
  216. if (tcgsize2size[newsize]<>tcgsize2size[left.location.size]) or
  217. ((newsize<>left.location.size) and (location.loc in [LOC_REGISTER,LOC_CREGISTER])) then
  218. location_force_reg(current_asmdata.CurrAsmList,location,newsize,true)
  219. else
  220. location.size:=newsize;
  221. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  222. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  223. exit;
  224. end;
  225. { Load left node into flag F_NE/F_E }
  226. resflags:=F_NE;
  227. case left.location.loc of
  228. LOC_CREFERENCE,
  229. LOC_REFERENCE :
  230. begin
  231. if left.location.size in [OS_64,OS_S64] then
  232. begin
  233. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  234. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.reference,hregister);
  235. href:=left.location.reference;
  236. inc(href.offset,4);
  237. tcgarm(cg).cgsetflags:=true;
  238. cg.a_op_ref_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,href,hregister);
  239. tcgarm(cg).cgsetflags:=false;
  240. end
  241. else
  242. begin
  243. location_force_reg(current_asmdata.CurrAsmList,left.location,left.location.size,true);
  244. tcgarm(cg).cgsetflags:=true;
  245. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,left.location.size,left.location.register,left.location.register);
  246. tcgarm(cg).cgsetflags:=false;
  247. end;
  248. end;
  249. LOC_FLAGS :
  250. begin
  251. resflags:=left.location.resflags;
  252. end;
  253. LOC_REGISTER,LOC_CREGISTER :
  254. begin
  255. if left.location.size in [OS_64,OS_S64] then
  256. begin
  257. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  258. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.register64.reglo,hregister);
  259. tcgarm(cg).cgsetflags:=true;
  260. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,left.location.register64.reghi,hregister);
  261. tcgarm(cg).cgsetflags:=false;
  262. end
  263. else
  264. begin
  265. tcgarm(cg).cgsetflags:=true;
  266. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,left.location.size,left.location.register,left.location.register);
  267. tcgarm(cg).cgsetflags:=false;
  268. end;
  269. end;
  270. LOC_JUMP :
  271. begin
  272. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  273. current_asmdata.getjumplabel(hlabel);
  274. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  275. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,1,hregister);
  276. cg.a_jmp_always(current_asmdata.CurrAsmList,hlabel);
  277. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  278. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,0,hregister);
  279. cg.a_label(current_asmdata.CurrAsmList,hlabel);
  280. tcgarm(cg).cgsetflags:=true;
  281. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_INT,hregister,hregister);
  282. tcgarm(cg).cgsetflags:=false;
  283. end;
  284. else
  285. internalerror(200311301);
  286. end;
  287. { load flags to register }
  288. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  289. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  290. cg.g_flags2reg(current_asmdata.CurrAsmList,location.size,resflags,location.register);
  291. if (is_cbool(resultdef)) then
  292. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,location.size,location.register,location.register);
  293. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  294. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  295. end;
  296. begin
  297. ctypeconvnode:=tarmtypeconvnode;
  298. end.