nrvadd.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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: 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: boolean);
  54. var
  55. flabel,tlabel: tasmlabel;
  56. op, opi: TAsmOp;
  57. begin
  58. pass_left_right;
  59. force_reg_left_right(true,true);
  60. if nf_swapped in flags then
  61. swapleftright;
  62. location_reset(location,LOC_REGISTER,OS_INT);
  63. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  64. if signed then op:=A_SLT else op:=A_SLTU;
  65. if signed then opi:=A_SLTI else opi:=A_SLTIU;
  66. case nodetype of
  67. equaln:
  68. begin
  69. if not (left.location.loc in [LOC_CREGISTER,LOC_REGISTER]) then
  70. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  71. if (right.location.loc=LOC_CONSTANT) and
  72. { right.location.value might be $8000000000000000,
  73. and its minus value generates an overflow here }
  74. {$ifdef AVOID_OVERFLOW} ((right.location.value = low_value) or {$endif}
  75. (not is_imm12(-right.location.value)) {$ifdef AVOID_OVERFLOW}){$endif} then
  76. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,false);
  77. if right.location.loc=LOC_CONSTANT then
  78. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_const(A_ADDI,location.register,left.location.register,-right.location.value))
  79. else
  80. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(A_SUB,location.register,left.location.register,right.location.register));
  81. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_const(A_SLTIU,location.register,location.register,1));
  82. end;
  83. unequaln:
  84. begin
  85. if not (left.location.loc in [LOC_CREGISTER,LOC_REGISTER]) then
  86. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  87. if (right.location.loc=LOC_CONSTANT) and
  88. { right.location.value might be $8000000000000000,
  89. and its minus value generates an overflow here }
  90. {$ifdef AVOID_OVERFLOW} ((right.location.value = low_value) or {$endif}
  91. (not is_imm12(-right.location.value)) {$ifdef AVOID_OVERFLOW}){$endif} then
  92. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,false);
  93. if right.location.loc=LOC_CONSTANT then
  94. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_const(A_ADDI,location.register,left.location.register,-right.location.value))
  95. else
  96. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(A_SUB,location.register,left.location.register,right.location.register));
  97. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(A_SLTU,location.register,NR_X0,location.register));
  98. end;
  99. ltn:
  100. begin
  101. if not (left.location.loc in [LOC_CREGISTER,LOC_REGISTER]) then
  102. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  103. if (right.location.loc=LOC_CONSTANT) and
  104. (not is_imm12(right.location.value)) then
  105. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,false);
  106. if right.location.loc=LOC_CONSTANT then
  107. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_const(opi,location.register,left.location.register,right.location.value))
  108. else
  109. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(op,location.register,left.location.register,right.location.register));
  110. end;
  111. gtn:
  112. begin
  113. if not (right.location.loc in [LOC_CREGISTER,LOC_REGISTER]) then
  114. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,false);
  115. if (left.location.loc=LOC_CONSTANT) and
  116. (not is_imm12(left.location.value)) then
  117. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  118. if left.location.loc=LOC_CONSTANT then
  119. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_const(opi,location.register,right.location.register,left.location.value))
  120. else
  121. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(op,location.register,right.location.register,left.location.register));
  122. end;
  123. lten:
  124. begin
  125. if not (right.location.loc in [LOC_CREGISTER,LOC_REGISTER]) then
  126. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,false);
  127. if (left.location.loc=LOC_CONSTANT) and
  128. (not is_imm12(left.location.value)) then
  129. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  130. if left.location.loc=LOC_CONSTANT then
  131. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_const(opi,location.register,right.location.register,left.location.value))
  132. else
  133. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(op,location.register,right.location.register,left.location.register));
  134. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_const(A_SLTIU,location.register,location.register,1));
  135. end;
  136. gten:
  137. begin
  138. if not (left.location.loc in [LOC_CREGISTER,LOC_REGISTER]) then
  139. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  140. if (right.location.loc=LOC_CONSTANT) and
  141. (not is_imm12(right.location.value)) then
  142. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,false);
  143. if right.location.loc=LOC_CONSTANT then
  144. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_const(opi,location.register,left.location.register,right.location.value))
  145. else
  146. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(op,location.register,left.location.register,right.location.register));
  147. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_const(A_SLTIU,location.register,location.register,1));
  148. end;
  149. else
  150. Internalerror(2016061101);
  151. end;
  152. end;
  153. function trvaddnode.use_mul_helper: boolean;
  154. begin
  155. if not (CPURV_HAS_MUL in cpu_capabilities[current_settings.cputype]) and
  156. (nodetype=muln) and
  157. not(torddef(resultdef).ordtype in [u8bit,s8bit]) then
  158. result:=true
  159. else
  160. Result:=inherited use_mul_helper;
  161. end;
  162. procedure trvaddnode.second_cmpsmallset;
  163. begin
  164. Cmp(true);
  165. end;
  166. procedure trvaddnode.second_cmpordinal;
  167. var
  168. unsigned: Boolean;
  169. begin
  170. unsigned:=not(is_signed(left.resultdef)) or
  171. not(is_signed(right.resultdef));
  172. Cmp(not unsigned);
  173. end;
  174. procedure trvaddnode.second_cmp64bit;
  175. var
  176. unsigned: Boolean;
  177. begin
  178. unsigned:=not(is_signed(left.resultdef)) or
  179. not(is_signed(right.resultdef));
  180. Cmp(not unsigned);
  181. end;
  182. procedure trvaddnode.second_addordinal;
  183. var
  184. unsigned: boolean;
  185. begin
  186. { 32x32->64 multiplication }
  187. if (nodetype=muln) and
  188. is_32bit(left.resultdef) and
  189. is_32bit(right.resultdef) and
  190. is_64bit(resultdef) then
  191. begin
  192. unsigned:=not(is_signed(left.resultdef)) or
  193. not(is_signed(right.resultdef));
  194. pass_left_right;
  195. force_reg_left_right(true,true);
  196. { force_reg_left_right can leave right as a LOC_CONSTANT (we can't
  197. say "a constant register is okay, but an ordinal constant isn't) }
  198. if right.location.loc=LOC_CONSTANT then
  199. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  200. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  201. location.register:=cg.getintregister(current_asmdata.CurrAsmList,def_cgsize(resultdef));
  202. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(A_MUL,location.register,left.location.register,right.location.register));
  203. end
  204. else
  205. inherited second_addordinal;
  206. end;
  207. function trvaddnode.pass_1: tnode;
  208. begin
  209. if (nodetype=muln) and
  210. (left.resultdef.typ=orddef) and (left.resultdef.typ=orddef) and
  211. (CPURV_HAS_MUL in cpu_capabilities[current_settings.cputype])
  212. {$ifdef cpu32bitalu}
  213. and (not (is_64bit(left.resultdef) or
  214. is_64bit(right.resultdef)))
  215. {$endif cpu32bitalu}
  216. then
  217. begin
  218. result:=nil;
  219. firstpass(left);
  220. firstpass(right);
  221. expectloc:=LOC_REGISTER;
  222. end
  223. else if (nodetype=muln) and
  224. (not (CPURV_HAS_MUL in cpu_capabilities[current_settings.cputype])) and
  225. (is_64bit(left.resultdef) or
  226. is_64bit(right.resultdef)) then
  227. begin
  228. result:=first_add64bitint;
  229. end
  230. else
  231. Result:=inherited pass_1;
  232. if expectloc=LOC_FLAGS then
  233. expectloc:=LOC_REGISTER;
  234. end;
  235. procedure trvaddnode.pass_left_and_right;
  236. begin
  237. { calculate the operator which is more difficult }
  238. firstcomplex(self);
  239. { in case of constant put it to the left }
  240. if (left.nodetype=ordconstn) then
  241. swapleftright;
  242. secondpass(left);
  243. secondpass(right);
  244. end;
  245. function trvaddnode.use_fma: boolean;
  246. begin
  247. Result:=current_settings.fputype in [fpu_fd];
  248. end;
  249. procedure trvaddnode.second_addfloat;
  250. var
  251. op : TAsmOp;
  252. cmpop,
  253. singleprec , inv: boolean;
  254. begin
  255. pass_left_and_right;
  256. if (nf_swapped in flags) then
  257. swapleftright;
  258. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  259. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  260. cmpop:=false;
  261. singleprec:=tfloatdef(left.resultdef).floattype=s32real;
  262. inv:=false;
  263. case nodetype of
  264. addn :
  265. if singleprec then
  266. op:=A_FADD_S
  267. else
  268. op:=A_FADD_D;
  269. muln :
  270. if singleprec then
  271. op:=A_FMUL_S
  272. else
  273. op:=A_FMUL_D;
  274. subn :
  275. if singleprec then
  276. op:=A_FSUB_S
  277. else
  278. op:=A_FSUB_D;
  279. slashn :
  280. if singleprec then
  281. op:=A_FDIV_S
  282. else
  283. op:=A_FDIV_D;
  284. equaln:
  285. begin
  286. if singleprec then
  287. op:=A_FEQ_S
  288. else
  289. op:=A_FEQ_D;
  290. cmpop:=true;
  291. end;
  292. unequaln:
  293. begin
  294. if singleprec then
  295. op:=A_FEQ_S
  296. else
  297. op:=A_FEQ_D;
  298. inv:=true;
  299. cmpop:=true;
  300. end;
  301. ltn:
  302. begin
  303. if singleprec then
  304. op:=A_FLT_S
  305. else
  306. op:=A_FLT_D;
  307. cmpop:=true;
  308. end;
  309. lten:
  310. begin
  311. if singleprec then
  312. op:=A_FLE_S
  313. else
  314. op:=A_FLE_D;
  315. cmpop:=true;
  316. end;
  317. gtn:
  318. begin
  319. if singleprec then
  320. op:=A_FLT_S
  321. else
  322. op:=A_FLT_D;
  323. swapleftright;
  324. cmpop:=true;
  325. end;
  326. gten:
  327. begin
  328. if singleprec then
  329. op:=A_FLE_S
  330. else
  331. op:=A_FLE_D;
  332. swapleftright;
  333. cmpop:=true;
  334. end;
  335. else
  336. internalerror(200403182);
  337. end;
  338. // put both operands in a register
  339. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  340. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  341. // initialize de result
  342. if not cmpop then
  343. begin
  344. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  345. location.register := cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  346. end
  347. else
  348. begin
  349. location_reset(location,LOC_REGISTER,OS_8);
  350. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  351. end;
  352. // emit the actual operation
  353. if not cmpop then
  354. begin
  355. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,location.register,left.location.register,right.location.register));
  356. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  357. end
  358. else
  359. begin
  360. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,location.register,left.location.register,right.location.register));
  361. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  362. if inv then
  363. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_const(A_XORI,location.register,location.register,1));
  364. end;
  365. end;
  366. procedure trvaddnode.second_cmpfloat;
  367. begin
  368. second_addfloat;
  369. end;
  370. end.