2
0

nrvadd.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 not (CPURV_HAS_MUL in cpu_capabilities[current_settings.cputype]) and
  176. (nodetype=muln) and
  177. not(torddef(resultdef).ordtype in [u8bit,s8bit]) then
  178. result:=true
  179. else
  180. Result:=inherited use_mul_helper;
  181. end;
  182. procedure trvaddnode.second_cmpsmallset;
  183. begin
  184. Cmp(false,true);
  185. end;
  186. procedure trvaddnode.second_cmpordinal;
  187. var
  188. unsigned: Boolean;
  189. begin
  190. unsigned:=not(is_signed(left.resultdef)) or
  191. not(is_signed(right.resultdef));
  192. Cmp(not unsigned,false);
  193. end;
  194. procedure trvaddnode.second_cmp64bit;
  195. var
  196. unsigned: Boolean;
  197. begin
  198. unsigned:=not(is_signed(left.resultdef)) or
  199. not(is_signed(right.resultdef));
  200. Cmp(not unsigned,false);
  201. end;
  202. procedure trvaddnode.second_addordinal;
  203. var
  204. unsigned: boolean;
  205. begin
  206. { 32x32->64 multiplication }
  207. if (nodetype=muln) and
  208. is_32bit(left.resultdef) and
  209. is_32bit(right.resultdef) and
  210. is_64bit(resultdef) then
  211. begin
  212. unsigned:=not(is_signed(left.resultdef)) or
  213. not(is_signed(right.resultdef));
  214. pass_left_right;
  215. force_reg_left_right(true,true);
  216. { force_reg_left_right can leave right as a LOC_CONSTANT (we can't
  217. say "a constant register is okay, but an ordinal constant isn't) }
  218. if right.location.loc=LOC_CONSTANT then
  219. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  220. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  221. location.register:=cg.getintregister(current_asmdata.CurrAsmList,def_cgsize(resultdef));
  222. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(A_MUL,location.register,left.location.register,right.location.register));
  223. end
  224. else
  225. inherited second_addordinal;
  226. end;
  227. function trvaddnode.pass_1: tnode;
  228. begin
  229. if (nodetype=muln) and
  230. (left.resultdef.typ=orddef) and (left.resultdef.typ=orddef) and
  231. (CPURV_HAS_MUL in cpu_capabilities[current_settings.cputype])
  232. {$ifdef cpu32bitalu}
  233. and (not (is_64bit(left.resultdef) or
  234. is_64bit(right.resultdef)))
  235. {$endif cpu32bitalu}
  236. then
  237. begin
  238. result:=nil;
  239. firstpass(left);
  240. firstpass(right);
  241. expectloc:=LOC_REGISTER;
  242. end
  243. else if (nodetype=muln) and
  244. (not (CPURV_HAS_MUL in cpu_capabilities[current_settings.cputype])) and
  245. (is_64bit(left.resultdef) or
  246. is_64bit(right.resultdef)) then
  247. begin
  248. result:=first_add64bitint;
  249. end
  250. else
  251. Result:=inherited pass_1;
  252. if expectloc=LOC_FLAGS then
  253. expectloc:=LOC_REGISTER;
  254. if (expectloc=LOC_JUMP) and (nodetype in [equaln, unequaln, ltn, lten, gtn, gten]) then
  255. expectloc:=LOC_REGISTER;
  256. end;
  257. procedure trvaddnode.pass_left_and_right;
  258. begin
  259. { calculate the operator which is more difficult }
  260. firstcomplex(self);
  261. { in case of constant put it to the left }
  262. if (left.nodetype=ordconstn) then
  263. swapleftright;
  264. secondpass(left);
  265. secondpass(right);
  266. end;
  267. function trvaddnode.use_fma: boolean;
  268. begin
  269. Result:=current_settings.fputype in [fpu_fd];
  270. end;
  271. procedure trvaddnode.second_addfloat;
  272. var
  273. op : TAsmOp;
  274. cmpop,
  275. singleprec , inv: boolean;
  276. begin
  277. pass_left_and_right;
  278. if (nf_swapped in flags) then
  279. swapleftright;
  280. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  281. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  282. cmpop:=false;
  283. singleprec:=tfloatdef(left.resultdef).floattype=s32real;
  284. inv:=false;
  285. case nodetype of
  286. addn :
  287. if singleprec then
  288. op:=A_FADD_S
  289. else
  290. op:=A_FADD_D;
  291. muln :
  292. if singleprec then
  293. op:=A_FMUL_S
  294. else
  295. op:=A_FMUL_D;
  296. subn :
  297. if singleprec then
  298. op:=A_FSUB_S
  299. else
  300. op:=A_FSUB_D;
  301. slashn :
  302. if singleprec then
  303. op:=A_FDIV_S
  304. else
  305. op:=A_FDIV_D;
  306. equaln:
  307. begin
  308. if singleprec then
  309. op:=A_FEQ_S
  310. else
  311. op:=A_FEQ_D;
  312. cmpop:=true;
  313. end;
  314. unequaln:
  315. begin
  316. if singleprec then
  317. op:=A_FEQ_S
  318. else
  319. op:=A_FEQ_D;
  320. inv:=true;
  321. cmpop:=true;
  322. end;
  323. ltn:
  324. begin
  325. if singleprec then
  326. op:=A_FLT_S
  327. else
  328. op:=A_FLT_D;
  329. cmpop:=true;
  330. end;
  331. lten:
  332. begin
  333. if singleprec then
  334. op:=A_FLE_S
  335. else
  336. op:=A_FLE_D;
  337. cmpop:=true;
  338. end;
  339. gtn:
  340. begin
  341. if singleprec then
  342. op:=A_FLT_S
  343. else
  344. op:=A_FLT_D;
  345. swapleftright;
  346. cmpop:=true;
  347. end;
  348. gten:
  349. begin
  350. if singleprec then
  351. op:=A_FLE_S
  352. else
  353. op:=A_FLE_D;
  354. swapleftright;
  355. cmpop:=true;
  356. end;
  357. else
  358. internalerror(200403182);
  359. end;
  360. // put both operands in a register
  361. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  362. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  363. // initialize de result
  364. if not cmpop then
  365. begin
  366. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  367. location.register := cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  368. end
  369. else
  370. begin
  371. location_reset(location,LOC_REGISTER,OS_8);
  372. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  373. end;
  374. // emit the actual operation
  375. if not cmpop then
  376. begin
  377. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,location.register,left.location.register,right.location.register));
  378. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  379. end
  380. else
  381. begin
  382. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,location.register,left.location.register,right.location.register));
  383. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  384. if inv then
  385. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_const(A_XORI,location.register,location.register,1));
  386. end;
  387. end;
  388. procedure trvaddnode.second_cmpfloat;
  389. begin
  390. second_addfloat;
  391. end;
  392. end.