nmat.pas 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  1. {
  2. Copyright (c) 2000-2005 by Florian Klaempfl
  3. Type checking and register allocation for math nodes
  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 nmat;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node;
  22. type
  23. tmoddivnode = class(tbinopnode)
  24. function pass_1 : tnode;override;
  25. function pass_typecheck:tnode;override;
  26. function simplify : tnode;override;
  27. protected
  28. {$ifndef cpu64bit}
  29. { override the following if you want to implement }
  30. { parts explicitely in the code generator (JM) }
  31. function first_moddiv64bitint: tnode; virtual;
  32. {$endif cpu64bit}
  33. function firstoptimize: tnode; virtual;
  34. function first_moddivint: tnode; virtual;
  35. end;
  36. tmoddivnodeclass = class of tmoddivnode;
  37. tshlshrnode = class(tbinopnode)
  38. function pass_1 : tnode;override;
  39. function pass_typecheck:tnode;override;
  40. function simplify : tnode;override;
  41. {$ifndef cpu64bit}
  42. { override the following if you want to implement }
  43. { parts explicitely in the code generator (CEC)
  44. Should return nil, if everything will be handled
  45. in the code generator
  46. }
  47. function first_shlshr64bitint: tnode; virtual;
  48. {$endif cpu64bit}
  49. end;
  50. tshlshrnodeclass = class of tshlshrnode;
  51. tunaryminusnode = class(tunarynode)
  52. constructor create(expr : tnode);virtual;
  53. function pass_1 : tnode;override;
  54. function pass_typecheck:tnode;override;
  55. function simplify : tnode;override;
  56. end;
  57. tunaryminusnodeclass = class of tunaryminusnode;
  58. tnotnode = class(tunarynode)
  59. constructor create(expr : tnode);virtual;
  60. function pass_1 : tnode;override;
  61. function pass_typecheck:tnode;override;
  62. function simplify : tnode;override;
  63. {$ifdef state_tracking}
  64. function track_state_pass(exec_known:boolean):boolean;override;
  65. {$endif}
  66. end;
  67. tnotnodeclass = class of tnotnode;
  68. var
  69. cmoddivnode : tmoddivnodeclass;
  70. cshlshrnode : tshlshrnodeclass;
  71. cunaryminusnode : tunaryminusnodeclass;
  72. cnotnode : tnotnodeclass;
  73. implementation
  74. uses
  75. systems,
  76. verbose,globals,cutils,
  77. globtype,constexp,
  78. symconst,symtype,symdef,symtable,
  79. defutil,
  80. htypechk,pass_1,
  81. cgbase,
  82. ncon,ncnv,ncal,nadd,
  83. nutils;
  84. {****************************************************************************
  85. TMODDIVNODE
  86. ****************************************************************************}
  87. function tmoddivnode.simplify:tnode;
  88. var
  89. t : tnode;
  90. rv,lv : tconstexprint;
  91. begin
  92. result:=nil;
  93. if is_constintnode(right) then
  94. begin
  95. if tordconstnode(right).value = 1 then
  96. begin
  97. case nodetype of
  98. modn:
  99. result := cordconstnode.create(0,left.resultdef,true);
  100. divn:
  101. result := left.getcopy;
  102. end;
  103. exit;
  104. end;
  105. end;
  106. if is_constintnode(right) and is_constintnode(left) then
  107. begin
  108. rv:=tordconstnode(right).value;
  109. lv:=tordconstnode(left).value;
  110. case nodetype of
  111. modn:
  112. t:=genintconstnode(lv mod rv);
  113. divn:
  114. t:=genintconstnode(lv div rv);
  115. end;
  116. result:=t;
  117. exit;
  118. end;
  119. end;
  120. function tmoddivnode.pass_typecheck:tnode;
  121. var
  122. hp,t : tnode;
  123. rd,ld : torddef;
  124. rv : tconstexprint;
  125. begin
  126. result:=nil;
  127. typecheckpass(left);
  128. typecheckpass(right);
  129. set_varstate(left,vs_read,[vsf_must_be_valid]);
  130. set_varstate(right,vs_read,[vsf_must_be_valid]);
  131. if codegenerror then
  132. exit;
  133. { tp procvar support }
  134. maybe_call_procvar(left,true);
  135. maybe_call_procvar(right,true);
  136. result:=simplify;
  137. if assigned(result) then
  138. exit;
  139. { allow operator overloading }
  140. t:=self;
  141. if isbinaryoverloaded(t) then
  142. begin
  143. result:=t;
  144. exit;
  145. end;
  146. { we need 2 orddefs always }
  147. if (left.resultdef.typ<>orddef) then
  148. inserttypeconv(right,sinttype);
  149. if (right.resultdef.typ<>orddef) then
  150. inserttypeconv(right,sinttype);
  151. if codegenerror then
  152. exit;
  153. rd:=torddef(right.resultdef);
  154. ld:=torddef(left.resultdef);
  155. { check for division by zero }
  156. if is_constintnode(right) then
  157. begin
  158. rv:=tordconstnode(right).value;
  159. if (rv=0) then
  160. begin
  161. Message(parser_e_division_by_zero);
  162. { recover }
  163. rv:=1;
  164. end;
  165. end;
  166. { if one operand is a cardinal and the other is a positive constant, convert the }
  167. { constant to a cardinal as well so we don't have to do a 64bit division (JM) }
  168. { Do the same for qwords and positive constants as well, otherwise things like }
  169. { "qword mod 10" are evaluated with int64 as result, which is wrong if the }
  170. { "qword" was > high(int64) (JM) }
  171. { Additionally, do the same for cardinal/qwords and other positive types, but }
  172. { always in a way that a smaller type is converted to a bigger type }
  173. { (webtbs/tw8870) }
  174. if (rd.ordtype in [u32bit,u64bit]) and
  175. ((is_constintnode(left) and
  176. (tordconstnode(left).value >= 0)) or
  177. (not is_signed(ld) and
  178. (rd.size >= ld.size))) then
  179. begin
  180. inserttypeconv(left,right.resultdef);
  181. ld:=torddef(left.resultdef);
  182. end;
  183. if (ld.ordtype in [u32bit,u64bit]) and
  184. ((is_constintnode(right) and
  185. (tordconstnode(right).value >= 0)) or
  186. (not is_signed(rd) and
  187. (ld.size >= rd.size))) then
  188. begin
  189. inserttypeconv(right,left.resultdef);
  190. rd:=torddef(right.resultdef);
  191. end;
  192. { when there is one currency value, everything is done
  193. using currency }
  194. if (ld.ordtype=scurrency) or
  195. (rd.ordtype=scurrency) then
  196. begin
  197. if (ld.ordtype<>scurrency) then
  198. inserttypeconv(left,s64currencytype);
  199. if (rd.ordtype<>scurrency) then
  200. inserttypeconv(right,s64currencytype);
  201. resultdef:=left.resultdef;
  202. end
  203. else
  204. {$ifndef cpu64bit}
  205. { when there is one 64bit value, everything is done
  206. in 64bit }
  207. if (is_64bitint(left.resultdef) or
  208. is_64bitint(right.resultdef)) then
  209. begin
  210. if is_signed(rd) or is_signed(ld) then
  211. begin
  212. if (ld.ordtype<>s64bit) then
  213. inserttypeconv(left,s64inttype);
  214. if (rd.ordtype<>s64bit) then
  215. inserttypeconv(right,s64inttype);
  216. end
  217. else
  218. begin
  219. if (ld.ordtype<>u64bit) then
  220. inserttypeconv(left,u64inttype);
  221. if (rd.ordtype<>u64bit) then
  222. inserttypeconv(right,u64inttype);
  223. end;
  224. resultdef:=left.resultdef;
  225. end
  226. else
  227. { when mixing cardinals and signed numbers, convert everythign to 64bit (JM) }
  228. if ((rd.ordtype = u32bit) and
  229. is_signed(ld)) or
  230. ((ld.ordtype = u32bit) and
  231. is_signed(rd)) then
  232. begin
  233. CGMessage(type_w_mixed_signed_unsigned);
  234. if (ld.ordtype<>s64bit) then
  235. inserttypeconv(left,s64inttype);
  236. if (rd.ordtype<>s64bit) then
  237. inserttypeconv(right,s64inttype);
  238. resultdef:=left.resultdef;
  239. end
  240. else
  241. {$endif cpu64bit}
  242. begin
  243. { Make everything always default singed int }
  244. if not(rd.ordtype in [torddef(sinttype).ordtype,torddef(uinttype).ordtype]) then
  245. inserttypeconv(right,sinttype);
  246. if not(ld.ordtype in [torddef(sinttype).ordtype,torddef(uinttype).ordtype]) then
  247. inserttypeconv(left,sinttype);
  248. resultdef:=right.resultdef;
  249. end;
  250. { when the result is currency we need some extra code for
  251. division. this should not be done when the divn node is
  252. created internally }
  253. if (nodetype=divn) and
  254. not(nf_is_currency in flags) and
  255. is_currency(resultdef) then
  256. begin
  257. hp:=caddnode.create(muln,getcopy,cordconstnode.create(10000,s64currencytype,false));
  258. include(hp.flags,nf_is_currency);
  259. result:=hp;
  260. end;
  261. end;
  262. function tmoddivnode.first_moddivint: tnode;
  263. {$ifdef cpuneedsdiv32helper}
  264. var
  265. procname: string[31];
  266. begin
  267. result := nil;
  268. { otherwise create a call to a helper }
  269. if nodetype = divn then
  270. procname := 'fpc_div_'
  271. else
  272. procname := 'fpc_mod_';
  273. { only qword needs the unsigned code, the
  274. signed code is also used for currency }
  275. if is_signed(resultdef) then
  276. procname := procname + 'longint'
  277. else
  278. procname := procname + 'dword';
  279. result := ccallnode.createintern(procname,ccallparanode.create(left,
  280. ccallparanode.create(right,nil)));
  281. left := nil;
  282. right := nil;
  283. firstpass(result);
  284. end;
  285. {$else cpuneedsdiv32helper}
  286. begin
  287. result:=nil;
  288. end;
  289. {$endif cpuneedsdiv32helper}
  290. {$ifndef cpu64bit}
  291. function tmoddivnode.first_moddiv64bitint: tnode;
  292. var
  293. procname: string[31];
  294. begin
  295. result := nil;
  296. { when currency is used set the result of the
  297. parameters to s64bit, so they are not converted }
  298. if is_currency(resultdef) then
  299. begin
  300. left.resultdef:=s64inttype;
  301. right.resultdef:=s64inttype;
  302. end;
  303. { otherwise create a call to a helper }
  304. if nodetype = divn then
  305. procname := 'fpc_div_'
  306. else
  307. procname := 'fpc_mod_';
  308. { only qword needs the unsigned code, the
  309. signed code is also used for currency }
  310. if is_signed(resultdef) then
  311. procname := procname + 'int64'
  312. else
  313. procname := procname + 'qword';
  314. result := ccallnode.createintern(procname,ccallparanode.create(left,
  315. ccallparanode.create(right,nil)));
  316. left := nil;
  317. right := nil;
  318. firstpass(result);
  319. end;
  320. {$endif cpu64bit}
  321. function tmoddivnode.firstoptimize: tnode;
  322. var
  323. power{,shiftval} : longint;
  324. newtype: tnodetype;
  325. begin
  326. result := nil;
  327. { divide/mod a number by a constant which is a power of 2? }
  328. if (cs_opt_peephole in current_settings.optimizerswitches) and
  329. (right.nodetype = ordconstn) and
  330. { ((nodetype = divn) or
  331. not is_signed(resultdef)) and}
  332. (not is_signed(resultdef)) and
  333. ispowerof2(tordconstnode(right).value,power) then
  334. begin
  335. if nodetype = divn then
  336. begin
  337. (*
  338. if is_signed(resultdef) then
  339. begin
  340. if is_64bitint(left.resultdef) then
  341. if not (cs_opt_size in current_settings.optimizerswitches) then
  342. shiftval := 63
  343. else
  344. { the shift code is a lot bigger than the call to }
  345. { the divide helper }
  346. exit
  347. else
  348. shiftval := 31;
  349. { we reuse left twice, so create once a copy of it }
  350. { !!! if left is a call is -> call gets executed twice }
  351. left := caddnode.create(addn,left,
  352. caddnode.create(andn,
  353. cshlshrnode.create(sarn,left.getcopy,
  354. cordconstnode.create(shiftval,sinttype,false)),
  355. cordconstnode.create(tordconstnode(right).value-1,
  356. right.resultdef,false)));
  357. newtype := sarn;
  358. end
  359. else
  360. *)
  361. newtype := shrn;
  362. tordconstnode(right).value := power;
  363. result := cshlshrnode.create(newtype,left,right)
  364. end
  365. else
  366. begin
  367. dec(tordconstnode(right).value.uvalue);
  368. result := caddnode.create(andn,left,right);
  369. end;
  370. { left and right are reused }
  371. left := nil;
  372. right := nil;
  373. firstpass(result);
  374. exit;
  375. end;
  376. end;
  377. function tmoddivnode.pass_1 : tnode;
  378. begin
  379. result:=nil;
  380. firstpass(left);
  381. firstpass(right);
  382. if codegenerror then
  383. exit;
  384. { Try to optimize mod/div }
  385. result := firstoptimize;
  386. if assigned(result) then
  387. exit;
  388. {$ifndef cpu64bit}
  389. { 64bit }
  390. if (left.resultdef.typ=orddef) and
  391. (right.resultdef.typ=orddef) and
  392. (is_64bitint(left.resultdef) or is_64bitint(right.resultdef)) then
  393. begin
  394. result := first_moddiv64bitint;
  395. if assigned(result) then
  396. exit;
  397. expectloc:=LOC_REGISTER;
  398. calcregisters(self,2,0,0);
  399. end
  400. else
  401. {$endif cpu64bit}
  402. begin
  403. result := first_moddivint;
  404. if assigned(result) then
  405. exit;
  406. left_right_max;
  407. if left.registersint<=right.registersint then
  408. inc(registersint);
  409. end;
  410. expectloc:=LOC_REGISTER;
  411. end;
  412. {****************************************************************************
  413. TSHLSHRNODE
  414. ****************************************************************************}
  415. function tshlshrnode.simplify:tnode;
  416. var
  417. t : tnode;
  418. begin
  419. result:=nil;
  420. { constant folding }
  421. if is_constintnode(left) and is_constintnode(right) then
  422. begin
  423. case nodetype of
  424. shrn:
  425. t:=genintconstnode(tordconstnode(left).value shr tordconstnode(right).value);
  426. shln:
  427. t:=genintconstnode(tordconstnode(left).value shl tordconstnode(right).value);
  428. end;
  429. result:=t;
  430. exit;
  431. end;
  432. end;
  433. function tshlshrnode.pass_typecheck:tnode;
  434. var
  435. t : tnode;
  436. begin
  437. result:=nil;
  438. typecheckpass(left);
  439. typecheckpass(right);
  440. set_varstate(right,vs_read,[vsf_must_be_valid]);
  441. set_varstate(left,vs_read,[vsf_must_be_valid]);
  442. if codegenerror then
  443. exit;
  444. { tp procvar support }
  445. maybe_call_procvar(left,true);
  446. maybe_call_procvar(right,true);
  447. result:=simplify;
  448. if assigned(result) then
  449. exit;
  450. { allow operator overloading }
  451. t:=self;
  452. if isbinaryoverloaded(t) then
  453. begin
  454. result:=t;
  455. exit;
  456. end;
  457. { calculations for ordinals < 32 bit have to be done in
  458. 32 bit for backwards compatibility. That way 'shl 33' is
  459. the same as 'shl 1'. It's ugly but compatible with delphi/tp/gcc }
  460. if (not is_64bit(left.resultdef)) and
  461. (torddef(left.resultdef).ordtype<>u32bit) then
  462. begin
  463. { keep singness of orignal type }
  464. if is_signed(left.resultdef) then
  465. inserttypeconv(left,s32inttype)
  466. else
  467. inserttypeconv(left,u32inttype);
  468. end;
  469. inserttypeconv(right,sinttype);
  470. resultdef:=left.resultdef;
  471. end;
  472. {$ifndef cpu64bit}
  473. function tshlshrnode.first_shlshr64bitint: tnode;
  474. var
  475. procname: string[31];
  476. begin
  477. result := nil;
  478. { otherwise create a call to a helper }
  479. if nodetype = shln then
  480. procname := 'fpc_shl_int64'
  481. else
  482. procname := 'fpc_shr_int64';
  483. { this order of parameters works at least for the arm,
  484. however it should work for any calling conventions (FK) }
  485. result := ccallnode.createintern(procname,ccallparanode.create(right,
  486. ccallparanode.create(left,nil)));
  487. left := nil;
  488. right := nil;
  489. firstpass(result);
  490. end;
  491. {$endif cpu64bit}
  492. function tshlshrnode.pass_1 : tnode;
  493. var
  494. regs : longint;
  495. begin
  496. result:=nil;
  497. firstpass(left);
  498. firstpass(right);
  499. if codegenerror then
  500. exit;
  501. {$ifndef cpu64bit}
  502. { 64 bit ints have their own shift handling }
  503. if is_64bit(left.resultdef) then
  504. begin
  505. result := first_shlshr64bitint;
  506. if assigned(result) then
  507. exit;
  508. regs:=2;
  509. end
  510. else
  511. {$endif cpu64bit}
  512. begin
  513. regs:=1
  514. end;
  515. if (right.nodetype<>ordconstn) then
  516. inc(regs);
  517. expectloc:=LOC_REGISTER;
  518. calcregisters(self,regs,0,0);
  519. end;
  520. {****************************************************************************
  521. TUNARYMINUSNODE
  522. ****************************************************************************}
  523. constructor tunaryminusnode.create(expr : tnode);
  524. begin
  525. inherited create(unaryminusn,expr);
  526. end;
  527. function tunaryminusnode.simplify:tnode;
  528. begin
  529. result:=nil;
  530. { constant folding }
  531. if is_constintnode(left) then
  532. begin
  533. result:=genintconstnode(-tordconstnode(left).value);
  534. exit;
  535. end;
  536. if is_constrealnode(left) then
  537. begin
  538. trealconstnode(left).value_real:=-trealconstnode(left).value_real;
  539. trealconstnode(left).value_currency:=-trealconstnode(left).value_currency;
  540. result:=left;
  541. left:=nil;
  542. exit;
  543. end;
  544. end;
  545. function tunaryminusnode.pass_typecheck : tnode;
  546. var
  547. t : tnode;
  548. begin
  549. result:=nil;
  550. typecheckpass(left);
  551. set_varstate(left,vs_read,[vsf_must_be_valid]);
  552. if codegenerror then
  553. exit;
  554. result:=simplify;
  555. if assigned(result) then
  556. exit;
  557. resultdef:=left.resultdef;
  558. if (left.resultdef.typ=floatdef) then
  559. begin
  560. end
  561. {$ifdef SUPPORT_MMX}
  562. else if (cs_mmx in current_settings.localswitches) and
  563. is_mmx_able_array(left.resultdef) then
  564. begin
  565. { if saturation is on, left.resultdef isn't
  566. "mmx able" (FK)
  567. if (cs_mmx_saturation in current_settings.localswitches^) and
  568. (torddef(tarraydef(resultdef).definition).typ in
  569. [s32bit,u32bit]) then
  570. CGMessage(type_e_mismatch);
  571. }
  572. end
  573. {$endif SUPPORT_MMX}
  574. {$ifndef cpu64bit}
  575. else if is_64bit(left.resultdef) then
  576. begin
  577. end
  578. {$endif cpu64bit}
  579. else if (left.resultdef.typ=orddef) then
  580. begin
  581. if (torddef(left.resultdef).ordtype <> scurrency) then begin
  582. inserttypeconv(left,sinttype);
  583. resultdef:=left.resultdef;
  584. end;
  585. end
  586. else
  587. begin
  588. { allow operator overloading }
  589. t:=self;
  590. if isunaryoverloaded(t) then
  591. begin
  592. result:=t;
  593. exit;
  594. end;
  595. CGMessage(type_e_mismatch);
  596. end;
  597. end;
  598. { generic code }
  599. { overridden by: }
  600. { i386 }
  601. function tunaryminusnode.pass_1 : tnode;
  602. var
  603. procname: string[31];
  604. fdef : tdef;
  605. begin
  606. result:=nil;
  607. firstpass(left);
  608. if codegenerror then
  609. exit;
  610. if (cs_fp_emulation in current_settings.moduleswitches) and (left.resultdef.typ=floatdef) then
  611. begin
  612. if not(target_info.system in system_wince) then
  613. begin
  614. case tfloatdef(resultdef).floattype of
  615. s32real:
  616. begin
  617. procname:='float32_sub';
  618. fdef:=search_system_type('FLOAT32REC').typedef;
  619. end;
  620. s64real:
  621. begin
  622. procname:='float64_sub';
  623. fdef:=search_system_type('FLOAT64').typedef;
  624. end;
  625. {!!! not yet implemented
  626. s128real:
  627. }
  628. else
  629. internalerror(2005082801);
  630. end;
  631. result:=ctypeconvnode.create_internal(ccallnode.createintern(procname,ccallparanode.create(
  632. ctypeconvnode.create_internal(left,fDef),
  633. ccallparanode.create(ctypeconvnode.create_internal(crealconstnode.create(0,resultdef),fdef),nil))),resultdef);
  634. end
  635. else
  636. begin
  637. case tfloatdef(resultdef).floattype of
  638. s32real:
  639. procname:='NEGS';
  640. s64real:
  641. procname:='NEGD';
  642. {!!! not yet implemented
  643. s128real:
  644. }
  645. else
  646. internalerror(2005082802);
  647. end;
  648. result:=ccallnode.createintern(procname,ccallparanode.create(left,nil));
  649. end;
  650. left:=nil;
  651. end
  652. else
  653. begin
  654. registersint:=left.registersint;
  655. registersfpu:=left.registersfpu;
  656. {$ifdef SUPPORT_MMX}
  657. registersmmx:=left.registersmmx;
  658. {$endif SUPPORT_MMX}
  659. if (left.resultdef.typ=floatdef) then
  660. begin
  661. if (left.expectloc<>LOC_REGISTER) and
  662. (registersfpu<1) then
  663. registersfpu:=1;
  664. expectloc:=LOC_FPUREGISTER;
  665. end
  666. {$ifdef SUPPORT_MMX}
  667. else if (cs_mmx in current_settings.localswitches) and
  668. is_mmx_able_array(left.resultdef) then
  669. begin
  670. if (left.expectloc<>LOC_MMXREGISTER) and
  671. (registersmmx<1) then
  672. registersmmx:=1;
  673. end
  674. {$endif SUPPORT_MMX}
  675. {$ifndef cpu64bit}
  676. else if is_64bit(left.resultdef) then
  677. begin
  678. if (left.expectloc<>LOC_REGISTER) and
  679. (registersint<2) then
  680. registersint:=2;
  681. expectloc:=LOC_REGISTER;
  682. end
  683. {$endif cpu64bit}
  684. else if (left.resultdef.typ=orddef) then
  685. begin
  686. if (left.expectloc<>LOC_REGISTER) and
  687. (registersint<1) then
  688. registersint:=1;
  689. expectloc:=LOC_REGISTER;
  690. end;
  691. end;
  692. end;
  693. {****************************************************************************
  694. TNOTNODE
  695. ****************************************************************************}
  696. const
  697. boolean_reverse:array[ltn..unequaln] of Tnodetype=(
  698. gten,gtn,lten,ltn,unequaln,equaln
  699. );
  700. constructor tnotnode.create(expr : tnode);
  701. begin
  702. inherited create(notn,expr);
  703. end;
  704. function tnotnode.simplify:tnode;
  705. var
  706. v : tconstexprint;
  707. t : tnode;
  708. def : tdef;
  709. begin
  710. result:=nil;
  711. { Try optmimizing ourself away }
  712. if left.nodetype=notn then
  713. begin
  714. { Double not. Remove both }
  715. result:=Tnotnode(left).left;
  716. tnotnode(left).left:=nil;
  717. exit;
  718. end;
  719. if (left.nodetype in [ltn,lten,equaln,unequaln,gtn,gten]) then
  720. begin
  721. { Not of boolean expression. Turn around the operator and remove
  722. the not. This is not allowed for sets with the gten/lten,
  723. because there is no ltn/gtn support }
  724. if (taddnode(left).left.resultdef.typ<>setdef) or
  725. (left.nodetype in [equaln,unequaln]) then
  726. begin
  727. result:=left;
  728. left.nodetype:=boolean_reverse[left.nodetype];
  729. left:=nil;
  730. exit;
  731. end;
  732. end;
  733. { constant folding }
  734. if (left.nodetype=ordconstn) then
  735. begin
  736. v:=tordconstnode(left).value;
  737. def:=left.resultdef;
  738. case torddef(left.resultdef).ordtype of
  739. bool8bit,
  740. bool16bit,
  741. bool32bit,
  742. bool64bit:
  743. begin
  744. v:=byte(not(boolean(int64(v))));
  745. end;
  746. uchar,
  747. uwidechar,
  748. u8bit,
  749. s8bit,
  750. u16bit,
  751. s16bit,
  752. u32bit,
  753. s32bit,
  754. s64bit,
  755. u64bit :
  756. begin
  757. v:=int64(not int64(v)); { maybe qword is required }
  758. int_to_type(v,def);
  759. end;
  760. else
  761. CGMessage(type_e_mismatch);
  762. end;
  763. t:=cordconstnode.create(v,def,true);
  764. result:=t;
  765. exit;
  766. end;
  767. end;
  768. function tnotnode.pass_typecheck : tnode;
  769. var
  770. t : tnode;
  771. begin
  772. result:=nil;
  773. typecheckpass(left);
  774. set_varstate(left,vs_read,[vsf_must_be_valid]);
  775. if codegenerror then
  776. exit;
  777. { tp procvar support }
  778. maybe_call_procvar(left,true);
  779. resultdef:=left.resultdef;
  780. result:=simplify;
  781. if assigned(result) then
  782. exit;
  783. if is_boolean(resultdef) then
  784. begin
  785. end
  786. else
  787. {$ifdef SUPPORT_MMX}
  788. if (cs_mmx in current_settings.localswitches) and
  789. is_mmx_able_array(left.resultdef) then
  790. begin
  791. end
  792. else
  793. {$endif SUPPORT_MMX}
  794. {$ifndef cpu64bit}
  795. if is_64bitint(left.resultdef) then
  796. begin
  797. end
  798. else
  799. {$endif cpu64bit}
  800. if is_integer(left.resultdef) then
  801. begin
  802. end
  803. else
  804. begin
  805. { allow operator overloading }
  806. t:=self;
  807. if isunaryoverloaded(t) then
  808. begin
  809. result:=t;
  810. exit;
  811. end;
  812. CGMessage(type_e_mismatch);
  813. end;
  814. end;
  815. function tnotnode.pass_1 : tnode;
  816. begin
  817. result:=nil;
  818. firstpass(left);
  819. if codegenerror then
  820. exit;
  821. expectloc:=left.expectloc;
  822. registersint:=left.registersint;
  823. {$ifdef SUPPORT_MMX}
  824. registersmmx:=left.registersmmx;
  825. {$endif SUPPORT_MMX}
  826. if is_boolean(resultdef) then
  827. begin
  828. if (expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  829. begin
  830. expectloc:=LOC_REGISTER;
  831. if (registersint<1) then
  832. registersint:=1;
  833. end;
  834. { before loading it into flags we need to load it into
  835. a register thus 1 register is need PM }
  836. {$ifdef cpuflags}
  837. if left.expectloc<>LOC_JUMP then
  838. expectloc:=LOC_FLAGS;
  839. {$endif def cpuflags}
  840. end
  841. else
  842. {$ifdef SUPPORT_MMX}
  843. if (cs_mmx in current_settings.localswitches) and
  844. is_mmx_able_array(left.resultdef) then
  845. begin
  846. if (left.expectloc<>LOC_MMXREGISTER) and
  847. (registersmmx<1) then
  848. registersmmx:=1;
  849. end
  850. else
  851. {$endif SUPPORT_MMX}
  852. {$ifndef cpu64bit}
  853. if is_64bit(left.resultdef) then
  854. begin
  855. if (expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  856. begin
  857. expectloc:=LOC_REGISTER;
  858. if (registersint<2) then
  859. registersint:=2;
  860. end;
  861. end
  862. else
  863. {$endif cpu64bit}
  864. if is_integer(left.resultdef) then
  865. begin
  866. if (left.expectloc<>LOC_REGISTER) and
  867. (registersint<1) then
  868. registersint:=1;
  869. expectloc:=LOC_REGISTER;
  870. end;
  871. end;
  872. {$ifdef state_tracking}
  873. function Tnotnode.track_state_pass(exec_known:boolean):boolean;
  874. begin
  875. track_state_pass:=true;
  876. if left.track_state_pass(exec_known) then
  877. begin
  878. left.resultdef:=nil;
  879. do_typecheckpass(left);
  880. end;
  881. end;
  882. {$endif}
  883. begin
  884. cmoddivnode:=tmoddivnode;
  885. cshlshrnode:=tshlshrnode;
  886. cunaryminusnode:=tunaryminusnode;
  887. cnotnode:=tnotnode;
  888. end.