nppccnv.pas 20 KB

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