nld.pas 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  1. {
  2. $Id$
  3. Copyright (c) 2000-2002 by Florian Klaempfl
  4. Type checking and register allocation for load/assignment nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit nld;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,
  23. {$ifdef state_tracking}
  24. nstate,
  25. {$endif}
  26. symconst,symppu,symbase,symtype,symsym,symdef;
  27. type
  28. tloadnode = class(tunarynode)
  29. symtableentry : tsym;
  30. symtable : tsymtable;
  31. procdef : tprocdef;
  32. constructor create(v : tsym;st : tsymtable);virtual;
  33. constructor create_procvar(v : tsym;d:tprocdef;st : tsymtable);virtual;
  34. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  35. procedure ppuwrite(ppufile:tcompilerppufile);override;
  36. procedure derefimpl;override;
  37. procedure set_mp(p:tnode);
  38. function getcopy : tnode;override;
  39. function pass_1 : tnode;override;
  40. function det_resulttype:tnode;override;
  41. {$ifdef var_notification}
  42. procedure mark_write;override;
  43. {$endif}
  44. function docompare(p: tnode): boolean; override;
  45. {$ifdef extdebug}
  46. procedure _dowrite;override;
  47. {$endif}
  48. end;
  49. tloadnodeclass = class of tloadnode;
  50. { different assignment types }
  51. tassigntype = (at_normal,at_plus,at_minus,at_star,at_slash);
  52. tassignmentnode = class(tbinarynode)
  53. assigntype : tassigntype;
  54. constructor create(l,r : tnode);virtual;
  55. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  56. procedure ppuwrite(ppufile:tcompilerppufile);override;
  57. function getcopy : tnode;override;
  58. function pass_1 : tnode;override;
  59. function det_resulttype:tnode;override;
  60. {$ifdef state_tracking}
  61. function track_state_pass(exec_known:boolean):boolean;override;
  62. {$endif state_tracking}
  63. function docompare(p: tnode): boolean; override;
  64. end;
  65. tassignmentnodeclass = class of tassignmentnode;
  66. tfuncretnode = class(tnode)
  67. funcretsym : tfuncretsym;
  68. constructor create(v:tsym);virtual;
  69. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  70. procedure ppuwrite(ppufile:tcompilerppufile);override;
  71. procedure derefimpl;override;
  72. function getcopy : tnode;override;
  73. function pass_1 : tnode;override;
  74. function det_resulttype:tnode;override;
  75. {$ifdef var_notification}
  76. procedure mark_write;override;
  77. {$endif}
  78. function docompare(p: tnode): boolean; override;
  79. end;
  80. tfuncretnodeclass = class of tfuncretnode;
  81. tarrayconstructorrangenode = class(tbinarynode)
  82. constructor create(l,r : tnode);virtual;
  83. function pass_1 : tnode;override;
  84. function det_resulttype:tnode;override;
  85. end;
  86. tarrayconstructorrangenodeclass = class of tarrayconstructorrangenode;
  87. tarrayconstructornode = class(tbinarynode)
  88. constructor create(l,r : tnode);virtual;
  89. function getcopy : tnode;override;
  90. function pass_1 : tnode;override;
  91. function det_resulttype:tnode;override;
  92. function docompare(p: tnode): boolean; override;
  93. procedure force_type(tt:ttype);
  94. end;
  95. tarrayconstructornodeclass = class of tarrayconstructornode;
  96. ttypenode = class(tnode)
  97. allowed : boolean;
  98. restype : ttype;
  99. constructor create(t : ttype);virtual;
  100. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  101. procedure ppuwrite(ppufile:tcompilerppufile);override;
  102. procedure derefimpl;override;
  103. function pass_1 : tnode;override;
  104. function det_resulttype:tnode;override;
  105. function docompare(p: tnode): boolean; override;
  106. end;
  107. ttypenodeclass = class of ttypenode;
  108. trttinode = class(tnode)
  109. l1,l2 : longint;
  110. rttitype : trttitype;
  111. rttidef : tstoreddef;
  112. constructor create(def:tstoreddef;rt:trttitype);virtual;
  113. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  114. procedure ppuwrite(ppufile:tcompilerppufile);override;
  115. procedure derefimpl;override;
  116. function getcopy : tnode;override;
  117. function pass_1 : tnode;override;
  118. procedure pass_2;override;
  119. function det_resulttype:tnode;override;
  120. function docompare(p: tnode): boolean; override;
  121. end;
  122. trttinodeclass = class of trttinode;
  123. var
  124. cloadnode : tloadnodeclass;
  125. cassignmentnode : tassignmentnodeclass;
  126. cfuncretnode : tfuncretnodeclass;
  127. carrayconstructorrangenode : tarrayconstructorrangenodeclass;
  128. carrayconstructornode : tarrayconstructornodeclass;
  129. ctypenode : ttypenodeclass;
  130. crttinode : trttinodeclass;
  131. implementation
  132. uses
  133. cutils,verbose,globtype,globals,systems,
  134. symtable,paramgr,defbase,
  135. htypechk,pass_1,
  136. ncon,ninl,ncnv,nmem,ncal,cpubase,rgobj,cginfo,cgbase
  137. ;
  138. {*****************************************************************************
  139. TLOADNODE
  140. *****************************************************************************}
  141. constructor tloadnode.create(v : tsym;st : tsymtable);
  142. begin
  143. inherited create(loadn,nil);
  144. if not assigned(v) then
  145. internalerror(200108121);
  146. symtableentry:=v;
  147. symtable:=st;
  148. procdef:=nil;
  149. end;
  150. constructor tloadnode.create_procvar(v : tsym;d:tprocdef;st : tsymtable);
  151. begin
  152. inherited create(loadn,nil);
  153. if not assigned(v) then
  154. internalerror(200108121);
  155. symtableentry:=v;
  156. symtable:=st;
  157. procdef:=d;
  158. end;
  159. constructor tloadnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  160. begin
  161. inherited ppuload(t,ppufile);
  162. symtableentry:=tsym(ppufile.getderef);
  163. {$warning FIXME: No withsymtable support}
  164. symtable:=nil;
  165. procdef:=tprocdef(ppufile.getderef);
  166. end;
  167. procedure tloadnode.ppuwrite(ppufile:tcompilerppufile);
  168. begin
  169. inherited ppuwrite(ppufile);
  170. ppufile.putderef(symtableentry);
  171. ppufile.putderef(procdef);
  172. end;
  173. procedure tloadnode.derefimpl;
  174. begin
  175. inherited derefimpl;
  176. resolvesym(pointer(symtableentry));
  177. symtable:=symtableentry.owner;
  178. resolvedef(pointer(procdef));
  179. end;
  180. procedure tloadnode.set_mp(p:tnode);
  181. begin
  182. left:=p;
  183. end;
  184. function tloadnode.getcopy : tnode;
  185. var
  186. n : tloadnode;
  187. begin
  188. n:=tloadnode(inherited getcopy);
  189. n.symtable:=symtable;
  190. n.symtableentry:=symtableentry;
  191. result:=n;
  192. end;
  193. function tloadnode.det_resulttype:tnode;
  194. var
  195. p1 : tnode;
  196. p : tprocinfo;
  197. begin
  198. result:=nil;
  199. { optimize simple with loadings }
  200. if (symtable.symtabletype=withsymtable) and
  201. (twithsymtable(symtable).direct_with) and
  202. (symtableentry.typ=varsym) then
  203. begin
  204. p1:=tnode(twithsymtable(symtable).withrefnode).getcopy;
  205. p1:=csubscriptnode.create(tvarsym(symtableentry),p1);
  206. left:=nil;
  207. result:=p1;
  208. exit;
  209. end;
  210. { handle first absolute as it will replace the symtableentry }
  211. if symtableentry.typ=absolutesym then
  212. begin
  213. { force the resulttype to the type of the absolute }
  214. resulttype:=tabsolutesym(symtableentry).vartype;
  215. { replace the symtableentry when it points to a var, else
  216. we are finished }
  217. if tabsolutesym(symtableentry).abstyp=tovar then
  218. begin
  219. symtableentry:=tabsolutesym(symtableentry).ref;
  220. symtable:=symtableentry.owner;
  221. include(flags,nf_absolute);
  222. end
  223. else
  224. exit;
  225. end;
  226. case symtableentry.typ of
  227. funcretsym :
  228. begin
  229. { find the main funcret for the function }
  230. p:=procinfo;
  231. while assigned(p) do
  232. begin
  233. if assigned(p.procdef.funcretsym) and
  234. ((tfuncretsym(symtableentry)=p.procdef.resultfuncretsym) or
  235. (tfuncretsym(symtableentry)=p.procdef.funcretsym)) then
  236. begin
  237. symtableentry:=p.procdef.funcretsym;
  238. break;
  239. end;
  240. p:=p.parent;
  241. end;
  242. { generate funcretnode }
  243. p1:=cfuncretnode.create(symtableentry);
  244. resulttypepass(p1);
  245. { if it's refered as absolute then we need to have the
  246. type of the absolute instead of the function return,
  247. the function return is then also assigned }
  248. if nf_absolute in flags then
  249. begin
  250. tfuncretsym(symtableentry).funcretstate:=vs_assigned;
  251. p1.resulttype:=resulttype;
  252. end;
  253. left:=nil;
  254. result:=p1;
  255. end;
  256. constsym:
  257. begin
  258. if tconstsym(symtableentry).consttyp=constresourcestring then
  259. resulttype:=cansistringtype
  260. else
  261. internalerror(22799);
  262. end;
  263. varsym :
  264. begin
  265. { if it's refered by absolute then it's used }
  266. if nf_absolute in flags then
  267. tvarsym(symtableentry).varstate:=vs_used
  268. else
  269. resulttype:=tvarsym(symtableentry).vartype;
  270. end;
  271. typedconstsym :
  272. if not(nf_absolute in flags) then
  273. resulttype:=ttypedconstsym(symtableentry).typedconsttype;
  274. procsym :
  275. begin
  276. if not assigned(procdef) then
  277. begin
  278. if Tprocsym(symtableentry).procdef_count>1 then
  279. CGMessage(parser_e_no_overloaded_procvars);
  280. resulttype.setdef(tprocsym(symtableentry).first_procdef);
  281. end
  282. else
  283. resulttype.setdef(procdef);
  284. if (m_tp_procvar in aktmodeswitches) then
  285. begin
  286. if assigned(left) then
  287. begin
  288. if left.nodetype=typen then
  289. begin
  290. { we need to return only a voidpointer,
  291. so no need to keep the typen }
  292. left.free;
  293. left:=nil;
  294. end;
  295. end
  296. else
  297. begin
  298. { if the owner of the procsym is a object, }
  299. { left must be set, if left isn't set }
  300. { it can be only self }
  301. if (tprocsym(symtableentry).owner.symtabletype=objectsymtable) then
  302. left:=cselfnode.create(tobjectdef(symtableentry.owner.defowner));
  303. end;
  304. end;
  305. { process methodpointer }
  306. if assigned(left) then
  307. begin
  308. resulttypepass(left);
  309. { turn on the allowed flag, the secondpass
  310. will handle the typen itself }
  311. if left.nodetype=typen then
  312. ttypenode(left).allowed:=true;
  313. end;
  314. end;
  315. else
  316. internalerror(200104141);
  317. end;
  318. end;
  319. {$ifdef var_notification}
  320. procedure Tloadnode.mark_write;
  321. begin
  322. include(flags,nf_write);
  323. end;
  324. {$endif}
  325. function tloadnode.pass_1 : tnode;
  326. begin
  327. result:=nil;
  328. location.loc:=LOC_REFERENCE;
  329. registers32:=0;
  330. registersfpu:=0;
  331. {$ifdef SUPPORT_MMX}
  332. registersmmx:=0;
  333. {$endif SUPPORT_MMX}
  334. case symtableentry.typ of
  335. absolutesym :
  336. ;
  337. funcretsym :
  338. internalerror(200104142);
  339. constsym:
  340. begin
  341. if tconstsym(symtableentry).consttyp=constresourcestring then
  342. begin
  343. { we use ansistrings so no fast exit here }
  344. if assigned(procinfo) then
  345. procinfo.no_fast_exit:=true;
  346. location.loc:=LOC_CREFERENCE;
  347. end;
  348. end;
  349. varsym :
  350. begin
  351. if (symtable.symtabletype in [parasymtable,localsymtable]) and
  352. (lexlevel>symtable.symtablelevel) then
  353. begin
  354. { if the variable is in an other stackframe then we need
  355. a register to dereference }
  356. if (symtable.symtablelevel)>0 then
  357. begin
  358. registers32:=1;
  359. { further, the variable can't be put into a register }
  360. tvarsym(symtableentry).varoptions:=
  361. tvarsym(symtableentry).varoptions-[vo_fpuregable,vo_regable];
  362. end;
  363. end;
  364. if (tvarsym(symtableentry).varspez=vs_const) then
  365. location.loc:=LOC_CREFERENCE;
  366. { we need a register for call by reference parameters }
  367. if (tvarsym(symtableentry).varspez in [vs_var,vs_out]) or
  368. ((tvarsym(symtableentry).varspez=vs_const) and
  369. paramanager.push_addr_param(tvarsym(symtableentry).vartype.def,false)) or
  370. { call by value open arrays are also indirect addressed }
  371. is_open_array(tvarsym(symtableentry).vartype.def) then
  372. registers32:=1;
  373. if symtable.symtabletype=withsymtable then
  374. inc(registers32);
  375. if ([vo_is_thread_var,vo_is_dll_var]*tvarsym(symtableentry).varoptions)<>[] then
  376. registers32:=1;
  377. { count variable references }
  378. { this will create problem with local var set by
  379. under_procedures
  380. if (assigned(tvarsym(symtableentry).owner) and assigned(aktprocsym)
  381. and ((tvarsym(symtableentry).owner = aktprocdef.localst)
  382. or (tvarsym(symtableentry).owner = aktprocdef.localst))) then }
  383. if rg.t_times<1 then
  384. inc(tvarsym(symtableentry).refs)
  385. else
  386. inc(tvarsym(symtableentry).refs,rg.t_times);
  387. end;
  388. typedconstsym :
  389. ;
  390. procsym :
  391. begin
  392. { method pointer ? }
  393. if assigned(left) then
  394. begin
  395. firstpass(left);
  396. registers32:=max(registers32,left.registers32);
  397. registersfpu:=max(registersfpu,left.registersfpu);
  398. {$ifdef SUPPORT_MMX}
  399. registersmmx:=max(registersmmx,left.registersmmx);
  400. {$endif SUPPORT_MMX}
  401. end;
  402. end;
  403. else
  404. internalerror(200104143);
  405. end;
  406. end;
  407. function tloadnode.docompare(p: tnode): boolean;
  408. begin
  409. docompare :=
  410. inherited docompare(p) and
  411. (symtableentry = tloadnode(p).symtableentry) and
  412. (symtable = tloadnode(p).symtable);
  413. end;
  414. {$ifdef extdebug}
  415. procedure Tloadnode._dowrite;
  416. begin
  417. inherited _dowrite;
  418. writeln(',');
  419. system.write(writenodeindention,'symbol = ',symtableentry.name);
  420. end;
  421. {$endif}
  422. {*****************************************************************************
  423. TASSIGNMENTNODE
  424. *****************************************************************************}
  425. constructor tassignmentnode.create(l,r : tnode);
  426. begin
  427. inherited create(assignn,l,r);
  428. {$ifdef var_notification}
  429. l.mark_write;
  430. {$endif}
  431. assigntype:=at_normal;
  432. end;
  433. constructor tassignmentnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  434. begin
  435. inherited ppuload(t,ppufile);
  436. assigntype:=tassigntype(ppufile.getbyte);
  437. end;
  438. procedure tassignmentnode.ppuwrite(ppufile:tcompilerppufile);
  439. begin
  440. inherited ppuwrite(ppufile);
  441. ppufile.putbyte(byte(assigntype));
  442. end;
  443. function tassignmentnode.getcopy : tnode;
  444. var
  445. n : tassignmentnode;
  446. begin
  447. n:=tassignmentnode(inherited getcopy);
  448. n.assigntype:=assigntype;
  449. getcopy:=n;
  450. end;
  451. function tassignmentnode.det_resulttype:tnode;
  452. var
  453. hp : tnode;
  454. useshelper : boolean;
  455. begin
  456. result:=nil;
  457. resulttype:=voidtype;
  458. { must be made unique }
  459. if assigned(left) then
  460. begin
  461. set_unique(left);
  462. { set we the function result? }
  463. set_funcret_is_valid(left);
  464. end;
  465. resulttypepass(left);
  466. resulttypepass(right);
  467. set_varstate(left,false);
  468. set_varstate(right,true);
  469. if codegenerror then
  470. exit;
  471. { assignments to open arrays aren't allowed }
  472. if is_open_array(left.resulttype.def) then
  473. CGMessage(type_e_mismatch);
  474. { test if node can be assigned, properties are allowed }
  475. valid_for_assignment(left);
  476. { assigning nil to a dynamic array clears the array }
  477. if is_dynamic_array(left.resulttype.def) and
  478. (right.nodetype=niln) then
  479. begin
  480. hp:=ccallparanode.create(caddrnode.create
  481. (crttinode.create(tstoreddef(left.resulttype.def),initrtti)),
  482. ccallparanode.create(ctypeconvnode.create_explicit(left,voidpointertype),nil));
  483. result := ccallnode.createintern('fpc_dynarray_clear',hp);
  484. left:=nil;
  485. exit;
  486. end;
  487. { shortstring helpers can do the conversion directly,
  488. so treat them separatly }
  489. if (is_shortstring(left.resulttype.def)) then
  490. begin
  491. { test for s:=s+anything ... }
  492. { the problem is for
  493. s:=s+s+s;
  494. this is broken here !! }
  495. {$ifdef newoptimizations2}
  496. { the above is fixed now, but still problem with s := s + f(); if }
  497. { f modifies s (bad programming, so only enable if uncertain }
  498. { optimizations are on) (JM) }
  499. if (cs_UncertainOpts in aktglobalswitches) then
  500. begin
  501. hp := right;
  502. while hp.treetype=addn do
  503. hp:=hp.left;
  504. if equal_trees(left,hp) and
  505. not multiple_uses(left,right) then
  506. begin
  507. concat_string:=true;
  508. hp:=right;
  509. while hp.treetype=addn do
  510. begin
  511. hp.use_strconcat:=true;
  512. hp:=hp.left;
  513. end;
  514. end;
  515. end;
  516. {$endif newoptimizations2}
  517. { insert typeconv, except for chars that are handled in
  518. secondpass and except for ansi/wide string that can
  519. be converted immediatly }
  520. if not(is_char(right.resulttype.def) or
  521. (right.resulttype.def.deftype=stringdef)) then
  522. inserttypeconv(right,left.resulttype);
  523. if right.resulttype.def.deftype=stringdef then
  524. begin
  525. useshelper:=true;
  526. { convert constant strings to shortstrings. But
  527. skip empty constant strings, that will be handled
  528. in secondpass }
  529. if (right.nodetype=stringconstn) then
  530. begin
  531. inserttypeconv(right,left.resulttype);
  532. if (tstringconstnode(right).len=0) then
  533. useshelper:=false;
  534. end;
  535. if useshelper then
  536. begin
  537. hp:=ccallparanode.create
  538. (right,
  539. ccallparanode.create(cinlinenode.create
  540. (in_high_x,false,left.getcopy),nil));
  541. result:=ccallnode.createinternreturn('fpc_'+tstringdef(right.resulttype.def).stringtypname+'_to_shortstr',hp,left);
  542. left:=nil;
  543. right:=nil;
  544. exit;
  545. end;
  546. end;
  547. end
  548. else
  549. inserttypeconv(right,left.resulttype);
  550. { call helpers for interface }
  551. if is_interfacecom(left.resulttype.def) then
  552. begin
  553. hp:=ccallparanode.create(ctypeconvnode.create_explicit
  554. (right,voidpointertype),
  555. ccallparanode.create(ctypeconvnode.create_explicit
  556. (left,voidpointertype),nil));
  557. result:=ccallnode.createintern('fpc_intf_assign',hp);
  558. left:=nil;
  559. right:=nil;
  560. exit;
  561. end;
  562. { check if local proc/func is assigned to procvar }
  563. if right.resulttype.def.deftype=procvardef then
  564. test_local_to_procvar(tprocvardef(right.resulttype.def),left.resulttype.def);
  565. end;
  566. function tassignmentnode.pass_1 : tnode;
  567. begin
  568. result:=nil;
  569. firstpass(left);
  570. firstpass(right);
  571. if codegenerror then
  572. exit;
  573. registers32:=left.registers32+right.registers32;
  574. registersfpu:=max(left.registersfpu,right.registersfpu);
  575. {$ifdef SUPPORT_MMX}
  576. registersmmx:=max(left.registersmmx,right.registersmmx);
  577. {$endif SUPPORT_MMX}
  578. end;
  579. function tassignmentnode.docompare(p: tnode): boolean;
  580. begin
  581. docompare :=
  582. inherited docompare(p) and
  583. (assigntype = tassignmentnode(p).assigntype);
  584. end;
  585. {$ifdef state_tracking}
  586. function Tassignmentnode.track_state_pass(exec_known:boolean):boolean;
  587. var se:Tstate_entry;
  588. begin
  589. track_state_pass:=false;
  590. if exec_known then
  591. begin
  592. track_state_pass:=right.track_state_pass(exec_known);
  593. {Force a new resulttype pass.}
  594. right.resulttype.def:=nil;
  595. do_resulttypepass(right);
  596. resulttypepass(right);
  597. aktstate.store_fact(left.getcopy,right.getcopy);
  598. end
  599. else
  600. aktstate.delete_fact(left);
  601. end;
  602. {$endif}
  603. {*****************************************************************************
  604. TFUNCRETNODE
  605. *****************************************************************************}
  606. constructor tfuncretnode.create(v:tsym);
  607. begin
  608. inherited create(funcretn);
  609. funcretsym:=tfuncretsym(v);
  610. end;
  611. constructor tfuncretnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  612. begin
  613. inherited ppuload(t,ppufile);
  614. funcretsym:=tfuncretsym(ppufile.getderef);
  615. end;
  616. procedure tfuncretnode.ppuwrite(ppufile:tcompilerppufile);
  617. begin
  618. inherited ppuwrite(ppufile);
  619. ppufile.putderef(funcretsym);
  620. end;
  621. procedure tfuncretnode.derefimpl;
  622. begin
  623. inherited derefimpl;
  624. resolvesym(pointer(funcretsym));
  625. end;
  626. function tfuncretnode.getcopy : tnode;
  627. var
  628. n : tfuncretnode;
  629. begin
  630. n:=tfuncretnode(inherited getcopy);
  631. n.funcretsym:=funcretsym;
  632. getcopy:=n;
  633. end;
  634. function tfuncretnode.det_resulttype:tnode;
  635. begin
  636. result:=nil;
  637. resulttype:=funcretsym.returntype;
  638. end;
  639. {$ifdef var_notification}
  640. procedure Tfuncretnode.mark_write;
  641. begin
  642. include(flags,nf_write);
  643. end;
  644. {$endif}
  645. function tfuncretnode.pass_1 : tnode;
  646. begin
  647. result:=nil;
  648. location.loc:=LOC_REFERENCE;
  649. if paramanager.ret_in_param(resulttype.def) or
  650. (lexlevel<>funcretsym.owner.symtablelevel) then
  651. registers32:=1;
  652. end;
  653. function tfuncretnode.docompare(p: tnode): boolean;
  654. begin
  655. docompare :=
  656. inherited docompare(p) and
  657. (funcretsym = tfuncretnode(p).funcretsym);
  658. end;
  659. {*****************************************************************************
  660. TARRAYCONSTRUCTORRANGENODE
  661. *****************************************************************************}
  662. constructor tarrayconstructorrangenode.create(l,r : tnode);
  663. begin
  664. inherited create(arrayconstructorrangen,l,r);
  665. end;
  666. function tarrayconstructorrangenode.det_resulttype:tnode;
  667. begin
  668. result:=nil;
  669. resulttypepass(left);
  670. resulttypepass(right);
  671. set_varstate(left,true);
  672. set_varstate(right,true);
  673. if codegenerror then
  674. exit;
  675. resulttype:=left.resulttype;
  676. end;
  677. function tarrayconstructorrangenode.pass_1 : tnode;
  678. begin
  679. firstpass(left);
  680. firstpass(right);
  681. location.loc := LOC_CREFERENCE;
  682. calcregisters(self,0,0,0);
  683. result:=nil;
  684. end;
  685. {****************************************************************************
  686. TARRAYCONSTRUCTORNODE
  687. *****************************************************************************}
  688. constructor tarrayconstructornode.create(l,r : tnode);
  689. begin
  690. inherited create(arrayconstructorn,l,r);
  691. end;
  692. function tarrayconstructornode.getcopy : tnode;
  693. var
  694. n : tarrayconstructornode;
  695. begin
  696. n:=tarrayconstructornode(inherited getcopy);
  697. result:=n;
  698. end;
  699. function tarrayconstructornode.det_resulttype:tnode;
  700. var
  701. htype : ttype;
  702. hp : tarrayconstructornode;
  703. len : longint;
  704. varia : boolean;
  705. begin
  706. result:=nil;
  707. { are we allowing array constructor? Then convert it to a set }
  708. if not allow_array_constructor then
  709. begin
  710. hp:=tarrayconstructornode(getcopy);
  711. arrayconstructor_to_set(hp);
  712. result:=hp;
  713. exit;
  714. end;
  715. { only pass left tree, right tree contains next construct if any }
  716. htype.reset;
  717. len:=0;
  718. varia:=false;
  719. if assigned(left) then
  720. begin
  721. hp:=self;
  722. while assigned(hp) do
  723. begin
  724. resulttypepass(hp.left);
  725. set_varstate(hp.left,true);
  726. if (htype.def=nil) then
  727. htype:=hp.left.resulttype
  728. else
  729. begin
  730. if ((nf_novariaallowed in flags) or (not varia)) and
  731. (not is_equal(htype.def,hp.left.resulttype.def)) then
  732. begin
  733. varia:=true;
  734. end;
  735. end;
  736. inc(len);
  737. hp:=tarrayconstructornode(hp.right);
  738. end;
  739. end;
  740. if not assigned(htype.def) then
  741. htype:=voidtype;
  742. resulttype.setdef(tarraydef.create(0,len-1,s32bittype));
  743. tarraydef(resulttype.def).elementtype:=htype;
  744. tarraydef(resulttype.def).IsConstructor:=true;
  745. tarraydef(resulttype.def).IsVariant:=varia;
  746. end;
  747. procedure tarrayconstructornode.force_type(tt:ttype);
  748. var
  749. hp : tarrayconstructornode;
  750. begin
  751. tarraydef(resulttype.def).elementtype:=tt;
  752. tarraydef(resulttype.def).IsConstructor:=true;
  753. tarraydef(resulttype.def).IsVariant:=false;
  754. if assigned(left) then
  755. begin
  756. hp:=self;
  757. while assigned(hp) do
  758. begin
  759. inserttypeconv(hp.left,tt);
  760. hp:=tarrayconstructornode(hp.right);
  761. end;
  762. end;
  763. end;
  764. function tarrayconstructornode.pass_1 : tnode;
  765. var
  766. thp,
  767. chp,
  768. hp : tarrayconstructornode;
  769. dovariant : boolean;
  770. htype : ttype;
  771. orgflags : tnodeflagset;
  772. begin
  773. dovariant:=(nf_forcevaria in flags) or tarraydef(resulttype.def).isvariant;
  774. result:=nil;
  775. { only pass left tree, right tree contains next construct if any }
  776. if assigned(left) then
  777. begin
  778. hp:=self;
  779. while assigned(hp) do
  780. begin
  781. firstpass(hp.left);
  782. { Insert typeconvs for array of const }
  783. if dovariant then
  784. begin
  785. case hp.left.resulttype.def.deftype of
  786. enumdef :
  787. begin
  788. hp.left:=ctypeconvnode.create(hp.left,s32bittype);
  789. firstpass(hp.left);
  790. end;
  791. orddef :
  792. begin
  793. if is_integer(hp.left.resulttype.def) and
  794. not(is_64bitint(hp.left.resulttype.def)) then
  795. begin
  796. hp.left:=ctypeconvnode.create(hp.left,s32bittype);
  797. firstpass(hp.left);
  798. end;
  799. end;
  800. floatdef :
  801. begin
  802. hp.left:=ctypeconvnode.create(hp.left,pbestrealtype^);
  803. firstpass(hp.left);
  804. end;
  805. stringdef :
  806. begin
  807. if nf_cargs in flags then
  808. begin
  809. hp.left:=ctypeconvnode.create(hp.left,charpointertype);
  810. firstpass(hp.left);
  811. end;
  812. end;
  813. procvardef :
  814. begin
  815. hp.left:=ctypeconvnode.create(hp.left,voidpointertype);
  816. firstpass(hp.left);
  817. end;
  818. pointerdef,
  819. classrefdef,
  820. objectdef : ;
  821. else
  822. CGMessagePos1(hp.left.fileinfo,type_e_wrong_type_in_array_constructor,hp.left.resulttype.def.typename);
  823. end;
  824. end;
  825. hp:=tarrayconstructornode(hp.right);
  826. end;
  827. { swap the tree for cargs }
  828. if (nf_cargs in flags) and (not(nf_cargswap in flags)) then
  829. begin
  830. chp:=nil;
  831. { save resulttype }
  832. htype:=resulttype;
  833. { we need a copy here, because self is destroyed }
  834. { by firstpass later }
  835. hp:=tarrayconstructornode(getcopy);
  836. { we also need a copy of the nf_ forcevaria flag to restore }
  837. { later) (JM) }
  838. orgflags := flags * [nf_forcevaria];
  839. while assigned(hp) do
  840. begin
  841. thp:=tarrayconstructornode(hp.right);
  842. hp.right:=chp;
  843. chp:=hp;
  844. hp:=thp;
  845. end;
  846. chp.flags := chp.flags+orgflags;
  847. include(chp.flags,nf_cargswap);
  848. chp.location.loc:=LOC_CREFERENCE;
  849. calcregisters(chp,0,0,0);
  850. chp.resulttype:=htype;
  851. result:=chp;
  852. exit;
  853. end;
  854. end;
  855. { C Arguments are pushed on the stack and
  856. are not accesible after the push }
  857. if not(nf_cargs in flags) then
  858. location.loc:=LOC_CREFERENCE
  859. else
  860. location.loc:=LOC_INVALID;
  861. calcregisters(self,0,0,0);
  862. end;
  863. function tarrayconstructornode.docompare(p: tnode): boolean;
  864. begin
  865. docompare :=
  866. inherited docompare(p);
  867. end;
  868. {*****************************************************************************
  869. TTYPENODE
  870. *****************************************************************************}
  871. constructor ttypenode.create(t : ttype);
  872. begin
  873. inherited create(typen);
  874. restype:=t;
  875. allowed:=false;
  876. end;
  877. constructor ttypenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  878. begin
  879. inherited ppuload(t,ppufile);
  880. ppufile.gettype(restype);
  881. allowed:=boolean(ppufile.getbyte);
  882. end;
  883. procedure ttypenode.ppuwrite(ppufile:tcompilerppufile);
  884. begin
  885. inherited ppuwrite(ppufile);
  886. ppufile.puttype(restype);
  887. ppufile.putbyte(byte(allowed));
  888. end;
  889. procedure ttypenode.derefimpl;
  890. begin
  891. inherited derefimpl;
  892. restype.resolve;
  893. end;
  894. function ttypenode.det_resulttype:tnode;
  895. begin
  896. result:=nil;
  897. resulttype:=restype;
  898. { check if it's valid }
  899. if restype.def.deftype = errordef then
  900. CGMessage(cg_e_illegal_expression);
  901. end;
  902. function ttypenode.pass_1 : tnode;
  903. begin
  904. result:=nil;
  905. { a typenode can't generate code, so we give here
  906. an error. Else it'll be an abstract error in pass_2.
  907. Only when the allowed flag is set we don't generate
  908. an error }
  909. if not allowed then
  910. Message(parser_e_no_type_not_allowed_here);
  911. end;
  912. function ttypenode.docompare(p: tnode): boolean;
  913. begin
  914. docompare :=
  915. inherited docompare(p);
  916. end;
  917. {*****************************************************************************
  918. TRTTINODE
  919. *****************************************************************************}
  920. constructor trttinode.create(def:tstoreddef;rt:trttitype);
  921. begin
  922. inherited create(rttin);
  923. rttidef:=def;
  924. rttitype:=rt;
  925. end;
  926. constructor trttinode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  927. begin
  928. inherited ppuload(t,ppufile);
  929. rttidef:=tstoreddef(ppufile.getderef);
  930. rttitype:=trttitype(ppufile.getbyte);
  931. end;
  932. procedure trttinode.ppuwrite(ppufile:tcompilerppufile);
  933. begin
  934. inherited ppuwrite(ppufile);
  935. ppufile.putderef(rttidef);
  936. ppufile.putbyte(byte(rttitype));
  937. end;
  938. procedure trttinode.derefimpl;
  939. begin
  940. inherited derefimpl;
  941. resolvedef(pointer(rttidef));
  942. end;
  943. function trttinode.getcopy : tnode;
  944. var
  945. n : trttinode;
  946. begin
  947. n:=trttinode(inherited getcopy);
  948. n.rttidef:=rttidef;
  949. n.rttitype:=rttitype;
  950. result:=n;
  951. end;
  952. function trttinode.det_resulttype:tnode;
  953. begin
  954. { rtti information will be returned as a void pointer }
  955. result:=nil;
  956. resulttype:=voidpointertype;
  957. end;
  958. function trttinode.pass_1 : tnode;
  959. begin
  960. result:=nil;
  961. location.loc:=LOC_CREFERENCE;
  962. end;
  963. function trttinode.docompare(p: tnode): boolean;
  964. begin
  965. docompare :=
  966. inherited docompare(p) and
  967. (rttidef = trttinode(p).rttidef) and
  968. (rttitype = trttinode(p).rttitype);
  969. end;
  970. procedure trttinode.pass_2;
  971. begin
  972. location_reset(location,LOC_CREFERENCE,OS_NO);
  973. location.reference.symbol:=rttidef.get_rtti_label(rttitype);
  974. end;
  975. begin
  976. cloadnode:=tloadnode;
  977. cassignmentnode:=tassignmentnode;
  978. cfuncretnode:=tfuncretnode;
  979. carrayconstructorrangenode:=tarrayconstructorrangenode;
  980. carrayconstructornode:=tarrayconstructornode;
  981. ctypenode:=ttypenode;
  982. crttinode:=trttinode;
  983. end.
  984. {
  985. $Log$
  986. Revision 1.57 2002-09-03 16:26:26 daniel
  987. * Make Tprocdef.defs protected
  988. Revision 1.56 2002/09/01 13:28:37 daniel
  989. - write_access fields removed in favor of a flag
  990. Revision 1.55 2002/09/01 08:01:16 daniel
  991. * Removed sets from Tcallnode.det_resulttype
  992. + Added read/write notifications of variables. These will be usefull
  993. for providing information for several optimizations. For example
  994. the value of the loop variable of a for loop does matter is the
  995. variable is read after the for loop, but if it's no longer used
  996. or written, it doesn't matter and this can be used to optimize
  997. the loop code generation.
  998. Revision 1.54 2002/08/25 19:25:19 peter
  999. * sym.insert_in_data removed
  1000. * symtable.insertvardata/insertconstdata added
  1001. * removed insert_in_data call from symtable.insert, it needs to be
  1002. called separatly. This allows to deref the address calculation
  1003. * procedures now calculate the parast addresses after the procedure
  1004. directives are parsed. This fixes the cdecl parast problem
  1005. * push_addr_param has an extra argument that specifies if cdecl is used
  1006. or not
  1007. Revision 1.53 2002/08/19 19:36:43 peter
  1008. * More fixes for cross unit inlining, all tnodes are now implemented
  1009. * Moved pocall_internconst to po_internconst because it is not a
  1010. calling type at all and it conflicted when inlining of these small
  1011. functions was requested
  1012. Revision 1.52 2002/08/18 20:06:23 peter
  1013. * inlining is now also allowed in interface
  1014. * renamed write/load to ppuwrite/ppuload
  1015. * tnode storing in ppu
  1016. * nld,ncon,nbas are already updated for storing in ppu
  1017. Revision 1.51 2002/08/17 22:09:46 florian
  1018. * result type handling in tcgcal.pass_2 overhauled
  1019. * better tnode.dowrite
  1020. * some ppc stuff fixed
  1021. Revision 1.50 2002/08/17 09:23:37 florian
  1022. * first part of procinfo rewrite
  1023. Revision 1.49 2002/07/20 11:57:54 florian
  1024. * types.pas renamed to defbase.pas because D6 contains a types
  1025. unit so this would conflicts if D6 programms are compiled
  1026. + Willamette/SSE2 instructions to assembler added
  1027. Revision 1.48 2002/07/20 07:44:37 daniel
  1028. * Forgot to add a $ifdef extdebug
  1029. Revision 1.47 2002/07/19 12:55:27 daniel
  1030. * Further developed state tracking in whilerepeatn
  1031. Revision 1.46 2002/07/19 11:41:36 daniel
  1032. * State tracker work
  1033. * The whilen and repeatn are now completely unified into whilerepeatn. This
  1034. allows the state tracker to change while nodes automatically into
  1035. repeat nodes.
  1036. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  1037. 'not(a>b)' is optimized into 'a<=b'.
  1038. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  1039. by removing the notn and later switchting the true and falselabels. The
  1040. same is done with 'repeat until not a'.
  1041. Revision 1.45 2002/07/15 18:03:15 florian
  1042. * readded removed changes
  1043. Revision 1.43 2002/07/11 14:41:28 florian
  1044. * start of the new generic parameter handling
  1045. Revision 1.44 2002/07/14 18:00:44 daniel
  1046. + Added the beginning of a state tracker. This will track the values of
  1047. variables through procedures and optimize things away.
  1048. Revision 1.42 2002/05/18 13:34:10 peter
  1049. * readded missing revisions
  1050. Revision 1.41 2002/05/16 19:46:38 carl
  1051. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1052. + try to fix temp allocation (still in ifdef)
  1053. + generic constructor calls
  1054. + start of tassembler / tmodulebase class cleanup
  1055. Revision 1.39 2002/05/12 16:53:07 peter
  1056. * moved entry and exitcode to ncgutil and cgobj
  1057. * foreach gets extra argument for passing local data to the
  1058. iterator function
  1059. * -CR checks also class typecasts at runtime by changing them
  1060. into as
  1061. * fixed compiler to cycle with the -CR option
  1062. * fixed stabs with elf writer, finally the global variables can
  1063. be watched
  1064. * removed a lot of routines from cga unit and replaced them by
  1065. calls to cgobj
  1066. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1067. u32bit then the other is typecasted also to u32bit without giving
  1068. a rangecheck warning/error.
  1069. * fixed pascal calling method with reversing also the high tree in
  1070. the parast, detected by tcalcst3 test
  1071. Revision 1.38 2002/04/25 20:16:39 peter
  1072. * moved more routines from cga/n386util
  1073. Revision 1.37 2002/04/23 19:16:34 peter
  1074. * add pinline unit that inserts compiler supported functions using
  1075. one or more statements
  1076. * moved finalize and setlength from ninl to pinline
  1077. Revision 1.36 2002/04/22 16:30:06 peter
  1078. * fixed @methodpointer
  1079. Revision 1.35 2002/04/21 19:02:04 peter
  1080. * removed newn and disposen nodes, the code is now directly
  1081. inlined from pexpr
  1082. * -an option that will write the secondpass nodes to the .s file, this
  1083. requires EXTDEBUG define to actually write the info
  1084. * fixed various internal errors and crashes due recent code changes
  1085. Revision 1.34 2002/04/02 17:11:29 peter
  1086. * tlocation,treference update
  1087. * LOC_CONSTANT added for better constant handling
  1088. * secondadd splitted in multiple routines
  1089. * location_force_reg added for loading a location to a register
  1090. of a specified size
  1091. * secondassignment parses now first the right and then the left node
  1092. (this is compatible with Kylix). This saves a lot of push/pop especially
  1093. with string operations
  1094. * adapted some routines to use the new cg methods
  1095. Revision 1.33 2002/03/31 20:26:34 jonas
  1096. + a_loadfpu_* and a_loadmm_* methods in tcg
  1097. * register allocation is now handled by a class and is mostly processor
  1098. independent (+rgobj.pas and i386/rgcpu.pas)
  1099. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  1100. * some small improvements and fixes to the optimizer
  1101. * some register allocation fixes
  1102. * some fpuvaroffset fixes in the unary minus node
  1103. * push/popusedregisters is now called rg.save/restoreusedregisters and
  1104. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  1105. also better optimizable)
  1106. * fixed and optimized register saving/restoring for new/dispose nodes
  1107. * LOC_FPU locations now also require their "register" field to be set to
  1108. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  1109. - list field removed of the tnode class because it's not used currently
  1110. and can cause hard-to-find bugs
  1111. Revision 1.32 2002/01/19 11:52:32 peter
  1112. * dynarr:=nil support added
  1113. }