nld.pas 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  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. l : longint;
  456. begin
  457. result:=nil;
  458. resulttype:=voidtype;
  459. { must be made unique }
  460. if assigned(left) then
  461. begin
  462. set_unique(left);
  463. { set we the function result? }
  464. set_funcret_is_valid(left);
  465. end;
  466. resulttypepass(left);
  467. resulttypepass(right);
  468. set_varstate(left,false);
  469. set_varstate(right,true);
  470. if codegenerror then
  471. exit;
  472. { assignments to open arrays aren't allowed }
  473. if is_open_array(left.resulttype.def) then
  474. CGMessage(type_e_mismatch);
  475. { test if node can be assigned, properties are allowed }
  476. valid_for_assignment(left);
  477. { assigning nil to a dynamic array clears the array }
  478. if is_dynamic_array(left.resulttype.def) and
  479. (right.nodetype=niln) then
  480. begin
  481. hp:=ccallparanode.create(caddrnode.create
  482. (crttinode.create(tstoreddef(left.resulttype.def),initrtti)),
  483. ccallparanode.create(ctypeconvnode.create_explicit(left,voidpointertype),nil));
  484. result := ccallnode.createintern('fpc_dynarray_clear',hp);
  485. left:=nil;
  486. exit;
  487. end;
  488. { shortstring helpers can do the conversion directly,
  489. so treat them separatly }
  490. if (is_shortstring(left.resulttype.def)) then
  491. begin
  492. { test for s:=s+anything ... }
  493. { the problem is for
  494. s:=s+s+s;
  495. this is broken here !! }
  496. {$ifdef newoptimizations2}
  497. { the above is fixed now, but still problem with s := s + f(); if }
  498. { f modifies s (bad programming, so only enable if uncertain }
  499. { optimizations are on) (JM) }
  500. if (cs_UncertainOpts in aktglobalswitches) then
  501. begin
  502. hp := right;
  503. while hp.treetype=addn do
  504. hp:=hp.left;
  505. if equal_trees(left,hp) and
  506. not multiple_uses(left,right) then
  507. begin
  508. concat_string:=true;
  509. hp:=right;
  510. while hp.treetype=addn do
  511. begin
  512. hp.use_strconcat:=true;
  513. hp:=hp.left;
  514. end;
  515. end;
  516. end;
  517. {$endif newoptimizations2}
  518. { insert typeconv, except for chars that are handled in
  519. secondpass and except for ansi/wide string that can
  520. be converted immediatly }
  521. if not(is_char(right.resulttype.def) or
  522. (right.resulttype.def.deftype=stringdef)) then
  523. inserttypeconv(right,left.resulttype);
  524. if right.resulttype.def.deftype=stringdef then
  525. begin
  526. useshelper:=true;
  527. { convert constant strings to shortstrings. But
  528. skip empty constant strings, that will be handled
  529. in secondpass }
  530. if (right.nodetype=stringconstn) then
  531. begin
  532. { verify if range fits within shortstring }
  533. { just emit a warning, delphi gives an }
  534. { error, only if the type definition of }
  535. { of the string is less < 255 characters }
  536. if (tstringconstnode(right).len
  537. > tstringdef(left.resulttype.def).len) then
  538. cgmessage(type_w_string_too_long);
  539. inserttypeconv(right,left.resulttype);
  540. if (tstringconstnode(right).len=0) then
  541. useshelper:=false;
  542. end;
  543. if useshelper then
  544. begin
  545. hp:=ccallparanode.create
  546. (right,
  547. ccallparanode.create(cinlinenode.create
  548. (in_high_x,false,left.getcopy),nil));
  549. result:=ccallnode.createinternreturn('fpc_'+tstringdef(right.resulttype.def).stringtypname+'_to_shortstr',hp,left);
  550. left:=nil;
  551. right:=nil;
  552. exit;
  553. end;
  554. end;
  555. end
  556. else
  557. inserttypeconv(right,left.resulttype);
  558. { call helpers for interface }
  559. if is_interfacecom(left.resulttype.def) then
  560. begin
  561. hp:=ccallparanode.create(ctypeconvnode.create_explicit
  562. (right,voidpointertype),
  563. ccallparanode.create(ctypeconvnode.create_explicit
  564. (left,voidpointertype),nil));
  565. result:=ccallnode.createintern('fpc_intf_assign',hp);
  566. left:=nil;
  567. right:=nil;
  568. exit;
  569. end;
  570. { check if local proc/func is assigned to procvar }
  571. if right.resulttype.def.deftype=procvardef then
  572. test_local_to_procvar(tprocvardef(right.resulttype.def),left.resulttype.def);
  573. end;
  574. function tassignmentnode.pass_1 : tnode;
  575. begin
  576. result:=nil;
  577. firstpass(left);
  578. firstpass(right);
  579. if codegenerror then
  580. exit;
  581. registers32:=left.registers32+right.registers32;
  582. registersfpu:=max(left.registersfpu,right.registersfpu);
  583. {$ifdef SUPPORT_MMX}
  584. registersmmx:=max(left.registersmmx,right.registersmmx);
  585. {$endif SUPPORT_MMX}
  586. end;
  587. function tassignmentnode.docompare(p: tnode): boolean;
  588. begin
  589. docompare :=
  590. inherited docompare(p) and
  591. (assigntype = tassignmentnode(p).assigntype);
  592. end;
  593. {$ifdef state_tracking}
  594. function Tassignmentnode.track_state_pass(exec_known:boolean):boolean;
  595. var se:Tstate_entry;
  596. begin
  597. track_state_pass:=false;
  598. if exec_known then
  599. begin
  600. track_state_pass:=right.track_state_pass(exec_known);
  601. {Force a new resulttype pass.}
  602. right.resulttype.def:=nil;
  603. do_resulttypepass(right);
  604. resulttypepass(right);
  605. aktstate.store_fact(left.getcopy,right.getcopy);
  606. end
  607. else
  608. aktstate.delete_fact(left);
  609. end;
  610. {$endif}
  611. {*****************************************************************************
  612. TFUNCRETNODE
  613. *****************************************************************************}
  614. constructor tfuncretnode.create(v:tsym);
  615. begin
  616. inherited create(funcretn);
  617. funcretsym:=tfuncretsym(v);
  618. end;
  619. constructor tfuncretnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  620. begin
  621. inherited ppuload(t,ppufile);
  622. funcretsym:=tfuncretsym(ppufile.getderef);
  623. end;
  624. procedure tfuncretnode.ppuwrite(ppufile:tcompilerppufile);
  625. begin
  626. inherited ppuwrite(ppufile);
  627. ppufile.putderef(funcretsym);
  628. end;
  629. procedure tfuncretnode.derefimpl;
  630. begin
  631. inherited derefimpl;
  632. resolvesym(pointer(funcretsym));
  633. end;
  634. function tfuncretnode.getcopy : tnode;
  635. var
  636. n : tfuncretnode;
  637. begin
  638. n:=tfuncretnode(inherited getcopy);
  639. n.funcretsym:=funcretsym;
  640. getcopy:=n;
  641. end;
  642. function tfuncretnode.det_resulttype:tnode;
  643. begin
  644. result:=nil;
  645. resulttype:=funcretsym.returntype;
  646. end;
  647. {$ifdef var_notification}
  648. procedure Tfuncretnode.mark_write;
  649. begin
  650. include(flags,nf_write);
  651. end;
  652. {$endif}
  653. function tfuncretnode.pass_1 : tnode;
  654. begin
  655. result:=nil;
  656. location.loc:=LOC_REFERENCE;
  657. if paramanager.ret_in_param(resulttype.def) or
  658. (lexlevel<>funcretsym.owner.symtablelevel) then
  659. registers32:=1;
  660. end;
  661. function tfuncretnode.docompare(p: tnode): boolean;
  662. begin
  663. docompare :=
  664. inherited docompare(p) and
  665. (funcretsym = tfuncretnode(p).funcretsym);
  666. end;
  667. {*****************************************************************************
  668. TARRAYCONSTRUCTORRANGENODE
  669. *****************************************************************************}
  670. constructor tarrayconstructorrangenode.create(l,r : tnode);
  671. begin
  672. inherited create(arrayconstructorrangen,l,r);
  673. end;
  674. function tarrayconstructorrangenode.det_resulttype:tnode;
  675. begin
  676. result:=nil;
  677. resulttypepass(left);
  678. resulttypepass(right);
  679. set_varstate(left,true);
  680. set_varstate(right,true);
  681. if codegenerror then
  682. exit;
  683. resulttype:=left.resulttype;
  684. end;
  685. function tarrayconstructorrangenode.pass_1 : tnode;
  686. begin
  687. firstpass(left);
  688. firstpass(right);
  689. location.loc := LOC_CREFERENCE;
  690. calcregisters(self,0,0,0);
  691. result:=nil;
  692. end;
  693. {****************************************************************************
  694. TARRAYCONSTRUCTORNODE
  695. *****************************************************************************}
  696. constructor tarrayconstructornode.create(l,r : tnode);
  697. begin
  698. inherited create(arrayconstructorn,l,r);
  699. end;
  700. function tarrayconstructornode.getcopy : tnode;
  701. var
  702. n : tarrayconstructornode;
  703. begin
  704. n:=tarrayconstructornode(inherited getcopy);
  705. result:=n;
  706. end;
  707. function tarrayconstructornode.det_resulttype:tnode;
  708. var
  709. htype : ttype;
  710. hp : tarrayconstructornode;
  711. len : longint;
  712. varia : boolean;
  713. begin
  714. result:=nil;
  715. { are we allowing array constructor? Then convert it to a set }
  716. if not allow_array_constructor then
  717. begin
  718. hp:=tarrayconstructornode(getcopy);
  719. arrayconstructor_to_set(hp);
  720. result:=hp;
  721. exit;
  722. end;
  723. { only pass left tree, right tree contains next construct if any }
  724. htype.reset;
  725. len:=0;
  726. varia:=false;
  727. if assigned(left) then
  728. begin
  729. hp:=self;
  730. while assigned(hp) do
  731. begin
  732. resulttypepass(hp.left);
  733. set_varstate(hp.left,true);
  734. if (htype.def=nil) then
  735. htype:=hp.left.resulttype
  736. else
  737. begin
  738. if ((nf_novariaallowed in flags) or (not varia)) and
  739. (not is_equal(htype.def,hp.left.resulttype.def)) then
  740. begin
  741. varia:=true;
  742. end;
  743. end;
  744. inc(len);
  745. hp:=tarrayconstructornode(hp.right);
  746. end;
  747. end;
  748. if not assigned(htype.def) then
  749. htype:=voidtype;
  750. resulttype.setdef(tarraydef.create(0,len-1,s32bittype));
  751. tarraydef(resulttype.def).setelementtype(htype);
  752. tarraydef(resulttype.def).IsConstructor:=true;
  753. tarraydef(resulttype.def).IsVariant:=varia;
  754. end;
  755. procedure tarrayconstructornode.force_type(tt:ttype);
  756. var
  757. hp : tarrayconstructornode;
  758. begin
  759. tarraydef(resulttype.def).setelementtype(tt);
  760. tarraydef(resulttype.def).IsConstructor:=true;
  761. tarraydef(resulttype.def).IsVariant:=false;
  762. if assigned(left) then
  763. begin
  764. hp:=self;
  765. while assigned(hp) do
  766. begin
  767. inserttypeconv(hp.left,tt);
  768. hp:=tarrayconstructornode(hp.right);
  769. end;
  770. end;
  771. end;
  772. function tarrayconstructornode.pass_1 : tnode;
  773. var
  774. thp,
  775. chp,
  776. hp : tarrayconstructornode;
  777. dovariant : boolean;
  778. htype : ttype;
  779. orgflags : tnodeflagset;
  780. begin
  781. dovariant:=(nf_forcevaria in flags) or tarraydef(resulttype.def).isvariant;
  782. result:=nil;
  783. { only pass left tree, right tree contains next construct if any }
  784. if assigned(left) then
  785. begin
  786. hp:=self;
  787. while assigned(hp) do
  788. begin
  789. firstpass(hp.left);
  790. { Insert typeconvs for array of const }
  791. if dovariant then
  792. begin
  793. case hp.left.resulttype.def.deftype of
  794. enumdef :
  795. begin
  796. hp.left:=ctypeconvnode.create(hp.left,s32bittype);
  797. firstpass(hp.left);
  798. end;
  799. orddef :
  800. begin
  801. if is_integer(hp.left.resulttype.def) and
  802. not(is_64bitint(hp.left.resulttype.def)) then
  803. begin
  804. hp.left:=ctypeconvnode.create(hp.left,s32bittype);
  805. firstpass(hp.left);
  806. end;
  807. end;
  808. floatdef :
  809. begin
  810. hp.left:=ctypeconvnode.create(hp.left,pbestrealtype^);
  811. firstpass(hp.left);
  812. end;
  813. stringdef :
  814. begin
  815. if nf_cargs in flags then
  816. begin
  817. hp.left:=ctypeconvnode.create(hp.left,charpointertype);
  818. firstpass(hp.left);
  819. end;
  820. end;
  821. procvardef :
  822. begin
  823. hp.left:=ctypeconvnode.create(hp.left,voidpointertype);
  824. firstpass(hp.left);
  825. end;
  826. variantdef,
  827. pointerdef,
  828. classrefdef,
  829. objectdef : ;
  830. else
  831. CGMessagePos1(hp.left.fileinfo,type_e_wrong_type_in_array_constructor,hp.left.resulttype.def.typename);
  832. end;
  833. end;
  834. hp:=tarrayconstructornode(hp.right);
  835. end;
  836. { swap the tree for cargs }
  837. if (nf_cargs in flags) and (not(nf_cargswap in flags)) then
  838. begin
  839. chp:=nil;
  840. { save resulttype }
  841. htype:=resulttype;
  842. { we need a copy here, because self is destroyed }
  843. { by firstpass later }
  844. hp:=tarrayconstructornode(getcopy);
  845. { we also need a copy of the nf_ forcevaria flag to restore }
  846. { later) (JM) }
  847. orgflags := flags * [nf_forcevaria];
  848. while assigned(hp) do
  849. begin
  850. thp:=tarrayconstructornode(hp.right);
  851. hp.right:=chp;
  852. chp:=hp;
  853. hp:=thp;
  854. end;
  855. chp.flags := chp.flags+orgflags;
  856. include(chp.flags,nf_cargswap);
  857. chp.location.loc:=LOC_CREFERENCE;
  858. calcregisters(chp,0,0,0);
  859. chp.resulttype:=htype;
  860. result:=chp;
  861. exit;
  862. end;
  863. end;
  864. { C Arguments are pushed on the stack and
  865. are not accesible after the push }
  866. if not(nf_cargs in flags) then
  867. location.loc:=LOC_CREFERENCE
  868. else
  869. location.loc:=LOC_INVALID;
  870. calcregisters(self,0,0,0);
  871. end;
  872. function tarrayconstructornode.docompare(p: tnode): boolean;
  873. begin
  874. docompare :=
  875. inherited docompare(p);
  876. end;
  877. {*****************************************************************************
  878. TTYPENODE
  879. *****************************************************************************}
  880. constructor ttypenode.create(t : ttype);
  881. begin
  882. inherited create(typen);
  883. restype:=t;
  884. allowed:=false;
  885. end;
  886. constructor ttypenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  887. begin
  888. inherited ppuload(t,ppufile);
  889. ppufile.gettype(restype);
  890. allowed:=boolean(ppufile.getbyte);
  891. end;
  892. procedure ttypenode.ppuwrite(ppufile:tcompilerppufile);
  893. begin
  894. inherited ppuwrite(ppufile);
  895. ppufile.puttype(restype);
  896. ppufile.putbyte(byte(allowed));
  897. end;
  898. procedure ttypenode.derefimpl;
  899. begin
  900. inherited derefimpl;
  901. restype.resolve;
  902. end;
  903. function ttypenode.det_resulttype:tnode;
  904. begin
  905. result:=nil;
  906. resulttype:=restype;
  907. { check if it's valid }
  908. if restype.def.deftype = errordef then
  909. CGMessage(cg_e_illegal_expression);
  910. end;
  911. function ttypenode.pass_1 : tnode;
  912. begin
  913. result:=nil;
  914. { a typenode can't generate code, so we give here
  915. an error. Else it'll be an abstract error in pass_2.
  916. Only when the allowed flag is set we don't generate
  917. an error }
  918. if not allowed then
  919. Message(parser_e_no_type_not_allowed_here);
  920. end;
  921. function ttypenode.docompare(p: tnode): boolean;
  922. begin
  923. docompare :=
  924. inherited docompare(p);
  925. end;
  926. {*****************************************************************************
  927. TRTTINODE
  928. *****************************************************************************}
  929. constructor trttinode.create(def:tstoreddef;rt:trttitype);
  930. begin
  931. inherited create(rttin);
  932. rttidef:=def;
  933. rttitype:=rt;
  934. end;
  935. constructor trttinode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  936. begin
  937. inherited ppuload(t,ppufile);
  938. rttidef:=tstoreddef(ppufile.getderef);
  939. rttitype:=trttitype(ppufile.getbyte);
  940. end;
  941. procedure trttinode.ppuwrite(ppufile:tcompilerppufile);
  942. begin
  943. inherited ppuwrite(ppufile);
  944. ppufile.putderef(rttidef);
  945. ppufile.putbyte(byte(rttitype));
  946. end;
  947. procedure trttinode.derefimpl;
  948. begin
  949. inherited derefimpl;
  950. resolvedef(pointer(rttidef));
  951. end;
  952. function trttinode.getcopy : tnode;
  953. var
  954. n : trttinode;
  955. begin
  956. n:=trttinode(inherited getcopy);
  957. n.rttidef:=rttidef;
  958. n.rttitype:=rttitype;
  959. result:=n;
  960. end;
  961. function trttinode.det_resulttype:tnode;
  962. begin
  963. { rtti information will be returned as a void pointer }
  964. result:=nil;
  965. resulttype:=voidpointertype;
  966. end;
  967. function trttinode.pass_1 : tnode;
  968. begin
  969. result:=nil;
  970. location.loc:=LOC_CREFERENCE;
  971. end;
  972. function trttinode.docompare(p: tnode): boolean;
  973. begin
  974. docompare :=
  975. inherited docompare(p) and
  976. (rttidef = trttinode(p).rttidef) and
  977. (rttitype = trttinode(p).rttitype);
  978. end;
  979. procedure trttinode.pass_2;
  980. begin
  981. location_reset(location,LOC_CREFERENCE,OS_NO);
  982. location.reference.symbol:=rttidef.get_rtti_label(rttitype);
  983. end;
  984. begin
  985. cloadnode:=tloadnode;
  986. cassignmentnode:=tassignmentnode;
  987. cfuncretnode:=tfuncretnode;
  988. carrayconstructorrangenode:=tarrayconstructorrangenode;
  989. carrayconstructornode:=tarrayconstructornode;
  990. ctypenode:=ttypenode;
  991. crttinode:=trttinode;
  992. end.
  993. {
  994. $Log$
  995. Revision 1.61 2002-10-03 21:26:08 carl
  996. + compile-time range checking for strings
  997. Revision 1.60 2002/09/27 21:13:28 carl
  998. * low-highval always checked if limit ober 2GB is reached (to avoid overflow)
  999. Revision 1.59 2002/09/26 15:02:05 florian
  1000. + support of passing variants to "array of const"
  1001. Revision 1.58 2002/09/07 15:25:03 peter
  1002. * old logs removed and tabs fixed
  1003. Revision 1.57 2002/09/03 16:26:26 daniel
  1004. * Make Tprocdef.defs protected
  1005. Revision 1.56 2002/09/01 13:28:37 daniel
  1006. - write_access fields removed in favor of a flag
  1007. Revision 1.55 2002/09/01 08:01:16 daniel
  1008. * Removed sets from Tcallnode.det_resulttype
  1009. + Added read/write notifications of variables. These will be usefull
  1010. for providing information for several optimizations. For example
  1011. the value of the loop variable of a for loop does matter is the
  1012. variable is read after the for loop, but if it's no longer used
  1013. or written, it doesn't matter and this can be used to optimize
  1014. the loop code generation.
  1015. Revision 1.54 2002/08/25 19:25:19 peter
  1016. * sym.insert_in_data removed
  1017. * symtable.insertvardata/insertconstdata added
  1018. * removed insert_in_data call from symtable.insert, it needs to be
  1019. called separatly. This allows to deref the address calculation
  1020. * procedures now calculate the parast addresses after the procedure
  1021. directives are parsed. This fixes the cdecl parast problem
  1022. * push_addr_param has an extra argument that specifies if cdecl is used
  1023. or not
  1024. Revision 1.53 2002/08/19 19:36:43 peter
  1025. * More fixes for cross unit inlining, all tnodes are now implemented
  1026. * Moved pocall_internconst to po_internconst because it is not a
  1027. calling type at all and it conflicted when inlining of these small
  1028. functions was requested
  1029. Revision 1.52 2002/08/18 20:06:23 peter
  1030. * inlining is now also allowed in interface
  1031. * renamed write/load to ppuwrite/ppuload
  1032. * tnode storing in ppu
  1033. * nld,ncon,nbas are already updated for storing in ppu
  1034. Revision 1.51 2002/08/17 22:09:46 florian
  1035. * result type handling in tcgcal.pass_2 overhauled
  1036. * better tnode.dowrite
  1037. * some ppc stuff fixed
  1038. Revision 1.50 2002/08/17 09:23:37 florian
  1039. * first part of procinfo rewrite
  1040. Revision 1.49 2002/07/20 11:57:54 florian
  1041. * types.pas renamed to defbase.pas because D6 contains a types
  1042. unit so this would conflicts if D6 programms are compiled
  1043. + Willamette/SSE2 instructions to assembler added
  1044. Revision 1.48 2002/07/20 07:44:37 daniel
  1045. * Forgot to add a $ifdef extdebug
  1046. Revision 1.47 2002/07/19 12:55:27 daniel
  1047. * Further developed state tracking in whilerepeatn
  1048. Revision 1.46 2002/07/19 11:41:36 daniel
  1049. * State tracker work
  1050. * The whilen and repeatn are now completely unified into whilerepeatn. This
  1051. allows the state tracker to change while nodes automatically into
  1052. repeat nodes.
  1053. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  1054. 'not(a>b)' is optimized into 'a<=b'.
  1055. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  1056. by removing the notn and later switchting the true and falselabels. The
  1057. same is done with 'repeat until not a'.
  1058. Revision 1.45 2002/07/15 18:03:15 florian
  1059. * readded removed changes
  1060. Revision 1.43 2002/07/11 14:41:28 florian
  1061. * start of the new generic parameter handling
  1062. Revision 1.44 2002/07/14 18:00:44 daniel
  1063. + Added the beginning of a state tracker. This will track the values of
  1064. variables through procedures and optimize things away.
  1065. Revision 1.42 2002/05/18 13:34:10 peter
  1066. * readded missing revisions
  1067. Revision 1.41 2002/05/16 19:46:38 carl
  1068. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1069. + try to fix temp allocation (still in ifdef)
  1070. + generic constructor calls
  1071. + start of tassembler / tmodulebase class cleanup
  1072. Revision 1.39 2002/05/12 16:53:07 peter
  1073. * moved entry and exitcode to ncgutil and cgobj
  1074. * foreach gets extra argument for passing local data to the
  1075. iterator function
  1076. * -CR checks also class typecasts at runtime by changing them
  1077. into as
  1078. * fixed compiler to cycle with the -CR option
  1079. * fixed stabs with elf writer, finally the global variables can
  1080. be watched
  1081. * removed a lot of routines from cga unit and replaced them by
  1082. calls to cgobj
  1083. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1084. u32bit then the other is typecasted also to u32bit without giving
  1085. a rangecheck warning/error.
  1086. * fixed pascal calling method with reversing also the high tree in
  1087. the parast, detected by tcalcst3 test
  1088. Revision 1.38 2002/04/25 20:16:39 peter
  1089. * moved more routines from cga/n386util
  1090. Revision 1.37 2002/04/23 19:16:34 peter
  1091. * add pinline unit that inserts compiler supported functions using
  1092. one or more statements
  1093. * moved finalize and setlength from ninl to pinline
  1094. }