nmat.pas 36 KB

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