ncgadd.pas 29 KB

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