ncpuadd.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. {
  2. Copyright (c) 2000-2009 by Florian Klaempfl and David Zhang
  3. Code generation for add nodes on the FVM32
  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, ncgadd, cpubase, aasmbase, cgbase;
  22. type
  23. { tmipsaddnode }
  24. tmipsaddnode = class(tcgaddnode)
  25. private
  26. procedure cmp64_lt(left_reg, right_reg: TRegister64;unsigned:boolean);
  27. procedure cmp64_le(left_reg, right_reg: TRegister64;unsigned:boolean);
  28. procedure second_generic_cmp32(unsigned,is_smallset: boolean);
  29. procedure second_mul64bit;
  30. protected
  31. procedure second_addfloat; override;
  32. procedure second_cmpfloat; override;
  33. procedure second_cmpboolean; override;
  34. procedure second_cmpsmallset; override;
  35. procedure second_add64bit; override;
  36. procedure second_cmp64bit; override;
  37. procedure second_cmpordinal; override;
  38. procedure second_addordinal; override;
  39. public
  40. function use_generic_mul32to64: boolean; override;
  41. function use_generic_mul64bit: boolean; override;
  42. end;
  43. implementation
  44. uses
  45. systems, globtype, globals,
  46. cutils, verbose,
  47. paramgr,
  48. aasmtai, aasmcpu, aasmdata,
  49. defutil,
  50. cpuinfo,
  51. {cgbase,} cgcpu, cgutils,
  52. cpupara,
  53. procinfo,
  54. symconst,symdef,
  55. ncon, nset, nadd,
  56. ncgutil, hlcgobj, cgobj;
  57. {*****************************************************************************
  58. tmipsaddnode
  59. *****************************************************************************}
  60. procedure tmipsaddnode.second_generic_cmp32(unsigned,is_smallset: boolean);
  61. var
  62. cond: TOpCmp;
  63. allow_constant : boolean;
  64. dreg : tregister;
  65. begin
  66. pass_left_right;
  67. allow_constant:=(not is_smallset) or not (nodetype in [lten,gten]);
  68. force_reg_left_right(True, allow_constant);
  69. location_reset(location,LOC_FLAGS,OS_NO);
  70. cond:=cmpnode2topcmp(unsigned);
  71. if nf_swapped in flags then
  72. cond:=swap_opcmp(cond);
  73. if is_smallset and (nodetype in [lten,gten]) then
  74. begin
  75. if ((nodetype=lten) and not (nf_swapped in flags)) or
  76. ((nodetype=gten) and (nf_swapped in flags)) then
  77. dreg:=right.location.register
  78. else
  79. dreg:=left.location.register;
  80. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(A_AND,dreg,right.location.register,left.location.register));
  81. cond:=OC_EQ;
  82. end;
  83. location.resflags.cond:=cond;
  84. location.resflags.reg1:=left.location.register;
  85. location.resflags.use_const:=(right.location.loc=LOC_CONSTANT);
  86. if location.resflags.use_const then
  87. location.resflags.value:=right.location.value
  88. else
  89. location.resflags.reg2:=right.location.register;
  90. end;
  91. procedure tmipsaddnode.second_add64bit;
  92. begin
  93. if (nodetype=muln) then
  94. second_mul64bit
  95. else
  96. inherited second_add64bit;
  97. end;
  98. const
  99. cmpops: array[boolean] of TOpCmp = (OC_LT,OC_B);
  100. procedure tmipsaddnode.cmp64_lt(left_reg, right_reg: TRegister64;unsigned: boolean);
  101. begin
  102. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,cmpops[unsigned],right_reg.reghi,left_reg.reghi,location.truelabel);
  103. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left_reg.reghi,right_reg.reghi,location.falselabel);
  104. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_B,right_reg.reglo,left_reg.reglo,location.truelabel);
  105. cg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  106. end;
  107. procedure tmipsaddnode.cmp64_le(left_reg, right_reg: TRegister64;unsigned: boolean);
  108. begin
  109. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,cmpops[unsigned],left_reg.reghi,right_reg.reghi,location.falselabel);
  110. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left_reg.reghi,right_reg.reghi,location.truelabel);
  111. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_B,left_reg.reglo,right_reg.reglo,location.falselabel);
  112. cg.a_jmp_always(current_asmdata.CurrAsmList,location.truelabel);
  113. end;
  114. procedure tmipsaddnode.second_cmp64bit;
  115. var
  116. truelabel,
  117. falselabel: tasmlabel;
  118. unsigned: boolean;
  119. left_reg,right_reg: TRegister64;
  120. begin
  121. current_asmdata.getjumplabel(truelabel);
  122. current_asmdata.getjumplabel(falselabel);
  123. location_reset_jump(location,truelabel,falselabel);
  124. pass_left_right;
  125. force_reg_left_right(true,true);
  126. unsigned:=not(is_signed(left.resultdef)) or
  127. not(is_signed(right.resultdef));
  128. left_reg:=left.location.register64;
  129. if (right.location.loc=LOC_CONSTANT) then
  130. begin
  131. if lo(right.location.value64)=0 then
  132. right_reg.reglo:=NR_R0
  133. else
  134. begin
  135. right_reg.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  136. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,lo(right.location.value64),right_reg.reglo);
  137. end;
  138. if hi(right.location.value64)=0 then
  139. right_reg.reghi:=NR_R0
  140. else
  141. begin
  142. right_reg.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  143. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,hi(right.location.value64),right_reg.reghi);
  144. end;
  145. end
  146. else
  147. right_reg:=right.location.register64;
  148. case NodeType of
  149. equaln:
  150. begin
  151. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left_reg.reghi,right_reg.reghi,location.falselabel);
  152. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left_reg.reglo,right_reg.reglo,location.falselabel);
  153. cg.a_jmp_always(current_asmdata.CurrAsmList,location.truelabel);
  154. end;
  155. unequaln:
  156. begin
  157. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left_reg.reghi,right_reg.reghi,location.truelabel);
  158. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left_reg.reglo,right_reg.reglo,location.truelabel);
  159. cg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  160. end;
  161. else
  162. if nf_swapped in flags then
  163. case NodeType of
  164. ltn:
  165. cmp64_lt(right_reg, left_reg,unsigned);
  166. lten:
  167. cmp64_le(right_reg, left_reg,unsigned);
  168. gtn:
  169. cmp64_lt(left_reg, right_reg,unsigned);
  170. gten:
  171. cmp64_le(left_reg, right_reg,unsigned);
  172. else
  173. internalerror(2019051034);
  174. end
  175. else
  176. case NodeType of
  177. ltn:
  178. cmp64_lt(left_reg, right_reg,unsigned);
  179. lten:
  180. cmp64_le(left_reg, right_reg,unsigned);
  181. gtn:
  182. cmp64_lt(right_reg, left_reg,unsigned);
  183. gten:
  184. cmp64_le(right_reg, left_reg,unsigned);
  185. else
  186. internalerror(2019051033);
  187. end;
  188. end;
  189. end;
  190. procedure tmipsaddnode.second_addfloat;
  191. var
  192. op: TAsmOp;
  193. begin
  194. pass_left_right;
  195. if (nf_swapped in flags) then
  196. swapleftright;
  197. { force fpureg as location, left right doesn't matter
  198. as both will be in a fpureg }
  199. hlcg.location_force_fpureg(current_asmdata.CurrAsmList, left.location, left.resultdef, True);
  200. hlcg.location_force_fpureg(current_asmdata.CurrAsmList, right.location, right.resultdef, True);
  201. location_reset(location, LOC_FPUREGISTER, def_cgsize(resultdef));
  202. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  203. case nodetype of
  204. addn:
  205. begin
  206. if location.size = OS_F64 then
  207. op := A_ADD_D
  208. else
  209. op := A_ADD_S;
  210. end;
  211. muln:
  212. begin
  213. if location.size = OS_F64 then
  214. op := A_MUL_D
  215. else
  216. op := A_MUL_S;
  217. end;
  218. subn:
  219. begin
  220. if location.size = OS_F64 then
  221. op := A_SUB_D
  222. else
  223. op := A_SUB_S;
  224. end;
  225. slashn:
  226. begin
  227. if location.size = OS_F64 then
  228. op := A_DIV_D
  229. else
  230. op := A_DIV_S;
  231. end;
  232. else
  233. internalerror(200306014);
  234. end;
  235. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,
  236. location.Register, left.location.Register, right.location.Register));
  237. end;
  238. const
  239. ops_cmpfloat: array[boolean,ltn..unequaln] of TAsmOp = (
  240. // ltn lten gtn gten equaln unequaln
  241. (A_C_LT_S, A_C_LE_S, A_C_LT_S, A_C_LE_S, A_C_EQ_S, A_C_EQ_S),
  242. (A_C_LT_D, A_C_LE_D, A_C_LT_D, A_C_LE_D, A_C_EQ_D, A_C_EQ_D)
  243. );
  244. procedure tmipsaddnode.second_cmpfloat;
  245. var
  246. op: tasmop;
  247. lreg,rreg: tregister;
  248. begin
  249. pass_left_right;
  250. if nf_swapped in flags then
  251. swapleftright;
  252. hlcg.location_force_fpureg(current_asmdata.CurrAsmList, left.location, left.resultdef, True);
  253. hlcg.location_force_fpureg(current_asmdata.CurrAsmList, right.location, right.resultdef, True);
  254. location_reset(location, LOC_FLAGS, OS_NO);
  255. op:=ops_cmpfloat[left.location.size=OS_F64,nodetype];
  256. if (nodetype in [gtn,gten]) then
  257. begin
  258. lreg:=right.location.register;
  259. rreg:=left.location.register;
  260. end
  261. else
  262. begin
  263. lreg:=left.location.register;
  264. rreg:=right.location.register;
  265. end;
  266. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,lreg,rreg));
  267. location.resflags.reg1:=NR_FCC0;
  268. if (nodetype=unequaln) then
  269. location.resflags.cond:=OC_EQ
  270. else
  271. location.resflags.cond:=OC_NE;
  272. end;
  273. procedure tmipsaddnode.second_cmpboolean;
  274. begin
  275. second_generic_cmp32(true,false);
  276. end;
  277. procedure tmipsaddnode.second_cmpsmallset;
  278. begin
  279. second_generic_cmp32(true,true);
  280. end;
  281. procedure tmipsaddnode.second_cmpordinal;
  282. var
  283. unsigned: boolean;
  284. begin
  285. unsigned := not (is_signed(left.resultdef)) or not (is_signed(right.resultdef));
  286. second_generic_cmp32(unsigned,false);
  287. end;
  288. const
  289. multops: array[boolean] of TAsmOp = (A_MULT, A_MULTU);
  290. procedure tmipsaddnode.second_addordinal;
  291. var
  292. unsigned: boolean;
  293. begin
  294. unsigned:=not(is_signed(left.resultdef)) or
  295. not(is_signed(right.resultdef));
  296. if (nodetype=muln) and is_64bit(resultdef) then
  297. begin
  298. pass_left_right;
  299. force_reg_left_right(true,false);
  300. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  301. location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  302. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  303. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg(multops[unsigned],left.location.register,right.location.register));
  304. current_asmdata.CurrAsmList.Concat(taicpu.op_reg(A_MFLO,location.register64.reglo));
  305. current_asmdata.CurrAsmList.Concat(taicpu.op_reg(A_MFHI,location.register64.reghi));
  306. end
  307. else
  308. inherited second_addordinal;
  309. end;
  310. procedure tmipsaddnode.second_mul64bit;
  311. var
  312. list: TAsmList;
  313. hreg1,hreg2,tmpreg: TRegister;
  314. begin
  315. list:=current_asmdata.CurrAsmList;
  316. pass_left_right;
  317. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  318. hlcg.location_force_reg(list,left.location,left.resultdef,left.resultdef,true);
  319. { calculate 32-bit terms lo(right)*hi(left) and hi(left)*lo(right) }
  320. hreg1:=NR_NO;
  321. hreg2:=NR_NO;
  322. tmpreg:=NR_NO;
  323. if (right.location.loc=LOC_CONSTANT) then
  324. begin
  325. { Omit zero terms, if any }
  326. if hi(right.location.value64)<>0 then
  327. begin
  328. hreg2:=cg.getintregister(list,OS_INT);
  329. tmpreg:=cg.getintregister(list,OS_INT);
  330. cg.a_load_const_reg(list,OS_INT,longint(hi(right.location.value64)),tmpreg);
  331. list.concat(taicpu.op_reg_reg_reg(A_MUL,hreg2,tmpreg,left.location.register64.reglo));
  332. end;
  333. tmpreg:=NR_NO;
  334. if lo(right.location.value64)<>0 then
  335. begin
  336. hreg1:=cg.getintregister(list,OS_INT);
  337. tmpreg:=cg.getintregister(list,OS_INT);
  338. cg.a_load_const_reg(list,OS_INT,longint(lo(right.location.value64)),tmpreg);
  339. list.concat(taicpu.op_reg_reg_reg(A_MUL,hreg1,tmpreg,left.location.register64.reghi));
  340. end;
  341. end
  342. else
  343. begin
  344. hlcg.location_force_reg(list,right.location,right.resultdef,right.resultdef,true);
  345. tmpreg:=right.location.register64.reglo;
  346. hreg1:=cg.getintregister(list,OS_INT);
  347. hreg2:=cg.getintregister(list,OS_INT);
  348. list.concat(taicpu.op_reg_reg_reg(A_MUL,hreg1,right.location.register64.reglo,left.location.register64.reghi));
  349. list.concat(taicpu.op_reg_reg_reg(A_MUL,hreg2,right.location.register64.reghi,left.location.register64.reglo));
  350. end;
  351. { At this point, tmpreg is either lo(right) or NR_NO if lo(left)*lo(right) is zero }
  352. if (tmpreg=NR_NO) then
  353. begin
  354. if (hreg2<>NR_NO) and (hreg1<>NR_NO) then
  355. begin
  356. location.register64.reghi:=cg.getintregister(list,OS_INT);
  357. list.concat(taicpu.op_reg_reg_reg(A_ADDU,location.register64.reghi,hreg1,hreg2));
  358. end
  359. else if (hreg2<>NR_NO) then
  360. location.register64.reghi:=hreg2
  361. else if (hreg1<>NR_NO) then
  362. location.register64.reghi:=hreg1
  363. else
  364. InternalError(2014122701);
  365. location.register64.reglo:=NR_R0;
  366. end
  367. else
  368. begin
  369. list.concat(taicpu.op_reg_reg(A_MULTU,left.location.register64.reglo,tmpreg));
  370. location.register64.reghi:=cg.getintregister(list,OS_INT);
  371. location.register64.reglo:=cg.getintregister(list,OS_INT);
  372. current_asmdata.CurrAsmList.Concat(taicpu.op_reg(A_MFLO,location.register64.reglo));
  373. current_asmdata.CurrAsmList.Concat(taicpu.op_reg(A_MFHI,location.register64.reghi));
  374. if (hreg2<>NR_NO) then
  375. list.concat(taicpu.op_reg_reg_reg(A_ADDU,location.register64.reghi,location.register64.reghi,hreg2));
  376. if (hreg1<>NR_NO) then
  377. list.concat(taicpu.op_reg_reg_reg(A_ADDU,location.register64.reghi,location.register64.reghi,hreg1));
  378. end;
  379. end;
  380. function tmipsaddnode.use_generic_mul32to64: boolean;
  381. begin
  382. result:=false;
  383. end;
  384. function tmipsaddnode.use_generic_mul64bit: boolean;
  385. begin
  386. result:=needoverflowcheck or
  387. (not (CPUMIPS_HAS_ISA32R2 in cpu_capabilities[current_settings.cputype]));
  388. end;
  389. begin
  390. caddnode := tmipsaddnode;
  391. end.