n68kinl.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. {
  2. Copyright (c) 2015 by the Free Pascal Development team
  3. Generates Motorola 68k inline 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 n68kinl;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,ninl,ncginl,symtype,cpubase;
  22. type
  23. t68kinlinenode = class(tcgInlineNode)
  24. function first_abs_real: tnode; override;
  25. function first_sqr_real: tnode; override;
  26. function first_sqrt_real: tnode; override;
  27. {function first_arctan_real: tnode; override;
  28. function first_ln_real: tnode; override;}
  29. function first_cos_real: tnode; override;
  30. function first_sin_real: tnode; override;
  31. function first_int_real: tnode; override;
  32. function first_frac_real: tnode; override;
  33. function first_round_real: tnode; override;
  34. function first_trunc_real: tnode; override;
  35. procedure second_abs_real; override;
  36. procedure second_sqr_real; override;
  37. procedure second_sqrt_real; override;
  38. {procedure second_arctan_real; override;
  39. procedure second_ln_real; override;}
  40. procedure second_cos_real; override;
  41. procedure second_sin_real; override;
  42. procedure second_int_real; override;
  43. procedure second_frac_real; override;
  44. procedure second_round_real; override;
  45. procedure second_trunc_real; override;
  46. {procedure second_prefetch; override;
  47. procedure second_abs_long; override;}
  48. protected
  49. function second_incdec_tempregdef: tdef; override;
  50. private
  51. procedure second_do_operation(op: TAsmOp);
  52. end;
  53. implementation
  54. uses
  55. globtype,verbose,globals,cutils,
  56. cpuinfo,defutil,symdef,aasmdata,aasmcpu,aasmtai,
  57. cgbase,cgutils,pass_1,pass_2,symconst,
  58. ncnv,ncgutil,cgobj,cgcpu,hlcgobj;
  59. {*****************************************************************************
  60. t68kinlinenode
  61. *****************************************************************************}
  62. function t68kinlinenode.first_abs_real : tnode;
  63. begin
  64. if FPUM68K_HAS_HARDWARE in fpu_capabilities[current_settings.fputype] then
  65. begin
  66. expectloc:=LOC_FPUREGISTER;
  67. result:=nil;
  68. end
  69. else
  70. result:=inherited first_abs_real;
  71. end;
  72. function t68kinlinenode.first_sqr_real : tnode;
  73. begin
  74. if FPUM68K_HAS_HARDWARE in fpu_capabilities[current_settings.fputype] then
  75. begin
  76. expectloc:=LOC_FPUREGISTER;
  77. result:=nil;
  78. end
  79. else
  80. result:=inherited first_sqr_real;
  81. end;
  82. function t68kinlinenode.first_sqrt_real : tnode;
  83. begin
  84. if FPUM68K_HAS_HARDWARE in fpu_capabilities[current_settings.fputype] then
  85. begin
  86. expectloc:=LOC_FPUREGISTER;
  87. result:=nil;
  88. end
  89. else
  90. result:=inherited first_sqrt_real;
  91. end;
  92. function t68kinlinenode.first_sin_real : tnode;
  93. begin
  94. if FPUM68K_HAS_TRIGONOMETRY in fpu_capabilities[current_settings.fputype] then
  95. begin
  96. expectloc:=LOC_FPUREGISTER;
  97. result:=nil;
  98. end
  99. else
  100. result:=inherited first_sin_real;
  101. end;
  102. function t68kinlinenode.first_cos_real : tnode;
  103. begin
  104. if FPUM68K_HAS_TRIGONOMETRY in fpu_capabilities[current_settings.fputype] then
  105. begin
  106. expectloc:=LOC_FPUREGISTER;
  107. result:=nil;
  108. end
  109. else
  110. result:=inherited first_cos_real;
  111. end;
  112. function t68kinlinenode.first_int_real : tnode;
  113. begin
  114. if FPUM68K_HAS_FINTRZ in fpu_capabilities[current_settings.fputype] then
  115. begin
  116. expectloc:=LOC_FPUREGISTER;
  117. result:=nil;
  118. end
  119. else
  120. result:=inherited first_int_real;
  121. end;
  122. function t68kinlinenode.first_frac_real : tnode;
  123. begin
  124. if FPUM68K_HAS_FINTRZ in fpu_capabilities[current_settings.fputype] then
  125. begin
  126. expectloc:=LOC_FPUREGISTER;
  127. result:=nil;
  128. end
  129. else
  130. result:=inherited first_frac_real;
  131. end;
  132. function t68kinlinenode.first_trunc_real : tnode;
  133. begin
  134. if (FPUM68K_HAS_FINTRZ in fpu_capabilities[current_settings.fputype]) and
  135. (resultdef.typ=orddef) and (torddef(resultdef).ordtype in [u8bit,u16bit,s8bit,s16bit,s32bit]) then
  136. begin
  137. expectloc:=LOC_REGISTER;
  138. result:=nil;
  139. end
  140. else
  141. result:=inherited first_trunc_real;
  142. end;
  143. function t68kinlinenode.first_round_real : tnode;
  144. begin
  145. if (FPUM68K_HAS_HARDWARE in fpu_capabilities[current_settings.fputype]) and
  146. (resultdef.typ=orddef) and (torddef(resultdef).ordtype in [u8bit,u16bit,s8bit,s16bit,s32bit]) then
  147. begin
  148. expectloc:=LOC_REGISTER;
  149. result:=nil;
  150. end
  151. else
  152. result:=inherited first_round_real;
  153. end;
  154. procedure t68kinlinenode.second_abs_real;
  155. begin
  156. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('second_abs_real called!')));
  157. second_do_operation(A_FABS);
  158. end;
  159. procedure t68kinlinenode.second_sqr_real;
  160. begin
  161. secondpass(left);
  162. if not (FPUM68K_HAS_HARDWARE in fpu_capabilities[current_settings.fputype]) then
  163. internalerror(2015022202);
  164. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('second_sqr_real called!')));
  165. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  166. location_copy(location,left.location);
  167. if left.location.loc=LOC_CFPUREGISTER then
  168. begin
  169. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('second_sqr_real called!: left was cfpuregister!')));
  170. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  171. location.loc := LOC_FPUREGISTER;
  172. cg.a_loadfpu_reg_reg(current_asmdata.CurrAsmlist,OS_NO,OS_NO,left.location.register,location.register);
  173. end;
  174. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FMUL,fpuregopsize,left.location.register,location.register));
  175. end;
  176. procedure t68kinlinenode.second_sqrt_real;
  177. begin
  178. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('second_sqrt_real called!')));
  179. second_do_operation(A_FSQRT);
  180. end;
  181. procedure t68kinlinenode.second_sin_real;
  182. begin
  183. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('second_sin_real called!')));
  184. second_do_operation(A_FSIN);
  185. end;
  186. procedure t68kinlinenode.second_cos_real;
  187. begin
  188. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('second_cos_real called!')));
  189. second_do_operation(A_FCOS);
  190. end;
  191. procedure t68kinlinenode.second_int_real;
  192. begin
  193. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('second_int_real called!')));
  194. second_do_operation(A_FINTRZ);
  195. end;
  196. procedure t68kinlinenode.second_do_operation(op: TAsmOp);
  197. var
  198. href: TReference;
  199. begin
  200. secondpass(left);
  201. if not (FPUM68K_HAS_HARDWARE in fpu_capabilities[current_settings.fputype]) then
  202. internalerror(2015022204);
  203. location_reset(location,LOC_FPUREGISTER,left.location.size);
  204. case left.location.loc of
  205. LOC_FPUREGISTER:
  206. begin
  207. location.register:=left.location.register;
  208. current_asmdata.CurrAsmList.concat(taicpu.op_reg(op,fpuregopsize,location.register))
  209. end;
  210. LOC_CFPUREGISTER:
  211. begin
  212. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  213. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,fpuregopsize,left.location.register,location.register));
  214. end;
  215. LOC_REFERENCE,LOC_CREFERENCE:
  216. begin
  217. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  218. href:=left.location.reference;
  219. tcg68k(cg).fixref(current_asmdata.CurrAsmList,href,current_settings.fputype = fpu_coldfire);
  220. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(op,tcgsize2opsize[left.location.size],href,location.register));
  221. end;
  222. else
  223. internalerror(2015022205);
  224. end;
  225. end;
  226. procedure t68kinlinenode.second_frac_real;
  227. var
  228. href: TReference;
  229. hreg: TRegister;
  230. begin
  231. secondpass(left);
  232. if not (FPUM68K_HAS_FINTRZ in fpu_capabilities[current_settings.fputype]) then
  233. internalerror(2017052102);
  234. location_reset(location,LOC_FPUREGISTER,left.location.size);
  235. case left.location.loc of
  236. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  237. begin
  238. hreg:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  239. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  240. cg.a_loadfpu_reg_reg(current_asmdata.CurrAsmlist,OS_NO,OS_NO,left.location.register,location.register);
  241. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FINTRZ,fpuregopsize,left.location.register,hreg));
  242. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FSUB,fpuregopsize,hreg,location.register));
  243. end;
  244. LOC_REFERENCE,LOC_CREFERENCE:
  245. begin
  246. hreg:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  247. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  248. href:=left.location.reference;
  249. tcg68k(cg).fixref(current_asmdata.CurrAsmList,href,current_settings.fputype = fpu_coldfire);
  250. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmlist,left.location.size,OS_NO,href,location.register);
  251. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FINTRZ,fpuregopsize,location.register,hreg));
  252. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FSUB,fpuregopsize,hreg,location.register));
  253. end;
  254. else
  255. internalerror(2017052101);
  256. end;
  257. end;
  258. procedure t68kinlinenode.second_round_real;
  259. var
  260. size: tcgsize;
  261. begin
  262. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('second_round_real called!')));
  263. secondpass(left);
  264. size:=def_cgsize(resultdef);
  265. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  266. location_reset(location,LOC_REGISTER,size);
  267. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  268. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FMOVE,tcgsize2opsize[size],left.location.register,location.register));
  269. end;
  270. procedure t68kinlinenode.second_trunc_real;
  271. var
  272. hreg: TRegister;
  273. size: tcgsize;
  274. begin
  275. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('second_trunc_real called!')));
  276. second_do_operation(A_FINTRZ);
  277. size:=def_cgsize(resultdef);
  278. hreg:=location.register;
  279. location_reset(location,LOC_REGISTER,size);
  280. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  281. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FMOVE,tcgsize2opsize[size],hreg,location.register));
  282. end;
  283. { ideas for second_abs_long (KB) }
  284. { This is probably faster on 68000 than the generic implementation,
  285. because shifting is slow on the original 68000, maybe also on the 68020?
  286. Also needs to be tested on 040/060. This can also work on a CF.
  287. input - d0, output - d2
  288. move.l d0,d2
  289. btst #31,d2
  290. sne d1
  291. extb.l d1 (or ext.w + ext.l on 68000)
  292. eor.l d1,d2
  293. sub.l d1,d2
  294. }
  295. { Solution using bitfield extraction, we don't support the necessary asm
  296. construct for this yet, probably this is the fastest on 020, slower on
  297. 040/060 than the one above, doesn't work on '000 or CF.
  298. input - d0, output - d2
  299. move.l d0,d2
  300. bfexts d0[0:1],d1
  301. eor.l d1,d2
  302. sub.l d1,d2
  303. }
  304. function t68kinlinenode.second_incdec_tempregdef: tdef;
  305. begin
  306. { this kludge results in the increment/decrement value of inc/dec to be loaded
  307. always in a datareg, regardless of the target type. This results in significantly
  308. better code on m68k, where if the inc/decrement is loaded to an address register
  309. for pointers, the compiler will generate a bunch of useless data<->address register
  310. shuffling, as it cannot do some operations on address registers (like shifting
  311. or multiplication) (KB) }
  312. second_incdec_tempregdef:=cgsize_orddef(def_cgsize(left.resultdef));
  313. end;
  314. begin
  315. cinlinenode:=t68kinlinenode;
  316. end.