n68kcnv.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. { 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,false);
  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. begin
  161. location.size:=newsize;
  162. if (location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  163. begin
  164. inc(location.reference.offset,TCGSize2Size[left.location.size]-TCGSize2Size[location.size]);
  165. l.reference.alignment:=newalignment(l.reference.alignment,TCGSize2Size[left.location.size]-TCGSize2Size[location.size]);
  166. end;
  167. end;
  168. exit;
  169. end;
  170. resflags:=F_NE;
  171. newsize:=def_cgsize(resultdef);
  172. opsize := def_cgsize(left.resultdef);
  173. if (left.location.loc in [LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF]) or
  174. ((left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and needs_unaligned(left.location.reference.alignment,opsize)) then
  175. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  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. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('typeconvnode second_int_to_bool #1')));
  182. reg64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  183. reg64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  184. cg64.a_load64_loc_reg(current_asmdata.CurrAsmList,left.location,reg64);
  185. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_OR,S_L,reg64.reghi,reg64.reglo));
  186. // it's not necessary to call TST after OR, which sets the flags as required already
  187. //current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,S_L,reg64.reglo));
  188. end
  189. else
  190. begin
  191. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('typeconvnode second_int_to_bool #2')));
  192. tmpreference:=left.location.reference;
  193. tcg68k(cg).fixref(current_asmdata.CurrAsmList,tmpreference,false);
  194. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_TST,TCGSize2OpSize[opsize],tmpreference));
  195. end;
  196. end;
  197. LOC_REGISTER,LOC_CREGISTER :
  198. begin
  199. if opsize in [OS_64,OS_S64] then
  200. begin
  201. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('typeconvnode second_int_to_bool #3')));
  202. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  203. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_MOVE,S_L,left.location.register64.reglo,hreg2));
  204. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_OR,S_L,left.location.register64.reghi,hreg2));
  205. // it's not necessary to call TST after OR, which sets the flags as required already
  206. //current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,S_L,hreg2));
  207. end
  208. else
  209. begin
  210. if (current_settings.cputype = cpu_mc68000) and isaddressregister(left.location.register) then
  211. begin
  212. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  213. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,opsize,left.location.register,hreg2);
  214. end
  215. else
  216. hreg2:=left.location.register;
  217. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[opsize],hreg2));
  218. end;
  219. end;
  220. LOC_FLAGS :
  221. begin
  222. resflags:=left.location.resflags;
  223. end;
  224. LOC_JUMP :
  225. begin
  226. { for now blindly copied from nx86cnv }
  227. location_reset(location,LOC_REGISTER,newsize);
  228. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  229. current_asmdata.getjumplabel(hlabel);
  230. cg.a_label(current_asmdata.CurrAsmList,left.location.truelabel);
  231. if not(is_cbool(resultdef)) then
  232. cg.a_load_const_reg(current_asmdata.CurrAsmList,location.size,1,location.register)
  233. else
  234. cg.a_load_const_reg(current_asmdata.CurrAsmList,location.size,-1,location.register);
  235. cg.a_jmp_always(current_asmdata.CurrAsmList,hlabel);
  236. cg.a_label(current_asmdata.CurrAsmList,left.location.falselabel);
  237. cg.a_load_const_reg(current_asmdata.CurrAsmList,location.size,0,location.register);
  238. cg.a_label(current_asmdata.CurrAsmList,hlabel);
  239. end;
  240. else
  241. internalerror(200512182);
  242. end;
  243. if left.location.loc<>LOC_JUMP then
  244. begin
  245. location_reset(location,LOC_REGISTER,newsize);
  246. if newsize in [OS_64,OS_S64] then
  247. begin
  248. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  249. cg.g_flags2reg(current_asmdata.CurrAsmList,OS_32,resflags,hreg2);
  250. if (is_cbool(resultdef)) then
  251. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,OS_32,hreg2,hreg2);
  252. location.register64.reglo:=hreg2;
  253. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  254. if (is_cbool(resultdef)) then
  255. { reglo is either 0 or -1 -> reghi has to become the same }
  256. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,location.register64.reglo,location.register64.reghi)
  257. else
  258. { unsigned }
  259. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,location.register64.reghi);
  260. end
  261. else
  262. begin
  263. location.register:=cg.getintregister(current_asmdata.CurrAsmList,newsize);
  264. cg.g_flags2reg(current_asmdata.CurrAsmList,newsize,resflags,location.register);
  265. if (is_cbool(resultdef)) then
  266. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,newsize,location.register,location.register);
  267. end
  268. end;
  269. end;
  270. begin
  271. ctypeconvnode:=tm68ktypeconvnode;
  272. end.