ncpucnv.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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;
  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. begin
  96. location_reset(location,LOC_FPUREGISTER,def_cgsize(resulttype.def));
  97. location_force_mem(exprasmlist,left.location);
  98. location.register:=cg.getfpuregister(exprasmlist,location.size);
  99. { Load memory in fpu register }
  100. cg.a_loadfpu_ref_reg(exprasmlist,location.size,left.location.reference,location.register);
  101. {$warning TODO Handle also double}
  102. { Convert value in fpu register from integer to float }
  103. exprasmlist.concat(taicpu.op_reg_reg(A_FiTOs,location.register,location.register));
  104. end;
  105. procedure TSparctypeconvnode.second_real_to_real;
  106. begin
  107. inherited second_real_to_real;
  108. end;
  109. procedure TSparctypeconvnode.second_int_to_bool;
  110. var
  111. hreg1,hreg2:tregister;
  112. resflags : tresflags;
  113. opsize : tcgsize;
  114. begin
  115. { byte(boolean) or word(wordbool) or longint(longbool) must }
  116. { be accepted for var parameters }
  117. if(nf_explicit in flags)and
  118. (left.resulttype.def.size=resulttype.def.size)and
  119. (left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER])
  120. then
  121. begin
  122. location_copy(location,left.location);
  123. exit;
  124. end;
  125. location_reset(location,LOC_REGISTER,def_cgsize(left.resulttype.def));
  126. opsize := def_cgsize(left.resulttype.def);
  127. case left.location.loc of
  128. LOC_CREFERENCE,LOC_REFERENCE,LOC_REGISTER,LOC_CREGISTER:
  129. begin
  130. if left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]
  131. then
  132. begin
  133. reference_release(exprasmlist,left.location.reference);
  134. hreg2:=cg.GetIntRegister(exprasmlist,opsize);
  135. cg.a_load_ref_reg(exprasmlist,OpSize,OpSize,left.location.reference,hreg2);
  136. end
  137. else
  138. hreg2 := left.location.register;
  139. hreg1 := cg.GetIntRegister(exprasmlist,opsize);
  140. exprasmlist.concat(taicpu.op_reg_const_reg(A_SUB,hreg1,1,hreg2));
  141. exprasmlist.concat(taicpu.op_reg_reg_reg(A_SUB,hreg1,hreg1,hreg2));
  142. cg.UnGetRegister(exprasmlist,hreg2);
  143. end;
  144. LOC_FLAGS :
  145. begin
  146. hreg1:=cg.GetIntRegister(exprasmlist,location.size);
  147. resflags:=left.location.resflags;
  148. cg.g_flags2reg(exprasmlist,location.size,resflags,hreg1);
  149. end;
  150. else
  151. internalerror(10062);
  152. end;
  153. location.register := hreg1;
  154. end;
  155. procedure TSparctypeconvnode.second_call_helper(c : tconverttype);
  156. const
  157. secondconvert : array[tconverttype] of pointer = (
  158. @second_nothing, {equal}
  159. @second_nothing, {not_possible}
  160. @second_nothing, {second_string_to_string, handled in resulttype pass }
  161. @second_char_to_string,
  162. @second_nothing, {char_to_charray}
  163. @second_nothing, { pchar_to_string, handled in resulttype pass }
  164. @second_nothing, {cchar_to_pchar}
  165. @second_cstring_to_pchar,
  166. @second_ansistring_to_pchar,
  167. @second_string_to_chararray,
  168. @second_nothing, { chararray_to_string, handled in resulttype pass }
  169. @second_array_to_pointer,
  170. @second_pointer_to_array,
  171. @second_int_to_int,
  172. @second_int_to_bool,
  173. @second_bool_to_int, { bool_to_bool }
  174. @second_bool_to_int,
  175. @second_real_to_real,
  176. @second_int_to_real,
  177. @second_nothing, { currency_to_real, handled in resulttype pass }
  178. @second_proc_to_procvar,
  179. @second_nothing, { arrayconstructor_to_set }
  180. @second_nothing, { second_load_smallset, handled in first pass }
  181. @second_cord_to_pointer,
  182. @second_nothing, { interface 2 string }
  183. @second_nothing, { interface 2 guid }
  184. @second_class_to_intf,
  185. @second_char_to_char,
  186. @second_nothing, { normal_2_smallset }
  187. @second_nothing, { dynarray_2_openarray }
  188. @second_nothing,
  189. {$ifdef fpc}@{$endif}second_nothing, { variant_2_dynarray }
  190. {$ifdef fpc}@{$endif}second_nothing { dynarray_2_variant}
  191. );
  192. type
  193. tprocedureofobject = procedure of object;
  194. var
  195. r:packed record
  196. proc : pointer;
  197. obj : pointer;
  198. end;
  199. begin
  200. { this is a little bit dirty but it works }
  201. { and should be quite portable too }
  202. r.proc:=secondconvert[c];
  203. r.obj:=self;
  204. tprocedureofobject(r){$ifdef FPC}();{$endif FPC}
  205. end;
  206. procedure TSparctypeconvnode.pass_2;
  207. {$ifdef TESTOBJEXT2}
  208. var
  209. r : preference;
  210. nillabel : plabel;
  211. {$endif TESTOBJEXT2}
  212. begin
  213. { this isn't good coding, I think tc_bool_2_int, shouldn't be }
  214. { type conversion (FK) }
  215. if not(convtype in [tc_bool_2_int,tc_bool_2_bool])
  216. then
  217. begin
  218. secondpass(left);
  219. location_copy(location,left.location);
  220. if codegenerror
  221. then
  222. exit;
  223. end;
  224. second_call_helper(convtype);
  225. end;
  226. begin
  227. ctypeconvnode:=TSparctypeconvnode;
  228. end.
  229. {
  230. $Log$
  231. Revision 1.20 2003-10-24 11:31:43 mazen
  232. *fixes related to removal of rg
  233. Revision 1.19 2003/10/01 20:34:50 peter
  234. * procinfo unit contains tprocinfo
  235. * cginfo renamed to cgbase
  236. * moved cgmessage to verbose
  237. * fixed ppc and sparc compiles
  238. Revision 1.18 2003/09/14 21:36:01 peter
  239. * remove ppc code
  240. Revision 1.17 2003/06/04 20:59:37 mazen
  241. + added size of destination in code gen methods
  242. + making g_overflowcheck declaration same as
  243. ancestor's method declaration
  244. Revision 1.16 2003/06/01 21:38:06 peter
  245. * getregisterfpu size parameter added
  246. * op_const_reg size parameter added
  247. * sparc updates
  248. Revision 1.15 2003/04/23 21:10:54 peter
  249. * fix compile for ppc,sparc,m68k
  250. Revision 1.14 2003/04/23 13:35:39 peter
  251. * fix sparc compile
  252. Revision 1.13 2003/03/10 21:59:54 mazen
  253. * fixing index overflow in handling new registers arrays.
  254. Revision 1.12 2003/02/19 22:00:17 daniel
  255. * Code generator converted to new register notation
  256. - Horribily outdated todo.txt removed
  257. Revision 1.11 2003/01/22 20:45:15 mazen
  258. * making math code in RTL compiling.
  259. *NB : This does NOT mean necessary that it will generate correct code!
  260. Revision 1.10 2003/01/20 22:21:36 mazen
  261. * many stuff related to RTL fixed
  262. Revision 1.9 2002/12/05 14:28:03 florian
  263. * some variant <-> dyn. array stuff
  264. Revision 1.8 2002/11/25 17:43:28 peter
  265. * splitted defbase in defutil,symutil,defcmp
  266. * merged isconvertable and is_equal into compare_defs(_ext)
  267. * made operator search faster by walking the list only once
  268. Revision 1.7 2002/11/10 19:07:46 mazen
  269. * SPARC calling mechanism almost OK (as in GCC./mppcsparc )
  270. Revision 1.6 2002/11/06 11:31:24 mazen
  271. * op_reg_reg_reg don't need any more a TOpSize parameter
  272. Revision 1.5 2002/10/22 13:43:01 mazen
  273. - cga.pas redueced to an empty unit
  274. Revision 1.4 2002/10/10 19:57:52 mazen
  275. * Just to update repsitory
  276. Revision 1.3 2002/09/07 15:25:14 peter
  277. * old logs removed and tabs fixed
  278. Revision 1.2 2002/08/30 06:15:27 mazen
  279. ncgcall.pas moved to ncpucall.pas (I'd like ncpu* insteade of nsparc* since it
  280. provides processor independent units naming)
  281. Revision 1.1 2002/08/29 10:16:20 mazen
  282. File added support to the new generic parameter handling
  283. Revision 1.24 2002/08/23 16:14:50 peter
  284. * tempgen cleanup
  285. * tt_noreuse temp type added that will be used in genentrycode
  286. Revision 1.23 2002/08/18 10:34:30 florian
  287. * more ppc assembling fixes
  288. Revision 1.22 2002/08/14 19:30:42 carl
  289. + added fixing because first_in_to_real is now completely generic
  290. Revision 1.21 2002/08/11 06:14:41 florian
  291. * fixed powerpc compilation problems
  292. Revision 1.20 2002/08/10 17:15:31 jonas
  293. * various fixes and optimizations
  294. Revision 1.19 2002/07/29 21:23:44 florian
  295. * more fixes for the ppc
  296. + wrappers for the tcnvnode.first_* stuff introduced
  297. Revision 1.18 2002/07/29 09:20:20 jonas
  298. + second_int_to_int implementation which is almost the same as the
  299. generic implementation, but it avoids some unnecessary type conversions
  300. Revision 1.17 2002/07/27 19:55:15 jonas
  301. + generic implementation of tcg.g_flags2ref()
  302. * tcg.flags2xxx() now also needs a size parameter
  303. Revision 1.16 2002/07/24 14:38:00 florian
  304. * small typo fixed, compiles with 1.0.x again
  305. Revision 1.15 2002/07/21 16:57:22 jonas
  306. * hopefully final fix for second_int_to_real()
  307. Revision 1.14 2002/07/20 11:58:05 florian
  308. * types.pas renamed to defbase.pas because D6 contains a types
  309. unit so this would conflicts if D6 programms are compiled
  310. + Willamette/SSE2 instructions to assembler added
  311. Revision 1.13 2002/07/13 06:49:39 jonas
  312. * fixed fpu constants in second_int_to_real (fpu values are also stored
  313. in big endian)
  314. Revision 1.12 2002/07/12 22:02:22 florian
  315. * fixed to compile with 1.1
  316. Revision 1.11 2002/07/11 14:41:34 florian
  317. * start of the new generic parameter handling
  318. Revision 1.10 2002/07/11 07:42:31 jonas
  319. * fixed nppccnv and enabled it
  320. - removed PPC specific second_int_to_int and use the generic one instead
  321. Revision 1.9 2002/05/20 13:30:42 carl
  322. * bugfix of hdisponen (base must be set, not index)
  323. * more portability fixes
  324. Revision 1.8 2002/05/18 13:34:26 peter
  325. * readded missing revisions
  326. Revision 1.7 2002/05/16 19:46:53 carl
  327. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  328. + try to fix temp allocation (still in ifdef)
  329. + generic constructor calls
  330. + start of tassembler / tmodulebase class cleanup
  331. }