ncgadd.pas 28 KB

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