nrvadd.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 not(CPURV_HAS_MUL in 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 in 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. (not (CPURV_HAS_MUL in 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:=current_settings.fputype in [fpu_fd];
  280. end;
  281. procedure trvaddnode.second_addfloat;
  282. var
  283. op : TAsmOp;
  284. cmpop,
  285. singleprec , inv: boolean;
  286. begin
  287. pass_left_and_right;
  288. if (nf_swapped in flags) then
  289. swapleftright;
  290. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  291. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  292. cmpop:=false;
  293. singleprec:=tfloatdef(left.resultdef).floattype=s32real;
  294. inv:=false;
  295. case nodetype of
  296. addn :
  297. if singleprec then
  298. op:=A_FADD_S
  299. else
  300. op:=A_FADD_D;
  301. muln :
  302. if singleprec then
  303. op:=A_FMUL_S
  304. else
  305. op:=A_FMUL_D;
  306. subn :
  307. if singleprec then
  308. op:=A_FSUB_S
  309. else
  310. op:=A_FSUB_D;
  311. slashn :
  312. if singleprec then
  313. op:=A_FDIV_S
  314. else
  315. op:=A_FDIV_D;
  316. equaln:
  317. begin
  318. if singleprec then
  319. op:=A_FEQ_S
  320. else
  321. op:=A_FEQ_D;
  322. cmpop:=true;
  323. end;
  324. unequaln:
  325. begin
  326. if singleprec then
  327. op:=A_FEQ_S
  328. else
  329. op:=A_FEQ_D;
  330. inv:=true;
  331. cmpop:=true;
  332. end;
  333. ltn:
  334. begin
  335. if singleprec then
  336. op:=A_FLT_S
  337. else
  338. op:=A_FLT_D;
  339. cmpop:=true;
  340. end;
  341. lten:
  342. begin
  343. if singleprec then
  344. op:=A_FLE_S
  345. else
  346. op:=A_FLE_D;
  347. cmpop:=true;
  348. end;
  349. gtn:
  350. begin
  351. if singleprec then
  352. op:=A_FLT_S
  353. else
  354. op:=A_FLT_D;
  355. swapleftright;
  356. cmpop:=true;
  357. end;
  358. gten:
  359. begin
  360. if singleprec then
  361. op:=A_FLE_S
  362. else
  363. op:=A_FLE_D;
  364. swapleftright;
  365. cmpop:=true;
  366. end;
  367. else
  368. internalerror(200403182);
  369. end;
  370. // put both operands in a register
  371. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  372. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  373. // initialize de result
  374. if not cmpop then
  375. begin
  376. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  377. location.register := cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  378. end
  379. else
  380. begin
  381. location_reset(location,LOC_REGISTER,OS_8);
  382. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  383. end;
  384. // emit the actual operation
  385. if not cmpop then
  386. begin
  387. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,location.register,left.location.register,right.location.register));
  388. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  389. end
  390. else
  391. begin
  392. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,location.register,left.location.register,right.location.register));
  393. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  394. if inv then
  395. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_const(A_XORI,location.register,location.register,1));
  396. end;
  397. end;
  398. procedure trvaddnode.second_cmpfloat;
  399. begin
  400. second_addfloat;
  401. end;
  402. end.