ncgadd.pas 30 KB

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