ncpuadd.pas 15 KB

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