ncgadd.pas 31 KB

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