nrvadd.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 expectloc=LOC_FLAGS then
  251. expectloc:=LOC_REGISTER;
  252. if (expectloc=LOC_JUMP) and (nodetype in [equaln, unequaln, ltn, lten, gtn, gten]) then
  253. expectloc:=LOC_REGISTER;
  254. end;
  255. procedure trvaddnode.pass_left_and_right;
  256. begin
  257. { calculate the operator which is more difficult }
  258. firstcomplex(self);
  259. { in case of constant put it to the left }
  260. if (left.nodetype=ordconstn) then
  261. swapleftright;
  262. secondpass(left);
  263. secondpass(right);
  264. end;
  265. function trvaddnode.use_fma: boolean;
  266. begin
  267. Result:=current_settings.fputype in [fpu_fd];
  268. end;
  269. procedure trvaddnode.second_addfloat;
  270. var
  271. op : TAsmOp;
  272. cmpop,
  273. singleprec , inv: boolean;
  274. begin
  275. pass_left_and_right;
  276. if (nf_swapped in flags) then
  277. swapleftright;
  278. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  279. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  280. cmpop:=false;
  281. singleprec:=tfloatdef(left.resultdef).floattype=s32real;
  282. inv:=false;
  283. case nodetype of
  284. addn :
  285. if singleprec then
  286. op:=A_FADD_S
  287. else
  288. op:=A_FADD_D;
  289. muln :
  290. if singleprec then
  291. op:=A_FMUL_S
  292. else
  293. op:=A_FMUL_D;
  294. subn :
  295. if singleprec then
  296. op:=A_FSUB_S
  297. else
  298. op:=A_FSUB_D;
  299. slashn :
  300. if singleprec then
  301. op:=A_FDIV_S
  302. else
  303. op:=A_FDIV_D;
  304. equaln:
  305. begin
  306. if singleprec then
  307. op:=A_FEQ_S
  308. else
  309. op:=A_FEQ_D;
  310. cmpop:=true;
  311. end;
  312. unequaln:
  313. begin
  314. if singleprec then
  315. op:=A_FEQ_S
  316. else
  317. op:=A_FEQ_D;
  318. inv:=true;
  319. cmpop:=true;
  320. end;
  321. ltn:
  322. begin
  323. if singleprec then
  324. op:=A_FLT_S
  325. else
  326. op:=A_FLT_D;
  327. cmpop:=true;
  328. end;
  329. lten:
  330. begin
  331. if singleprec then
  332. op:=A_FLE_S
  333. else
  334. op:=A_FLE_D;
  335. cmpop:=true;
  336. end;
  337. gtn:
  338. begin
  339. if singleprec then
  340. op:=A_FLT_S
  341. else
  342. op:=A_FLT_D;
  343. swapleftright;
  344. cmpop:=true;
  345. end;
  346. gten:
  347. begin
  348. if singleprec then
  349. op:=A_FLE_S
  350. else
  351. op:=A_FLE_D;
  352. swapleftright;
  353. cmpop:=true;
  354. end;
  355. else
  356. internalerror(200403182);
  357. end;
  358. // put both operands in a register
  359. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  360. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  361. // initialize de result
  362. if not cmpop then
  363. begin
  364. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  365. location.register := cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  366. end
  367. else
  368. begin
  369. location_reset(location,LOC_REGISTER,OS_8);
  370. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  371. end;
  372. // emit the actual operation
  373. if not cmpop then
  374. begin
  375. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,location.register,left.location.register,right.location.register));
  376. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  377. end
  378. else
  379. begin
  380. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,location.register,left.location.register,right.location.register));
  381. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  382. if inv then
  383. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_const(A_XORI,location.register,location.register,1));
  384. end;
  385. end;
  386. procedure trvaddnode.second_cmpfloat;
  387. begin
  388. second_addfloat;
  389. end;
  390. end.