n68kcnv.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. // procedure pass_generate_code;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,
  37. ncgutil,
  38. cpubase,cpuinfo,aasmcpu,
  39. rgobj,tgobj,cgobj,hlcgobj,cgutils,globtype,cgcpu;
  40. {*****************************************************************************
  41. FirstTypeConv
  42. *****************************************************************************}
  43. function tm68ktypeconvnode.first_int_to_real: tnode;
  44. var
  45. fname: string[32];
  46. begin
  47. { In case we are in emulation mode, we must
  48. always call the helpers
  49. }
  50. if (cs_fp_emulation in current_settings.moduleswitches)
  51. or (current_settings.fputype=fpu_soft) then
  52. begin
  53. result := inherited first_int_to_real;
  54. exit;
  55. end
  56. else
  57. { converting a 64bit integer to a float requires a helper }
  58. if is_64bitint(left.resultdef) then
  59. begin
  60. if is_signed(left.resultdef) then
  61. fname := 'fpc_int64_to_double'
  62. else
  63. fname := 'fpc_qword_to_double';
  64. result := ccallnode.createintern(fname,ccallparanode.create(
  65. left,nil));
  66. left:=nil;
  67. firstpass(result);
  68. exit;
  69. end
  70. else
  71. { other integers are supposed to be 32 bit }
  72. begin
  73. if is_signed(left.resultdef) then
  74. inserttypeconv(left,s32inttype)
  75. else
  76. { the fpu always considers 32-bit values as signed
  77. therefore we need to call the helper in case of
  78. a cardinal value.
  79. }
  80. begin
  81. fname := 'fpc_longword_to_double';
  82. result := ccallnode.createintern(fname,ccallparanode.create(
  83. left,nil));
  84. left:=nil;
  85. firstpass(result);
  86. exit;
  87. end;
  88. firstpass(left);
  89. end;
  90. result := nil;
  91. location.loc:=LOC_FPUREGISTER;
  92. end;
  93. {*****************************************************************************
  94. SecondTypeConv
  95. *****************************************************************************}
  96. procedure tm68ktypeconvnode.second_int_to_real;
  97. var
  98. tempconst: trealconstnode;
  99. ref: treference;
  100. valuereg, tempreg, leftreg, tmpfpureg: tregister;
  101. signed : boolean;
  102. scratch_used : boolean;
  103. opsize : tcgsize;
  104. begin
  105. scratch_used := false;
  106. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  107. signed := is_signed(left.resultdef);
  108. opsize := def_cgsize(left.resultdef);
  109. { has to be handled by a helper }
  110. if is_64bitint(left.resultdef) then
  111. internalerror(200110011);
  112. { has to be handled by a helper }
  113. if not signed then
  114. internalerror(2002081404);
  115. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,opsize);
  116. if not(left.location.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_REFERENCE,LOC_CREFERENCE]) then
  117. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,osuinttype,false);
  118. case left.location.loc of
  119. LOC_REGISTER, LOC_CREGISTER:
  120. begin
  121. leftreg := left.location.register;
  122. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FMOVE,TCGSize2OpSize[opsize],leftreg,
  123. location.register));
  124. end;
  125. LOC_REFERENCE,LOC_CREFERENCE:
  126. begin
  127. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_FMOVE,TCGSize2OpSize[opsize],
  128. left.location.reference,location.register));
  129. end
  130. else
  131. internalerror(200110012);
  132. end;
  133. end;
  134. procedure tm68ktypeconvnode.second_int_to_bool;
  135. var
  136. hreg1,
  137. hreg2 : tregister;
  138. reg64 : tregister64;
  139. resflags : tresflags;
  140. opsize : tcgsize;
  141. newsize : tcgsize;
  142. hlabel,
  143. oldTrueLabel,
  144. oldFalseLabel : tasmlabel;
  145. tmpreference : treference;
  146. begin
  147. oldTrueLabel:=current_procinfo.CurrTrueLabel;
  148. oldFalseLabel:=current_procinfo.CurrFalseLabel;
  149. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  150. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  151. secondpass(left);
  152. { Explicit typecasts from any ordinal type to a boolean type }
  153. { must not change the ordinal value }
  154. if (nf_explicit in flags) and
  155. not(left.location.loc in [LOC_FLAGS,LOC_JUMP]) then
  156. begin
  157. location_copy(location,left.location);
  158. newsize:=def_cgsize(resultdef);
  159. { change of size? change sign only if location is LOC_(C)REGISTER? Then we have to sign/zero-extend }
  160. if (tcgsize2size[newsize]<>tcgsize2size[left.location.size]) or
  161. ((newsize<>left.location.size) and (location.loc in [LOC_REGISTER,LOC_CREGISTER])) then
  162. hlcg.location_force_reg(current_asmdata.CurrAsmList,location,left.resultdef,resultdef,true)
  163. else
  164. location.size:=newsize;
  165. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  166. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  167. exit;
  168. end;
  169. location_reset(location,LOC_REGISTER,def_cgsize(left.resultdef));
  170. opsize := def_cgsize(left.resultdef);
  171. case left.location.loc of
  172. LOC_CREFERENCE,LOC_REFERENCE :
  173. begin
  174. if opsize in [OS_64,OS_S64] then
  175. begin
  176. reg64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  177. reg64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  178. cg64.a_load64_loc_reg(current_asmdata.CurrAsmList,left.location,reg64);
  179. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_OR,S_L,reg64.reghi,reg64.reglo));
  180. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,S_L,reg64.reglo));
  181. end
  182. else
  183. begin
  184. { can we optimize it, or do we need to fix the ref. ? }
  185. if isvalidrefoffset(left.location.reference) then
  186. begin
  187. { Coldfire cannot handle tst.l 123(dX) }
  188. if (current_settings.cputype in cpu_coldfire) and
  189. isintregister(left.location.reference.base) then
  190. begin
  191. tmpreference:=left.location.reference;
  192. hreg2:=cg.getaddressregister(current_asmdata.CurrAsmList);
  193. tmpreference.base:=hreg2;
  194. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_MOVE,S_L,left.location.reference.base,hreg2));
  195. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_TST,TCGSize2OpSize[opsize],tmpreference));
  196. end
  197. else
  198. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_TST,TCGSize2OpSize[opsize],left.location.reference));
  199. end
  200. else
  201. begin
  202. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  203. cg.a_load_ref_reg(current_asmdata.CurrAsmList,opsize,opsize,
  204. left.location.reference,hreg2);
  205. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[opsize],hreg2));
  206. // cg.ungetcpuregister(current_asmdata.CurrAsmList,hreg2);
  207. end;
  208. // reference_release(current_asmdata.CurrAsmList,left.location.reference);
  209. end;
  210. resflags:=F_NE;
  211. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  212. end;
  213. LOC_REGISTER,LOC_CREGISTER :
  214. begin
  215. if opsize in [OS_64,OS_S64] then
  216. begin
  217. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  218. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_MOVE,S_L,left.location.register64.reglo,hreg2));
  219. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_OR,S_L,left.location.register64.reghi,hreg2));
  220. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,S_L,hreg2));
  221. end
  222. else
  223. begin
  224. hreg2:=left.location.register;
  225. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[opsize],hreg2));
  226. // cg.ungetcpuregister(current_asmdata.CurrAsmList,hreg2);
  227. end;
  228. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  229. resflags:=F_NE;
  230. end;
  231. LOC_FLAGS :
  232. begin
  233. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  234. resflags:=left.location.resflags;
  235. end;
  236. LOC_JUMP :
  237. begin
  238. { for now blindly copied from nx86cnv }
  239. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  240. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  241. current_asmdata.getjumplabel(hlabel);
  242. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  243. if not(is_cbool(resultdef)) then
  244. cg.a_load_const_reg(current_asmdata.CurrAsmList,location.size,1,location.register)
  245. else
  246. cg.a_load_const_reg(current_asmdata.CurrAsmList,location.size,-1,location.register);
  247. cg.a_jmp_always(current_asmdata.CurrAsmList,hlabel);
  248. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  249. cg.a_load_const_reg(current_asmdata.CurrAsmList,location.size,0,location.register);
  250. cg.a_label(current_asmdata.CurrAsmList,hlabel);
  251. end;
  252. else
  253. internalerror(200512182);
  254. end;
  255. if left.location.loc<>LOC_JUMP then
  256. begin
  257. cg.g_flags2reg(current_asmdata.CurrAsmList,location.size,resflags,hreg1);
  258. if (is_cbool(resultdef)) then
  259. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,location.size,hreg1,hreg1);
  260. location.register := hreg1;
  261. end;
  262. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  263. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  264. end;
  265. {
  266. procedure tm68ktypeconvnode.pass_generate_code;
  267. {$ifdef TESTOBJEXT2}
  268. var
  269. r : preference;
  270. nillabel : plabel;
  271. {$endif TESTOBJEXT2}
  272. begin
  273. { this isn't good coding, I think tc_bool_2_int, shouldn't be }
  274. { type conversion (FK) }
  275. if not(convtype in [tc_bool_2_int,tc_bool_2_bool]) then
  276. begin
  277. secondpass(left);
  278. location_copy(location,left.location);
  279. if codegenerror then
  280. exit;
  281. end;
  282. second_call_helper(convtype);
  283. end;
  284. }
  285. begin
  286. ctypeconvnode:=tm68ktypeconvnode;
  287. end.