nppccnv.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate PowerPC 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. }
  17. unit nppccnv;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,ncnv,ncgcnv,defcmp;
  22. type
  23. tppctypeconvnode = class(tcgtypeconvnode)
  24. protected
  25. { procedure second_int_to_int;override; }
  26. { procedure second_string_to_string;override; }
  27. { procedure second_cstring_to_pchar;override; }
  28. { procedure second_string_to_chararray;override; }
  29. { procedure second_array_to_pointer;override; }
  30. function first_int_to_real: tnode; override;
  31. { procedure second_pointer_to_array;override; }
  32. { procedure second_chararray_to_string;override; }
  33. { procedure second_char_to_string;override; }
  34. procedure second_int_to_real;override;
  35. procedure second_real_to_real;override;
  36. { procedure second_cord_to_pointer;override; }
  37. { procedure second_proc_to_procvar;override; }
  38. { procedure second_bool_to_int;override; }
  39. procedure second_int_to_bool;override;
  40. { procedure second_load_smallset;override; }
  41. { procedure second_ansistring_to_pchar;override; }
  42. { procedure second_pchar_to_string;override; }
  43. { procedure second_class_to_intf;override; }
  44. { procedure second_char_to_char;override; }
  45. end;
  46. implementation
  47. uses
  48. verbose,globtype,globals,systems,
  49. symconst,symdef,aasmbase,aasmtai,aasmdata,
  50. defutil,
  51. cgbase,cgutils,pass_1,pass_2,
  52. ncon,ncal,
  53. ncgutil,procinfo,
  54. cpubase,aasmcpu,
  55. rgobj,tgobj,cgobj;
  56. {*****************************************************************************
  57. FirstTypeConv
  58. *****************************************************************************}
  59. function tppctypeconvnode.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. firstpass(result);
  79. exit;
  80. end
  81. else
  82. { other integers are supposed to be 32 bit }
  83. begin
  84. if is_signed(left.resultdef) then
  85. inserttypeconv(left,s32inttype)
  86. else
  87. inserttypeconv(left,u32inttype);
  88. firstpass(left);
  89. end;
  90. result := nil;
  91. if registersfpu<1 then
  92. registersfpu:=1;
  93. expectloc:=LOC_FPUREGISTER;
  94. end;
  95. {*****************************************************************************
  96. SecondTypeConv
  97. *****************************************************************************}
  98. procedure tppctypeconvnode.second_int_to_real;
  99. type
  100. tdummyarray = packed array[0..7] of byte;
  101. const
  102. dummy1: int64 = $4330000080000000;
  103. dummy2: int64 = $4330000000000000;
  104. var
  105. tempconst: trealconstnode;
  106. ref: treference;
  107. valuereg, tempreg, leftreg, tmpfpureg: tregister;
  108. size: tcgsize;
  109. signed : boolean;
  110. begin
  111. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  112. { the code here comes from the PowerPC Compiler Writer's Guide }
  113. { * longint to double }
  114. { addis R0,R0,0x4330 # R0 = 0x43300000 }
  115. { stw R0,disp(R1) # store upper half }
  116. { xoris R3,R3,0x8000 # flip sign bit }
  117. { stw R3,disp+4(R1) # store lower half }
  118. { lfd FR1,disp(R1) # float load double of value }
  119. { fsub FR1,FR1,FR2 # subtract 0x4330000080000000 }
  120. { * cardinal to double }
  121. { addis R0,R0,0x4330 # R0 = 0x43300000 }
  122. { stw R0,disp(R1) # store upper half }
  123. { stw R3,disp+4(R1) # store lower half }
  124. { lfd FR1,disp(R1) # float load double of value }
  125. { fsub FR1,FR1,FR2 # subtract 0x4330000000000000 }
  126. tg.Gettemp(current_asmdata.CurrAsmList,8,tt_normal,ref);
  127. signed := is_signed(left.resultdef);
  128. { we need a certain constant for the conversion, so create it here }
  129. if signed then
  130. tempconst :=
  131. crealconstnode.create(double(tdummyarray(dummy1)),
  132. pbestrealtype^)
  133. else
  134. tempconst :=
  135. crealconstnode.create(double(tdummyarray(dummy2)),
  136. pbestrealtype^);
  137. typecheckpass(tempconst);
  138. firstpass(tempconst);
  139. secondpass(tempconst);
  140. if (tempconst.location.loc <> LOC_CREFERENCE) or
  141. { has to be handled by a helper }
  142. is_64bitint(left.resultdef) then
  143. internalerror(200110011);
  144. case left.location.loc of
  145. LOC_REGISTER:
  146. begin
  147. leftreg := left.location.register;
  148. valuereg := leftreg;
  149. end;
  150. LOC_CREGISTER:
  151. begin
  152. leftreg := left.location.register;
  153. if signed then
  154. valuereg := cg.getintregister(current_asmdata.CurrAsmList,OS_INT)
  155. else
  156. valuereg := leftreg;
  157. end;
  158. LOC_REFERENCE,LOC_CREFERENCE:
  159. begin
  160. leftreg := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  161. valuereg := leftreg;
  162. if signed then
  163. size := OS_S32
  164. else
  165. size := OS_32;
  166. cg.a_load_ref_reg(current_asmdata.CurrAsmList,def_cgsize(left.resultdef),
  167. size,left.location.reference,leftreg);
  168. end
  169. else
  170. internalerror(200110012);
  171. end;
  172. tempreg := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  173. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_LIS,tempreg,$4330));
  174. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_32,OS_32,tempreg,ref);
  175. if signed then
  176. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_const(A_XORIS,valuereg,
  177. { xoris expects a unsigned 16 bit int (FK) }
  178. leftreg,$8000));
  179. inc(ref.offset,4);
  180. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_32,OS_32,valuereg,ref);
  181. dec(ref.offset,4);
  182. tmpfpureg := cg.getfpuregister(current_asmdata.CurrAsmList,OS_F64);
  183. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList,OS_F64,tempconst.location.reference,
  184. tmpfpureg);
  185. tempconst.free;
  186. location.register := cg.getfpuregister(current_asmdata.CurrAsmList,OS_F64);
  187. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList,OS_F64,ref,location.register);
  188. tg.ungetiftemp(current_asmdata.CurrAsmList,ref);
  189. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_FSUB,location.register,
  190. location.register,tmpfpureg));
  191. { work around bug in some PowerPC processors }
  192. if (tfloatdef(resultdef).typ = s32real) then
  193. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FRSP,location.register,
  194. location.register));
  195. end;
  196. procedure tppctypeconvnode.second_real_to_real;
  197. begin
  198. inherited second_real_to_real;
  199. { work around bug in some powerpc processors where doubles aren't }
  200. { properly converted to singles }
  201. if (tfloatdef(left.resultdef).typ = s64real) and
  202. (tfloatdef(resultdef).typ = s32real) then
  203. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FRSP,location.register,
  204. location.register));
  205. end;
  206. procedure tppctypeconvnode.second_int_to_bool;
  207. var
  208. hreg1,
  209. hreg2 : tregister;
  210. href : treference;
  211. resflags : tresflags;
  212. opsize : tcgsize;
  213. hlabel, oldTrueLabel, oldFalseLabel : tasmlabel;
  214. begin
  215. oldTrueLabel:=current_procinfo.CurrTrueLabel;
  216. oldFalseLabel:=current_procinfo.CurrFalseLabel;
  217. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  218. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  219. secondpass(left);
  220. if codegenerror then
  221. exit;
  222. { byte(boolean) or word(wordbool) or longint(longbool) must }
  223. { be accepted for var parameters }
  224. if (nf_explicit in flags) and
  225. (left.resultdef.size=resultdef.size) and
  226. (left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  227. begin
  228. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  229. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  230. location_copy(location,left.location);
  231. exit;
  232. end;
  233. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  234. opsize := def_cgsize(left.resultdef);
  235. case left.location.loc of
  236. LOC_CREFERENCE,LOC_REFERENCE,LOC_REGISTER,LOC_CREGISTER :
  237. begin
  238. if left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE] then
  239. begin
  240. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  241. if left.location.size in [OS_64,OS_S64] then
  242. begin
  243. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,left.location.reference,hreg1);
  244. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  245. href:=left.location.reference;
  246. inc(href.offset,4);
  247. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,href,hreg2);
  248. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,hreg1,hreg2,hreg1);
  249. end
  250. else
  251. cg.a_load_ref_reg(current_asmdata.CurrAsmList,opsize,opsize,left.location.reference,hreg1);
  252. end
  253. else
  254. begin
  255. if left.location.size in [OS_64,OS_S64] then
  256. begin
  257. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  258. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,left.location.register64.reghi,left.location.register64.reglo,hreg1);
  259. end
  260. else
  261. hreg1 := left.location.register;
  262. end;
  263. hreg2 := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  264. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_const(A_SUBIC,hreg2,hreg1,1));
  265. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUBFE,hreg1,hreg2,hreg1));
  266. end;
  267. LOC_FLAGS :
  268. begin
  269. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  270. resflags:=left.location.resflags;
  271. cg.g_flags2reg(current_asmdata.CurrAsmList,location.size,resflags,hreg1);
  272. end;
  273. LOC_JUMP :
  274. begin
  275. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  276. current_asmdata.getjumplabel(hlabel);
  277. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  278. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,1,hreg1);
  279. cg.a_jmp_always(current_asmdata.CurrAsmList,hlabel);
  280. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  281. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,0,hreg1);
  282. cg.a_label(current_asmdata.CurrAsmList,hlabel);
  283. end;
  284. else
  285. internalerror(10062);
  286. end;
  287. location.register := hreg1;
  288. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  289. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  290. end;
  291. begin
  292. ctypeconvnode:=tppctypeconvnode;
  293. end.