ncgadd.pas 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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.getjumplabel(truelabel);
  85. ofl:=falselabel;
  86. objectlibrary.getjumplabel(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.getjumplabel(truelabel);
  106. ofl:=falselabel;
  107. objectlibrary.getjumplabel(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) then
  343. begin
  344. location_reset(location,LOC_JUMP,OS_NO);
  345. case nodetype of
  346. andn :
  347. begin
  348. otl:=truelabel;
  349. objectlibrary.getjumplabel(truelabel);
  350. secondpass(left);
  351. maketojumpbool(exprasmlist,left,lr_load_regvars);
  352. cg.a_label(exprasmlist,truelabel);
  353. truelabel:=otl;
  354. end;
  355. orn :
  356. begin
  357. ofl:=falselabel;
  358. objectlibrary.getjumplabel(falselabel);
  359. secondpass(left);
  360. maketojumpbool(exprasmlist,left,lr_load_regvars);
  361. cg.a_label(exprasmlist,falselabel);
  362. falselabel:=ofl;
  363. end;
  364. else
  365. internalerror(200307044);
  366. end;
  367. secondpass(right);
  368. maketojumpbool(exprasmlist,right,lr_load_regvars);
  369. end
  370. else
  371. begin
  372. pass_left_right;
  373. force_reg_left_right(false,true);
  374. set_result_location_reg;
  375. case nodetype of
  376. xorn :
  377. cgop:=OP_XOR;
  378. orn :
  379. cgop:=OP_OR;
  380. andn :
  381. cgop:=OP_AND;
  382. else
  383. internalerror(200203247);
  384. end;
  385. if right.location.loc <> LOC_CONSTANT then
  386. cg.a_op_reg_reg_reg(exprasmlist,cgop,location.size,
  387. left.location.register,right.location.register,
  388. location.register)
  389. else
  390. cg.a_op_const_reg_reg(exprasmlist,cgop,location.size,
  391. right.location.value,left.location.register,
  392. location.register);
  393. end;
  394. end;
  395. {*****************************************************************************
  396. 64-bit
  397. *****************************************************************************}
  398. procedure tcgaddnode.second_op64bit;
  399. begin
  400. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  401. second_cmp64bit
  402. else
  403. second_add64bit;
  404. end;
  405. procedure tcgaddnode.second_add64bit;
  406. var
  407. op : TOpCG;
  408. checkoverflow : boolean;
  409. ovloc : tlocation;
  410. begin
  411. ovloc.loc:=LOC_VOID;
  412. pass_left_right;
  413. force_reg_left_right(false,(cs_check_overflow in aktlocalswitches) and
  414. (nodetype in [addn,subn]));
  415. set_result_location_reg;
  416. { assume no overflow checking is required }
  417. checkoverflow := false;
  418. case nodetype of
  419. addn :
  420. begin
  421. op:=OP_ADD;
  422. checkoverflow:=true;
  423. end;
  424. subn :
  425. begin
  426. op:=OP_SUB;
  427. checkoverflow:=true;
  428. end;
  429. xorn:
  430. op:=OP_XOR;
  431. orn:
  432. op:=OP_OR;
  433. andn:
  434. op:=OP_AND;
  435. muln:
  436. begin
  437. { should be handled in pass_1 (JM) }
  438. internalerror(200109051);
  439. end;
  440. else
  441. internalerror(2002072705);
  442. end;
  443. {$ifdef cpu64bit}
  444. case nodetype of
  445. xorn,orn,andn,addn:
  446. begin
  447. if (right.location.loc = LOC_CONSTANT) then
  448. cg.a_op_const_reg_reg(exprasmlist,op,location.size,right.location.value,
  449. left.location.register,location.register)
  450. else
  451. cg.a_op_reg_reg_reg(exprasmlist,op,location.size,right.location.register,
  452. left.location.register,location.register);
  453. end;
  454. subn:
  455. begin
  456. if (nf_swaped in flags) then
  457. swapleftright;
  458. if left.location.loc <> LOC_CONSTANT then
  459. begin
  460. if right.location.loc <> LOC_CONSTANT then
  461. // reg64 - reg64
  462. cg.a_op_reg_reg_reg_checkoverflow(exprasmlist,OP_SUB,location.size,
  463. right.location.register,left.location.register,location.register,
  464. checkoverflow and (cs_check_overflow in aktlocalswitches),ovloc)
  465. else
  466. // reg64 - const64
  467. cg.a_op_const_reg_reg_checkoverflow(exprasmlist,OP_SUB,location.size,
  468. right.location.value,left.location.register,location.register,
  469. checkoverflow and (cs_check_overflow in aktlocalswitches),ovloc);
  470. end
  471. else
  472. begin
  473. // const64 - reg64
  474. location_force_reg(exprasmlist,left.location,left.location.size,true);
  475. cg.a_op_reg_reg_reg_checkoverflow(exprasmlist,OP_SUB,location.size,
  476. right.location.register,left.location.register,location.register,
  477. checkoverflow and (cs_check_overflow in aktlocalswitches),ovloc);
  478. end;
  479. end;
  480. else
  481. internalerror(2002072803);
  482. end;
  483. {$else cpu64bit}
  484. case nodetype of
  485. xorn,orn,andn,addn:
  486. begin
  487. if (right.location.loc = LOC_CONSTANT) then
  488. cg64.a_op64_const_reg_reg_checkoverflow(exprasmlist,op,location.size,right.location.value64,
  489. left.location.register64,location.register64,
  490. checkoverflow and (cs_check_overflow in aktlocalswitches),ovloc)
  491. else
  492. cg64.a_op64_reg_reg_reg_checkoverflow(exprasmlist,op,location.size,right.location.register64,
  493. left.location.register64,location.register64,
  494. checkoverflow and (cs_check_overflow in aktlocalswitches),ovloc);
  495. end;
  496. subn:
  497. begin
  498. if (nf_swaped in flags) then
  499. swapleftright;
  500. if left.location.loc <> LOC_CONSTANT then
  501. begin
  502. if right.location.loc <> LOC_CONSTANT then
  503. // reg64 - reg64
  504. cg64.a_op64_reg_reg_reg_checkoverflow(exprasmlist,OP_SUB,location.size,
  505. right.location.register64,left.location.register64,
  506. location.register64,
  507. checkoverflow and (cs_check_overflow in aktlocalswitches),ovloc)
  508. else
  509. // reg64 - const64
  510. cg64.a_op64_const_reg_reg_checkoverflow(exprasmlist,OP_SUB,location.size,
  511. right.location.value64,left.location.register64,
  512. location.register64,
  513. checkoverflow and (cs_check_overflow in aktlocalswitches),ovloc)
  514. end
  515. else
  516. begin
  517. // const64 - reg64
  518. location_force_reg(exprasmlist,left.location,left.location.size,true);
  519. cg64.a_op64_reg_reg_reg_checkoverflow(exprasmlist,OP_SUB,location.size,
  520. right.location.register64,left.location.register64,
  521. location.register64,
  522. checkoverflow and (cs_check_overflow in aktlocalswitches),ovloc);
  523. end;
  524. end;
  525. else
  526. internalerror(2002072803);
  527. end;
  528. {$endif cpu64bit}
  529. { emit overflow check if enabled }
  530. if checkoverflow then
  531. cg.g_overflowcheck_loc(exprasmlist,Location,ResultType.Def,ovloc);
  532. end;
  533. {*****************************************************************************
  534. Strings
  535. *****************************************************************************}
  536. procedure tcgaddnode.second_addstring;
  537. begin
  538. { this should already be handled in pass1 }
  539. internalerror(2002072402);
  540. end;
  541. {*****************************************************************************
  542. Floats
  543. *****************************************************************************}
  544. procedure tcgaddnode.second_opfloat;
  545. begin
  546. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  547. second_cmpfloat
  548. else
  549. second_addfloat;
  550. end;
  551. {*****************************************************************************
  552. Ordinals
  553. *****************************************************************************}
  554. procedure tcgaddnode.second_opordinal;
  555. begin
  556. if (nodetype in [ltn,lten,gtn,gten,equaln,unequaln]) then
  557. second_cmpordinal
  558. else
  559. second_addordinal;
  560. end;
  561. procedure tcgaddnode.second_addordinal;
  562. var
  563. unsigned,
  564. checkoverflow : boolean;
  565. cgop : topcg;
  566. tmpreg : tregister;
  567. ovloc : tlocation;
  568. begin
  569. ovloc.loc:=LOC_VOID;
  570. pass_left_right;
  571. force_reg_left_right(false,(cs_check_overflow in aktlocalswitches) and
  572. (nodetype in [addn,subn,muln]));
  573. set_result_location_reg;
  574. { determine if the comparison will be unsigned }
  575. unsigned:=not(is_signed(left.resulttype.def)) or
  576. not(is_signed(right.resulttype.def));
  577. { assume no overflow checking is require }
  578. checkoverflow := false;
  579. case nodetype of
  580. addn:
  581. begin
  582. cgop:=OP_ADD;
  583. checkoverflow:=true;
  584. end;
  585. xorn :
  586. begin
  587. cgop:=OP_XOR;
  588. end;
  589. orn :
  590. begin
  591. cgop:=OP_OR;
  592. end;
  593. andn:
  594. begin
  595. cgop:=OP_AND;
  596. end;
  597. muln:
  598. begin
  599. checkoverflow:=true;
  600. if unsigned then
  601. cgop:=OP_MUL
  602. else
  603. cgop:=OP_IMUL;
  604. end;
  605. subn :
  606. begin
  607. checkoverflow:=true;
  608. cgop:=OP_SUB;
  609. end;
  610. end;
  611. if nodetype<>subn then
  612. begin
  613. if (right.location.loc >LOC_CONSTANT) then
  614. cg.a_op_reg_reg_reg_checkoverflow(exprasmlist,cgop,location.size,
  615. left.location.register,right.location.register,
  616. location.register,checkoverflow and (cs_check_overflow in aktlocalswitches),ovloc)
  617. else
  618. cg.a_op_const_reg_reg_checkoverflow(exprasmlist,cgop,location.size,
  619. right.location.value,left.location.register,
  620. location.register,checkoverflow and (cs_check_overflow in aktlocalswitches),ovloc);
  621. end
  622. else { subtract is a special case since its not commutative }
  623. begin
  624. if (nf_swaped in flags) then
  625. swapleftright;
  626. if left.location.loc<>LOC_CONSTANT then
  627. begin
  628. if right.location.loc<>LOC_CONSTANT then
  629. cg.a_op_reg_reg_reg_checkoverflow(exprasmlist,OP_SUB,location.size,
  630. right.location.register,left.location.register,
  631. location.register,checkoverflow and (cs_check_overflow in aktlocalswitches),ovloc)
  632. else
  633. cg.a_op_const_reg_reg_checkoverflow(exprasmlist,OP_SUB,location.size,
  634. aword(right.location.value),left.location.register,
  635. location.register,checkoverflow and (cs_check_overflow in aktlocalswitches),ovloc);
  636. end
  637. else
  638. begin
  639. tmpreg:=cg.getintregister(exprasmlist,location.size);
  640. cg.a_load_const_reg(exprasmlist,location.size,
  641. aword(left.location.value),tmpreg);
  642. cg.a_op_reg_reg_reg_checkoverflow(exprasmlist,OP_SUB,location.size,
  643. right.location.register,tmpreg,location.register,checkoverflow and (cs_check_overflow in aktlocalswitches),ovloc);
  644. end;
  645. end;
  646. { emit overflow check if required }
  647. if checkoverflow then
  648. cg.g_overflowcheck_loc(exprasmlist,Location,ResultType.Def,ovloc);
  649. end;
  650. procedure tcgaddnode.second_cmpboolean;
  651. begin
  652. second_cmpordinal;
  653. end;
  654. {*****************************************************************************
  655. pass_2
  656. *****************************************************************************}
  657. procedure tcgaddnode.pass_2;
  658. begin
  659. case left.resulttype.def.deftype of
  660. orddef :
  661. begin
  662. { handling boolean expressions }
  663. if is_boolean(left.resulttype.def) and
  664. is_boolean(right.resulttype.def) then
  665. second_opboolean
  666. { 64bit operations }
  667. else if is_64bit(left.resulttype.def) then
  668. second_op64bit
  669. else
  670. second_opordinal;
  671. end;
  672. stringdef :
  673. begin
  674. second_addstring;
  675. end;
  676. setdef :
  677. begin
  678. {Normalsets are already handled in pass1 if mmx
  679. should not be used.}
  680. if (tsetdef(left.resulttype.def).settype<>smallset) then
  681. begin
  682. {$ifdef SUPPORT_MMX}
  683. {$ifdef i386}
  684. if cs_mmx in aktlocalswitches then
  685. second_opmmxset
  686. else
  687. {$endif}
  688. {$endif SUPPORT_MMX}
  689. internalerror(200109041);
  690. end
  691. else
  692. second_opsmallset;
  693. end;
  694. arraydef :
  695. begin
  696. { support dynarr=nil }
  697. if is_dynamic_array(left.resulttype.def) then
  698. second_opordinal
  699. {$ifdef SUPPORT_MMX}
  700. else
  701. if is_mmx_able_array(left.resulttype.def) then
  702. second_opmmx
  703. {$endif SUPPORT_MMX}
  704. else
  705. internalerror(200306016);
  706. end;
  707. floatdef :
  708. second_opfloat;
  709. else
  710. second_opordinal;
  711. end;
  712. end;
  713. begin
  714. caddnode:=tcgaddnode;
  715. end.