ncpuadd.pas 14 KB

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