ncpuadd.pas 10 KB

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