nld.pas 45 KB

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