naddcpu.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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,defbase,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. begin
  180. emit_reg(A_INC,opsize,left.location.register);
  181. end
  182. ELSE IF(op=A_SUB)AND(right.location.loc=LOC_CONSTANT)AND(right.location.value=1)AND NOT(cs_check_overflow in aktlocalswitches)
  183. THEN
  184. begin
  185. emit_reg(A_DEC,opsize,left.location.register);
  186. end
  187. ELSE IF(op=A_SMUL)AND(right.location.loc=LOC_CONSTANT)AND(ispowerof2(right.location.value,power))AND NOT(cs_check_overflow in aktlocalswitches)
  188. THEN
  189. begin
  190. emit_const_reg(A_SLL,opsize,power,left.location.register);
  191. end
  192. ELSE
  193. begin
  194. IF extra_not
  195. THEN
  196. begin
  197. // rg.getexplicitregisterint(exprasmlist,R_EDI);
  198. // cg.a_load_loc_reg(exprasmlist,right.location,R_EDI);
  199. // emit_reg(A_NOT,S_L,R_EDI);
  200. // emit_reg_reg(A_AND,S_L,R_EDI,left.location.register);
  201. // rg.ungetregisterint(exprasmlist,R_EDI);
  202. end
  203. ELSE
  204. begin
  205. emit_op_right_left(op);
  206. end;
  207. end;
  208. end;
  209. end;
  210. { only in case of overflow operations }
  211. { produce overflow code }
  212. { we must put it here directly, because sign of operation }
  213. { is in unsigned VAR!! }
  214. IF mboverflow
  215. THEN
  216. begin
  217. IF cs_check_overflow IN aktlocalswitches
  218. THEN
  219. begin
  220. // getlabel(hl4);
  221. IF unsigned
  222. THEN
  223. emitjmp(C_NB,hl4)
  224. ELSE
  225. emitjmp(C_NO,hl4);
  226. cg.a_call_name(exprasmlist,'FPC_OVERFLOW');
  227. cg.a_label(exprasmlist,hl4);
  228. end;
  229. end;
  230. end;
  231. procedure TSparcAddNode.emit_op_right_left(op:TAsmOp);
  232. begin
  233. {left must be a register}
  234. with exprasmlist do
  235. case right.location.loc of
  236. LOC_REGISTER,LOC_CREGISTER:
  237. concat(taicpu.op_reg_reg_reg(op,right.location.register,left.location.register,left.location.register));
  238. LOC_REFERENCE,LOC_CREFERENCE :
  239. concat(taicpu.op_reg_ref_reg(op,S_W,left.location.register,right.location.reference,left.location.register));
  240. LOC_CONSTANT:
  241. concat(taicpu.op_reg_const_reg(op,S_W,left.location.register,right.location.value,left.location.register));
  242. else
  243. InternalError(200203232);
  244. end;
  245. end;
  246. procedure TSparcAddNode.set_result_location(cmpOp,unsigned:Boolean);
  247. begin
  248. IF cmpOp
  249. THEN
  250. begin
  251. location_reset(location,LOC_FLAGS,OS_NO);
  252. location.resflags:=GetResFlags(unsigned);
  253. end
  254. ELSE
  255. location_copy(location,left.location);
  256. end;
  257. procedure TSparcAddNode.pass_2;
  258. {is also being used for "xor", and "mul", "sub", or and comparative operators}
  259. var
  260. popeax,popedx,pushedfpu,mboverflow,cmpop:Boolean;
  261. op:TAsmOp;
  262. power:LongInt;
  263. OpSize:TOpSize;
  264. unsigned:Boolean;{true, if unsigned types are compared}
  265. extra_not:Boolean;
  266. cgop:TOpCg;
  267. begin
  268. {to make it more readable, string and set (not smallset!) have their own
  269. procedures }
  270. case left.resulttype.def.deftype of
  271. orddef:
  272. if is_boolean(left.resulttype.def)and is_boolean(right.resulttype.def)
  273. then{handling boolean expressions}
  274. InternalError(20020726)//second_addboolean;
  275. else if is_64bitint(left.resulttype.def)
  276. then{64bit operations}
  277. InternalError(20020726);//second_add64bit;
  278. stringdef:
  279. InternalError(20020726);//second_addstring;
  280. setdef:
  281. {normalsets are already handled in pass1}
  282. if(tsetdef(left.resulttype.def).settype<>smallset)
  283. then
  284. internalerror(200109041)
  285. else
  286. InternalError(20020726);//second_addsmallset;
  287. arraydef :
  288. InternalError(2002110600);
  289. floatdef :
  290. InternalError(20020726);//second_addfloat;
  291. end;
  292. {defaults}
  293. extra_not:=false;
  294. mboverflow:=false;
  295. cmpop:=false;
  296. unsigned:=not(is_signed(left.resulttype.def))or
  297. not(is_signed(right.resulttype.def));
  298. opsize:=def_opsize(left.resulttype.def);
  299. pass_left_and_right;
  300. if(left.resulttype.def.deftype=pointerdef)or
  301. (right.resulttype.def.deftype=pointerdef)or
  302. (is_class_or_interface(right.resulttype.def)and is_class_or_interface(left.resulttype.def)) or
  303. (left.resulttype.def.deftype=classrefdef) or
  304. (left.resulttype.def.deftype=procvardef) or
  305. ((left.resulttype.def.deftype=enumdef)and(left.resulttype.def.size=4))or
  306. ((left.resulttype.def.deftype=orddef)and(torddef(left.resulttype.def).typ in [s32bit,u32bit]))or
  307. ((right.resulttype.def.deftype=orddef)and(torddef(right.resulttype.def).typ in [s32bit,u32bit]))
  308. then
  309. begin
  310. case NodeType of
  311. addn:
  312. begin
  313. op:=A_ADD;
  314. mboverflow:=true;
  315. end;
  316. muln:
  317. begin
  318. IF unsigned
  319. THEN
  320. op:=A_UMUL
  321. ELSE
  322. op:=A_SMUL;
  323. mboverflow:=true;
  324. end;
  325. subn:
  326. begin
  327. op:=A_SUB;
  328. mboverflow:=true;
  329. end;
  330. ltn,lten,
  331. gtn,gten,
  332. equaln,unequaln:
  333. begin
  334. op:=A_CMP;
  335. cmpop:=true;
  336. end;
  337. xorn:
  338. op:=A_XOR;
  339. orn:
  340. op:=A_OR;
  341. andn:
  342. op:=A_AND;
  343. else
  344. CGMessage(type_e_mismatch);
  345. end;
  346. { Convert flags to register first }
  347. if(left.location.loc=LOC_FLAGS)
  348. then
  349. location_force_reg(exprasmlist,left.location,opsize_2_cgsize[opsize],false);
  350. if (right.location.loc=LOC_FLAGS)
  351. then
  352. location_force_reg(exprasmlist,right.location,opsize_2_cgsize[opsize],false);
  353. left_must_be_reg(OpSize,false);
  354. emit_generic_code(op,opsize,unsigned,extra_not,mboverflow);
  355. location_freetemp(exprasmlist,right.location);
  356. location_release(exprasmlist,right.location);
  357. if cmpop and(left.location.loc<>LOC_CREGISTER)
  358. then
  359. begin
  360. location_freetemp(exprasmlist,left.location);
  361. location_release(exprasmlist,left.location);
  362. end;
  363. set_result_location(cmpop,unsigned);
  364. end;
  365. end;
  366. procedure TSparcAddNode.pass_left_and_right;
  367. var
  368. pushedregs:tmaybesave;
  369. tmpreg:tregister;
  370. pushedfpu:boolean;
  371. begin
  372. { calculate the operator which is more difficult }
  373. firstcomplex(self);
  374. { in case of constant put it to the left }
  375. if (left.nodetype=ordconstn)
  376. then
  377. swapleftright;
  378. secondpass(left);
  379. { are too few registers free? }
  380. maybe_save(exprasmlist,right.registers32,left.location,pushedregs);
  381. if location.loc=LOC_FPUREGISTER
  382. then
  383. pushedfpu:=maybe_pushfpu(exprasmlist,right.registersfpu,left.location)
  384. else
  385. pushedfpu:=false;
  386. secondpass(right);
  387. maybe_restore(exprasmlist,left.location,pushedregs);
  388. if pushedfpu
  389. then
  390. begin
  391. tmpreg := rg.getregisterfpu(exprasmlist);
  392. cg.a_loadfpu_loc_reg(exprasmlist,left.location,tmpreg);
  393. location_reset(left.location,LOC_FPUREGISTER,left.location.size);
  394. left.location.register := tmpreg;
  395. end;
  396. end;
  397. begin
  398. cAddNode:=TSparcAddNode;
  399. end.
  400. {
  401. $Log$
  402. Revision 1.8 2002-11-06 15:34:00 mazen
  403. *** empty log message ***
  404. Revision 1.7 2002/11/06 11:31:24 mazen
  405. * op_reg_reg_reg don't need any more a TOpSize parameter
  406. Revision 1.6 2002/11/05 16:15:00 mazen
  407. *** empty log message ***
  408. Revision 1.5 2002/10/22 13:43:01 mazen
  409. - cga.pas redueced to an empty unit
  410. Revision 1.4 2002/10/10 20:23:57 mazen
  411. * tabs replaces by spaces
  412. }