ncpucnv.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl and David Zhang
  3. Generate MIPSEL 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. unit ncpucnv;
  17. {$i fpcdefs.inc}
  18. interface
  19. uses
  20. node, ncnv, ncgcnv, defcmp;
  21. type
  22. tMIPSELtypeconvnode = class(TCgTypeConvNode)
  23. protected
  24. { procedure second_int_to_int;override; }
  25. { procedure second_string_to_string;override; }
  26. { procedure second_cstring_to_pchar;override; }
  27. { procedure second_string_to_chararray;override; }
  28. { procedure second_array_to_pointer;override; }
  29. function first_int_to_real: tnode; override;
  30. { procedure second_pointer_to_array;override; }
  31. { procedure second_chararray_to_string;override; }
  32. { procedure second_char_to_string;override; }
  33. procedure second_int_to_real; override;
  34. procedure second_real_to_real; override;
  35. { procedure second_cord_to_pointer;override; }
  36. { procedure second_proc_to_procvar;override; }
  37. { procedure second_bool_to_int;override; }
  38. procedure second_int_to_bool; override;
  39. { procedure second_load_smallset;override; }
  40. { procedure second_ansistring_to_pchar;override; }
  41. { procedure second_pchar_to_string;override; }
  42. { procedure second_class_to_intf;override; }
  43. { procedure second_char_to_char;override; }
  44. end;
  45. implementation
  46. uses
  47. verbose, globtype, globals, systems,
  48. symconst, symdef, aasmbase, aasmtai, aasmdata,
  49. defutil,
  50. cgbase, cgutils, pass_1, pass_2, procinfo,
  51. ncon, ncal,
  52. ncgutil,
  53. cpubase, aasmcpu,
  54. tgobj, cgobj;
  55. {*****************************************************************************
  56. FirstTypeConv
  57. *****************************************************************************}
  58. function tMIPSELtypeconvnode.first_int_to_real: tnode;
  59. var
  60. fname: string[19];
  61. begin
  62. { converting a 64bit integer to a float requires a helper }
  63. if is_64bitint(left.resultdef) or
  64. is_currency(left.resultdef) then
  65. begin
  66. { hack to avoid double division by 10000, as it's
  67. already done by resulttypepass.resulttype_int_to_real }
  68. if is_currency(left.resultdef) then
  69. left.resultdef := s64inttype;
  70. if is_signed(left.resultdef) then
  71. fname := 'fpc_int64_to_double'
  72. else
  73. fname := 'fpc_qword_to_double';
  74. Result := ccallnode.createintern(fname, ccallparanode.Create(
  75. left, nil));
  76. left := nil;
  77. firstpass(Result);
  78. exit;
  79. end
  80. else
  81. { other integers are supposed to be 32 bit }
  82. begin
  83. if is_signed(left.resultdef) then
  84. inserttypeconv(left, s32inttype)
  85. else
  86. inserttypeconv(left, u32inttype);
  87. firstpass(left);
  88. end;
  89. Result := nil;
  90. expectloc := LOC_FPUREGISTER;
  91. end;
  92. {*****************************************************************************
  93. SecondTypeConv
  94. *****************************************************************************}
  95. procedure tMIPSELtypeconvnode.second_int_to_real;
  96. procedure loadsigned;
  97. begin
  98. location_force_mem(current_asmdata.CurrAsmList, left.location);
  99. location.Register := cg.getfpuregister(current_asmdata.CurrAsmList, location.size);
  100. { Load memory in fpu register }
  101. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList, OS_F32, OS_F32, left.location.reference, location.Register);
  102. tg.ungetiftemp(current_asmdata.CurrAsmList, left.location.reference);
  103. { Convert value in fpu register from integer to float }
  104. case tfloatdef(resultdef).floattype of
  105. s32real:
  106. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CVT_S_W, location.Register, location.Register));
  107. s64real:
  108. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CVT_D_W, location.Register, location.Register));
  109. else
  110. internalerror(200408011);
  111. end;
  112. end;
  113. var
  114. href: treference;
  115. hregister: tregister;
  116. l1, l2: tasmlabel;
  117. begin
  118. location_reset(location, LOC_FPUREGISTER, def_cgsize(resultdef));
  119. if is_signed(left.resultdef) then
  120. loadsigned
  121. else
  122. begin
  123. current_asmdata.getdatalabel(l1);
  124. current_asmdata.getjumplabel(l2);
  125. reference_reset_symbol(href, l1, 0, sizeof(aint));
  126. hregister := cg.getintregister(current_asmdata.CurrAsmList, OS_32);
  127. cg.a_load_loc_reg(current_asmdata.CurrAsmList, OS_32, left.location, hregister);
  128. loadsigned;
  129. current_asmdata.CurrAsmList.concat(Taicpu.op_reg_reg_sym(A_BGE, hregister, NR_R0, l2));
  130. case tfloatdef(resultdef).floattype of
  131. { converting dword to s64real first and cut off at the end avoids precision loss }
  132. s32real,
  133. s64real:
  134. begin
  135. hregister := cg.getfpuregister(current_asmdata.CurrAsmList, OS_F64);
  136. current_asmdata.asmlists[al_typedconsts].concat(tai_align.Create(const_align(8)));
  137. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(l1));
  138. { I got this constant from a test program (FK) }
  139. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_32bit(0));
  140. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_32bit($0000f041));
  141. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList, OS_F64, OS_F64, href, hregister);
  142. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ADD_D, location.Register, hregister, location.Register));
  143. cg.a_label(current_asmdata.CurrAsmList, l2);
  144. { cut off if we should convert to single }
  145. if tfloatdef(resultdef).floattype = s32real then
  146. begin
  147. hregister := location.Register;
  148. location.Register := cg.getfpuregister(current_asmdata.CurrAsmList, location.size);
  149. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CVT_S_D, location.Register, hregister));
  150. end;
  151. end;
  152. else
  153. internalerror(200410031);
  154. end;
  155. end;
  156. end;
  157. procedure tMIPSELtypeconvnode.second_real_to_real;
  158. const
  159. conv_op: array[tfloattype, tfloattype] of tasmop = (
  160. { from: s32 s64 s80 sc80 c64 cur f128 }
  161. { s32 } (A_MOV_S, A_CVT_S_D, A_NONE, A_NONE, A_NONE, A_NONE, A_NONE),
  162. { s64 } (A_CVT_D_S, A_MOV_D, A_NONE, A_NONE, A_NONE, A_NONE, A_NONE),
  163. { s80 } (A_NONE, A_NONE, A_NONE, A_NONE, A_NONE, A_NONE, A_NONE),
  164. { sc80 } (A_NONE, A_NONE, A_NONE, A_NONE, A_NONE, A_NONE, A_NONE),
  165. { c64 } (A_NONE, A_NONE, A_NONE, A_NONE, A_NONE, A_NONE, A_NONE),
  166. { cur } (A_NONE, A_NONE, A_NONE, A_NONE, A_NONE, A_NONE, A_NONE),
  167. { f128 } (A_NONE, A_NONE, A_NONE, A_NONE, A_NONE, A_NONE, A_NONE)
  168. );
  169. var
  170. op: tasmop;
  171. begin
  172. location_reset(location, LOC_FPUREGISTER, def_cgsize(resultdef));
  173. location_force_fpureg(current_asmdata.CurrAsmList, left.location, False);
  174. { Convert value in fpu register from integer to float }
  175. op := conv_op[tfloatdef(resultdef).floattype, tfloatdef(left.resultdef).floattype];
  176. if op = A_NONE then
  177. internalerror(200401121);
  178. location.Register := cg.getfpuregister(current_asmdata.CurrAsmList, location.size);
  179. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op, location.Register, left.location.Register));
  180. end;
  181. procedure tMIPSELtypeconvnode.second_int_to_bool;
  182. var
  183. hreg1, hreg2: tregister;
  184. opsize: tcgsize;
  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. location_reset(location, LOC_REGISTER, def_cgsize(resultdef));
  206. opsize := def_cgsize(left.resultdef);
  207. case left.location.loc of
  208. LOC_CREFERENCE, LOC_REFERENCE, LOC_REGISTER, LOC_CREGISTER:
  209. begin
  210. if left.location.loc in [LOC_CREFERENCE, LOC_REFERENCE] then
  211. begin
  212. hreg2 := cg.getintregister(current_asmdata.CurrAsmList, opsize);
  213. cg.a_load_ref_reg(current_asmdata.CurrAsmList, opsize, opsize, left.location.reference, hreg2);
  214. end
  215. else
  216. hreg2 := left.location.Register;
  217. {$ifndef cpu64bit}
  218. if left.location.size in [OS_64, OS_S64] then
  219. begin
  220. hreg1 := cg.getintregister(current_asmdata.CurrAsmList, OS_32);
  221. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_OR, OS_32, hreg2, tregister(succ(longint(hreg2))), hreg1);
  222. hreg2 := hreg1;
  223. opsize := OS_32;
  224. end;
  225. {$endif cpu64bit}
  226. hreg1 := cg.getintregister(current_asmdata.CurrAsmList, opsize);
  227. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SNE, hreg1, hreg2, NR_R0));
  228. end;
  229. LOC_JUMP:
  230. begin
  231. hreg1 := cg.getintregister(current_asmdata.CurrAsmList, OS_INT);
  232. current_asmdata.getjumplabel(hlabel);
  233. cg.a_label(current_asmdata.CurrAsmList, current_procinfo.CurrTrueLabel);
  234. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_INT, 1, hreg1);
  235. cg.a_jmp_always(current_asmdata.CurrAsmList, hlabel);
  236. cg.a_label(current_asmdata.CurrAsmList, current_procinfo.CurrFalseLabel);
  237. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_INT, 0, hreg1);
  238. cg.a_label(current_asmdata.CurrAsmList, hlabel);
  239. end;
  240. else
  241. internalerror(10062);
  242. end;
  243. location.Register := hreg1;
  244. if location.size in [OS_64, OS_S64] then
  245. internalerror(200408241);
  246. current_procinfo.CurrTrueLabel := oldtruelabel;
  247. current_procinfo.CurrFalseLabel := oldfalselabel;
  248. end;
  249. begin
  250. ctypeconvnode := tMIPSELtypeconvnode;
  251. end.