nmat.pas 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  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. { not-nodes are not range checked by the code generator -> also
  907. don't range check while inlining; the resultdef is a bit tricky
  908. though: the node's resultdef gets changed in most cases compared
  909. to left, but the not-operation itself is caried out in the code
  910. generator using the size of left
  911. }
  912. if not(forinline) then
  913. t:=cordconstnode.create(v,def,false)
  914. else
  915. begin
  916. { cut off the value if necessary }
  917. t:=cordconstnode.create(v,left.resultdef,false);
  918. { now convert to node's resultdef }
  919. inserttypeconv_explicit(t,def);
  920. end;
  921. result:=t;
  922. exit;
  923. end;
  924. end;
  925. function tnotnode.pass_typecheck : tnode;
  926. var
  927. t : tnode;
  928. begin
  929. result:=nil;
  930. typecheckpass(left);
  931. set_varstate(left,vs_read,[vsf_must_be_valid]);
  932. if codegenerror then
  933. exit;
  934. { tp procvar support }
  935. maybe_call_procvar(left,true);
  936. resultdef:=left.resultdef;
  937. result:=simplify(false);
  938. if assigned(result) then
  939. exit;
  940. if is_boolean(resultdef) then
  941. begin
  942. end
  943. else
  944. {$ifdef SUPPORT_MMX}
  945. if (cs_mmx in current_settings.localswitches) and
  946. is_mmx_able_array(left.resultdef) then
  947. begin
  948. end
  949. else
  950. {$endif SUPPORT_MMX}
  951. {$ifndef cpu64bitaddr}
  952. if is_64bitint(left.resultdef) then
  953. begin
  954. end
  955. else
  956. {$endif not cpu64bitaddr}
  957. if is_integer(left.resultdef) then
  958. begin
  959. end
  960. else
  961. begin
  962. { allow operator overloading }
  963. t:=self;
  964. if isunaryoverloaded(t) then
  965. begin
  966. result:=t;
  967. exit;
  968. end;
  969. CGMessage(type_e_mismatch);
  970. end;
  971. end;
  972. function tnotnode.pass_1 : tnode;
  973. begin
  974. result:=nil;
  975. firstpass(left);
  976. if codegenerror then
  977. exit;
  978. expectloc:=left.expectloc;
  979. if is_boolean(resultdef) then
  980. begin
  981. if (expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  982. expectloc:=LOC_REGISTER;
  983. { before loading it into flags we need to load it into
  984. a register thus 1 register is need PM }
  985. {$ifdef cpuflags}
  986. if left.expectloc<>LOC_JUMP then
  987. expectloc:=LOC_FLAGS;
  988. {$endif def cpuflags}
  989. end
  990. else
  991. {$ifdef SUPPORT_MMX}
  992. if (cs_mmx in current_settings.localswitches) and
  993. is_mmx_able_array(left.resultdef) then
  994. expectloc:=LOC_MMXREGISTER
  995. else
  996. {$endif SUPPORT_MMX}
  997. {$ifndef cpu64bitalu}
  998. if is_64bit(left.resultdef) then
  999. begin
  1000. if (expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1001. expectloc:=LOC_REGISTER;
  1002. end
  1003. else
  1004. {$endif not cpu64bitalu}
  1005. if is_integer(left.resultdef) then
  1006. expectloc:=LOC_REGISTER;
  1007. end;
  1008. {$ifdef state_tracking}
  1009. function Tnotnode.track_state_pass(exec_known:boolean):boolean;
  1010. begin
  1011. track_state_pass:=true;
  1012. if left.track_state_pass(exec_known) then
  1013. begin
  1014. left.resultdef:=nil;
  1015. do_typecheckpass(left);
  1016. end;
  1017. end;
  1018. {$endif}
  1019. end.