nld.pas 43 KB

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