ncpucnv.pas 13 KB

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