narminl.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generates ARM 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 narminl;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,ninl,ncginl;
  22. type
  23. tarminlinenode = 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_fma : tnode; override;
  28. { atn,sin,cos,lgn isn't supported by the linux fpe
  29. function first_arctan_real: tnode; override;
  30. function first_ln_real: tnode; override;
  31. function first_cos_real: tnode; override;
  32. function first_sin_real: tnode; override;
  33. }
  34. procedure second_abs_real; override;
  35. procedure second_sqr_real; override;
  36. procedure second_sqrt_real; override;
  37. { atn,sin,cos,lgn isn't supported by the linux fpe
  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. }
  43. procedure second_prefetch; override;
  44. procedure second_abs_long; override;
  45. procedure second_fma; override;
  46. private
  47. procedure load_fpu_location(out singleprec: boolean);
  48. end;
  49. implementation
  50. uses
  51. globtype,verbose,globals,
  52. cpuinfo, defutil,symdef,aasmdata,aasmcpu,
  53. cgbase,cgutils,pass_1,pass_2,
  54. cpubase,ncgutil,cgobj,cgcpu, hlcgobj,
  55. nutils,ncal;
  56. {*****************************************************************************
  57. tarminlinenode
  58. *****************************************************************************}
  59. procedure tarminlinenode.load_fpu_location(out singleprec: boolean);
  60. begin
  61. secondpass(left);
  62. case current_settings.fputype of
  63. fpu_fpa,
  64. fpu_fpa10,
  65. fpu_fpa11:
  66. begin
  67. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  68. location_copy(location,left.location);
  69. if left.location.loc=LOC_CFPUREGISTER then
  70. begin
  71. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  72. location.loc := LOC_FPUREGISTER;
  73. end;
  74. end;
  75. fpu_vfp_first..fpu_vfp_last:
  76. begin
  77. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  78. location_copy(location,left.location);
  79. if left.location.loc=LOC_CMMREGISTER then
  80. begin
  81. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  82. location.loc := LOC_MMREGISTER;
  83. end;
  84. end;
  85. fpu_soft:
  86. begin
  87. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  88. location_copy(location,left.location);
  89. end
  90. else
  91. internalerror(2009111801);
  92. end;
  93. singleprec:=tfloatdef(left.resultdef).floattype=s32real;
  94. end;
  95. function tarminlinenode.first_abs_real : tnode;
  96. begin
  97. if (cs_fp_emulation in current_settings.moduleswitches) then
  98. begin
  99. firstpass(left);
  100. expectloc:=LOC_REGISTER;
  101. first_abs_real:=nil;
  102. end
  103. else
  104. begin
  105. case current_settings.fputype of
  106. fpu_fpa,
  107. fpu_fpa10,
  108. fpu_fpa11:
  109. expectloc:=LOC_FPUREGISTER;
  110. fpu_vfpv2,
  111. fpu_vfpv3,
  112. fpu_vfpv4,
  113. fpu_vfpv3_d16:
  114. expectloc:=LOC_MMREGISTER;
  115. fpu_fpv4_s16:
  116. begin
  117. if tfloatdef(left.resultdef).floattype=s32real then
  118. expectloc:=LOC_MMREGISTER
  119. else
  120. exit(inherited first_abs_real);
  121. end;
  122. else
  123. internalerror(2009112401);
  124. end;
  125. first_abs_real:=nil;
  126. end;
  127. end;
  128. function tarminlinenode.first_sqr_real : tnode;
  129. begin
  130. if (cs_fp_emulation in current_settings.moduleswitches) then
  131. result:=inherited first_sqr_real
  132. else
  133. begin
  134. case current_settings.fputype of
  135. fpu_fpa,
  136. fpu_fpa10,
  137. fpu_fpa11:
  138. expectloc:=LOC_FPUREGISTER;
  139. fpu_vfpv2,
  140. fpu_vfpv3,
  141. fpu_vfpv4,
  142. fpu_vfpv3_d16:
  143. expectloc:=LOC_MMREGISTER;
  144. fpu_fpv4_s16:
  145. begin
  146. if tfloatdef(left.resultdef).floattype=s32real then
  147. expectloc:=LOC_MMREGISTER
  148. else
  149. exit(inherited first_sqr_real);
  150. end;
  151. else
  152. internalerror(2009112402);
  153. end;
  154. first_sqr_real:=nil;
  155. end;
  156. end;
  157. function tarminlinenode.first_sqrt_real : tnode;
  158. begin
  159. if cs_fp_emulation in current_settings.moduleswitches then
  160. result:=inherited first_sqrt_real
  161. else
  162. begin
  163. case current_settings.fputype of
  164. fpu_fpa,
  165. fpu_fpa10,
  166. fpu_fpa11:
  167. expectloc:=LOC_FPUREGISTER;
  168. fpu_vfpv2,
  169. fpu_vfpv3,
  170. fpu_vfpv4,
  171. fpu_vfpv3_d16:
  172. expectloc:=LOC_MMREGISTER;
  173. fpu_fpv4_s16:
  174. begin
  175. if tfloatdef(left.resultdef).floattype=s32real then
  176. expectloc:=LOC_MMREGISTER
  177. else
  178. exit(inherited first_sqrt_real);
  179. end;
  180. else
  181. internalerror(2009112403);
  182. end;
  183. first_sqrt_real := nil;
  184. end;
  185. end;
  186. function tarminlinenode.first_fma : tnode;
  187. begin
  188. if (true) and
  189. ((is_double(resultdef)) or (is_single(resultdef))) then
  190. begin
  191. expectloc:=LOC_MMREGISTER;
  192. Result:=nil;
  193. end
  194. else
  195. Result:=inherited first_fma;
  196. end;
  197. { atn,sin,cos,lgn isn't supported by the linux fpe
  198. function tarminlinenode.first_arctan_real: tnode;
  199. begin
  200. expectloc:=LOC_FPUREGISTER;
  201. result:=nil;
  202. end;
  203. function tarminlinenode.first_ln_real: tnode;
  204. begin
  205. expectloc:=LOC_FPUREGISTER;
  206. result:=nil;
  207. end;
  208. function tarminlinenode.first_cos_real: tnode;
  209. begin
  210. expectloc:=LOC_FPUREGISTER;
  211. result:=nil;
  212. end;
  213. function tarminlinenode.first_sin_real: tnode;
  214. begin
  215. expectloc:=LOC_FPUREGISTER;
  216. result:=nil;
  217. end;
  218. }
  219. procedure tarminlinenode.second_abs_real;
  220. var
  221. singleprec: boolean;
  222. pf: TOpPostfix;
  223. begin
  224. load_fpu_location(singleprec);
  225. case current_settings.fputype of
  226. fpu_fpa,
  227. fpu_fpa10,
  228. fpu_fpa11:
  229. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_ABS,location.register,left.location.register),get_fpu_postfix(resultdef)));
  230. fpu_vfpv2,
  231. fpu_vfpv3,
  232. fpu_vfpv4,
  233. fpu_vfpv3_d16:
  234. begin
  235. if singleprec then
  236. pf:=PF_F32
  237. else
  238. pf:=PF_F64;
  239. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_VABS,location.register,left.location.register),pf));
  240. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  241. end;
  242. fpu_fpv4_s16:
  243. begin
  244. current_asmdata.CurrAsmList.Concat(setoppostfix(taicpu.op_reg_reg(A_VABS,location.register,left.location.register), PF_F32));
  245. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  246. end;
  247. fpu_soft:
  248. begin
  249. if singleprec then
  250. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_AND,OS_32,tcgint($7fffffff),location.register)
  251. else
  252. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_AND,OS_32,tcgint($7fffffff),location.registerhi);
  253. end
  254. else
  255. internalerror(2009111402);
  256. end;
  257. end;
  258. procedure tarminlinenode.second_sqr_real;
  259. var
  260. singleprec: boolean;
  261. pf: TOpPostfix;
  262. begin
  263. load_fpu_location(singleprec);
  264. case current_settings.fputype of
  265. fpu_fpa,
  266. fpu_fpa10,
  267. fpu_fpa11:
  268. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg_reg(A_MUF,location.register,left.location.register,left.location.register),get_fpu_postfix(resultdef)));
  269. fpu_vfpv2,
  270. fpu_vfpv3,
  271. fpu_vfpv4,
  272. fpu_vfpv3_d16:
  273. begin
  274. if singleprec then
  275. pf:=PF_F32
  276. else
  277. pf:=PF_F64;
  278. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg_reg(A_VMUL,location.register,left.location.register,left.location.register),pf));
  279. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  280. end;
  281. fpu_fpv4_s16:
  282. begin
  283. current_asmdata.CurrAsmList.Concat(setoppostfix(taicpu.op_reg_reg_reg(A_VMUL,location.register,left.location.register,left.location.register), PF_F32));
  284. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  285. end;
  286. else
  287. internalerror(2009111403);
  288. end;
  289. end;
  290. procedure tarminlinenode.second_sqrt_real;
  291. var
  292. singleprec: boolean;
  293. pf: TOpPostfix;
  294. begin
  295. load_fpu_location(singleprec);
  296. case current_settings.fputype of
  297. fpu_fpa,
  298. fpu_fpa10,
  299. fpu_fpa11:
  300. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_SQT,location.register,left.location.register),get_fpu_postfix(resultdef)));
  301. fpu_vfpv2,
  302. fpu_vfpv3,
  303. fpu_vfpv4,
  304. fpu_vfpv3_d16:
  305. begin
  306. if singleprec then
  307. pf:=PF_F32
  308. else
  309. pf:=PF_F64;
  310. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_VSQRT,location.register,left.location.register),pf));
  311. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  312. end;
  313. fpu_fpv4_s16:
  314. begin
  315. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_VSQRT,location.register,left.location.register), PF_F32));
  316. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  317. end;
  318. else
  319. internalerror(2009111402);
  320. end;
  321. end;
  322. { atn, sin, cos, lgn isn't supported by the linux fpe
  323. procedure tarminlinenode.second_arctan_real;
  324. begin
  325. load_fpu_location;
  326. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_ATN,location.register,left.location.register),get_fpu_postfix(resultdef)));
  327. end;
  328. procedure tarminlinenode.second_ln_real;
  329. begin
  330. load_fpu_location;
  331. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_LGN,location.register,left.location.register),get_fpu_postfix(resultdef)));
  332. end;
  333. procedure tarminlinenode.second_cos_real;
  334. begin
  335. load_fpu_location;
  336. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_COS,location.register,left.location.register),get_fpu_postfix(resultdef)));
  337. end;
  338. procedure tarminlinenode.second_sin_real;
  339. begin
  340. load_fpu_location;
  341. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_SIN,location.register,left.location.register),get_fpu_postfix(resultdef)));
  342. end;
  343. }
  344. procedure tarminlinenode.second_prefetch;
  345. var
  346. ref : treference;
  347. r : tregister;
  348. checkpointer_used : boolean;
  349. begin
  350. if not(GenerateThumbCode) and (CPUARM_HAS_EDSP in cpu_capabilities[current_settings.cputype]) then
  351. begin
  352. { do not call Checkpointer for left node }
  353. checkpointer_used:=(cs_checkpointer in current_settings.localswitches);
  354. if checkpointer_used then
  355. node_change_local_switch(left,cs_checkpointer,false);
  356. secondpass(left);
  357. if checkpointer_used then
  358. node_change_local_switch(left,cs_checkpointer,false);
  359. case left.location.loc of
  360. LOC_CREFERENCE,
  361. LOC_REFERENCE:
  362. begin
  363. r:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  364. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.location.reference,r);
  365. reference_reset_base(ref,r,0,location.reference.temppos,left.location.reference.alignment,location.reference.volatility);
  366. { since the address might be nil we can't use ldr for older cpus }
  367. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_PLD,ref));
  368. end;
  369. else
  370. { nothing to prefetch };
  371. end;
  372. end;
  373. end;
  374. procedure tarminlinenode.second_abs_long;
  375. var
  376. opsize : tcgsize;
  377. begin
  378. if GenerateThumbCode then
  379. begin
  380. inherited second_abs_long;
  381. exit;
  382. end;
  383. secondpass(left);
  384. opsize:=def_cgsize(left.resultdef);
  385. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  386. location:=left.location;
  387. location.register:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  388. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  389. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_MOV,location.register,left.location.register), PF_S));
  390. if GenerateThumb2Code then
  391. current_asmdata.CurrAsmList.concat(taicpu.op_cond(A_IT,C_MI));
  392. current_asmdata.CurrAsmList.concat(setcondition(taicpu.op_reg_reg_const(A_RSB,location.register,location.register, 0), C_MI));
  393. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  394. end;
  395. procedure tarminlinenode.second_fma;
  396. const
  397. op : array[false..true,false..true] of TAsmOp =
  398. { positive product }
  399. (
  400. { positive third operand }
  401. (A_VFMA,
  402. { negative third operand }
  403. A_VFNMS),
  404. { negative product }
  405. { positive third operand }
  406. (A_VFMS,
  407. A_VFNMA)
  408. );
  409. var
  410. paraarray : array[1..3] of tnode;
  411. i : integer;
  412. negop3,
  413. negproduct : boolean;
  414. oppostfix : TOpPostfix;
  415. begin
  416. if current_settings.fputype in [fpu_vfpv4] then
  417. begin
  418. negop3:=false;
  419. negproduct:=false;
  420. paraarray[1]:=tcallparanode(tcallparanode(tcallparanode(parameters).nextpara).nextpara).paravalue;
  421. paraarray[2]:=tcallparanode(tcallparanode(parameters).nextpara).paravalue;
  422. paraarray[3]:=tcallparanode(parameters).paravalue;
  423. { check if a neg. node can be removed
  424. this is possible because changing the sign of
  425. a floating point number does not affect its absolute
  426. value in any way
  427. }
  428. if paraarray[1].nodetype=unaryminusn then
  429. begin
  430. paraarray[1]:=tunarynode(paraarray[1]).left;
  431. { do not release the unused unary minus node, it is kept and release together with the other nodes,
  432. only no code is generated for it }
  433. negproduct:=not(negproduct);
  434. end;
  435. if paraarray[2].nodetype=unaryminusn then
  436. begin
  437. paraarray[2]:=tunarynode(paraarray[2]).left;
  438. { do not release the unused unary minus node, it is kept and release together with the other nodes,
  439. only no code is generated for it }
  440. negproduct:=not(negproduct);
  441. end;
  442. if paraarray[3].nodetype=unaryminusn then
  443. begin
  444. paraarray[3]:=tunarynode(paraarray[3]).left;
  445. { do not release the unused unary minus node, it is kept and release together with the other nodes,
  446. only no code is generated for it }
  447. negop3:=true;
  448. end;
  449. for i:=1 to 3 do
  450. secondpass(paraarray[i]);
  451. { no memory operand is allowed }
  452. for i:=1 to 3 do
  453. begin
  454. if not(paraarray[i].location.loc in [LOC_MMREGISTER,LOC_CMMREGISTER]) then
  455. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,paraarray[i].location,paraarray[i].resultdef,true);
  456. end;
  457. location_reset(location,LOC_MMREGISTER,paraarray[1].location.size);
  458. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  459. hlcg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,paraarray[3].resultdef,resultdef,
  460. paraarray[3].location.register,location.register,mms_movescalar);
  461. if is_double(resultdef) then
  462. oppostfix:=PF_F64
  463. else
  464. oppostfix:=PF_F32;
  465. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg_reg(op[negproduct,negop3],
  466. location.register,paraarray[1].location.register,paraarray[2].location.register),oppostfix));
  467. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  468. end
  469. else
  470. internalerror(2014032301);
  471. end;
  472. begin
  473. cinlinenode:=tarminlinenode;
  474. end.