ncgadd.pas 30 KB

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