ncpuadd.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. protected
  30. procedure second_addfloat; override;
  31. procedure second_cmpfloat; override;
  32. procedure second_cmpboolean; override;
  33. procedure second_cmpsmallset; override;
  34. procedure second_cmp64bit; override;
  35. procedure second_cmpordinal; override;
  36. procedure second_addordinal; override;
  37. public
  38. function pass_1: tnode; override;
  39. function use_generic_mul32to64: boolean; override;
  40. end;
  41. implementation
  42. uses
  43. systems,
  44. cutils, verbose,
  45. paramgr,
  46. aasmtai, aasmcpu, aasmdata,
  47. defutil,
  48. {cgbase,} cgcpu, cgutils,
  49. cpupara,
  50. procinfo,
  51. symconst,symdef,
  52. ncon, nset, nadd,
  53. ncgutil, cgobj;
  54. {*****************************************************************************
  55. tmipsaddnode
  56. *****************************************************************************}
  57. const
  58. swapped_nodetype: array[ltn..gten] of tnodetype =
  59. //lt lte gt gte
  60. (gtn, gten,ltn,lten);
  61. ops: array[boolean] of tasmop = (A_SLT,A_SLTU);
  62. ops_immed: array[boolean] of tasmop = (A_SLTI,A_SLTIU);
  63. procedure tmipsaddnode.second_generic_cmp32(unsigned: boolean);
  64. var
  65. ntype: tnodetype;
  66. tmp_left,tmp_right: TRegister;
  67. begin
  68. pass_left_right;
  69. force_reg_left_right(True, True);
  70. location_reset(location,LOC_REGISTER,OS_INT);
  71. location.register:=cg.GetIntRegister(current_asmdata.CurrAsmList, OS_INT);
  72. if nodetype in [equaln,unequaln] then
  73. begin
  74. tmp_left:=location.register;
  75. { XORI needs unsigned immediate in range 0-65535 }
  76. if (right.location.loc=LOC_CONSTANT) and (right.location.value>=0) and
  77. (right.location.value<=65535) then
  78. begin
  79. if right.location.value<>0 then
  80. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_const(A_XORI,location.register,left.location.register,right.location.value))
  81. else
  82. tmp_left:=left.location.register;
  83. end
  84. else
  85. begin
  86. if (right.location.loc<>LOC_CONSTANT) then
  87. tmp_right:=right.location.register
  88. else
  89. begin
  90. tmp_right:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_INT);
  91. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,right.location.value,tmp_right);
  92. end;
  93. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_XOR,location.register,left.location.register,tmp_right));
  94. end;
  95. if nodetype=equaln then
  96. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_const(A_SLTIU,location.register,tmp_left,1))
  97. else
  98. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SLTU,location.register,NR_R0,tmp_left));
  99. exit;
  100. end;
  101. ntype:=nodetype;
  102. if nf_swapped in flags then
  103. ntype:=swapped_nodetype[nodetype];
  104. {
  105. sle x,a,b --> slt x,b,a; xori x,x,1 immediate not possible (or must be at left)
  106. sgt x,a,b --> slt x,b,a likewise
  107. sge x,a,b --> slt x,a,b; xori x,x,1
  108. slt x,a,b --> unchanged
  109. }
  110. if (ntype in [gten,ltn]) and
  111. (right.location.loc=LOC_CONSTANT) and
  112. (right.location.value>=simm16lo) and
  113. (right.location.value<=simm16hi) then
  114. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_const(ops_immed[unsigned],location.register,left.location.register,right.location.value))
  115. else
  116. begin
  117. if (right.location.loc=LOC_CONSTANT) then
  118. begin
  119. if (right.location.value=0) then
  120. tmp_right:=NR_R0
  121. else
  122. begin
  123. tmp_right:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_INT);
  124. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,right.location.value,tmp_right);
  125. end;
  126. end
  127. else
  128. tmp_right:=right.location.register;
  129. if (ntype in [lten,gtn]) then
  130. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(ops[unsigned],location.register,tmp_right,left.location.register))
  131. else
  132. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(ops[unsigned],location.register,left.location.register,tmp_right));
  133. end;
  134. if (ntype in [lten,gten]) then
  135. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_const(A_XORI,location.register,location.register,1));
  136. end;
  137. const
  138. cmpops: array[boolean] of TOpCmp = (OC_LT,OC_B);
  139. procedure tmipsaddnode.cmp64_lt(left_reg, right_reg: TRegister64;unsigned: boolean);
  140. begin
  141. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,cmpops[unsigned],right_reg.reghi,left_reg.reghi,current_procinfo.CurrTrueLabel);
  142. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left_reg.reghi,right_reg.reghi,current_procinfo.CurrFalseLabel);
  143. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_B,right_reg.reglo,left_reg.reglo,current_procinfo.CurrTrueLabel);
  144. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  145. end;
  146. procedure tmipsaddnode.cmp64_le(left_reg, right_reg: TRegister64;unsigned: boolean);
  147. begin
  148. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,cmpops[unsigned],left_reg.reghi,right_reg.reghi,current_procinfo.CurrFalseLabel);
  149. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left_reg.reghi,right_reg.reghi,current_procinfo.CurrTrueLabel);
  150. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_B,left_reg.reglo,right_reg.reglo,current_procinfo.CurrFalseLabel);
  151. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  152. end;
  153. procedure tmipsaddnode.second_cmp64bit;
  154. var
  155. unsigned: boolean;
  156. left_reg,right_reg: TRegister64;
  157. begin
  158. location_reset(location, LOC_JUMP, OS_NO);
  159. pass_left_right;
  160. force_reg_left_right(true,true);
  161. unsigned:=not(is_signed(left.resultdef)) or
  162. not(is_signed(right.resultdef));
  163. left_reg:=left.location.register64;
  164. if (right.location.loc=LOC_CONSTANT) then
  165. begin
  166. if lo(right.location.value64)=0 then
  167. right_reg.reglo:=NR_R0
  168. else
  169. begin
  170. right_reg.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  171. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,lo(right.location.value64),right_reg.reglo);
  172. end;
  173. if hi(right.location.value64)=0 then
  174. right_reg.reghi:=NR_R0
  175. else
  176. begin
  177. right_reg.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  178. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,hi(right.location.value64),right_reg.reghi);
  179. end;
  180. end
  181. else
  182. right_reg:=right.location.register64;
  183. case NodeType of
  184. equaln:
  185. begin
  186. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left_reg.reghi,right_reg.reghi,current_procinfo.CurrFalseLabel);
  187. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left_reg.reglo,right_reg.reglo,current_procinfo.CurrFalseLabel);
  188. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  189. end;
  190. unequaln:
  191. begin
  192. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left_reg.reghi,right_reg.reghi,current_procinfo.CurrTrueLabel);
  193. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left_reg.reglo,right_reg.reglo,current_procinfo.CurrTrueLabel);
  194. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  195. end;
  196. else
  197. if nf_swapped in flags then
  198. case NodeType of
  199. ltn:
  200. cmp64_lt(right_reg, left_reg,unsigned);
  201. lten:
  202. cmp64_le(right_reg, left_reg,unsigned);
  203. gtn:
  204. cmp64_lt(left_reg, right_reg,unsigned);
  205. gten:
  206. cmp64_le(left_reg, right_reg,unsigned);
  207. end
  208. else
  209. case NodeType of
  210. ltn:
  211. cmp64_lt(left_reg, right_reg,unsigned);
  212. lten:
  213. cmp64_le(left_reg, right_reg,unsigned);
  214. gtn:
  215. cmp64_lt(right_reg, left_reg,unsigned);
  216. gten:
  217. cmp64_le(right_reg, left_reg,unsigned);
  218. end;
  219. end;
  220. end;
  221. function tmipsaddnode.pass_1 : tnode;
  222. begin
  223. result:=inherited pass_1;
  224. if not(assigned(result)) then
  225. begin
  226. if (nodetype in [ltn,lten,gtn,gten,equaln,unequaln]) then
  227. begin
  228. if (left.resultdef.typ=floatdef) or (right.resultdef.typ=floatdef) then
  229. expectloc:=LOC_JUMP
  230. else if ((left.resultdef.typ<>orddef) or
  231. (not (torddef(left.resultdef).ordtype in [s64bit,u64bit,scurrency]))) then
  232. expectloc:=LOC_REGISTER;
  233. end;
  234. end;
  235. end;
  236. procedure tmipsaddnode.second_addfloat;
  237. var
  238. op: TAsmOp;
  239. begin
  240. pass_left_right;
  241. if (nf_swapped in flags) then
  242. swapleftright;
  243. { force fpureg as location, left right doesn't matter
  244. as both will be in a fpureg }
  245. location_force_fpureg(current_asmdata.CurrAsmList, left.location, True);
  246. location_force_fpureg(current_asmdata.CurrAsmList, right.location, (left.location.loc <> LOC_CFPUREGISTER));
  247. location_reset(location, LOC_FPUREGISTER, def_cgsize(resultdef));
  248. if left.location.loc <> LOC_CFPUREGISTER then
  249. location.Register := left.location.Register
  250. else
  251. location.Register := right.location.Register;
  252. case nodetype of
  253. addn:
  254. begin
  255. if location.size = OS_F64 then
  256. op := A_ADD_D
  257. else
  258. op := A_ADD_S;
  259. end;
  260. muln:
  261. begin
  262. if location.size = OS_F64 then
  263. op := A_MUL_D
  264. else
  265. op := A_MUL_S;
  266. end;
  267. subn:
  268. begin
  269. if location.size = OS_F64 then
  270. op := A_SUB_D
  271. else
  272. op := A_SUB_S;
  273. end;
  274. slashn:
  275. begin
  276. if location.size = OS_F64 then
  277. op := A_DIV_D
  278. else
  279. op := A_DIV_S;
  280. end;
  281. else
  282. internalerror(200306014);
  283. end;
  284. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,
  285. location.Register, left.location.Register, right.location.Register));
  286. end;
  287. const
  288. ops_cmpfloat: array[boolean,ltn..unequaln] of TAsmOp = (
  289. // ltn lten gtn gten equaln unequaln
  290. (A_C_LT_S, A_C_LE_S, A_C_LT_S, A_C_LE_S, A_C_EQ_S, A_C_EQ_S),
  291. (A_C_LT_D, A_C_LE_D, A_C_LT_D, A_C_LE_D, A_C_EQ_D, A_C_EQ_D)
  292. );
  293. procedure tmipsaddnode.second_cmpfloat;
  294. var
  295. op: tasmop;
  296. lreg,rreg: tregister;
  297. ai: Taicpu;
  298. begin
  299. pass_left_right;
  300. if nf_swapped in flags then
  301. swapleftright;
  302. location_force_fpureg(current_asmdata.CurrAsmList, left.location, True);
  303. location_force_fpureg(current_asmdata.CurrAsmList, right.location, True);
  304. location_reset(location, LOC_JUMP, OS_NO);
  305. op:=ops_cmpfloat[left.location.size=OS_F64,nodetype];
  306. if (nodetype in [gtn,gten]) then
  307. begin
  308. lreg:=right.location.register;
  309. rreg:=left.location.register;
  310. end
  311. else
  312. begin
  313. lreg:=left.location.register;
  314. rreg:=right.location.register;
  315. end;
  316. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,lreg,rreg));
  317. ai:=taicpu.op_sym(A_BC,current_procinfo.CurrTrueLabel);
  318. if (nodetype=unequaln) then
  319. ai.SetCondition(C_COP1FALSE)
  320. else
  321. ai.SetCondition(C_COP1TRUE);
  322. current_asmdata.CurrAsmList.concat(ai);
  323. current_asmdata.CurrAsmList.concat(TAiCpu.Op_none(A_NOP));
  324. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  325. end;
  326. procedure tmipsaddnode.second_cmpboolean;
  327. begin
  328. second_generic_cmp32(true);
  329. end;
  330. procedure tmipsaddnode.second_cmpsmallset;
  331. begin
  332. second_generic_cmp32(true);
  333. end;
  334. procedure tmipsaddnode.second_cmpordinal;
  335. var
  336. unsigned: boolean;
  337. begin
  338. unsigned := not (is_signed(left.resultdef)) or not (is_signed(right.resultdef));
  339. second_generic_cmp32(unsigned);
  340. end;
  341. const
  342. multops: array[boolean] of TAsmOp = (A_MULT, A_MULTU);
  343. procedure tmipsaddnode.second_addordinal;
  344. var
  345. unsigned: boolean;
  346. begin
  347. unsigned:=not(is_signed(left.resultdef)) or
  348. not(is_signed(right.resultdef));
  349. if (nodetype=muln) and is_64bit(resultdef) then
  350. begin
  351. pass_left_right;
  352. force_reg_left_right(true,false);
  353. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  354. location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  355. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  356. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg(multops[unsigned],left.location.register,right.location.register));
  357. current_asmdata.CurrAsmList.Concat(taicpu.op_reg(A_MFLO,location.register64.reglo));
  358. current_asmdata.CurrAsmList.Concat(taicpu.op_reg(A_MFHI,location.register64.reghi));
  359. end
  360. else
  361. inherited second_addordinal;
  362. end;
  363. function tmipsaddnode.use_generic_mul32to64: boolean;
  364. begin
  365. result:=false;
  366. end;
  367. begin
  368. caddnode := tmipsaddnode;
  369. end.