n68kcnv.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Generate m68k 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 n68kcnv;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,ncnv,ncgcnv,defbase;
  23. type
  24. tm68ktypeconvnode = class(tcgtypeconvnode)
  25. protected
  26. function first_int_to_real: tnode; override;
  27. procedure second_int_to_real;override;
  28. procedure second_int_to_bool;override;
  29. procedure pass_2;override;
  30. procedure second_call_helper(c : tconverttype); override;
  31. end;
  32. implementation
  33. uses
  34. verbose,globals,systems,
  35. symconst,symdef,aasmbase,aasmtai,
  36. cgbase,pass_1,pass_2,
  37. ncon,ncal,
  38. ncgutil,
  39. cpubase,aasmcpu,
  40. rgobj,tgobj,cgobj,cginfo,globtype,cgcpu;
  41. {*****************************************************************************
  42. FirstTypeConv
  43. *****************************************************************************}
  44. function tm68ktypeconvnode.first_int_to_real: tnode;
  45. var
  46. fname: string[19];
  47. begin
  48. { In case we are in emulation mode, we must
  49. always call the helpers
  50. }
  51. if (cs_fp_emulation in aktmoduleswitches) then
  52. begin
  53. result := inherited first_int_to_real;
  54. exit;
  55. end
  56. else
  57. { converting a 64bit integer to a float requires a helper }
  58. if is_64bitint(left.resulttype.def) then
  59. begin
  60. if is_signed(left.resulttype.def) then
  61. fname := 'fpc_int64_to_double'
  62. else
  63. fname := 'fpc_qword_to_double';
  64. result := ccallnode.createintern(fname,ccallparanode.create(
  65. left,nil));
  66. left:=nil;
  67. firstpass(result);
  68. exit;
  69. end
  70. else
  71. { other integers are supposed to be 32 bit }
  72. begin
  73. if is_signed(left.resulttype.def) then
  74. inserttypeconv(left,s32bittype)
  75. else
  76. { the fpu always considers 32-bit values as signed
  77. therefore we need to call the helper in case of
  78. a cardinal value.
  79. }
  80. begin
  81. fname := 'fpc_longword_to_double';
  82. result := ccallnode.createintern(fname,ccallparanode.create(
  83. left,nil));
  84. left:=nil;
  85. firstpass(result);
  86. exit;
  87. end;
  88. firstpass(left);
  89. end;
  90. result := nil;
  91. if registersfpu<1 then
  92. registersfpu:=1;
  93. location.loc:=LOC_FPUREGISTER;
  94. end;
  95. {*****************************************************************************
  96. SecondTypeConv
  97. *****************************************************************************}
  98. procedure tm68ktypeconvnode.second_int_to_real;
  99. var
  100. tempconst: trealconstnode;
  101. ref: treference;
  102. valuereg, tempreg, leftreg, tmpfpureg: tregister;
  103. signed : boolean;
  104. scratch_used : boolean;
  105. opsize : tcgsize;
  106. begin
  107. scratch_used := false;
  108. location_reset(location,LOC_FPUREGISTER,def_cgsize(resulttype.def));
  109. signed := is_signed(left.resulttype.def);
  110. opsize := def_cgsize(left.resulttype.def);
  111. { has to be handled by a helper }
  112. if is_64bitint(left.resulttype.def) then
  113. internalerror(200110011);
  114. { has to be handled by a helper }
  115. if not signed then
  116. internalerror(20020814);
  117. location.register := rg.getregisterfpu(exprasmlist);
  118. case left.location.loc of
  119. LOC_REGISTER, LOC_CREGISTER:
  120. begin
  121. leftreg := left.location.register;
  122. exprasmlist.concat(taicpu.op_reg_reg(A_FMOVE,TCGSize2OpSize[opsize],leftreg,
  123. location.register));
  124. end;
  125. LOC_REFERENCE,LOC_CREFERENCE:
  126. begin
  127. exprasmlist.concat(taicpu.op_ref_reg(A_FMOVE,TCGSize2OpSize[opsize],
  128. left.location.reference,location.register));
  129. end
  130. else
  131. internalerror(200110012);
  132. end;
  133. end;
  134. procedure tm68ktypeconvnode.second_int_to_bool;
  135. var
  136. hreg1,
  137. hreg2 : tregister;
  138. resflags : tresflags;
  139. opsize : tcgsize;
  140. begin
  141. { byte(boolean) or word(wordbool) or longint(longbool) must }
  142. { be accepted for var parameters }
  143. if (nf_explizit in flags) and
  144. (left.resulttype.def.size=resulttype.def.size) and
  145. (left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  146. begin
  147. location_copy(location,left.location);
  148. exit;
  149. end;
  150. location_reset(location,LOC_REGISTER,def_cgsize(left.resulttype.def));
  151. opsize := def_cgsize(left.resulttype.def);
  152. case left.location.loc of
  153. LOC_CREFERENCE,LOC_REFERENCE :
  154. begin
  155. { can we optimize it, or do we need to fix the ref. ? }
  156. if isvalidrefoffset(left.location.reference) then
  157. begin
  158. exprasmlist.concat(taicpu.op_ref(A_TST,TCGSize2OpSize[opsize],
  159. left.location.reference));
  160. end
  161. else
  162. begin
  163. hreg2:=rg.getregisterint(exprasmlist);
  164. cg.a_load_ref_reg(exprasmlist,opsize,
  165. left.location.reference,hreg2);
  166. exprasmlist.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[opsize],hreg2));
  167. rg.ungetregister(exprasmlist,hreg2);
  168. end;
  169. reference_release(exprasmlist,left.location.reference);
  170. resflags:=F_NE;
  171. hreg1 := rg.getregisterint(exprasmlist);
  172. end;
  173. LOC_REGISTER,LOC_CREGISTER :
  174. begin
  175. hreg2 := left.location.register;
  176. exprasmlist.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[opsize],hreg2));
  177. rg.ungetregister(exprasmlist,hreg2);
  178. hreg1 := rg.getregisterint(exprasmlist);
  179. resflags:=F_NE;
  180. end;
  181. LOC_FLAGS :
  182. begin
  183. hreg1:=rg.getregisterint(exprasmlist);
  184. resflags:=left.location.resflags;
  185. end;
  186. else
  187. internalerror(10062);
  188. end;
  189. cg.g_flags2reg(exprasmlist,location.size,resflags,hreg1);
  190. location.register := hreg1;
  191. end;
  192. procedure tm68ktypeconvnode.second_call_helper(c : tconverttype);
  193. const
  194. secondconvert : array[tconverttype] of pointer = (
  195. @second_nothing, {equal}
  196. @second_nothing, {not_possible}
  197. @second_nothing, {second_string_to_string, handled in resulttype pass }
  198. @second_char_to_string,
  199. @second_nothing, {char_to_charray}
  200. @second_nothing, { pchar_to_string, handled in resulttype pass }
  201. @second_nothing, {cchar_to_pchar}
  202. @second_cstring_to_pchar,
  203. @second_ansistring_to_pchar,
  204. @second_string_to_chararray,
  205. @second_nothing, { chararray_to_string, handled in resulttype pass }
  206. @second_array_to_pointer,
  207. @second_pointer_to_array,
  208. @second_int_to_int,
  209. @second_int_to_bool,
  210. @second_bool_to_int, { bool_to_bool }
  211. @second_bool_to_int,
  212. @second_real_to_real,
  213. @second_int_to_real,
  214. @second_proc_to_procvar,
  215. @second_nothing, { arrayconstructor_to_set }
  216. @second_nothing, { second_load_smallset, handled in first pass }
  217. @second_cord_to_pointer,
  218. @second_nothing, { interface 2 string }
  219. @second_nothing, { interface 2 guid }
  220. @second_class_to_intf,
  221. @second_char_to_char,
  222. @second_nothing, { normal_2_smallset }
  223. @second_nothing { dynarray_2_openarray }
  224. );
  225. type
  226. tprocedureofobject = procedure of object;
  227. var
  228. r : packed record
  229. proc : pointer;
  230. obj : pointer;
  231. end;
  232. begin
  233. { this is a little bit dirty but it works }
  234. { and should be quite portable too }
  235. r.proc:=secondconvert[c];
  236. r.obj:=self;
  237. tprocedureofobject(r){$ifdef FPC}();{$endif FPC}
  238. end;
  239. procedure tm68ktypeconvnode.pass_2;
  240. {$ifdef TESTOBJEXT2}
  241. var
  242. r : preference;
  243. nillabel : plabel;
  244. {$endif TESTOBJEXT2}
  245. begin
  246. { this isn't good coding, I think tc_bool_2_int, shouldn't be }
  247. { type conversion (FK) }
  248. if not(convtype in [tc_bool_2_int,tc_bool_2_bool]) then
  249. begin
  250. secondpass(left);
  251. location_copy(location,left.location);
  252. if codegenerror then
  253. exit;
  254. end;
  255. second_call_helper(convtype);
  256. end;
  257. begin
  258. ctypeconvnode:=tm68ktypeconvnode;
  259. end.
  260. {
  261. $Log$
  262. Revision 1.4 2002-09-07 20:53:28 carl
  263. * cardinal -> longword
  264. Revision 1.3 2002/09/07 15:25:13 peter
  265. * old logs removed and tabs fixed
  266. Revision 1.2 2002/08/14 19:31:26 carl
  267. * fix small compilation problem
  268. Revision 1.1 2002/08/14 19:16:34 carl
  269. + m68k type conversion nodes
  270. + started some mathematical nodes
  271. * out of bound references should now be handled correctly
  272. }