narminl.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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,ncal,ncon;
  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_arm: 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_arm; 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. {*****************************************************************************
  56. tarminlinenode
  57. *****************************************************************************}
  58. procedure tarminlinenode.load_fpu_location(out singleprec: boolean);
  59. begin
  60. secondpass(left);
  61. case current_settings.fputype of
  62. fpu_fpa,
  63. fpu_fpa10,
  64. fpu_fpa11:
  65. begin
  66. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  67. location_copy(location,left.location);
  68. if left.location.loc=LOC_CFPUREGISTER then
  69. begin
  70. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  71. location.loc := LOC_FPUREGISTER;
  72. end;
  73. end;
  74. fpu_vfpv2,
  75. fpu_vfpv3,
  76. fpu_vfpv3_d16,
  77. fpu_fpv4_s16:
  78. begin
  79. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  80. location_copy(location,left.location);
  81. if left.location.loc=LOC_CMMREGISTER then
  82. begin
  83. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  84. location.loc := LOC_MMREGISTER;
  85. end;
  86. end;
  87. fpu_soft:
  88. begin
  89. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  90. location_copy(location,left.location);
  91. end
  92. else
  93. internalerror(2009111801);
  94. end;
  95. singleprec:=tfloatdef(left.resultdef).floattype=s32real;
  96. end;
  97. function tarminlinenode.first_abs_real : tnode;
  98. begin
  99. if (cs_fp_emulation in current_settings.moduleswitches) then
  100. begin
  101. firstpass(left);
  102. expectloc:=LOC_REGISTER;
  103. first_abs_real:=nil;
  104. end
  105. else
  106. begin
  107. case current_settings.fputype of
  108. fpu_fpa,
  109. fpu_fpa10,
  110. fpu_fpa11:
  111. expectloc:=LOC_FPUREGISTER;
  112. fpu_vfpv2,
  113. fpu_vfpv3,
  114. fpu_vfpv3_d16:
  115. expectloc:=LOC_MMREGISTER;
  116. fpu_fpv4_s16:
  117. begin
  118. if tfloatdef(left.resultdef).floattype=s32real then
  119. expectloc:=LOC_MMREGISTER
  120. else
  121. exit(inherited first_abs_real);
  122. end;
  123. else
  124. internalerror(2009112401);
  125. end;
  126. first_abs_real:=nil;
  127. end;
  128. end;
  129. function tarminlinenode.first_sqr_real : tnode;
  130. begin
  131. if (cs_fp_emulation in current_settings.moduleswitches) then
  132. result:=inherited first_sqr_real
  133. else
  134. begin
  135. case current_settings.fputype of
  136. fpu_fpa,
  137. fpu_fpa10,
  138. fpu_fpa11:
  139. expectloc:=LOC_FPUREGISTER;
  140. fpu_vfpv2,
  141. fpu_vfpv3,
  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_vfpv3_d16:
  171. expectloc:=LOC_MMREGISTER;
  172. fpu_fpv4_s16:
  173. begin
  174. if tfloatdef(left.resultdef).floattype=s32real then
  175. expectloc:=LOC_MMREGISTER
  176. else
  177. exit(inherited first_sqrt_real);
  178. end;
  179. else
  180. internalerror(2009112403);
  181. end;
  182. first_sqrt_real := nil;
  183. end;
  184. end;
  185. function tarminlinenode.first_arm : tnode;
  186. begin
  187. case inlinenumber of
  188. {$i armfirst.inc}
  189. end;
  190. end;
  191. { atn,sin,cos,lgn isn't supported by the linux fpe
  192. function tarminlinenode.first_arctan_real: tnode;
  193. begin
  194. expectloc:=LOC_FPUREGISTER;
  195. result:=nil;
  196. end;
  197. function tarminlinenode.first_ln_real: tnode;
  198. begin
  199. expectloc:=LOC_FPUREGISTER;
  200. result:=nil;
  201. end;
  202. function tarminlinenode.first_cos_real: tnode;
  203. begin
  204. expectloc:=LOC_FPUREGISTER;
  205. result:=nil;
  206. end;
  207. function tarminlinenode.first_sin_real: tnode;
  208. begin
  209. expectloc:=LOC_FPUREGISTER;
  210. result:=nil;
  211. end;
  212. }
  213. procedure tarminlinenode.second_abs_real;
  214. var
  215. singleprec: boolean;
  216. pf: TOpPostfix;
  217. begin
  218. load_fpu_location(singleprec);
  219. case current_settings.fputype of
  220. fpu_fpa,
  221. fpu_fpa10,
  222. fpu_fpa11:
  223. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_ABS,location.register,left.location.register),get_fpu_postfix(resultdef)));
  224. fpu_vfpv2,
  225. fpu_vfpv3,
  226. fpu_vfpv3_d16:
  227. begin
  228. if singleprec then
  229. pf:=PF_F32
  230. else
  231. pf:=PF_F64;
  232. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_VABS,location.register,left.location.register),pf));
  233. end;
  234. fpu_fpv4_s16:
  235. current_asmdata.CurrAsmList.Concat(setoppostfix(taicpu.op_reg_reg(A_VABS,location.register,left.location.register), PF_F32));
  236. fpu_soft:
  237. begin
  238. if singleprec then
  239. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_AND,OS_32,tcgint($7fffffff),location.register)
  240. else
  241. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_AND,OS_32,tcgint($7fffffff),location.registerhi);
  242. end
  243. else
  244. internalerror(2009111402);
  245. end;
  246. end;
  247. procedure tarminlinenode.second_sqr_real;
  248. var
  249. singleprec: boolean;
  250. pf: TOpPostfix;
  251. begin
  252. load_fpu_location(singleprec);
  253. case current_settings.fputype of
  254. fpu_fpa,
  255. fpu_fpa10,
  256. fpu_fpa11:
  257. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg_reg(A_MUF,location.register,left.location.register,left.location.register),get_fpu_postfix(resultdef)));
  258. fpu_vfpv2,
  259. fpu_vfpv3,
  260. fpu_vfpv3_d16:
  261. begin
  262. if singleprec then
  263. pf:=PF_F32
  264. else
  265. pf:=PF_F64;
  266. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg_reg(A_VMUL,location.register,left.location.register,left.location.register),pf));
  267. end;
  268. fpu_fpv4_s16:
  269. current_asmdata.CurrAsmList.Concat(setoppostfix(taicpu.op_reg_reg_reg(A_VMUL,location.register,left.location.register,left.location.register), PF_F32));
  270. else
  271. internalerror(2009111403);
  272. end;
  273. end;
  274. procedure tarminlinenode.second_sqrt_real;
  275. var
  276. singleprec: boolean;
  277. pf: TOpPostfix;
  278. begin
  279. load_fpu_location(singleprec);
  280. case current_settings.fputype of
  281. fpu_fpa,
  282. fpu_fpa10,
  283. fpu_fpa11:
  284. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_SQT,location.register,left.location.register),get_fpu_postfix(resultdef)));
  285. fpu_vfpv2,
  286. fpu_vfpv3,
  287. fpu_vfpv3_d16:
  288. begin
  289. if singleprec then
  290. pf:=PF_F32
  291. else
  292. pf:=PF_F64;
  293. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_VSQRT,location.register,left.location.register),pf));
  294. end;
  295. fpu_fpv4_s16:
  296. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_VSQRT,location.register,left.location.register), PF_F32));
  297. else
  298. internalerror(2009111402);
  299. end;
  300. end;
  301. { atn, sin, cos, lgn isn't supported by the linux fpe
  302. procedure tarminlinenode.second_arctan_real;
  303. begin
  304. load_fpu_location;
  305. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_ATN,location.register,left.location.register),get_fpu_postfix(resultdef)));
  306. end;
  307. procedure tarminlinenode.second_ln_real;
  308. begin
  309. load_fpu_location;
  310. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_LGN,location.register,left.location.register),get_fpu_postfix(resultdef)));
  311. end;
  312. procedure tarminlinenode.second_cos_real;
  313. begin
  314. load_fpu_location;
  315. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_COS,location.register,left.location.register),get_fpu_postfix(resultdef)));
  316. end;
  317. procedure tarminlinenode.second_sin_real;
  318. begin
  319. load_fpu_location;
  320. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_SIN,location.register,left.location.register),get_fpu_postfix(resultdef)));
  321. end;
  322. }
  323. procedure tarminlinenode.second_prefetch;
  324. var
  325. ref : treference;
  326. r : tregister;
  327. begin
  328. if not(GenerateThumbCode) and (CPUARM_HAS_EDSP in cpu_capabilities[current_settings.cputype]) then
  329. begin
  330. secondpass(left);
  331. case left.location.loc of
  332. LOC_CREFERENCE,
  333. LOC_REFERENCE:
  334. begin
  335. r:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  336. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.location.reference,r);
  337. reference_reset_base(ref,r,0,left.location.reference.alignment);
  338. { since the address might be nil we can't use ldr for older cpus }
  339. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_PLD,ref));
  340. end;
  341. else
  342. { nothing to prefetch };
  343. end;
  344. end;
  345. end;
  346. procedure tarminlinenode.second_abs_long;
  347. var
  348. opsize : tcgsize;
  349. hp : taicpu;
  350. begin
  351. if GenerateThumbCode then
  352. begin
  353. inherited second_abs_long;
  354. exit;
  355. end;
  356. secondpass(left);
  357. opsize:=def_cgsize(left.resultdef);
  358. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  359. location:=left.location;
  360. location.register:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  361. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  362. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_MOV,location.register,left.location.register), PF_S));
  363. if GenerateThumb2Code then
  364. current_asmdata.CurrAsmList.concat(taicpu.op_cond(A_IT,C_MI));
  365. current_asmdata.CurrAsmList.concat(setcondition(taicpu.op_reg_reg_const(A_RSB,location.register,location.register, 0), C_MI));
  366. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  367. end;
  368. procedure tarminlinenode.second_arm;
  369. var
  370. paraarray : array[1..4] of tnode;
  371. i : integer;
  372. op: TAsmOp;
  373. pf: TOpPostfix;
  374. function GetConstInt(n: tnode): longint;
  375. begin
  376. if is_constintnode(n) then
  377. result:=tordconstnode(n).value.svalue
  378. else
  379. Message(type_e_constant_expr_expected);
  380. end;
  381. function GetShifterOp(AShiftMode: tshiftmode; AAmount: tnode): tshifterop;
  382. begin
  383. result.shiftmode:=AShiftMode;
  384. result.rs:=NR_NO;
  385. Result.shiftimm:=GetConstInt(AAmount);
  386. end;
  387. procedure GetParameters(count: longint);
  388. var
  389. i: longint;
  390. p: tnode;
  391. begin
  392. if count=1 then
  393. paraarray[1]:=left
  394. else
  395. begin
  396. p:=left;
  397. for i := count downto 1 do
  398. begin
  399. paraarray[i]:=tcallparanode(p).paravalue;
  400. p:=tcallparanode(p).nextpara;
  401. end;
  402. end;
  403. end;
  404. procedure location_make_ref(var loc: tlocation);
  405. var
  406. hloc: tlocation;
  407. begin
  408. case loc.loc of
  409. LOC_CREGISTER,
  410. LOC_REGISTER:
  411. begin
  412. location_reset_ref(hloc, LOC_REFERENCE, OS_32, 1);
  413. hloc.reference.base:=loc.register;
  414. loc:=hloc;
  415. end;
  416. LOC_CREFERENCE,
  417. LOC_REFERENCE:
  418. begin
  419. end;
  420. else
  421. begin
  422. hlcg.location_force_reg(current_asmdata.CurrAsmList,loc,u32inttype,u32inttype,false);
  423. location_reset_ref(hloc, LOC_REFERENCE, OS_32, 1);
  424. hloc.reference.base:=loc.register;
  425. loc:=hloc;
  426. end;
  427. end;
  428. end;
  429. begin
  430. case inlinenumber of
  431. {$i armsecond.inc}
  432. end;
  433. end;
  434. begin
  435. cinlinenode:=tarminlinenode;
  436. end.