2
0

ncpuadd.pas 11 KB

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