ncgadd.pas 30 KB

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