n68kcnv.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. resflags : tresflags;
  139. opsize : tcgsize;
  140. newsize : tcgsize;
  141. hlabel,
  142. oldTrueLabel,
  143. oldFalseLabel : tasmlabel;
  144. begin
  145. oldTrueLabel:=current_procinfo.CurrTrueLabel;
  146. oldFalseLabel:=current_procinfo.CurrFalseLabel;
  147. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  148. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  149. secondpass(left);
  150. { Explicit typecasts from any ordinal type to a boolean type }
  151. { must not change the ordinal value }
  152. if (nf_explicit in flags) and
  153. not(left.location.loc in [LOC_FLAGS,LOC_JUMP]) then
  154. begin
  155. location_copy(location,left.location);
  156. newsize:=def_cgsize(resultdef);
  157. { change of size? change sign only if location is LOC_(C)REGISTER? Then we have to sign/zero-extend }
  158. if (tcgsize2size[newsize]<>tcgsize2size[left.location.size]) or
  159. ((newsize<>left.location.size) and (location.loc in [LOC_REGISTER,LOC_CREGISTER])) then
  160. hlcg.location_force_reg(current_asmdata.CurrAsmList,location,left.resultdef,resultdef,true)
  161. else
  162. location.size:=newsize;
  163. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  164. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  165. exit;
  166. end;
  167. location_reset(location,LOC_REGISTER,def_cgsize(left.resultdef));
  168. opsize := def_cgsize(left.resultdef);
  169. case left.location.loc of
  170. LOC_CREFERENCE,LOC_REFERENCE :
  171. begin
  172. { can we optimize it, or do we need to fix the ref. ? }
  173. if isvalidrefoffset(left.location.reference) then
  174. begin
  175. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_TST,TCGSize2OpSize[opsize],
  176. left.location.reference));
  177. end
  178. else
  179. begin
  180. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  181. cg.a_load_ref_reg(current_asmdata.CurrAsmList,opsize,opsize,
  182. left.location.reference,hreg2);
  183. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[opsize],hreg2));
  184. // cg.ungetcpuregister(current_asmdata.CurrAsmList,hreg2);
  185. end;
  186. // reference_release(current_asmdata.CurrAsmList,left.location.reference);
  187. resflags:=F_NE;
  188. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  189. end;
  190. LOC_REGISTER,LOC_CREGISTER :
  191. begin
  192. hreg2:=left.location.register;
  193. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[opsize],hreg2));
  194. // cg.ungetcpuregister(current_asmdata.CurrAsmList,hreg2);
  195. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  196. resflags:=F_NE;
  197. end;
  198. LOC_FLAGS :
  199. begin
  200. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  201. resflags:=left.location.resflags;
  202. end;
  203. LOC_JUMP :
  204. begin
  205. { for now blindly copied from nx86cnv }
  206. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  207. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  208. current_asmdata.getjumplabel(hlabel);
  209. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  210. if not(is_cbool(resultdef)) then
  211. cg.a_load_const_reg(current_asmdata.CurrAsmList,location.size,1,location.register)
  212. else
  213. cg.a_load_const_reg(current_asmdata.CurrAsmList,location.size,-1,location.register);
  214. cg.a_jmp_always(current_asmdata.CurrAsmList,hlabel);
  215. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  216. cg.a_load_const_reg(current_asmdata.CurrAsmList,location.size,0,location.register);
  217. cg.a_label(current_asmdata.CurrAsmList,hlabel);
  218. end;
  219. else
  220. internalerror(200512182);
  221. end;
  222. if left.location.loc<>LOC_JUMP then
  223. begin
  224. cg.g_flags2reg(current_asmdata.CurrAsmList,location.size,resflags,hreg1);
  225. if (is_cbool(resultdef)) then
  226. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,location.size,hreg1,hreg1);
  227. location.register := hreg1;
  228. end;
  229. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  230. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  231. end;
  232. {
  233. procedure tm68ktypeconvnode.pass_generate_code;
  234. {$ifdef TESTOBJEXT2}
  235. var
  236. r : preference;
  237. nillabel : plabel;
  238. {$endif TESTOBJEXT2}
  239. begin
  240. { this isn't good coding, I think tc_bool_2_int, shouldn't be }
  241. { type conversion (FK) }
  242. if not(convtype in [tc_bool_2_int,tc_bool_2_bool]) then
  243. begin
  244. secondpass(left);
  245. location_copy(location,left.location);
  246. if codegenerror then
  247. exit;
  248. end;
  249. second_call_helper(convtype);
  250. end;
  251. }
  252. begin
  253. ctypeconvnode:=tm68ktypeconvnode;
  254. end.