nmat.pas 31 KB

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