ncpucnv.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate SPARC 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. tsparctypeconvnode = 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,globals,systems,globtype,
  48. symconst,symdef,aasmbase,aasmtai,aasmdata,
  49. defutil,
  50. cgbase,cgutils,pass_1,pass_2,
  51. ncon,ncal,procinfo,
  52. ncgutil,
  53. cpubase,aasmcpu,
  54. tgobj,cgobj;
  55. {*****************************************************************************
  56. FirstTypeConv
  57. *****************************************************************************}
  58. function tsparctypeconvnode.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 typecheckpass.resultdef_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 tsparctypeconvnode.second_int_to_real;
  96. procedure loadsigned;
  97. begin
  98. location_force_mem(current_asmdata.CurrAsmList,left.location);
  99. { Load memory in fpu register }
  100. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList,OS_F32,OS_F32,left.location.reference,location.register);
  101. tg.ungetiftemp(current_asmdata.CurrAsmList,left.location.reference);
  102. { Convert value in fpu register from integer to float }
  103. case tfloatdef(resultdef).floattype of
  104. s32real:
  105. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FiTOs,location.register,location.register));
  106. s64real:
  107. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FiTOd,location.register,location.register));
  108. s128real:
  109. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FiTOq,location.register,location.register));
  110. else
  111. internalerror(200408011);
  112. end;
  113. end;
  114. var
  115. href : treference;
  116. hregister : tregister;
  117. l1,l2 : tasmlabel;
  118. begin
  119. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  120. if is_signed(left.resultdef) then
  121. begin
  122. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  123. loadsigned;
  124. end
  125. else
  126. begin
  127. current_asmdata.getdatalabel(l1);
  128. current_asmdata.getjumplabel(l2);
  129. reference_reset_symbol(href,l1,0,8);
  130. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  131. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_32,left.location,hregister);
  132. { here we need always an 64 bit register }
  133. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,OS_F64);
  134. location_force_mem(current_asmdata.CurrAsmList,left.location);
  135. { Load memory in fpu register }
  136. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList,OS_F32,OS_F32,left.location.reference,location.register);
  137. tg.ungetiftemp(current_asmdata.CurrAsmList,left.location.reference);
  138. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FiTOd,location.register,location.register));
  139. current_asmdata.CurrAsmList.concat(Taicpu.op_reg_reg(A_CMP,hregister,NR_G0));
  140. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_GE,l2);
  141. case tfloatdef(resultdef).floattype of
  142. { converting dword to s64real first and cut off at the end avoids precision loss }
  143. s32real,
  144. s64real:
  145. begin
  146. hregister:=cg.getfpuregister(current_asmdata.CurrAsmList,OS_F64);
  147. current_asmdata.asmlists[al_typedconsts].concat(tai_align.create(const_align(8)));
  148. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(l1));
  149. { I got this constant from a test program (FK) }
  150. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_32bit($41f00000));
  151. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_32bit(0));
  152. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList,OS_F64,OS_F64,href,hregister);
  153. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_FADDD,location.register,hregister,location.register));
  154. cg.a_label(current_asmdata.CurrAsmList,l2);
  155. { cut off if we should convert to single }
  156. if tfloatdef(resultdef).floattype=s32real then
  157. begin
  158. hregister:=location.register;
  159. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  160. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FDTOS,hregister,location.register));
  161. end;
  162. end;
  163. else
  164. internalerror(200410031);
  165. end;
  166. end;
  167. end;
  168. (*
  169. procedure tsparctypeconvnode.second_real_to_real;
  170. const
  171. conv_op : array[tfloattype,tfloattype] of tasmop = (
  172. { from: s32 s64 s80 sc80 c64 cur f128 }
  173. { s32 } ( A_FMOVS,A_FDTOS,A_NONE, A_NONE, A_NONE, A_NONE, A_NONE ),
  174. { s64 } ( A_FSTOD,A_FMOVD,A_NONE, A_NONE, A_NONE, A_NONE, A_NONE ),
  175. { s80 } ( A_NONE, A_NONE, A_NONE, A_NONE, A_NONE, A_NONE, A_NONE ),
  176. { sc80 } ( A_NONE, A_NONE, A_NONE, A_NONE, A_NONE, A_NONE, A_NONE ),
  177. { c64 } ( A_NONE, A_NONE, A_NONE, A_NONE, A_NONE, A_NONE, A_NONE ),
  178. { cur } ( A_NONE, A_NONE, A_NONE, A_NONE, A_NONE, A_NONE, A_NONE ),
  179. { f128 } ( A_NONE, A_NONE, A_NONE, A_NONE, A_NONE, A_NONE, A_NONE )
  180. );
  181. var
  182. op : tasmop;
  183. begin
  184. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  185. location_force_fpureg(current_asmdata.CurrAsmList,left.location,false);
  186. { Convert value in fpu register from integer to float }
  187. op:=conv_op[tfloatdef(resultdef).floattype,tfloatdef(left.resultdef).floattype];
  188. if op=A_NONE then
  189. internalerror(200401121);
  190. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  191. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,left.location.register,location.register));
  192. end;
  193. *)
  194. procedure tsparctypeconvnode.second_int_to_bool;
  195. var
  196. href: treference;
  197. hreg1,hreg2 : tregister;
  198. resflags : tresflags;
  199. opsize : tcgsize;
  200. hlabel,oldTrueLabel,oldFalseLabel : tasmlabel;
  201. newsize : tcgsize;
  202. begin
  203. oldTrueLabel:=current_procinfo.CurrTrueLabel;
  204. oldFalseLabel:=current_procinfo.CurrFalseLabel;
  205. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  206. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  207. secondpass(left);
  208. if codegenerror then
  209. exit;
  210. { Explicit typecasts from any ordinal type to a boolean type }
  211. { must not change the ordinal value }
  212. if (nf_explicit in flags) and
  213. not(left.location.loc in [LOC_FLAGS,LOC_JUMP]) then
  214. begin
  215. location_copy(location,left.location);
  216. newsize:=def_cgsize(resultdef);
  217. { change of size? change sign only if location is LOC_(C)REGISTER? Then we have to sign/zero-extend }
  218. if (tcgsize2size[newsize]<>tcgsize2size[left.location.size]) or
  219. ((newsize<>left.location.size) and (location.loc in [LOC_REGISTER,LOC_CREGISTER])) then
  220. location_force_reg(current_asmdata.CurrAsmList,location,newsize,true)
  221. else
  222. location.size:=newsize;
  223. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  224. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  225. exit;
  226. end;
  227. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  228. opsize:=def_cgsize(left.resultdef);
  229. case left.location.loc of
  230. LOC_CREFERENCE,LOC_REFERENCE,LOC_REGISTER,LOC_CREGISTER:
  231. begin
  232. if left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE] then
  233. begin
  234. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  235. {$ifndef cpu64bitalu}
  236. if left.location.size in [OS_64,OS_S64] then
  237. begin
  238. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,left.location.reference,hreg2);
  239. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  240. href:=left.location.reference;
  241. inc(href.offset,4);
  242. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,href,hreg1);
  243. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,hreg1,hreg2,hreg2);
  244. end
  245. else
  246. {$endif not cpu64bitalu}
  247. cg.a_load_ref_reg(current_asmdata.CurrAsmList,opsize,opsize,left.location.reference,hreg2);
  248. end
  249. else
  250. begin
  251. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  252. {$ifndef cpu64bitalu}
  253. if left.location.size in [OS_64,OS_S64] then
  254. begin
  255. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  256. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,left.location.register64.reghi,left.location.register64.reglo,hreg2);
  257. end
  258. else
  259. {$endif not cpu64bitalu}
  260. cg.a_load_reg_reg(current_asmdata.CurrAsmList,opsize,opsize,left.location.register,hreg2);
  261. end;
  262. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  263. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUBCC,NR_G0,hreg2,NR_G0));
  264. if is_pasbool(resultdef) then
  265. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ADDX,NR_G0,NR_G0,hreg1))
  266. else
  267. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUBX,NR_G0,NR_G0,hreg1));
  268. end;
  269. LOC_FLAGS :
  270. begin
  271. hreg1:=cg.GetIntRegister(current_asmdata.CurrAsmList,location.size);
  272. resflags:=left.location.resflags;
  273. cg.g_flags2reg(current_asmdata.CurrAsmList,location.size,resflags,hreg1);
  274. if (is_cbool(resultdef)) then
  275. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,location.size,hreg1,hreg1);
  276. end;
  277. LOC_JUMP :
  278. begin
  279. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  280. current_asmdata.getjumplabel(hlabel);
  281. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  282. if not(is_cbool(resultdef)) then
  283. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,1,hreg1)
  284. else
  285. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,-1,hreg1);
  286. cg.a_jmp_always(current_asmdata.CurrAsmList,hlabel);
  287. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  288. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,0,hreg1);
  289. cg.a_label(current_asmdata.CurrAsmList,hlabel);
  290. end;
  291. else
  292. internalerror(10062);
  293. end;
  294. {$ifndef cpu64bitalu}
  295. if (location.size in [OS_64,OS_S64]) then
  296. begin
  297. location.register64.reglo:=hreg1;
  298. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  299. if (is_cbool(resultdef)) then
  300. { reglo is either 0 or -1 -> reghi has to become the same }
  301. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,location.register64.reglo,location.register64.reghi)
  302. else
  303. { unsigned }
  304. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,location.register64.reghi);
  305. end
  306. else
  307. {$endif not cpu64bitalu}
  308. location.register:=hreg1;
  309. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  310. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  311. end;
  312. begin
  313. ctypeconvnode:=tsparctypeconvnode;
  314. end.