nrvadd.pas 15 KB

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