n68kcnv.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. end;
  29. implementation
  30. uses
  31. verbose,globals,systems,
  32. symconst,symdef,aasmbase,aasmtai,aasmdata,
  33. defutil,
  34. cgbase,pass_1,pass_2,procinfo,
  35. ncon,ncal,
  36. ncgutil,
  37. cpubase,cpuinfo,aasmcpu,
  38. rgobj,tgobj,cgobj,hlcgobj,cgutils,globtype,cgcpu;
  39. {*****************************************************************************
  40. FirstTypeConv
  41. *****************************************************************************}
  42. function tm68ktypeconvnode.first_int_to_real: tnode;
  43. var
  44. fname: string[32];
  45. begin
  46. { In case we are in emulation mode, we must
  47. always call the helpers
  48. }
  49. if (cs_fp_emulation in current_settings.moduleswitches)
  50. or (current_settings.fputype=fpu_soft) 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) or
  58. is_currency(left.resultdef) then
  59. begin
  60. { hack to avoid double division by 10000, as it's
  61. already done by typecheckpass.resultdef_int_to_real }
  62. if is_currency(left.resultdef) then
  63. left.resultdef := s64inttype;
  64. if is_signed(left.resultdef) then
  65. fname := 'fpc_int64_to_double'
  66. else
  67. fname := 'fpc_qword_to_double';
  68. result := ccallnode.createintern(fname,ccallparanode.create(
  69. left,nil));
  70. left:=nil;
  71. firstpass(result);
  72. exit;
  73. end
  74. else
  75. { other integers are supposed to be 32 bit }
  76. begin
  77. if is_signed(left.resultdef) then
  78. inserttypeconv(left,s32inttype)
  79. else
  80. { the fpu always considers 32-bit values as signed
  81. therefore we need to call the helper in case of
  82. a cardinal value.
  83. }
  84. begin
  85. fname := 'fpc_longword_to_double';
  86. result := ccallnode.createintern(fname,ccallparanode.create(
  87. left,nil));
  88. left:=nil;
  89. firstpass(result);
  90. exit;
  91. end;
  92. firstpass(left);
  93. end;
  94. result := nil;
  95. location.loc:=LOC_FPUREGISTER;
  96. end;
  97. {*****************************************************************************
  98. SecondTypeConv
  99. *****************************************************************************}
  100. procedure tm68ktypeconvnode.second_int_to_real;
  101. var
  102. ref: treference;
  103. leftreg: tregister;
  104. signed : boolean;
  105. opsize : tcgsize;
  106. begin
  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(2002081404);
  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. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,osuinttype,false);
  119. case left.location.loc of
  120. LOC_REGISTER, LOC_CREGISTER:
  121. begin
  122. leftreg:=tcg68k(cg).force_to_dataregister(current_asmdata.CurrAsmList,left.location.size,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. ref:=left.location.reference;
  129. tcg68k(cg).fixref(current_asmdata.CurrAsmList,ref);
  130. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_FMOVE,TCGSize2OpSize[opsize],ref,location.register));
  131. end
  132. else
  133. internalerror(200110012);
  134. end;
  135. end;
  136. procedure tm68ktypeconvnode.second_int_to_bool;
  137. var
  138. hreg1,
  139. hreg2 : tregister;
  140. reg64 : tregister64;
  141. resflags : tresflags;
  142. opsize : tcgsize;
  143. newsize : tcgsize;
  144. hlabel : tasmlabel;
  145. tmpreference : treference;
  146. begin
  147. secondpass(left);
  148. { Explicit typecasts from any ordinal type to a boolean type }
  149. { must not change the ordinal value }
  150. if (nf_explicit in flags) and
  151. not(left.location.loc in [LOC_FLAGS,LOC_JUMP]) then
  152. begin
  153. location_copy(location,left.location);
  154. newsize:=def_cgsize(resultdef);
  155. { change of size? change sign only if location is LOC_(C)REGISTER? Then we have to sign/zero-extend }
  156. if (tcgsize2size[newsize]<>tcgsize2size[left.location.size]) or
  157. ((newsize<>left.location.size) and (location.loc in [LOC_REGISTER,LOC_CREGISTER])) then
  158. hlcg.location_force_reg(current_asmdata.CurrAsmList,location,left.resultdef,resultdef,true)
  159. else
  160. location.size:=newsize;
  161. exit;
  162. end;
  163. resflags:=F_NE;
  164. newsize:=def_cgsize(resultdef);
  165. opsize := def_cgsize(left.resultdef);
  166. if (left.location.loc in [LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF]) then
  167. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  168. case left.location.loc of
  169. LOC_CREFERENCE,LOC_REFERENCE :
  170. begin
  171. if opsize in [OS_64,OS_S64] then
  172. begin
  173. reg64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  174. reg64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  175. cg64.a_load64_loc_reg(current_asmdata.CurrAsmList,left.location,reg64);
  176. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_OR,S_L,reg64.reghi,reg64.reglo));
  177. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,S_L,reg64.reglo));
  178. end
  179. else
  180. begin
  181. { can we optimize it, or do we need to fix the ref. ? }
  182. if isvalidrefoffset(left.location.reference) then
  183. begin
  184. { Coldfire cannot handle tst.l 123(dX) }
  185. if (current_settings.cputype in (cpu_coldfire + [cpu_mc68000])) and
  186. isintregister(left.location.reference.base) then
  187. begin
  188. tmpreference:=left.location.reference;
  189. hreg2:=cg.getaddressregister(current_asmdata.CurrAsmList);
  190. tmpreference.base:=hreg2;
  191. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_MOVE,S_L,left.location.reference.base,hreg2));
  192. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_TST,TCGSize2OpSize[opsize],tmpreference));
  193. end
  194. else
  195. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_TST,TCGSize2OpSize[opsize],left.location.reference));
  196. end
  197. else
  198. begin
  199. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  200. cg.a_load_ref_reg(current_asmdata.CurrAsmList,opsize,opsize,
  201. left.location.reference,hreg2);
  202. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[opsize],hreg2));
  203. end;
  204. end;
  205. end;
  206. LOC_REGISTER,LOC_CREGISTER :
  207. begin
  208. if opsize in [OS_64,OS_S64] then
  209. begin
  210. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  211. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_MOVE,S_L,left.location.register64.reglo,hreg2));
  212. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_OR,S_L,left.location.register64.reghi,hreg2));
  213. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,S_L,hreg2));
  214. end
  215. else
  216. begin
  217. hreg2:=left.location.register;
  218. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[opsize],hreg2));
  219. end;
  220. end;
  221. LOC_FLAGS :
  222. begin
  223. resflags:=left.location.resflags;
  224. end;
  225. LOC_JUMP :
  226. begin
  227. { for now blindly copied from nx86cnv }
  228. location_reset(location,LOC_REGISTER,newsize);
  229. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  230. current_asmdata.getjumplabel(hlabel);
  231. cg.a_label(current_asmdata.CurrAsmList,left.location.truelabel);
  232. if not(is_cbool(resultdef)) then
  233. cg.a_load_const_reg(current_asmdata.CurrAsmList,location.size,1,location.register)
  234. else
  235. cg.a_load_const_reg(current_asmdata.CurrAsmList,location.size,-1,location.register);
  236. cg.a_jmp_always(current_asmdata.CurrAsmList,hlabel);
  237. cg.a_label(current_asmdata.CurrAsmList,left.location.falselabel);
  238. cg.a_load_const_reg(current_asmdata.CurrAsmList,location.size,0,location.register);
  239. cg.a_label(current_asmdata.CurrAsmList,hlabel);
  240. end;
  241. else
  242. internalerror(200512182);
  243. end;
  244. if left.location.loc<>LOC_JUMP then
  245. begin
  246. location_reset(location,LOC_REGISTER,newsize);
  247. if newsize in [OS_64,OS_S64] then
  248. begin
  249. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  250. cg.g_flags2reg(current_asmdata.CurrAsmList,OS_32,resflags,hreg2);
  251. if (is_cbool(resultdef)) then
  252. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,OS_32,hreg2,hreg2);
  253. location.register64.reglo:=hreg2;
  254. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  255. if (is_cbool(resultdef)) then
  256. { reglo is either 0 or -1 -> reghi has to become the same }
  257. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,location.register64.reglo,location.register64.reghi)
  258. else
  259. { unsigned }
  260. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,location.register64.reghi);
  261. end
  262. else
  263. begin
  264. location.register:=cg.getintregister(current_asmdata.CurrAsmList,newsize);
  265. cg.g_flags2reg(current_asmdata.CurrAsmList,newsize,resflags,location.register);
  266. if (is_cbool(resultdef)) then
  267. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,newsize,location.register,location.register);
  268. end
  269. end;
  270. end;
  271. begin
  272. ctypeconvnode:=tm68ktypeconvnode;
  273. end.