n68kcnv.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. tempconst: trealconstnode;
  103. ref: treference;
  104. valuereg, tempreg, leftreg, tmpfpureg: tregister;
  105. signed : boolean;
  106. scratch_used : boolean;
  107. opsize : tcgsize;
  108. begin
  109. scratch_used := false;
  110. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  111. signed := is_signed(left.resultdef);
  112. opsize := def_cgsize(left.resultdef);
  113. { has to be handled by a helper }
  114. if is_64bitint(left.resultdef) then
  115. internalerror(200110011);
  116. { has to be handled by a helper }
  117. if not signed then
  118. internalerror(2002081404);
  119. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,opsize);
  120. if not(left.location.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_REFERENCE,LOC_CREFERENCE]) then
  121. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,osuinttype,false);
  122. case left.location.loc of
  123. LOC_REGISTER, LOC_CREGISTER:
  124. begin
  125. leftreg := left.location.register;
  126. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FMOVE,TCGSize2OpSize[opsize],leftreg,
  127. location.register));
  128. end;
  129. LOC_REFERENCE,LOC_CREFERENCE:
  130. begin
  131. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_FMOVE,TCGSize2OpSize[opsize],
  132. left.location.reference,location.register));
  133. end
  134. else
  135. internalerror(200110012);
  136. end;
  137. end;
  138. procedure tm68ktypeconvnode.second_int_to_bool;
  139. var
  140. hreg1,
  141. hreg2 : tregister;
  142. reg64 : tregister64;
  143. resflags : tresflags;
  144. opsize : tcgsize;
  145. newsize : tcgsize;
  146. hlabel,
  147. oldTrueLabel,
  148. oldFalseLabel : tasmlabel;
  149. tmpreference : treference;
  150. begin
  151. oldTrueLabel:=current_procinfo.CurrTrueLabel;
  152. oldFalseLabel:=current_procinfo.CurrFalseLabel;
  153. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  154. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  155. secondpass(left);
  156. { Explicit typecasts from any ordinal type to a boolean type }
  157. { must not change the ordinal value }
  158. if (nf_explicit in flags) and
  159. not(left.location.loc in [LOC_FLAGS,LOC_JUMP]) then
  160. begin
  161. location_copy(location,left.location);
  162. newsize:=def_cgsize(resultdef);
  163. { change of size? change sign only if location is LOC_(C)REGISTER? Then we have to sign/zero-extend }
  164. if (tcgsize2size[newsize]<>tcgsize2size[left.location.size]) or
  165. ((newsize<>left.location.size) and (location.loc in [LOC_REGISTER,LOC_CREGISTER])) then
  166. hlcg.location_force_reg(current_asmdata.CurrAsmList,location,left.resultdef,resultdef,true)
  167. else
  168. location.size:=newsize;
  169. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  170. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  171. exit;
  172. end;
  173. resflags:=F_NE;
  174. newsize:=def_cgsize(resultdef);
  175. opsize := def_cgsize(left.resultdef);
  176. case left.location.loc of
  177. LOC_CREFERENCE,LOC_REFERENCE :
  178. begin
  179. if opsize in [OS_64,OS_S64] then
  180. begin
  181. reg64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  182. reg64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  183. cg64.a_load64_loc_reg(current_asmdata.CurrAsmList,left.location,reg64);
  184. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_OR,S_L,reg64.reghi,reg64.reglo));
  185. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,S_L,reg64.reglo));
  186. end
  187. else
  188. begin
  189. { can we optimize it, or do we need to fix the ref. ? }
  190. if isvalidrefoffset(left.location.reference) then
  191. begin
  192. { Coldfire cannot handle tst.l 123(dX) }
  193. if (current_settings.cputype in (cpu_coldfire + [cpu_mc68000])) and
  194. isintregister(left.location.reference.base) then
  195. begin
  196. tmpreference:=left.location.reference;
  197. hreg2:=cg.getaddressregister(current_asmdata.CurrAsmList);
  198. tmpreference.base:=hreg2;
  199. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_MOVE,S_L,left.location.reference.base,hreg2));
  200. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_TST,TCGSize2OpSize[opsize],tmpreference));
  201. end
  202. else
  203. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_TST,TCGSize2OpSize[opsize],left.location.reference));
  204. end
  205. else
  206. begin
  207. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  208. cg.a_load_ref_reg(current_asmdata.CurrAsmList,opsize,opsize,
  209. left.location.reference,hreg2);
  210. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[opsize],hreg2));
  211. end;
  212. end;
  213. end;
  214. LOC_REGISTER,LOC_CREGISTER :
  215. begin
  216. if opsize in [OS_64,OS_S64] then
  217. begin
  218. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  219. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_MOVE,S_L,left.location.register64.reglo,hreg2));
  220. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_OR,S_L,left.location.register64.reghi,hreg2));
  221. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,S_L,hreg2));
  222. end
  223. else
  224. begin
  225. hreg2:=left.location.register;
  226. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[opsize],hreg2));
  227. end;
  228. end;
  229. LOC_FLAGS :
  230. begin
  231. resflags:=left.location.resflags;
  232. end;
  233. LOC_JUMP :
  234. begin
  235. { for now blindly copied from nx86cnv }
  236. location_reset(location,LOC_REGISTER,newsize);
  237. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  238. current_asmdata.getjumplabel(hlabel);
  239. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  240. if not(is_cbool(resultdef)) then
  241. cg.a_load_const_reg(current_asmdata.CurrAsmList,location.size,1,location.register)
  242. else
  243. cg.a_load_const_reg(current_asmdata.CurrAsmList,location.size,-1,location.register);
  244. cg.a_jmp_always(current_asmdata.CurrAsmList,hlabel);
  245. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  246. cg.a_load_const_reg(current_asmdata.CurrAsmList,location.size,0,location.register);
  247. cg.a_label(current_asmdata.CurrAsmList,hlabel);
  248. end;
  249. else
  250. internalerror(200512182);
  251. end;
  252. if left.location.loc<>LOC_JUMP then
  253. begin
  254. location_reset(location,LOC_REGISTER,newsize);
  255. if newsize in [OS_64,OS_S64] then
  256. begin
  257. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  258. cg.g_flags2reg(current_asmdata.CurrAsmList,OS_32,resflags,hreg2);
  259. if (is_cbool(resultdef)) then
  260. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,OS_32,hreg2,hreg2);
  261. location.register64.reglo:=hreg2;
  262. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  263. if (is_cbool(resultdef)) then
  264. { reglo is either 0 or -1 -> reghi has to become the same }
  265. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,location.register64.reglo,location.register64.reghi)
  266. else
  267. { unsigned }
  268. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,location.register64.reghi);
  269. end
  270. else
  271. begin
  272. location.register:=cg.getintregister(current_asmdata.CurrAsmList,newsize);
  273. cg.g_flags2reg(current_asmdata.CurrAsmList,newsize,resflags,location.register);
  274. if (is_cbool(resultdef)) then
  275. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,newsize,location.register,location.register);
  276. end
  277. end;
  278. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  279. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  280. end;
  281. begin
  282. ctypeconvnode:=tm68ktypeconvnode;
  283. end.