ncpuadd.pas 13 KB

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