ncgadd.pas 31 KB

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