ncgadd.pas 30 KB

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