n68kcnv.pas 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. location.loc:=LOC_FPUREGISTER;
  91. end;
  92. {*****************************************************************************
  93. SecondTypeConv
  94. *****************************************************************************}
  95. procedure tm68ktypeconvnode.second_int_to_real;
  96. var
  97. tempconst: trealconstnode;
  98. ref: treference;
  99. valuereg, tempreg, leftreg, tmpfpureg: tregister;
  100. signed : boolean;
  101. scratch_used : boolean;
  102. opsize : tcgsize;
  103. begin
  104. scratch_used := false;
  105. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  106. signed := is_signed(left.resultdef);
  107. opsize := def_cgsize(left.resultdef);
  108. { has to be handled by a helper }
  109. if is_64bitint(left.resultdef) then
  110. internalerror(200110011);
  111. { has to be handled by a helper }
  112. if not signed then
  113. internalerror(20020814);
  114. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,opsize);
  115. if not(left.location.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_REFERENCE,LOC_CREFERENCE]) then
  116. location_force_reg(current_asmdata.CurrAsmList,left.location,OS_INT,false);
  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. {$warning needs LOC_JUMP support, because called for bool_to_bool from ncgcnv }
  142. { Explicit typecasts from any ordinal type to a boolean type }
  143. { must not change the ordinal value }
  144. if (nf_explicit in flags) and
  145. (left.resultdef.size=resultdef.size) and
  146. not(left.location.loc in [LOC_FLAGS,LOC_JUMP]) then
  147. begin
  148. location_copy(location,left.location);
  149. location.size:=def_cgsize(resultdef);
  150. { change of sign? Then we have to sign/zero-extend in }
  151. { case of a loc_(c)register }
  152. if (location.size<>left.location.size) and
  153. (location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  154. location_force_reg(current_asmdata.CurrAsmList,location,location.size,true);
  155. { ACTIVATE when loc_jump support is added
  156. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  157. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  158. }
  159. exit;
  160. end;
  161. location_reset(location,LOC_REGISTER,def_cgsize(left.resultdef));
  162. opsize := def_cgsize(left.resultdef);
  163. case left.location.loc of
  164. LOC_CREFERENCE,LOC_REFERENCE :
  165. begin
  166. { can we optimize it, or do we need to fix the ref. ? }
  167. if isvalidrefoffset(left.location.reference) then
  168. begin
  169. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_TST,TCGSize2OpSize[opsize],
  170. left.location.reference));
  171. end
  172. else
  173. begin
  174. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  175. cg.a_load_ref_reg(current_asmdata.CurrAsmList,opsize,opsize,
  176. left.location.reference,hreg2);
  177. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[opsize],hreg2));
  178. // cg.ungetcpuregister(current_asmdata.CurrAsmList,hreg2);
  179. end;
  180. // reference_release(current_asmdata.CurrAsmList,left.location.reference);
  181. resflags:=F_NE;
  182. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  183. end;
  184. LOC_REGISTER,LOC_CREGISTER :
  185. begin
  186. hreg2:=left.location.register;
  187. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[opsize],hreg2));
  188. // cg.ungetcpuregister(current_asmdata.CurrAsmList,hreg2);
  189. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  190. resflags:=F_NE;
  191. end;
  192. LOC_FLAGS :
  193. begin
  194. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  195. resflags:=left.location.resflags;
  196. end;
  197. else
  198. internalerror(200512182);
  199. end;
  200. cg.g_flags2reg(current_asmdata.CurrAsmList,location.size,resflags,hreg1);
  201. if (is_cbool(resultdef)) then
  202. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,location.size,hreg1,hreg1);
  203. location.register := hreg1;
  204. end;
  205. {
  206. procedure tm68ktypeconvnode.pass_generate_code;
  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]) then
  216. begin
  217. secondpass(left);
  218. location_copy(location,left.location);
  219. if codegenerror then
  220. exit;
  221. end;
  222. second_call_helper(convtype);
  223. end;
  224. }
  225. begin
  226. ctypeconvnode:=tm68ktypeconvnode;
  227. end.