ncpuadd.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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, True);
  190. location_reset(location, LOC_FPUREGISTER, def_cgsize(resultdef));
  191. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  192. case nodetype of
  193. addn:
  194. begin
  195. if location.size = OS_F64 then
  196. op := A_ADD_D
  197. else
  198. op := A_ADD_S;
  199. end;
  200. muln:
  201. begin
  202. if location.size = OS_F64 then
  203. op := A_MUL_D
  204. else
  205. op := A_MUL_S;
  206. end;
  207. subn:
  208. begin
  209. if location.size = OS_F64 then
  210. op := A_SUB_D
  211. else
  212. op := A_SUB_S;
  213. end;
  214. slashn:
  215. begin
  216. if location.size = OS_F64 then
  217. op := A_DIV_D
  218. else
  219. op := A_DIV_S;
  220. end;
  221. else
  222. internalerror(200306014);
  223. end;
  224. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,
  225. location.Register, left.location.Register, right.location.Register));
  226. end;
  227. const
  228. ops_cmpfloat: array[boolean,ltn..unequaln] of TAsmOp = (
  229. // ltn lten gtn gten equaln unequaln
  230. (A_C_LT_S, A_C_LE_S, A_C_LT_S, A_C_LE_S, A_C_EQ_S, A_C_EQ_S),
  231. (A_C_LT_D, A_C_LE_D, A_C_LT_D, A_C_LE_D, A_C_EQ_D, A_C_EQ_D)
  232. );
  233. procedure tmipsaddnode.second_cmpfloat;
  234. var
  235. op: tasmop;
  236. lreg,rreg: tregister;
  237. ai: Taicpu;
  238. begin
  239. pass_left_right;
  240. if nf_swapped in flags then
  241. swapleftright;
  242. location_force_fpureg(current_asmdata.CurrAsmList, left.location, True);
  243. location_force_fpureg(current_asmdata.CurrAsmList, right.location, True);
  244. location_reset(location, LOC_JUMP, OS_NO);
  245. op:=ops_cmpfloat[left.location.size=OS_F64,nodetype];
  246. if (nodetype in [gtn,gten]) then
  247. begin
  248. lreg:=right.location.register;
  249. rreg:=left.location.register;
  250. end
  251. else
  252. begin
  253. lreg:=left.location.register;
  254. rreg:=right.location.register;
  255. end;
  256. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,lreg,rreg));
  257. ai:=taicpu.op_sym(A_BC,current_procinfo.CurrTrueLabel);
  258. if (nodetype=unequaln) then
  259. ai.SetCondition(C_COP1FALSE)
  260. else
  261. ai.SetCondition(C_COP1TRUE);
  262. current_asmdata.CurrAsmList.concat(ai);
  263. current_asmdata.CurrAsmList.concat(TAiCpu.Op_none(A_NOP));
  264. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  265. end;
  266. procedure tmipsaddnode.second_cmpboolean;
  267. begin
  268. second_generic_cmp32(true);
  269. end;
  270. procedure tmipsaddnode.second_cmpsmallset;
  271. begin
  272. second_generic_cmp32(true);
  273. end;
  274. procedure tmipsaddnode.second_cmpordinal;
  275. var
  276. unsigned: boolean;
  277. begin
  278. unsigned := not (is_signed(left.resultdef)) or not (is_signed(right.resultdef));
  279. second_generic_cmp32(unsigned);
  280. end;
  281. const
  282. multops: array[boolean] of TAsmOp = (A_MULT, A_MULTU);
  283. procedure tmipsaddnode.second_addordinal;
  284. var
  285. unsigned: boolean;
  286. begin
  287. unsigned:=not(is_signed(left.resultdef)) or
  288. not(is_signed(right.resultdef));
  289. if (nodetype=muln) and is_64bit(resultdef) then
  290. begin
  291. pass_left_right;
  292. force_reg_left_right(true,false);
  293. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  294. location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  295. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  296. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg(multops[unsigned],left.location.register,right.location.register));
  297. current_asmdata.CurrAsmList.Concat(taicpu.op_reg(A_MFLO,location.register64.reglo));
  298. current_asmdata.CurrAsmList.Concat(taicpu.op_reg(A_MFHI,location.register64.reghi));
  299. end
  300. else
  301. inherited second_addordinal;
  302. end;
  303. function tmipsaddnode.use_generic_mul32to64: boolean;
  304. begin
  305. result:=false;
  306. end;
  307. begin
  308. caddnode := tmipsaddnode;
  309. end.