nrvadd.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. if (nf_swapped in flags) then
  235. swapleftright;
  236. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  237. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  238. cmpop:=false;
  239. singleprec:=tfloatdef(left.resultdef).floattype=s32real;
  240. inv:=false;
  241. case nodetype of
  242. addn :
  243. if singleprec then
  244. op:=A_FADD_S
  245. else
  246. op:=A_FADD_D;
  247. muln :
  248. if singleprec then
  249. op:=A_FMUL_S
  250. else
  251. op:=A_FMUL_D;
  252. subn :
  253. if singleprec then
  254. op:=A_FSUB_S
  255. else
  256. op:=A_FSUB_D;
  257. slashn :
  258. if singleprec then
  259. op:=A_FDIV_S
  260. else
  261. op:=A_FDIV_D;
  262. equaln:
  263. begin
  264. if singleprec then
  265. op:=A_FEQ_S
  266. else
  267. op:=A_FEQ_D;
  268. cmpop:=true;
  269. end;
  270. unequaln:
  271. begin
  272. if singleprec then
  273. op:=A_FEQ_S
  274. else
  275. op:=A_FEQ_D;
  276. inv:=true;
  277. cmpop:=true;
  278. end;
  279. ltn:
  280. begin
  281. if singleprec then
  282. op:=A_FLT_S
  283. else
  284. op:=A_FLT_D;
  285. cmpop:=true;
  286. end;
  287. lten:
  288. begin
  289. if singleprec then
  290. op:=A_FLE_S
  291. else
  292. op:=A_FLE_D;
  293. cmpop:=true;
  294. end;
  295. gtn:
  296. begin
  297. if singleprec then
  298. op:=A_FLT_S
  299. else
  300. op:=A_FLT_D;
  301. swapleftright;
  302. cmpop:=true;
  303. end;
  304. gten:
  305. begin
  306. if singleprec then
  307. op:=A_FLE_S
  308. else
  309. op:=A_FLE_D;
  310. swapleftright;
  311. cmpop:=true;
  312. end;
  313. else
  314. internalerror(200403182);
  315. end;
  316. // put both operands in a register
  317. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  318. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  319. // initialize de result
  320. if not cmpop then
  321. begin
  322. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  323. location.register := cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  324. end
  325. else
  326. begin
  327. location_reset(location,LOC_REGISTER,OS_8);
  328. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  329. end;
  330. // emit the actual operation
  331. if not cmpop then
  332. begin
  333. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,location.register,left.location.register,right.location.register));
  334. cg.g_check_for_fpu_exception(current_asmdata.CurrAsmList);
  335. end
  336. else
  337. begin
  338. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,location.register,left.location.register,right.location.register));
  339. cg.g_check_for_fpu_exception(current_asmdata.CurrAsmList);
  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.