ncpucnv.pas 18 KB

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