ncgadd.pas 31 KB

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