n68kcnv.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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,cutils;
  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. begin
  76. { The FPU can load any size int, but only signed. Therefore, we convert
  77. 16 and 8 bit unsigned to 32bit signed, the rest we can load directly,
  78. and we have a special codepath for 32bit unsigned in second pass (KB) }
  79. if not (is_32bitint(left.resultdef) or is_signed(left.resultdef)) then
  80. begin
  81. inserttypeconv(left,s32inttype);
  82. firstpass(left);
  83. end;
  84. end;
  85. result := nil;
  86. location.loc:=LOC_FPUREGISTER;
  87. end;
  88. {*****************************************************************************
  89. SecondTypeConv
  90. *****************************************************************************}
  91. procedure tm68ktypeconvnode.second_int_to_real;
  92. var
  93. l: tasmlabel;
  94. ref: treference;
  95. tempref: treference;
  96. leftreg: tregister;
  97. signed : boolean;
  98. opsize : tcgsize;
  99. begin
  100. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  101. signed := is_signed(left.resultdef);
  102. opsize := def_cgsize(left.resultdef);
  103. { has to be handled by a helper }
  104. if is_64bitint(left.resultdef) then
  105. internalerror(200110011);
  106. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,opsize);
  107. if not signed then
  108. begin
  109. // current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('typeconvnode second_int_to_real cardinal')));
  110. { the idea behind this code is based on the cardinal to double code in the PPC and x86 CG (KB) }
  111. tg.GetTemp(current_asmdata.CurrAsmList,sizeof(double),sizeof(double),tt_normal,tempref);
  112. hlcg.a_load_const_ref(current_asmdata.CurrAsmList,u32inttype,$43300000,tempref);
  113. inc(tempref.offset,sizeof(aint));
  114. hlcg.a_load_loc_ref(current_asmdata.CurrAsmList,left.resultdef,u32inttype,left.location,tempref);
  115. dec(tempref.offset,sizeof(aint));
  116. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_FMOVE,S_FD,tempref,location.register));
  117. if current_settings.fputype in [fpu_coldfire] then
  118. begin
  119. current_asmdata.getglobaldatalabel(l);
  120. new_section(current_asmdata.asmlists[al_typedconsts],sec_rodata_norel,l.name,const_align(sizeof(pint)));
  121. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(l));
  122. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_32bit($59800000));
  123. reference_reset_symbol(ref,l,0,4,[]);
  124. tcg68k(cg).fixref(current_asmdata.CurrAsmList,ref,true);
  125. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_FSUB,S_FS,ref,location.register));
  126. end
  127. else
  128. { using single here for (1 shl 52) is safe, the optimizer would simplify it anyway }
  129. current_asmdata.CurrAsmList.concat(taicpu.op_realconst_reg(A_FSUB,S_FS,(1 shl 52),location.register));
  130. tg.UnGetTemp(current_asmdata.CurrAsmList,tempref);
  131. exit;
  132. end;
  133. if not(left.location.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_REFERENCE,LOC_CREFERENCE]) then
  134. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,osuinttype,false);
  135. case left.location.loc of
  136. LOC_REGISTER, LOC_CREGISTER:
  137. begin
  138. leftreg:=tcg68k(cg).force_to_dataregister(current_asmdata.CurrAsmList,left.location.size,left.location.register);
  139. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FMOVE,TCGSize2OpSize[opsize],leftreg,
  140. location.register));
  141. end;
  142. LOC_REFERENCE,LOC_CREFERENCE:
  143. begin
  144. ref:=left.location.reference;
  145. tcg68k(cg).fixref(current_asmdata.CurrAsmList,ref,false);
  146. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_FMOVE,TCGSize2OpSize[opsize],ref,location.register));
  147. end
  148. else
  149. internalerror(200110012);
  150. end;
  151. end;
  152. procedure tm68ktypeconvnode.second_int_to_bool;
  153. var
  154. hreg1,
  155. hreg2 : tregister;
  156. reg64 : tregister64;
  157. resflags : tresflags;
  158. opsize : tcgsize;
  159. newsize : tcgsize;
  160. hlabel : tasmlabel;
  161. tmpreference : treference;
  162. begin
  163. secondpass(left);
  164. { Explicit typecasts from any ordinal type to a boolean type }
  165. { must not change the ordinal value }
  166. if (nf_explicit in flags) and
  167. not(left.location.loc in [LOC_FLAGS,LOC_JUMP]) then
  168. begin
  169. location_copy(location,left.location);
  170. newsize:=def_cgsize(resultdef);
  171. { change of size? change sign only if location is LOC_(C)REGISTER? Then we have to sign/zero-extend }
  172. if (tcgsize2size[newsize]>tcgsize2size[left.location.size]) or
  173. ((newsize<>left.location.size) and (location.loc in [LOC_REGISTER,LOC_CREGISTER])) then
  174. hlcg.location_force_reg(current_asmdata.CurrAsmList,location,left.resultdef,resultdef,true)
  175. else
  176. begin
  177. location.size:=newsize;
  178. if (location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  179. begin
  180. inc(location.reference.offset,TCGSize2Size[left.location.size]-TCGSize2Size[location.size]);
  181. location.reference.alignment:=newalignment(location.reference.alignment,TCGSize2Size[left.location.size]-TCGSize2Size[location.size]);
  182. end;
  183. end;
  184. exit;
  185. end;
  186. resflags:=F_NE;
  187. newsize:=def_cgsize(resultdef);
  188. opsize := def_cgsize(left.resultdef);
  189. if (left.location.loc in [LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF]) or
  190. ((left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and needs_unaligned(left.location.reference.alignment,opsize)) then
  191. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  192. case left.location.loc of
  193. LOC_CREFERENCE,LOC_REFERENCE :
  194. begin
  195. if opsize in [OS_64,OS_S64] then
  196. begin
  197. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('typeconvnode second_int_to_bool #1')));
  198. reg64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  199. reg64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  200. cg64.a_load64_loc_reg(current_asmdata.CurrAsmList,left.location,reg64);
  201. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_OR,S_L,reg64.reghi,reg64.reglo));
  202. // it's not necessary to call TST after OR, which sets the flags as required already
  203. //current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,S_L,reg64.reglo));
  204. end
  205. else
  206. begin
  207. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('typeconvnode second_int_to_bool #2')));
  208. tmpreference:=left.location.reference;
  209. tcg68k(cg).fixref(current_asmdata.CurrAsmList,tmpreference,false);
  210. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_TST,TCGSize2OpSize[opsize],tmpreference));
  211. end;
  212. end;
  213. LOC_REGISTER,LOC_CREGISTER :
  214. begin
  215. if opsize in [OS_64,OS_S64] then
  216. begin
  217. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('typeconvnode second_int_to_bool #3')));
  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. // it's not necessary to call TST after OR, which sets the flags as required already
  222. //current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,S_L,hreg2));
  223. end
  224. else
  225. begin
  226. if (current_settings.cputype = cpu_mc68000) and isaddressregister(left.location.register) then
  227. begin
  228. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  229. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,opsize,left.location.register,hreg2);
  230. end
  231. else
  232. hreg2:=left.location.register;
  233. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[opsize],hreg2));
  234. end;
  235. end;
  236. LOC_FLAGS :
  237. begin
  238. resflags:=left.location.resflags;
  239. end;
  240. LOC_JUMP :
  241. begin
  242. { for now blindly copied from nx86cnv }
  243. location_reset(location,LOC_REGISTER,newsize);
  244. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  245. current_asmdata.getjumplabel(hlabel);
  246. cg.a_label(current_asmdata.CurrAsmList,left.location.truelabel);
  247. if not(is_cbool(resultdef)) then
  248. cg.a_load_const_reg(current_asmdata.CurrAsmList,location.size,1,location.register)
  249. else
  250. cg.a_load_const_reg(current_asmdata.CurrAsmList,location.size,-1,location.register);
  251. cg.a_jmp_always(current_asmdata.CurrAsmList,hlabel);
  252. cg.a_label(current_asmdata.CurrAsmList,left.location.falselabel);
  253. cg.a_load_const_reg(current_asmdata.CurrAsmList,location.size,0,location.register);
  254. cg.a_label(current_asmdata.CurrAsmList,hlabel);
  255. end;
  256. else
  257. internalerror(200512182);
  258. end;
  259. if left.location.loc<>LOC_JUMP then
  260. begin
  261. location_reset(location,LOC_REGISTER,newsize);
  262. if newsize in [OS_64,OS_S64] then
  263. begin
  264. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  265. cg.g_flags2reg(current_asmdata.CurrAsmList,OS_32,resflags,hreg2);
  266. if (is_cbool(resultdef)) then
  267. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,OS_32,hreg2,hreg2);
  268. location.register64.reglo:=hreg2;
  269. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  270. if (is_cbool(resultdef)) then
  271. { reglo is either 0 or -1 -> reghi has to become the same }
  272. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,location.register64.reglo,location.register64.reghi)
  273. else
  274. { unsigned }
  275. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,location.register64.reghi);
  276. end
  277. else
  278. begin
  279. location.register:=cg.getintregister(current_asmdata.CurrAsmList,newsize);
  280. cg.g_flags2reg(current_asmdata.CurrAsmList,newsize,resflags,location.register);
  281. if (is_cbool(resultdef)) then
  282. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,newsize,location.register,location.register);
  283. end
  284. end;
  285. end;
  286. begin
  287. ctypeconvnode:=tm68ktypeconvnode;
  288. end.