n68kcnv.pas 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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,defcmp;
  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. end;
  31. implementation
  32. uses
  33. verbose,globals,systems,
  34. symconst,symdef,aasmbase,aasmtai,
  35. defutil,
  36. cgbase,pass_1,pass_2,
  37. ncon,ncal,
  38. ncgutil,
  39. cpubase,aasmcpu,
  40. rgobj,tgobj,cgobj,cgutils,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,s32inttype)
  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:=cg.getfpuregister(exprasmlist,opsize);
  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_explicit 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:=cg.getintregister(exprasmlist,opsize);
  164. cg.a_load_ref_reg(exprasmlist,opsize,opsize,
  165. left.location.reference,hreg2);
  166. exprasmlist.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[opsize],hreg2));
  167. cg.ungetcpuregister(exprasmlist,hreg2);
  168. end;
  169. // reference_release(exprasmlist,left.location.reference);
  170. resflags:=F_NE;
  171. hreg1:=cg.getintregister(exprasmlist,opsize);
  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. cg.ungetcpuregister(exprasmlist,hreg2);
  178. hreg1:=cg.getintregister(exprasmlist,opsize);
  179. resflags:=F_NE;
  180. end;
  181. LOC_FLAGS :
  182. begin
  183. hreg1:=cg.getintregister(exprasmlist,opsize);
  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.pass_2;
  193. {$ifdef TESTOBJEXT2}
  194. var
  195. r : preference;
  196. nillabel : plabel;
  197. {$endif TESTOBJEXT2}
  198. begin
  199. { this isn't good coding, I think tc_bool_2_int, shouldn't be }
  200. { type conversion (FK) }
  201. if not(convtype in [tc_bool_2_int,tc_bool_2_bool]) then
  202. begin
  203. secondpass(left);
  204. location_copy(location,left.location);
  205. if codegenerror then
  206. exit;
  207. end;
  208. second_call_helper(convtype);
  209. end;
  210. begin
  211. ctypeconvnode:=tm68ktypeconvnode;
  212. end.
  213. {
  214. $Log$
  215. Revision 1.14 2005-01-08 04:10:36 karoly
  216. * made m68k to compile again
  217. Revision 1.13 2004/06/20 08:55:31 florian
  218. * logs truncated
  219. Revision 1.12 2004/04/25 21:26:16 florian
  220. * some m68k stuff fixed
  221. Revision 1.11 2004/02/03 22:32:54 peter
  222. * renamed xNNbittype to xNNinttype
  223. * renamed registers32 to registersint
  224. * replace some s32bit,u32bit with torddef([su]inttype).def.typ
  225. }