ncpuadd.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. {
  2. Copyright (c) 2000-2006 by Florian Klaempfl and Jonas Maebe
  3. Code generation for add nodes on the LoongArch64
  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 ncpuadd;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,nadd,ncgadd,cpubase;
  22. type
  23. tloongarch64addnode = class(tcgaddnode)
  24. private
  25. procedure Cmp(signed,is_smallset: boolean);
  26. protected
  27. procedure second_cmpsmallset;override;
  28. procedure second_cmpordinal;override;
  29. procedure second_cmp64bit; override;
  30. procedure second_addordinal; override;
  31. procedure second_add64bit; override;
  32. procedure pass_left_and_right;
  33. procedure second_addfloat;override;
  34. procedure second_cmpfloat;override;
  35. public
  36. function use_generic_mul32to64: boolean; override;
  37. function pass_1 : tnode;override;
  38. end;
  39. implementation
  40. uses
  41. globtype,systems,
  42. cutils,verbose,globals,
  43. symconst,symdef,paramgr,
  44. aasmbase,aasmtai,aasmdata,aasmcpu,defutil,htypechk,
  45. cgbase,cpuinfo,pass_1,pass_2,
  46. cpupara,cgcpu,cgutils,procinfo,
  47. ncon,nset,
  48. ncgutil,tgobj,rgobj,rgcpu,cgobj,hlcgobj;
  49. procedure tloongarch64addnode.Cmp(signed,is_smallset: boolean);
  50. var
  51. flabel,tlabel: tasmlabel;
  52. op, opi: TAsmOp;
  53. allow_constant : boolean;
  54. begin
  55. pass_left_right;
  56. allow_constant:=(not is_smallset) or not (nodetype in [lten,gten]);
  57. force_reg_left_right(true,allow_constant);
  58. if nf_swapped in flags then
  59. swapleftright;
  60. location_reset(location,LOC_REGISTER,OS_INT);
  61. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  62. if signed then op:=A_SLT else op:=A_SLTU;
  63. if signed then opi:=A_SLTI else opi:=A_SLTUI;
  64. case nodetype of
  65. equaln:
  66. begin
  67. if not (left.location.loc in [LOC_CREGISTER,LOC_REGISTER]) then
  68. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  69. if (right.location.loc=LOC_CONSTANT) and
  70. (not is_uimm12(right.location.value)) then
  71. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,false);
  72. if right.location.loc=LOC_CONSTANT then
  73. if right.location.value = 0 then
  74. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,left.location.register,location.register)
  75. else
  76. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_const(A_XORI,location.register,left.location.register,right.location.value))
  77. else
  78. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(A_XOR,location.register,left.location.register,right.location.register));
  79. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_const(A_SLTUI,location.register,location.register,1));
  80. end;
  81. unequaln:
  82. begin
  83. if not (left.location.loc in [LOC_CREGISTER,LOC_REGISTER]) then
  84. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  85. if (right.location.loc=LOC_CONSTANT) and
  86. (not is_uimm12(right.location.value)) then
  87. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,false);
  88. if right.location.loc=LOC_CONSTANT then
  89. if right.location.value = 0 then
  90. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,left.location.register,location.register)
  91. else
  92. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_const(A_XORI,location.register,left.location.register,right.location.value))
  93. else
  94. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(A_XOR,location.register,left.location.register,right.location.register));
  95. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(A_SLTU,location.register,NR_R0,location.register));
  96. end;
  97. ltn:
  98. begin
  99. if not (left.location.loc in [LOC_CREGISTER,LOC_REGISTER]) then
  100. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  101. if (right.location.loc=LOC_CONSTANT) and
  102. (not is_simm12(right.location.value)) then
  103. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,false);
  104. if right.location.loc=LOC_CONSTANT then
  105. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_const(opi,location.register,left.location.register,right.location.value))
  106. else
  107. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(op,location.register,left.location.register,right.location.register));
  108. end;
  109. gtn:
  110. begin
  111. if not (right.location.loc in [LOC_CREGISTER,LOC_REGISTER]) then
  112. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,false);
  113. if (left.location.loc=LOC_CONSTANT) and
  114. (not is_simm12(left.location.value)) then
  115. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  116. if left.location.loc=LOC_CONSTANT then
  117. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_const(opi,location.register,right.location.register,left.location.value))
  118. else
  119. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(op,location.register,right.location.register,left.location.register));
  120. end;
  121. lten:
  122. begin
  123. if not (right.location.loc in [LOC_CREGISTER,LOC_REGISTER]) then
  124. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,false);
  125. if (left.location.loc=LOC_CONSTANT) and
  126. (not is_simm12(left.location.value)) then
  127. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  128. if is_smallset then
  129. begin
  130. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(A_ANDN,location.register,left.location.register,right.location.register));
  131. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_const(A_SLTUI,location.register,location.register,1));
  132. end
  133. else
  134. begin
  135. if left.location.loc=LOC_CONSTANT then
  136. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_const(opi,location.register,right.location.register,left.location.value))
  137. else
  138. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(op,location.register,right.location.register,left.location.register));
  139. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_const(A_XORI,location.register,location.register,1));
  140. end;
  141. end;
  142. gten:
  143. begin
  144. if not (left.location.loc in [LOC_CREGISTER,LOC_REGISTER]) then
  145. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  146. if (right.location.loc=LOC_CONSTANT) and
  147. (not is_simm12(right.location.value)) then
  148. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,false);
  149. if is_smallset then
  150. begin
  151. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(A_ANDN,location.register,right.location.register,left.location.register));
  152. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_const(A_SLTUI,location.register,location.register,1));
  153. end
  154. else
  155. begin
  156. if right.location.loc=LOC_CONSTANT then
  157. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_const(opi,location.register,left.location.register,right.location.value))
  158. else
  159. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(op,location.register,left.location.register,right.location.register));
  160. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_const(A_XORI,location.register,location.register,1));
  161. end;
  162. end;
  163. else
  164. Internalerror(2022111946);
  165. end;
  166. end;
  167. { Smallset means the one all bits in another one. }
  168. procedure tloongarch64addnode.second_cmpsmallset;
  169. begin
  170. Cmp(false,true);
  171. end;
  172. procedure tloongarch64addnode.second_cmpordinal;
  173. var
  174. unsigned: Boolean;
  175. begin
  176. unsigned:=not(is_signed(left.resultdef)) or
  177. not(is_signed(right.resultdef));
  178. Cmp(not unsigned,false);
  179. end;
  180. procedure tloongarch64addnode.second_cmp64bit;
  181. var
  182. unsigned: Boolean;
  183. begin
  184. unsigned:=not(is_signed(left.resultdef)) or
  185. not(is_signed(right.resultdef));
  186. Cmp(not unsigned,false);
  187. end;
  188. procedure tloongarch64addnode.second_addordinal;
  189. const
  190. multops: array[boolean] of TAsmOp = (A_MULW_D_W,A_MULW_D_WU);
  191. var
  192. unsigned: boolean;
  193. begin
  194. { 32x32->64 multiplication }
  195. if (nodetype=muln) and
  196. is_32bit(left.resultdef) and
  197. is_32bit(right.resultdef) and
  198. is_64bit(resultdef) then
  199. begin
  200. unsigned:=not(is_signed(left.resultdef)) or
  201. not(is_signed(right.resultdef));
  202. pass_left_right;
  203. force_reg_left_right(true,true);
  204. { force_reg_left_right can leave right as a LOC_CONSTANT (we can't
  205. say "a constant register is okay, but an ordinal constant isn't) }
  206. if right.location.loc=LOC_CONSTANT then
  207. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  208. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  209. location.register:=cg.getintregister(current_asmdata.CurrAsmList,def_cgsize(resultdef));
  210. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(multops[unsigned],location.register,left.location.register,right.location.register));
  211. end
  212. else
  213. inherited second_addordinal;
  214. end;
  215. procedure tloongarch64addnode.second_add64bit;
  216. begin
  217. second_addordinal;
  218. end;
  219. procedure tloongarch64addnode.pass_left_and_right;
  220. begin
  221. { calculate the operator which is more difficult }
  222. firstcomplex(self);
  223. { in case of constant put it to the left }
  224. if (left.nodetype=ordconstn) then
  225. swapleftright;
  226. secondpass(left);
  227. secondpass(right);
  228. end;
  229. procedure tloongarch64addnode.second_addfloat;
  230. var
  231. op : TAsmOp;
  232. cmpop,
  233. singleprec: boolean;
  234. begin
  235. pass_left_and_right;
  236. if (nf_swapped in flags) then
  237. swapleftright;
  238. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  239. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  240. cmpop:=false;
  241. singleprec:=tfloatdef(left.resultdef).floattype=s32real;
  242. case nodetype of
  243. addn :
  244. if singleprec then
  245. op:=A_FADD_S
  246. else
  247. op:=A_FADD_D;
  248. muln :
  249. if singleprec then
  250. op:=A_FMUL_S
  251. else
  252. op:=A_FMUL_D;
  253. subn :
  254. if singleprec then
  255. op:=A_FSUB_S
  256. else
  257. op:=A_FSUB_D;
  258. slashn :
  259. if singleprec then
  260. op:=A_FDIV_S
  261. else
  262. op:=A_FDIV_D;
  263. equaln:
  264. begin
  265. if singleprec then
  266. op:=A_FCMP_CEQ_S
  267. else
  268. op:=A_FCMP_CEQ_D;
  269. cmpop:=true;
  270. end;
  271. unequaln:
  272. begin
  273. if singleprec then
  274. op:=A_FCMP_CUNE_S
  275. else
  276. op:=A_FCMP_CUNE_D;
  277. cmpop:=true;
  278. end;
  279. ltn:
  280. begin
  281. if singleprec then
  282. op:=A_FCMP_SLT_S
  283. else
  284. op:=A_FCMP_SLT_D;
  285. cmpop:=true;
  286. end;
  287. lten:
  288. begin
  289. if singleprec then
  290. op:=A_FCMP_SLE_S
  291. else
  292. op:=A_FCMP_SLE_D;
  293. cmpop:=true;
  294. end;
  295. gtn:
  296. begin
  297. if singleprec then
  298. op:=A_FCMP_SGT_S
  299. else
  300. op:=A_FCMP_SGT_D;
  301. cmpop:=true;
  302. end;
  303. gten:
  304. begin
  305. if singleprec then
  306. op:=A_FCMP_SGE_S
  307. else
  308. op:=A_FCMP_SGE_D;
  309. cmpop:=true;
  310. end;
  311. else
  312. internalerror(2022111947);
  313. end;
  314. if cmpop then
  315. begin
  316. { TODO This should be like mips, but... }
  317. { location_reset(location, LOC_FLAGS, OS_NO); }
  318. { location.register := cg.getfpuregister(current_asmdata.CurrAsmList,location.size); }
  319. location_reset(location,LOC_REGISTER,OS_8);
  320. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  321. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,NR_FCC0,left.location.register,right.location.register));
  322. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_MOVCF2GR,location.register,NR_FCC0));
  323. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  324. end
  325. else
  326. begin
  327. location_reset(location, LOC_FPUREGISTER, def_cgsize(resultdef));
  328. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  329. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,location.register,left.location.register,right.location.register));
  330. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  331. end;
  332. end;
  333. procedure tloongarch64addnode.second_cmpfloat;
  334. begin
  335. second_addfloat;
  336. end;
  337. function tloongarch64addnode.use_generic_mul32to64: boolean;
  338. begin
  339. result:=false;
  340. end;
  341. function tloongarch64addnode.pass_1: tnode;
  342. begin
  343. Result:=inherited pass_1;
  344. { if the result is not nil, a new node has been generated and the current node will be discarted }
  345. if Result=nil then
  346. begin
  347. if left.resultdef.typ=floatdef then
  348. if needs_check_for_fpu_exceptions then
  349. Include(current_procinfo.flags,pi_do_call);
  350. end;
  351. end;
  352. begin
  353. caddnode := tloongarch64addnode;
  354. end.