nppccnv.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. ncgutil,
  56. cpubase,aasmcpu,
  57. rgobj,tgobj,cgobj,cginfo;
  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 := inherited first_int_to_real;
  88. end;
  89. {*****************************************************************************
  90. SecondTypeConv
  91. *****************************************************************************}
  92. procedure tppctypeconvnode.second_int_to_int;
  93. var
  94. newsize : tcgsize;
  95. size, leftsize : cardinal;
  96. begin
  97. newsize:=def_cgsize(resulttype.def);
  98. { insert range check if not explicit conversion }
  99. if not(nf_explizit in flags) then
  100. cg.g_rangecheck(exprasmlist,left,resulttype.def);
  101. { is the result size smaller ? }
  102. size := resulttype.def.size;
  103. leftsize := left.resulttype.def.size;
  104. if (size < leftsize) or
  105. (((newsize in [OS_64,OS_S64]) or
  106. (left.location.loc <> LOC_REGISTER)) and
  107. (size > leftsize)) then
  108. begin
  109. { reuse the left location by default }
  110. location_copy(location,left.location);
  111. location_force_reg(exprasmlist,location,newsize,false);
  112. end
  113. else
  114. begin
  115. { no special loading is required, reuse current location }
  116. location_copy(location,left.location);
  117. location.size:=newsize;
  118. end;
  119. end;
  120. procedure tppctypeconvnode.second_int_to_real;
  121. type
  122. tdummyarray = packed array[0..7] of byte;
  123. {$ifdef VER1_0}
  124. var
  125. dummy1, dummy2: int64;
  126. {$else VER1_0}
  127. const
  128. dummy1: int64 = $4330000080000000;
  129. dummy2: int64 = $4330000000000000;
  130. {$endif VER1_0}
  131. var
  132. tempconst: trealconstnode;
  133. ref: treference;
  134. valuereg, tempreg, leftreg, tmpfpureg: tregister;
  135. signed, valuereg_is_scratch: boolean;
  136. begin
  137. {$ifdef VER1_0}
  138. { the "and" is because 1.0.x will sign-extend the $80000000 to }
  139. { $ffffffff80000000 when converting it to int64 (JM) }
  140. dummy1 := int64($80000000) and (int64($43300000) shl 32);
  141. dummy2 := int64($43300000) shl 32;
  142. {$endif VER1_0}
  143. valuereg_is_scratch := false;
  144. location_reset(location,LOC_FPUREGISTER,def_cgsize(resulttype.def));
  145. { the code here comes from the PowerPC Compiler Writer's Guide }
  146. { * longint to double }
  147. { addis R0,R0,0x4330 # R0 = 0x43300000 }
  148. { stw R0,disp(R1) # store upper half }
  149. { xoris R3,R3,0x8000 # flip sign bit }
  150. { stw R3,disp+4(R1) # store lower half }
  151. { lfd FR1,disp(R1) # float load double of value }
  152. { fsub FR1,FR1,FR2 # subtract 0x4330000080000000 }
  153. { * cardinal to double }
  154. { addis R0,R0,0x4330 # R0 = 0x43300000 }
  155. { stw R0,disp(R1) # store upper half }
  156. { stw R3,disp+4(R1) # store lower half }
  157. { lfd FR1,disp(R1) # float load double of value }
  158. { fsub FR1,FR1,FR2 # subtract 0x4330000000000000 }
  159. tg.gettempofsizereference(exprasmlist,8,ref);
  160. signed := is_signed(left.resulttype.def);
  161. { we need a certain constant for the conversion, so create it here }
  162. if signed then
  163. tempconst :=
  164. crealconstnode.create(double(dummy1),
  165. pbestrealtype^)
  166. else
  167. tempconst :=
  168. crealconstnode.create(double(dummy2),
  169. pbestrealtype^);
  170. resulttypepass(tempconst);
  171. firstpass(tempconst);
  172. secondpass(tempconst);
  173. if (tempconst.location.loc <> LOC_CREFERENCE) or
  174. { has to be handled by a helper }
  175. is_64bitint(left.resulttype.def) then
  176. internalerror(200110011);
  177. case left.location.loc of
  178. LOC_REGISTER:
  179. begin
  180. leftreg := left.location.register;
  181. valuereg := leftreg;
  182. end;
  183. LOC_CREGISTER:
  184. begin
  185. leftreg := left.location.register;
  186. if signed then
  187. begin
  188. valuereg := cg.get_scratch_reg_int(exprasmlist);
  189. valuereg_is_scratch := true;
  190. end
  191. else
  192. valuereg := leftreg;
  193. end;
  194. LOC_REFERENCE,LOC_CREFERENCE:
  195. begin
  196. leftreg := cg.get_scratch_reg_int(exprasmlist);
  197. valuereg := leftreg;
  198. valuereg_is_scratch := true;
  199. cg.a_load_ref_reg(exprasmlist,def_cgsize(left.resulttype.def),
  200. left.location.reference,leftreg);
  201. end
  202. else
  203. internalerror(200110012);
  204. end;
  205. tempreg := cg.get_scratch_reg_int(exprasmlist);
  206. exprasmlist.concat(taicpu.op_reg_const(A_LIS,tempreg,$4330));
  207. cg.a_load_reg_ref(exprasmlist,OS_32,tempreg,ref);
  208. cg.free_scratch_reg(exprasmlist,tempreg);
  209. if signed then
  210. exprasmlist.concat(taicpu.op_reg_reg_const(A_XORIS,valuereg,
  211. leftreg,smallint($8000)));
  212. inc(ref.offset,4);
  213. cg.a_load_reg_ref(exprasmlist,OS_32,valuereg,ref);
  214. dec(ref.offset,4);
  215. if (valuereg_is_scratch) then
  216. cg.free_scratch_reg(exprasmlist,valuereg);
  217. if (left.location.loc = LOC_REGISTER) or
  218. ((left.location.loc = LOC_CREGISTER) and
  219. not signed) then
  220. rg.ungetregister(exprasmlist,leftreg)
  221. else
  222. cg.free_scratch_reg(exprasmlist,valuereg);
  223. tmpfpureg := rg.getregisterfpu(exprasmlist);
  224. cg.a_loadfpu_ref_reg(exprasmlist,OS_F64,tempconst.location.reference,
  225. tmpfpureg);
  226. tempconst.free;
  227. location.register := rg.getregisterfpu(exprasmlist);
  228. exprasmlist.concat(taicpu.op_reg_ref(A_LFD,location.register,
  229. ref));
  230. tg.ungetiftemp(exprasmlist,ref);
  231. exprasmlist.concat(taicpu.op_reg_reg_reg(A_FSUB,location.register,
  232. location.register,tmpfpureg));
  233. rg.ungetregisterfpu(exprasmlist,tmpfpureg);
  234. { work around bug in some PowerPC processors }
  235. if (tfloatdef(resulttype.def).typ = s32real) then
  236. exprasmlist.concat(taicpu.op_reg_reg(A_FRSP,location.register,
  237. location.register));
  238. end;
  239. procedure tppctypeconvnode.second_real_to_real;
  240. begin
  241. inherited second_real_to_real;
  242. { work around bug in some powerpc processors where doubles aren't }
  243. { properly converted to singles }
  244. if (tfloatdef(left.resulttype.def).typ = s64real) and
  245. (tfloatdef(resulttype.def).typ = s32real) then
  246. exprasmlist.concat(taicpu.op_reg_reg(A_FRSP,location.register,
  247. location.register));
  248. end;
  249. procedure tppctypeconvnode.second_int_to_bool;
  250. var
  251. hreg1,
  252. hreg2 : tregister;
  253. resflags : tresflags;
  254. opsize : tcgsize;
  255. begin
  256. { byte(boolean) or word(wordbool) or longint(longbool) must }
  257. { be accepted for var parameters }
  258. if (nf_explizit in flags) and
  259. (left.resulttype.def.size=resulttype.def.size) and
  260. (left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  261. begin
  262. location_copy(location,left.location);
  263. exit;
  264. end;
  265. location_reset(location,LOC_REGISTER,def_cgsize(left.resulttype.def));
  266. opsize := def_cgsize(left.resulttype.def);
  267. case left.location.loc of
  268. LOC_CREFERENCE,LOC_REFERENCE,LOC_REGISTER,LOC_CREGISTER :
  269. begin
  270. if left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE] then
  271. begin
  272. reference_release(exprasmlist,left.location.reference);
  273. hreg2:=rg.getregisterint(exprasmlist);
  274. cg.a_load_ref_reg(exprasmlist,opsize,
  275. left.location.reference,hreg2);
  276. end
  277. else
  278. hreg2 := left.location.register;
  279. hreg1 := rg.getregisterint(exprasmlist);
  280. exprasmlist.concat(taicpu.op_reg_reg_const(A_SUBIC,hreg1,
  281. hreg2,1));
  282. exprasmlist.concat(taicpu.op_reg_reg_reg(A_SUBFE,hreg1,hreg1,
  283. hreg2));
  284. rg.ungetregister(exprasmlist,hreg2);
  285. end;
  286. LOC_FLAGS :
  287. begin
  288. hreg1:=rg.getregisterint(exprasmlist);
  289. resflags:=left.location.resflags;
  290. cg.g_flags2reg(exprasmlist,location.size,resflags,hreg1);
  291. end;
  292. else
  293. internalerror(10062);
  294. end;
  295. location.register := hreg1;
  296. end;
  297. procedure tppctypeconvnode.second_call_helper(c : tconverttype);
  298. const
  299. secondconvert : array[tconverttype] of pointer = (
  300. @second_nothing, {equal}
  301. @second_nothing, {not_possible}
  302. @second_nothing, {second_string_to_string, handled in resulttype pass }
  303. @second_char_to_string,
  304. @second_nothing, {char_to_charray}
  305. @second_nothing, { pchar_to_string, handled in resulttype pass }
  306. @second_nothing, {cchar_to_pchar}
  307. @second_cstring_to_pchar,
  308. @second_ansistring_to_pchar,
  309. @second_string_to_chararray,
  310. @second_nothing, { chararray_to_string, handled in resulttype pass }
  311. @second_array_to_pointer,
  312. @second_pointer_to_array,
  313. @second_int_to_int,
  314. @second_int_to_bool,
  315. @second_bool_to_int, { bool_to_bool }
  316. @second_bool_to_int,
  317. @second_real_to_real,
  318. @second_int_to_real,
  319. @second_proc_to_procvar,
  320. @second_nothing, { arrayconstructor_to_set }
  321. @second_nothing, { second_load_smallset, handled in first pass }
  322. @second_cord_to_pointer,
  323. @second_nothing, { interface 2 string }
  324. @second_nothing, { interface 2 guid }
  325. @second_class_to_intf,
  326. @second_char_to_char,
  327. @second_nothing, { normal_2_smallset }
  328. @second_nothing { dynarray_2_openarray }
  329. );
  330. type
  331. tprocedureofobject = procedure of object;
  332. var
  333. r : packed record
  334. proc : pointer;
  335. obj : pointer;
  336. end;
  337. begin
  338. { this is a little bit dirty but it works }
  339. { and should be quite portable too }
  340. r.proc:=secondconvert[c];
  341. r.obj:=self;
  342. tprocedureofobject(r){$ifdef FPC}();{$endif FPC}
  343. end;
  344. procedure tppctypeconvnode.pass_2;
  345. {$ifdef TESTOBJEXT2}
  346. var
  347. r : preference;
  348. nillabel : plabel;
  349. {$endif TESTOBJEXT2}
  350. begin
  351. { this isn't good coding, I think tc_bool_2_int, shouldn't be }
  352. { type conversion (FK) }
  353. if not(convtype in [tc_bool_2_int,tc_bool_2_bool]) then
  354. begin
  355. secondpass(left);
  356. location_copy(location,left.location);
  357. if codegenerror then
  358. exit;
  359. end;
  360. second_call_helper(convtype);
  361. end;
  362. begin
  363. ctypeconvnode:=tppctypeconvnode;
  364. end.
  365. {
  366. $Log$
  367. Revision 1.21 2002-08-11 06:14:41 florian
  368. * fixed powerpc compilation problems
  369. Revision 1.20 2002/08/10 17:15:31 jonas
  370. * various fixes and optimizations
  371. Revision 1.19 2002/07/29 21:23:44 florian
  372. * more fixes for the ppc
  373. + wrappers for the tcnvnode.first_* stuff introduced
  374. Revision 1.18 2002/07/29 09:20:20 jonas
  375. + second_int_to_int implementation which is almost the same as the
  376. generic implementation, but it avoids some unnecessary type conversions
  377. Revision 1.17 2002/07/27 19:55:15 jonas
  378. + generic implementation of tcg.g_flags2ref()
  379. * tcg.flags2xxx() now also needs a size parameter
  380. Revision 1.16 2002/07/24 14:38:00 florian
  381. * small typo fixed, compiles with 1.0.x again
  382. Revision 1.15 2002/07/21 16:57:22 jonas
  383. * hopefully final fix for second_int_to_real()
  384. Revision 1.14 2002/07/20 11:58:05 florian
  385. * types.pas renamed to defbase.pas because D6 contains a types
  386. unit so this would conflicts if D6 programms are compiled
  387. + Willamette/SSE2 instructions to assembler added
  388. Revision 1.13 2002/07/13 06:49:39 jonas
  389. * fixed fpu constants in second_int_to_real (fpu values are also stored
  390. in big endian)
  391. Revision 1.12 2002/07/12 22:02:22 florian
  392. * fixed to compile with 1.1
  393. Revision 1.11 2002/07/11 14:41:34 florian
  394. * start of the new generic parameter handling
  395. Revision 1.10 2002/07/11 07:42:31 jonas
  396. * fixed nppccnv and enabled it
  397. - removed PPC specific second_int_to_int and use the generic one instead
  398. Revision 1.9 2002/05/20 13:30:42 carl
  399. * bugfix of hdisponen (base must be set, not index)
  400. * more portability fixes
  401. Revision 1.8 2002/05/18 13:34:26 peter
  402. * readded missing revisions
  403. Revision 1.7 2002/05/16 19:46:53 carl
  404. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  405. + try to fix temp allocation (still in ifdef)
  406. + generic constructor calls
  407. + start of tassembler / tmodulebase class cleanup
  408. Revision 1.5 2002/04/06 18:13:02 jonas
  409. * several powerpc-related additions and fixes
  410. }