ncgadd.pas 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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. end;
  58. implementation
  59. uses
  60. globtype,systems,
  61. verbose,globals,
  62. symconst,symdef,
  63. aasmbase,aasmdata,defutil,
  64. pass_2,tgobj,
  65. nutils,nset,ncgutil,cgobj,cgutils,
  66. hlcgobj
  67. ;
  68. {*****************************************************************************
  69. Helpers
  70. *****************************************************************************}
  71. procedure tcgaddnode.pass_left_right;
  72. {$if defined(x86) and not defined(llvm)}
  73. var
  74. tmpreg : tregister;
  75. pushedfpu : boolean;
  76. {$endif x86 and not llvm}
  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. secondpass(left);
  84. if left.location.loc in [LOC_FLAGS,LOC_JUMP] then
  85. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,false);
  86. {$if defined(x86) and not defined(llvm)}
  87. { are too few registers free? }
  88. pushedfpu:=false;
  89. if (left.location.loc=LOC_FPUREGISTER) and
  90. (node_resources_fpu(right)>=maxfpuregs) then
  91. begin
  92. hlcg.location_force_mem(current_asmdata.CurrAsmList,left.location,left.resultdef);
  93. pushedfpu:=true;
  94. end;
  95. {$endif x86 and not llvm}
  96. secondpass(right);
  97. if right.location.loc in [LOC_FLAGS,LOC_JUMP] then
  98. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,resultdef,false);
  99. {$if defined(x86) and not defined(llvm)}
  100. if pushedfpu then
  101. begin
  102. if use_vectorfpu(left.resultdef) then
  103. begin
  104. tmpreg := cg.getmmregister(current_asmdata.CurrAsmList,left.location.size);
  105. hlcg.a_loadmm_loc_reg(current_asmdata.CurrAsmList,left.resultdef,left.resultdef,left.location,tmpreg,mms_movescalar);
  106. location_freetemp(current_asmdata.CurrAsmList,left.location);
  107. location_reset(left.location,LOC_MMREGISTER,left.location.size);
  108. left.location.register:=tmpreg;
  109. end
  110. else
  111. begin
  112. tmpreg := cg.getfpuregister(current_asmdata.CurrAsmList,left.location.size);
  113. cg.a_loadfpu_loc_reg(current_asmdata.CurrAsmList,left.location.size,left.location,tmpreg);
  114. location_freetemp(current_asmdata.CurrAsmList,left.location);
  115. location_reset(left.location,LOC_FPUREGISTER,left.location.size);
  116. left.location.register := tmpreg;
  117. { left operand is now on top of the stack, instead of the right one! }
  118. if (right.location.loc=LOC_FPUREGISTER) then
  119. toggleflag(nf_swapped);
  120. end;
  121. end;
  122. {$endif x86 and not llvm}
  123. end;
  124. procedure tcgaddnode.set_result_location_reg;
  125. begin
  126. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  127. {$ifndef cpu64bitalu}
  128. if location.size in [OS_64,OS_S64] then
  129. begin
  130. location.register64.reglo := cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  131. location.register64.reghi := cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  132. end
  133. else
  134. {$endif}
  135. location.register := hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  136. end;
  137. procedure tcgaddnode.force_reg_left_right(allow_swap,allow_constant:boolean);
  138. begin
  139. if (left.location.loc<>LOC_REGISTER) and
  140. not(
  141. allow_constant and
  142. (left.location.loc in [LOC_CONSTANT,LOC_CREGISTER])
  143. ) then
  144. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  145. if (right.location.loc<>LOC_REGISTER) and
  146. not(
  147. allow_constant and
  148. (right.location.loc in [LOC_CONSTANT,LOC_CREGISTER]) and
  149. (left.location.loc<>LOC_CONSTANT)
  150. ) then
  151. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,false);
  152. { Left is always a register, right can be register or constant }
  153. if left.location.loc=LOC_CONSTANT then
  154. begin
  155. { when it is not allowed to swap we have a constant on
  156. left, that will give problems }
  157. if not allow_swap then
  158. internalerror(200307043);
  159. swapleftright;
  160. end;
  161. end;
  162. function tcgaddnode.cmpnode2topcmp(unsigned: boolean): TOpCmp;
  163. begin
  164. if unsigned then
  165. case nodetype of
  166. gtn: result:=OC_A;
  167. gten: result:=OC_AE;
  168. ltn: result:=OC_B;
  169. lten: result:=OC_BE;
  170. equaln: result:=OC_EQ;
  171. unequaln: result:=OC_NE;
  172. else
  173. internalerror(2011010412);
  174. end
  175. else
  176. case nodetype of
  177. gtn: result:=OC_GT;
  178. gten: result:=OC_GTE;
  179. ltn: result:=OC_LT;
  180. lten: result:=OC_LTE;
  181. equaln: result:=OC_EQ;
  182. unequaln: result:=OC_NE;
  183. else
  184. internalerror(2011010412);
  185. end
  186. end;
  187. {*****************************************************************************
  188. Smallsets
  189. *****************************************************************************}
  190. procedure tcgaddnode.second_opsmallset;
  191. begin
  192. { when a setdef is passed, it has to be a smallset }
  193. if not(
  194. ((left.nodetype=setelementn) or is_smallset(left.resultdef)) and
  195. ((right.nodetype=setelementn) or is_smallset(right.resultdef))
  196. ) then
  197. internalerror(200203302);
  198. if (left.nodetype=setelementn) or (right.nodetype=setelementn) then
  199. second_addsmallsetelement
  200. else if nodetype in [equaln,unequaln,gtn,gten,lten,ltn] then
  201. second_cmpsmallset
  202. else
  203. second_addsmallset;
  204. end;
  205. procedure tcgaddnode.second_addsmallset;
  206. var
  207. cgop : TOpCg;
  208. opdone : boolean;
  209. begin
  210. opdone := false;
  211. pass_left_right;
  212. force_reg_left_right(true,true);
  213. set_result_location_reg;
  214. case nodetype of
  215. addn :
  216. cgop:=OP_OR;
  217. symdifn :
  218. cgop:=OP_XOR;
  219. muln :
  220. cgop:=OP_AND;
  221. subn :
  222. begin
  223. cgop:=OP_AND;
  224. if (not(nf_swapped in flags)) then
  225. if (right.location.loc=LOC_CONSTANT) then
  226. right.location.value := not(right.location.value)
  227. else
  228. opdone := true
  229. else if (left.location.loc=LOC_CONSTANT) then
  230. left.location.value := not(left.location.value)
  231. else
  232. begin
  233. swapleftright;
  234. opdone := true;
  235. end;
  236. if opdone then
  237. begin
  238. if (right.location.size<>left.location.size) or
  239. (location.size<>left.location.size) then
  240. internalerror(2010123001);
  241. { make sure that location.register is different from
  242. left.location.register, since right will overwrite it
  243. and we'll use left afterwards }
  244. if (right.location.loc=LOC_REGISTER) then
  245. location.register:=right.location.register
  246. else
  247. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  248. { make sure we don't modify left/right.location, because we told
  249. force_reg_left_right above that they can be constant }
  250. hlcg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NOT,resultdef,right.location.register,location.register);
  251. if left.location.loc = LOC_CONSTANT then
  252. hlcg.a_op_const_reg(current_asmdata.CurrAsmList,OP_AND,resultdef,left.location.value,location.register)
  253. else
  254. hlcg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_AND,resultdef,left.location.register,location.register);
  255. end;
  256. end;
  257. else
  258. internalerror(2002072701);
  259. end;
  260. if not opdone then
  261. begin
  262. // these are all commutative operations
  263. if (left.location.loc = LOC_CONSTANT) then
  264. swapleftright;
  265. if (right.location.loc = LOC_CONSTANT) then
  266. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,cgop,resultdef,
  267. right.location.value,left.location.register,
  268. location.register)
  269. else
  270. hlcg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,cgop,resultdef,
  271. right.location.register,left.location.register,
  272. location.register);
  273. end;
  274. end;
  275. procedure tcgaddnode.second_addsmallsetelement;
  276. var
  277. tmpreg : tregister;
  278. mask,
  279. setbase : aint;
  280. cgop : TOpCg;
  281. begin
  282. if nodetype<>addn then
  283. internalerror(20080302);
  284. { no range support for smallsets }
  285. if assigned(tsetelementnode(right).right) then
  286. internalerror(20080303);
  287. pass_left_right;
  288. { setelementn is a special case, it must be on right }
  289. if (nf_swapped in flags) and
  290. (left.nodetype=setelementn) then
  291. swapleftright;
  292. force_reg_left_right(false,false);
  293. set_result_location_reg;
  294. setbase:=tsetdef(left.resultdef).setbase;
  295. if (right.location.loc = LOC_CONSTANT) then
  296. begin
  297. if (target_info.endian=endian_big) then
  298. mask:=aint((aword(1) shl (resultdef.size*8-1)) shr aword(right.location.value-setbase))
  299. else
  300. mask:=aint(1 shl (right.location.value-setbase));
  301. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_OR,resultdef,
  302. mask,left.location.register,location.register);
  303. end
  304. else
  305. begin
  306. if (target_info.endian=endian_big) then
  307. begin
  308. mask:=aint((aword(1) shl (resultdef.size*8-1)));
  309. cgop:=OP_SHR
  310. end
  311. else
  312. begin
  313. mask:=1;
  314. cgop:=OP_SHL
  315. end;
  316. tmpreg:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  317. hlcg.a_load_const_reg(current_asmdata.CurrAsmList,resultdef,mask,tmpreg);
  318. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,resultdef,true);
  319. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,resultdef,right.location,setbase);
  320. hlcg.a_op_reg_reg(current_asmdata.CurrAsmList,cgop,resultdef,
  321. right.location.register,tmpreg);
  322. if left.location.loc <> LOC_CONSTANT then
  323. hlcg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_OR,resultdef,tmpreg,
  324. left.location.register,location.register)
  325. else
  326. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_OR,resultdef,
  327. left.location.value,tmpreg,location.register);
  328. end;
  329. end;
  330. {*****************************************************************************
  331. Boolean
  332. *****************************************************************************}
  333. procedure tcgaddnode.second_opboolean;
  334. begin
  335. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  336. second_cmpboolean
  337. else
  338. second_addboolean;
  339. end;
  340. procedure tcgaddnode.second_addboolean;
  341. var
  342. cgop : TOpCg;
  343. truelabel, falselabel : tasmlabel;
  344. oldflowcontrol : tflowcontrol;
  345. begin
  346. { And,Or will only evaluate from left to right only the
  347. needed nodes unless full boolean evaluation is enabled }
  348. if (nodetype in [orn,andn]) and
  349. (not(cs_full_boolean_eval in current_settings.localswitches) or
  350. (nf_short_bool in flags)) then
  351. begin
  352. case nodetype of
  353. andn :
  354. begin
  355. secondpass(left);
  356. hlcg.maketojumpbool(current_asmdata.CurrAsmList,left);
  357. hlcg.a_label(current_asmdata.CurrAsmList,left.location.truelabel);
  358. current_asmdata.getjumplabel(truelabel);
  359. location_reset_jump(location,truelabel,left.location.falselabel);
  360. end;
  361. orn :
  362. begin
  363. secondpass(left);
  364. hlcg.maketojumpbool(current_asmdata.CurrAsmList,left);
  365. hlcg.a_label(current_asmdata.CurrAsmList,left.location.falselabel);
  366. current_asmdata.getjumplabel(falselabel);
  367. location_reset_jump(location,left.location.truelabel,falselabel);
  368. end;
  369. else
  370. internalerror(200307044);
  371. end;
  372. { these jumps mean we're now in a flow control construct }
  373. oldflowcontrol:=flowcontrol;
  374. include(flowcontrol,fc_inflowcontrol);
  375. secondpass(right);
  376. { jump to the same labels as the left side, since the andn/orn
  377. merges the results of left and right }
  378. hlcg.maketojumpboollabels(current_asmdata.CurrAsmList,right,location.truelabel,location.falselabel);
  379. flowcontrol:=oldflowcontrol+(flowcontrol-[fc_inflowcontrol]);
  380. end
  381. else
  382. begin
  383. pass_left_right;
  384. force_reg_left_right(false,true);
  385. set_result_location_reg;
  386. case nodetype of
  387. xorn :
  388. cgop:=OP_XOR;
  389. orn :
  390. cgop:=OP_OR;
  391. andn :
  392. cgop:=OP_AND;
  393. else
  394. internalerror(200203247);
  395. end;
  396. {$ifndef cpu64bitalu}
  397. if right.location.size in [OS_64,OS_S64] then
  398. begin
  399. if right.location.loc <> LOC_CONSTANT then
  400. cg64.a_op64_reg_reg_reg(current_asmdata.CurrAsmList,cgop,location.size,
  401. left.location.register64,right.location.register64,
  402. location.register64)
  403. else
  404. cg64.a_op64_const_reg_reg(current_asmdata.CurrAsmList,cgop,location.size,
  405. right.location.value,left.location.register64,
  406. location.register64);
  407. end
  408. else
  409. {$endif cpu64bitalu}
  410. begin
  411. if right.location.loc <> LOC_CONSTANT then
  412. hlcg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,cgop,resultdef,
  413. left.location.register,right.location.register,
  414. location.register)
  415. else
  416. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,cgop,resultdef,
  417. right.location.value,left.location.register,
  418. location.register);
  419. end;
  420. end;
  421. end;
  422. {*****************************************************************************
  423. 64-bit
  424. *****************************************************************************}
  425. procedure tcgaddnode.second_op64bit;
  426. begin
  427. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  428. second_cmp64bit
  429. else
  430. second_add64bit;
  431. end;
  432. procedure tcgaddnode.second_add64bit;
  433. var
  434. op : TOpCG;
  435. checkoverflow : boolean;
  436. ovloc : tlocation;
  437. begin
  438. ovloc.loc:=LOC_VOID;
  439. pass_left_right;
  440. force_reg_left_right(false,true);
  441. set_result_location_reg;
  442. { assume no overflow checking is required }
  443. checkoverflow := false;
  444. case nodetype of
  445. addn :
  446. begin
  447. op:=OP_ADD;
  448. checkoverflow:=true;
  449. end;
  450. subn :
  451. begin
  452. op:=OP_SUB;
  453. checkoverflow:=true;
  454. end;
  455. xorn:
  456. op:=OP_XOR;
  457. orn:
  458. op:=OP_OR;
  459. andn:
  460. op:=OP_AND;
  461. muln:
  462. begin
  463. { should be handled in pass_1 (JM) }
  464. internalerror(200109051);
  465. end;
  466. else
  467. internalerror(2002072705);
  468. end;
  469. checkoverflow:=
  470. checkoverflow and
  471. (left.resultdef.typ<>pointerdef) and
  472. (right.resultdef.typ<>pointerdef) and
  473. (cs_check_overflow in current_settings.localswitches) and not(nf_internal in flags);
  474. {$ifdef cpu64bitalu}
  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}
  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(2002072803);
  558. end;
  559. {$endif cpu64bitalu}
  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(false,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. {*****************************************************************************
  692. pass_generate_code;
  693. *****************************************************************************}
  694. procedure tcgaddnode.pass_generate_code;
  695. begin
  696. case left.resultdef.typ of
  697. orddef :
  698. begin
  699. { handling boolean expressions }
  700. if is_boolean(left.resultdef) and
  701. is_boolean(right.resultdef) then
  702. second_opboolean
  703. { 64bit operations }
  704. else if is_64bit(left.resultdef) then
  705. second_op64bit
  706. else
  707. second_opordinal;
  708. end;
  709. stringdef :
  710. begin
  711. second_addstring;
  712. end;
  713. setdef :
  714. begin
  715. if is_smallset(tsetdef(left.resultdef)) then
  716. second_opsmallset
  717. else
  718. internalerror(200109041);
  719. end;
  720. arraydef :
  721. begin
  722. { support dynarr=nil }
  723. if is_dynamic_array(left.resultdef) then
  724. second_opordinal
  725. else
  726. if (cs_support_vectors in current_settings.globalswitches) and
  727. is_vector(left.resultdef) then
  728. second_opvector
  729. {$ifdef SUPPORT_MMX}
  730. else
  731. if is_mmx_able_array(left.resultdef) then
  732. second_opmmx
  733. {$endif SUPPORT_MMX}
  734. else
  735. internalerror(200306016);
  736. end;
  737. floatdef :
  738. second_opfloat;
  739. else
  740. second_opordinal;
  741. end;
  742. end;
  743. begin
  744. caddnode:=tcgaddnode;
  745. end.