n68kinl.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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_length; override;
  36. procedure second_abs_real; override;
  37. procedure second_sqr_real; override;
  38. procedure second_sqrt_real; override;
  39. {procedure second_arctan_real; override;
  40. procedure second_ln_real; override;}
  41. procedure second_cos_real; override;
  42. procedure second_sin_real; override;
  43. procedure second_int_real; override;
  44. procedure second_frac_real; override;
  45. procedure second_round_real; override;
  46. procedure second_trunc_real; override;
  47. {procedure second_prefetch; override;
  48. procedure second_abs_long; override;}
  49. protected
  50. function second_incdec_tempregdef: tdef; override;
  51. private
  52. procedure second_do_operation(op: TAsmOp);
  53. end;
  54. implementation
  55. uses
  56. globtype,verbose,globals,cutils,
  57. cpuinfo,defutil,symdef,aasmbase,aasmdata,aasmcpu,aasmtai,
  58. cgbase,cgutils,pass_1,pass_2,symconst,
  59. ncnv,ncgutil,cgobj,cgcpu,hlcgobj;
  60. {*****************************************************************************
  61. t68kinlinenode
  62. *****************************************************************************}
  63. procedure t68kinlinenode.second_Length;
  64. var
  65. lengthlab,zerolab : tasmlabel;
  66. hregister : tregister;
  67. lendef : tdef;
  68. href : treference;
  69. begin
  70. secondpass(left);
  71. if is_shortstring(left.resultdef) then
  72. begin
  73. location_copy(location,left.location);
  74. location.size:=OS_8;
  75. end
  76. else
  77. begin
  78. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('second_length called!')));
  79. { length in ansi/wide strings and high in dynamic arrays is at offset -sizeof(pint) }
  80. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  81. current_asmdata.getjumplabel(zerolab);
  82. hregister:=hlcg.getregisterfordef(current_asmdata.CurrAsmList,resultdef);
  83. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,left.resultdef,OC_EQ,0,left.location.register,zerolab);
  84. { the length of a widestring is a 32 bit unsigned int. Since every
  85. character occupies 2 bytes, on a 32 bit platform you can express
  86. the maximum length using 31 bits. On a 64 bit platform, it may be
  87. 32 bits. This means that regardless of the platform, a location
  88. with size OS_SINT/ossinttype can hold the length without
  89. overflowing (this code returns an ossinttype value) }
  90. if is_widestring(left.resultdef) then
  91. lendef:=u32inttype
  92. else
  93. lendef:=ossinttype;
  94. { volatility of the ansistring/widestring refers to the volatility of the
  95. string pointer, not of the string data }
  96. hlcg.reference_reset_base(href,left.resultdef,left.location.register,-lendef.size,ctempposinvalid,lendef.alignment,[]);
  97. hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,lendef,resultdef,href,hregister);
  98. if is_widestring(left.resultdef) then
  99. hlcg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SHR,resultdef,1,hregister);
  100. { Dynamic arrays do not have their length attached but their maximum index }
  101. if is_dynamic_array(left.resultdef) then
  102. hlcg.a_op_const_reg(current_asmdata.CurrAsmList,OP_ADD,resultdef,1,hregister);
  103. current_asmdata.getjumplabel(lengthlab);
  104. hlcg.a_jmp_always(current_asmdata.CurrAsmlist,lengthlab);
  105. cg.a_label(current_asmdata.CurrAsmList,zerolab);
  106. hlcg.a_load_const_reg(current_asmdata.CurrAsmList,resultdef,0,hregister);
  107. cg.a_label(current_asmdata.CurrAsmList,lengthlab);
  108. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  109. location.register:=hregister;
  110. end;
  111. end;
  112. function t68kinlinenode.first_abs_real : tnode;
  113. begin
  114. if FPUM68K_HAS_HARDWARE in fpu_capabilities[current_settings.fputype] then
  115. begin
  116. expectloc:=LOC_FPUREGISTER;
  117. result:=nil;
  118. end
  119. else
  120. result:=inherited first_abs_real;
  121. end;
  122. function t68kinlinenode.first_sqr_real : tnode;
  123. begin
  124. if FPUM68K_HAS_HARDWARE in fpu_capabilities[current_settings.fputype] then
  125. begin
  126. expectloc:=LOC_FPUREGISTER;
  127. result:=nil;
  128. end
  129. else
  130. result:=inherited first_sqr_real;
  131. end;
  132. function t68kinlinenode.first_sqrt_real : tnode;
  133. begin
  134. if FPUM68K_HAS_HARDWARE in fpu_capabilities[current_settings.fputype] then
  135. begin
  136. expectloc:=LOC_FPUREGISTER;
  137. result:=nil;
  138. end
  139. else
  140. result:=inherited first_sqrt_real;
  141. end;
  142. function t68kinlinenode.first_sin_real : tnode;
  143. begin
  144. if FPUM68K_HAS_TRIGONOMETRY in fpu_capabilities[current_settings.fputype] then
  145. begin
  146. expectloc:=LOC_FPUREGISTER;
  147. result:=nil;
  148. end
  149. else
  150. result:=inherited first_sin_real;
  151. end;
  152. function t68kinlinenode.first_cos_real : tnode;
  153. begin
  154. if FPUM68K_HAS_TRIGONOMETRY in fpu_capabilities[current_settings.fputype] then
  155. begin
  156. expectloc:=LOC_FPUREGISTER;
  157. result:=nil;
  158. end
  159. else
  160. result:=inherited first_cos_real;
  161. end;
  162. function t68kinlinenode.first_int_real : tnode;
  163. begin
  164. if FPUM68K_HAS_FINTRZ in fpu_capabilities[current_settings.fputype] then
  165. begin
  166. expectloc:=LOC_FPUREGISTER;
  167. result:=nil;
  168. end
  169. else
  170. result:=inherited first_int_real;
  171. end;
  172. function t68kinlinenode.first_frac_real : tnode;
  173. begin
  174. if FPUM68K_HAS_FINTRZ in fpu_capabilities[current_settings.fputype] then
  175. begin
  176. expectloc:=LOC_FPUREGISTER;
  177. result:=nil;
  178. end
  179. else
  180. result:=inherited first_frac_real;
  181. end;
  182. function t68kinlinenode.first_trunc_real : tnode;
  183. begin
  184. if (FPUM68K_HAS_FINTRZ in fpu_capabilities[current_settings.fputype]) and
  185. (resultdef.typ=orddef) and (torddef(resultdef).ordtype in [u8bit,u16bit,s8bit,s16bit,s32bit]) then
  186. begin
  187. expectloc:=LOC_REGISTER;
  188. result:=nil;
  189. end
  190. else
  191. result:=inherited first_trunc_real;
  192. end;
  193. function t68kinlinenode.first_round_real : tnode;
  194. begin
  195. if (FPUM68K_HAS_HARDWARE in fpu_capabilities[current_settings.fputype]) and
  196. (resultdef.typ=orddef) and (torddef(resultdef).ordtype in [u8bit,u16bit,s8bit,s16bit,s32bit]) then
  197. begin
  198. expectloc:=LOC_REGISTER;
  199. result:=nil;
  200. end
  201. else
  202. result:=inherited first_round_real;
  203. end;
  204. procedure t68kinlinenode.second_abs_real;
  205. begin
  206. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('second_abs_real called!')));
  207. second_do_operation(A_FABS);
  208. end;
  209. procedure t68kinlinenode.second_sqr_real;
  210. begin
  211. secondpass(left);
  212. if not (FPUM68K_HAS_HARDWARE in fpu_capabilities[current_settings.fputype]) then
  213. internalerror(2015022202);
  214. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('second_sqr_real called!')));
  215. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  216. location_copy(location,left.location);
  217. if left.location.loc=LOC_CFPUREGISTER then
  218. begin
  219. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('second_sqr_real called!: left was cfpuregister!')));
  220. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  221. location.loc := LOC_FPUREGISTER;
  222. cg.a_loadfpu_reg_reg(current_asmdata.CurrAsmlist,left.location.size,location.size,left.location.register,location.register);
  223. end;
  224. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FMUL,fpuregopsize,left.location.register,location.register));
  225. end;
  226. procedure t68kinlinenode.second_sqrt_real;
  227. begin
  228. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('second_sqrt_real called!')));
  229. second_do_operation(A_FSQRT);
  230. end;
  231. procedure t68kinlinenode.second_sin_real;
  232. begin
  233. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('second_sin_real called!')));
  234. second_do_operation(A_FSIN);
  235. end;
  236. procedure t68kinlinenode.second_cos_real;
  237. begin
  238. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('second_cos_real called!')));
  239. second_do_operation(A_FCOS);
  240. end;
  241. procedure t68kinlinenode.second_int_real;
  242. begin
  243. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('second_int_real called!')));
  244. second_do_operation(A_FINTRZ);
  245. end;
  246. procedure t68kinlinenode.second_do_operation(op: TAsmOp);
  247. var
  248. href: TReference;
  249. begin
  250. secondpass(left);
  251. if not (FPUM68K_HAS_HARDWARE in fpu_capabilities[current_settings.fputype]) then
  252. internalerror(2015022204);
  253. location_reset(location,LOC_FPUREGISTER,left.location.size);
  254. case left.location.loc of
  255. LOC_FPUREGISTER:
  256. begin
  257. location.register:=left.location.register;
  258. current_asmdata.CurrAsmList.concat(taicpu.op_reg(op,fpuregopsize,location.register))
  259. end;
  260. LOC_CFPUREGISTER:
  261. begin
  262. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  263. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,fpuregopsize,left.location.register,location.register));
  264. end;
  265. LOC_REFERENCE,LOC_CREFERENCE:
  266. begin
  267. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  268. href:=left.location.reference;
  269. tcg68k(cg).fixref(current_asmdata.CurrAsmList,href,current_settings.fputype = fpu_coldfire);
  270. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(op,tcgsize2opsize[left.location.size],href,location.register));
  271. end;
  272. else
  273. internalerror(2015022205);
  274. end;
  275. end;
  276. procedure t68kinlinenode.second_frac_real;
  277. var
  278. href: TReference;
  279. hreg: TRegister;
  280. begin
  281. secondpass(left);
  282. if not (FPUM68K_HAS_FINTRZ in fpu_capabilities[current_settings.fputype]) then
  283. internalerror(2017052102);
  284. location_reset(location,LOC_FPUREGISTER,left.location.size);
  285. case left.location.loc of
  286. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  287. begin
  288. hreg:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  289. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  290. cg.a_loadfpu_reg_reg(current_asmdata.CurrAsmlist,left.location.size,location.size,left.location.register,location.register);
  291. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FINTRZ,fpuregopsize,left.location.register,hreg));
  292. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FSUB,fpuregopsize,hreg,location.register));
  293. end;
  294. LOC_REFERENCE,LOC_CREFERENCE:
  295. begin
  296. hreg:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  297. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  298. href:=left.location.reference;
  299. tcg68k(cg).fixref(current_asmdata.CurrAsmList,href,current_settings.fputype = fpu_coldfire);
  300. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmlist,left.location.size,location.size,href,location.register);
  301. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FINTRZ,fpuregopsize,location.register,hreg));
  302. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FSUB,fpuregopsize,hreg,location.register));
  303. end;
  304. else
  305. internalerror(2017052101);
  306. end;
  307. end;
  308. procedure t68kinlinenode.second_round_real;
  309. var
  310. size: tcgsize;
  311. begin
  312. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('second_round_real called!')));
  313. secondpass(left);
  314. size:=def_cgsize(resultdef);
  315. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  316. location_reset(location,LOC_REGISTER,size);
  317. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  318. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FMOVE,tcgsize2opsize[size],left.location.register,location.register));
  319. end;
  320. procedure t68kinlinenode.second_trunc_real;
  321. var
  322. hreg: TRegister;
  323. size: tcgsize;
  324. begin
  325. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('second_trunc_real called!')));
  326. second_do_operation(A_FINTRZ);
  327. size:=def_cgsize(resultdef);
  328. hreg:=location.register;
  329. location_reset(location,LOC_REGISTER,size);
  330. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  331. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FMOVE,tcgsize2opsize[size],hreg,location.register));
  332. end;
  333. { ideas for second_abs_long (KB) }
  334. { This is probably faster on 68000 than the generic implementation,
  335. because shifting is slow on the original 68000, maybe also on the 68020?
  336. Also needs to be tested on 040/060. This can also work on a CF.
  337. input - d0, output - d2
  338. move.l d0,d2
  339. btst #31,d2
  340. sne d1
  341. extb.l d1 (or ext.w + ext.l on 68000)
  342. eor.l d1,d2
  343. sub.l d1,d2
  344. }
  345. { Solution using bitfield extraction, we don't support the necessary asm
  346. construct for this yet, probably this is the fastest on 020, slower on
  347. 040/060 than the one above, doesn't work on '000 or CF.
  348. input - d0, output - d2
  349. move.l d0,d2
  350. bfexts d0[0:1],d1
  351. eor.l d1,d2
  352. sub.l d1,d2
  353. }
  354. function t68kinlinenode.second_incdec_tempregdef: tdef;
  355. begin
  356. { this kludge results in the increment/decrement value of inc/dec to be loaded
  357. always in a datareg, regardless of the target type. This results in significantly
  358. better code on m68k, where if the inc/decrement is loaded to an address register
  359. for pointers, the compiler will generate a bunch of useless data<->address register
  360. shuffling, as it cannot do some operations on address registers (like shifting
  361. or multiplication) (KB) }
  362. second_incdec_tempregdef:=cgsize_orddef(def_cgsize(left.resultdef));
  363. end;
  364. begin
  365. cinlinenode:=t68kinlinenode;
  366. end.