n68kcnv.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 typecheck_int_to_int: tnode; override;
  26. function first_int_to_real: tnode; override;
  27. procedure second_int_to_real;override;
  28. procedure second_int_to_bool;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,procinfo,
  36. ncon,ncal,ninl,compinnr,
  37. ncgutil,
  38. cpubase,cpuinfo,aasmcpu,
  39. rgobj,tgobj,cgobj,hlcgobj,cgutils,globtype,cgcpu,cutils;
  40. function tm68ktypeconvnode.typecheck_int_to_int : tnode;
  41. begin
  42. if (left.nodetype=inlinen) then
  43. if (cs_opt_fastmath in current_settings.optimizerswitches) and
  44. (((tinlinenode(left).inlinenumber = in_trunc_real) and (FPUM68K_HAS_FINTRZ in fpu_capabilities[current_settings.fputype])) or
  45. ((tinlinenode(left).inlinenumber = in_round_real) and (FPUM68K_HAS_HARDWARE in fpu_capabilities[current_settings.fputype]))) and
  46. (resultdef.typ=orddef) and (torddef(resultdef).ordtype in [u8bit,u16bit,s8bit,s16bit,s32bit]) then
  47. begin
  48. { this triggers the special codepath for trunc/round inline nodes on m68k (KB) }
  49. left.resultdef:=s32inttype;
  50. end;
  51. result:=inherited typecheck_int_to_int;
  52. end;
  53. {*****************************************************************************
  54. FirstTypeConv
  55. *****************************************************************************}
  56. function tm68ktypeconvnode.first_int_to_real: tnode;
  57. var
  58. fname: string[32];
  59. begin
  60. { In case we are in emulation mode, we must
  61. always call the helpers
  62. }
  63. if (cs_fp_emulation in current_settings.moduleswitches)
  64. or (current_settings.fputype=fpu_soft) then
  65. begin
  66. result := inherited first_int_to_real;
  67. exit;
  68. end
  69. else
  70. { converting a 64bit integer to a float requires a helper }
  71. if is_64bitint(left.resultdef) or
  72. is_currency(left.resultdef) then
  73. begin
  74. { hack to avoid double division by 10000, as it's
  75. already done by typecheckpass.resultdef_int_to_real }
  76. if is_currency(left.resultdef) then
  77. left.resultdef := s64inttype;
  78. if is_signed(left.resultdef) then
  79. fname := 'fpc_int64_to_double'
  80. else
  81. fname := 'fpc_qword_to_double';
  82. result := ccallnode.createintern(fname,ccallparanode.create(
  83. left,nil));
  84. left:=nil;
  85. firstpass(result);
  86. exit;
  87. end
  88. else
  89. begin
  90. { The FPU can load any size int, but only signed. Therefore, we convert
  91. 16 and 8 bit unsigned to 32bit signed, the rest we can load directly,
  92. and we have a special codepath for 32bit unsigned in second pass (KB) }
  93. if not (is_32bitint(left.resultdef) or is_signed(left.resultdef)) then
  94. begin
  95. inserttypeconv(left,s32inttype);
  96. firstpass(left);
  97. end;
  98. end;
  99. result := nil;
  100. location.loc:=LOC_FPUREGISTER;
  101. end;
  102. {*****************************************************************************
  103. SecondTypeConv
  104. *****************************************************************************}
  105. procedure tm68ktypeconvnode.second_int_to_real;
  106. var
  107. l: tasmlabel;
  108. ref: treference;
  109. tempref: treference;
  110. leftreg: tregister;
  111. signed : boolean;
  112. opsize : tcgsize;
  113. begin
  114. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  115. signed := is_signed(left.resultdef);
  116. opsize := def_cgsize(left.resultdef);
  117. { has to be handled by a helper }
  118. if is_64bitint(left.resultdef) then
  119. internalerror(200110011);
  120. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,opsize);
  121. if not signed then
  122. begin
  123. // current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('typeconvnode second_int_to_real cardinal')));
  124. { the idea behind this code is based on the cardinal to double code in the PPC and x86 CG (KB) }
  125. tg.GetTemp(current_asmdata.CurrAsmList,sizeof(double),sizeof(double),tt_normal,tempref);
  126. hlcg.a_load_const_ref(current_asmdata.CurrAsmList,u32inttype,$43300000,tempref);
  127. inc(tempref.offset,sizeof(aint));
  128. hlcg.a_load_loc_ref(current_asmdata.CurrAsmList,left.resultdef,u32inttype,left.location,tempref);
  129. dec(tempref.offset,sizeof(aint));
  130. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_FMOVE,S_FD,tempref,location.register));
  131. if current_settings.fputype in [fpu_coldfire] then
  132. begin
  133. current_asmdata.getglobaldatalabel(l);
  134. new_section(current_asmdata.asmlists[al_typedconsts],sec_rodata_norel,l.name,const_align(sizeof(pint)));
  135. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(l));
  136. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_32bit($59800000));
  137. reference_reset_symbol(ref,l,0,4,[]);
  138. tcg68k(cg).fixref(current_asmdata.CurrAsmList,ref,true);
  139. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_FSUB,S_FS,ref,location.register));
  140. end
  141. else
  142. { using single here for (1 shl 52) is safe, the optimizer would simplify it anyway }
  143. current_asmdata.CurrAsmList.concat(taicpu.op_realconst_reg(A_FSUB,S_FS,(1 shl 52),location.register));
  144. tg.UnGetTemp(current_asmdata.CurrAsmList,tempref);
  145. exit;
  146. end;
  147. if not(left.location.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_REFERENCE,LOC_CREFERENCE]) then
  148. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,osuinttype,false);
  149. case left.location.loc of
  150. LOC_REGISTER, LOC_CREGISTER:
  151. begin
  152. leftreg:=tcg68k(cg).force_to_dataregister(current_asmdata.CurrAsmList,left.location.size,left.location.register);
  153. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FMOVE,TCGSize2OpSize[opsize],leftreg,
  154. location.register));
  155. end;
  156. LOC_REFERENCE,LOC_CREFERENCE:
  157. begin
  158. ref:=left.location.reference;
  159. tcg68k(cg).fixref(current_asmdata.CurrAsmList,ref,false);
  160. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_FMOVE,TCGSize2OpSize[opsize],ref,location.register));
  161. end
  162. else
  163. internalerror(200110012);
  164. end;
  165. end;
  166. procedure tm68ktypeconvnode.second_int_to_bool;
  167. var
  168. hreg1,
  169. hreg2 : tregister;
  170. reg64 : tregister64;
  171. resflags : tresflags;
  172. opsize : tcgsize;
  173. newsize : tcgsize;
  174. hlabel : tasmlabel;
  175. tmpreference : treference;
  176. begin
  177. secondpass(left);
  178. { Explicit typecasts from any ordinal type to a boolean type }
  179. { must not change the ordinal value }
  180. if (nf_explicit in flags) and
  181. not(left.location.loc in [LOC_FLAGS,LOC_JUMP]) then
  182. begin
  183. location_copy(location,left.location);
  184. newsize:=def_cgsize(resultdef);
  185. { change of size? change sign only if location is LOC_(C)REGISTER? Then we have to sign/zero-extend }
  186. if (tcgsize2size[newsize]>tcgsize2size[left.location.size]) or
  187. ((newsize<>left.location.size) and (location.loc in [LOC_REGISTER,LOC_CREGISTER])) then
  188. hlcg.location_force_reg(current_asmdata.CurrAsmList,location,left.resultdef,resultdef,true)
  189. else
  190. begin
  191. location.size:=newsize;
  192. if (location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  193. begin
  194. inc(location.reference.offset,TCGSize2Size[left.location.size]-TCGSize2Size[location.size]);
  195. location.reference.alignment:=newalignment(location.reference.alignment,TCGSize2Size[left.location.size]-TCGSize2Size[location.size]);
  196. end;
  197. end;
  198. exit;
  199. end;
  200. resflags:=F_NE;
  201. newsize:=def_cgsize(resultdef);
  202. opsize := def_cgsize(left.resultdef);
  203. if (left.location.loc in [LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF]) or
  204. ((left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and needs_unaligned(left.location.reference.alignment,opsize)) then
  205. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  206. case left.location.loc of
  207. LOC_CREFERENCE,LOC_REFERENCE :
  208. begin
  209. if opsize in [OS_64,OS_S64] then
  210. begin
  211. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('typeconvnode second_int_to_bool #1')));
  212. reg64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  213. reg64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  214. cg64.a_load64_loc_reg(current_asmdata.CurrAsmList,left.location,reg64);
  215. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_OR,S_L,reg64.reghi,reg64.reglo));
  216. // it's not necessary to call TST after OR, which sets the flags as required already
  217. //current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,S_L,reg64.reglo));
  218. end
  219. else
  220. begin
  221. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('typeconvnode second_int_to_bool #2')));
  222. tmpreference:=left.location.reference;
  223. tcg68k(cg).fixref(current_asmdata.CurrAsmList,tmpreference,false);
  224. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_TST,TCGSize2OpSize[opsize],tmpreference));
  225. end;
  226. end;
  227. LOC_REGISTER,LOC_CREGISTER :
  228. begin
  229. if opsize in [OS_64,OS_S64] then
  230. begin
  231. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('typeconvnode second_int_to_bool #3')));
  232. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  233. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_MOVE,S_L,left.location.register64.reglo,hreg2));
  234. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_OR,S_L,left.location.register64.reghi,hreg2));
  235. // it's not necessary to call TST after OR, which sets the flags as required already
  236. //current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,S_L,hreg2));
  237. end
  238. else
  239. begin
  240. if (current_settings.cputype = cpu_mc68000) and isaddressregister(left.location.register) then
  241. begin
  242. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  243. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,opsize,left.location.register,hreg2);
  244. end
  245. else
  246. hreg2:=left.location.register;
  247. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[opsize],hreg2));
  248. end;
  249. end;
  250. LOC_FLAGS :
  251. begin
  252. resflags:=left.location.resflags;
  253. end;
  254. LOC_JUMP :
  255. begin
  256. { for now blindly copied from nx86cnv }
  257. location_reset(location,LOC_REGISTER,newsize);
  258. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  259. current_asmdata.getjumplabel(hlabel);
  260. cg.a_label(current_asmdata.CurrAsmList,left.location.truelabel);
  261. if not(is_cbool(resultdef)) then
  262. cg.a_load_const_reg(current_asmdata.CurrAsmList,location.size,1,location.register)
  263. else
  264. cg.a_load_const_reg(current_asmdata.CurrAsmList,location.size,-1,location.register);
  265. cg.a_jmp_always(current_asmdata.CurrAsmList,hlabel);
  266. cg.a_label(current_asmdata.CurrAsmList,left.location.falselabel);
  267. cg.a_load_const_reg(current_asmdata.CurrAsmList,location.size,0,location.register);
  268. cg.a_label(current_asmdata.CurrAsmList,hlabel);
  269. end;
  270. else
  271. internalerror(200512182);
  272. end;
  273. if left.location.loc<>LOC_JUMP then
  274. begin
  275. location_reset(location,LOC_REGISTER,newsize);
  276. if newsize in [OS_64,OS_S64] then
  277. begin
  278. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  279. cg.g_flags2reg(current_asmdata.CurrAsmList,OS_32,resflags,hreg2);
  280. if (is_cbool(resultdef)) then
  281. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,OS_32,hreg2,hreg2);
  282. location.register64.reglo:=hreg2;
  283. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  284. if (is_cbool(resultdef)) then
  285. { reglo is either 0 or -1 -> reghi has to become the same }
  286. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,location.register64.reglo,location.register64.reghi)
  287. else
  288. { unsigned }
  289. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,location.register64.reghi);
  290. end
  291. else
  292. begin
  293. location.register:=cg.getintregister(current_asmdata.CurrAsmList,newsize);
  294. cg.g_flags2reg(current_asmdata.CurrAsmList,newsize,resflags,location.register);
  295. if (is_cbool(resultdef)) then
  296. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,newsize,location.register,location.register);
  297. end
  298. end;
  299. end;
  300. begin
  301. ctypeconvnode:=tm68ktypeconvnode;
  302. end.