nppccnv.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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,defcmp;
  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. end;
  48. implementation
  49. uses
  50. verbose,globals,systems,
  51. symconst,symdef,aasmbase,aasmtai,
  52. defutil,
  53. cgbase,pass_1,pass_2,
  54. ncon,ncal,
  55. ncgutil,
  56. cpubase,aasmcpu,
  57. rgobj,tgobj,cgobj;
  58. {*****************************************************************************
  59. FirstTypeConv
  60. *****************************************************************************}
  61. function tppctypeconvnode.first_int_to_real: tnode;
  62. var
  63. fname: string[19];
  64. begin
  65. { converting a 64bit integer to a float requires a helper }
  66. if is_64bitint(left.resulttype.def) then
  67. begin
  68. if is_signed(left.resulttype.def) then
  69. fname := 'fpc_int64_to_double'
  70. else
  71. fname := 'fpc_qword_to_double';
  72. result := ccallnode.createintern(fname,ccallparanode.create(
  73. left,nil));
  74. left:=nil;
  75. firstpass(result);
  76. exit;
  77. end
  78. else
  79. { other integers are supposed to be 32 bit }
  80. begin
  81. if is_signed(left.resulttype.def) then
  82. inserttypeconv(left,s32bittype)
  83. else
  84. inserttypeconv(left,u32bittype);
  85. firstpass(left);
  86. end;
  87. result := nil;
  88. if registersfpu<1 then
  89. registersfpu:=1;
  90. expectloc:=LOC_FPUREGISTER;
  91. end;
  92. {*****************************************************************************
  93. SecondTypeConv
  94. *****************************************************************************}
  95. procedure tppctypeconvnode.second_int_to_real;
  96. type
  97. tdummyarray = packed array[0..7] of byte;
  98. {$ifdef VER1_0}
  99. var
  100. dummy1, dummy2: int64;
  101. {$else VER1_0}
  102. const
  103. dummy1: int64 = $4330000080000000;
  104. dummy2: int64 = $4330000000000000;
  105. {$endif VER1_0}
  106. var
  107. tempconst: trealconstnode;
  108. ref: treference;
  109. valuereg, tempreg, leftreg, tmpfpureg: tregister;
  110. size: tcgsize;
  111. signed, valuereg_is_scratch: boolean;
  112. begin
  113. {$ifdef VER1_0}
  114. dummy1 := (int64(1) shl 31) or (int64($43300000) shl 32);
  115. dummy2 := int64($43300000) shl 32;
  116. {$endif VER1_0}
  117. valuereg_is_scratch := false;
  118. location_reset(location,LOC_FPUREGISTER,def_cgsize(resulttype.def));
  119. { the code here comes from the PowerPC Compiler Writer's Guide }
  120. { * longint to double }
  121. { addis R0,R0,0x4330 # R0 = 0x43300000 }
  122. { stw R0,disp(R1) # store upper half }
  123. { xoris R3,R3,0x8000 # flip sign bit }
  124. { stw R3,disp+4(R1) # store lower half }
  125. { lfd FR1,disp(R1) # float load double of value }
  126. { fsub FR1,FR1,FR2 # subtract 0x4330000080000000 }
  127. { * cardinal to double }
  128. { addis R0,R0,0x4330 # R0 = 0x43300000 }
  129. { stw R0,disp(R1) # store upper half }
  130. { stw R3,disp+4(R1) # store lower half }
  131. { lfd FR1,disp(R1) # float load double of value }
  132. { fsub FR1,FR1,FR2 # subtract 0x4330000000000000 }
  133. tg.Gettemp(exprasmlist,8,tt_normal,ref);
  134. signed := is_signed(left.resulttype.def);
  135. { we need a certain constant for the conversion, so create it here }
  136. if signed then
  137. tempconst :=
  138. crealconstnode.create(double(tdummyarray(dummy1)),
  139. pbestrealtype^)
  140. else
  141. tempconst :=
  142. crealconstnode.create(double(tdummyarray(dummy2)),
  143. pbestrealtype^);
  144. resulttypepass(tempconst);
  145. firstpass(tempconst);
  146. secondpass(tempconst);
  147. if (tempconst.location.loc <> LOC_CREFERENCE) or
  148. { has to be handled by a helper }
  149. is_64bitint(left.resulttype.def) then
  150. internalerror(200110011);
  151. case left.location.loc of
  152. LOC_REGISTER:
  153. begin
  154. leftreg := left.location.register;
  155. valuereg := leftreg;
  156. end;
  157. LOC_CREGISTER:
  158. begin
  159. leftreg := left.location.register;
  160. if signed then
  161. begin
  162. valuereg := cg.getintregister(exprasmlist,OS_INT);
  163. valuereg_is_scratch := true;
  164. end
  165. else
  166. valuereg := leftreg;
  167. end;
  168. LOC_REFERENCE,LOC_CREFERENCE:
  169. begin
  170. leftreg := cg.getintregister(exprasmlist,OS_INT);
  171. valuereg := leftreg;
  172. valuereg_is_scratch := true;
  173. if signed then
  174. size := OS_S32
  175. else
  176. size := OS_32;
  177. cg.a_load_ref_reg(exprasmlist,def_cgsize(left.resulttype.def),
  178. size,left.location.reference,leftreg);
  179. end
  180. else
  181. internalerror(200110012);
  182. end;
  183. tempreg := cg.getintregister(exprasmlist,OS_INT);
  184. exprasmlist.concat(taicpu.op_reg_const(A_LIS,tempreg,$4330));
  185. cg.a_load_reg_ref(exprasmlist,OS_32,OS_32,tempreg,ref);
  186. cg.ungetregister(exprasmlist,tempreg);
  187. if signed then
  188. exprasmlist.concat(taicpu.op_reg_reg_const(A_XORIS,valuereg,
  189. { xoris expects a unsigned 16 bit int (FK) }
  190. leftreg,$8000));
  191. inc(ref.offset,4);
  192. cg.a_load_reg_ref(exprasmlist,OS_32,OS_32,valuereg,ref);
  193. dec(ref.offset,4);
  194. if (valuereg_is_scratch) then
  195. cg.ungetregister(exprasmlist,valuereg);
  196. if (left.location.loc = LOC_REGISTER) or
  197. ((left.location.loc = LOC_CREGISTER) and
  198. not signed) then
  199. cg.ungetregister(exprasmlist,leftreg)
  200. else
  201. cg.ungetregister(exprasmlist,valuereg);
  202. tmpfpureg := cg.getfpuregister(exprasmlist,OS_F64);
  203. cg.a_loadfpu_ref_reg(exprasmlist,OS_F64,tempconst.location.reference,
  204. tmpfpureg);
  205. tempconst.free;
  206. location.register := cg.getfpuregister(exprasmlist,OS_F64);
  207. exprasmlist.concat(taicpu.op_reg_ref(A_LFD,location.register,
  208. ref));
  209. tg.ungetiftemp(exprasmlist,ref);
  210. exprasmlist.concat(taicpu.op_reg_reg_reg(A_FSUB,location.register,
  211. location.register,tmpfpureg));
  212. cg.ungetregister(exprasmlist,tmpfpureg);
  213. { work around bug in some PowerPC processors }
  214. if (tfloatdef(resulttype.def).typ = s32real) then
  215. exprasmlist.concat(taicpu.op_reg_reg(A_FRSP,location.register,
  216. location.register));
  217. end;
  218. procedure tppctypeconvnode.second_real_to_real;
  219. begin
  220. inherited second_real_to_real;
  221. { work around bug in some powerpc processors where doubles aren't }
  222. { properly converted to singles }
  223. if (tfloatdef(left.resulttype.def).typ = s64real) and
  224. (tfloatdef(resulttype.def).typ = s32real) then
  225. exprasmlist.concat(taicpu.op_reg_reg(A_FRSP,location.register,
  226. location.register));
  227. end;
  228. procedure tppctypeconvnode.second_int_to_bool;
  229. var
  230. hreg1,
  231. hreg2 : tregister;
  232. href : treference;
  233. resflags : tresflags;
  234. opsize : tcgsize;
  235. begin
  236. (*
  237. !!!!!!!!!!!!!!!!!!
  238. Causes problems with "boolvar := boolean(bytevar)" on the ppc
  239. (the conversion isn't done in that case), don't know why it works
  240. on the 80x86 (JM)
  241. { byte(boolean) or word(wordbool) or longint(longbool) must }
  242. { be accepted for var parameters }
  243. if (nf_explicit in flags) and
  244. (left.resulttype.def.size=resulttype.def.size) and
  245. (left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  246. begin
  247. location_copy(location,left.location);
  248. exit;
  249. end;
  250. *)
  251. { Already done in tppctypeconvnode.pass_2! (JM)
  252. secondpass(left); }
  253. if codegenerror then
  254. exit;
  255. location_reset(location,LOC_REGISTER,def_cgsize(resulttype.def));
  256. opsize := def_cgsize(left.resulttype.def);
  257. case left.location.loc of
  258. LOC_CREFERENCE,LOC_REFERENCE,LOC_REGISTER,LOC_CREGISTER :
  259. begin
  260. if left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE] then
  261. begin
  262. reference_release(exprasmlist,left.location.reference);
  263. hreg2:=cg.getintregister(exprasmlist,OS_INT);
  264. if left.location.size in [OS_64,OS_S64] then
  265. begin
  266. cg.a_load_ref_reg(exprasmlist,OS_INT,OS_INT,
  267. left.location.reference,hreg2);
  268. hreg1:=cg.getintregister(exprasmlist,OS_INT);
  269. href:=left.location.reference;
  270. inc(href.offset,4);
  271. cg.a_load_ref_reg(exprasmlist,OS_INT,OS_INT,
  272. href,hreg1);
  273. cg.a_op_reg_reg_reg(exprasmlist,OP_OR,OS_32,hreg2,hreg1,hreg2);
  274. cg.ungetregister(exprasmlist,hreg1);
  275. end
  276. else
  277. cg.a_load_ref_reg(exprasmlist,opsize,opsize,
  278. left.location.reference,hreg2);
  279. end
  280. else
  281. begin
  282. if left.location.size in [OS_64,OS_S64] then
  283. begin
  284. hreg2:=cg.getintregister(exprasmlist,OS_32);
  285. cg.a_op_reg_reg_reg(exprasmlist,OP_OR,OS_32,left.location.registerhigh,left.location.registerlow,hreg2);
  286. location_release(exprasmlist,left.location);
  287. end
  288. else
  289. hreg2 := left.location.register;
  290. end;
  291. hreg1 := cg.getintregister(exprasmlist,OS_INT);
  292. exprasmlist.concat(taicpu.op_reg_reg_const(A_SUBIC,hreg1,
  293. hreg2,1));
  294. exprasmlist.concat(taicpu.op_reg_reg_reg(A_SUBFE,hreg1,hreg1,
  295. hreg2));
  296. cg.ungetregister(exprasmlist,hreg2);
  297. end;
  298. LOC_FLAGS :
  299. begin
  300. hreg1:=cg.getintregister(exprasmlist,OS_INT);
  301. resflags:=left.location.resflags;
  302. cg.g_flags2reg(exprasmlist,location.size,resflags,hreg1);
  303. end;
  304. else
  305. internalerror(10062);
  306. end;
  307. location.register := hreg1;
  308. end;
  309. procedure tppctypeconvnode.pass_2;
  310. {$ifdef TESTOBJEXT2}
  311. var
  312. r : preference;
  313. nillabel : plabel;
  314. {$endif TESTOBJEXT2}
  315. begin
  316. { this isn't good coding, I think tc_bool_2_int, shouldn't be }
  317. { type conversion (FK) }
  318. if not(convtype in [tc_bool_2_int,tc_bool_2_bool]) then
  319. begin
  320. secondpass(left);
  321. location_copy(location,left.location);
  322. if codegenerror then
  323. exit;
  324. end;
  325. second_call_helper(convtype);
  326. end;
  327. begin
  328. ctypeconvnode:=tppctypeconvnode;
  329. end.
  330. {
  331. $Log$
  332. Revision 1.45 2003-11-04 22:30:15 florian
  333. + type cast variant<->enum
  334. * cnv. node second pass uses now as well helper wrappers
  335. Revision 1.44 2003/10/17 01:22:08 florian
  336. * compilation of the powerpc compiler fixed
  337. Revision 1.43 2003/10/01 20:34:49 peter
  338. * procinfo unit contains tprocinfo
  339. * cginfo renamed to cgbase
  340. * moved cgmessage to verbose
  341. * fixed ppc and sparc compiles
  342. Revision 1.42 2003/09/03 19:35:24 peter
  343. * powerpc compiles again
  344. Revision 1.41 2003/08/09 15:28:29 jonas
  345. * fixed conversion from signed value to floats if the compiler is
  346. compiled with a 1.0.x compiler
  347. Revision 1.40 2003/06/14 22:32:43 jonas
  348. * ppc compiles with -dnewra, haven't tried to compile anything with it
  349. yet though
  350. Revision 1.39 2003/06/12 22:09:54 jonas
  351. * tcginnode.pass_2 doesn't call a helper anymore in any case
  352. * fixed ungetregisterfpu compilation problems
  353. Revision 1.38 2003/06/04 11:58:58 jonas
  354. * calculate localsize also in g_return_from_proc since it's now called
  355. before g_stackframe_entry (still have to fix macos)
  356. * compilation fixes (cycle doesn't work yet though)
  357. Revision 1.37 2003/06/01 21:38:06 peter
  358. * getregisterfpu size parameter added
  359. * op_const_reg size parameter added
  360. * sparc updates
  361. Revision 1.36 2003/05/11 20:42:08 jonas
  362. * fixed bug in second_int_to_bool I introduced previous time
  363. (secondpass was being called twice!)
  364. Revision 1.35 2003/05/11 13:06:44 jonas
  365. * fixed second_int_to_bool() (but still problem with typecasts used for
  366. var parameters, not sure about solution)
  367. Revision 1.34 2003/05/02 15:13:38 jonas
  368. * yet another final fix for second_int_to_real() :) (tested this time)
  369. Revision 1.33 2003/04/24 22:29:58 florian
  370. * fixed a lot of PowerPC related stuff
  371. Revision 1.32 2003/04/23 21:10:54 peter
  372. * fix compile for ppc,sparc,m68k
  373. Revision 1.31 2003/04/23 12:35:35 florian
  374. * fixed several issues with powerpc
  375. + applied a patch from Jonas for nested function calls (PowerPC only)
  376. * ...
  377. Revision 1.30 2003/03/11 21:46:24 jonas
  378. * lots of new regallocator fixes, both in generic and ppc-specific code
  379. (ppc compiler still can't compile the linux system unit though)
  380. Revision 1.29 2003/02/19 22:00:16 daniel
  381. * Code generator converted to new register notation
  382. - Horribily outdated todo.txt removed
  383. Revision 1.28 2002/12/05 14:28:13 florian
  384. * some variant <-> dyn. array stuff
  385. Revision 1.27 2002/11/25 17:43:28 peter
  386. * splitted defbase in defutil,symutil,defcmp
  387. * merged isconvertable and is_equal into compare_defs(_ext)
  388. * made operator search faster by walking the list only once
  389. Revision 1.26 2002/10/18 16:38:42 jonas
  390. + added entry for pwchar_to_string conversion addition
  391. Revision 1.25 2002/09/17 18:54:06 jonas
  392. * a_load_reg_reg() now has two size parameters: source and dest. This
  393. allows some optimizations on architectures that don't encode the
  394. register size in the register name.
  395. Revision 1.24 2002/08/23 16:14:50 peter
  396. * tempgen cleanup
  397. * tt_noreuse temp type added that will be used in genentrycode
  398. Revision 1.23 2002/08/18 10:34:30 florian
  399. * more ppc assembling fixes
  400. Revision 1.22 2002/08/14 19:30:42 carl
  401. + added fixing because first_in_to_real is now completely generic
  402. Revision 1.21 2002/08/11 06:14:41 florian
  403. * fixed powerpc compilation problems
  404. Revision 1.20 2002/08/10 17:15:31 jonas
  405. * various fixes and optimizations
  406. Revision 1.19 2002/07/29 21:23:44 florian
  407. * more fixes for the ppc
  408. + wrappers for the tcnvnode.first_* stuff introduced
  409. Revision 1.18 2002/07/29 09:20:20 jonas
  410. + second_int_to_int implementation which is almost the same as the
  411. generic implementation, but it avoids some unnecessary type conversions
  412. Revision 1.17 2002/07/27 19:55:15 jonas
  413. + generic implementation of tcg.g_flags2ref()
  414. * tcg.flags2xxx() now also needs a size parameter
  415. Revision 1.16 2002/07/24 14:38:00 florian
  416. * small typo fixed, compiles with 1.0.x again
  417. Revision 1.15 2002/07/21 16:57:22 jonas
  418. * hopefully final fix for second_int_to_real()
  419. Revision 1.14 2002/07/20 11:58:05 florian
  420. * types.pas renamed to defbase.pas because D6 contains a types
  421. unit so this would conflicts if D6 programms are compiled
  422. + Willamette/SSE2 instructions to assembler added
  423. Revision 1.13 2002/07/13 06:49:39 jonas
  424. * fixed fpu constants in second_int_to_real (fpu values are also stored
  425. in big endian)
  426. Revision 1.12 2002/07/12 22:02:22 florian
  427. * fixed to compile with 1.1
  428. Revision 1.11 2002/07/11 14:41:34 florian
  429. * start of the new generic parameter handling
  430. Revision 1.10 2002/07/11 07:42:31 jonas
  431. * fixed nppccnv and enabled it
  432. - removed PPC specific second_int_to_int and use the generic one instead
  433. Revision 1.9 2002/05/20 13:30:42 carl
  434. * bugfix of hdisponen (base must be set, not index)
  435. * more portability fixes
  436. Revision 1.8 2002/05/18 13:34:26 peter
  437. * readded missing revisions
  438. Revision 1.7 2002/05/16 19:46:53 carl
  439. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  440. + try to fix temp allocation (still in ifdef)
  441. + generic constructor calls
  442. + start of tassembler / tmodulebase class cleanup
  443. Revision 1.5 2002/04/06 18:13:02 jonas
  444. * several powerpc-related additions and fixes
  445. }