ncgadd.pas 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. {
  2. Copyright (c) 2000-2002 by the FPC development team
  3. Code generation for add nodes (generic version)
  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 ncgadd;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,nadd,cpubase;
  22. type
  23. tcgaddnode = class(taddnode)
  24. { function pass_1: tnode; override;}
  25. procedure pass_2;override;
  26. protected
  27. { call secondpass for both left and right }
  28. procedure pass_left_right;
  29. { set the register of the result location }
  30. procedure set_result_location_reg;
  31. { load left and right nodes into registers }
  32. procedure force_reg_left_right(allow_swap,allow_constant:boolean);
  33. procedure second_opfloat;
  34. procedure second_opboolean;
  35. procedure second_opsmallset;
  36. procedure second_op64bit;
  37. procedure second_opordinal;
  38. procedure second_addstring;virtual;
  39. procedure second_addfloat;virtual;abstract;
  40. procedure second_addboolean;virtual;
  41. procedure second_addsmallset;virtual;
  42. {$ifdef x86}
  43. {$ifdef SUPPORT_MMX}
  44. procedure second_opmmxset;virtual;abstract;
  45. procedure second_opmmx;virtual;abstract;
  46. {$endif SUPPORT_MMX}
  47. {$endif x86}
  48. procedure second_add64bit;virtual;
  49. procedure second_addordinal;virtual;
  50. procedure second_cmpfloat;virtual;abstract;
  51. procedure second_cmpboolean;virtual;
  52. procedure second_cmpsmallset;virtual;abstract;
  53. procedure second_cmp64bit;virtual;abstract;
  54. procedure second_cmpordinal;virtual;abstract;
  55. end;
  56. implementation
  57. uses
  58. globtype,systems,
  59. cutils,verbose,globals,
  60. symconst,symdef,paramgr,
  61. aasmbase,aasmtai,defutil,
  62. cgbase,pass_2,
  63. ncon,nset,ncgutil,cgobj,cgutils
  64. ;
  65. {*****************************************************************************
  66. Helpers
  67. *****************************************************************************}
  68. procedure tcgaddnode.pass_left_right;
  69. var
  70. tmpreg : tregister;
  71. isjump,
  72. pushedfpu : boolean;
  73. otl,ofl : tasmlabel;
  74. begin
  75. { calculate the operator which is more difficult }
  76. firstcomplex(self);
  77. { in case of constant put it to the left }
  78. if (left.nodetype=ordconstn) then
  79. swapleftright;
  80. isjump:=(left.expectloc=LOC_JUMP);
  81. if isjump then
  82. begin
  83. otl:=truelabel;
  84. objectlibrary.getlabel(truelabel);
  85. ofl:=falselabel;
  86. objectlibrary.getlabel(falselabel);
  87. end;
  88. secondpass(left);
  89. if left.location.loc in [LOC_FLAGS,LOC_JUMP] then
  90. location_force_reg(exprasmlist,left.location,def_cgsize(resulttype.def),false);
  91. if isjump then
  92. begin
  93. truelabel:=otl;
  94. falselabel:=ofl;
  95. end;
  96. { are too few registers free? }
  97. if left.location.loc=LOC_FPUREGISTER then
  98. pushedfpu:=maybe_pushfpu(exprasmlist,right.registersfpu,left.location)
  99. else
  100. pushedfpu:=false;
  101. isjump:=(right.expectloc=LOC_JUMP);
  102. if isjump then
  103. begin
  104. otl:=truelabel;
  105. objectlibrary.getlabel(truelabel);
  106. ofl:=falselabel;
  107. objectlibrary.getlabel(falselabel);
  108. end;
  109. secondpass(right);
  110. if right.location.loc in [LOC_FLAGS,LOC_JUMP] then
  111. location_force_reg(exprasmlist,right.location,def_cgsize(resulttype.def),false);
  112. if isjump then
  113. begin
  114. truelabel:=otl;
  115. falselabel:=ofl;
  116. end;
  117. if pushedfpu then
  118. begin
  119. tmpreg := cg.getfpuregister(exprasmlist,left.location.size);
  120. cg.a_loadfpu_loc_reg(exprasmlist,left.location,tmpreg);
  121. location_reset(left.location,LOC_FPUREGISTER,left.location.size);
  122. left.location.register := tmpreg;
  123. {$ifdef x86}
  124. { left operand is now on top of the stack, instead of the right one! }
  125. toggleflag(nf_swaped);
  126. {$endif x86}
  127. end;
  128. end;
  129. procedure tcgaddnode.set_result_location_reg;
  130. begin
  131. location_reset(location,LOC_REGISTER,def_cgsize(resulttype.def));
  132. {$ifdef x86}
  133. if left.location.loc=LOC_REGISTER then
  134. begin
  135. if TCGSize2Size[left.location.size]<>TCGSize2Size[location.size] then
  136. internalerror(200307041);
  137. {$ifndef cpu64bit}
  138. if location.size in [OS_64,OS_S64] then
  139. begin
  140. location.register64.reglo := left.location.register64.reglo;
  141. location.register64.reghi := left.location.register64.reghi;
  142. end
  143. else
  144. {$endif}
  145. location.register := left.location.register;
  146. end
  147. else
  148. if right.location.loc=LOC_REGISTER then
  149. begin
  150. if TCGSize2Size[right.location.size]<>TCGSize2Size[location.size] then
  151. internalerror(200307042);
  152. {$ifndef cpu64bit}
  153. if location.size in [OS_64,OS_S64] then
  154. begin
  155. location.register64.reglo := right.location.register64.reglo;
  156. location.register64.reghi := right.location.register64.reghi;
  157. end
  158. else
  159. {$endif}
  160. location.register := right.location.register;
  161. end
  162. else
  163. {$endif}
  164. begin
  165. {$ifndef cpu64bit}
  166. if location.size in [OS_64,OS_S64] then
  167. begin
  168. location.register64.reglo := cg.getintregister(exprasmlist,OS_INT);
  169. location.register64.reghi := cg.getintregister(exprasmlist,OS_INT);
  170. end
  171. else
  172. {$endif}
  173. location.register := cg.getintregister(exprasmlist,location.size);
  174. end;
  175. end;
  176. procedure tcgaddnode.force_reg_left_right(allow_swap,allow_constant:boolean);
  177. begin
  178. if (left.location.loc<>LOC_REGISTER) and
  179. not(
  180. allow_constant and
  181. (left.location.loc in [LOC_CONSTANT,LOC_CREGISTER])
  182. ) then
  183. location_force_reg(exprasmlist,left.location,left.location.size,false);
  184. if (right.location.loc<>LOC_REGISTER) and
  185. not(
  186. allow_constant and
  187. (right.location.loc in [LOC_CONSTANT,LOC_CREGISTER]) and
  188. (left.location.loc<>LOC_CONSTANT)
  189. ) then
  190. location_force_reg(exprasmlist,right.location,right.location.size,false);
  191. { Left is always a register, right can be register or constant }
  192. if left.location.loc=LOC_CONSTANT then
  193. begin
  194. { when it is not allowed to swap we have a constant on
  195. left, that will give problems }
  196. if not allow_swap then
  197. internalerror(200307041);
  198. swapleftright;
  199. end;
  200. end;
  201. {*****************************************************************************
  202. Smallsets
  203. *****************************************************************************}
  204. procedure tcgaddnode.second_opsmallset;
  205. begin
  206. { when a setdef is passed, it has to be a smallset }
  207. if ((left.resulttype.def.deftype=setdef) and
  208. (tsetdef(left.resulttype.def).settype<>smallset)) or
  209. ((right.resulttype.def.deftype=setdef) and
  210. (tsetdef(right.resulttype.def).settype<>smallset)) then
  211. internalerror(200203301);
  212. if nodetype in [equaln,unequaln,gtn,gten,lten,ltn] then
  213. second_cmpsmallset
  214. else
  215. second_addsmallset;
  216. end;
  217. procedure tcgaddnode.second_addsmallset;
  218. var
  219. cgop : TOpCg;
  220. tmpreg : tregister;
  221. opdone : boolean;
  222. begin
  223. opdone := false;
  224. pass_left_right;
  225. force_reg_left_right(true,true);
  226. { setelementn is a special case, it must be on right.
  227. We need an extra check if left is a register because the
  228. default case can skip the register loading when the
  229. setelementn is in a register (PFV) }
  230. if (nf_swaped in flags) and
  231. (left.nodetype=setelementn) then
  232. swapleftright;
  233. if (right.nodetype=setelementn) and
  234. (left.location.loc<>LOC_REGISTER) then
  235. location_force_reg(exprasmlist,left.location,left.location.size,false);
  236. set_result_location_reg;
  237. case nodetype of
  238. addn :
  239. begin
  240. { are we adding set elements ? }
  241. if right.nodetype=setelementn then
  242. begin
  243. { no range support for smallsets! }
  244. if assigned(tsetelementnode(right).right) then
  245. internalerror(43244);
  246. if (right.location.loc = LOC_CONSTANT) then
  247. cg.a_op_const_reg_reg(exprasmlist,OP_OR,location.size,
  248. aint(1 shl right.location.value),
  249. left.location.register,location.register)
  250. else
  251. begin
  252. tmpreg := cg.getintregister(exprasmlist,location.size);
  253. cg.a_load_const_reg(exprasmlist,location.size,1,tmpreg);
  254. cg.a_op_reg_reg(exprasmlist,OP_SHL,location.size,
  255. right.location.register,tmpreg);
  256. if left.location.loc <> LOC_CONSTANT then
  257. cg.a_op_reg_reg_reg(exprasmlist,OP_OR,location.size,tmpreg,
  258. left.location.register,location.register)
  259. else
  260. cg.a_op_const_reg_reg(exprasmlist,OP_OR,location.size,
  261. left.location.value,tmpreg,location.register);
  262. end;
  263. opdone := true;
  264. end
  265. else
  266. cgop := OP_OR;
  267. end;
  268. symdifn :
  269. cgop:=OP_XOR;
  270. muln :
  271. cgop:=OP_AND;
  272. subn :
  273. begin
  274. cgop:=OP_AND;
  275. if (not(nf_swaped in flags)) then
  276. if (right.location.loc=LOC_CONSTANT) then
  277. right.location.value := not(right.location.value)
  278. else
  279. opdone := true
  280. else if (left.location.loc=LOC_CONSTANT) then
  281. left.location.value := not(left.location.value)
  282. else
  283. begin
  284. swapleftright;
  285. opdone := true;
  286. end;
  287. if opdone then
  288. begin
  289. if left.location.loc = LOC_CONSTANT then
  290. begin
  291. tmpreg := cg.getintregister(exprasmlist,location.size);
  292. cg.a_load_const_reg(exprasmlist,location.size,
  293. left.location.value,tmpreg);
  294. cg.a_op_reg_reg(exprasmlist,OP_NOT,location.size,right.location.register,right.location.register);
  295. cg.a_op_reg_reg(exprasmlist,OP_AND,location.size,right.location.register,tmpreg);
  296. cg.a_load_reg_reg(exprasmlist,OS_INT,location.size,tmpreg,location.register);
  297. end
  298. else
  299. begin
  300. cg.a_op_reg_reg(exprasmlist,OP_NOT,right.location.size,right.location.register,right.location.register);
  301. cg.a_op_reg_reg(exprasmlist,OP_AND,left.location.size,right.location.register,left.location.register);
  302. cg.a_load_reg_reg(exprasmlist,left.location.size,location.size,left.location.register,location.register);
  303. end;
  304. end;
  305. end;
  306. else
  307. internalerror(2002072701);
  308. end;
  309. if not opdone then
  310. begin
  311. // these are all commutative operations
  312. if (left.location.loc = LOC_CONSTANT) then
  313. swapleftright;
  314. if (right.location.loc = LOC_CONSTANT) then
  315. cg.a_op_const_reg_reg(exprasmlist,cgop,location.size,
  316. right.location.value,left.location.register,
  317. location.register)
  318. else
  319. cg.a_op_reg_reg_reg(exprasmlist,cgop,location.size,
  320. right.location.register,left.location.register,
  321. location.register);
  322. end;
  323. end;
  324. {*****************************************************************************
  325. Boolean
  326. *****************************************************************************}
  327. procedure tcgaddnode.second_opboolean;
  328. begin
  329. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  330. second_cmpboolean
  331. else
  332. second_addboolean;
  333. end;
  334. procedure tcgaddnode.second_addboolean;
  335. var
  336. cgop : TOpCg;
  337. otl,ofl : tasmlabel;
  338. begin
  339. { And,Or will only evaluate from left to right only the
  340. needed nodes unless full boolean evaluation is enabled }
  341. if (nodetype in [orn,andn]) and
  342. (not(cs_full_boolean_eval in aktlocalswitches) or
  343. (nf_short_bool in flags)) then
  344. begin
  345. location_reset(location,LOC_JUMP,OS_NO);
  346. case nodetype of
  347. andn :
  348. begin
  349. otl:=truelabel;
  350. objectlibrary.getlabel(truelabel);
  351. secondpass(left);
  352. maketojumpbool(exprasmlist,left,lr_load_regvars);
  353. cg.a_label(exprasmlist,truelabel);
  354. truelabel:=otl;
  355. end;
  356. orn :
  357. begin
  358. ofl:=falselabel;
  359. objectlibrary.getlabel(falselabel);
  360. secondpass(left);
  361. maketojumpbool(exprasmlist,left,lr_load_regvars);
  362. cg.a_label(exprasmlist,falselabel);
  363. falselabel:=ofl;
  364. end;
  365. else
  366. internalerror(200307044);
  367. end;
  368. secondpass(right);
  369. maketojumpbool(exprasmlist,right,lr_load_regvars);
  370. end
  371. else
  372. begin
  373. pass_left_right;
  374. force_reg_left_right(false,true);
  375. set_result_location_reg;
  376. case nodetype of
  377. xorn :
  378. cgop:=OP_XOR;
  379. orn :
  380. cgop:=OP_OR;
  381. andn :
  382. cgop:=OP_AND;
  383. else
  384. internalerror(200203247);
  385. end;
  386. if right.location.loc <> LOC_CONSTANT then
  387. cg.a_op_reg_reg_reg(exprasmlist,cgop,location.size,
  388. left.location.register,right.location.register,
  389. location.register)
  390. else
  391. cg.a_op_const_reg_reg(exprasmlist,cgop,location.size,
  392. right.location.value,left.location.register,
  393. location.register);
  394. end;
  395. end;
  396. {*****************************************************************************
  397. 64-bit
  398. *****************************************************************************}
  399. procedure tcgaddnode.second_op64bit;
  400. begin
  401. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  402. second_cmp64bit
  403. else
  404. second_add64bit;
  405. end;
  406. procedure tcgaddnode.second_add64bit;
  407. var
  408. op : TOpCG;
  409. checkoverflow : boolean;
  410. ovloc : tlocation;
  411. begin
  412. ovloc.loc:=LOC_VOID;
  413. pass_left_right;
  414. force_reg_left_right(false,(cs_check_overflow in aktlocalswitches) and
  415. (nodetype in [addn,subn]));
  416. set_result_location_reg;
  417. { assume no overflow checking is required }
  418. checkoverflow := false;
  419. case nodetype of
  420. addn :
  421. begin
  422. op:=OP_ADD;
  423. checkoverflow:=true;
  424. end;
  425. subn :
  426. begin
  427. op:=OP_SUB;
  428. checkoverflow:=true;
  429. end;
  430. xorn:
  431. op:=OP_XOR;
  432. orn:
  433. op:=OP_OR;
  434. andn:
  435. op:=OP_AND;
  436. muln:
  437. begin
  438. { should be handled in pass_1 (JM) }
  439. internalerror(200109051);
  440. end;
  441. else
  442. internalerror(2002072705);
  443. end;
  444. {$ifdef cpu64bit}
  445. case nodetype of
  446. xorn,orn,andn,addn:
  447. begin
  448. if (right.location.loc = LOC_CONSTANT) then
  449. cg.a_op_const_reg_reg(exprasmlist,op,location.size,right.location.value,
  450. left.location.register,location.register)
  451. else
  452. cg.a_op_reg_reg_reg(exprasmlist,op,location.size,right.location.register,
  453. left.location.register,location.register);
  454. end;
  455. subn:
  456. begin
  457. if (nf_swaped in flags) then
  458. swapleftright;
  459. if left.location.loc <> LOC_CONSTANT then
  460. begin
  461. if right.location.loc <> LOC_CONSTANT then
  462. // reg64 - reg64
  463. cg.a_op_reg_reg_reg_checkoverflow(exprasmlist,OP_SUB,location.size,
  464. right.location.register,left.location.register,location.register,
  465. checkoverflow and (cs_check_overflow in aktlocalswitches),ovloc)
  466. else
  467. // reg64 - const64
  468. cg.a_op_const_reg_reg_checkoverflow(exprasmlist,OP_SUB,location.size,
  469. right.location.value,left.location.register,location.register,
  470. checkoverflow and (cs_check_overflow in aktlocalswitches),ovloc);
  471. end
  472. else
  473. begin
  474. // const64 - reg64
  475. location_force_reg(exprasmlist,left.location,left.location.size,true);
  476. cg.a_op_reg_reg_reg_checkoverflow(exprasmlist,OP_SUB,location.size,
  477. right.location.register,left.location.register,location.register,
  478. checkoverflow and (cs_check_overflow in aktlocalswitches),ovloc);
  479. end;
  480. end;
  481. else
  482. internalerror(2002072803);
  483. end;
  484. {$else cpu64bit}
  485. case nodetype of
  486. xorn,orn,andn,addn:
  487. begin
  488. if (right.location.loc = LOC_CONSTANT) then
  489. cg64.a_op64_const_reg_reg_checkoverflow(exprasmlist,op,location.size,right.location.value64,
  490. left.location.register64,location.register64,
  491. checkoverflow and (cs_check_overflow in aktlocalswitches),ovloc)
  492. else
  493. cg64.a_op64_reg_reg_reg_checkoverflow(exprasmlist,op,location.size,right.location.register64,
  494. left.location.register64,location.register64,
  495. checkoverflow and (cs_check_overflow in aktlocalswitches),ovloc);
  496. end;
  497. subn:
  498. begin
  499. if (nf_swaped in flags) then
  500. swapleftright;
  501. if left.location.loc <> LOC_CONSTANT then
  502. begin
  503. if right.location.loc <> LOC_CONSTANT then
  504. // reg64 - reg64
  505. cg64.a_op64_reg_reg_reg_checkoverflow(exprasmlist,OP_SUB,location.size,
  506. right.location.register64,left.location.register64,
  507. location.register64,
  508. checkoverflow and (cs_check_overflow in aktlocalswitches),ovloc)
  509. else
  510. // reg64 - const64
  511. cg64.a_op64_const_reg_reg_checkoverflow(exprasmlist,OP_SUB,location.size,
  512. right.location.value64,left.location.register64,
  513. location.register64,
  514. checkoverflow and (cs_check_overflow in aktlocalswitches),ovloc)
  515. end
  516. else
  517. begin
  518. // const64 - reg64
  519. location_force_reg(exprasmlist,left.location,left.location.size,true);
  520. cg64.a_op64_reg_reg_reg_checkoverflow(exprasmlist,OP_SUB,location.size,
  521. right.location.register64,left.location.register64,
  522. location.register64,
  523. checkoverflow and (cs_check_overflow in aktlocalswitches),ovloc);
  524. end;
  525. end;
  526. else
  527. internalerror(2002072803);
  528. end;
  529. {$endif cpu64bit}
  530. { emit overflow check if enabled }
  531. if checkoverflow then
  532. cg.g_overflowcheck_loc(exprasmlist,Location,ResultType.Def,ovloc);
  533. end;
  534. {*****************************************************************************
  535. Strings
  536. *****************************************************************************}
  537. procedure tcgaddnode.second_addstring;
  538. begin
  539. { this should already be handled in pass1 }
  540. internalerror(2002072402);
  541. end;
  542. {*****************************************************************************
  543. Floats
  544. *****************************************************************************}
  545. procedure tcgaddnode.second_opfloat;
  546. begin
  547. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  548. second_cmpfloat
  549. else
  550. second_addfloat;
  551. end;
  552. {*****************************************************************************
  553. Ordinals
  554. *****************************************************************************}
  555. procedure tcgaddnode.second_opordinal;
  556. begin
  557. if (nodetype in [ltn,lten,gtn,gten,equaln,unequaln]) then
  558. second_cmpordinal
  559. else
  560. second_addordinal;
  561. end;
  562. procedure tcgaddnode.second_addordinal;
  563. var
  564. unsigned,
  565. checkoverflow : boolean;
  566. cgop : topcg;
  567. tmpreg : tregister;
  568. ovloc : tlocation;
  569. begin
  570. ovloc.loc:=LOC_VOID;
  571. pass_left_right;
  572. force_reg_left_right(false,(cs_check_overflow in aktlocalswitches) and
  573. (nodetype in [addn,subn,muln]));
  574. set_result_location_reg;
  575. { determine if the comparison will be unsigned }
  576. unsigned:=not(is_signed(left.resulttype.def)) or
  577. not(is_signed(right.resulttype.def));
  578. { assume no overflow checking is require }
  579. checkoverflow := false;
  580. case nodetype of
  581. addn:
  582. begin
  583. cgop:=OP_ADD;
  584. checkoverflow:=true;
  585. end;
  586. xorn :
  587. begin
  588. cgop:=OP_XOR;
  589. end;
  590. orn :
  591. begin
  592. cgop:=OP_OR;
  593. end;
  594. andn:
  595. begin
  596. cgop:=OP_AND;
  597. end;
  598. muln:
  599. begin
  600. checkoverflow:=true;
  601. if unsigned then
  602. cgop:=OP_MUL
  603. else
  604. cgop:=OP_IMUL;
  605. end;
  606. subn :
  607. begin
  608. checkoverflow:=true;
  609. cgop:=OP_SUB;
  610. end;
  611. end;
  612. if nodetype<>subn then
  613. begin
  614. if (right.location.loc >LOC_CONSTANT) then
  615. cg.a_op_reg_reg_reg_checkoverflow(exprasmlist,cgop,location.size,
  616. left.location.register,right.location.register,
  617. location.register,checkoverflow and (cs_check_overflow in aktlocalswitches),ovloc)
  618. else
  619. cg.a_op_const_reg_reg_checkoverflow(exprasmlist,cgop,location.size,
  620. right.location.value,left.location.register,
  621. location.register,checkoverflow and (cs_check_overflow in aktlocalswitches),ovloc);
  622. end
  623. else { subtract is a special case since its not commutative }
  624. begin
  625. if (nf_swaped in flags) then
  626. swapleftright;
  627. if left.location.loc<>LOC_CONSTANT then
  628. begin
  629. if right.location.loc<>LOC_CONSTANT then
  630. cg.a_op_reg_reg_reg_checkoverflow(exprasmlist,OP_SUB,location.size,
  631. right.location.register,left.location.register,
  632. location.register,checkoverflow and (cs_check_overflow in aktlocalswitches),ovloc)
  633. else
  634. cg.a_op_const_reg_reg_checkoverflow(exprasmlist,OP_SUB,location.size,
  635. aword(right.location.value),left.location.register,
  636. location.register,checkoverflow and (cs_check_overflow in aktlocalswitches),ovloc);
  637. end
  638. else
  639. begin
  640. tmpreg:=cg.getintregister(exprasmlist,location.size);
  641. cg.a_load_const_reg(exprasmlist,location.size,
  642. aword(left.location.value),tmpreg);
  643. cg.a_op_reg_reg_reg_checkoverflow(exprasmlist,OP_SUB,location.size,
  644. right.location.register,tmpreg,location.register,checkoverflow and (cs_check_overflow in aktlocalswitches),ovloc);
  645. end;
  646. end;
  647. { emit overflow check if required }
  648. if checkoverflow then
  649. cg.g_overflowcheck_loc(exprasmlist,Location,ResultType.Def,ovloc);
  650. end;
  651. procedure tcgaddnode.second_cmpboolean;
  652. begin
  653. second_cmpordinal;
  654. end;
  655. {*****************************************************************************
  656. pass_2
  657. *****************************************************************************}
  658. procedure tcgaddnode.pass_2;
  659. begin
  660. case left.resulttype.def.deftype of
  661. orddef :
  662. begin
  663. { handling boolean expressions }
  664. if is_boolean(left.resulttype.def) and
  665. is_boolean(right.resulttype.def) then
  666. second_opboolean
  667. { 64bit operations }
  668. else if is_64bit(left.resulttype.def) then
  669. second_op64bit
  670. else
  671. second_opordinal;
  672. end;
  673. stringdef :
  674. begin
  675. second_addstring;
  676. end;
  677. setdef :
  678. begin
  679. {Normalsets are already handled in pass1 if mmx
  680. should not be used.}
  681. if (tsetdef(left.resulttype.def).settype<>smallset) then
  682. begin
  683. {$ifdef SUPPORT_MMX}
  684. {$ifdef i386}
  685. if cs_mmx in aktlocalswitches then
  686. second_opmmxset
  687. else
  688. {$endif}
  689. {$endif SUPPORT_MMX}
  690. internalerror(200109041);
  691. end
  692. else
  693. second_opsmallset;
  694. end;
  695. arraydef :
  696. begin
  697. { support dynarr=nil }
  698. if is_dynamic_array(left.resulttype.def) then
  699. second_opordinal
  700. {$ifdef SUPPORT_MMX}
  701. else
  702. if is_mmx_able_array(left.resulttype.def) then
  703. second_opmmx
  704. {$endif SUPPORT_MMX}
  705. else
  706. internalerror(200306016);
  707. end;
  708. floatdef :
  709. second_opfloat;
  710. else
  711. second_opordinal;
  712. end;
  713. end;
  714. begin
  715. caddnode:=tcgaddnode;
  716. end.