narmcnv.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 aktmoduleswitches then
  66. begin
  67. if target_info.system in system_wince then
  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:='I64TOD'
  79. else
  80. fname:='UI64TOD';
  81. end
  82. else
  83. { other integers are supposed to be 32 bit }
  84. begin
  85. if is_signed(left.resultdef) then
  86. fname:='ITOD'
  87. else
  88. fname:='UTOD';
  89. firstpass(left);
  90. end;
  91. result:=ccallnode.createintern(fname,ccallparanode.create(
  92. left,nil));
  93. left:=nil;
  94. firstpass(result);
  95. exit;
  96. end
  97. else
  98. begin
  99. { converting a 64bit integer to a float requires a helper }
  100. if is_64bitint(left.resultdef) or
  101. is_currency(left.resultdef) then
  102. begin
  103. { hack to avoid double division by 10000, as it's
  104. already done by typecheckpass.resultdef_int_to_real }
  105. if is_currency(left.resultdef) then
  106. left.resultdef := s64inttype;
  107. if is_signed(left.resultdef) then
  108. fname:='int64_to_'
  109. else
  110. { we can't do better currently }
  111. fname:='int64_to_';
  112. end
  113. else
  114. { other integers are supposed to be 32 bit }
  115. begin
  116. if is_signed(left.resultdef) then
  117. fname:='int32_to_'
  118. else
  119. { we can't do better currently }
  120. fname:='int32_to_';
  121. firstpass(left);
  122. end;
  123. if tfloatdef(resultdef).typ=s64real then
  124. fname:=fname+'float64'
  125. else
  126. fname:=fname+'float32';
  127. result:=ctypeconvnode.create_internal(ccallnode.createintern(fname,ccallparanode.create(
  128. left,nil)),resultdef);
  129. left:=nil;
  130. firstpass(result);
  131. exit;
  132. end;
  133. end
  134. else
  135. begin
  136. { converting a 64bit integer to a float requires a helper }
  137. if is_64bitint(left.resultdef) or
  138. is_currency(left.resultdef) then
  139. begin
  140. { hack to avoid double division by 10000, as it's
  141. already done by typecheckpass.resultdef_int_to_real }
  142. if is_currency(left.resultdef) then
  143. left.resultdef := s64inttype;
  144. if is_signed(left.resultdef) then
  145. fname := 'fpc_int64_to_double'
  146. else
  147. fname := 'fpc_qword_to_double';
  148. result := ccallnode.createintern(fname,ccallparanode.create(
  149. left,nil));
  150. left:=nil;
  151. firstpass(result);
  152. exit;
  153. end
  154. else
  155. { other integers are supposed to be 32 bit }
  156. begin
  157. if is_signed(left.resultdef) then
  158. inserttypeconv(left,s32inttype)
  159. else
  160. inserttypeconv(left,u32inttype);
  161. firstpass(left);
  162. end;
  163. result := nil;
  164. if registersfpu<1 then
  165. registersfpu:=1;
  166. expectloc:=LOC_FPUREGISTER;
  167. end;
  168. end;
  169. procedure tarmtypeconvnode.second_int_to_real;
  170. var
  171. instr : taicpu;
  172. begin
  173. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  174. location_force_reg(current_asmdata.CurrAsmList,left.location,OS_32,true);
  175. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  176. instr:=taicpu.op_reg_reg(A_FLT,location.register,left.location.register);
  177. instr.oppostfix:=cgsize2fpuoppostfix[def_cgsize(resultdef)];
  178. current_asmdata.CurrAsmList.concat(instr);
  179. end;
  180. procedure tarmtypeconvnode.second_int_to_bool;
  181. var
  182. hregister : tregister;
  183. href : treference;
  184. resflags : tresflags;
  185. hlabel,oldTrueLabel,oldFalseLabel : tasmlabel;
  186. begin
  187. oldTrueLabel:=current_procinfo.CurrTrueLabel;
  188. oldFalseLabel:=current_procinfo.CurrFalseLabel;
  189. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  190. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  191. secondpass(left);
  192. if codegenerror then
  193. exit;
  194. { byte(boolean) or word(wordbool) or longint(longbool) must
  195. be accepted for var parameters }
  196. if (nf_explicit in flags) and
  197. (left.resultdef.size=resultdef.size) and
  198. (left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  199. begin
  200. location_copy(location,left.location);
  201. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  202. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  203. exit;
  204. end;
  205. { Load left node into flag F_NE/F_E }
  206. resflags:=F_NE;
  207. case left.location.loc of
  208. LOC_CREFERENCE,
  209. LOC_REFERENCE :
  210. begin
  211. if left.location.size in [OS_64,OS_S64] then
  212. begin
  213. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  214. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.reference,hregister);
  215. href:=left.location.reference;
  216. inc(href.offset,4);
  217. tcgarm(cg).cgsetflags:=true;
  218. cg.a_op_ref_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,href,hregister);
  219. tcgarm(cg).cgsetflags:=false;
  220. end
  221. else
  222. begin
  223. location_force_reg(current_asmdata.CurrAsmList,left.location,left.location.size,true);
  224. tcgarm(cg).cgsetflags:=true;
  225. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,left.location.size,left.location.register,left.location.register);
  226. tcgarm(cg).cgsetflags:=false;
  227. end;
  228. end;
  229. LOC_FLAGS :
  230. begin
  231. resflags:=left.location.resflags;
  232. end;
  233. LOC_REGISTER,LOC_CREGISTER :
  234. begin
  235. if left.location.size in [OS_64,OS_S64] then
  236. begin
  237. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  238. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.register64.reglo,hregister);
  239. tcgarm(cg).cgsetflags:=true;
  240. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,left.location.register64.reghi,hregister);
  241. tcgarm(cg).cgsetflags:=false;
  242. end
  243. else
  244. begin
  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_JUMP :
  251. begin
  252. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  253. current_asmdata.getjumplabel(hlabel);
  254. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  255. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,1,hregister);
  256. cg.a_jmp_always(current_asmdata.CurrAsmList,hlabel);
  257. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  258. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,0,hregister);
  259. cg.a_label(current_asmdata.CurrAsmList,hlabel);
  260. tcgarm(cg).cgsetflags:=true;
  261. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_INT,hregister,hregister);
  262. tcgarm(cg).cgsetflags:=false;
  263. end;
  264. else
  265. internalerror(200311301);
  266. end;
  267. { load flags to register }
  268. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  269. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  270. cg.g_flags2reg(current_asmdata.CurrAsmList,location.size,resflags,location.register);
  271. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  272. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  273. end;
  274. begin
  275. ctypeconvnode:=tarmtypeconvnode;
  276. end.