ncgadd.pas 30 KB

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