nmat.pas 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  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,
  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. rd,ld : torddef;
  91. rv,lv : tconstexprint;
  92. begin
  93. result:=nil;
  94. if is_constintnode(right) then
  95. begin
  96. if tordconstnode(right).value = 1 then
  97. begin
  98. case nodetype of
  99. modn:
  100. result := cordconstnode.create(0,left.resultdef,true);
  101. divn:
  102. result := left.getcopy;
  103. end;
  104. exit;
  105. end;
  106. end;
  107. if is_constintnode(right) and is_constintnode(left) then
  108. begin
  109. rd:=torddef(right.resultdef);
  110. ld:=torddef(left.resultdef);
  111. rv:=tordconstnode(right).value;
  112. lv:=tordconstnode(left).value;
  113. case nodetype of
  114. modn:
  115. if (torddef(ld).ordtype <> u64bit) or
  116. (torddef(rd).ordtype <> u64bit) then
  117. t:=genintconstnode(lv mod rv)
  118. else
  119. t:=genintconstnode(int64(qword(lv) mod qword(rv)));
  120. divn:
  121. if (torddef(ld).ordtype <> u64bit) or
  122. (torddef(rd).ordtype <> u64bit) then
  123. t:=genintconstnode(lv div rv)
  124. else
  125. t:=genintconstnode(int64(qword(lv) div qword(rv)));
  126. end;
  127. result:=t;
  128. exit;
  129. end;
  130. end;
  131. function tmoddivnode.pass_typecheck:tnode;
  132. var
  133. hp,t : tnode;
  134. rd,ld : torddef;
  135. rv : tconstexprint;
  136. begin
  137. result:=nil;
  138. typecheckpass(left);
  139. typecheckpass(right);
  140. set_varstate(left,vs_read,[vsf_must_be_valid]);
  141. set_varstate(right,vs_read,[vsf_must_be_valid]);
  142. if codegenerror then
  143. exit;
  144. { tp procvar support }
  145. maybe_call_procvar(left,true);
  146. maybe_call_procvar(right,true);
  147. result:=simplify;
  148. if assigned(result) then
  149. exit;
  150. { allow operator overloading }
  151. t:=self;
  152. if isbinaryoverloaded(t) then
  153. begin
  154. result:=t;
  155. exit;
  156. end;
  157. { we need 2 orddefs always }
  158. if (left.resultdef.typ<>orddef) then
  159. inserttypeconv(right,sinttype);
  160. if (right.resultdef.typ<>orddef) then
  161. inserttypeconv(right,sinttype);
  162. if codegenerror then
  163. exit;
  164. rd:=torddef(right.resultdef);
  165. ld:=torddef(left.resultdef);
  166. { check for division by zero }
  167. if is_constintnode(right) then
  168. begin
  169. rv:=tordconstnode(right).value;
  170. if (rv=0) then
  171. begin
  172. Message(parser_e_division_by_zero);
  173. { recover }
  174. rv:=1;
  175. end;
  176. end;
  177. { if one operand is a cardinal and the other is a positive constant, convert the }
  178. { constant to a cardinal as well so we don't have to do a 64bit division (JM) }
  179. { Do the same for qwords and positive constants as well, otherwise things like }
  180. { "qword mod 10" are evaluated with int64 as result, which is wrong if the }
  181. { "qword" was > high(int64) (JM) }
  182. if (rd.ordtype in [u32bit,u64bit]) and
  183. is_constintnode(left) and
  184. (tordconstnode(left).value >= 0) then
  185. begin
  186. inserttypeconv(left,right.resultdef);
  187. ld:=torddef(left.resultdef);
  188. end;
  189. if (ld.ordtype in [u32bit,u64bit]) and
  190. is_constintnode(right) and
  191. (tordconstnode(right).value >= 0) then
  192. begin
  193. inserttypeconv(right,left.resultdef);
  194. rd:=torddef(right.resultdef);
  195. end;
  196. { when there is one currency value, everything is done
  197. using currency }
  198. if (ld.ordtype=scurrency) or
  199. (rd.ordtype=scurrency) then
  200. begin
  201. if (ld.ordtype<>scurrency) then
  202. inserttypeconv(left,s64currencytype);
  203. if (rd.ordtype<>scurrency) then
  204. inserttypeconv(right,s64currencytype);
  205. resultdef:=left.resultdef;
  206. end
  207. else
  208. {$ifndef cpu64bit}
  209. { when there is one 64bit value, everything is done
  210. in 64bit }
  211. if (is_64bitint(left.resultdef) or
  212. is_64bitint(right.resultdef)) then
  213. begin
  214. if is_signed(rd) or is_signed(ld) then
  215. begin
  216. if (ld.ordtype<>s64bit) then
  217. inserttypeconv(left,s64inttype);
  218. if (rd.ordtype<>s64bit) then
  219. inserttypeconv(right,s64inttype);
  220. end
  221. else
  222. begin
  223. if (ld.ordtype<>u64bit) then
  224. inserttypeconv(left,u64inttype);
  225. if (rd.ordtype<>u64bit) then
  226. inserttypeconv(right,u64inttype);
  227. end;
  228. resultdef:=left.resultdef;
  229. end
  230. else
  231. { when mixing cardinals and signed numbers, convert everythign to 64bit (JM) }
  232. if ((rd.ordtype = u32bit) and
  233. is_signed(ld)) or
  234. ((ld.ordtype = u32bit) and
  235. is_signed(rd)) then
  236. begin
  237. CGMessage(type_w_mixed_signed_unsigned);
  238. if (ld.ordtype<>s64bit) then
  239. inserttypeconv(left,s64inttype);
  240. if (rd.ordtype<>s64bit) then
  241. inserttypeconv(right,s64inttype);
  242. resultdef:=left.resultdef;
  243. end
  244. else
  245. {$endif cpu64bit}
  246. begin
  247. { Make everything always default singed int }
  248. if not(rd.ordtype in [torddef(sinttype).ordtype,torddef(uinttype).ordtype]) then
  249. inserttypeconv(right,sinttype);
  250. if not(ld.ordtype in [torddef(sinttype).ordtype,torddef(uinttype).ordtype]) then
  251. inserttypeconv(left,sinttype);
  252. resultdef:=right.resultdef;
  253. end;
  254. { when the result is currency we need some extra code for
  255. division. this should not be done when the divn node is
  256. created internally }
  257. if (nodetype=divn) and
  258. not(nf_is_currency in flags) and
  259. is_currency(resultdef) then
  260. begin
  261. hp:=caddnode.create(muln,getcopy,cordconstnode.create(10000,s64currencytype,false));
  262. include(hp.flags,nf_is_currency);
  263. result:=hp;
  264. end;
  265. end;
  266. function tmoddivnode.first_moddivint: tnode;
  267. {$ifdef cpuneedsdiv32helper}
  268. var
  269. procname: string[31];
  270. begin
  271. result := nil;
  272. { otherwise create a call to a helper }
  273. if nodetype = divn then
  274. procname := 'fpc_div_'
  275. else
  276. procname := 'fpc_mod_';
  277. { only qword needs the unsigned code, the
  278. signed code is also used for currency }
  279. if is_signed(resultdef) then
  280. procname := procname + 'longint'
  281. else
  282. procname := procname + 'dword';
  283. result := ccallnode.createintern(procname,ccallparanode.create(left,
  284. ccallparanode.create(right,nil)));
  285. left := nil;
  286. right := nil;
  287. firstpass(result);
  288. end;
  289. {$else cpuneedsdiv32helper}
  290. begin
  291. result:=nil;
  292. end;
  293. {$endif cpuneedsdiv32helper}
  294. {$ifndef cpu64bit}
  295. function tmoddivnode.first_moddiv64bitint: tnode;
  296. var
  297. procname: string[31];
  298. begin
  299. result := nil;
  300. { when currency is used set the result of the
  301. parameters to s64bit, so they are not converted }
  302. if is_currency(resultdef) then
  303. begin
  304. left.resultdef:=s64inttype;
  305. right.resultdef:=s64inttype;
  306. end;
  307. { otherwise create a call to a helper }
  308. if nodetype = divn then
  309. procname := 'fpc_div_'
  310. else
  311. procname := 'fpc_mod_';
  312. { only qword needs the unsigned code, the
  313. signed code is also used for currency }
  314. if is_signed(resultdef) then
  315. procname := procname + 'int64'
  316. else
  317. procname := procname + 'qword';
  318. result := ccallnode.createintern(procname,ccallparanode.create(left,
  319. ccallparanode.create(right,nil)));
  320. left := nil;
  321. right := nil;
  322. firstpass(result);
  323. end;
  324. {$endif cpu64bit}
  325. function tmoddivnode.firstoptimize: tnode;
  326. var
  327. power{,shiftval} : longint;
  328. newtype: tnodetype;
  329. begin
  330. result := nil;
  331. { divide/mod a number by a constant which is a power of 2? }
  332. if (cs_opt_peephole in current_settings.optimizerswitches) and
  333. (right.nodetype = ordconstn) and
  334. { ((nodetype = divn) or
  335. not is_signed(resultdef)) and}
  336. (not is_signed(resultdef)) and
  337. ispowerof2(tordconstnode(right).value,power) then
  338. begin
  339. if nodetype = divn then
  340. begin
  341. (*
  342. if is_signed(resultdef) then
  343. begin
  344. if is_64bitint(left.resultdef) then
  345. if not (cs_opt_size in current_settings.optimizerswitches) then
  346. shiftval := 63
  347. else
  348. { the shift code is a lot bigger than the call to }
  349. { the divide helper }
  350. exit
  351. else
  352. shiftval := 31;
  353. { we reuse left twice, so create once a copy of it }
  354. { !!! if left is a call is -> call gets executed twice }
  355. left := caddnode.create(addn,left,
  356. caddnode.create(andn,
  357. cshlshrnode.create(sarn,left.getcopy,
  358. cordconstnode.create(shiftval,sinttype,false)),
  359. cordconstnode.create(tordconstnode(right).value-1,
  360. right.resultdef,false)));
  361. newtype := sarn;
  362. end
  363. else
  364. *)
  365. newtype := shrn;
  366. tordconstnode(right).value := power;
  367. result := cshlshrnode.create(newtype,left,right)
  368. end
  369. else
  370. begin
  371. dec(tordconstnode(right).value);
  372. result := caddnode.create(andn,left,right);
  373. end;
  374. { left and right are reused }
  375. left := nil;
  376. right := nil;
  377. firstpass(result);
  378. exit;
  379. end;
  380. end;
  381. function tmoddivnode.pass_1 : tnode;
  382. begin
  383. result:=nil;
  384. firstpass(left);
  385. firstpass(right);
  386. if codegenerror then
  387. exit;
  388. { Try to optimize mod/div }
  389. result := firstoptimize;
  390. if assigned(result) then
  391. exit;
  392. {$ifndef cpu64bit}
  393. { 64bit }
  394. if (left.resultdef.typ=orddef) and
  395. (right.resultdef.typ=orddef) and
  396. (is_64bitint(left.resultdef) or is_64bitint(right.resultdef)) then
  397. begin
  398. result := first_moddiv64bitint;
  399. if assigned(result) then
  400. exit;
  401. expectloc:=LOC_REGISTER;
  402. calcregisters(self,2,0,0);
  403. end
  404. else
  405. {$endif cpu64bit}
  406. begin
  407. result := first_moddivint;
  408. if assigned(result) then
  409. exit;
  410. left_right_max;
  411. if left.registersint<=right.registersint then
  412. inc(registersint);
  413. end;
  414. expectloc:=LOC_REGISTER;
  415. end;
  416. {****************************************************************************
  417. TSHLSHRNODE
  418. ****************************************************************************}
  419. function tshlshrnode.simplify:tnode;
  420. var
  421. t : tnode;
  422. begin
  423. result:=nil;
  424. { constant folding }
  425. if is_constintnode(left) and is_constintnode(right) then
  426. begin
  427. case nodetype of
  428. shrn:
  429. t:=genintconstnode(tordconstnode(left).value shr tordconstnode(right).value);
  430. shln:
  431. t:=genintconstnode(tordconstnode(left).value shl tordconstnode(right).value);
  432. end;
  433. result:=t;
  434. exit;
  435. end;
  436. end;
  437. function tshlshrnode.pass_typecheck:tnode;
  438. var
  439. t : tnode;
  440. begin
  441. result:=nil;
  442. typecheckpass(left);
  443. typecheckpass(right);
  444. set_varstate(right,vs_read,[vsf_must_be_valid]);
  445. set_varstate(left,vs_read,[vsf_must_be_valid]);
  446. if codegenerror then
  447. exit;
  448. { tp procvar support }
  449. maybe_call_procvar(left,true);
  450. maybe_call_procvar(right,true);
  451. result:=simplify;
  452. if assigned(result) then
  453. exit;
  454. { allow operator overloading }
  455. t:=self;
  456. if isbinaryoverloaded(t) then
  457. begin
  458. result:=t;
  459. exit;
  460. end;
  461. { calculations for ordinals < 32 bit have to be done in
  462. 32 bit for backwards compatibility. That way 'shl 33' is
  463. the same as 'shl 1'. It's ugly but compatible with delphi/tp/gcc }
  464. if (not is_64bit(left.resultdef)) and
  465. (torddef(left.resultdef).ordtype<>u32bit) then
  466. inserttypeconv(left,s32inttype);
  467. inserttypeconv(right,sinttype);
  468. resultdef:=left.resultdef;
  469. end;
  470. {$ifndef cpu64bit}
  471. function tshlshrnode.first_shlshr64bitint: tnode;
  472. var
  473. procname: string[31];
  474. begin
  475. result := nil;
  476. { otherwise create a call to a helper }
  477. if nodetype = shln then
  478. procname := 'fpc_shl_int64'
  479. else
  480. procname := 'fpc_shr_int64';
  481. { this order of parameters works at least for the arm,
  482. however it should work for any calling conventions (FK) }
  483. result := ccallnode.createintern(procname,ccallparanode.create(right,
  484. ccallparanode.create(left,nil)));
  485. left := nil;
  486. right := nil;
  487. firstpass(result);
  488. end;
  489. {$endif cpu64bit}
  490. function tshlshrnode.pass_1 : tnode;
  491. var
  492. regs : longint;
  493. begin
  494. result:=nil;
  495. firstpass(left);
  496. firstpass(right);
  497. if codegenerror then
  498. exit;
  499. {$ifndef cpu64bit}
  500. { 64 bit ints have their own shift handling }
  501. if is_64bit(left.resultdef) then
  502. begin
  503. result := first_shlshr64bitint;
  504. if assigned(result) then
  505. exit;
  506. regs:=2;
  507. end
  508. else
  509. {$endif cpu64bit}
  510. begin
  511. regs:=1
  512. end;
  513. if (right.nodetype<>ordconstn) then
  514. inc(regs);
  515. expectloc:=LOC_REGISTER;
  516. calcregisters(self,regs,0,0);
  517. end;
  518. {****************************************************************************
  519. TUNARYMINUSNODE
  520. ****************************************************************************}
  521. constructor tunaryminusnode.create(expr : tnode);
  522. begin
  523. inherited create(unaryminusn,expr);
  524. end;
  525. function tunaryminusnode.simplify:tnode;
  526. begin
  527. result:=nil;
  528. { constant folding }
  529. if is_constintnode(left) then
  530. begin
  531. result:=genintconstnode(-tordconstnode(left).value);
  532. exit;
  533. end;
  534. if is_constrealnode(left) then
  535. begin
  536. trealconstnode(left).value_real:=-trealconstnode(left).value_real;
  537. trealconstnode(left).value_currency:=-trealconstnode(left).value_currency;
  538. result:=left;
  539. left:=nil;
  540. exit;
  541. end;
  542. end;
  543. function tunaryminusnode.pass_typecheck : tnode;
  544. var
  545. t : tnode;
  546. begin
  547. result:=nil;
  548. typecheckpass(left);
  549. set_varstate(left,vs_read,[vsf_must_be_valid]);
  550. if codegenerror then
  551. exit;
  552. result:=simplify;
  553. if assigned(result) then
  554. exit;
  555. resultdef:=left.resultdef;
  556. if (left.resultdef.typ=floatdef) then
  557. begin
  558. end
  559. {$ifdef SUPPORT_MMX}
  560. else if (cs_mmx in current_settings.localswitches) and
  561. is_mmx_able_array(left.resultdef) then
  562. begin
  563. { if saturation is on, left.resultdef isn't
  564. "mmx able" (FK)
  565. if (cs_mmx_saturation in current_settings.localswitches^) and
  566. (torddef(tarraydef(resultdef).definition).typ in
  567. [s32bit,u32bit]) then
  568. CGMessage(type_e_mismatch);
  569. }
  570. end
  571. {$endif SUPPORT_MMX}
  572. {$ifndef cpu64bit}
  573. else if is_64bit(left.resultdef) then
  574. begin
  575. end
  576. {$endif cpu64bit}
  577. else if (left.resultdef.typ=orddef) then
  578. begin
  579. if (torddef(left.resultdef).ordtype <> scurrency) then begin
  580. inserttypeconv(left,sinttype);
  581. resultdef:=left.resultdef;
  582. end;
  583. end
  584. else
  585. begin
  586. { allow operator overloading }
  587. t:=self;
  588. if isunaryoverloaded(t) then
  589. begin
  590. result:=t;
  591. exit;
  592. end;
  593. CGMessage(type_e_mismatch);
  594. end;
  595. end;
  596. { generic code }
  597. { overridden by: }
  598. { i386 }
  599. function tunaryminusnode.pass_1 : tnode;
  600. var
  601. procname: string[31];
  602. fdef : tdef;
  603. begin
  604. result:=nil;
  605. firstpass(left);
  606. if codegenerror then
  607. exit;
  608. if (cs_fp_emulation in current_settings.moduleswitches) and (left.resultdef.typ=floatdef) then
  609. begin
  610. if not(target_info.system in system_wince) then
  611. begin
  612. case tfloatdef(resultdef).floattype of
  613. s32real:
  614. begin
  615. procname:='float32_sub';
  616. fdef:=search_system_type('FLOAT32REC').typedef;
  617. end;
  618. s64real:
  619. begin
  620. procname:='float64_sub';
  621. fdef:=search_system_type('FLOAT64').typedef;
  622. end;
  623. {!!! not yet implemented
  624. s128real:
  625. }
  626. else
  627. internalerror(2005082801);
  628. end;
  629. result:=ctypeconvnode.create_internal(ccallnode.createintern(procname,ccallparanode.create(
  630. ctypeconvnode.create_internal(crealconstnode.create(0,resultdef),fdef),
  631. ccallparanode.create(ctypeconvnode.create_internal(left,fDef),nil))),resultdef);
  632. end
  633. else
  634. begin
  635. case tfloatdef(resultdef).floattype of
  636. s32real:
  637. procname:='NEGS';
  638. s64real:
  639. procname:='NEGD';
  640. {!!! not yet implemented
  641. s128real:
  642. }
  643. else
  644. internalerror(2005082802);
  645. end;
  646. result:=ccallnode.createintern(procname,ccallparanode.create(left,nil));
  647. end;
  648. left:=nil;
  649. end
  650. else
  651. begin
  652. registersint:=left.registersint;
  653. registersfpu:=left.registersfpu;
  654. {$ifdef SUPPORT_MMX}
  655. registersmmx:=left.registersmmx;
  656. {$endif SUPPORT_MMX}
  657. if (left.resultdef.typ=floatdef) then
  658. begin
  659. if (left.expectloc<>LOC_REGISTER) and
  660. (registersfpu<1) then
  661. registersfpu:=1;
  662. expectloc:=LOC_FPUREGISTER;
  663. end
  664. {$ifdef SUPPORT_MMX}
  665. else if (cs_mmx in current_settings.localswitches) and
  666. is_mmx_able_array(left.resultdef) then
  667. begin
  668. if (left.expectloc<>LOC_MMXREGISTER) and
  669. (registersmmx<1) then
  670. registersmmx:=1;
  671. end
  672. {$endif SUPPORT_MMX}
  673. {$ifndef cpu64bit}
  674. else if is_64bit(left.resultdef) then
  675. begin
  676. if (left.expectloc<>LOC_REGISTER) and
  677. (registersint<2) then
  678. registersint:=2;
  679. expectloc:=LOC_REGISTER;
  680. end
  681. {$endif cpu64bit}
  682. else if (left.resultdef.typ=orddef) then
  683. begin
  684. if (left.expectloc<>LOC_REGISTER) and
  685. (registersint<1) then
  686. registersint:=1;
  687. expectloc:=LOC_REGISTER;
  688. end;
  689. end;
  690. end;
  691. {****************************************************************************
  692. TNOTNODE
  693. ****************************************************************************}
  694. const
  695. boolean_reverse:array[ltn..unequaln] of Tnodetype=(
  696. gten,gtn,lten,ltn,unequaln,equaln
  697. );
  698. constructor tnotnode.create(expr : tnode);
  699. begin
  700. inherited create(notn,expr);
  701. end;
  702. function tnotnode.simplify:tnode;
  703. var
  704. v : tconstexprint;
  705. t : tnode;
  706. def : tdef;
  707. begin
  708. result:=nil;
  709. { Try optmimizing ourself away }
  710. if left.nodetype=notn then
  711. begin
  712. { Double not. Remove both }
  713. result:=Tnotnode(left).left;
  714. tnotnode(left).left:=nil;
  715. exit;
  716. end;
  717. if (left.nodetype in [ltn,lten,equaln,unequaln,gtn,gten]) then
  718. begin
  719. { Not of boolean expression. Turn around the operator and remove
  720. the not. This is not allowed for sets with the gten/lten,
  721. because there is no ltn/gtn support }
  722. if (taddnode(left).left.resultdef.typ<>setdef) or
  723. (left.nodetype in [equaln,unequaln]) then
  724. begin
  725. result:=left;
  726. left.nodetype:=boolean_reverse[left.nodetype];
  727. left:=nil;
  728. exit;
  729. end;
  730. end;
  731. { constant folding }
  732. if (left.nodetype=ordconstn) then
  733. begin
  734. v:=tordconstnode(left).value;
  735. def:=left.resultdef;
  736. case torddef(left.resultdef).ordtype of
  737. bool8bit,
  738. bool16bit,
  739. bool32bit,
  740. bool64bit:
  741. begin
  742. { here we do a boolean(byte(..)) type cast because }
  743. { boolean(<int64>) is buggy in 1.00 }
  744. v:=byte(not(boolean(byte(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.