nppccnv.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Generate PowerPC assembler for type converting nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit nppccnv;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,ncnv,ncgcnv,defbase;
  23. type
  24. tppctypeconvnode = class(tcgtypeconvnode)
  25. protected
  26. { procedure second_int_to_int;override; }
  27. { procedure second_string_to_string;override; }
  28. { procedure second_cstring_to_pchar;override; }
  29. { procedure second_string_to_chararray;override; }
  30. { procedure second_array_to_pointer;override; }
  31. function first_int_to_real: tnode; override;
  32. { procedure second_pointer_to_array;override; }
  33. { procedure second_chararray_to_string;override; }
  34. { procedure second_char_to_string;override; }
  35. procedure second_int_to_real;override;
  36. procedure second_real_to_real;override;
  37. { procedure second_cord_to_pointer;override; }
  38. { procedure second_proc_to_procvar;override; }
  39. { procedure second_bool_to_int;override; }
  40. procedure second_int_to_bool;override;
  41. { procedure second_load_smallset;override; }
  42. { procedure second_ansistring_to_pchar;override; }
  43. { procedure second_pchar_to_string;override; }
  44. { procedure second_class_to_intf;override; }
  45. { procedure second_char_to_char;override; }
  46. procedure pass_2;override;
  47. procedure second_call_helper(c : tconverttype); override;
  48. end;
  49. implementation
  50. uses
  51. verbose,globals,systems,
  52. symconst,symdef,aasmbase,aasmtai,
  53. cgbase,pass_1,pass_2,
  54. ncon,ncal,
  55. cpubase,aasmcpu,
  56. rgobj,tgobj,cgobj,cginfo;
  57. {*****************************************************************************
  58. FirstTypeConv
  59. *****************************************************************************}
  60. function tppctypeconvnode.first_int_to_real: tnode;
  61. var
  62. fname: string[19];
  63. begin
  64. { converting a 64bit integer to a float requires a helper }
  65. if is_64bitint(left.resulttype.def) then
  66. begin
  67. if is_signed(left.resulttype.def) then
  68. fname := 'fpc_int64_to_double'
  69. else
  70. fname := 'fpc_qword_to_double';
  71. result := ccallnode.createintern(fname,ccallparanode.create(
  72. left,nil));
  73. firstpass(result);
  74. exit;
  75. end
  76. else
  77. { other integers are supposed to be 32 bit }
  78. begin
  79. if is_signed(left.resulttype.def) then
  80. inserttypeconv(left,s32bittype)
  81. else
  82. inserttypeconv(left,u32bittype);
  83. firstpass(left);
  84. end;
  85. result := inherited first_int_to_real;
  86. end;
  87. {*****************************************************************************
  88. SecondTypeConv
  89. *****************************************************************************}
  90. procedure tppctypeconvnode.second_int_to_real;
  91. type
  92. tdummyarray = packed array[0..7] of byte;
  93. {$ifdef VER1_0}
  94. var
  95. dummy1, dummy2: int64;
  96. {$else VER1_0}
  97. const
  98. dummy1: int64 = $4330000080000000;
  99. dummy2: int64 = $4330000000000000;
  100. {$endif VER1_0}
  101. var
  102. tempconst: trealconstnode;
  103. ref: treference;
  104. valuereg, tempreg, leftreg, tmpfpureg: tregister;
  105. signed, valuereg_is_scratch: boolean;
  106. begin
  107. {$ifdef VER1_0}
  108. { the "and" is because 1.0.x will sign-extend the $80000000 to }
  109. { $ffffffff80000000 when converting it to int64 (JM) }
  110. dummy1 := int64($80000000) and (int64($43300000) shl 32);
  111. dymmy2 := int64($43300000) shl 32;
  112. {$endif VER1_0}
  113. valuereg_is_scratch := false;
  114. location_reset(location,LOC_FPUREGISTER,def_cgsize(resulttype.def));
  115. { the code here comes from the PowerPC Compiler Writer's Guide }
  116. { * longint to double }
  117. { addis R0,R0,0x4330 # R0 = 0x43300000 }
  118. { stw R0,disp(R1) # store upper half }
  119. { xoris R3,R3,0x8000 # flip sign bit }
  120. { stw R3,disp+4(R1) # store lower half }
  121. { lfd FR1,disp(R1) # float load double of value }
  122. { fsub FR1,FR1,FR2 # subtract 0x4330000080000000 }
  123. { * cardinal to double }
  124. { addis R0,R0,0x4330 # R0 = 0x43300000 }
  125. { stw R0,disp(R1) # store upper half }
  126. { stw R3,disp+4(R1) # store lower half }
  127. { lfd FR1,disp(R1) # float load double of value }
  128. { fsub FR1,FR1,FR2 # subtract 0x4330000000000000 }
  129. tg.gettempofsizereference(exprasmlist,8,ref);
  130. signed := is_signed(left.resulttype.def);
  131. { we need a certain constant for the conversion, so create it here }
  132. if signed then
  133. tempconst :=
  134. crealconstnode.create(double(dummy1),
  135. pbestrealtype^)
  136. else
  137. tempconst :=
  138. crealconstnode.create(double(dummy2),
  139. pbestrealtype^);
  140. resulttypepass(tempconst);
  141. firstpass(tempconst);
  142. secondpass(tempconst);
  143. if (tempconst.location.loc <> LOC_CREFERENCE) or
  144. { has to be handled by a helper }
  145. is_64bitint(left.resulttype.def) then
  146. internalerror(200110011);
  147. case left.location.loc of
  148. LOC_REGISTER:
  149. begin
  150. leftreg := left.location.register;
  151. valuereg := leftreg;
  152. end;
  153. LOC_CREGISTER:
  154. begin
  155. leftreg := left.location.register;
  156. if signed then
  157. begin
  158. valuereg := cg.get_scratch_reg_int(exprasmlist);
  159. valuereg_is_scratch := true;
  160. end
  161. else
  162. valuereg := leftreg;
  163. end;
  164. LOC_REFERENCE,LOC_CREFERENCE:
  165. begin
  166. leftreg := cg.get_scratch_reg_int(exprasmlist);
  167. valuereg := leftreg;
  168. valuereg_is_scratch := true;
  169. cg.a_load_ref_reg(exprasmlist,def_cgsize(left.resulttype.def),
  170. left.location.reference,leftreg);
  171. end
  172. else
  173. internalerror(200110012);
  174. end;
  175. tempreg := cg.get_scratch_reg_int(exprasmlist);
  176. exprasmlist.concat(taicpu.op_reg_const(A_LIS,tempreg,$4330));
  177. cg.a_load_reg_ref(exprasmlist,OS_32,tempreg,ref);
  178. cg.free_scratch_reg(exprasmlist,tempreg);
  179. if signed then
  180. exprasmlist.concat(taicpu.op_reg_reg_const(A_XORIS,valuereg,
  181. leftreg,smallint($8000)));
  182. inc(ref.offset,4);
  183. cg.a_load_reg_ref(exprasmlist,OS_32,valuereg,ref);
  184. dec(ref.offset,4);
  185. if (valuereg_is_scratch) then
  186. cg.free_scratch_reg(exprasmlist,valuereg);
  187. if (left.location.loc = LOC_REGISTER) or
  188. ((left.location.loc = LOC_CREGISTER) and
  189. not signed) then
  190. rg.ungetregister(exprasmlist,leftreg)
  191. else
  192. cg.free_scratch_reg(exprasmlist,valuereg);
  193. tmpfpureg := rg.getregisterfpu(exprasmlist);
  194. exprasmlist.concat(taicpu.op_reg_ref(A_LFD,tmpfpureg,
  195. tempconst.location.reference));
  196. tempconst.free;
  197. location.register := rg.getregisterfpu(exprasmlist);
  198. exprasmlist.concat(taicpu.op_reg_ref(A_LFD,location.register,
  199. ref));
  200. tg.ungetiftemp(exprasmlist,ref);
  201. exprasmlist.concat(taicpu.op_reg_reg_reg(A_FSUB,location.register,
  202. location.register,tmpfpureg));
  203. rg.ungetregisterfpu(exprasmlist,tmpfpureg);
  204. { work around bug in some PowerPC processors }
  205. if (tfloatdef(resulttype.def).typ = s32real) then
  206. exprasmlist.concat(taicpu.op_reg_reg(A_FRSP,location.register,
  207. location.register));
  208. end;
  209. procedure tppctypeconvnode.second_real_to_real;
  210. begin
  211. inherited second_real_to_real;
  212. { work around bug in some powerpc processors where doubles aren't }
  213. { properly converted to singles }
  214. if (tfloatdef(left.resulttype.def).typ = s64real) and
  215. (tfloatdef(resulttype.def).typ = s32real) then
  216. exprasmlist.concat(taicpu.op_reg_reg(A_FRSP,location.register,
  217. location.register));
  218. end;
  219. procedure tppctypeconvnode.second_int_to_bool;
  220. var
  221. hreg1,
  222. hreg2 : tregister;
  223. resflags : tresflags;
  224. opsize : tcgsize;
  225. begin
  226. { byte(boolean) or word(wordbool) or longint(longbool) must }
  227. { be accepted for var parameters }
  228. if (nf_explizit in flags) and
  229. (left.resulttype.def.size=resulttype.def.size) and
  230. (left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  231. begin
  232. location_copy(location,left.location);
  233. exit;
  234. end;
  235. location_reset(location,LOC_REGISTER,def_cgsize(left.resulttype.def));
  236. opsize := def_cgsize(left.resulttype.def);
  237. case left.location.loc of
  238. LOC_CREFERENCE,LOC_REFERENCE,LOC_REGISTER,LOC_CREGISTER :
  239. begin
  240. if left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE] then
  241. begin
  242. reference_release(exprasmlist,left.location.reference);
  243. hreg2:=rg.getregisterint(exprasmlist);
  244. cg.a_load_ref_reg(exprasmlist,opsize,
  245. left.location.reference,hreg2);
  246. end
  247. else
  248. hreg2 := left.location.register;
  249. hreg1 := rg.getregisterint(exprasmlist);
  250. exprasmlist.concat(taicpu.op_reg_reg_const(A_SUBIC,hreg1,
  251. hreg2,1));
  252. exprasmlist.concat(taicpu.op_reg_reg_reg(A_SUBFE,hreg1,hreg1,
  253. hreg2));
  254. rg.ungetregister(exprasmlist,hreg2);
  255. end;
  256. LOC_FLAGS :
  257. begin
  258. hreg1:=rg.getregisterint(exprasmlist);
  259. resflags:=left.location.resflags;
  260. cg.g_flags2reg(exprasmlist,resflags,hreg1);
  261. end;
  262. else
  263. internalerror(10062);
  264. end;
  265. location.register := hreg1;
  266. end;
  267. procedure tppctypeconvnode.second_call_helper(c : tconverttype);
  268. const
  269. secondconvert : array[tconverttype] of pointer = (
  270. @second_nothing, {equal}
  271. @second_nothing, {not_possible}
  272. @second_nothing, {second_string_to_string, handled in resulttype pass }
  273. @second_char_to_string,
  274. @second_nothing, {char_to_charray}
  275. @second_nothing, { pchar_to_string, handled in resulttype pass }
  276. @second_nothing, {cchar_to_pchar}
  277. @second_cstring_to_pchar,
  278. @second_ansistring_to_pchar,
  279. @second_string_to_chararray,
  280. @second_nothing, { chararray_to_string, handled in resulttype pass }
  281. @second_array_to_pointer,
  282. @second_pointer_to_array,
  283. @second_int_to_int,
  284. @second_int_to_bool,
  285. @second_bool_to_int, { bool_to_bool }
  286. @second_bool_to_int,
  287. @second_real_to_real,
  288. @second_int_to_real,
  289. @second_proc_to_procvar,
  290. @second_nothing, { arrayconstructor_to_set }
  291. @second_nothing, { second_load_smallset, handled in first pass }
  292. @second_cord_to_pointer,
  293. @second_nothing, { interface 2 string }
  294. @second_nothing, { interface 2 guid }
  295. @second_class_to_intf,
  296. @second_char_to_char,
  297. @second_nothing, { normal_2_smallset }
  298. @second_nothing { dynarray_2_openarray }
  299. );
  300. type
  301. tprocedureofobject = procedure of object;
  302. var
  303. r : packed record
  304. proc : pointer;
  305. obj : pointer;
  306. end;
  307. begin
  308. { this is a little bit dirty but it works }
  309. { and should be quite portable too }
  310. r.proc:=secondconvert[c];
  311. r.obj:=self;
  312. tprocedureofobject(r){$ifdef FPC}();{$endif FPC}
  313. end;
  314. procedure tppctypeconvnode.pass_2;
  315. {$ifdef TESTOBJEXT2}
  316. var
  317. r : preference;
  318. nillabel : plabel;
  319. {$endif TESTOBJEXT2}
  320. begin
  321. { this isn't good coding, I think tc_bool_2_int, shouldn't be }
  322. { type conversion (FK) }
  323. if not(convtype in [tc_bool_2_int,tc_bool_2_bool]) then
  324. begin
  325. secondpass(left);
  326. location_copy(location,left.location);
  327. if codegenerror then
  328. exit;
  329. end;
  330. second_call_helper(convtype);
  331. end;
  332. begin
  333. ctypeconvnode:=tppctypeconvnode;
  334. end.
  335. {
  336. $Log$
  337. Revision 1.15 2002-07-21 16:57:22 jonas
  338. * hopefully final fix for second_int_to_real()
  339. Revision 1.14 2002/07/20 11:58:05 florian
  340. * types.pas renamed to defbase.pas because D6 contains a types
  341. unit so this would conflicts if D6 programms are compiled
  342. + Willamette/SSE2 instructions to assembler added
  343. Revision 1.13 2002/07/13 06:49:39 jonas
  344. * fixed fpu constants in second_int_to_real (fpu values are also stored
  345. in big endian)
  346. Revision 1.12 2002/07/12 22:02:22 florian
  347. * fixed to compile with 1.1
  348. Revision 1.11 2002/07/11 14:41:34 florian
  349. * start of the new generic parameter handling
  350. Revision 1.10 2002/07/11 07:42:31 jonas
  351. * fixed nppccnv and enabled it
  352. - removed PPC specific second_int_to_int and use the generic one instead
  353. Revision 1.9 2002/05/20 13:30:42 carl
  354. * bugfix of hdisponen (base must be set, not index)
  355. * more portability fixes
  356. Revision 1.8 2002/05/18 13:34:26 peter
  357. * readded missing revisions
  358. Revision 1.7 2002/05/16 19:46:53 carl
  359. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  360. + try to fix temp allocation (still in ifdef)
  361. + generic constructor calls
  362. + start of tassembler / tmodulebase class cleanup
  363. Revision 1.5 2002/04/06 18:13:02 jonas
  364. * several powerpc-related additions and fixes
  365. }