ncpuadd.pas 13 KB

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