ncpuadd.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. {
  2. Copyright (c) 2014 Jonas Maebe
  3. Code generation for add nodes on AArch64
  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 ncpuadd;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,ncgadd,cpubase;
  22. type
  23. taarch64addnode = class(tcgaddnode)
  24. private
  25. function GetResFlags(unsigned:Boolean):TResFlags;
  26. function GetFPUResFlags:TResFlags;
  27. protected
  28. function use_fma : boolean;override;
  29. procedure second_addfloat;override;
  30. procedure second_cmpfloat;override;
  31. procedure second_cmpboolean;override;
  32. procedure second_cmpsmallset;override;
  33. procedure second_cmpordinal;override;
  34. procedure second_addordinal;override;
  35. procedure second_add64bit; override;
  36. procedure second_cmp64bit; override;
  37. public
  38. function use_generic_mul32to64: boolean; override;
  39. end;
  40. implementation
  41. uses
  42. systems,
  43. cutils,verbose,
  44. paramgr,procinfo,
  45. aasmtai,aasmdata,aasmcpu,defutil,
  46. cgbase,cgcpu,cgutils,
  47. cpupara,
  48. ncon,nset,nadd,
  49. hlcgobj, ncgutil,cgobj;
  50. {*****************************************************************************
  51. taarch64addnode
  52. *****************************************************************************}
  53. function taarch64addnode.use_fma : boolean;
  54. begin
  55. Result:=true;
  56. end;
  57. function taarch64addnode.GetResFlags(unsigned:Boolean):TResFlags;
  58. begin
  59. case NodeType of
  60. equaln:
  61. GetResFlags:=F_EQ;
  62. unequaln:
  63. GetResFlags:=F_NE;
  64. else
  65. if not(unsigned) then
  66. begin
  67. if nf_swapped in flags then
  68. case NodeType of
  69. ltn:
  70. GetResFlags:=F_GT;
  71. lten:
  72. GetResFlags:=F_GE;
  73. gtn:
  74. GetResFlags:=F_LT;
  75. gten:
  76. GetResFlags:=F_LE;
  77. else
  78. internalerror(2014082010);
  79. end
  80. else
  81. case NodeType of
  82. ltn:
  83. GetResFlags:=F_LT;
  84. lten:
  85. GetResFlags:=F_LE;
  86. gtn:
  87. GetResFlags:=F_GT;
  88. gten:
  89. GetResFlags:=F_GE;
  90. else
  91. internalerror(2014082011);
  92. end;
  93. end
  94. else
  95. begin
  96. if nf_swapped in Flags then
  97. case NodeType of
  98. ltn:
  99. GetResFlags:=F_HI;
  100. lten:
  101. GetResFlags:=F_HS;
  102. gtn:
  103. GetResFlags:=F_LO;
  104. gten:
  105. GetResFlags:=F_LS;
  106. else
  107. internalerror(2014082012);
  108. end
  109. else
  110. case NodeType of
  111. ltn:
  112. GetResFlags:=F_LO;
  113. lten:
  114. GetResFlags:=F_LS;
  115. gtn:
  116. GetResFlags:=F_HI;
  117. gten:
  118. GetResFlags:=F_HS;
  119. else
  120. internalerror(2014082013);
  121. end;
  122. end;
  123. end;
  124. end;
  125. function taarch64addnode.GetFPUResFlags:TResFlags;
  126. begin
  127. case NodeType of
  128. equaln:
  129. result:=F_EQ;
  130. unequaln:
  131. result:=F_NE;
  132. else
  133. begin
  134. if nf_swapped in Flags then
  135. case NodeType of
  136. ltn:
  137. result:=F_GT;
  138. lten:
  139. result:=F_GE;
  140. gtn:
  141. result:=F_LO;
  142. gten:
  143. result:=F_LS;
  144. else
  145. internalerror(2014082014);
  146. end
  147. else
  148. case NodeType of
  149. ltn:
  150. result:=F_LO;
  151. lten:
  152. result:=F_LS;
  153. gtn:
  154. result:=F_GT;
  155. gten:
  156. result:=F_GE;
  157. else
  158. internalerror(2014082015);
  159. end;
  160. end;
  161. end;
  162. end;
  163. procedure taarch64addnode.second_addfloat;
  164. var
  165. op : TAsmOp;
  166. begin
  167. pass_left_right;
  168. if nf_swapped in flags then
  169. swapleftright;
  170. { force fpureg as location, left right doesn't matter
  171. as both will be in a fpureg }
  172. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  173. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  174. location_reset(location,LOC_MMREGISTER,def_cgsize(resultdef));
  175. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  176. case nodetype of
  177. addn :
  178. begin
  179. op:=A_FADD;
  180. end;
  181. muln :
  182. begin
  183. op:=A_FMUL;
  184. end;
  185. subn :
  186. begin
  187. op:=A_FSUB;
  188. end;
  189. slashn :
  190. begin
  191. op:=A_FDIV;
  192. end;
  193. else
  194. internalerror(200306014);
  195. end;
  196. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,
  197. location.register,left.location.register,right.location.register));
  198. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  199. end;
  200. procedure taarch64addnode.second_cmpfloat;
  201. begin
  202. pass_left_right;
  203. if nf_swapped in flags then
  204. swapleftright;
  205. { force fpureg as location, left right doesn't matter
  206. as both will be in a fpureg }
  207. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  208. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  209. location_reset(location,LOC_FLAGS,OS_NO);
  210. location.resflags:=getfpuresflags;
  211. { signalling compare so we can get exceptions }
  212. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FCMPE,
  213. left.location.register,right.location.register));
  214. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  215. end;
  216. procedure taarch64addnode.second_cmpboolean;
  217. begin
  218. pass_left_right;
  219. force_reg_left_right(true,true);
  220. if right.location.loc=LOC_CONSTANT then
  221. begin
  222. if right.location.value>=0 then
  223. Tcgaarch64(cg).handle_reg_imm12_reg(current_asmdata.CurrAsmList,A_CMP,left.location.size,left.location.register,right.location.value,NR_XZR,NR_NO,false,false)
  224. else
  225. { avoid overflow if value=low(int64) }
  226. {$push}{$r-}{$q-}
  227. Tcgaarch64(cg).handle_reg_imm12_reg(current_asmdata.CurrAsmList,A_CMN,left.location.size,left.location.register,-right.location.value,NR_XZR,NR_NO,false,false)
  228. {$pop}
  229. end
  230. else
  231. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register,right.location.register));
  232. location_reset(location,LOC_FLAGS,OS_NO);
  233. location.resflags:=getresflags(true);
  234. end;
  235. procedure taarch64addnode.second_cmpsmallset;
  236. var
  237. tmpreg : tregister;
  238. op: tasmop;
  239. begin
  240. pass_left_right;
  241. location_reset(location,LOC_FLAGS,OS_NO);
  242. force_reg_left_right(true,true);
  243. if right.location.loc=LOC_CONSTANT then
  244. begin
  245. { when doing a cmp/cmn on 32 bit, we care whether the *lower 32 bit*
  246. is a positive/negative value -> sign extend }
  247. if not(right.location.size in [OS_64,OS_S64]) then
  248. right.location.value:=longint(right.location.value);
  249. if right.location.value>=0 then
  250. op:=A_CMP
  251. else
  252. op:=A_CMN;
  253. end
  254. else
  255. { for DFA }
  256. op:=A_NONE;
  257. case nodetype of
  258. equaln,
  259. unequaln:
  260. begin
  261. if right.location.loc=LOC_CONSTANT then
  262. tcgaarch64(cg).handle_reg_imm12_reg(current_asmdata.CurrAsmList,op,def_cgsize(resultdef),left.location.register,abs(right.location.value),NR_XZR,NR_NO,false,false)
  263. else
  264. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register,right.location.register));
  265. location.resflags:=getresflags(true);
  266. end;
  267. lten,
  268. gten:
  269. begin
  270. if (not(nf_swapped in flags) and
  271. (nodetype=lten)) or
  272. ((nf_swapped in flags) and
  273. (nodetype=gten)) then
  274. swapleftright;
  275. { we can't handle left as a constant yet }
  276. if left.location.loc=LOC_CONSTANT then
  277. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  278. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,left.location.size);
  279. if right.location.loc=LOC_CONSTANT then
  280. begin
  281. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_AND,resultdef,right.location.value,left.location.register,tmpreg);
  282. tcgaarch64(cg).handle_reg_imm12_reg(current_asmdata.CurrAsmList,op,def_cgsize(resultdef),tmpreg,abs(right.location.value),NR_XZR,NR_NO,false,false)
  283. end
  284. else
  285. begin
  286. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_AND,tmpreg,left.location.register,right.location.register));
  287. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,tmpreg,right.location.register));
  288. end;
  289. location.resflags:=F_EQ;
  290. end;
  291. else
  292. internalerror(2012042701);
  293. end;
  294. end;
  295. procedure taarch64addnode.second_cmpordinal;
  296. var
  297. unsigned : boolean;
  298. begin
  299. pass_left_right;
  300. force_reg_left_right(true,true);
  301. unsigned:=not(is_signed(left.resultdef)) or
  302. not(is_signed(right.resultdef));
  303. if right.location.loc = LOC_CONSTANT then
  304. begin
  305. if right.location.value>=0 then
  306. Tcgaarch64(cg).handle_reg_imm12_reg(current_asmdata.CurrAsmList,A_CMP,left.location.size,left.location.register,right.location.value,NR_XZR,NR_NO,false,false)
  307. else
  308. {$push}{$r-}{$q-}
  309. Tcgaarch64(cg).handle_reg_imm12_reg(current_asmdata.CurrAsmList,A_CMN,left.location.size,left.location.register,-right.location.value,NR_XZR,NR_NO,false,false)
  310. {$pop}
  311. end
  312. else
  313. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register,right.location.register));
  314. location_reset(location,LOC_FLAGS,OS_NO);
  315. location.resflags:=getresflags(unsigned);
  316. end;
  317. procedure taarch64addnode.second_addordinal;
  318. const
  319. multops: array[boolean] of TAsmOp = (A_SMULL,A_UMULL);
  320. var
  321. unsigned: boolean;
  322. begin
  323. { 32x32->64 multiplication }
  324. if (nodetype=muln) and
  325. is_32bit(left.resultdef) and
  326. is_32bit(right.resultdef) and
  327. is_64bit(resultdef) then
  328. begin
  329. unsigned:=not(is_signed(left.resultdef)) or
  330. not(is_signed(right.resultdef));
  331. pass_left_right;
  332. force_reg_left_right(true,true);
  333. { force_reg_left_right can leave right as a LOC_CONSTANT (we can't
  334. say "a constant register is okay, but an ordinal constant isn't) }
  335. if right.location.loc=LOC_CONSTANT then
  336. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  337. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  338. location.register:=cg.getintregister(current_asmdata.CurrAsmList,def_cgsize(resultdef));
  339. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(multops[unsigned],location.register,left.location.register,right.location.register));
  340. end
  341. else
  342. inherited second_addordinal;
  343. end;
  344. procedure taarch64addnode.second_add64bit;
  345. begin
  346. second_addordinal;
  347. end;
  348. procedure taarch64addnode.second_cmp64bit;
  349. begin
  350. second_cmpordinal;
  351. end;
  352. function taarch64addnode.use_generic_mul32to64: boolean;
  353. begin
  354. result:=false;
  355. end;
  356. begin
  357. caddnode:=taarch64addnode;
  358. end.