nld.pas 42 KB

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