ncgadd.pas 31 KB

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