ncgadd.pas 31 KB

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