nmat.pas 35 KB

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