ncpucnv.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. hlcgobj;
  56. {*****************************************************************************
  57. FirstTypeConv
  58. *****************************************************************************}
  59. function tmipseltypeconvnode.first_int_to_real: tnode;
  60. var
  61. fname: string[19];
  62. begin
  63. if cs_fp_emulation in current_settings.moduleswitches then
  64. begin
  65. result:=inherited;
  66. exit;
  67. end;
  68. { converting a 64bit integer to a float requires a helper }
  69. if is_64bitint(left.resultdef) or
  70. is_currency(left.resultdef) then
  71. begin
  72. { hack to avoid double division by 10000, as it's
  73. already done by typecheckpass.resultdef_int_to_real }
  74. if is_currency(left.resultdef) then
  75. left.resultdef := s64inttype;
  76. if is_signed(left.resultdef) then
  77. fname := 'fpc_int64_to_double'
  78. else
  79. fname := 'fpc_qword_to_double';
  80. result := ccallnode.createintern(fname,ccallparanode.create(
  81. left,nil));
  82. left:=nil;
  83. if (tfloatdef(resultdef).floattype=s32real) then
  84. inserttypeconv(result,s32floattype);
  85. firstpass(result);
  86. exit;
  87. end
  88. else
  89. { other integers are supposed to be 32 bit }
  90. begin
  91. if is_signed(left.resultdef) then
  92. inserttypeconv(left,s32inttype)
  93. else
  94. begin
  95. inserttypeconv(left,u32inttype);
  96. if (cs_create_pic in current_settings.moduleswitches) then
  97. include(current_procinfo.flags,pi_needs_got);
  98. end;
  99. firstpass(left);
  100. end;
  101. result := nil;
  102. expectloc:=LOC_FPUREGISTER;
  103. end;
  104. {*****************************************************************************
  105. SecondTypeConv
  106. *****************************************************************************}
  107. procedure tMIPSELtypeconvnode.second_int_to_real;
  108. procedure loadsigned(restype: tfloattype);
  109. begin
  110. location.Register := cg.getfpuregister(current_asmdata.CurrAsmList, tfloat2tcgsize[restype]);
  111. if (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  112. { 32-bit values can be loaded directly }
  113. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_MTC1, left.location.register, location.register))
  114. else
  115. begin
  116. { Load memory in fpu register }
  117. hlcg.location_force_mem(current_asmdata.CurrAsmList, left.location, left.resultdef);
  118. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList, OS_F32, OS_F32, left.location.reference, location.Register);
  119. tg.ungetiftemp(current_asmdata.CurrAsmList, left.location.reference);
  120. end;
  121. { Convert value in fpu register from integer to float }
  122. case restype of
  123. s32real:
  124. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CVT_S_W, location.Register, location.Register));
  125. s64real:
  126. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CVT_D_W, location.Register, location.Register));
  127. else
  128. internalerror(200408011);
  129. end;
  130. end;
  131. var
  132. href: treference;
  133. hregister: tregister;
  134. l1, l2: tasmlabel;
  135. begin
  136. location_reset(location, LOC_FPUREGISTER, def_cgsize(resultdef));
  137. if is_signed(left.resultdef) then
  138. loadsigned(tfloatdef(resultdef).floattype)
  139. else
  140. begin
  141. current_asmdata.getglobaldatalabel(l1);
  142. current_asmdata.getjumplabel(l2);
  143. reference_reset_symbol(href, l1, 0, sizeof(aint), []);
  144. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  145. { Always load into 64-bit FPU register }
  146. loadsigned(s64real);
  147. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_INT, OC_GTE, 0, left.location.register, l2);
  148. case tfloatdef(resultdef).floattype of
  149. { converting dword to s64real first and cut off at the end avoids precision loss }
  150. s32real,
  151. s64real:
  152. begin
  153. hregister := cg.getfpuregister(current_asmdata.CurrAsmList, OS_F64);
  154. new_section(current_asmdata.asmlists[al_typedconsts],sec_rodata_norel,l1.name,const_align(8));
  155. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(l1));
  156. current_asmdata.asmlists[al_typedconsts].concat(tai_realconst.create_s64real(4294967296.0));
  157. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList, OS_F64, OS_F64, href, hregister);
  158. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ADD_D, location.Register, hregister, location.Register));
  159. cg.a_label(current_asmdata.CurrAsmList, l2);
  160. { cut off if we should convert to single }
  161. if tfloatdef(resultdef).floattype = s32real then
  162. begin
  163. hregister := location.Register;
  164. location.Register := cg.getfpuregister(current_asmdata.CurrAsmList, location.size);
  165. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CVT_S_D, location.Register, hregister));
  166. end;
  167. end;
  168. else
  169. internalerror(2004100306);
  170. end;
  171. end;
  172. end;
  173. procedure tMIPSELtypeconvnode.second_int_to_bool;
  174. var
  175. hreg1, hreg2: tregister;
  176. opsize: tcgsize;
  177. hlabel: tasmlabel;
  178. newsize : tcgsize;
  179. href: treference;
  180. begin
  181. secondpass(left);
  182. if codegenerror then
  183. exit;
  184. { Explicit typecasts from any ordinal type to a boolean type }
  185. { must not change the ordinal value }
  186. if (nf_explicit in flags) and
  187. not(left.location.loc in [LOC_FLAGS,LOC_JUMP]) then
  188. begin
  189. location_copy(location,left.location);
  190. newsize:=def_cgsize(resultdef);
  191. { change of size? change sign only if location is LOC_(C)REGISTER? Then we have to sign/zero-extend }
  192. if (tcgsize2size[newsize]<>tcgsize2size[left.location.size]) or
  193. ((newsize<>left.location.size) and (location.loc in [LOC_REGISTER,LOC_CREGISTER])) then
  194. hlcg.location_force_reg(current_asmdata.CurrAsmList,location,left.resultdef,resultdef,true)
  195. else
  196. location.size:=newsize;
  197. exit;
  198. end;
  199. location_reset(location, LOC_REGISTER, def_cgsize(resultdef));
  200. opsize := def_cgsize(left.resultdef);
  201. if (left.location.loc in [LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF]) then
  202. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  203. case left.location.loc of
  204. LOC_CREFERENCE, LOC_REFERENCE, LOC_REGISTER, LOC_CREGISTER:
  205. begin
  206. if left.location.loc in [LOC_CREFERENCE, LOC_REFERENCE] then
  207. begin
  208. hreg2 := cg.getintregister(current_asmdata.CurrAsmList, opsize);
  209. {$ifndef cpu64bitalu}
  210. if left.location.size in [OS_64,OS_S64] then
  211. begin
  212. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,left.location.reference,hreg2);
  213. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  214. href:=left.location.reference;
  215. inc(href.offset,4);
  216. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,href,hreg1);
  217. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,hreg1,hreg2,hreg2);
  218. end
  219. else
  220. {$endif not cpu64bitalu}
  221. cg.a_load_ref_reg(current_asmdata.CurrAsmList, opsize, opsize, left.location.reference, hreg2);
  222. end
  223. else
  224. begin
  225. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  226. {$ifndef cpu64bitalu}
  227. if left.location.size in [OS_64,OS_S64] then
  228. begin
  229. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  230. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,left.location.register64.reghi,left.location.register64.reglo,hreg2);
  231. end
  232. else
  233. {$endif not cpu64bitalu}
  234. cg.a_load_reg_reg(current_asmdata.CurrAsmList,opsize,opsize,left.location.register,hreg2);
  235. end;
  236. hreg1 := cg.getintregister(current_asmdata.CurrAsmList, opsize);
  237. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SLTU, hreg1, NR_R0, hreg2));
  238. end;
  239. LOC_JUMP:
  240. begin
  241. hreg1 := cg.getintregister(current_asmdata.CurrAsmList, OS_INT);
  242. current_asmdata.getjumplabel(hlabel);
  243. cg.a_label(current_asmdata.CurrAsmList, left.location.truelabel);
  244. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_INT, 1, hreg1);
  245. cg.a_jmp_always(current_asmdata.CurrAsmList, hlabel);
  246. cg.a_label(current_asmdata.CurrAsmList, left.location.falselabel);
  247. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_INT, 0, hreg1);
  248. cg.a_label(current_asmdata.CurrAsmList, hlabel);
  249. end;
  250. LOC_FLAGS:
  251. begin
  252. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  253. cg.g_flags2reg(current_asmdata.CurrAsmList,OS_INT,left.location.resflags,hreg1);
  254. end
  255. else
  256. internalerror(10062);
  257. end;
  258. { Now hreg1 is either 0 or 1. For C booleans it must be 0 or -1. }
  259. if is_cbool(resultdef) then
  260. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,OS_SINT,hreg1,hreg1);
  261. {$ifndef cpu64bitalu}
  262. if (location.size in [OS_64,OS_S64]) then
  263. begin
  264. location.register64.reglo:=hreg1;
  265. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  266. if (is_cbool(resultdef)) then
  267. { reglo is either 0 or -1 -> reghi has to become the same }
  268. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,location.register64.reglo,location.register64.reghi)
  269. else
  270. { unsigned }
  271. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,location.register64.reghi);
  272. end
  273. else
  274. {$endif not cpu64bitalu}
  275. location.Register := hreg1;
  276. end;
  277. begin
  278. ctypeconvnode := tMIPSELtypeconvnode;
  279. end.