ncpucnv.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. { $Id$
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate SPARC assembler for type converting nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************}
  16. unit ncpucnv;
  17. {$i fpcdefs.inc}
  18. interface
  19. uses
  20. node,ncnv,ncgcnv,defcmp;
  21. type
  22. TSparcTypeConvNode = class(TCgTypeConvNode)
  23. protected
  24. { procedure second_int_to_int;override; }
  25. { procedure second_string_to_string;override; }
  26. { procedure second_cstring_to_pchar;override; }
  27. { procedure second_string_to_chararray;override; }
  28. { procedure second_array_to_pointer;override; }
  29. function first_int_to_real: tnode; override;
  30. { procedure second_pointer_to_array;override; }
  31. { procedure second_chararray_to_string;override; }
  32. { procedure second_char_to_string;override; }
  33. procedure second_int_to_real;override;
  34. procedure second_real_to_real;override;
  35. { procedure second_cord_to_pointer;override; }
  36. { procedure second_proc_to_procvar;override; }
  37. { procedure second_bool_to_int;override; }
  38. procedure second_int_to_bool;override;
  39. { procedure second_load_smallset;override; }
  40. { procedure second_ansistring_to_pchar;override; }
  41. { procedure second_pchar_to_string;override; }
  42. { procedure second_class_to_intf;override; }
  43. { procedure second_char_to_char;override; }
  44. procedure pass_2;override;
  45. procedure second_call_helper(c : tconverttype); 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,cginfo;
  57. {*****************************************************************************
  58. FirstTypeConv
  59. *****************************************************************************}
  60. function TSparctypeconvnode.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. left:=nil;
  74. firstpass(result);
  75. exit;
  76. end
  77. else
  78. { other integers are supposed to be 32 bit }
  79. begin
  80. if is_signed(left.resulttype.def) then
  81. inserttypeconv(left,s32bittype)
  82. else
  83. inserttypeconv(left,u32bittype);
  84. firstpass(left);
  85. end;
  86. result := nil;
  87. if registersfpu<1 then
  88. registersfpu:=1;
  89. location.loc:=LOC_FPUREGISTER;
  90. end;
  91. {*****************************************************************************
  92. SecondTypeConv
  93. *****************************************************************************}
  94. procedure TSparctypeconvnode.second_int_to_real;
  95. type
  96. tdummyarray = packed array[0..7] of byte;
  97. {$ifdef VER1_0}
  98. var
  99. dummy1, dummy2: int64;
  100. {$else VER1_0}
  101. const
  102. dummy1: int64 = $4330000080000000;
  103. dummy2: int64 = $4330000000000000;
  104. {$endif VER1_0}
  105. var
  106. tempconst: trealconstnode;
  107. ref: treference;
  108. valuereg, tempreg, leftreg, tmpfpureg: tregister;
  109. signed, valuereg_is_scratch: boolean;
  110. begin
  111. {$ifdef VER1_0}
  112. { the "and" is because 1.0.x will sign-extend the $80000000 to }
  113. { $ffffffff80000000 when converting it to int64 (JM) }
  114. dummy1 := int64($80000000) and (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
  137. then
  138. tempconst:=crealconstnode.create(double(dummy1),pbestrealtype^)
  139. else
  140. tempconst:=crealconstnode.create(double(dummy2),pbestrealtype^);
  141. resulttypepass(tempconst);
  142. firstpass(tempconst);
  143. secondpass(tempconst);
  144. if (tempconst.location.loc <> LOC_CREFERENCE)or
  145. { has to be handled by a helper }
  146. is_64bitint(left.resulttype.def)
  147. then
  148. internalerror(200110011);
  149. case left.location.loc of
  150. LOC_REGISTER:
  151. begin
  152. leftreg := left.location.register;
  153. valuereg := leftreg;
  154. end;
  155. LOC_CREGISTER:
  156. begin
  157. leftreg := left.location.register;
  158. if signed
  159. then
  160. begin
  161. valuereg := cg.get_scratch_reg_int(exprasmlist,OS_INT);
  162. valuereg_is_scratch := true;
  163. end
  164. else
  165. valuereg := leftreg;
  166. end;
  167. LOC_REFERENCE,LOC_CREFERENCE:
  168. begin
  169. leftreg := cg.get_scratch_reg_int(exprasmlist,OS_INT);
  170. valuereg := leftreg;
  171. valuereg_is_scratch := true;
  172. cg.a_load_ref_reg(exprasmlist,def_cgsize(left.resulttype.def),
  173. left.location.reference,leftreg);
  174. end
  175. else
  176. internalerror(200110012);
  177. end;
  178. tempreg := cg.get_scratch_reg_int(exprasmlist,OS_32);
  179. {$WARNING FIXME what really should be done?}
  180. exprasmlist.concat(taicpu.op_reg_const_reg(A_OR,tempreg,$4330,tempreg));
  181. cg.a_load_reg_ref(exprasmlist,OS_32,tempreg,ref);
  182. cg.free_scratch_reg(exprasmlist,tempreg);
  183. if signed
  184. then
  185. {$WARNING FIXME what really should be done?}
  186. exprasmlist.concat(taicpu.op_reg_const_reg(A_XOR,leftreg,$8000,valuereg));
  187. inc(ref.offset,4);
  188. cg.a_load_reg_ref(exprasmlist,OS_32,valuereg,ref);
  189. dec(ref.offset,4);
  190. if (valuereg_is_scratch)
  191. then
  192. cg.free_scratch_reg(exprasmlist,valuereg);
  193. if(left.location.loc = LOC_REGISTER) or
  194. ((left.location.loc = LOC_CREGISTER) and not signed)
  195. then
  196. rg.UnGetRegisterInt(exprasmlist,leftreg)
  197. else
  198. cg.free_scratch_reg(exprasmlist,valuereg);
  199. tmpfpureg := rg.getregisterfpu(exprasmlist);
  200. cg.a_loadfpu_ref_reg(exprasmlist,OS_F64,tempconst.location.reference,tmpfpureg);
  201. tempconst.free;
  202. location.register := rg.getregisterfpu(exprasmlist);
  203. {$WARNING FIXME what really should be done?}
  204. exprasmlist.concat(taicpu.op_Ref_Reg(A_LDF,Ref,location.register));
  205. tg.ungetiftemp(exprasmlist,ref);
  206. exprasmlist.concat(taicpu.op_reg_reg_reg(A_SUB,location.register,location.register,tmpfpureg));
  207. rg.ungetregisterfpu(exprasmlist,tmpfpureg);
  208. { work around bug in some PowerPC processors }
  209. if (tfloatdef(resulttype.def).typ = s32real)
  210. then
  211. {$WARNING FIXME what really should be done?}
  212. exprasmlist.concat(taicpu.op_reg_reg(A_ADD,location.register,location.register));
  213. end;
  214. procedure TSparctypeconvnode.second_real_to_real;
  215. begin
  216. inherited second_real_to_real;
  217. { work around bug in some powerpc processors where doubles aren't }
  218. { properly converted to singles }
  219. if(tfloatdef(left.resulttype.def).typ = s64real)and
  220. (tfloatdef(resulttype.def).typ = s32real)
  221. then
  222. {$WARNING FIXME what really should be done?}
  223. exprasmlist.concat(taicpu.op_reg_reg(A_ADD,location.register,location.register));
  224. end;
  225. procedure TSparctypeconvnode.second_int_to_bool;
  226. var
  227. hreg1,hreg2:tregister;
  228. resflags : tresflags;
  229. opsize : tcgsize;
  230. begin
  231. { byte(boolean) or word(wordbool) or longint(longbool) must }
  232. { be accepted for var parameters }
  233. if(nf_explicit in flags)and
  234. (left.resulttype.def.size=resulttype.def.size)and
  235. (left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER])
  236. then
  237. begin
  238. location_copy(location,left.location);
  239. exit;
  240. end;
  241. location_reset(location,LOC_REGISTER,def_cgsize(left.resulttype.def));
  242. opsize := def_cgsize(left.resulttype.def);
  243. case left.location.loc of
  244. LOC_CREFERENCE,LOC_REFERENCE,LOC_REGISTER,LOC_CREGISTER:
  245. begin
  246. if left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]
  247. then
  248. begin
  249. reference_release(exprasmlist,left.location.reference);
  250. hreg2:=rg.getregisterint(exprasmlist,opsize);
  251. cg.a_load_ref_reg(exprasmlist,opsize,left.location.reference,hreg2);
  252. end
  253. else
  254. hreg2 := left.location.register;
  255. hreg1 := rg.getregisterint(exprasmlist,opsize);
  256. exprasmlist.concat(taicpu.op_reg_const_reg(A_SUB,hreg1,1,hreg2));
  257. exprasmlist.concat(taicpu.op_reg_reg_reg(A_SUB,hreg1,hreg1,hreg2));
  258. rg.UnGetRegisterInt(exprasmlist,hreg2);
  259. end;
  260. LOC_FLAGS :
  261. begin
  262. hreg1:=rg.getregisterint(exprasmlist,location.size);
  263. resflags:=left.location.resflags;
  264. cg.g_flags2reg(exprasmlist,location.size,resflags,hreg1);
  265. end;
  266. else
  267. internalerror(10062);
  268. end;
  269. location.register := hreg1;
  270. end;
  271. procedure TSparctypeconvnode.second_call_helper(c : tconverttype);
  272. const
  273. secondconvert : array[tconverttype] of pointer = (
  274. @second_nothing, {equal}
  275. @second_nothing, {not_possible}
  276. @second_nothing, {second_string_to_string, handled in resulttype pass }
  277. @second_char_to_string,
  278. @second_nothing, {char_to_charray}
  279. @second_nothing, { pchar_to_string, handled in resulttype pass }
  280. @second_nothing, {cchar_to_pchar}
  281. @second_cstring_to_pchar,
  282. @second_ansistring_to_pchar,
  283. @second_string_to_chararray,
  284. @second_nothing, { chararray_to_string, handled in resulttype pass }
  285. @second_array_to_pointer,
  286. @second_pointer_to_array,
  287. @second_int_to_int,
  288. @second_int_to_bool,
  289. @second_bool_to_int, { bool_to_bool }
  290. @second_bool_to_int,
  291. @second_real_to_real,
  292. @second_int_to_real,
  293. @second_nothing, { currency_to_real, handled in resulttype pass }
  294. @second_proc_to_procvar,
  295. @second_nothing, { arrayconstructor_to_set }
  296. @second_nothing, { second_load_smallset, handled in first pass }
  297. @second_cord_to_pointer,
  298. @second_nothing, { interface 2 string }
  299. @second_nothing, { interface 2 guid }
  300. @second_class_to_intf,
  301. @second_char_to_char,
  302. @second_nothing, { normal_2_smallset }
  303. @second_nothing, { dynarray_2_openarray }
  304. @second_nothing,
  305. {$ifdef fpc}@{$endif}second_nothing, { variant_2_dynarray }
  306. {$ifdef fpc}@{$endif}second_nothing { dynarray_2_variant}
  307. );
  308. type
  309. tprocedureofobject = procedure of object;
  310. var
  311. r:packed record
  312. proc : pointer;
  313. obj : pointer;
  314. end;
  315. begin
  316. { this is a little bit dirty but it works }
  317. { and should be quite portable too }
  318. r.proc:=secondconvert[c];
  319. r.obj:=self;
  320. tprocedureofobject(r){$ifdef FPC}();{$endif FPC}
  321. end;
  322. procedure TSparctypeconvnode.pass_2;
  323. {$ifdef TESTOBJEXT2}
  324. var
  325. r : preference;
  326. nillabel : plabel;
  327. {$endif TESTOBJEXT2}
  328. begin
  329. { this isn't good coding, I think tc_bool_2_int, shouldn't be }
  330. { type conversion (FK) }
  331. if not(convtype in [tc_bool_2_int,tc_bool_2_bool])
  332. then
  333. begin
  334. secondpass(left);
  335. location_copy(location,left.location);
  336. if codegenerror
  337. then
  338. exit;
  339. end;
  340. second_call_helper(convtype);
  341. end;
  342. begin
  343. ctypeconvnode:=TSparctypeconvnode;
  344. end.
  345. {
  346. $Log$
  347. Revision 1.15 2003-04-23 21:10:54 peter
  348. * fix compile for ppc,sparc,m68k
  349. Revision 1.14 2003/04/23 13:35:39 peter
  350. * fix sparc compile
  351. Revision 1.13 2003/03/10 21:59:54 mazen
  352. * fixing index overflow in handling new registers arrays.
  353. Revision 1.12 2003/02/19 22:00:17 daniel
  354. * Code generator converted to new register notation
  355. - Horribily outdated todo.txt removed
  356. Revision 1.11 2003/01/22 20:45:15 mazen
  357. * making math code in RTL compiling.
  358. *NB : This does NOT mean necessary that it will generate correct code!
  359. Revision 1.10 2003/01/20 22:21:36 mazen
  360. * many stuff related to RTL fixed
  361. Revision 1.9 2002/12/05 14:28:03 florian
  362. * some variant <-> dyn. array stuff
  363. Revision 1.8 2002/11/25 17:43:28 peter
  364. * splitted defbase in defutil,symutil,defcmp
  365. * merged isconvertable and is_equal into compare_defs(_ext)
  366. * made operator search faster by walking the list only once
  367. Revision 1.7 2002/11/10 19:07:46 mazen
  368. * SPARC calling mechanism almost OK (as in GCC./mppcsparc )
  369. Revision 1.6 2002/11/06 11:31:24 mazen
  370. * op_reg_reg_reg don't need any more a TOpSize parameter
  371. Revision 1.5 2002/10/22 13:43:01 mazen
  372. - cga.pas redueced to an empty unit
  373. Revision 1.4 2002/10/10 19:57:52 mazen
  374. * Just to update repsitory
  375. Revision 1.3 2002/09/07 15:25:14 peter
  376. * old logs removed and tabs fixed
  377. Revision 1.2 2002/08/30 06:15:27 mazen
  378. ncgcall.pas moved to ncpucall.pas (I'd like ncpu* insteade of nsparc* since it
  379. provides processor independent units naming)
  380. Revision 1.1 2002/08/29 10:16:20 mazen
  381. File added support to the new generic parameter handling
  382. Revision 1.24 2002/08/23 16:14:50 peter
  383. * tempgen cleanup
  384. * tt_noreuse temp type added that will be used in genentrycode
  385. Revision 1.23 2002/08/18 10:34:30 florian
  386. * more ppc assembling fixes
  387. Revision 1.22 2002/08/14 19:30:42 carl
  388. + added fixing because first_in_to_real is now completely generic
  389. Revision 1.21 2002/08/11 06:14:41 florian
  390. * fixed powerpc compilation problems
  391. Revision 1.20 2002/08/10 17:15:31 jonas
  392. * various fixes and optimizations
  393. Revision 1.19 2002/07/29 21:23:44 florian
  394. * more fixes for the ppc
  395. + wrappers for the tcnvnode.first_* stuff introduced
  396. Revision 1.18 2002/07/29 09:20:20 jonas
  397. + second_int_to_int implementation which is almost the same as the
  398. generic implementation, but it avoids some unnecessary type conversions
  399. Revision 1.17 2002/07/27 19:55:15 jonas
  400. + generic implementation of tcg.g_flags2ref()
  401. * tcg.flags2xxx() now also needs a size parameter
  402. Revision 1.16 2002/07/24 14:38:00 florian
  403. * small typo fixed, compiles with 1.0.x again
  404. Revision 1.15 2002/07/21 16:57:22 jonas
  405. * hopefully final fix for second_int_to_real()
  406. Revision 1.14 2002/07/20 11:58:05 florian
  407. * types.pas renamed to defbase.pas because D6 contains a types
  408. unit so this would conflicts if D6 programms are compiled
  409. + Willamette/SSE2 instructions to assembler added
  410. Revision 1.13 2002/07/13 06:49:39 jonas
  411. * fixed fpu constants in second_int_to_real (fpu values are also stored
  412. in big endian)
  413. Revision 1.12 2002/07/12 22:02:22 florian
  414. * fixed to compile with 1.1
  415. Revision 1.11 2002/07/11 14:41:34 florian
  416. * start of the new generic parameter handling
  417. Revision 1.10 2002/07/11 07:42:31 jonas
  418. * fixed nppccnv and enabled it
  419. - removed PPC specific second_int_to_int and use the generic one instead
  420. Revision 1.9 2002/05/20 13:30:42 carl
  421. * bugfix of hdisponen (base must be set, not index)
  422. * more portability fixes
  423. Revision 1.8 2002/05/18 13:34:26 peter
  424. * readded missing revisions
  425. Revision 1.7 2002/05/16 19:46:53 carl
  426. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  427. + try to fix temp allocation (still in ifdef)
  428. + generic constructor calls
  429. + start of tassembler / tmodulebase class cleanup
  430. }