ncpuadd.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. procedure tmipsaddnode.cmp64_lt(left_reg, right_reg: TRegister64;unsigned: boolean);
  138. var
  139. hreg: tregister;
  140. begin
  141. hreg:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_INT);
  142. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(ops[unsigned], hreg, left_reg.reghi, right_reg.reghi));
  143. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,NR_R0,hreg,current_procinfo.CurrTrueLabel);
  144. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left_reg.reghi,right_reg.reghi,current_procinfo.CurrFalseLabel);
  145. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SLTU, hreg, left_reg.reglo, right_reg.reglo));
  146. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,NR_R0,hreg,current_procinfo.CurrTrueLabel);
  147. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  148. end;
  149. procedure tmipsaddnode.cmp64_le(left_reg, right_reg: TRegister64;unsigned: boolean);
  150. var
  151. hreg: TRegister;
  152. begin
  153. hreg:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_INT);
  154. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(ops[unsigned], hreg, right_reg.reghi, left_reg.reghi));
  155. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,NR_R0,hreg,current_procinfo.CurrFalseLabel);
  156. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left_reg.reghi,right_reg.reghi,current_procinfo.CurrTrueLabel);
  157. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SLTU, hreg, right_reg.reglo, left_reg.reglo));
  158. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,NR_R0,hreg,current_procinfo.CurrFalseLabel);
  159. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  160. end;
  161. procedure tmipsaddnode.second_cmp64bit;
  162. var
  163. unsigned: boolean;
  164. left_reg,right_reg: TRegister64;
  165. begin
  166. location_reset(location, LOC_JUMP, OS_NO);
  167. pass_left_right;
  168. force_reg_left_right(true,true);
  169. unsigned:=not(is_signed(left.resultdef)) or
  170. not(is_signed(right.resultdef));
  171. left_reg:=left.location.register64;
  172. if (right.location.loc=LOC_CONSTANT) then
  173. begin
  174. if lo(right.location.value64)=0 then
  175. right_reg.reglo:=NR_R0
  176. else
  177. begin
  178. right_reg.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  179. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,lo(right.location.value64),right_reg.reglo);
  180. end;
  181. if hi(right.location.value64)=0 then
  182. right_reg.reghi:=NR_R0
  183. else
  184. begin
  185. right_reg.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  186. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,hi(right.location.value64),right_reg.reghi);
  187. end;
  188. end
  189. else
  190. right_reg:=right.location.register64;
  191. case NodeType of
  192. equaln:
  193. begin
  194. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left_reg.reghi,right_reg.reghi,current_procinfo.CurrFalseLabel);
  195. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left_reg.reglo,right_reg.reglo,current_procinfo.CurrFalseLabel);
  196. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  197. end;
  198. unequaln:
  199. begin
  200. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left_reg.reghi,right_reg.reghi,current_procinfo.CurrTrueLabel);
  201. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left_reg.reglo,right_reg.reglo,current_procinfo.CurrTrueLabel);
  202. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  203. end;
  204. else
  205. if nf_swapped in flags then
  206. case NodeType of
  207. ltn:
  208. cmp64_lt(right_reg, left_reg,unsigned);
  209. lten:
  210. cmp64_le(right_reg, left_reg,unsigned);
  211. gtn:
  212. cmp64_lt(left_reg, right_reg,unsigned);
  213. gten:
  214. cmp64_le(left_reg, right_reg,unsigned);
  215. end
  216. else
  217. case NodeType of
  218. ltn:
  219. cmp64_lt(left_reg, right_reg,unsigned);
  220. lten:
  221. cmp64_le(left_reg, right_reg,unsigned);
  222. gtn:
  223. cmp64_lt(right_reg, left_reg,unsigned);
  224. gten:
  225. cmp64_le(right_reg, left_reg,unsigned);
  226. end;
  227. end;
  228. end;
  229. function tmipsaddnode.pass_1 : tnode;
  230. begin
  231. result:=inherited pass_1;
  232. if not(assigned(result)) then
  233. begin
  234. if (nodetype in [ltn,lten,gtn,gten,equaln,unequaln]) then
  235. begin
  236. if (left.resultdef.typ=floatdef) or (right.resultdef.typ=floatdef) then
  237. expectloc:=LOC_JUMP
  238. else if ((left.resultdef.typ<>orddef) or
  239. (not (torddef(left.resultdef).ordtype in [s64bit,u64bit,scurrency]))) then
  240. expectloc:=LOC_REGISTER;
  241. end;
  242. end;
  243. end;
  244. procedure tmipsaddnode.second_addfloat;
  245. var
  246. op: TAsmOp;
  247. begin
  248. pass_left_right;
  249. if (nf_swapped in flags) then
  250. swapleftright;
  251. { force fpureg as location, left right doesn't matter
  252. as both will be in a fpureg }
  253. location_force_fpureg(current_asmdata.CurrAsmList, left.location, True);
  254. location_force_fpureg(current_asmdata.CurrAsmList, right.location, (left.location.loc <> LOC_CFPUREGISTER));
  255. location_reset(location, LOC_FPUREGISTER, def_cgsize(resultdef));
  256. if left.location.loc <> LOC_CFPUREGISTER then
  257. location.Register := left.location.Register
  258. else
  259. location.Register := right.location.Register;
  260. case nodetype of
  261. addn:
  262. begin
  263. if location.size = OS_F64 then
  264. op := A_ADD_D
  265. else
  266. op := A_ADD_S;
  267. end;
  268. muln:
  269. begin
  270. if location.size = OS_F64 then
  271. op := A_MUL_D
  272. else
  273. op := A_MUL_S;
  274. end;
  275. subn:
  276. begin
  277. if location.size = OS_F64 then
  278. op := A_SUB_D
  279. else
  280. op := A_SUB_S;
  281. end;
  282. slashn:
  283. begin
  284. if location.size = OS_F64 then
  285. op := A_DIV_D
  286. else
  287. op := A_DIV_S;
  288. end;
  289. else
  290. internalerror(200306014);
  291. end;
  292. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,
  293. location.Register, left.location.Register, right.location.Register));
  294. end;
  295. const
  296. ops_cmpfloat: array[boolean,ltn..unequaln] of TAsmOp = (
  297. // ltn lten gtn gten equaln unequaln
  298. (A_C_LT_S, A_C_LE_S, A_C_LT_S, A_C_LE_S, A_C_EQ_S, A_C_EQ_S),
  299. (A_C_LT_D, A_C_LE_D, A_C_LT_D, A_C_LE_D, A_C_EQ_D, A_C_EQ_D)
  300. );
  301. procedure tmipsaddnode.second_cmpfloat;
  302. var
  303. op,op2: tasmop;
  304. lreg,rreg: tregister;
  305. begin
  306. pass_left_right;
  307. if nf_swapped in flags then
  308. swapleftright;
  309. location_force_fpureg(current_asmdata.CurrAsmList, left.location, True);
  310. location_force_fpureg(current_asmdata.CurrAsmList, right.location, True);
  311. location_reset(location, LOC_JUMP, OS_NO);
  312. op:=ops_cmpfloat[left.location.size=OS_F64,nodetype];
  313. if (nodetype=unequaln) then
  314. op2:=A_BC1F
  315. else
  316. op2:=A_BC1T;
  317. if (nodetype in [gtn,gten]) then
  318. begin
  319. lreg:=right.location.register;
  320. rreg:=left.location.register;
  321. end
  322. else
  323. begin
  324. lreg:=left.location.register;
  325. rreg:=right.location.register;
  326. end;
  327. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,lreg,rreg));
  328. current_asmdata.CurrAsmList.concat(Taicpu.op_sym(op2,current_procinfo.CurrTrueLabel));
  329. current_asmdata.CurrAsmList.concat(TAiCpu.Op_none(A_NOP));
  330. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  331. end;
  332. procedure tmipsaddnode.second_cmpboolean;
  333. begin
  334. second_generic_cmp32(true);
  335. end;
  336. procedure tmipsaddnode.second_cmpsmallset;
  337. begin
  338. second_generic_cmp32(true);
  339. end;
  340. procedure tmipsaddnode.second_cmpordinal;
  341. var
  342. unsigned: boolean;
  343. begin
  344. unsigned := not (is_signed(left.resultdef)) or not (is_signed(right.resultdef));
  345. second_generic_cmp32(unsigned);
  346. end;
  347. const
  348. multops: array[boolean] of TAsmOp = (A_MULT, A_MULTU);
  349. procedure tmipsaddnode.second_addordinal;
  350. var
  351. unsigned: boolean;
  352. begin
  353. unsigned:=not(is_signed(left.resultdef)) or
  354. not(is_signed(right.resultdef));
  355. if (nodetype=muln) and is_64bit(resultdef) then
  356. begin
  357. pass_left_right;
  358. force_reg_left_right(true,false);
  359. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  360. location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  361. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  362. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg(multops[unsigned],left.location.register,right.location.register));
  363. current_asmdata.CurrAsmList.Concat(taicpu.op_reg(A_MFLO,location.register64.reglo));
  364. current_asmdata.CurrAsmList.Concat(taicpu.op_reg(A_MFHI,location.register64.reghi));
  365. end
  366. else
  367. inherited second_addordinal;
  368. end;
  369. function tmipsaddnode.use_generic_mul32to64: boolean;
  370. begin
  371. result:=false;
  372. end;
  373. begin
  374. caddnode := tmipsaddnode;
  375. end.