ncgadd.pas 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. {
  2. $Id$
  3. Copyright (c) 2000-2002 by the FPC development team
  4. Code generation for add nodes (generic version)
  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. }
  18. unit ncgadd;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,nadd,cpubase,cginfo;
  23. type
  24. tcgaddnode = class(taddnode)
  25. { function pass_1: tnode; override;}
  26. procedure pass_2;override;
  27. protected
  28. procedure pass_left_and_right;
  29. { load left and right nodes into registers }
  30. procedure load_left_right(cmpop, load_constants: boolean);
  31. { free used registers, except result location }
  32. procedure clear_left_right(cmpop: boolean);
  33. procedure second_opfloat;
  34. procedure second_opboolean;
  35. procedure second_opsmallset;
  36. procedure second_op64bit;
  37. { procedure second_addfloat;virtual;}
  38. procedure second_addboolean;virtual;
  39. procedure second_addsmallset;virtual;
  40. procedure second_add64bit;virtual;
  41. procedure second_addordinal;virtual;
  42. { procedure second_cmpfloat;virtual;}
  43. procedure second_cmpboolean;virtual;abstract;
  44. procedure second_cmpsmallset;virtual;abstract;
  45. procedure second_cmp64bit;virtual;abstract;
  46. procedure second_cmpordinal;virtual;abstract;
  47. end;
  48. implementation
  49. uses
  50. globtype,systems,
  51. cutils,verbose,globals,
  52. symconst,symdef,paramgr,
  53. aasmbase,aasmtai,aasmcpu,defutil,htypechk,
  54. cgbase,cpuinfo,pass_1,pass_2,regvars,
  55. cpupara,
  56. ncon,nset,ncgutil,tgobj,rgobj,rgcpu,cgobj,
  57. {$ifdef cpu64bit}
  58. cg64f64
  59. {$else cpu64bit}
  60. cg64f32
  61. {$endif cpu64bit}
  62. ;
  63. {*****************************************************************************
  64. Helpers
  65. *****************************************************************************}
  66. procedure tcgaddnode.pass_left_and_right;
  67. var
  68. pushedregs : tmaybesave;
  69. tmpreg : tregister;
  70. pushedfpu : boolean;
  71. begin
  72. { calculate the operator which is more difficult }
  73. firstcomplex(self);
  74. { in case of constant put it to the left }
  75. if (left.nodetype=ordconstn) then
  76. swapleftright;
  77. secondpass(left);
  78. { are too few registers free? }
  79. maybe_save(exprasmlist,right.registers32,left.location,pushedregs);
  80. if location.loc=LOC_FPUREGISTER then
  81. pushedfpu:=maybe_pushfpu(exprasmlist,right.registersfpu,left.location)
  82. else
  83. pushedfpu:=false;
  84. secondpass(right);
  85. maybe_restore(exprasmlist,left.location,pushedregs);
  86. if pushedfpu then
  87. begin
  88. tmpreg := rg.getregisterfpu(exprasmlist);
  89. cg.a_loadfpu_loc_reg(exprasmlist,left.location,tmpreg);
  90. location_reset(left.location,LOC_FPUREGISTER,left.location.size);
  91. left.location.register := tmpreg;
  92. end;
  93. end;
  94. procedure tcgaddnode.load_left_right(cmpop, load_constants: boolean);
  95. procedure load_node(var n: tnode);
  96. begin
  97. case n.location.loc of
  98. LOC_REGISTER:
  99. if not cmpop then
  100. begin
  101. location.register := n.location.register;
  102. if is_64bit(n.resulttype.def) then
  103. location.registerhigh := n.location.registerhigh;
  104. end;
  105. LOC_REFERENCE,LOC_CREFERENCE:
  106. begin
  107. location_force_reg(exprasmlist,n.location,def_cgsize(n.resulttype.def),false);
  108. if not cmpop then
  109. begin
  110. location.register := n.location.register;
  111. if is_64bit(n.resulttype.def) then
  112. location.registerhigh := n.location.registerhigh;
  113. end;
  114. end;
  115. LOC_CONSTANT:
  116. begin
  117. if load_constants then
  118. begin
  119. location_force_reg(exprasmlist,n.location,def_cgsize(n.resulttype.def),false);
  120. if not cmpop then
  121. location.register := n.location.register;
  122. if is_64bit(n.resulttype.def) then
  123. location.registerhigh := n.location.registerhigh;
  124. end;
  125. end;
  126. end;
  127. end;
  128. begin
  129. load_node(left);
  130. load_node(right);
  131. end;
  132. procedure tcgaddnode.clear_left_right(cmpop: boolean);
  133. begin
  134. if (right.location.loc in [LOC_REGISTER,LOC_FPUREGISTER]) and
  135. (cmpop or
  136. (location.register.enum <> right.location.register.enum)) then
  137. begin
  138. rg.ungetregister(exprasmlist,right.location.register);
  139. if is_64bit(right.resulttype.def) then
  140. rg.ungetregister(exprasmlist,right.location.registerhigh);
  141. end;
  142. if (left.location.loc in [LOC_REGISTER,LOC_FPUREGISTER]) and
  143. (cmpop or
  144. (location.register.enum <> left.location.register.enum)) then
  145. begin
  146. rg.ungetregister(exprasmlist,left.location.register);
  147. if is_64bit(left.resulttype.def) then
  148. rg.ungetregister(exprasmlist,left.location.registerhigh);
  149. end;
  150. end;
  151. {*****************************************************************************
  152. Smallsets
  153. *****************************************************************************}
  154. procedure tcgaddnode.second_opsmallset;
  155. var
  156. cmpop : boolean;
  157. begin
  158. cmpop := false;
  159. pass_left_and_right;
  160. { when a setdef is passed, it has to be a smallset }
  161. if ((left.resulttype.def.deftype=setdef) and
  162. (tsetdef(left.resulttype.def).settype<>smallset)) or
  163. ((right.resulttype.def.deftype=setdef) and
  164. (tsetdef(right.resulttype.def).settype<>smallset)) then
  165. internalerror(200203301);
  166. if nodetype in [equaln,unequaln,gtn,gten,lten,ltn] then
  167. cmpop := true;
  168. { load non-constant values (left and right) into registers }
  169. load_left_right(cmpop,false);
  170. if cmpop then
  171. second_cmpsmallset
  172. else
  173. second_addsmallset;
  174. clear_left_right(cmpop);
  175. end;
  176. procedure tcgaddnode.second_addsmallset;
  177. var
  178. cgop : TOpCg;
  179. tmpreg : tregister;
  180. opdone,
  181. cmpop : boolean;
  182. size:Tcgsize;
  183. begin
  184. opdone := false;
  185. size:=def_cgsize(resulttype.def);
  186. location_reset(location,LOC_REGISTER,size);
  187. if (location.register.enum = R_NO) then
  188. location.register := rg.getregisterint(exprasmlist,size);
  189. case nodetype of
  190. addn :
  191. begin
  192. if (nf_swaped in flags) and (left.nodetype=setelementn) then
  193. swapleftright;
  194. { are we adding set elements ? }
  195. if right.nodetype=setelementn then
  196. begin
  197. { no range support for smallsets! }
  198. if assigned(tsetelementnode(right).right) then
  199. internalerror(43244);
  200. if (right.location.loc = LOC_CONSTANT) then
  201. cg.a_op_const_reg_reg(exprasmlist,OP_OR,OS_INT,
  202. aword(1 shl aword(right.location.value)),
  203. left.location.register,location.register)
  204. else
  205. begin
  206. tmpreg := cg.get_scratch_reg_int(exprasmlist,size);
  207. cg.a_load_const_reg(exprasmlist,OS_INT,1,tmpreg);
  208. cg.a_op_reg_reg(exprasmlist,OP_SHL,OS_INT,
  209. right.location.register,tmpreg);
  210. if left.location.loc <> LOC_CONSTANT then
  211. cg.a_op_reg_reg_reg(exprasmlist,OP_OR,OS_INT,tmpreg,
  212. left.location.register,location.register)
  213. else
  214. cg.a_op_const_reg_reg(exprasmlist,OP_OR,OS_INT,
  215. aword(left.location.value),tmpreg,location.register);
  216. cg.free_scratch_reg(exprasmlist,tmpreg);
  217. end;
  218. opdone := true;
  219. end
  220. else
  221. cgop := OP_OR;
  222. end;
  223. symdifn :
  224. cgop:=OP_XOR;
  225. muln :
  226. cgop:=OP_AND;
  227. subn :
  228. begin
  229. cgop:=OP_AND;
  230. if (not(nf_swaped in flags)) then
  231. if (right.location.loc=LOC_CONSTANT) then
  232. right.location.value := not(right.location.value)
  233. else
  234. opdone := true
  235. else if (left.location.loc=LOC_CONSTANT) then
  236. left.location.value := not(left.location.value)
  237. else
  238. begin
  239. swapleftright;
  240. opdone := true;
  241. end;
  242. if opdone then
  243. begin
  244. if left.location.loc = LOC_CONSTANT then
  245. begin
  246. tmpreg := cg.get_scratch_reg_int(exprasmlist,OS_INT);
  247. cg.a_load_const_reg(exprasmlist,OS_INT,
  248. aword(left.location.value),tmpreg);
  249. cg.a_op_reg_reg(exprasmlist,OP_NOT,OS_INT,right.location.register,right.location.register);
  250. cg.a_op_reg_reg(exprasmlist,OP_AND,OS_INT,right.location.register,tmpreg);
  251. cg.a_load_reg_reg(exprasmlist,OS_INT,OS_INT,tmpreg,location.register);
  252. cg.free_scratch_reg(exprasmlist,tmpreg);
  253. end
  254. else
  255. begin
  256. cg.a_op_reg_reg(exprasmlist,OP_NOT,OS_INT,right.location.register,right.location.register);
  257. cg.a_op_reg_reg(exprasmlist,OP_AND,OS_INT,right.location.register,left.location.register);
  258. cg.a_load_reg_reg(exprasmlist,OS_INT,OS_INT,left.location.register,location.register);
  259. end;
  260. end;
  261. end;
  262. else
  263. internalerror(2002072701);
  264. end;
  265. if not opdone then
  266. begin
  267. // these are all commutative operations
  268. if (left.location.loc = LOC_CONSTANT) then
  269. swapleftright;
  270. if (right.location.loc = LOC_CONSTANT) then
  271. cg.a_op_const_reg_reg(exprasmlist,cgop,OS_INT,
  272. aword(right.location.value),left.location.register,
  273. location.register)
  274. else
  275. cg.a_op_reg_reg_reg(exprasmlist,cgop,OS_INT,
  276. right.location.register,left.location.register,
  277. location.register);
  278. end;
  279. end;
  280. {*****************************************************************************
  281. Boolean
  282. *****************************************************************************}
  283. procedure tcgaddnode.second_opboolean;
  284. var
  285. cmpop : boolean;
  286. begin
  287. cmpop := false;
  288. { calculate the operator which is more difficult }
  289. firstcomplex(self);
  290. cmpop := nodetype in [ltn,lten,gtn,gten,equaln,unequaln];
  291. if cmpop then
  292. second_cmpboolean
  293. else
  294. second_addboolean;
  295. end;
  296. procedure tcgaddnode.second_addboolean;
  297. var
  298. cgop : TOpCg;
  299. cgsize : TCgSize;
  300. isjump : boolean;
  301. otl,ofl : tasmlabel;
  302. pushedregs : tmaybesave;
  303. begin
  304. if (torddef(left.resulttype.def).typ=bool8bit) or
  305. (torddef(right.resulttype.def).typ=bool8bit) then
  306. cgsize:=OS_8
  307. else
  308. if (torddef(left.resulttype.def).typ=bool16bit) or
  309. (torddef(right.resulttype.def).typ=bool16bit) then
  310. cgsize:=OS_16
  311. else
  312. cgsize:=OS_32;
  313. if (cs_full_boolean_eval in aktlocalswitches) or
  314. (nodetype in [unequaln,ltn,lten,gtn,gten,equaln,xorn]) then
  315. begin
  316. if left.nodetype in [ordconstn,realconstn] then
  317. swapleftright;
  318. isjump:=(left.location.loc=LOC_JUMP);
  319. if isjump then
  320. begin
  321. otl:=truelabel;
  322. objectlibrary.getlabel(truelabel);
  323. ofl:=falselabel;
  324. objectlibrary.getlabel(falselabel);
  325. end;
  326. secondpass(left);
  327. if left.location.loc in [LOC_FLAGS,LOC_JUMP] then
  328. location_force_reg(exprasmlist,left.location,cgsize,false);
  329. if isjump then
  330. begin
  331. truelabel:=otl;
  332. falselabel:=ofl;
  333. end;
  334. maybe_save(exprasmlist,right.registers32,left.location,pushedregs);
  335. isjump:=(right.location.loc=LOC_JUMP);
  336. if isjump then
  337. begin
  338. otl:=truelabel;
  339. objectlibrary.getlabel(truelabel);
  340. ofl:=falselabel;
  341. objectlibrary.getlabel(falselabel);
  342. end;
  343. secondpass(right);
  344. maybe_restore(exprasmlist,left.location,pushedregs);
  345. if right.location.loc in [LOC_FLAGS,LOC_JUMP] then
  346. location_force_reg(exprasmlist,right.location,cgsize,false);
  347. if isjump then
  348. begin
  349. truelabel:=otl;
  350. falselabel:=ofl;
  351. end;
  352. { set result location }
  353. location_reset(location,LOC_REGISTER,def_cgsize(resulttype.def));
  354. load_left_right(false,false);
  355. if (left.location.loc = LOC_CONSTANT) then
  356. swapleftright;
  357. case nodetype of
  358. xorn :
  359. cgop:=OP_XOR;
  360. orn :
  361. cgop:=OP_OR;
  362. andn :
  363. cgop:=OP_AND;
  364. else
  365. internalerror(200203247);
  366. end;
  367. if right.location.loc <> LOC_CONSTANT then
  368. cg.a_op_reg_reg_reg(exprasmlist,cgop,OS_INT,
  369. left.location.register,right.location.register,
  370. location.register)
  371. else
  372. cg.a_op_const_reg_reg(exprasmlist,cgop,OS_INT,
  373. aword(right.location.value),left.location.register,
  374. location.register);
  375. end
  376. else
  377. begin
  378. case nodetype of
  379. andn,
  380. orn :
  381. begin
  382. location_reset(location,LOC_JUMP,OS_NO);
  383. case nodetype of
  384. andn :
  385. begin
  386. otl:=truelabel;
  387. objectlibrary.getlabel(truelabel);
  388. secondpass(left);
  389. maketojumpbool(exprasmlist,left,lr_load_regvars);
  390. cg.a_label(exprasmlist,truelabel);
  391. truelabel:=otl;
  392. end;
  393. orn :
  394. begin
  395. ofl:=falselabel;
  396. objectlibrary.getlabel(falselabel);
  397. secondpass(left);
  398. maketojumpbool(exprasmlist,left,lr_load_regvars);
  399. cg.a_label(exprasmlist,falselabel);
  400. falselabel:=ofl;
  401. end;
  402. end;
  403. secondpass(right);
  404. maketojumpbool(exprasmlist,right,lr_load_regvars);
  405. end;
  406. end;
  407. end;
  408. { free used register (except the result register) }
  409. clear_left_right(true);
  410. end;
  411. {*****************************************************************************
  412. 64-bit
  413. *****************************************************************************}
  414. procedure tcgaddnode.second_op64bit;
  415. var
  416. cmpop : boolean;
  417. begin
  418. cmpop := false;
  419. firstcomplex(self);
  420. pass_left_and_right;
  421. if nodetype in [equaln,unequaln,gtn,gten,ltn,lten] then
  422. cmpop := true;
  423. if cmpop then
  424. second_cmp64bit
  425. else
  426. second_add64bit;
  427. { free used register (except the result register) }
  428. clear_left_right(cmpop);
  429. end;
  430. procedure tcgaddnode.second_add64bit;
  431. var
  432. op : TOpCG;
  433. unsigned : boolean;
  434. checkoverflow : boolean;
  435. begin
  436. unsigned:=((left.resulttype.def.deftype=orddef) and
  437. (torddef(left.resulttype.def).typ=u64bit)) or
  438. ((right.resulttype.def.deftype=orddef) and
  439. (torddef(right.resulttype.def).typ=u64bit));
  440. { assume no overflow checking is required }
  441. checkoverflow := false;
  442. case nodetype of
  443. addn :
  444. begin
  445. op:=OP_ADD;
  446. checkoverflow := true;
  447. end;
  448. subn :
  449. begin
  450. op:=OP_SUB;
  451. checkoverflow := true;
  452. end;
  453. xorn:
  454. op:=OP_XOR;
  455. orn:
  456. op:=OP_OR;
  457. andn:
  458. op:=OP_AND;
  459. muln:
  460. begin
  461. { should be handled in pass_1 (JM) }
  462. internalerror(200109051);
  463. end;
  464. else
  465. internalerror(2002072705);
  466. end;
  467. location_reset(location,LOC_REGISTER,def_cgsize(resulttype.def));
  468. load_left_right(false,(cs_check_overflow in aktlocalswitches) and
  469. (nodetype in [addn,subn]));
  470. case nodetype of
  471. xorn,orn,andn,addn:
  472. begin
  473. if (location.registerlow.enum = R_NO) then
  474. begin
  475. location.registerlow := rg.getregisterint(exprasmlist,OS_INT);
  476. location.registerhigh := rg.getregisterint(exprasmlist,OS_INT);
  477. end;
  478. if (left.location.loc = LOC_CONSTANT) then
  479. swapleftright;
  480. if (right.location.loc = LOC_CONSTANT) then
  481. cg64.a_op64_const_reg_reg(exprasmlist,op,right.location.valueqword,
  482. left.location.register64,location.register64)
  483. else
  484. cg64.a_op64_reg_reg_reg(exprasmlist,op,right.location.register64,
  485. left.location.register64,location.register64);
  486. end;
  487. subn:
  488. begin
  489. if (nf_swaped in flags) then
  490. swapleftright;
  491. if left.location.loc <> LOC_CONSTANT then
  492. begin
  493. if (location.registerlow.enum = R_NO) then
  494. begin
  495. location.registerlow := rg.getregisterint(exprasmlist,OS_INT);
  496. location.registerhigh := rg.getregisterint(exprasmlist,OS_INT);
  497. end;
  498. if right.location.loc <> LOC_CONSTANT then
  499. // reg64 - reg64
  500. cg64.a_op64_reg_reg_reg(exprasmlist,OP_SUB,
  501. right.location.register64,left.location.register64,
  502. location.register64)
  503. else
  504. // reg64 - const64
  505. cg64.a_op64_const_reg_reg(exprasmlist,OP_SUB,
  506. right.location.valueqword,left.location.register64,
  507. location.register64)
  508. end
  509. else
  510. begin
  511. // const64 - reg64
  512. location_force_reg(exprasmlist,left.location,
  513. def_cgsize(left.resulttype.def),true);
  514. if (left.location.loc = LOC_REGISTER) then
  515. location.register64 := left.location.register64
  516. else if (location.registerlow.enum = R_NO) then
  517. begin
  518. location.registerlow := rg.getregisterint(exprasmlist,OS_INT);
  519. location.registerhigh := rg.getregisterint(exprasmlist,OS_INT);
  520. end;
  521. cg64.a_op64_reg_reg_reg(exprasmlist,OP_SUB,
  522. right.location.register64,left.location.register64,
  523. location.register64);
  524. end;
  525. end;
  526. else
  527. internalerror(2002072803);
  528. end;
  529. { emit overflow check if enabled }
  530. if checkoverflow then
  531. cg.g_overflowcheck(exprasmlist,self);
  532. end;
  533. {*****************************************************************************
  534. Floats
  535. *****************************************************************************}
  536. procedure tcgaddnode.second_opfloat;
  537. begin
  538. end;
  539. {*****************************************************************************
  540. Ordinals
  541. *****************************************************************************}
  542. procedure tcgaddnode.second_addordinal;
  543. var
  544. unsigned : boolean;
  545. checkoverflow : boolean;
  546. cgop : topcg;
  547. tmpreg : tregister;
  548. size:Tcgsize;
  549. begin
  550. size:=def_cgsize(resulttype.def);
  551. { set result location }
  552. location_reset(location,LOC_REGISTER,size);
  553. { determine if the comparison will be unsigned }
  554. unsigned:=not(is_signed(left.resulttype.def)) or
  555. not(is_signed(right.resulttype.def));
  556. { load values into registers }
  557. load_left_right(false, (cs_check_overflow in aktlocalswitches) and
  558. (nodetype in [addn,subn,muln]));
  559. if (location.register.enum = R_NO) then
  560. location.register := rg.getregisterint(exprasmlist,OS_INT);
  561. { assume no overflow checking is require }
  562. checkoverflow := false;
  563. case nodetype of
  564. addn:
  565. begin
  566. cgop := OP_ADD;
  567. checkoverflow := true;
  568. end;
  569. xorn :
  570. begin
  571. cgop := OP_XOR;
  572. end;
  573. orn :
  574. begin
  575. cgop := OP_OR;
  576. end;
  577. andn:
  578. begin
  579. cgop := OP_AND;
  580. end;
  581. muln:
  582. begin
  583. checkoverflow := true;
  584. if unsigned then
  585. cgop := OP_MUL
  586. else
  587. cgop := OP_IMUL;
  588. end;
  589. subn :
  590. begin
  591. checkoverflow := true;
  592. cgop := OP_SUB;
  593. end;
  594. end;
  595. if nodetype <> subn then
  596. begin
  597. if (left.location.loc = LOC_CONSTANT) then
  598. swapleftright;
  599. if (right.location.loc <> LOC_CONSTANT) then
  600. cg.a_op_reg_reg_reg(exprasmlist,cgop,OS_INT,
  601. left.location.register,right.location.register,
  602. location.register)
  603. else
  604. cg.a_op_const_reg_reg(exprasmlist,cgop,OS_INT,
  605. aword(right.location.value),left.location.register,
  606. location.register);
  607. end
  608. else { subtract is a special case since its not commutative }
  609. begin
  610. if (nf_swaped in flags) then
  611. swapleftright;
  612. if left.location.loc <> LOC_CONSTANT then
  613. begin
  614. if right.location.loc <> LOC_CONSTANT then
  615. cg.a_op_reg_reg_reg(exprasmlist,OP_SUB,OS_INT,
  616. right.location.register,left.location.register,
  617. location.register)
  618. else
  619. cg.a_op_const_reg_reg(exprasmlist,OP_SUB,OS_INT,
  620. aword(right.location.value),left.location.register,
  621. location.register);
  622. end
  623. else
  624. begin
  625. tmpreg := cg.get_scratch_reg_int(exprasmlist,OS_INT);
  626. cg.a_load_const_reg(exprasmlist,OS_INT,
  627. aword(left.location.value),tmpreg);
  628. cg.a_op_reg_reg_reg(exprasmlist,OP_SUB,OS_INT,
  629. right.location.register,tmpreg,location.register);
  630. cg.free_scratch_reg(exprasmlist,tmpreg);
  631. end;
  632. end;
  633. { emit overflow check if required }
  634. if checkoverflow then
  635. cg.g_overflowcheck(exprasmlist,self);
  636. end;
  637. {*****************************************************************************
  638. pass_2
  639. *****************************************************************************}
  640. procedure tcgaddnode.pass_2;
  641. { is also being used for xor, and "mul", "sub, or and comparative }
  642. { operators }
  643. var
  644. cmpop : boolean;
  645. cgop : topcg;
  646. op : tasmop;
  647. tmpreg : tregister;
  648. { true, if unsigned types are compared }
  649. unsigned : boolean;
  650. regstopush: tregisterset;
  651. begin
  652. { to make it more readable, string and set (not smallset!) have their
  653. own procedures }
  654. case left.resulttype.def.deftype of
  655. orddef :
  656. begin
  657. { handling boolean expressions }
  658. if is_boolean(left.resulttype.def) and
  659. is_boolean(right.resulttype.def) then
  660. begin
  661. second_opboolean;
  662. exit;
  663. end
  664. { 64bit operations }
  665. else if is_64bit(left.resulttype.def) then
  666. begin
  667. second_op64bit;
  668. exit;
  669. end;
  670. end;
  671. stringdef :
  672. begin
  673. { this should already be handled in pass1 }
  674. internalerror(2002072402);
  675. exit;
  676. end;
  677. setdef :
  678. begin
  679. { normalsets are already handled in pass1 }
  680. if (tsetdef(left.resulttype.def).settype<>smallset) then
  681. internalerror(200109041);
  682. second_opsmallset;
  683. exit;
  684. end;
  685. arraydef :
  686. begin
  687. {$ifdef SUPPORT_MMX}
  688. if is_mmx_able_array(left.resulttype.def) then
  689. begin
  690. second_opmmx;
  691. exit;
  692. end;
  693. {$endif SUPPORT_MMX}
  694. end;
  695. floatdef :
  696. begin
  697. second_opfloat;
  698. exit;
  699. end;
  700. end;
  701. {*********************** ordinals / integrals *******************}
  702. cmpop:=nodetype in [ltn,lten,gtn,gten,equaln,unequaln];
  703. { normally nothing should be in flags }
  704. if (left.location.loc = LOC_FLAGS) or
  705. (right.location.loc = LOC_FLAGS) then
  706. internalerror(2002072602);
  707. pass_left_and_right;
  708. if cmpop then
  709. second_cmpordinal
  710. else
  711. second_addordinal;
  712. { free used register (except the result register) }
  713. clear_left_right(cmpop);
  714. end;
  715. begin
  716. caddnode:=tcgaddnode;
  717. end.
  718. {
  719. $Log$
  720. Revision 1.9 2003-04-30 22:15:59 florian
  721. * some 64 bit adaptions in ncgadd
  722. * x86-64 now uses ncgadd
  723. * tparamanager.ret_in_acc doesn't return true anymore for a void-def
  724. Revision 1.8 2003/04/23 20:16:04 peter
  725. + added currency support based on int64
  726. + is_64bit for use in cg units instead of is_64bitint
  727. * removed cgmessage from n386add, replace with internalerrors
  728. Revision 1.7 2003/04/22 23:50:22 peter
  729. * firstpass uses expectloc
  730. * checks if there are differences between the expectloc and
  731. location.loc from secondpass in EXTDEBUG
  732. Revision 1.6 2003/02/19 22:00:14 daniel
  733. * Code generator converted to new register notation
  734. - Horribily outdated todo.txt removed
  735. Revision 1.5 2003/02/02 19:25:54 carl
  736. * Several bugfixes for m68k target (register alloc., opcode emission)
  737. + VIS target
  738. + Generic add more complete (still not verified)
  739. Revision 1.4 2003/01/08 18:43:56 daniel
  740. * Tregister changed into a record
  741. Revision 1.3 2002/12/14 15:02:03 carl
  742. * maxoperands -> max_operands (for portability in rautils.pas)
  743. * fix some range-check errors with loadconst
  744. + add ncgadd unit to m68k
  745. * some bugfix of a_param_reg with LOC_CREFERENCE
  746. Revision 1.2 2002/12/08 15:02:17 carl
  747. + more fixes
  748. Revision 1.1 2002/12/07 19:51:35 carl
  749. + first version (uncompilable!)
  750. }