n68kcnv.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. if (left.location.loc in [LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF]) then
  177. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  178. case left.location.loc of
  179. LOC_CREFERENCE,LOC_REFERENCE :
  180. begin
  181. if opsize in [OS_64,OS_S64] then
  182. begin
  183. reg64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  184. reg64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  185. cg64.a_load64_loc_reg(current_asmdata.CurrAsmList,left.location,reg64);
  186. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_OR,S_L,reg64.reghi,reg64.reglo));
  187. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,S_L,reg64.reglo));
  188. end
  189. else
  190. begin
  191. { can we optimize it, or do we need to fix the ref. ? }
  192. if isvalidrefoffset(left.location.reference) then
  193. begin
  194. { Coldfire cannot handle tst.l 123(dX) }
  195. if (current_settings.cputype in (cpu_coldfire + [cpu_mc68000])) and
  196. isintregister(left.location.reference.base) then
  197. begin
  198. tmpreference:=left.location.reference;
  199. hreg2:=cg.getaddressregister(current_asmdata.CurrAsmList);
  200. tmpreference.base:=hreg2;
  201. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_MOVE,S_L,left.location.reference.base,hreg2));
  202. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_TST,TCGSize2OpSize[opsize],tmpreference));
  203. end
  204. else
  205. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_TST,TCGSize2OpSize[opsize],left.location.reference));
  206. end
  207. else
  208. begin
  209. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  210. cg.a_load_ref_reg(current_asmdata.CurrAsmList,opsize,opsize,
  211. left.location.reference,hreg2);
  212. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[opsize],hreg2));
  213. end;
  214. end;
  215. end;
  216. LOC_REGISTER,LOC_CREGISTER :
  217. begin
  218. if opsize in [OS_64,OS_S64] then
  219. begin
  220. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  221. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_MOVE,S_L,left.location.register64.reglo,hreg2));
  222. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_OR,S_L,left.location.register64.reghi,hreg2));
  223. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,S_L,hreg2));
  224. end
  225. else
  226. begin
  227. hreg2:=left.location.register;
  228. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[opsize],hreg2));
  229. end;
  230. end;
  231. LOC_FLAGS :
  232. begin
  233. resflags:=left.location.resflags;
  234. end;
  235. LOC_JUMP :
  236. begin
  237. { for now blindly copied from nx86cnv }
  238. location_reset(location,LOC_REGISTER,newsize);
  239. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  240. current_asmdata.getjumplabel(hlabel);
  241. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  242. if not(is_cbool(resultdef)) then
  243. cg.a_load_const_reg(current_asmdata.CurrAsmList,location.size,1,location.register)
  244. else
  245. cg.a_load_const_reg(current_asmdata.CurrAsmList,location.size,-1,location.register);
  246. cg.a_jmp_always(current_asmdata.CurrAsmList,hlabel);
  247. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  248. cg.a_load_const_reg(current_asmdata.CurrAsmList,location.size,0,location.register);
  249. cg.a_label(current_asmdata.CurrAsmList,hlabel);
  250. end;
  251. else
  252. internalerror(200512182);
  253. end;
  254. if left.location.loc<>LOC_JUMP then
  255. begin
  256. location_reset(location,LOC_REGISTER,newsize);
  257. if newsize in [OS_64,OS_S64] then
  258. begin
  259. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  260. cg.g_flags2reg(current_asmdata.CurrAsmList,OS_32,resflags,hreg2);
  261. if (is_cbool(resultdef)) then
  262. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,OS_32,hreg2,hreg2);
  263. location.register64.reglo:=hreg2;
  264. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  265. if (is_cbool(resultdef)) then
  266. { reglo is either 0 or -1 -> reghi has to become the same }
  267. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,location.register64.reglo,location.register64.reghi)
  268. else
  269. { unsigned }
  270. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,location.register64.reghi);
  271. end
  272. else
  273. begin
  274. location.register:=cg.getintregister(current_asmdata.CurrAsmList,newsize);
  275. cg.g_flags2reg(current_asmdata.CurrAsmList,newsize,resflags,location.register);
  276. if (is_cbool(resultdef)) then
  277. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,newsize,location.register,location.register);
  278. end
  279. end;
  280. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  281. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  282. end;
  283. begin
  284. ctypeconvnode:=tm68ktypeconvnode;
  285. end.