narmcnv.pas 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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,globals,systems,
  50. symconst,symdef,aasmbase,aasmtai,
  51. defutil,
  52. cgbase,cgutils,
  53. pass_1,pass_2,
  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. { converting a 64bit integer to a float requires a helper }
  66. if is_64bitint(left.resulttype.def) or
  67. is_currency(left.resulttype.def) then
  68. begin
  69. { hack to avoid double division by 10000, as it's
  70. already done by resulttypepass.resulttype_int_to_real }
  71. if is_currency(left.resulttype.def) then
  72. left.resulttype := s64inttype;
  73. if is_signed(left.resulttype.def) then
  74. fname := 'fpc_int64_to_double'
  75. else
  76. fname := 'fpc_qword_to_double';
  77. result := ccallnode.createintern(fname,ccallparanode.create(
  78. left,nil));
  79. left:=nil;
  80. firstpass(result);
  81. exit;
  82. end
  83. else
  84. { other integers are supposed to be 32 bit }
  85. begin
  86. if is_signed(left.resulttype.def) then
  87. inserttypeconv(left,s32inttype)
  88. else
  89. inserttypeconv(left,u32inttype);
  90. firstpass(left);
  91. end;
  92. result := nil;
  93. if registersfpu<1 then
  94. registersfpu:=1;
  95. expectloc:=LOC_FPUREGISTER;
  96. end;
  97. procedure tarmtypeconvnode.second_int_to_real;
  98. var
  99. instr : taicpu;
  100. begin
  101. location_reset(location,LOC_FPUREGISTER,def_cgsize(resulttype.def));
  102. location_force_reg(exprasmlist,left.location,OS_32,true);
  103. location.register:=cg.getfpuregister(exprasmlist,location.size);
  104. instr:=taicpu.op_reg_reg(A_FLT,location.register,left.location.register);
  105. instr.oppostfix:=cgsize2fpuoppostfix[def_cgsize(resulttype.def)];
  106. exprasmlist.concat(instr);
  107. end;
  108. procedure tarmtypeconvnode.second_int_to_bool;
  109. var
  110. hregister : tregister;
  111. href : treference;
  112. resflags : tresflags;
  113. hlabel,oldtruelabel,oldfalselabel : tasmlabel;
  114. begin
  115. oldtruelabel:=truelabel;
  116. oldfalselabel:=falselabel;
  117. objectlibrary.getlabel(truelabel);
  118. objectlibrary.getlabel(falselabel);
  119. secondpass(left);
  120. if codegenerror then
  121. exit;
  122. { byte(boolean) or word(wordbool) or longint(longbool) must
  123. be accepted for var parameters }
  124. if (nf_explicit in flags) and
  125. (left.resulttype.def.size=resulttype.def.size) and
  126. (left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  127. begin
  128. location_copy(location,left.location);
  129. truelabel:=oldtruelabel;
  130. falselabel:=oldfalselabel;
  131. exit;
  132. end;
  133. { Load left node into flag F_NE/F_E }
  134. resflags:=F_NE;
  135. case left.location.loc of
  136. LOC_CREFERENCE,
  137. LOC_REFERENCE :
  138. begin
  139. if left.location.size in [OS_64,OS_S64] then
  140. begin
  141. hregister:=cg.getintregister(exprasmlist,OS_INT);
  142. cg.a_load_ref_reg(exprasmlist,OS_32,OS_32,left.location.reference,hregister);
  143. href:=left.location.reference;
  144. inc(href.offset,4);
  145. tcgarm(cg).cgsetflags:=true;
  146. cg.a_op_ref_reg(exprasmlist,OP_OR,OS_32,href,hregister);
  147. tcgarm(cg).cgsetflags:=false;
  148. end
  149. else
  150. begin
  151. location_force_reg(exprasmlist,left.location,left.location.size,true);
  152. tcgarm(cg).cgsetflags:=true;
  153. cg.a_op_reg_reg(exprasmlist,OP_OR,left.location.size,left.location.register,left.location.register);
  154. tcgarm(cg).cgsetflags:=false;
  155. end;
  156. end;
  157. LOC_FLAGS :
  158. begin
  159. resflags:=left.location.resflags;
  160. end;
  161. LOC_REGISTER,LOC_CREGISTER :
  162. begin
  163. if left.location.size in [OS_64,OS_S64] then
  164. begin
  165. hregister:=cg.getintregister(exprasmlist,OS_32);
  166. cg.a_load_reg_reg(exprasmlist,OS_32,OS_32,left.location.register64.reglo,hregister);
  167. tcgarm(cg).cgsetflags:=true;
  168. cg.a_op_reg_reg(exprasmlist,OP_OR,OS_32,left.location.register64.reghi,hregister);
  169. tcgarm(cg).cgsetflags:=false;
  170. end
  171. else
  172. begin
  173. tcgarm(cg).cgsetflags:=true;
  174. cg.a_op_reg_reg(exprasmlist,OP_OR,left.location.size,left.location.register,left.location.register);
  175. tcgarm(cg).cgsetflags:=false;
  176. end;
  177. end;
  178. LOC_JUMP :
  179. begin
  180. hregister:=cg.getintregister(exprasmlist,OS_INT);
  181. objectlibrary.getlabel(hlabel);
  182. cg.a_label(exprasmlist,truelabel);
  183. cg.a_load_const_reg(exprasmlist,OS_INT,1,hregister);
  184. cg.a_jmp_always(exprasmlist,hlabel);
  185. cg.a_label(exprasmlist,falselabel);
  186. cg.a_load_const_reg(exprasmlist,OS_INT,0,hregister);
  187. cg.a_label(exprasmlist,hlabel);
  188. tcgarm(cg).cgsetflags:=true;
  189. cg.a_op_reg_reg(exprasmlist,OP_OR,OS_INT,hregister,hregister);
  190. tcgarm(cg).cgsetflags:=false;
  191. end;
  192. else
  193. internalerror(200311301);
  194. end;
  195. { load flags to register }
  196. location_reset(location,LOC_REGISTER,def_cgsize(resulttype.def));
  197. location.register:=cg.getintregister(exprasmlist,location.size);
  198. cg.g_flags2reg(exprasmlist,location.size,resflags,location.register);
  199. truelabel:=oldtruelabel;
  200. falselabel:=oldfalselabel;
  201. end;
  202. begin
  203. ctypeconvnode:=tarmtypeconvnode;
  204. end.