n68kcnv.pas 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate m68k 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. }
  17. unit n68kcnv;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,ncnv,ncgcnv,defcmp;
  22. type
  23. tm68ktypeconvnode = class(tcgtypeconvnode)
  24. protected
  25. function first_int_to_real: tnode; override;
  26. procedure second_int_to_real;override;
  27. procedure second_int_to_bool;override;
  28. // procedure pass_2;override;
  29. end;
  30. implementation
  31. uses
  32. verbose,globals,systems,
  33. symconst,symdef,aasmbase,aasmtai,aasmdata,
  34. defutil,
  35. cgbase,pass_1,pass_2,
  36. ncon,ncal,
  37. ncgutil,
  38. cpubase,aasmcpu,
  39. rgobj,tgobj,cgobj,cgutils,globtype,cgcpu;
  40. {*****************************************************************************
  41. FirstTypeConv
  42. *****************************************************************************}
  43. function tm68ktypeconvnode.first_int_to_real: tnode;
  44. var
  45. fname: string[32];
  46. begin
  47. { In case we are in emulation mode, we must
  48. always call the helpers
  49. }
  50. if (cs_fp_emulation in aktmoduleswitches) then
  51. begin
  52. result := inherited first_int_to_real;
  53. exit;
  54. end
  55. else
  56. { converting a 64bit integer to a float requires a helper }
  57. if is_64bitint(left.resulttype.def) then
  58. begin
  59. if is_signed(left.resulttype.def) then
  60. fname := 'fpc_int64_to_double'
  61. else
  62. fname := 'fpc_qword_to_double';
  63. result := ccallnode.createintern(fname,ccallparanode.create(
  64. left,nil));
  65. left:=nil;
  66. firstpass(result);
  67. exit;
  68. end
  69. else
  70. { other integers are supposed to be 32 bit }
  71. begin
  72. if is_signed(left.resulttype.def) then
  73. inserttypeconv(left,s32inttype)
  74. else
  75. { the fpu always considers 32-bit values as signed
  76. therefore we need to call the helper in case of
  77. a cardinal value.
  78. }
  79. begin
  80. fname := 'fpc_longword_to_double';
  81. result := ccallnode.createintern(fname,ccallparanode.create(
  82. left,nil));
  83. left:=nil;
  84. firstpass(result);
  85. exit;
  86. end;
  87. firstpass(left);
  88. end;
  89. result := nil;
  90. if registersfpu<1 then
  91. registersfpu:=1;
  92. location.loc:=LOC_FPUREGISTER;
  93. end;
  94. {*****************************************************************************
  95. SecondTypeConv
  96. *****************************************************************************}
  97. procedure tm68ktypeconvnode.second_int_to_real;
  98. var
  99. tempconst: trealconstnode;
  100. ref: treference;
  101. valuereg, tempreg, leftreg, tmpfpureg: tregister;
  102. signed : boolean;
  103. scratch_used : boolean;
  104. opsize : tcgsize;
  105. begin
  106. scratch_used := false;
  107. location_reset(location,LOC_FPUREGISTER,def_cgsize(resulttype.def));
  108. signed := is_signed(left.resulttype.def);
  109. opsize := def_cgsize(left.resulttype.def);
  110. { has to be handled by a helper }
  111. if is_64bitint(left.resulttype.def) then
  112. internalerror(200110011);
  113. { has to be handled by a helper }
  114. if not signed then
  115. internalerror(20020814);
  116. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,opsize);
  117. case left.location.loc of
  118. LOC_REGISTER, LOC_CREGISTER:
  119. begin
  120. leftreg := left.location.register;
  121. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FMOVE,TCGSize2OpSize[opsize],leftreg,
  122. location.register));
  123. end;
  124. LOC_REFERENCE,LOC_CREFERENCE:
  125. begin
  126. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_FMOVE,TCGSize2OpSize[opsize],
  127. left.location.reference,location.register));
  128. end
  129. else
  130. internalerror(200110012);
  131. end;
  132. end;
  133. procedure tm68ktypeconvnode.second_int_to_bool;
  134. var
  135. hreg1,
  136. hreg2 : tregister;
  137. resflags : tresflags;
  138. opsize : tcgsize;
  139. begin
  140. secondpass(left);
  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. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_TST,TCGSize2OpSize[opsize],
  159. left.location.reference));
  160. end
  161. else
  162. begin
  163. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  164. cg.a_load_ref_reg(current_asmdata.CurrAsmList,opsize,opsize,
  165. left.location.reference,hreg2);
  166. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[opsize],hreg2));
  167. // cg.ungetcpuregister(current_asmdata.CurrAsmList,hreg2);
  168. end;
  169. // reference_release(current_asmdata.CurrAsmList,left.location.reference);
  170. resflags:=F_NE;
  171. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  172. end;
  173. LOC_REGISTER,LOC_CREGISTER :
  174. begin
  175. hreg2:=left.location.register;
  176. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[opsize],hreg2));
  177. // cg.ungetcpuregister(current_asmdata.CurrAsmList,hreg2);
  178. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  179. resflags:=F_NE;
  180. end;
  181. LOC_FLAGS :
  182. begin
  183. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  184. resflags:=left.location.resflags;
  185. end;
  186. else
  187. internalerror(200512182);
  188. end;
  189. cg.g_flags2reg(current_asmdata.CurrAsmList,location.size,resflags,hreg1);
  190. location.register := hreg1;
  191. end;
  192. {
  193. procedure tm68ktypeconvnode.pass_2;
  194. {$ifdef TESTOBJEXT2}
  195. var
  196. r : preference;
  197. nillabel : plabel;
  198. {$endif TESTOBJEXT2}
  199. begin
  200. { this isn't good coding, I think tc_bool_2_int, shouldn't be }
  201. { type conversion (FK) }
  202. if not(convtype in [tc_bool_2_int,tc_bool_2_bool]) then
  203. begin
  204. secondpass(left);
  205. location_copy(location,left.location);
  206. if codegenerror then
  207. exit;
  208. end;
  209. second_call_helper(convtype);
  210. end;
  211. }
  212. begin
  213. ctypeconvnode:=tm68ktypeconvnode;
  214. end.