n68kinl.pas 13 KB

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