nmat.pas 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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. result:=left;
  538. left:=nil;
  539. exit;
  540. end;
  541. end;
  542. function tunaryminusnode.pass_typecheck : tnode;
  543. var
  544. t : tnode;
  545. begin
  546. result:=nil;
  547. typecheckpass(left);
  548. set_varstate(left,vs_read,[vsf_must_be_valid]);
  549. if codegenerror then
  550. exit;
  551. result:=simplify;
  552. if assigned(result) then
  553. exit;
  554. resultdef:=left.resultdef;
  555. if (left.resultdef.typ=floatdef) then
  556. begin
  557. end
  558. {$ifdef SUPPORT_MMX}
  559. else if (cs_mmx in current_settings.localswitches) and
  560. is_mmx_able_array(left.resultdef) then
  561. begin
  562. { if saturation is on, left.resultdef isn't
  563. "mmx able" (FK)
  564. if (cs_mmx_saturation in current_settings.localswitches^) and
  565. (torddef(tarraydef(resultdef).definition).typ in
  566. [s32bit,u32bit]) then
  567. CGMessage(type_e_mismatch);
  568. }
  569. end
  570. {$endif SUPPORT_MMX}
  571. {$ifndef cpu64bit}
  572. else if is_64bitint(left.resultdef) then
  573. begin
  574. end
  575. {$endif cpu64bit}
  576. else if (left.resultdef.typ=orddef) then
  577. begin
  578. inserttypeconv(left,sinttype);
  579. resultdef:=left.resultdef;
  580. end
  581. else
  582. begin
  583. { allow operator overloading }
  584. t:=self;
  585. if isunaryoverloaded(t) then
  586. begin
  587. result:=t;
  588. exit;
  589. end;
  590. CGMessage(type_e_mismatch);
  591. end;
  592. end;
  593. { generic code }
  594. { overridden by: }
  595. { i386 }
  596. function tunaryminusnode.pass_1 : tnode;
  597. var
  598. procname: string[31];
  599. fdef : tdef;
  600. begin
  601. result:=nil;
  602. firstpass(left);
  603. if codegenerror then
  604. exit;
  605. if (cs_fp_emulation in current_settings.moduleswitches) and (left.resultdef.typ=floatdef) then
  606. begin
  607. if not(target_info.system in system_wince) then
  608. begin
  609. case tfloatdef(resultdef).floattype of
  610. s32real:
  611. begin
  612. procname:='float32_sub';
  613. fdef:=search_system_type('FLOAT32REC').typedef;
  614. end;
  615. s64real:
  616. begin
  617. procname:='float64_sub';
  618. fdef:=search_system_type('FLOAT64').typedef;
  619. end;
  620. {!!! not yet implemented
  621. s128real:
  622. }
  623. else
  624. internalerror(2005082801);
  625. end;
  626. result:=ctypeconvnode.create_internal(ccallnode.createintern(procname,ccallparanode.create(
  627. ctypeconvnode.create_internal(crealconstnode.create(0,resultdef),fdef),
  628. ccallparanode.create(ctypeconvnode.create_internal(left,fDef),nil))),resultdef);
  629. end
  630. else
  631. begin
  632. case tfloatdef(resultdef).floattype of
  633. s32real:
  634. procname:='NEGS';
  635. s64real:
  636. procname:='NEGD';
  637. {!!! not yet implemented
  638. s128real:
  639. }
  640. else
  641. internalerror(2005082802);
  642. end;
  643. result:=ccallnode.createintern(procname,ccallparanode.create(left,nil));
  644. end;
  645. left:=nil;
  646. end
  647. else
  648. begin
  649. registersint:=left.registersint;
  650. registersfpu:=left.registersfpu;
  651. {$ifdef SUPPORT_MMX}
  652. registersmmx:=left.registersmmx;
  653. {$endif SUPPORT_MMX}
  654. if (left.resultdef.typ=floatdef) then
  655. begin
  656. if (left.expectloc<>LOC_REGISTER) and
  657. (registersfpu<1) then
  658. registersfpu:=1;
  659. expectloc:=LOC_FPUREGISTER;
  660. end
  661. {$ifdef SUPPORT_MMX}
  662. else if (cs_mmx in current_settings.localswitches) and
  663. is_mmx_able_array(left.resultdef) then
  664. begin
  665. if (left.expectloc<>LOC_MMXREGISTER) and
  666. (registersmmx<1) then
  667. registersmmx:=1;
  668. end
  669. {$endif SUPPORT_MMX}
  670. {$ifndef cpu64bit}
  671. else if is_64bit(left.resultdef) then
  672. begin
  673. if (left.expectloc<>LOC_REGISTER) and
  674. (registersint<2) then
  675. registersint:=2;
  676. expectloc:=LOC_REGISTER;
  677. end
  678. {$endif cpu64bit}
  679. else if (left.resultdef.typ=orddef) then
  680. begin
  681. if (left.expectloc<>LOC_REGISTER) and
  682. (registersint<1) then
  683. registersint:=1;
  684. expectloc:=LOC_REGISTER;
  685. end;
  686. end;
  687. end;
  688. {****************************************************************************
  689. TNOTNODE
  690. ****************************************************************************}
  691. const
  692. boolean_reverse:array[ltn..unequaln] of Tnodetype=(
  693. gten,gtn,lten,ltn,unequaln,equaln
  694. );
  695. constructor tnotnode.create(expr : tnode);
  696. begin
  697. inherited create(notn,expr);
  698. end;
  699. function tnotnode.simplify:tnode;
  700. var
  701. v : tconstexprint;
  702. t : tnode;
  703. def : tdef;
  704. begin
  705. result:=nil;
  706. { Try optmimizing ourself away }
  707. if left.nodetype=notn then
  708. begin
  709. { Double not. Remove both }
  710. result:=Tnotnode(left).left;
  711. tnotnode(left).left:=nil;
  712. exit;
  713. end;
  714. if (left.nodetype in [ltn,lten,equaln,unequaln,gtn,gten]) then
  715. begin
  716. { Not of boolean expression. Turn around the operator and remove
  717. the not. This is not allowed for sets with the gten/lten,
  718. because there is no ltn/gtn support }
  719. if (taddnode(left).left.resultdef.typ<>setdef) or
  720. (left.nodetype in [equaln,unequaln]) then
  721. begin
  722. result:=left;
  723. left.nodetype:=boolean_reverse[left.nodetype];
  724. left:=nil;
  725. exit;
  726. end;
  727. end;
  728. { constant folding }
  729. if (left.nodetype=ordconstn) then
  730. begin
  731. v:=tordconstnode(left).value;
  732. def:=left.resultdef;
  733. case torddef(left.resultdef).ordtype of
  734. bool8bit,
  735. bool16bit,
  736. bool32bit,
  737. bool64bit:
  738. begin
  739. { here we do a boolean(byte(..)) type cast because }
  740. { boolean(<int64>) is buggy in 1.00 }
  741. v:=byte(not(boolean(byte(v))));
  742. end;
  743. uchar,
  744. uwidechar,
  745. u8bit,
  746. s8bit,
  747. u16bit,
  748. s16bit,
  749. u32bit,
  750. s32bit,
  751. s64bit,
  752. u64bit :
  753. begin
  754. v:=int64(not int64(v)); { maybe qword is required }
  755. int_to_type(v,def);
  756. end;
  757. else
  758. CGMessage(type_e_mismatch);
  759. end;
  760. t:=cordconstnode.create(v,def,true);
  761. result:=t;
  762. exit;
  763. end;
  764. end;
  765. function tnotnode.pass_typecheck : tnode;
  766. var
  767. t : tnode;
  768. begin
  769. result:=nil;
  770. typecheckpass(left);
  771. set_varstate(left,vs_read,[]);
  772. if codegenerror then
  773. exit;
  774. { tp procvar support }
  775. maybe_call_procvar(left,true);
  776. resultdef:=left.resultdef;
  777. result:=simplify;
  778. if assigned(result) then
  779. exit;
  780. if is_boolean(resultdef) then
  781. begin
  782. end
  783. else
  784. {$ifdef SUPPORT_MMX}
  785. if (cs_mmx in current_settings.localswitches) and
  786. is_mmx_able_array(left.resultdef) then
  787. begin
  788. end
  789. else
  790. {$endif SUPPORT_MMX}
  791. {$ifndef cpu64bit}
  792. if is_64bitint(left.resultdef) then
  793. begin
  794. end
  795. else
  796. {$endif cpu64bit}
  797. if is_integer(left.resultdef) then
  798. begin
  799. end
  800. else
  801. begin
  802. { allow operator overloading }
  803. t:=self;
  804. if isunaryoverloaded(t) then
  805. begin
  806. result:=t;
  807. exit;
  808. end;
  809. CGMessage(type_e_mismatch);
  810. end;
  811. end;
  812. function tnotnode.pass_1 : tnode;
  813. begin
  814. result:=nil;
  815. firstpass(left);
  816. if codegenerror then
  817. exit;
  818. expectloc:=left.expectloc;
  819. registersint:=left.registersint;
  820. {$ifdef SUPPORT_MMX}
  821. registersmmx:=left.registersmmx;
  822. {$endif SUPPORT_MMX}
  823. if is_boolean(resultdef) then
  824. begin
  825. if (expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  826. begin
  827. expectloc:=LOC_REGISTER;
  828. if (registersint<1) then
  829. registersint:=1;
  830. end;
  831. { before loading it into flags we need to load it into
  832. a register thus 1 register is need PM }
  833. {$ifdef cpuflags}
  834. if left.expectloc<>LOC_JUMP then
  835. expectloc:=LOC_FLAGS;
  836. {$endif def cpuflags}
  837. end
  838. else
  839. {$ifdef SUPPORT_MMX}
  840. if (cs_mmx in current_settings.localswitches) and
  841. is_mmx_able_array(left.resultdef) then
  842. begin
  843. if (left.expectloc<>LOC_MMXREGISTER) and
  844. (registersmmx<1) then
  845. registersmmx:=1;
  846. end
  847. else
  848. {$endif SUPPORT_MMX}
  849. {$ifndef cpu64bit}
  850. if is_64bit(left.resultdef) then
  851. begin
  852. if (expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  853. begin
  854. expectloc:=LOC_REGISTER;
  855. if (registersint<2) then
  856. registersint:=2;
  857. end;
  858. end
  859. else
  860. {$endif cpu64bit}
  861. if is_integer(left.resultdef) then
  862. begin
  863. if (left.expectloc<>LOC_REGISTER) and
  864. (registersint<1) then
  865. registersint:=1;
  866. expectloc:=LOC_REGISTER;
  867. end;
  868. end;
  869. {$ifdef state_tracking}
  870. function Tnotnode.track_state_pass(exec_known:boolean):boolean;
  871. begin
  872. track_state_pass:=true;
  873. if left.track_state_pass(exec_known) then
  874. begin
  875. left.resultdef:=nil;
  876. do_typecheckpass(left);
  877. end;
  878. end;
  879. {$endif}
  880. begin
  881. cmoddivnode:=tmoddivnode;
  882. cshlshrnode:=tshlshrnode;
  883. cunaryminusnode:=tunaryminusnode;
  884. cnotnode:=tnotnode;
  885. end.