nppccnv.pas 21 KB

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