2
0

narmcnv.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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,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. firstpass(result);
  85. exit;
  86. end
  87. else
  88. { other integers are supposed to be 32 bit }
  89. begin
  90. if is_signed(left.resultdef) then
  91. inserttypeconv(left,s32inttype)
  92. else
  93. inserttypeconv(left,u32inttype);
  94. firstpass(left);
  95. end;
  96. result := nil;
  97. if registersfpu<1 then
  98. registersfpu:=1;
  99. expectloc:=LOC_FPUREGISTER;
  100. end;
  101. end;
  102. procedure tarmtypeconvnode.second_int_to_real;
  103. var
  104. instr : taicpu;
  105. href : treference;
  106. l1,l2 : tasmlabel;
  107. hregister : tregister;
  108. begin
  109. { convert first to double to avoid precision loss }
  110. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  111. location_force_reg(current_asmdata.CurrAsmList,left.location,OS_32,true);
  112. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  113. instr:=taicpu.op_reg_reg(A_FLT,location.register,left.location.register);
  114. if is_signed(left.resultdef) then
  115. begin
  116. instr.oppostfix:=cgsize2fpuoppostfix[def_cgsize(resultdef)];;
  117. current_asmdata.CurrAsmList.concat(instr);
  118. end
  119. else
  120. begin
  121. { flt does a signed load, fix this }
  122. case tfloatdef(resultdef).floattype of
  123. s32real,
  124. s64real:
  125. begin
  126. { converting dword to s64real first and cut off at the end avoids precision loss }
  127. instr.oppostfix:=PF_D;
  128. current_asmdata.CurrAsmList.concat(instr);
  129. current_asmdata.getdatalabel(l1);
  130. current_asmdata.getjumplabel(l2);
  131. reference_reset_symbol(href,l1,0);
  132. current_asmdata.CurrAsmList.concat(Taicpu.op_reg_const(A_CMP,left.location.register,0));
  133. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_GE,l2);
  134. hregister:=cg.getfpuregister(current_asmdata.CurrAsmList,OS_F64);
  135. current_asmdata.asmlists[al_typedconsts].concat(tai_align.create(const_align(8)));
  136. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(l1));
  137. { I got this constant from a test program (FK) }
  138. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_32bit($41f00000));
  139. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_32bit(0));
  140. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList,OS_F64,OS_F64,href,hregister);
  141. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADF,location.register,hregister,location.register),PF_D));
  142. cg.a_label(current_asmdata.CurrAsmList,l2);
  143. { cut off if we should convert to single }
  144. if tfloatdef(resultdef).floattype=s32real then
  145. begin
  146. hregister:=location.register;
  147. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  148. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_MVF,location.register,hregister),PF_S));
  149. end;
  150. end;
  151. else
  152. internalerror(200410031);
  153. end;
  154. end;
  155. end;
  156. procedure tarmtypeconvnode.second_int_to_bool;
  157. var
  158. hregister : tregister;
  159. href : treference;
  160. resflags : tresflags;
  161. hlabel,oldTrueLabel,oldFalseLabel : tasmlabel;
  162. begin
  163. oldTrueLabel:=current_procinfo.CurrTrueLabel;
  164. oldFalseLabel:=current_procinfo.CurrFalseLabel;
  165. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  166. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  167. secondpass(left);
  168. if codegenerror then
  169. exit;
  170. { byte(boolean) or word(wordbool) or longint(longbool) must
  171. be accepted for var parameters }
  172. if (nf_explicit in flags) and
  173. (left.resultdef.size=resultdef.size) and
  174. (left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  175. begin
  176. location_copy(location,left.location);
  177. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  178. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  179. exit;
  180. end;
  181. { Load left node into flag F_NE/F_E }
  182. resflags:=F_NE;
  183. case left.location.loc of
  184. LOC_CREFERENCE,
  185. LOC_REFERENCE :
  186. begin
  187. if left.location.size in [OS_64,OS_S64] then
  188. begin
  189. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  190. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.reference,hregister);
  191. href:=left.location.reference;
  192. inc(href.offset,4);
  193. tcgarm(cg).cgsetflags:=true;
  194. cg.a_op_ref_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,href,hregister);
  195. tcgarm(cg).cgsetflags:=false;
  196. end
  197. else
  198. begin
  199. location_force_reg(current_asmdata.CurrAsmList,left.location,left.location.size,true);
  200. tcgarm(cg).cgsetflags:=true;
  201. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,left.location.size,left.location.register,left.location.register);
  202. tcgarm(cg).cgsetflags:=false;
  203. end;
  204. end;
  205. LOC_FLAGS :
  206. begin
  207. resflags:=left.location.resflags;
  208. end;
  209. LOC_REGISTER,LOC_CREGISTER :
  210. begin
  211. if left.location.size in [OS_64,OS_S64] then
  212. begin
  213. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  214. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.register64.reglo,hregister);
  215. tcgarm(cg).cgsetflags:=true;
  216. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,left.location.register64.reghi,hregister);
  217. tcgarm(cg).cgsetflags:=false;
  218. end
  219. else
  220. begin
  221. tcgarm(cg).cgsetflags:=true;
  222. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,left.location.size,left.location.register,left.location.register);
  223. tcgarm(cg).cgsetflags:=false;
  224. end;
  225. end;
  226. LOC_JUMP :
  227. begin
  228. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  229. current_asmdata.getjumplabel(hlabel);
  230. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  231. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,1,hregister);
  232. cg.a_jmp_always(current_asmdata.CurrAsmList,hlabel);
  233. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  234. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,0,hregister);
  235. cg.a_label(current_asmdata.CurrAsmList,hlabel);
  236. tcgarm(cg).cgsetflags:=true;
  237. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_INT,hregister,hregister);
  238. tcgarm(cg).cgsetflags:=false;
  239. end;
  240. else
  241. internalerror(200311301);
  242. end;
  243. { load flags to register }
  244. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  245. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  246. cg.g_flags2reg(current_asmdata.CurrAsmList,location.size,resflags,location.register);
  247. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  248. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  249. end;
  250. begin
  251. ctypeconvnode:=tarmtypeconvnode;
  252. end.