nrvadd.pas 15 KB

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