nrvadd.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. {
  2. Copyright (c) 2000-2006 by Florian Klaempfl and Jonas Maebe
  3. Code generation for add nodes on the Risc-V (32 and 64 bit generic)
  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 nrvadd;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,nadd,ncgadd,cpubase;
  22. type
  23. trvaddnode = class(tcgaddnode)
  24. function pass_1: tnode; override;
  25. protected
  26. procedure Cmp(signed,is_smallset: boolean);
  27. function use_mul_helper: boolean; override;
  28. procedure second_cmpsmallset;override;
  29. procedure second_cmpordinal;override;
  30. procedure second_cmp64bit; override;
  31. procedure second_addordinal; override;
  32. procedure pass_left_and_right;
  33. function use_fma: boolean; override;
  34. procedure second_addfloat;override;
  35. procedure second_cmpfloat;override;
  36. end;
  37. implementation
  38. uses
  39. globtype,systems,
  40. cutils,verbose,globals,
  41. symconst,symdef,paramgr,
  42. aasmbase,aasmtai,aasmdata,aasmcpu,defutil,htypechk,
  43. cgbase,cpuinfo,pass_1,pass_2,
  44. cpupara,cgcpu,cgutils,procinfo,
  45. ncon,nset,
  46. ncgutil,tgobj,rgobj,rgcpu,cgobj,hlcgobj;
  47. {$undef AVOID_OVERFLOW}
  48. {$ifopt Q+}
  49. {$define AVOID_OVERFLOW}
  50. const
  51. low_value = {$ifdef CPU64BITALU} low(int64) {$else} low(longint) {$endif};
  52. {$endif}
  53. procedure trvaddnode.Cmp(signed,is_smallset: boolean);
  54. var
  55. flabel,tlabel: tasmlabel;
  56. op, opi: TAsmOp;
  57. allow_constant : boolean;
  58. begin
  59. pass_left_right;
  60. allow_constant:=(not is_smallset) or not (nodetype in [lten,gten]);
  61. force_reg_left_right(true,allow_constant);
  62. if nf_swapped in flags then
  63. swapleftright;
  64. location_reset(location,LOC_REGISTER,OS_INT);
  65. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  66. if signed then op:=A_SLT else op:=A_SLTU;
  67. if signed then opi:=A_SLTI else opi:=A_SLTIU;
  68. case nodetype of
  69. equaln:
  70. begin
  71. if not (left.location.loc in [LOC_CREGISTER,LOC_REGISTER]) then
  72. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  73. if (right.location.loc=LOC_CONSTANT) and
  74. { right.location.value might be $8000000000000000,
  75. and its minus value generates an overflow here }
  76. {$ifdef AVOID_OVERFLOW} ((right.location.value = low_value) or {$endif}
  77. (not is_imm12(-right.location.value)) {$ifdef AVOID_OVERFLOW}){$endif} then
  78. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,false);
  79. if right.location.loc=LOC_CONSTANT then
  80. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_const(A_ADDI,location.register,left.location.register,-right.location.value))
  81. else
  82. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(A_SUB,location.register,left.location.register,right.location.register));
  83. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_const(A_SLTIU,location.register,location.register,1));
  84. end;
  85. unequaln:
  86. begin
  87. if not (left.location.loc in [LOC_CREGISTER,LOC_REGISTER]) then
  88. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  89. if (right.location.loc=LOC_CONSTANT) and
  90. { right.location.value might be $8000000000000000,
  91. and its minus value generates an overflow here }
  92. {$ifdef AVOID_OVERFLOW} ((right.location.value = low_value) or {$endif}
  93. (not is_imm12(-right.location.value)) {$ifdef AVOID_OVERFLOW}){$endif} then
  94. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,false);
  95. if right.location.loc=LOC_CONSTANT then
  96. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_const(A_ADDI,location.register,left.location.register,-right.location.value))
  97. else
  98. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(A_SUB,location.register,left.location.register,right.location.register));
  99. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(A_SLTU,location.register,NR_X0,location.register));
  100. end;
  101. ltn:
  102. begin
  103. if not (left.location.loc in [LOC_CREGISTER,LOC_REGISTER]) then
  104. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  105. if (right.location.loc=LOC_CONSTANT) and
  106. (not is_imm12(right.location.value)) then
  107. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,false);
  108. if right.location.loc=LOC_CONSTANT then
  109. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_const(opi,location.register,left.location.register,right.location.value))
  110. else
  111. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(op,location.register,left.location.register,right.location.register));
  112. end;
  113. gtn:
  114. begin
  115. if not (right.location.loc in [LOC_CREGISTER,LOC_REGISTER]) then
  116. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,false);
  117. if (left.location.loc=LOC_CONSTANT) and
  118. (not is_imm12(left.location.value)) then
  119. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  120. if left.location.loc=LOC_CONSTANT then
  121. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_const(opi,location.register,right.location.register,left.location.value))
  122. else
  123. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(op,location.register,right.location.register,left.location.register));
  124. end;
  125. lten:
  126. begin
  127. if not (right.location.loc in [LOC_CREGISTER,LOC_REGISTER]) then
  128. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,false);
  129. if (left.location.loc=LOC_CONSTANT) and
  130. (not is_imm12(left.location.value)) then
  131. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  132. if is_smallset then
  133. begin
  134. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(A_AND,right.location.register,right.location.register,left.location.register));
  135. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(A_SUB,location.register,left.location.register,right.location.register));
  136. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_const(A_SLTIU,location.register,location.register,1));
  137. end
  138. else
  139. begin
  140. if left.location.loc=LOC_CONSTANT then
  141. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_const(opi,location.register,right.location.register,left.location.value))
  142. else
  143. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(op,location.register,right.location.register,left.location.register));
  144. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_const(A_SLTIU,location.register,location.register,1));
  145. end;
  146. end;
  147. gten:
  148. begin
  149. if not (left.location.loc in [LOC_CREGISTER,LOC_REGISTER]) then
  150. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  151. if (right.location.loc=LOC_CONSTANT) and
  152. (not is_imm12(right.location.value)) then
  153. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,false);
  154. if is_smallset then
  155. begin
  156. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(A_AND,left.location.register,right.location.register,left.location.register));
  157. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(A_SUB,location.register,left.location.register,right.location.register));
  158. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_const(A_SLTIU,location.register,location.register,1));
  159. end
  160. else
  161. begin
  162. if right.location.loc=LOC_CONSTANT then
  163. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_const(opi,location.register,left.location.register,right.location.value))
  164. else
  165. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(op,location.register,left.location.register,right.location.register));
  166. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_const(A_SLTIU,location.register,location.register,1));
  167. end;
  168. end;
  169. else
  170. Internalerror(2016061101);
  171. end;
  172. end;
  173. function trvaddnode.use_mul_helper: boolean;
  174. begin
  175. if (nodetype=muln) and ([CPURV_HAS_MUL,CPURV_HAS_ZMMUL]*cpu_capabilities[current_settings.cputype]=[]) then
  176. result:=true
  177. else
  178. Result:=inherited use_mul_helper;
  179. end;
  180. procedure trvaddnode.second_cmpsmallset;
  181. begin
  182. Cmp(false,true);
  183. end;
  184. procedure trvaddnode.second_cmpordinal;
  185. var
  186. unsigned: Boolean;
  187. begin
  188. unsigned:=not(is_signed(left.resultdef)) or
  189. not(is_signed(right.resultdef));
  190. Cmp(not unsigned,false);
  191. end;
  192. procedure trvaddnode.second_cmp64bit;
  193. var
  194. unsigned: Boolean;
  195. begin
  196. unsigned:=not(is_signed(left.resultdef)) or
  197. not(is_signed(right.resultdef));
  198. Cmp(not unsigned,false);
  199. end;
  200. procedure trvaddnode.second_addordinal;
  201. var
  202. unsigned: boolean;
  203. begin
  204. { 32x32->64 multiplication }
  205. if (nodetype=muln) and
  206. is_32bit(left.resultdef) and
  207. is_32bit(right.resultdef) and
  208. is_64bit(resultdef) then
  209. begin
  210. unsigned:=not(is_signed(left.resultdef)) or
  211. not(is_signed(right.resultdef));
  212. pass_left_right;
  213. force_reg_left_right(true,true);
  214. { force_reg_left_right can leave right as a LOC_CONSTANT (we can't
  215. say "a constant register is okay, but an ordinal constant isn't) }
  216. if right.location.loc=LOC_CONSTANT then
  217. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  218. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  219. location.register:=cg.getintregister(current_asmdata.CurrAsmList,def_cgsize(resultdef));
  220. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(A_MUL,location.register,left.location.register,right.location.register));
  221. end
  222. else
  223. inherited second_addordinal;
  224. end;
  225. function trvaddnode.pass_1: tnode;
  226. begin
  227. if (nodetype=muln) and
  228. (left.resultdef.typ=orddef) and (left.resultdef.typ=orddef) and
  229. ([CPURV_HAS_MUL,CPURV_HAS_ZMMUL]*cpu_capabilities[current_settings.cputype]<>[])
  230. {$ifdef cpu32bitalu}
  231. and (not (is_64bit(left.resultdef) or
  232. is_64bit(right.resultdef)))
  233. {$endif cpu32bitalu}
  234. then
  235. begin
  236. result:=nil;
  237. firstpass(left);
  238. firstpass(right);
  239. expectloc:=LOC_REGISTER;
  240. end
  241. else if (nodetype=muln) and
  242. ([CPURV_HAS_MUL,CPURV_HAS_ZMMUL]*cpu_capabilities[current_settings.cputype]=[]) and
  243. (is_64bit(left.resultdef) or
  244. is_64bit(right.resultdef)) then
  245. begin
  246. result:=first_add64bitint;
  247. end
  248. else
  249. Result:=inherited pass_1;
  250. { if the result is not nil, a new node has been generated and the current node will be discarted }
  251. if Result=nil then
  252. begin
  253. if left.resultdef.typ=floatdef then
  254. if needs_check_for_fpu_exceptions then
  255. Include(current_procinfo.flags,pi_do_call);
  256. end;
  257. if expectloc=LOC_FLAGS then
  258. expectloc:=LOC_REGISTER;
  259. if (expectloc=LOC_JUMP)
  260. {$ifdef cpu32bitalu}
  261. and (not (is_64bit(left.resultdef) or
  262. is_64bit(right.resultdef)))
  263. {$endif cpu32bitalu}
  264. and (nodetype in [equaln, unequaln, ltn, lten, gtn, gten]) then
  265. expectloc:=LOC_REGISTER;
  266. end;
  267. procedure trvaddnode.pass_left_and_right;
  268. begin
  269. { calculate the operator which is more difficult }
  270. firstcomplex(self);
  271. { in case of constant put it to the left }
  272. if (left.nodetype=ordconstn) then
  273. swapleftright;
  274. secondpass(left);
  275. secondpass(right);
  276. end;
  277. function trvaddnode.use_fma: boolean;
  278. begin
  279. Result:=(is_single(left.resultdef) and is_single(right.resultdef) and
  280. (CPURV_HAS_F in cpu_capabilities[current_settings.cputype])) or
  281. (is_double(left.resultdef) and is_double(right.resultdef) and
  282. (CPURV_HAS_D in cpu_capabilities[current_settings.cputype]));
  283. end;
  284. procedure trvaddnode.second_addfloat;
  285. var
  286. op : TAsmOp;
  287. cmpop,
  288. singleprec , inv: boolean;
  289. begin
  290. pass_left_and_right;
  291. if (nf_swapped in flags) then
  292. swapleftright;
  293. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  294. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  295. cmpop:=false;
  296. singleprec:=tfloatdef(left.resultdef).floattype=s32real;
  297. inv:=false;
  298. case nodetype of
  299. addn :
  300. if singleprec then
  301. op:=A_FADD_S
  302. else
  303. op:=A_FADD_D;
  304. muln :
  305. if singleprec then
  306. op:=A_FMUL_S
  307. else
  308. op:=A_FMUL_D;
  309. subn :
  310. if singleprec then
  311. op:=A_FSUB_S
  312. else
  313. op:=A_FSUB_D;
  314. slashn :
  315. if singleprec then
  316. op:=A_FDIV_S
  317. else
  318. op:=A_FDIV_D;
  319. equaln:
  320. begin
  321. if singleprec then
  322. op:=A_FEQ_S
  323. else
  324. op:=A_FEQ_D;
  325. cmpop:=true;
  326. end;
  327. unequaln:
  328. begin
  329. if singleprec then
  330. op:=A_FEQ_S
  331. else
  332. op:=A_FEQ_D;
  333. inv:=true;
  334. cmpop:=true;
  335. end;
  336. ltn:
  337. begin
  338. if singleprec then
  339. op:=A_FLT_S
  340. else
  341. op:=A_FLT_D;
  342. cmpop:=true;
  343. end;
  344. lten:
  345. begin
  346. if singleprec then
  347. op:=A_FLE_S
  348. else
  349. op:=A_FLE_D;
  350. cmpop:=true;
  351. end;
  352. gtn:
  353. begin
  354. if singleprec then
  355. op:=A_FLT_S
  356. else
  357. op:=A_FLT_D;
  358. swapleftright;
  359. cmpop:=true;
  360. end;
  361. gten:
  362. begin
  363. if singleprec then
  364. op:=A_FLE_S
  365. else
  366. op:=A_FLE_D;
  367. swapleftright;
  368. cmpop:=true;
  369. end;
  370. else
  371. internalerror(200403182);
  372. end;
  373. // put both operands in a register
  374. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  375. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  376. // initialize de result
  377. if not cmpop then
  378. begin
  379. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  380. location.register := cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  381. end
  382. else
  383. begin
  384. location_reset(location,LOC_REGISTER,OS_8);
  385. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  386. end;
  387. // emit the actual operation
  388. if not cmpop then
  389. begin
  390. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,location.register,left.location.register,right.location.register));
  391. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  392. end
  393. else
  394. begin
  395. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,location.register,left.location.register,right.location.register));
  396. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  397. if inv then
  398. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_const(A_XORI,location.register,location.register,1));
  399. end;
  400. end;
  401. procedure trvaddnode.second_cmpfloat;
  402. begin
  403. second_addfloat;
  404. end;
  405. end.