ncpuadd.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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..unequaln] of tnodetype =
  59. //lt lte gt gte
  60. (gtn, gten,ltn,lten, equaln, unequaln);
  61. nodetype2opcmp: array[boolean,ltn..unequaln] of TOpCmp = (
  62. (OC_LT, OC_LTE, OC_GT, OC_GTE, OC_EQ, OC_NE),
  63. (OC_B, OC_BE, OC_A, OC_AE, OC_EQ, OC_NE)
  64. );
  65. procedure tmipsaddnode.second_generic_cmp32(unsigned: boolean);
  66. var
  67. ntype: tnodetype;
  68. begin
  69. pass_left_right;
  70. force_reg_left_right(True, True);
  71. location_reset(location,LOC_FLAGS,OS_NO);
  72. ntype:=nodetype;
  73. if nf_swapped in flags then
  74. ntype:=swapped_nodetype[nodetype];
  75. location.resflags.cond:=nodetype2opcmp[unsigned,ntype];
  76. location.resflags.reg1:=left.location.register;
  77. location.resflags.use_const:=(right.location.loc=LOC_CONSTANT);
  78. if location.resflags.use_const then
  79. location.resflags.value:=right.location.value
  80. else
  81. location.resflags.reg2:=right.location.register;
  82. end;
  83. const
  84. cmpops: array[boolean] of TOpCmp = (OC_LT,OC_B);
  85. procedure tmipsaddnode.cmp64_lt(left_reg, right_reg: TRegister64;unsigned: boolean);
  86. begin
  87. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,cmpops[unsigned],right_reg.reghi,left_reg.reghi,current_procinfo.CurrTrueLabel);
  88. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left_reg.reghi,right_reg.reghi,current_procinfo.CurrFalseLabel);
  89. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_B,right_reg.reglo,left_reg.reglo,current_procinfo.CurrTrueLabel);
  90. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  91. end;
  92. procedure tmipsaddnode.cmp64_le(left_reg, right_reg: TRegister64;unsigned: boolean);
  93. begin
  94. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,cmpops[unsigned],left_reg.reghi,right_reg.reghi,current_procinfo.CurrFalseLabel);
  95. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left_reg.reghi,right_reg.reghi,current_procinfo.CurrTrueLabel);
  96. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_B,left_reg.reglo,right_reg.reglo,current_procinfo.CurrFalseLabel);
  97. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  98. end;
  99. procedure tmipsaddnode.second_cmp64bit;
  100. var
  101. unsigned: boolean;
  102. left_reg,right_reg: TRegister64;
  103. begin
  104. location_reset(location, LOC_JUMP, OS_NO);
  105. pass_left_right;
  106. force_reg_left_right(true,true);
  107. unsigned:=not(is_signed(left.resultdef)) or
  108. not(is_signed(right.resultdef));
  109. left_reg:=left.location.register64;
  110. if (right.location.loc=LOC_CONSTANT) then
  111. begin
  112. if lo(right.location.value64)=0 then
  113. right_reg.reglo:=NR_R0
  114. else
  115. begin
  116. right_reg.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  117. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,lo(right.location.value64),right_reg.reglo);
  118. end;
  119. if hi(right.location.value64)=0 then
  120. right_reg.reghi:=NR_R0
  121. else
  122. begin
  123. right_reg.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  124. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,hi(right.location.value64),right_reg.reghi);
  125. end;
  126. end
  127. else
  128. right_reg:=right.location.register64;
  129. case NodeType of
  130. equaln:
  131. begin
  132. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left_reg.reghi,right_reg.reghi,current_procinfo.CurrFalseLabel);
  133. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left_reg.reglo,right_reg.reglo,current_procinfo.CurrFalseLabel);
  134. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  135. end;
  136. unequaln:
  137. begin
  138. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left_reg.reghi,right_reg.reghi,current_procinfo.CurrTrueLabel);
  139. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left_reg.reglo,right_reg.reglo,current_procinfo.CurrTrueLabel);
  140. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  141. end;
  142. else
  143. if nf_swapped in flags then
  144. case NodeType of
  145. ltn:
  146. cmp64_lt(right_reg, left_reg,unsigned);
  147. lten:
  148. cmp64_le(right_reg, left_reg,unsigned);
  149. gtn:
  150. cmp64_lt(left_reg, right_reg,unsigned);
  151. gten:
  152. cmp64_le(left_reg, right_reg,unsigned);
  153. end
  154. else
  155. case NodeType of
  156. ltn:
  157. cmp64_lt(left_reg, right_reg,unsigned);
  158. lten:
  159. cmp64_le(left_reg, right_reg,unsigned);
  160. gtn:
  161. cmp64_lt(right_reg, left_reg,unsigned);
  162. gten:
  163. cmp64_le(right_reg, left_reg,unsigned);
  164. end;
  165. end;
  166. end;
  167. function tmipsaddnode.pass_1 : tnode;
  168. begin
  169. result:=inherited pass_1;
  170. if not(assigned(result)) then
  171. begin
  172. if (nodetype in [ltn,lten,gtn,gten,equaln,unequaln]) then
  173. begin
  174. if (left.resultdef.typ=floatdef) or (right.resultdef.typ=floatdef) then
  175. expectloc:=LOC_JUMP;
  176. end;
  177. end;
  178. end;
  179. procedure tmipsaddnode.second_addfloat;
  180. var
  181. op: TAsmOp;
  182. begin
  183. pass_left_right;
  184. if (nf_swapped in flags) then
  185. swapleftright;
  186. { force fpureg as location, left right doesn't matter
  187. as both will be in a fpureg }
  188. location_force_fpureg(current_asmdata.CurrAsmList, left.location, True);
  189. location_force_fpureg(current_asmdata.CurrAsmList, right.location, (left.location.loc <> LOC_CFPUREGISTER));
  190. location_reset(location, LOC_FPUREGISTER, def_cgsize(resultdef));
  191. if left.location.loc <> LOC_CFPUREGISTER then
  192. location.Register := left.location.Register
  193. else
  194. location.Register := right.location.Register;
  195. case nodetype of
  196. addn:
  197. begin
  198. if location.size = OS_F64 then
  199. op := A_ADD_D
  200. else
  201. op := A_ADD_S;
  202. end;
  203. muln:
  204. begin
  205. if location.size = OS_F64 then
  206. op := A_MUL_D
  207. else
  208. op := A_MUL_S;
  209. end;
  210. subn:
  211. begin
  212. if location.size = OS_F64 then
  213. op := A_SUB_D
  214. else
  215. op := A_SUB_S;
  216. end;
  217. slashn:
  218. begin
  219. if location.size = OS_F64 then
  220. op := A_DIV_D
  221. else
  222. op := A_DIV_S;
  223. end;
  224. else
  225. internalerror(200306014);
  226. end;
  227. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,
  228. location.Register, left.location.Register, right.location.Register));
  229. end;
  230. const
  231. ops_cmpfloat: array[boolean,ltn..unequaln] of TAsmOp = (
  232. // ltn lten gtn gten equaln unequaln
  233. (A_C_LT_S, A_C_LE_S, A_C_LT_S, A_C_LE_S, A_C_EQ_S, A_C_EQ_S),
  234. (A_C_LT_D, A_C_LE_D, A_C_LT_D, A_C_LE_D, A_C_EQ_D, A_C_EQ_D)
  235. );
  236. procedure tmipsaddnode.second_cmpfloat;
  237. var
  238. op: tasmop;
  239. lreg,rreg: tregister;
  240. ai: Taicpu;
  241. begin
  242. pass_left_right;
  243. if nf_swapped in flags then
  244. swapleftright;
  245. location_force_fpureg(current_asmdata.CurrAsmList, left.location, True);
  246. location_force_fpureg(current_asmdata.CurrAsmList, right.location, True);
  247. location_reset(location, LOC_JUMP, OS_NO);
  248. op:=ops_cmpfloat[left.location.size=OS_F64,nodetype];
  249. if (nodetype in [gtn,gten]) then
  250. begin
  251. lreg:=right.location.register;
  252. rreg:=left.location.register;
  253. end
  254. else
  255. begin
  256. lreg:=left.location.register;
  257. rreg:=right.location.register;
  258. end;
  259. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,lreg,rreg));
  260. ai:=taicpu.op_sym(A_BC,current_procinfo.CurrTrueLabel);
  261. if (nodetype=unequaln) then
  262. ai.SetCondition(C_COP1FALSE)
  263. else
  264. ai.SetCondition(C_COP1TRUE);
  265. current_asmdata.CurrAsmList.concat(ai);
  266. current_asmdata.CurrAsmList.concat(TAiCpu.Op_none(A_NOP));
  267. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  268. end;
  269. procedure tmipsaddnode.second_cmpboolean;
  270. begin
  271. second_generic_cmp32(true);
  272. end;
  273. procedure tmipsaddnode.second_cmpsmallset;
  274. begin
  275. second_generic_cmp32(true);
  276. end;
  277. procedure tmipsaddnode.second_cmpordinal;
  278. var
  279. unsigned: boolean;
  280. begin
  281. unsigned := not (is_signed(left.resultdef)) or not (is_signed(right.resultdef));
  282. second_generic_cmp32(unsigned);
  283. end;
  284. const
  285. multops: array[boolean] of TAsmOp = (A_MULT, A_MULTU);
  286. procedure tmipsaddnode.second_addordinal;
  287. var
  288. unsigned: boolean;
  289. begin
  290. unsigned:=not(is_signed(left.resultdef)) or
  291. not(is_signed(right.resultdef));
  292. if (nodetype=muln) and is_64bit(resultdef) then
  293. begin
  294. pass_left_right;
  295. force_reg_left_right(true,false);
  296. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  297. location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  298. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  299. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg(multops[unsigned],left.location.register,right.location.register));
  300. current_asmdata.CurrAsmList.Concat(taicpu.op_reg(A_MFLO,location.register64.reglo));
  301. current_asmdata.CurrAsmList.Concat(taicpu.op_reg(A_MFHI,location.register64.reghi));
  302. end
  303. else
  304. inherited second_addordinal;
  305. end;
  306. function tmipsaddnode.use_generic_mul32to64: boolean;
  307. begin
  308. result:=false;
  309. end;
  310. begin
  311. caddnode := tmipsaddnode;
  312. end.