ncgadd.pas 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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 we don't modify left/right.location, because we told
  277. force_reg_left_right above that they can be constant }
  278. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NOT,location.size,right.location.register,location.register);
  279. if left.location.loc = LOC_CONSTANT then
  280. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_AND,location.size,left.location.value,location.register)
  281. else
  282. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_AND,location.size,left.location.register,location.register);
  283. end;
  284. end;
  285. else
  286. internalerror(2002072701);
  287. end;
  288. if not opdone then
  289. begin
  290. // these are all commutative operations
  291. if (left.location.loc = LOC_CONSTANT) then
  292. swapleftright;
  293. if (right.location.loc = LOC_CONSTANT) then
  294. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,cgop,location.size,
  295. right.location.value,left.location.register,
  296. location.register)
  297. else
  298. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,cgop,location.size,
  299. right.location.register,left.location.register,
  300. location.register);
  301. end;
  302. end;
  303. procedure tcgaddnode.second_addsmallsetelement;
  304. var
  305. tmpreg : tregister;
  306. mask,
  307. setbase : aint;
  308. cgop : TOpCg;
  309. begin
  310. if nodetype<>addn then
  311. internalerror(20080302);
  312. { setelementn is a special case, it must be on right }
  313. if (nf_swapped in flags) and
  314. (left.nodetype=setelementn) then
  315. swapleftright;
  316. { no range support for smallsets }
  317. if assigned(tsetelementnode(right).right) then
  318. internalerror(20080303);
  319. pass_left_right;
  320. force_reg_left_right(false,false);
  321. set_result_location_reg;
  322. setbase:=tsetdef(left.resultdef).setbase;
  323. if (right.location.loc = LOC_CONSTANT) then
  324. begin
  325. if (target_info.endian=endian_big) then
  326. mask:=aint((aword(1) shl (resultdef.size*8-1)) shr aword(right.location.value-setbase))
  327. else
  328. mask:=aint(1 shl (right.location.value-setbase));
  329. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_OR,location.size,
  330. mask,left.location.register,location.register);
  331. end
  332. else
  333. begin
  334. if (target_info.endian=endian_big) then
  335. begin
  336. mask:=aint((aword(1) shl (resultdef.size*8-1)));
  337. cgop:=OP_SHR
  338. end
  339. else
  340. begin
  341. mask:=1;
  342. cgop:=OP_SHL
  343. end;
  344. tmpreg := cg.getintregister(current_asmdata.CurrAsmList,location.size);
  345. cg.a_load_const_reg(current_asmdata.CurrAsmList,location.size,mask,tmpreg);
  346. location_force_reg(current_asmdata.CurrAsmList,right.location,location.size,true);
  347. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,right.location,setbase);
  348. cg.a_op_reg_reg(current_asmdata.CurrAsmList,cgop,location.size,
  349. right.location.register,tmpreg);
  350. if left.location.loc <> LOC_CONSTANT then
  351. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_OR,location.size,tmpreg,
  352. left.location.register,location.register)
  353. else
  354. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_OR,location.size,
  355. left.location.value,tmpreg,location.register);
  356. end;
  357. end;
  358. {*****************************************************************************
  359. Boolean
  360. *****************************************************************************}
  361. procedure tcgaddnode.second_opboolean;
  362. begin
  363. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  364. second_cmpboolean
  365. else
  366. second_addboolean;
  367. end;
  368. procedure tcgaddnode.second_addboolean;
  369. var
  370. cgop : TOpCg;
  371. otl,ofl : tasmlabel;
  372. oldflowcontrol : tflowcontrol;
  373. begin
  374. { And,Or will only evaluate from left to right only the
  375. needed nodes unless full boolean evaluation is enabled }
  376. if (nodetype in [orn,andn]) and
  377. (not(cs_full_boolean_eval in current_settings.localswitches) or
  378. (nf_short_bool in flags)) then
  379. begin
  380. location_reset(location,LOC_JUMP,OS_NO);
  381. case nodetype of
  382. andn :
  383. begin
  384. otl:=current_procinfo.CurrTrueLabel;
  385. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  386. secondpass(left);
  387. maketojumpbool(current_asmdata.CurrAsmList,left,lr_load_regvars);
  388. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  389. current_procinfo.CurrTrueLabel:=otl;
  390. end;
  391. orn :
  392. begin
  393. ofl:=current_procinfo.CurrFalseLabel;
  394. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  395. secondpass(left);
  396. maketojumpbool(current_asmdata.CurrAsmList,left,lr_load_regvars);
  397. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  398. current_procinfo.CurrFalseLabel:=ofl;
  399. end;
  400. else
  401. internalerror(200307044);
  402. end;
  403. { these jumps mean we're now in a flow control construct }
  404. oldflowcontrol:=flowcontrol;
  405. include(flowcontrol,fc_inflowcontrol);
  406. secondpass(right);
  407. maketojumpbool(current_asmdata.CurrAsmList,right,lr_load_regvars);
  408. flowcontrol:=oldflowcontrol+(flowcontrol-[fc_inflowcontrol]);
  409. end
  410. else
  411. begin
  412. pass_left_right;
  413. force_reg_left_right(false,true);
  414. set_result_location_reg;
  415. case nodetype of
  416. xorn :
  417. cgop:=OP_XOR;
  418. orn :
  419. cgop:=OP_OR;
  420. andn :
  421. cgop:=OP_AND;
  422. else
  423. internalerror(200203247);
  424. end;
  425. {$ifndef cpu64bitalu}
  426. if right.location.size in [OS_64,OS_S64] then
  427. begin
  428. if right.location.loc <> LOC_CONSTANT then
  429. cg64.a_op64_reg_reg_reg(current_asmdata.CurrAsmList,cgop,location.size,
  430. left.location.register64,right.location.register64,
  431. location.register64)
  432. else
  433. cg64.a_op64_const_reg_reg(current_asmdata.CurrAsmList,cgop,location.size,
  434. right.location.value,left.location.register64,
  435. location.register64);
  436. end
  437. else
  438. {$endif cpu64bitalu}
  439. begin
  440. if right.location.loc <> LOC_CONSTANT then
  441. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,cgop,location.size,
  442. left.location.register,right.location.register,
  443. location.register)
  444. else
  445. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,cgop,location.size,
  446. right.location.value,left.location.register,
  447. location.register);
  448. end;
  449. end;
  450. end;
  451. {*****************************************************************************
  452. 64-bit
  453. *****************************************************************************}
  454. procedure tcgaddnode.second_op64bit;
  455. begin
  456. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  457. second_cmp64bit
  458. else
  459. second_add64bit;
  460. end;
  461. procedure tcgaddnode.second_add64bit;
  462. var
  463. op : TOpCG;
  464. checkoverflow : boolean;
  465. ovloc : tlocation;
  466. begin
  467. ovloc.loc:=LOC_VOID;
  468. pass_left_right;
  469. force_reg_left_right(false,true);
  470. set_result_location_reg;
  471. { assume no overflow checking is required }
  472. checkoverflow := false;
  473. case nodetype of
  474. addn :
  475. begin
  476. op:=OP_ADD;
  477. checkoverflow:=true;
  478. end;
  479. subn :
  480. begin
  481. op:=OP_SUB;
  482. checkoverflow:=true;
  483. end;
  484. xorn:
  485. op:=OP_XOR;
  486. orn:
  487. op:=OP_OR;
  488. andn:
  489. op:=OP_AND;
  490. muln:
  491. begin
  492. { should be handled in pass_1 (JM) }
  493. internalerror(200109051);
  494. end;
  495. else
  496. internalerror(2002072705);
  497. end;
  498. checkoverflow:=
  499. checkoverflow and
  500. (left.resultdef.typ<>pointerdef) and
  501. (right.resultdef.typ<>pointerdef);
  502. {$ifdef cpu64bitalu}
  503. case nodetype of
  504. xorn,orn,andn,addn:
  505. begin
  506. if (right.location.loc = LOC_CONSTANT) then
  507. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,op,location.size,right.location.value,
  508. left.location.register,location.register)
  509. else
  510. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,op,location.size,right.location.register,
  511. left.location.register,location.register);
  512. end;
  513. subn:
  514. begin
  515. if (nf_swapped in flags) then
  516. swapleftright;
  517. if left.location.loc <> LOC_CONSTANT then
  518. begin
  519. if right.location.loc <> LOC_CONSTANT then
  520. // reg64 - reg64
  521. cg.a_op_reg_reg_reg_checkoverflow(current_asmdata.CurrAsmList,OP_SUB,location.size,
  522. right.location.register,left.location.register,location.register,
  523. checkoverflow and (cs_check_overflow in current_settings.localswitches),ovloc)
  524. else
  525. // reg64 - const64
  526. cg.a_op_const_reg_reg_checkoverflow(current_asmdata.CurrAsmList,OP_SUB,location.size,
  527. right.location.value,left.location.register,location.register,
  528. checkoverflow and (cs_check_overflow in current_settings.localswitches),ovloc);
  529. end
  530. else
  531. begin
  532. // const64 - reg64
  533. location_force_reg(current_asmdata.CurrAsmList,left.location,left.location.size,true);
  534. cg.a_op_reg_reg_reg_checkoverflow(current_asmdata.CurrAsmList,OP_SUB,location.size,
  535. right.location.register,left.location.register,location.register,
  536. checkoverflow and (cs_check_overflow in current_settings.localswitches),ovloc);
  537. end;
  538. end;
  539. else
  540. internalerror(2002072803);
  541. end;
  542. {$else cpu64bitalu}
  543. case nodetype of
  544. xorn,orn,andn,addn:
  545. begin
  546. if (right.location.loc = LOC_CONSTANT) then
  547. cg64.a_op64_const_reg_reg_checkoverflow(current_asmdata.CurrAsmList,op,location.size,right.location.value64,
  548. left.location.register64,location.register64,
  549. checkoverflow and (cs_check_overflow in current_settings.localswitches),ovloc)
  550. else
  551. cg64.a_op64_reg_reg_reg_checkoverflow(current_asmdata.CurrAsmList,op,location.size,right.location.register64,
  552. left.location.register64,location.register64,
  553. checkoverflow and (cs_check_overflow in current_settings.localswitches),ovloc);
  554. end;
  555. subn:
  556. begin
  557. if (nf_swapped in flags) then
  558. swapleftright;
  559. if left.location.loc <> LOC_CONSTANT then
  560. begin
  561. if right.location.loc <> LOC_CONSTANT then
  562. // reg64 - reg64
  563. cg64.a_op64_reg_reg_reg_checkoverflow(current_asmdata.CurrAsmList,OP_SUB,location.size,
  564. right.location.register64,left.location.register64,
  565. location.register64,
  566. checkoverflow and (cs_check_overflow in current_settings.localswitches),ovloc)
  567. else
  568. // reg64 - const64
  569. cg64.a_op64_const_reg_reg_checkoverflow(current_asmdata.CurrAsmList,OP_SUB,location.size,
  570. right.location.value64,left.location.register64,
  571. location.register64,
  572. checkoverflow and (cs_check_overflow in current_settings.localswitches),ovloc)
  573. end
  574. else
  575. begin
  576. // const64 - reg64
  577. location_force_reg(current_asmdata.CurrAsmList,left.location,left.location.size,true);
  578. cg64.a_op64_reg_reg_reg_checkoverflow(current_asmdata.CurrAsmList,OP_SUB,location.size,
  579. right.location.register64,left.location.register64,
  580. location.register64,
  581. checkoverflow and (cs_check_overflow in current_settings.localswitches),ovloc);
  582. end;
  583. end;
  584. else
  585. internalerror(2002072803);
  586. end;
  587. {$endif cpu64bitalu}
  588. { emit overflow check if enabled }
  589. if checkoverflow then
  590. cg.g_overflowcheck_loc(current_asmdata.CurrAsmList,Location,resultdef,ovloc);
  591. end;
  592. {*****************************************************************************
  593. Strings
  594. *****************************************************************************}
  595. procedure tcgaddnode.second_addstring;
  596. begin
  597. { this should already be handled in pass1 }
  598. internalerror(2002072402);
  599. end;
  600. {*****************************************************************************
  601. Floats
  602. *****************************************************************************}
  603. procedure tcgaddnode.second_opfloat;
  604. begin
  605. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  606. second_cmpfloat
  607. else
  608. second_addfloat;
  609. end;
  610. {*****************************************************************************
  611. Ordinals
  612. *****************************************************************************}
  613. procedure tcgaddnode.second_opordinal;
  614. begin
  615. if (nodetype in [ltn,lten,gtn,gten,equaln,unequaln]) then
  616. second_cmpordinal
  617. else
  618. second_addordinal;
  619. end;
  620. procedure tcgaddnode.second_addordinal;
  621. var
  622. unsigned,
  623. checkoverflow : boolean;
  624. cgop : topcg;
  625. tmpreg : tregister;
  626. ovloc : tlocation;
  627. begin
  628. ovloc.loc:=LOC_VOID;
  629. pass_left_right;
  630. force_reg_left_right(false,true);
  631. set_result_location_reg;
  632. { determine if the comparison will be unsigned }
  633. unsigned:=not(is_signed(left.resultdef)) or
  634. not(is_signed(right.resultdef));
  635. { assume no overflow checking is require }
  636. checkoverflow := false;
  637. case nodetype of
  638. addn:
  639. begin
  640. cgop:=OP_ADD;
  641. checkoverflow:=true;
  642. end;
  643. xorn :
  644. begin
  645. cgop:=OP_XOR;
  646. end;
  647. orn :
  648. begin
  649. cgop:=OP_OR;
  650. end;
  651. andn:
  652. begin
  653. cgop:=OP_AND;
  654. end;
  655. muln:
  656. begin
  657. checkoverflow:=true;
  658. if unsigned then
  659. cgop:=OP_MUL
  660. else
  661. cgop:=OP_IMUL;
  662. end;
  663. subn :
  664. begin
  665. checkoverflow:=true;
  666. cgop:=OP_SUB;
  667. end;
  668. end;
  669. checkoverflow:=
  670. checkoverflow and
  671. (left.resultdef.typ<>pointerdef) and
  672. (right.resultdef.typ<>pointerdef);
  673. if nodetype<>subn then
  674. begin
  675. if (right.location.loc<>LOC_CONSTANT) then
  676. cg.a_op_reg_reg_reg_checkoverflow(current_asmdata.CurrAsmList,cgop,location.size,
  677. left.location.register,right.location.register,
  678. location.register,checkoverflow and (cs_check_overflow in current_settings.localswitches),ovloc)
  679. else
  680. cg.a_op_const_reg_reg_checkoverflow(current_asmdata.CurrAsmList,cgop,location.size,
  681. right.location.value,left.location.register,
  682. location.register,checkoverflow and (cs_check_overflow in current_settings.localswitches),ovloc);
  683. end
  684. else { subtract is a special case since its not commutative }
  685. begin
  686. if (nf_swapped in flags) then
  687. swapleftright;
  688. if left.location.loc<>LOC_CONSTANT then
  689. begin
  690. if right.location.loc<>LOC_CONSTANT then
  691. cg.a_op_reg_reg_reg_checkoverflow(current_asmdata.CurrAsmList,OP_SUB,location.size,
  692. right.location.register,left.location.register,
  693. location.register,checkoverflow and (cs_check_overflow in current_settings.localswitches),ovloc)
  694. else
  695. cg.a_op_const_reg_reg_checkoverflow(current_asmdata.CurrAsmList,OP_SUB,location.size,
  696. right.location.value,left.location.register,
  697. location.register,checkoverflow and (cs_check_overflow in current_settings.localswitches),ovloc);
  698. end
  699. else
  700. begin
  701. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  702. cg.a_load_const_reg(current_asmdata.CurrAsmList,location.size,
  703. left.location.value,tmpreg);
  704. cg.a_op_reg_reg_reg_checkoverflow(current_asmdata.CurrAsmList,OP_SUB,location.size,
  705. right.location.register,tmpreg,location.register,checkoverflow and (cs_check_overflow in current_settings.localswitches),ovloc);
  706. end;
  707. end;
  708. { emit overflow check if required }
  709. if checkoverflow then
  710. cg.g_overflowcheck_loc(current_asmdata.CurrAsmList,Location,resultdef,ovloc);
  711. end;
  712. procedure tcgaddnode.second_cmpboolean;
  713. begin
  714. second_cmpordinal;
  715. end;
  716. {*****************************************************************************
  717. pass_generate_code;
  718. *****************************************************************************}
  719. procedure tcgaddnode.pass_generate_code;
  720. begin
  721. case left.resultdef.typ of
  722. orddef :
  723. begin
  724. { handling boolean expressions }
  725. if is_boolean(left.resultdef) and
  726. is_boolean(right.resultdef) then
  727. second_opboolean
  728. { 64bit operations }
  729. else if is_64bit(left.resultdef) then
  730. second_op64bit
  731. else
  732. second_opordinal;
  733. end;
  734. stringdef :
  735. begin
  736. second_addstring;
  737. end;
  738. setdef :
  739. begin
  740. if is_smallset(tsetdef(left.resultdef)) then
  741. second_opsmallset
  742. else
  743. internalerror(200109041);
  744. end;
  745. arraydef :
  746. begin
  747. { support dynarr=nil }
  748. if is_dynamic_array(left.resultdef) then
  749. second_opordinal
  750. else
  751. if (cs_support_vectors in current_settings.globalswitches) and
  752. is_vector(left.resultdef) then
  753. second_opvector
  754. {$ifdef SUPPORT_MMX}
  755. else
  756. if is_mmx_able_array(left.resultdef) then
  757. second_opmmx
  758. {$endif SUPPORT_MMX}
  759. else
  760. internalerror(200306016);
  761. end;
  762. floatdef :
  763. second_opfloat;
  764. else
  765. second_opordinal;
  766. end;
  767. end;
  768. begin
  769. caddnode:=tcgaddnode;
  770. end.