ncpuadd.pas 14 KB

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