ncgadd.pas 30 KB

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