nld.pas 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  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. set_unique(left);
  463. resulttypepass(left);
  464. resulttypepass(right);
  465. set_varstate(left,false);
  466. set_varstate(right,true);
  467. set_funcret_is_valid(left);
  468. if codegenerror then
  469. exit;
  470. { assignments to open arrays aren't allowed }
  471. if is_open_array(left.resulttype.def) then
  472. CGMessage(type_e_mismatch);
  473. { test if node can be assigned, properties are allowed }
  474. valid_for_assignment(left);
  475. { assigning nil to a dynamic array clears the array }
  476. if is_dynamic_array(left.resulttype.def) and
  477. (right.nodetype=niln) then
  478. begin
  479. hp:=ccallparanode.create(caddrnode.create
  480. (crttinode.create(tstoreddef(left.resulttype.def),initrtti)),
  481. ccallparanode.create(ctypeconvnode.create_explicit(left,voidpointertype),nil));
  482. result := ccallnode.createintern('fpc_dynarray_clear',hp);
  483. left:=nil;
  484. exit;
  485. end;
  486. if is_ansistring(left.resulttype.def) then
  487. begin
  488. { fold <ansistring>:=<ansistring>+<char|shortstring|ansistring> }
  489. if (right.nodetype=addn) and
  490. left.isequal(tbinarynode(right).left) and
  491. { don't fold multiple concatenations else we could get trouble
  492. with multiple uses of s
  493. }
  494. (tbinarynode(right).left.nodetype<>addn) and
  495. (tbinarynode(right).right.nodetype<>addn) and
  496. (is_char(tbinarynode(right).right.resulttype.def) or
  497. is_shortstring(tbinarynode(right).right.resulttype.def) or
  498. is_ansistring(tbinarynode(right).right.resulttype.def)
  499. ) then
  500. begin
  501. hp:=ccallparanode.create(tbinarynode(right).right,
  502. ccallparanode.create(left,nil));
  503. if is_char(tbinarynode(right).right.resulttype.def) then
  504. result:=ccallnode.createintern('fpc_ansistr_append_char',hp)
  505. else if is_shortstring(tbinarynode(right).right.resulttype.def) then
  506. result:=ccallnode.createintern('fpc_ansistr_append_shortstring',hp)
  507. else if is_ansistring(tbinarynode(right).right.resulttype.def) then
  508. result:=ccallnode.createintern('fpc_ansistr_append_ansistring',hp);
  509. tbinarynode(right).right:=nil;
  510. left:=nil;
  511. exit;
  512. end;
  513. end;
  514. { shortstring helpers can do the conversion directly,
  515. so treat them separatly }
  516. if (is_shortstring(left.resulttype.def)) then
  517. begin
  518. { test for s:=s+anything ... }
  519. { the problem is for
  520. s:=s+s+s;
  521. this is broken here !! }
  522. {$ifdef newoptimizations2}
  523. { the above is fixed now, but still problem with s := s + f(); if }
  524. { f modifies s (bad programming, so only enable if uncertain }
  525. { optimizations are on) (JM) }
  526. if (cs_UncertainOpts in aktglobalswitches) then
  527. begin
  528. hp := right;
  529. while hp.treetype=addn do
  530. hp:=hp.left;
  531. if left.docompare(hp) then
  532. begin
  533. concat_string:=true;
  534. hp:=right;
  535. while hp.treetype=addn do
  536. begin
  537. hp.use_strconcat:=true;
  538. hp:=hp.left;
  539. end;
  540. end;
  541. end;
  542. {$endif newoptimizations2}
  543. { insert typeconv, except for chars that are handled in
  544. secondpass and except for ansi/wide string that can
  545. be converted immediatly }
  546. if not(is_char(right.resulttype.def) or
  547. (right.resulttype.def.deftype=stringdef)) then
  548. inserttypeconv(right,left.resulttype);
  549. if right.resulttype.def.deftype=stringdef then
  550. begin
  551. useshelper:=true;
  552. { convert constant strings to shortstrings. But
  553. skip empty constant strings, that will be handled
  554. in secondpass }
  555. if (right.nodetype=stringconstn) then
  556. begin
  557. { verify if range fits within shortstring }
  558. { just emit a warning, delphi gives an }
  559. { error, only if the type definition of }
  560. { of the string is less < 255 characters }
  561. if (tstringconstnode(right).len
  562. > tstringdef(left.resulttype.def).len) then
  563. cgmessage(type_w_string_too_long);
  564. inserttypeconv(right,left.resulttype);
  565. if (tstringconstnode(right).len=0) then
  566. useshelper:=false;
  567. end;
  568. if useshelper then
  569. begin
  570. hp:=ccallparanode.create
  571. (right,
  572. ccallparanode.create(cinlinenode.create
  573. (in_high_x,false,left.getcopy),nil));
  574. result:=ccallnode.createinternreturn('fpc_'+tstringdef(right.resulttype.def).stringtypname+'_to_shortstr',hp,left);
  575. left:=nil;
  576. right:=nil;
  577. exit;
  578. end;
  579. end;
  580. end
  581. else
  582. inserttypeconv(right,left.resulttype);
  583. { call helpers for interface }
  584. if is_interfacecom(left.resulttype.def) then
  585. begin
  586. hp:=ccallparanode.create(ctypeconvnode.create_explicit
  587. (right,voidpointertype),
  588. ccallparanode.create(ctypeconvnode.create_explicit
  589. (left,voidpointertype),nil));
  590. result:=ccallnode.createintern('fpc_intf_assign',hp);
  591. left:=nil;
  592. right:=nil;
  593. exit;
  594. end;
  595. { check if local proc/func is assigned to procvar }
  596. if right.resulttype.def.deftype=procvardef then
  597. test_local_to_procvar(tprocvardef(right.resulttype.def),left.resulttype.def);
  598. end;
  599. function tassignmentnode.pass_1 : tnode;
  600. begin
  601. result:=nil;
  602. firstpass(left);
  603. firstpass(right);
  604. if codegenerror then
  605. exit;
  606. registers32:=left.registers32+right.registers32;
  607. registersfpu:=max(left.registersfpu,right.registersfpu);
  608. {$ifdef SUPPORT_MMX}
  609. registersmmx:=max(left.registersmmx,right.registersmmx);
  610. {$endif SUPPORT_MMX}
  611. end;
  612. function tassignmentnode.docompare(p: tnode): boolean;
  613. begin
  614. docompare :=
  615. inherited docompare(p) and
  616. (assigntype = tassignmentnode(p).assigntype);
  617. end;
  618. {$ifdef state_tracking}
  619. function Tassignmentnode.track_state_pass(exec_known:boolean):boolean;
  620. var se:Tstate_entry;
  621. begin
  622. track_state_pass:=false;
  623. if exec_known then
  624. begin
  625. track_state_pass:=right.track_state_pass(exec_known);
  626. {Force a new resulttype pass.}
  627. right.resulttype.def:=nil;
  628. do_resulttypepass(right);
  629. resulttypepass(right);
  630. aktstate.store_fact(left.getcopy,right.getcopy);
  631. end
  632. else
  633. aktstate.delete_fact(left);
  634. end;
  635. {$endif}
  636. {*****************************************************************************
  637. TFUNCRETNODE
  638. *****************************************************************************}
  639. constructor tfuncretnode.create(v:tsym);
  640. begin
  641. inherited create(funcretn);
  642. funcretsym:=tfuncretsym(v);
  643. end;
  644. constructor tfuncretnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  645. begin
  646. inherited ppuload(t,ppufile);
  647. funcretsym:=tfuncretsym(ppufile.getderef);
  648. end;
  649. procedure tfuncretnode.ppuwrite(ppufile:tcompilerppufile);
  650. begin
  651. inherited ppuwrite(ppufile);
  652. ppufile.putderef(funcretsym);
  653. end;
  654. procedure tfuncretnode.derefimpl;
  655. begin
  656. inherited derefimpl;
  657. resolvesym(pointer(funcretsym));
  658. end;
  659. function tfuncretnode.getcopy : tnode;
  660. var
  661. n : tfuncretnode;
  662. begin
  663. n:=tfuncretnode(inherited getcopy);
  664. n.funcretsym:=funcretsym;
  665. getcopy:=n;
  666. end;
  667. function tfuncretnode.det_resulttype:tnode;
  668. begin
  669. result:=nil;
  670. resulttype:=funcretsym.returntype;
  671. end;
  672. {$ifdef var_notification}
  673. procedure Tfuncretnode.mark_write;
  674. begin
  675. include(flags,nf_write);
  676. end;
  677. {$endif}
  678. function tfuncretnode.pass_1 : tnode;
  679. begin
  680. result:=nil;
  681. location.loc:=LOC_REFERENCE;
  682. if paramanager.ret_in_param(resulttype.def) or
  683. (lexlevel<>funcretsym.owner.symtablelevel) then
  684. registers32:=1;
  685. end;
  686. function tfuncretnode.docompare(p: tnode): boolean;
  687. begin
  688. docompare :=
  689. inherited docompare(p) and
  690. (funcretsym = tfuncretnode(p).funcretsym);
  691. end;
  692. {*****************************************************************************
  693. TARRAYCONSTRUCTORRANGENODE
  694. *****************************************************************************}
  695. constructor tarrayconstructorrangenode.create(l,r : tnode);
  696. begin
  697. inherited create(arrayconstructorrangen,l,r);
  698. end;
  699. function tarrayconstructorrangenode.det_resulttype:tnode;
  700. begin
  701. result:=nil;
  702. resulttypepass(left);
  703. resulttypepass(right);
  704. set_varstate(left,true);
  705. set_varstate(right,true);
  706. if codegenerror then
  707. exit;
  708. resulttype:=left.resulttype;
  709. end;
  710. function tarrayconstructorrangenode.pass_1 : tnode;
  711. begin
  712. firstpass(left);
  713. firstpass(right);
  714. location.loc := LOC_CREFERENCE;
  715. calcregisters(self,0,0,0);
  716. result:=nil;
  717. end;
  718. {****************************************************************************
  719. TARRAYCONSTRUCTORNODE
  720. *****************************************************************************}
  721. constructor tarrayconstructornode.create(l,r : tnode);
  722. begin
  723. inherited create(arrayconstructorn,l,r);
  724. end;
  725. function tarrayconstructornode.getcopy : tnode;
  726. var
  727. n : tarrayconstructornode;
  728. begin
  729. n:=tarrayconstructornode(inherited getcopy);
  730. result:=n;
  731. end;
  732. function tarrayconstructornode.det_resulttype:tnode;
  733. var
  734. htype : ttype;
  735. hp : tarrayconstructornode;
  736. len : longint;
  737. varia : boolean;
  738. begin
  739. result:=nil;
  740. { are we allowing array constructor? Then convert it to a set }
  741. if not allow_array_constructor then
  742. begin
  743. hp:=tarrayconstructornode(getcopy);
  744. arrayconstructor_to_set(tnode(hp));
  745. result:=hp;
  746. exit;
  747. end;
  748. { only pass left tree, right tree contains next construct if any }
  749. htype.reset;
  750. len:=0;
  751. varia:=false;
  752. if assigned(left) then
  753. begin
  754. hp:=self;
  755. while assigned(hp) do
  756. begin
  757. resulttypepass(hp.left);
  758. set_varstate(hp.left,true);
  759. if (htype.def=nil) then
  760. htype:=hp.left.resulttype
  761. else
  762. begin
  763. if ((nf_novariaallowed in flags) or (not varia)) and
  764. (not is_equal(htype.def,hp.left.resulttype.def)) then
  765. begin
  766. varia:=true;
  767. end;
  768. end;
  769. inc(len);
  770. hp:=tarrayconstructornode(hp.right);
  771. end;
  772. end;
  773. if not assigned(htype.def) then
  774. htype:=voidtype;
  775. resulttype.setdef(tarraydef.create(0,len-1,s32bittype));
  776. tarraydef(resulttype.def).setelementtype(htype);
  777. tarraydef(resulttype.def).IsConstructor:=true;
  778. tarraydef(resulttype.def).IsVariant:=varia;
  779. end;
  780. procedure tarrayconstructornode.force_type(tt:ttype);
  781. var
  782. hp : tarrayconstructornode;
  783. begin
  784. tarraydef(resulttype.def).setelementtype(tt);
  785. tarraydef(resulttype.def).IsConstructor:=true;
  786. tarraydef(resulttype.def).IsVariant:=false;
  787. if assigned(left) then
  788. begin
  789. hp:=self;
  790. while assigned(hp) do
  791. begin
  792. inserttypeconv(hp.left,tt);
  793. hp:=tarrayconstructornode(hp.right);
  794. end;
  795. end;
  796. end;
  797. function tarrayconstructornode.pass_1 : tnode;
  798. var
  799. thp,
  800. chp,
  801. hp : tarrayconstructornode;
  802. dovariant : boolean;
  803. htype : ttype;
  804. orgflags : tnodeflagset;
  805. begin
  806. dovariant:=(nf_forcevaria in flags) or tarraydef(resulttype.def).isvariant;
  807. result:=nil;
  808. { only pass left tree, right tree contains next construct if any }
  809. if assigned(left) then
  810. begin
  811. hp:=self;
  812. while assigned(hp) do
  813. begin
  814. firstpass(hp.left);
  815. { Insert typeconvs for array of const }
  816. if dovariant then
  817. begin
  818. case hp.left.resulttype.def.deftype of
  819. enumdef :
  820. begin
  821. hp.left:=ctypeconvnode.create(hp.left,s32bittype);
  822. firstpass(hp.left);
  823. end;
  824. orddef :
  825. begin
  826. if is_integer(hp.left.resulttype.def) and
  827. not(is_64bitint(hp.left.resulttype.def)) then
  828. begin
  829. hp.left:=ctypeconvnode.create(hp.left,s32bittype);
  830. firstpass(hp.left);
  831. end;
  832. end;
  833. floatdef :
  834. begin
  835. { C uses 64bit floats }
  836. if nf_cargs in flags then
  837. hp.left:=ctypeconvnode.create(hp.left,s64floattype)
  838. else
  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.64 2002-11-15 01:58:52 peter
  1025. * merged changes from 1.0.7 up to 04-11
  1026. - -V option for generating bug report tracing
  1027. - more tracing for option parsing
  1028. - errors for cdecl and high()
  1029. - win32 import stabs
  1030. - win32 records<=8 are returned in eax:edx (turned off by default)
  1031. - heaptrc update
  1032. - more info for temp management in .s file with EXTDEBUG
  1033. Revision 1.63 2002/10/17 12:44:09 florian
  1034. + s:=s+<string type> where s is an ansistring is done via calls to append_ansistring_*
  1035. Revision 1.62 2002/10/05 12:43:25 carl
  1036. * fixes for Delphi 6 compilation
  1037. (warning : Some features do not work under Delphi)
  1038. Revision 1.61 2002/10/03 21:26:08 carl
  1039. + compile-time range checking for strings
  1040. Revision 1.60 2002/09/27 21:13:28 carl
  1041. * low-highval always checked if limit ober 2GB is reached (to avoid overflow)
  1042. Revision 1.59 2002/09/26 15:02:05 florian
  1043. + support of passing variants to "array of const"
  1044. Revision 1.58 2002/09/07 15:25:03 peter
  1045. * old logs removed and tabs fixed
  1046. Revision 1.57 2002/09/03 16:26:26 daniel
  1047. * Make Tprocdef.defs protected
  1048. Revision 1.56 2002/09/01 13:28:37 daniel
  1049. - write_access fields removed in favor of a flag
  1050. Revision 1.55 2002/09/01 08:01:16 daniel
  1051. * Removed sets from Tcallnode.det_resulttype
  1052. + Added read/write notifications of variables. These will be usefull
  1053. for providing information for several optimizations. For example
  1054. the value of the loop variable of a for loop does matter is the
  1055. variable is read after the for loop, but if it's no longer used
  1056. or written, it doesn't matter and this can be used to optimize
  1057. the loop code generation.
  1058. Revision 1.54 2002/08/25 19:25:19 peter
  1059. * sym.insert_in_data removed
  1060. * symtable.insertvardata/insertconstdata added
  1061. * removed insert_in_data call from symtable.insert, it needs to be
  1062. called separatly. This allows to deref the address calculation
  1063. * procedures now calculate the parast addresses after the procedure
  1064. directives are parsed. This fixes the cdecl parast problem
  1065. * push_addr_param has an extra argument that specifies if cdecl is used
  1066. or not
  1067. Revision 1.53 2002/08/19 19:36:43 peter
  1068. * More fixes for cross unit inlining, all tnodes are now implemented
  1069. * Moved pocall_internconst to po_internconst because it is not a
  1070. calling type at all and it conflicted when inlining of these small
  1071. functions was requested
  1072. Revision 1.52 2002/08/18 20:06:23 peter
  1073. * inlining is now also allowed in interface
  1074. * renamed write/load to ppuwrite/ppuload
  1075. * tnode storing in ppu
  1076. * nld,ncon,nbas are already updated for storing in ppu
  1077. Revision 1.51 2002/08/17 22:09:46 florian
  1078. * result type handling in tcgcal.pass_2 overhauled
  1079. * better tnode.dowrite
  1080. * some ppc stuff fixed
  1081. Revision 1.50 2002/08/17 09:23:37 florian
  1082. * first part of procinfo rewrite
  1083. Revision 1.49 2002/07/20 11:57:54 florian
  1084. * types.pas renamed to defbase.pas because D6 contains a types
  1085. unit so this would conflicts if D6 programms are compiled
  1086. + Willamette/SSE2 instructions to assembler added
  1087. Revision 1.48 2002/07/20 07:44:37 daniel
  1088. * Forgot to add a $ifdef extdebug
  1089. Revision 1.47 2002/07/19 12:55:27 daniel
  1090. * Further developed state tracking in whilerepeatn
  1091. Revision 1.46 2002/07/19 11:41:36 daniel
  1092. * State tracker work
  1093. * The whilen and repeatn are now completely unified into whilerepeatn. This
  1094. allows the state tracker to change while nodes automatically into
  1095. repeat nodes.
  1096. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  1097. 'not(a>b)' is optimized into 'a<=b'.
  1098. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  1099. by removing the notn and later switchting the true and falselabels. The
  1100. same is done with 'repeat until not a'.
  1101. Revision 1.45 2002/07/15 18:03:15 florian
  1102. * readded removed changes
  1103. Revision 1.43 2002/07/11 14:41:28 florian
  1104. * start of the new generic parameter handling
  1105. Revision 1.44 2002/07/14 18:00:44 daniel
  1106. + Added the beginning of a state tracker. This will track the values of
  1107. variables through procedures and optimize things away.
  1108. Revision 1.42 2002/05/18 13:34:10 peter
  1109. * readded missing revisions
  1110. Revision 1.41 2002/05/16 19:46:38 carl
  1111. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1112. + try to fix temp allocation (still in ifdef)
  1113. + generic constructor calls
  1114. + start of tassembler / tmodulebase class cleanup
  1115. Revision 1.39 2002/05/12 16:53:07 peter
  1116. * moved entry and exitcode to ncgutil and cgobj
  1117. * foreach gets extra argument for passing local data to the
  1118. iterator function
  1119. * -CR checks also class typecasts at runtime by changing them
  1120. into as
  1121. * fixed compiler to cycle with the -CR option
  1122. * fixed stabs with elf writer, finally the global variables can
  1123. be watched
  1124. * removed a lot of routines from cga unit and replaced them by
  1125. calls to cgobj
  1126. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1127. u32bit then the other is typecasted also to u32bit without giving
  1128. a rangecheck warning/error.
  1129. * fixed pascal calling method with reversing also the high tree in
  1130. the parast, detected by tcalcst3 test
  1131. Revision 1.38 2002/04/25 20:16:39 peter
  1132. * moved more routines from cga/n386util
  1133. Revision 1.37 2002/04/23 19:16:34 peter
  1134. * add pinline unit that inserts compiler supported functions using
  1135. one or more statements
  1136. * moved finalize and setlength from ninl to pinline
  1137. }