nmat.pas 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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. nd : tdef;
  434. begin
  435. result:=nil;
  436. typecheckpass(left);
  437. typecheckpass(right);
  438. set_varstate(right,vs_read,[vsf_must_be_valid]);
  439. set_varstate(left,vs_read,[vsf_must_be_valid]);
  440. if codegenerror then
  441. exit;
  442. { tp procvar support }
  443. maybe_call_procvar(left,true);
  444. maybe_call_procvar(right,true);
  445. result:=simplify(false);
  446. if assigned(result) then
  447. exit;
  448. { allow operator overloading }
  449. t:=self;
  450. if isbinaryoverloaded(t) then
  451. begin
  452. result:=t;
  453. exit;
  454. end;
  455. {$ifdef cpunodefaultint}
  456. { for small cpus we use the smallest common type }
  457. if (left.resultdef.typ=orddef) and (right.resultdef.typ=orddef) then
  458. nd:=get_common_intdef(torddef(left.resultdef),torddef(right.resultdef),false)
  459. else
  460. nd:=s32inttype;
  461. {$endif cpunodefaultint}
  462. { calculations for ordinals < 32 bit have to be done in
  463. 32 bit for backwards compatibility. That way 'shl 33' is
  464. the same as 'shl 1'. It's ugly but compatible with delphi/tp/gcc }
  465. if (not is_64bit(left.resultdef)) and
  466. (torddef(left.resultdef).ordtype<>u32bit) then
  467. begin
  468. { keep singness of orignal type }
  469. if is_signed(left.resultdef) then
  470. {$ifdef cpunodefaultint}
  471. inserttypeconv(left,nd)
  472. {$else cpunodefaultint}
  473. inserttypeconv(left,s32inttype)
  474. {$endif cpunodefaultint}
  475. else
  476. begin
  477. {$ifdef cpunodefaultint}
  478. inserttypeconv(left,nd)
  479. {$else cpunodefaultint}
  480. inserttypeconv(left,u32inttype);
  481. {$endif cpunodefaultint}
  482. end
  483. end;
  484. {$ifdef cpunodefaultint}
  485. inserttypeconv(right,nd);
  486. {$else cpunodefaultint}
  487. inserttypeconv(right,sinttype);
  488. {$endif cpunodefaultint}
  489. resultdef:=left.resultdef;
  490. end;
  491. {$ifndef cpu64bitalu}
  492. function tshlshrnode.first_shlshr64bitint: tnode;
  493. var
  494. procname: string[31];
  495. begin
  496. result := nil;
  497. { otherwise create a call to a helper }
  498. if nodetype = shln then
  499. procname := 'fpc_shl_int64'
  500. else
  501. procname := 'fpc_shr_int64';
  502. { this order of parameters works at least for the arm,
  503. however it should work for any calling conventions (FK) }
  504. result := ccallnode.createintern(procname,ccallparanode.create(right,
  505. ccallparanode.create(left,nil)));
  506. left := nil;
  507. right := nil;
  508. firstpass(result);
  509. end;
  510. {$endif not cpu64bitalu}
  511. function tshlshrnode.pass_1 : tnode;
  512. var
  513. regs : longint;
  514. begin
  515. result:=nil;
  516. firstpass(left);
  517. firstpass(right);
  518. if codegenerror then
  519. exit;
  520. {$ifndef cpu64bitalu}
  521. { 64 bit ints have their own shift handling }
  522. if is_64bit(left.resultdef) then
  523. begin
  524. result := first_shlshr64bitint;
  525. if assigned(result) then
  526. exit;
  527. regs:=2;
  528. end
  529. else
  530. {$endif not cpu64bitalu}
  531. begin
  532. regs:=1
  533. end;
  534. if (right.nodetype<>ordconstn) then
  535. inc(regs);
  536. expectloc:=LOC_REGISTER;
  537. end;
  538. {****************************************************************************
  539. TUNARYMINUSNODE
  540. ****************************************************************************}
  541. constructor tunaryminusnode.create(expr : tnode);
  542. begin
  543. inherited create(unaryminusn,expr);
  544. end;
  545. function tunaryminusnode.simplify(forinline : boolean):tnode;
  546. begin
  547. result:=nil;
  548. { constant folding }
  549. if is_constintnode(left) then
  550. begin
  551. result:=create_simplified_ord_const(-tordconstnode(left).value,resultdef,forinline);
  552. exit;
  553. end;
  554. if is_constrealnode(left) then
  555. begin
  556. trealconstnode(left).value_real:=-trealconstnode(left).value_real;
  557. trealconstnode(left).value_currency:=-trealconstnode(left).value_currency;
  558. result:=left;
  559. left:=nil;
  560. exit;
  561. end;
  562. end;
  563. function tunaryminusnode.pass_typecheck : tnode;
  564. var
  565. t : tnode;
  566. begin
  567. result:=nil;
  568. typecheckpass(left);
  569. set_varstate(left,vs_read,[vsf_must_be_valid]);
  570. if codegenerror then
  571. exit;
  572. result:=simplify(false);
  573. if assigned(result) then
  574. exit;
  575. resultdef:=left.resultdef;
  576. if (left.resultdef.typ=floatdef) or
  577. is_currency(left.resultdef) then
  578. begin
  579. end
  580. {$ifdef SUPPORT_MMX}
  581. else if (cs_mmx in current_settings.localswitches) and
  582. is_mmx_able_array(left.resultdef) then
  583. begin
  584. { if saturation is on, left.resultdef isn't
  585. "mmx able" (FK)
  586. if (cs_mmx_saturation in current_settings.localswitches^) and
  587. (torddef(tarraydef(resultdef).definition).typ in
  588. [s32bit,u32bit]) then
  589. CGMessage(type_e_mismatch);
  590. }
  591. end
  592. {$endif SUPPORT_MMX}
  593. {$ifndef cpu64bitaddr}
  594. else if is_64bit(left.resultdef) then
  595. begin
  596. inserttypeconv(left,s64inttype);
  597. resultdef:=left.resultdef
  598. end
  599. {$endif not cpu64bitaddr}
  600. else if (left.resultdef.typ=orddef) then
  601. begin
  602. inserttypeconv(left,sinttype);
  603. resultdef:=left.resultdef
  604. end
  605. else
  606. begin
  607. { allow operator overloading }
  608. t:=self;
  609. if isunaryoverloaded(t) then
  610. begin
  611. result:=t;
  612. exit;
  613. end;
  614. CGMessage(type_e_mismatch);
  615. end;
  616. end;
  617. { generic code }
  618. { overridden by: }
  619. { i386 }
  620. function tunaryminusnode.pass_1 : tnode;
  621. var
  622. procname: string[31];
  623. fdef : tdef;
  624. begin
  625. result:=nil;
  626. firstpass(left);
  627. if codegenerror then
  628. exit;
  629. if (cs_fp_emulation in current_settings.moduleswitches) and (left.resultdef.typ=floatdef) then
  630. begin
  631. if not(target_info.system in systems_wince) then
  632. begin
  633. case tfloatdef(resultdef).floattype of
  634. s32real:
  635. begin
  636. procname:='float32_sub';
  637. fdef:=search_system_type('FLOAT32REC').typedef;
  638. end;
  639. s64real:
  640. begin
  641. procname:='float64_sub';
  642. fdef:=search_system_type('FLOAT64').typedef;
  643. end;
  644. {!!! not yet implemented
  645. s128real:
  646. }
  647. else
  648. internalerror(2005082801);
  649. end;
  650. result:=ctypeconvnode.create_internal(ccallnode.createintern(procname,ccallparanode.create(
  651. ctypeconvnode.create_internal(left,fDef),
  652. ccallparanode.create(ctypeconvnode.create_internal(crealconstnode.create(0,resultdef),fdef),nil))),resultdef);
  653. end
  654. else
  655. begin
  656. case tfloatdef(resultdef).floattype of
  657. s32real:
  658. procname:='NEGS';
  659. s64real:
  660. procname:='NEGD';
  661. {!!! not yet implemented
  662. s128real:
  663. }
  664. else
  665. internalerror(2005082802);
  666. end;
  667. result:=ccallnode.createintern(procname,ccallparanode.create(left,nil));
  668. end;
  669. left:=nil;
  670. end
  671. else
  672. begin
  673. if (left.resultdef.typ=floatdef) then
  674. expectloc:=LOC_FPUREGISTER
  675. {$ifdef SUPPORT_MMX}
  676. else if (cs_mmx in current_settings.localswitches) and
  677. is_mmx_able_array(left.resultdef) then
  678. expectloc:=LOC_MMXREGISTER
  679. {$endif SUPPORT_MMX}
  680. else if (left.resultdef.typ=orddef) then
  681. expectloc:=LOC_REGISTER;
  682. end;
  683. end;
  684. {****************************************************************************
  685. TUNARYPLUSNODE
  686. ****************************************************************************}
  687. constructor tunaryplusnode.create(expr: tnode);
  688. begin
  689. inherited create(unaryplusn,expr);
  690. end;
  691. function tunaryplusnode.pass_1: tnode;
  692. begin
  693. result:=nil;
  694. { can never happen because all the conversions happen
  695. in pass_typecheck }
  696. internalerror(201012250);
  697. end;
  698. function tunaryplusnode.pass_typecheck: tnode;
  699. var
  700. t:tnode;
  701. begin
  702. result:=nil;
  703. typecheckpass(left);
  704. set_varstate(left,vs_read,[vsf_must_be_valid]);
  705. if codegenerror then
  706. exit;
  707. if is_constintnode(left) or
  708. is_constrealnode(left) or
  709. (left.resultdef.typ=floatdef) or
  710. is_currency(left.resultdef)
  711. {$ifdef SUPPORT_MMX}
  712. or ((cs_mmx in current_settings.localswitches) and
  713. is_mmx_able_array(left.resultdef))
  714. {$endif SUPPORT_MMX}
  715. then
  716. begin
  717. result:=left;
  718. left:=nil;
  719. end
  720. {$ifndef cpu64bitaddr}
  721. else if is_64bit(left.resultdef) then
  722. begin
  723. inserttypeconv(left,s64inttype);
  724. result:=left;
  725. left:=nil;
  726. end
  727. {$endif not cpu64bitaddr}
  728. else if (left.resultdef.typ=orddef) then
  729. begin
  730. inserttypeconv(left,sinttype);
  731. result:=left;
  732. left:=nil;
  733. end
  734. else
  735. begin
  736. { allow operator overloading }
  737. t:=self;
  738. if isunaryoverloaded(t) then
  739. begin
  740. result:=t;
  741. exit;
  742. end;
  743. CGMessage(type_e_mismatch);
  744. end;
  745. end;
  746. {****************************************************************************
  747. TNOTNODE
  748. ****************************************************************************}
  749. const
  750. boolean_reverse:array[ltn..unequaln] of Tnodetype=(
  751. gten,gtn,lten,ltn,unequaln,equaln
  752. );
  753. constructor tnotnode.create(expr : tnode);
  754. begin
  755. inherited create(notn,expr);
  756. end;
  757. function tnotnode.simplify(forinline : boolean):tnode;
  758. var
  759. v : tconstexprint;
  760. t : tnode;
  761. def : tdef;
  762. begin
  763. result:=nil;
  764. { Try optmimizing ourself away }
  765. if left.nodetype=notn then
  766. begin
  767. { Double not. Remove both }
  768. result:=Tnotnode(left).left;
  769. tnotnode(left).left:=nil;
  770. exit;
  771. end;
  772. if (left.nodetype in [ltn,lten,equaln,unequaln,gtn,gten]) then
  773. begin
  774. { Not of boolean expression. Turn around the operator and remove
  775. the not. This is not allowed for sets with the gten/lten,
  776. because there is no ltn/gtn support }
  777. if (taddnode(left).left.resultdef.typ<>setdef) or
  778. (left.nodetype in [equaln,unequaln]) then
  779. begin
  780. result:=left;
  781. left.nodetype:=boolean_reverse[left.nodetype];
  782. left:=nil;
  783. exit;
  784. end;
  785. end;
  786. { constant folding }
  787. if (left.nodetype=ordconstn) then
  788. begin
  789. v:=tordconstnode(left).value;
  790. def:=left.resultdef;
  791. case torddef(left.resultdef).ordtype of
  792. pasbool,
  793. bool8bit,
  794. bool16bit,
  795. bool32bit,
  796. bool64bit:
  797. begin
  798. v:=byte(not(boolean(int64(v))));
  799. if (torddef(left.resultdef).ordtype<>pasbool) then
  800. v:=-v;
  801. end;
  802. uchar,
  803. uwidechar,
  804. u8bit,
  805. s8bit,
  806. u16bit,
  807. s16bit,
  808. s32bit,
  809. {$ifdef cpu64bitaddr}
  810. u32bit,
  811. {$endif cpu64bitaddr}
  812. s64bit:
  813. begin
  814. v:=int64(not int64(v));
  815. if (torddef(left.resultdef).ordtype<>s64bit) then
  816. def:=sinttype
  817. else
  818. def:=s64inttype;
  819. end;
  820. {$ifndef cpu64bitaddr}
  821. u32bit,
  822. {$endif not cpu64bitaddr}
  823. u64bit :
  824. begin
  825. { Delphi-compatible: not dword = dword (not word = longint) }
  826. { Extension: not qword = qword }
  827. v:=qword(not qword(v));
  828. { will be truncated by the ordconstnode for u32bit }
  829. end;
  830. else
  831. CGMessage(type_e_mismatch);
  832. end;
  833. if not forinline then
  834. t:=cordconstnode.create(v,def,false)
  835. else
  836. t:=create_simplified_ord_const(v,resultdef,true);
  837. result:=t;
  838. exit;
  839. end;
  840. end;
  841. function tnotnode.pass_typecheck : tnode;
  842. var
  843. t : tnode;
  844. begin
  845. result:=nil;
  846. typecheckpass(left);
  847. set_varstate(left,vs_read,[vsf_must_be_valid]);
  848. if codegenerror then
  849. exit;
  850. { tp procvar support }
  851. maybe_call_procvar(left,true);
  852. resultdef:=left.resultdef;
  853. result:=simplify(false);
  854. if assigned(result) then
  855. exit;
  856. if is_boolean(resultdef) then
  857. begin
  858. end
  859. else
  860. {$ifdef SUPPORT_MMX}
  861. if (cs_mmx in current_settings.localswitches) and
  862. is_mmx_able_array(left.resultdef) then
  863. begin
  864. end
  865. else
  866. {$endif SUPPORT_MMX}
  867. {$ifndef cpu64bitaddr}
  868. if is_64bitint(left.resultdef) then
  869. begin
  870. end
  871. else
  872. {$endif not cpu64bitaddr}
  873. if is_integer(left.resultdef) then
  874. begin
  875. end
  876. else
  877. begin
  878. { allow operator overloading }
  879. t:=self;
  880. if isunaryoverloaded(t) then
  881. begin
  882. result:=t;
  883. exit;
  884. end;
  885. CGMessage(type_e_mismatch);
  886. end;
  887. end;
  888. function tnotnode.pass_1 : tnode;
  889. begin
  890. result:=nil;
  891. firstpass(left);
  892. if codegenerror then
  893. exit;
  894. expectloc:=left.expectloc;
  895. if is_boolean(resultdef) then
  896. begin
  897. if (expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  898. expectloc:=LOC_REGISTER;
  899. { before loading it into flags we need to load it into
  900. a register thus 1 register is need PM }
  901. {$ifdef cpuflags}
  902. if left.expectloc<>LOC_JUMP then
  903. expectloc:=LOC_FLAGS;
  904. {$endif def cpuflags}
  905. end
  906. else
  907. {$ifdef SUPPORT_MMX}
  908. if (cs_mmx in current_settings.localswitches) and
  909. is_mmx_able_array(left.resultdef) then
  910. expectloc:=LOC_MMXREGISTER
  911. else
  912. {$endif SUPPORT_MMX}
  913. {$ifndef cpu64bitalu}
  914. if is_64bit(left.resultdef) then
  915. begin
  916. if (expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  917. expectloc:=LOC_REGISTER;
  918. end
  919. else
  920. {$endif not cpu64bitalu}
  921. if is_integer(left.resultdef) then
  922. expectloc:=LOC_REGISTER;
  923. end;
  924. {$ifdef state_tracking}
  925. function Tnotnode.track_state_pass(exec_known:boolean):boolean;
  926. begin
  927. track_state_pass:=true;
  928. if left.track_state_pass(exec_known) then
  929. begin
  930. left.resultdef:=nil;
  931. do_typecheckpass(left);
  932. end;
  933. end;
  934. {$endif}
  935. end.