n68kcnv.pas 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. { bytebool(byte) or wordbool(word) or longbool(longint) must }
  143. { be accepted for var parameters, and must not change the }
  144. { the ordinal value }
  145. if (nf_explicit in flags) and
  146. (left.resultdef.size=resultdef.size) and
  147. not(left.location.loc in [LOC_FLAGS,LOC_JUMP]) and
  148. is_cbool(resultdef) and
  149. not is_pasbool(left.resultdef) then
  150. begin
  151. location_copy(location,left.location);
  152. location.size:=def_cgsize(resultdef);
  153. { change of sign? Then we have to sign/zero-extend in }
  154. { case of a loc_(c)register }
  155. if (location.size<>left.location.size) and
  156. (location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  157. location_force_reg(current_asmdata.CurrAsmList,location,location.size,true);
  158. { ACTIVATE when loc_jump support is added
  159. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  160. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  161. }
  162. exit;
  163. end;
  164. location_reset(location,LOC_REGISTER,def_cgsize(left.resultdef));
  165. opsize := def_cgsize(left.resultdef);
  166. case left.location.loc of
  167. LOC_CREFERENCE,LOC_REFERENCE :
  168. begin
  169. { can we optimize it, or do we need to fix the ref. ? }
  170. if isvalidrefoffset(left.location.reference) then
  171. begin
  172. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_TST,TCGSize2OpSize[opsize],
  173. left.location.reference));
  174. end
  175. else
  176. begin
  177. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  178. cg.a_load_ref_reg(current_asmdata.CurrAsmList,opsize,opsize,
  179. left.location.reference,hreg2);
  180. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[opsize],hreg2));
  181. // cg.ungetcpuregister(current_asmdata.CurrAsmList,hreg2);
  182. end;
  183. // reference_release(current_asmdata.CurrAsmList,left.location.reference);
  184. resflags:=F_NE;
  185. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  186. end;
  187. LOC_REGISTER,LOC_CREGISTER :
  188. begin
  189. hreg2:=left.location.register;
  190. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[opsize],hreg2));
  191. // cg.ungetcpuregister(current_asmdata.CurrAsmList,hreg2);
  192. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  193. resflags:=F_NE;
  194. end;
  195. LOC_FLAGS :
  196. begin
  197. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  198. resflags:=left.location.resflags;
  199. end;
  200. else
  201. internalerror(200512182);
  202. end;
  203. cg.g_flags2reg(current_asmdata.CurrAsmList,location.size,resflags,hreg1);
  204. if (is_cbool(resultdef)) then
  205. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,location.size,hreg1,hreg1);
  206. location.register := hreg1;
  207. end;
  208. {
  209. procedure tm68ktypeconvnode.pass_generate_code;
  210. {$ifdef TESTOBJEXT2}
  211. var
  212. r : preference;
  213. nillabel : plabel;
  214. {$endif TESTOBJEXT2}
  215. begin
  216. { this isn't good coding, I think tc_bool_2_int, shouldn't be }
  217. { type conversion (FK) }
  218. if not(convtype in [tc_bool_2_int,tc_bool_2_bool]) then
  219. begin
  220. secondpass(left);
  221. location_copy(location,left.location);
  222. if codegenerror then
  223. exit;
  224. end;
  225. second_call_helper(convtype);
  226. end;
  227. }
  228. begin
  229. ctypeconvnode:=tm68ktypeconvnode;
  230. end.