ncpucnv.pas 13 KB

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