ncpucnv.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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,defbase;
  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. cgbase,pass_1,pass_2,
  52. ncon,ncal,
  53. ncgutil,
  54. cpubase,aasmcpu,
  55. rgobj,tgobj,cgobj,cginfo;
  56. {*****************************************************************************
  57. FirstTypeConv
  58. *****************************************************************************}
  59. function TSparctypeconvnode.first_int_to_real: tnode;
  60. var
  61. fname: string[19];
  62. begin
  63. { converting a 64bit integer to a float requires a helper }
  64. if is_64bitint(left.resulttype.def) then
  65. begin
  66. if is_signed(left.resulttype.def) then
  67. fname := 'fpc_int64_to_double'
  68. else
  69. fname := 'fpc_qword_to_double';
  70. result := ccallnode.createintern(fname,ccallparanode.create(
  71. left,nil));
  72. left:=nil;
  73. firstpass(result);
  74. exit;
  75. end
  76. else
  77. { other integers are supposed to be 32 bit }
  78. begin
  79. if is_signed(left.resulttype.def) then
  80. inserttypeconv(left,s32bittype)
  81. else
  82. inserttypeconv(left,u32bittype);
  83. firstpass(left);
  84. end;
  85. result := nil;
  86. if registersfpu<1 then
  87. registersfpu:=1;
  88. location.loc:=LOC_FPUREGISTER;
  89. end;
  90. {*****************************************************************************
  91. SecondTypeConv
  92. *****************************************************************************}
  93. procedure TSparctypeconvnode.second_int_to_int;
  94. var
  95. newsize : tcgsize;
  96. size, leftsize : cardinal;
  97. begin
  98. newsize:=def_cgsize(resulttype.def);
  99. { insert range check if not explicit conversion }
  100. if not(nf_explizit in flags) then
  101. cg.g_rangecheck(exprasmlist,left,resulttype.def);
  102. { is the result size smaller ? }
  103. size := resulttype.def.size;
  104. leftsize := left.resulttype.def.size;
  105. if (size < leftsize) or
  106. (((newsize in [OS_64,OS_S64]) or
  107. (left.location.loc <> LOC_REGISTER)) and
  108. (size > leftsize)) then
  109. begin
  110. { reuse the left location by default }
  111. location_copy(location,left.location);
  112. location_force_reg(exprasmlist,location,newsize,false);
  113. end
  114. else
  115. begin
  116. { no special loading is required, reuse current location }
  117. location_copy(location,left.location);
  118. location.size:=newsize;
  119. end;
  120. end;
  121. procedure TSparctypeconvnode.second_int_to_real;
  122. type
  123. tdummyarray = packed array[0..7] of byte;
  124. {$ifdef VER1_0}
  125. var
  126. dummy1, dummy2: int64;
  127. {$else VER1_0}
  128. const
  129. dummy1: int64 = $4330000080000000;
  130. dummy2: int64 = $4330000000000000;
  131. {$endif VER1_0}
  132. var
  133. tempconst: trealconstnode;
  134. ref: treference;
  135. valuereg, tempreg, leftreg, tmpfpureg: tregister;
  136. signed, valuereg_is_scratch: boolean;
  137. begin
  138. {$ifdef VER1_0}
  139. { the "and" is because 1.0.x will sign-extend the $80000000 to }
  140. { $ffffffff80000000 when converting it to int64 (JM) }
  141. dummy1 := int64($80000000) and (int64($43300000) shl 32);
  142. dummy2 := int64($43300000) shl 32;
  143. {$endif VER1_0}
  144. valuereg_is_scratch := false;
  145. location_reset(location,LOC_FPUREGISTER,def_cgsize(resulttype.def));
  146. { the code here comes from the PowerPC Compiler Writer's Guide }
  147. { * longint to double }
  148. { addis R0,R0,0x4330 # R0 = 0x43300000 }
  149. { stw R0,disp(R1) # store upper half }
  150. { xoris R3,R3,0x8000 # flip sign bit }
  151. { stw R3,disp+4(R1) # store lower half }
  152. { lfd FR1,disp(R1) # float load double of value }
  153. { fsub FR1,FR1,FR2 # subtract 0x4330000080000000 }
  154. { * cardinal to double }
  155. { addis R0,R0,0x4330 # R0 = 0x43300000 }
  156. { stw R0,disp(R1) # store upper half }
  157. { stw R3,disp+4(R1) # store lower half }
  158. { lfd FR1,disp(R1) # float load double of value }
  159. { fsub FR1,FR1,FR2 # subtract 0x4330000000000000 }
  160. tg.Gettemp(exprasmlist,8,tt_normal,ref);
  161. signed := is_signed(left.resulttype.def);
  162. { we need a certain constant for the conversion, so create it here }
  163. if signed then
  164. tempconst :=
  165. crealconstnode.create(double(dummy1),
  166. pbestrealtype^)
  167. else
  168. tempconst :=
  169. crealconstnode.create(double(dummy2),
  170. pbestrealtype^);
  171. resulttypepass(tempconst);
  172. firstpass(tempconst);
  173. secondpass(tempconst);
  174. if (tempconst.location.loc <> LOC_CREFERENCE) or
  175. { has to be handled by a helper }
  176. is_64bitint(left.resulttype.def) then
  177. internalerror(200110011);
  178. case left.location.loc of
  179. LOC_REGISTER:
  180. begin
  181. leftreg := left.location.register;
  182. valuereg := leftreg;
  183. end;
  184. LOC_CREGISTER:
  185. begin
  186. leftreg := left.location.register;
  187. if signed then
  188. begin
  189. valuereg := cg.get_scratch_reg_int(exprasmlist);
  190. valuereg_is_scratch := true;
  191. end
  192. else
  193. valuereg := leftreg;
  194. end;
  195. LOC_REFERENCE,LOC_CREFERENCE:
  196. begin
  197. leftreg := cg.get_scratch_reg_int(exprasmlist);
  198. valuereg := leftreg;
  199. valuereg_is_scratch := true;
  200. cg.a_load_ref_reg(exprasmlist,def_cgsize(left.resulttype.def),
  201. left.location.reference,leftreg);
  202. end
  203. else
  204. internalerror(200110012);
  205. end;
  206. tempreg := cg.get_scratch_reg_int(exprasmlist);
  207. {$WARNING FIXME what reallty should be done?}
  208. exprasmlist.concat(taicpu.op_reg_const_reg(A_OR,S_L,tempreg,$4330,tempreg));
  209. cg.a_load_reg_ref(exprasmlist,OS_32,tempreg,ref);
  210. cg.free_scratch_reg(exprasmlist,tempreg);
  211. if signed then
  212. {$WARNING FIXME what reallty should be done?}
  213. exprasmlist.concat(taicpu.op_reg_const_reg(A_XOR,S_L,leftreg,$8000,valuereg));
  214. inc(ref.offset,4);
  215. cg.a_load_reg_ref(exprasmlist,OS_32,valuereg,ref);
  216. dec(ref.offset,4);
  217. if (valuereg_is_scratch) then
  218. cg.free_scratch_reg(exprasmlist,valuereg);
  219. if (left.location.loc = LOC_REGISTER) or
  220. ((left.location.loc = LOC_CREGISTER) and
  221. not signed) then
  222. rg.ungetregister(exprasmlist,leftreg)
  223. else
  224. cg.free_scratch_reg(exprasmlist,valuereg);
  225. tmpfpureg := rg.getregisterfpu(exprasmlist);
  226. cg.a_loadfpu_ref_reg(exprasmlist,OS_F64,tempconst.location.reference,
  227. tmpfpureg);
  228. tempconst.free;
  229. location.register := rg.getregisterfpu(exprasmlist);
  230. {$WARNING FIXME what reallty should be done?}
  231. exprasmlist.concat(taicpu.op_reg_ref(A_LD,S_L,location.register,ref));
  232. tg.ungetiftemp(exprasmlist,ref);
  233. exprasmlist.concat(taicpu.op_reg_reg_reg(A_SUB,S_L,location.register,
  234. location.register,tmpfpureg));
  235. rg.ungetregisterfpu(exprasmlist,tmpfpureg);
  236. { work around bug in some PowerPC processors }
  237. if (tfloatdef(resulttype.def).typ = s32real) then
  238. {$WARNING FIXME what reallty should be done?}
  239. exprasmlist.concat(taicpu.op_reg_reg(A_ADD,S_L,location.register,
  240. 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 reallty should be done?}
  250. exprasmlist.concat(taicpu.op_reg_reg(A_ADD,S_L,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,S_L,hreg1,1,
  284. hreg2));
  285. exprasmlist.concat(taicpu.op_reg_reg_reg(A_SUB,S_L,hreg1,hreg1,
  286. hreg2));
  287. rg.ungetregister(exprasmlist,hreg2);
  288. end;
  289. LOC_FLAGS :
  290. begin
  291. hreg1:=rg.getregisterint(exprasmlist);
  292. resflags:=left.location.resflags;
  293. cg.g_flags2reg(exprasmlist,location.size,resflags,hreg1);
  294. end;
  295. else
  296. internalerror(10062);
  297. end;
  298. location.register := hreg1;
  299. end;
  300. procedure TSparctypeconvnode.second_call_helper(c : tconverttype);
  301. const
  302. secondconvert : array[tconverttype] of pointer = (
  303. @second_nothing, {equal}
  304. @second_nothing, {not_possible}
  305. @second_nothing, {second_string_to_string, handled in resulttype pass }
  306. @second_char_to_string,
  307. @second_nothing, {char_to_charray}
  308. @second_nothing, { pchar_to_string, handled in resulttype pass }
  309. @second_nothing, {cchar_to_pchar}
  310. @second_cstring_to_pchar,
  311. @second_ansistring_to_pchar,
  312. @second_string_to_chararray,
  313. @second_nothing, { chararray_to_string, handled in resulttype pass }
  314. @second_array_to_pointer,
  315. @second_pointer_to_array,
  316. @second_int_to_int,
  317. @second_int_to_bool,
  318. @second_bool_to_int, { bool_to_bool }
  319. @second_bool_to_int,
  320. @second_real_to_real,
  321. @second_int_to_real,
  322. @second_proc_to_procvar,
  323. @second_nothing, { arrayconstructor_to_set }
  324. @second_nothing, { second_load_smallset, handled in first pass }
  325. @second_cord_to_pointer,
  326. @second_nothing, { interface 2 string }
  327. @second_nothing, { interface 2 guid }
  328. @second_class_to_intf,
  329. @second_char_to_char,
  330. @second_nothing, { normal_2_smallset }
  331. @second_nothing { dynarray_2_openarray }
  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.3 2002-09-07 15:25:14 peter
  371. * old logs removed and tabs fixed
  372. Revision 1.2 2002/08/30 06:15:27 mazen
  373. ncgcall.pas moved to ncpucall.pas (I'd like ncpu* insteade of nsparc* since it
  374. provides processor independent units naming)
  375. Revision 1.1 2002/08/29 10:16:20 mazen
  376. File added support to the new generic parameter handling
  377. Revision 1.24 2002/08/23 16:14:50 peter
  378. * tempgen cleanup
  379. * tt_noreuse temp type added that will be used in genentrycode
  380. Revision 1.23 2002/08/18 10:34:30 florian
  381. * more ppc assembling fixes
  382. Revision 1.22 2002/08/14 19:30:42 carl
  383. + added fixing because first_in_to_real is now completely generic
  384. Revision 1.21 2002/08/11 06:14:41 florian
  385. * fixed powerpc compilation problems
  386. Revision 1.20 2002/08/10 17:15:31 jonas
  387. * various fixes and optimizations
  388. Revision 1.19 2002/07/29 21:23:44 florian
  389. * more fixes for the ppc
  390. + wrappers for the tcnvnode.first_* stuff introduced
  391. Revision 1.18 2002/07/29 09:20:20 jonas
  392. + second_int_to_int implementation which is almost the same as the
  393. generic implementation, but it avoids some unnecessary type conversions
  394. Revision 1.17 2002/07/27 19:55:15 jonas
  395. + generic implementation of tcg.g_flags2ref()
  396. * tcg.flags2xxx() now also needs a size parameter
  397. Revision 1.16 2002/07/24 14:38:00 florian
  398. * small typo fixed, compiles with 1.0.x again
  399. Revision 1.15 2002/07/21 16:57:22 jonas
  400. * hopefully final fix for second_int_to_real()
  401. Revision 1.14 2002/07/20 11:58:05 florian
  402. * types.pas renamed to defbase.pas because D6 contains a types
  403. unit so this would conflicts if D6 programms are compiled
  404. + Willamette/SSE2 instructions to assembler added
  405. Revision 1.13 2002/07/13 06:49:39 jonas
  406. * fixed fpu constants in second_int_to_real (fpu values are also stored
  407. in big endian)
  408. Revision 1.12 2002/07/12 22:02:22 florian
  409. * fixed to compile with 1.1
  410. Revision 1.11 2002/07/11 14:41:34 florian
  411. * start of the new generic parameter handling
  412. Revision 1.10 2002/07/11 07:42:31 jonas
  413. * fixed nppccnv and enabled it
  414. - removed PPC specific second_int_to_int and use the generic one instead
  415. Revision 1.9 2002/05/20 13:30:42 carl
  416. * bugfix of hdisponen (base must be set, not index)
  417. * more portability fixes
  418. Revision 1.8 2002/05/18 13:34:26 peter
  419. * readded missing revisions
  420. Revision 1.7 2002/05/16 19:46:53 carl
  421. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  422. + try to fix temp allocation (still in ifdef)
  423. + generic constructor calls
  424. + start of tassembler / tmodulebase class cleanup
  425. }