n68kcnv.pas 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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_generate_code;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 current_settings.moduleswitches) 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.resultdef) then
  58. begin
  59. if is_signed(left.resultdef) 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.resultdef) 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(resultdef));
  108. signed := is_signed(left.resultdef);
  109. opsize := def_cgsize(left.resultdef);
  110. { has to be handled by a helper }
  111. if is_64bitint(left.resultdef) 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. if not(left.location.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_REFERENCE,LOC_CREFERENCE]) then
  118. location_force_reg(current_asmdata.CurrAsmList,left.location,OS_INT,false);
  119. case left.location.loc of
  120. LOC_REGISTER, LOC_CREGISTER:
  121. begin
  122. leftreg := left.location.register;
  123. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FMOVE,TCGSize2OpSize[opsize],leftreg,
  124. location.register));
  125. end;
  126. LOC_REFERENCE,LOC_CREFERENCE:
  127. begin
  128. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_FMOVE,TCGSize2OpSize[opsize],
  129. left.location.reference,location.register));
  130. end
  131. else
  132. internalerror(200110012);
  133. end;
  134. end;
  135. procedure tm68ktypeconvnode.second_int_to_bool;
  136. var
  137. hreg1,
  138. hreg2 : tregister;
  139. resflags : tresflags;
  140. opsize : tcgsize;
  141. begin
  142. secondpass(left);
  143. { byte(boolean) or word(wordbool) or longint(longbool) must }
  144. { be accepted for var parameters }
  145. if (nf_explicit in flags) and
  146. (left.resultdef.size=resultdef.size) and
  147. (left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  148. begin
  149. location_copy(location,left.location);
  150. exit;
  151. end;
  152. location_reset(location,LOC_REGISTER,def_cgsize(left.resultdef));
  153. opsize := def_cgsize(left.resultdef);
  154. case left.location.loc of
  155. LOC_CREFERENCE,LOC_REFERENCE :
  156. begin
  157. { can we optimize it, or do we need to fix the ref. ? }
  158. if isvalidrefoffset(left.location.reference) then
  159. begin
  160. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_TST,TCGSize2OpSize[opsize],
  161. left.location.reference));
  162. end
  163. else
  164. begin
  165. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  166. cg.a_load_ref_reg(current_asmdata.CurrAsmList,opsize,opsize,
  167. left.location.reference,hreg2);
  168. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[opsize],hreg2));
  169. // cg.ungetcpuregister(current_asmdata.CurrAsmList,hreg2);
  170. end;
  171. // reference_release(current_asmdata.CurrAsmList,left.location.reference);
  172. resflags:=F_NE;
  173. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  174. end;
  175. LOC_REGISTER,LOC_CREGISTER :
  176. begin
  177. hreg2:=left.location.register;
  178. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[opsize],hreg2));
  179. // cg.ungetcpuregister(current_asmdata.CurrAsmList,hreg2);
  180. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  181. resflags:=F_NE;
  182. end;
  183. LOC_FLAGS :
  184. begin
  185. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  186. resflags:=left.location.resflags;
  187. end;
  188. else
  189. internalerror(200512182);
  190. end;
  191. cg.g_flags2reg(current_asmdata.CurrAsmList,location.size,resflags,hreg1);
  192. location.register := hreg1;
  193. end;
  194. {
  195. procedure tm68ktypeconvnode.pass_generate_code;
  196. {$ifdef TESTOBJEXT2}
  197. var
  198. r : preference;
  199. nillabel : plabel;
  200. {$endif TESTOBJEXT2}
  201. begin
  202. { this isn't good coding, I think tc_bool_2_int, shouldn't be }
  203. { type conversion (FK) }
  204. if not(convtype in [tc_bool_2_int,tc_bool_2_bool]) then
  205. begin
  206. secondpass(left);
  207. location_copy(location,left.location);
  208. if codegenerror then
  209. exit;
  210. end;
  211. second_call_helper(convtype);
  212. end;
  213. }
  214. begin
  215. ctypeconvnode:=tm68ktypeconvnode;
  216. end.