narmcnv.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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.resulttype.def) or
  71. is_currency(left.resulttype.def) then
  72. begin
  73. { hack to avoid double division by 10000, as it's
  74. already done by resulttypepass.resulttype_int_to_real }
  75. if is_currency(left.resulttype.def) then
  76. left.resulttype := s64inttype;
  77. if is_signed(left.resulttype.def) 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.resulttype.def) 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. internalerror(2005082803);
  100. end;
  101. end
  102. else
  103. begin
  104. { converting a 64bit integer to a float requires a helper }
  105. if is_64bitint(left.resulttype.def) or
  106. is_currency(left.resulttype.def) then
  107. begin
  108. { hack to avoid double division by 10000, as it's
  109. already done by resulttypepass.resulttype_int_to_real }
  110. if is_currency(left.resulttype.def) then
  111. left.resulttype := s64inttype;
  112. if is_signed(left.resulttype.def) then
  113. fname := 'fpc_int64_to_double'
  114. else
  115. fname := 'fpc_qword_to_double';
  116. result := ccallnode.createintern(fname,ccallparanode.create(
  117. left,nil));
  118. left:=nil;
  119. firstpass(result);
  120. exit;
  121. end
  122. else
  123. { other integers are supposed to be 32 bit }
  124. begin
  125. if is_signed(left.resulttype.def) then
  126. inserttypeconv(left,s32inttype)
  127. else
  128. inserttypeconv(left,u32inttype);
  129. firstpass(left);
  130. end;
  131. result := nil;
  132. if registersfpu<1 then
  133. registersfpu:=1;
  134. expectloc:=LOC_FPUREGISTER;
  135. end;
  136. end;
  137. procedure tarmtypeconvnode.second_int_to_real;
  138. var
  139. instr : taicpu;
  140. begin
  141. location_reset(location,LOC_FPUREGISTER,def_cgsize(resulttype.def));
  142. location_force_reg(current_asmdata.CurrAsmList,left.location,OS_32,true);
  143. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  144. instr:=taicpu.op_reg_reg(A_FLT,location.register,left.location.register);
  145. instr.oppostfix:=cgsize2fpuoppostfix[def_cgsize(resulttype.def)];
  146. current_asmdata.CurrAsmList.concat(instr);
  147. end;
  148. procedure tarmtypeconvnode.second_int_to_bool;
  149. var
  150. hregister : tregister;
  151. href : treference;
  152. resflags : tresflags;
  153. hlabel,oldTrueLabel,oldFalseLabel : tasmlabel;
  154. begin
  155. oldTrueLabel:=current_procinfo.CurrTrueLabel;
  156. oldFalseLabel:=current_procinfo.CurrFalseLabel;
  157. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  158. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  159. secondpass(left);
  160. if codegenerror then
  161. exit;
  162. { byte(boolean) or word(wordbool) or longint(longbool) must
  163. be accepted for var parameters }
  164. if (nf_explicit in flags) and
  165. (left.resulttype.def.size=resulttype.def.size) and
  166. (left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  167. begin
  168. location_copy(location,left.location);
  169. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  170. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  171. exit;
  172. end;
  173. { Load left node into flag F_NE/F_E }
  174. resflags:=F_NE;
  175. case left.location.loc of
  176. LOC_CREFERENCE,
  177. LOC_REFERENCE :
  178. begin
  179. if left.location.size in [OS_64,OS_S64] then
  180. begin
  181. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  182. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.reference,hregister);
  183. href:=left.location.reference;
  184. inc(href.offset,4);
  185. tcgarm(cg).cgsetflags:=true;
  186. cg.a_op_ref_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,href,hregister);
  187. tcgarm(cg).cgsetflags:=false;
  188. end
  189. else
  190. begin
  191. location_force_reg(current_asmdata.CurrAsmList,left.location,left.location.size,true);
  192. tcgarm(cg).cgsetflags:=true;
  193. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,left.location.size,left.location.register,left.location.register);
  194. tcgarm(cg).cgsetflags:=false;
  195. end;
  196. end;
  197. LOC_FLAGS :
  198. begin
  199. resflags:=left.location.resflags;
  200. end;
  201. LOC_REGISTER,LOC_CREGISTER :
  202. begin
  203. if left.location.size in [OS_64,OS_S64] then
  204. begin
  205. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  206. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.register64.reglo,hregister);
  207. tcgarm(cg).cgsetflags:=true;
  208. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,left.location.register64.reghi,hregister);
  209. tcgarm(cg).cgsetflags:=false;
  210. end
  211. else
  212. begin
  213. tcgarm(cg).cgsetflags:=true;
  214. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,left.location.size,left.location.register,left.location.register);
  215. tcgarm(cg).cgsetflags:=false;
  216. end;
  217. end;
  218. LOC_JUMP :
  219. begin
  220. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  221. current_asmdata.getjumplabel(hlabel);
  222. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  223. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,1,hregister);
  224. cg.a_jmp_always(current_asmdata.CurrAsmList,hlabel);
  225. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  226. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,0,hregister);
  227. cg.a_label(current_asmdata.CurrAsmList,hlabel);
  228. tcgarm(cg).cgsetflags:=true;
  229. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_INT,hregister,hregister);
  230. tcgarm(cg).cgsetflags:=false;
  231. end;
  232. else
  233. internalerror(200311301);
  234. end;
  235. { load flags to register }
  236. location_reset(location,LOC_REGISTER,def_cgsize(resulttype.def));
  237. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  238. cg.g_flags2reg(current_asmdata.CurrAsmList,location.size,resflags,location.register);
  239. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  240. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  241. end;
  242. begin
  243. ctypeconvnode:=tarmtypeconvnode;
  244. end.