naddcpu.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. {******************************************************************************
  2. $Id$
  3. Copyright (c) 2000-2002 by Florian Klaempfl
  4. Code generation for add nodes on the i386
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; IF not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************}
  17. UNIT naddcpu;
  18. {$INCLUDE fpcdefs.inc}
  19. INTERFACE
  20. USES
  21. node,nadd,cpubase,cginfo;
  22. TYPE
  23. TSparcAddNode=CLASS(TAddNode)
  24. procedure pass_2;override;
  25. PRIVATE
  26. FUNCTION GetResFlags(unsigned:Boolean):TResFlags;
  27. procedure left_must_be_reg(OpSize:TOpSize;NoSwap:Boolean);
  28. procedure emit_generic_code(op:TAsmOp;OpSize:TOpSize;unsigned,extra_not,mboverflow:Boolean);
  29. procedure emit_op_right_left(op:TAsmOp);
  30. procedure pass_left_and_right;
  31. procedure set_result_location(cmpOp,unsigned:Boolean);
  32. end;
  33. implementation
  34. uses
  35. globtype,systems,
  36. cutils,verbose,globals,
  37. symconst,symdef,paramgr,
  38. aasmbase,aasmtai,aasmcpu,defutil,htypechk,
  39. cgbase,pass_2,regvars,
  40. cpupara,
  41. ncon,nset,
  42. cga,ncgutil,tgobj,rgobj,rgcpu,cgobj,cg64f32;
  43. const
  44. opsize_2_cgSize:array[S_B..S_L]of TCgSize=(OS_8,OS_16,OS_32);
  45. function TSparcAddNode.GetResFlags(unsigned:Boolean):TResFlags;
  46. begin
  47. case NodeType of
  48. equaln:
  49. GetResFlags:=F_E;
  50. unequaln:
  51. GetResFlags:=F_NE;
  52. else
  53. if not(unsigned)
  54. then
  55. if nf_swaped IN flags
  56. then
  57. case NodeType of
  58. ltn:
  59. GetResFlags:=F_G;
  60. lten:
  61. GetResFlags:=F_GE;
  62. gtn:
  63. GetResFlags:=F_L;
  64. gten:
  65. GetResFlags:=F_LE;
  66. end
  67. else
  68. case NodeType of
  69. ltn:
  70. GetResFlags:=F_L;
  71. lten:
  72. GetResFlags:=F_LE;
  73. gtn:
  74. GetResFlags:=F_G;
  75. gten:
  76. GetResFlags:=F_GE;
  77. end
  78. else
  79. if nf_swaped IN Flags
  80. then
  81. case NodeType of
  82. ltn:
  83. GetResFlags:=F_A;
  84. lten:
  85. GetResFlags:=F_AE;
  86. gtn:
  87. GetResFlags:=F_B;
  88. gten:
  89. GetResFlags:=F_BE;
  90. end
  91. else
  92. case NodeType of
  93. ltn:
  94. GetResFlags:=F_B;
  95. lten:
  96. GetResFlags:=F_BE;
  97. gtn:
  98. GetResFlags:=F_A;
  99. gten:
  100. GetResFlags:=F_AE;
  101. end;
  102. end;
  103. end;
  104. procedure TSparcAddNode.left_must_be_reg(OpSize:TOpSize;NoSwap:Boolean);
  105. begin
  106. if(left.location.loc=LOC_REGISTER)
  107. then
  108. exit;
  109. {left location is not a register}
  110. if(not NoSwap)and(right.location.loc=LOC_REGISTER)
  111. then{right is register so we can swap the locations}
  112. begin
  113. location_swap(left.location,right.location);
  114. toggleflag(nf_swaped);
  115. end
  116. else
  117. begin
  118. {maybe we can reuse a constant register when the operation is a comparison that
  119. doesn't change the value of the register}
  120. location_force_reg(exprasmlist,left.location,opsize_2_cgsize[opsize],(nodetype in [ltn,lten,gtn,gten,equaln,unequaln]));
  121. end;
  122. end;
  123. procedure TSparcAddNode.emit_generic_code(op:TAsmOp;OpSize:TOpSize;unsigned,extra_not,mboverflow:Boolean);
  124. VAR
  125. power:LongInt;
  126. hl4:TAsmLabel;
  127. begin
  128. { at this point, left.location.loc should be LOC_REGISTER }
  129. if right.location.loc=LOC_REGISTER
  130. then
  131. begin
  132. { right.location is a LOC_REGISTER }
  133. { when swapped another result register }
  134. if(nodetype=subn)and(nf_swaped in flags)
  135. then
  136. begin
  137. if extra_not
  138. then
  139. emit_reg(A_NOT,S_L,left.location.register);
  140. exprasmList.concat(Taicpu.Op_reg_reg_reg(Op,right.location.register,left.location.register,right.location.register));
  141. { newly swapped also set swapped flag }
  142. location_swap(left.location,right.location);
  143. toggleflag(nf_swaped);
  144. end
  145. else
  146. begin
  147. if extra_not
  148. then
  149. emit_reg(A_NOT,S_L,right.location.register);
  150. // emit_reg_reg(op,opsize,right.location.register,left.location.register);
  151. exprasmList.concat(Taicpu.Op_reg_reg_reg(Op,right.location.register,left.location.register,right.location.register));
  152. end;
  153. end
  154. ELSE
  155. begin
  156. { right.location is not a LOC_REGISTER }
  157. IF(nodetype=subn)AND(nf_swaped IN flags)
  158. THEN
  159. begin
  160. IF extra_not
  161. THEN
  162. emit_reg(A_NOT,opsize,left.location.register);
  163. // rg.getexplicitregisterint(exprasmlist,R_EDI);
  164. // cg.a_load_loc_reg(exprasmlist,right.location,R_EDI);
  165. // emit_reg_reg(op,opsize,left.location.register,R_EDI);
  166. // emit_reg_reg(A_MOV,opsize,R_EDI,left.location.register);
  167. // rg.ungetregisterint(exprasmlist,R_EDI);
  168. end
  169. ELSE
  170. begin
  171. { Optimizations when right.location is a constant value }
  172. IF(op=A_CMP)AND(nodetype IN [equaln,unequaln])AND(right.location.loc=LOC_CONSTANT)AND(right.location.value=0)
  173. THEN
  174. begin
  175. // emit_reg_reg(A_TEST,opsize,left.location.register,left.location.register);
  176. end
  177. ELSE IF(op=A_ADD)AND(right.location.loc=LOC_CONSTANT)AND(right.location.value=1)AND NOT(cs_check_overflow in aktlocalswitches)
  178. THEN
  179. with ExprAsmList,left.location do
  180. begin
  181. concat(TAiCpu.op_reg_const_reg(A_ADD,register,1,register));
  182. end
  183. ELSE IF(op=A_SUB)AND(right.location.loc=LOC_CONSTANT)AND(right.location.value=1)AND NOT(cs_check_overflow in aktlocalswitches)
  184. THEN
  185. begin
  186. emit_reg(A_DEC,opsize,left.location.register);
  187. end
  188. ELSE IF(op=A_SMUL)AND(right.location.loc=LOC_CONSTANT)AND(ispowerof2(right.location.value,power))AND NOT(cs_check_overflow in aktlocalswitches)
  189. THEN
  190. begin
  191. emit_const_reg(A_SLL,opsize,power,left.location.register);
  192. end
  193. ELSE
  194. begin
  195. IF extra_not
  196. THEN
  197. begin
  198. // rg.getexplicitregisterint(exprasmlist,R_EDI);
  199. // cg.a_load_loc_reg(exprasmlist,right.location,R_EDI);
  200. // emit_reg(A_NOT,S_L,R_EDI);
  201. // emit_reg_reg(A_AND,S_L,R_EDI,left.location.register);
  202. // rg.ungetregisterint(exprasmlist,R_EDI);
  203. end
  204. ELSE
  205. begin
  206. emit_op_right_left(op);
  207. end;
  208. end;
  209. end;
  210. end;
  211. { only in case of overflow operations }
  212. { produce overflow code }
  213. { we must put it here directly, because sign of operation }
  214. { is in unsigned VAR!! }
  215. IF mboverflow
  216. THEN
  217. begin
  218. IF cs_check_overflow IN aktlocalswitches
  219. THEN
  220. begin
  221. // getlabel(hl4);
  222. IF unsigned
  223. THEN
  224. emitjmp(C_NB,hl4)
  225. ELSE
  226. emitjmp(C_NO,hl4);
  227. cg.a_call_name(exprasmlist,'FPC_OVERFLOW');
  228. cg.a_label(exprasmlist,hl4);
  229. end;
  230. end;
  231. end;
  232. procedure TSparcAddNode.emit_op_right_left(op:TAsmOp);
  233. begin
  234. {left must be a register}
  235. with left,location,exprasmlist do
  236. case Right.Location.Loc of
  237. LOC_REGISTER,LOC_CREGISTER:
  238. concat(taicpu.op_reg_reg_reg(op,Register,Right.Location.register,register));
  239. LOC_REFERENCE,LOC_CREFERENCE :
  240. begin
  241. location_force_reg(exprasmlist,Right.Location,OS_32,(nodetype in [ltn,lten,gtn,gten,equaln,unequaln]));
  242. concat(taicpu.op_reg_reg_reg(op,register,Right.Location.register,register));
  243. end;
  244. LOC_CONSTANT:
  245. concat(taicpu.op_reg_const_reg(op,register,Right.Location.value,register));
  246. else
  247. InternalError(200203232);
  248. end;
  249. end;
  250. procedure TSparcAddNode.set_result_location(cmpOp,unsigned:Boolean);
  251. begin
  252. IF cmpOp
  253. THEN
  254. begin
  255. location_reset(location,LOC_FLAGS,OS_NO);
  256. location.resflags:=GetResFlags(unsigned);
  257. end
  258. ELSE
  259. location_copy(location,left.location);
  260. end;
  261. procedure TSparcAddNode.pass_2;
  262. {is also being used for "xor", and "mul", "sub", or and comparative operators}
  263. var
  264. popeax,popedx,pushedfpu,mboverflow,cmpop:Boolean;
  265. op:TAsmOp;
  266. power:LongInt;
  267. OpSize:TOpSize;
  268. unsigned:Boolean;{true, if unsigned types are compared}
  269. extra_not:Boolean;
  270. cgop:TOpCg;
  271. begin
  272. {to make it more readable, string and set (not smallset!) have their own
  273. procedures }
  274. case left.resulttype.def.deftype of
  275. orddef:
  276. if is_boolean(left.resulttype.def)and is_boolean(right.resulttype.def)
  277. then{handling boolean expressions}
  278. InternalError(20020726)//second_addboolean;
  279. else if is_64bitint(left.resulttype.def)
  280. then{64bit operations}
  281. InternalError(20020726);//second_add64bit;
  282. stringdef:
  283. InternalError(20020726);//second_addstring;
  284. setdef:
  285. {normalsets are already handled in pass1}
  286. if(tsetdef(left.resulttype.def).settype<>smallset)
  287. then
  288. internalerror(200109041)
  289. else
  290. InternalError(20020726);//second_addsmallset;
  291. arraydef :
  292. InternalError(2002110600);
  293. floatdef :
  294. InternalError(20020726);//second_addfloat;
  295. end;
  296. {defaults}
  297. extra_not:=false;
  298. mboverflow:=false;
  299. cmpop:=false;
  300. unsigned:=not(is_signed(left.resulttype.def))or
  301. not(is_signed(right.resulttype.def));
  302. opsize:=def_opsize(left.resulttype.def);
  303. pass_left_and_right;
  304. if(left.resulttype.def.deftype=pointerdef)or
  305. (right.resulttype.def.deftype=pointerdef)or
  306. (is_class_or_interface(right.resulttype.def)and is_class_or_interface(left.resulttype.def)) or
  307. (left.resulttype.def.deftype=classrefdef) or
  308. (left.resulttype.def.deftype=procvardef) or
  309. ((left.resulttype.def.deftype=enumdef)and(left.resulttype.def.size=4))or
  310. ((left.resulttype.def.deftype=orddef)and(torddef(left.resulttype.def).typ in [s32bit,u32bit]))or
  311. ((right.resulttype.def.deftype=orddef)and(torddef(right.resulttype.def).typ in [s32bit,u32bit]))
  312. then
  313. begin
  314. case NodeType of
  315. addn:
  316. begin
  317. op:=A_ADD;
  318. mboverflow:=true;
  319. end;
  320. muln:
  321. begin
  322. IF unsigned
  323. THEN
  324. op:=A_UMUL
  325. ELSE
  326. op:=A_SMUL;
  327. mboverflow:=true;
  328. end;
  329. subn:
  330. begin
  331. op:=A_SUB;
  332. mboverflow:=true;
  333. end;
  334. ltn,lten,
  335. gtn,gten,
  336. equaln,unequaln:
  337. begin
  338. op:=A_CMP;
  339. cmpop:=true;
  340. end;
  341. xorn:
  342. op:=A_XOR;
  343. orn:
  344. op:=A_OR;
  345. andn:
  346. op:=A_AND;
  347. else
  348. CGMessage(type_e_mismatch);
  349. end;
  350. { Convert flags to register first }
  351. if(left.location.loc=LOC_FLAGS)
  352. then
  353. location_force_reg(exprasmlist,left.location,opsize_2_cgsize[opsize],false);
  354. if (right.location.loc=LOC_FLAGS)
  355. then
  356. location_force_reg(exprasmlist,right.location,opsize_2_cgsize[opsize],false);
  357. left_must_be_reg(OpSize,false);
  358. emit_generic_code(op,opsize,unsigned,extra_not,mboverflow);
  359. location_freetemp(exprasmlist,right.location);
  360. location_release(exprasmlist,right.location);
  361. if cmpop and(left.location.loc<>LOC_CREGISTER)
  362. then
  363. begin
  364. location_freetemp(exprasmlist,left.location);
  365. location_release(exprasmlist,left.location);
  366. end;
  367. set_result_location(cmpop,unsigned);
  368. end;
  369. end;
  370. procedure TSparcAddNode.pass_left_and_right;
  371. var
  372. pushedregs:tmaybesave;
  373. tmpreg:tregister;
  374. pushedfpu:boolean;
  375. begin
  376. { calculate the operator which is more difficult }
  377. firstcomplex(self);
  378. { in case of constant put it to the left }
  379. if (left.nodetype=ordconstn)
  380. then
  381. swapleftright;
  382. secondpass(left);
  383. { are too few registers free? }
  384. maybe_save(exprasmlist,right.registers32,left.location,pushedregs);
  385. if location.loc=LOC_FPUREGISTER
  386. then
  387. pushedfpu:=maybe_pushfpu(exprasmlist,right.registersfpu,left.location)
  388. else
  389. pushedfpu:=false;
  390. secondpass(right);
  391. maybe_restore(exprasmlist,left.location,pushedregs);
  392. if pushedfpu
  393. then
  394. begin
  395. tmpreg := rg.getregisterfpu(exprasmlist);
  396. cg.a_loadfpu_loc_reg(exprasmlist,left.location,tmpreg);
  397. location_reset(left.location,LOC_FPUREGISTER,left.location.size);
  398. left.location.register := tmpreg;
  399. end;
  400. end;
  401. begin
  402. cAddNode:=TSparcAddNode;
  403. end.
  404. {
  405. $Log$
  406. Revision 1.10 2002-11-25 17:43:28 peter
  407. * splitted defbase in defutil,symutil,defcmp
  408. * merged isconvertable and is_equal into compare_defs(_ext)
  409. * made operator search faster by walking the list only once
  410. Revision 1.9 2002/11/10 19:07:46 mazen
  411. * SPARC calling mechanism almost OK (as in GCC./mppcsparc )
  412. Revision 1.8 2002/11/06 15:34:00 mazen
  413. *** empty log message ***
  414. Revision 1.7 2002/11/06 11:31:24 mazen
  415. * op_reg_reg_reg don't need any more a TOpSize parameter
  416. Revision 1.6 2002/11/05 16:15:00 mazen
  417. *** empty log message ***
  418. Revision 1.5 2002/10/22 13:43:01 mazen
  419. - cga.pas redueced to an empty unit
  420. Revision 1.4 2002/10/10 20:23:57 mazen
  421. * tabs replaces by spaces
  422. }