nld.pas 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  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,symbase,symtype,symsym,symdef;
  27. type
  28. tloadnode = class(tunarynode)
  29. symtableentry : tsym;
  30. symtableentryderef : tderef;
  31. symtable : tsymtable;
  32. procdef : tprocdef;
  33. procdefderef : tderef;
  34. constructor create(v : tsym;st : tsymtable);virtual;
  35. constructor create_procvar(v : tsym;d:tprocdef;st : tsymtable);virtual;
  36. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  37. procedure ppuwrite(ppufile:tcompilerppufile);override;
  38. procedure buildderefimpl;override;
  39. procedure derefimpl;override;
  40. procedure set_mp(p:tnode);
  41. function is_addr_param_load:boolean;
  42. function getcopy : tnode;override;
  43. function pass_1 : tnode;override;
  44. function det_resulttype:tnode;override;
  45. procedure mark_write;override;
  46. function docompare(p: tnode): boolean; override;
  47. procedure printnodedata(var t:text);override;
  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. tarrayconstructorrangenode = class(tbinarynode)
  67. constructor create(l,r : tnode);virtual;
  68. function pass_1 : tnode;override;
  69. function det_resulttype:tnode;override;
  70. end;
  71. tarrayconstructorrangenodeclass = class of tarrayconstructorrangenode;
  72. tarrayconstructornode = class(tbinarynode)
  73. constructor create(l,r : tnode);virtual;
  74. function getcopy : tnode;override;
  75. function pass_1 : tnode;override;
  76. function det_resulttype:tnode;override;
  77. function docompare(p: tnode): boolean; override;
  78. procedure force_type(tt:ttype);
  79. procedure insert_typeconvs;
  80. end;
  81. tarrayconstructornodeclass = class of tarrayconstructornode;
  82. ttypenode = class(tnode)
  83. allowed : boolean;
  84. restype : ttype;
  85. constructor create(t : ttype);virtual;
  86. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  87. procedure ppuwrite(ppufile:tcompilerppufile);override;
  88. procedure buildderefimpl;override;
  89. procedure derefimpl;override;
  90. function pass_1 : tnode;override;
  91. function det_resulttype:tnode;override;
  92. function docompare(p: tnode): boolean; override;
  93. end;
  94. ttypenodeclass = class of ttypenode;
  95. trttinode = class(tnode)
  96. l1,l2 : longint;
  97. rttitype : trttitype;
  98. rttidef : tstoreddef;
  99. rttidefderef : tderef;
  100. constructor create(def:tstoreddef;rt:trttitype);virtual;
  101. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  102. procedure ppuwrite(ppufile:tcompilerppufile);override;
  103. procedure buildderefimpl;override;
  104. procedure derefimpl;override;
  105. function getcopy : tnode;override;
  106. function pass_1 : tnode;override;
  107. function det_resulttype:tnode;override;
  108. function docompare(p: tnode): boolean; override;
  109. end;
  110. trttinodeclass = class of trttinode;
  111. var
  112. cloadnode : tloadnodeclass;
  113. cassignmentnode : tassignmentnodeclass;
  114. carrayconstructorrangenode : tarrayconstructorrangenodeclass;
  115. carrayconstructornode : tarrayconstructornodeclass;
  116. ctypenode : ttypenodeclass;
  117. crttinode : trttinodeclass;
  118. implementation
  119. uses
  120. cutils,verbose,globtype,globals,systems,
  121. symnot,
  122. defutil,defcmp,
  123. htypechk,pass_1,procinfo,paramgr,
  124. ncon,ninl,ncnv,nmem,ncal,nutils,
  125. cgobj,cgbase
  126. ;
  127. {*****************************************************************************
  128. TLOADNODE
  129. *****************************************************************************}
  130. constructor tloadnode.create(v : tsym;st : tsymtable);
  131. begin
  132. inherited create(loadn,nil);
  133. if not assigned(v) then
  134. internalerror(200108121);
  135. symtableentry:=v;
  136. symtable:=st;
  137. procdef:=nil;
  138. end;
  139. constructor tloadnode.create_procvar(v : tsym;d:tprocdef;st : tsymtable);
  140. begin
  141. inherited create(loadn,nil);
  142. if not assigned(v) then
  143. internalerror(200108121);
  144. symtableentry:=v;
  145. symtable:=st;
  146. procdef:=d;
  147. end;
  148. constructor tloadnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  149. begin
  150. inherited ppuload(t,ppufile);
  151. ppufile.getderef(symtableentryderef);
  152. symtable:=nil;
  153. ppufile.getderef(procdefderef);
  154. end;
  155. procedure tloadnode.ppuwrite(ppufile:tcompilerppufile);
  156. begin
  157. inherited ppuwrite(ppufile);
  158. ppufile.putderef(symtableentryderef);
  159. ppufile.putderef(procdefderef);
  160. end;
  161. procedure tloadnode.buildderefimpl;
  162. begin
  163. inherited buildderefimpl;
  164. symtableentryderef.build(symtableentry);
  165. procdefderef.build(procdef);
  166. end;
  167. procedure tloadnode.derefimpl;
  168. begin
  169. inherited derefimpl;
  170. symtableentry:=tsym(symtableentryderef.resolve);
  171. symtable:=symtableentry.owner;
  172. procdef:=tprocdef(procdefderef.resolve);
  173. end;
  174. procedure tloadnode.set_mp(p:tnode);
  175. begin
  176. { typen nodes should not be set }
  177. if p.nodetype=typen then
  178. internalerror(200301042);
  179. left:=p;
  180. end;
  181. function tloadnode.getcopy : tnode;
  182. var
  183. n : tloadnode;
  184. begin
  185. n:=tloadnode(inherited getcopy);
  186. n.symtable:=symtable;
  187. n.symtableentry:=symtableentry;
  188. n.procdef:=procdef;
  189. result:=n;
  190. end;
  191. function tloadnode.is_addr_param_load:boolean;
  192. begin
  193. result:=(symtable.symtabletype=parasymtable) and
  194. (symtableentry.typ=paravarsym) and
  195. not(vo_has_local_copy in tparavarsym(symtableentry).varoptions) and
  196. not(nf_load_self_pointer in flags) and
  197. paramanager.push_addr_param(tparavarsym(symtableentry).varspez,tparavarsym(symtableentry).vartype.def,tprocdef(symtable.defowner).proccalloption);
  198. end;
  199. function tloadnode.det_resulttype:tnode;
  200. begin
  201. result:=nil;
  202. case symtableentry.typ of
  203. absolutevarsym :
  204. resulttype:=tabsolutevarsym(symtableentry).vartype;
  205. constsym:
  206. begin
  207. if tconstsym(symtableentry).consttyp=constresourcestring then
  208. begin
  209. {$ifdef ansistring_bits}
  210. case aktansistring_bits of
  211. sb_16:
  212. resulttype:=cansistringtype16;
  213. sb_32:
  214. resulttype:=cansistringtype32;
  215. sb_64:
  216. resulttype:=cansistringtype64;
  217. end;
  218. {$else}
  219. resulttype:=cansistringtype
  220. {$endif}
  221. end
  222. else
  223. internalerror(22799);
  224. end;
  225. globalvarsym,
  226. paravarsym,
  227. localvarsym :
  228. begin
  229. inc(tabstractvarsym(symtableentry).refs);
  230. { Nested variable? The we need to load the framepointer of
  231. the parent procedure }
  232. if assigned(current_procinfo) then
  233. begin
  234. if (symtable.symtabletype in [localsymtable,parasymtable]) and
  235. (symtable.symtablelevel<>current_procinfo.procdef.parast.symtablelevel) then
  236. begin
  237. if assigned(left) then
  238. internalerror(200309289);
  239. left:=cloadparentfpnode.create(tprocdef(symtable.defowner));
  240. { reference in nested procedures, variable needs to be in memory }
  241. make_not_regable(self);
  242. end;
  243. { static variables referenced in procedures or from finalization,
  244. variable needs to be in memory.
  245. It is too hard and the benefit is too small to detect whether a
  246. variable is only used in the finalization to add support for it (PFV) }
  247. if (symtable.symtabletype=staticsymtable) and
  248. (
  249. (symtable.symtablelevel<>current_procinfo.procdef.localst.symtablelevel) or
  250. (current_procinfo.procdef.proctypeoption=potype_unitfinalize)
  251. ) then
  252. make_not_regable(self);
  253. end;
  254. { fix self type which is declared as voidpointer in the
  255. definition }
  256. if vo_is_self in tabstractvarsym(symtableentry).varoptions then
  257. begin
  258. resulttype.setdef(tprocdef(symtableentry.owner.defowner)._class);
  259. if (po_classmethod in tprocdef(symtableentry.owner.defowner).procoptions) or
  260. (po_staticmethod in tprocdef(symtableentry.owner.defowner).procoptions) then
  261. resulttype.setdef(tclassrefdef.create(resulttype))
  262. else if is_object(resulttype.def) and
  263. (nf_load_self_pointer in flags) then
  264. resulttype.setdef(tpointerdef.create(resulttype));
  265. end
  266. else if vo_is_vmt in tabstractvarsym(symtableentry).varoptions then
  267. begin
  268. resulttype.setdef(tprocdef(symtableentry.owner.defowner)._class);
  269. resulttype.setdef(tclassrefdef.create(resulttype));
  270. end
  271. else
  272. resulttype:=tabstractvarsym(symtableentry).vartype;
  273. end;
  274. typedconstsym :
  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. procdef:=tprocsym(symtableentry).first_procdef;
  283. end;
  284. { the result is a procdef, addrn and proc_to_procvar
  285. typeconvn need this as resulttype so they know
  286. that the address needs to be returned }
  287. resulttype.setdef(procdef);
  288. { process methodpointer }
  289. if assigned(left) then
  290. resulttypepass(left);
  291. end;
  292. labelsym:
  293. resulttype:=voidtype;
  294. else
  295. internalerror(200104141);
  296. end;
  297. end;
  298. procedure Tloadnode.mark_write;
  299. begin
  300. include(flags,nf_write);
  301. end;
  302. function tloadnode.pass_1 : tnode;
  303. begin
  304. result:=nil;
  305. expectloc:=LOC_REFERENCE;
  306. registersint:=0;
  307. registersfpu:=0;
  308. {$ifdef SUPPORT_MMX}
  309. registersmmx:=0;
  310. {$endif SUPPORT_MMX}
  311. case symtableentry.typ of
  312. absolutevarsym :
  313. ;
  314. constsym:
  315. begin
  316. if tconstsym(symtableentry).consttyp=constresourcestring then
  317. expectloc:=LOC_CREFERENCE;
  318. end;
  319. globalvarsym,
  320. localvarsym,
  321. paravarsym :
  322. begin
  323. if assigned(left) then
  324. firstpass(left);
  325. if not is_addr_param_load and
  326. tabstractvarsym(symtableentry).is_regvar then
  327. begin
  328. case tabstractvarsym(symtableentry).varregable of
  329. vr_intreg :
  330. expectloc:=LOC_CREGISTER;
  331. vr_fpureg :
  332. expectloc:=LOC_CFPUREGISTER;
  333. vr_mmreg :
  334. expectloc:=LOC_CMMREGISTER;
  335. end
  336. end
  337. else
  338. if (tabstractvarsym(symtableentry).varspez=vs_const) then
  339. expectloc:=LOC_CREFERENCE;
  340. { we need a register for call by reference parameters }
  341. if paramanager.push_addr_param(tabstractvarsym(symtableentry).varspez,tabstractvarsym(symtableentry).vartype.def,pocall_default) then
  342. registersint:=1;
  343. if ([vo_is_thread_var,vo_is_dll_var]*tabstractvarsym(symtableentry).varoptions)<>[] then
  344. registersint:=1;
  345. if (target_info.system=system_powerpc_darwin) and (vo_is_dll_var in tabstractvarsym(symtableentry).varoptions) then
  346. include(current_procinfo.flags,pi_needs_got);
  347. { call to get address of threadvar }
  348. if (vo_is_thread_var in tabstractvarsym(symtableentry).varoptions) then
  349. include(current_procinfo.flags,pi_do_call);
  350. if nf_write in flags then
  351. Tabstractvarsym(symtableentry).trigger_notifications(vn_onwrite)
  352. else
  353. Tabstractvarsym(symtableentry).trigger_notifications(vn_onread);
  354. { count variable references }
  355. if cg.t_times>1 then
  356. inc(tabstractvarsym(symtableentry).refs,cg.t_times-1);
  357. end;
  358. typedconstsym :
  359. ;
  360. procsym :
  361. begin
  362. { method pointer ? }
  363. if assigned(left) then
  364. begin
  365. expectloc:=LOC_CREFERENCE;
  366. firstpass(left);
  367. registersint:=max(registersint,left.registersint);
  368. registersfpu:=max(registersfpu,left.registersfpu);
  369. {$ifdef SUPPORT_MMX}
  370. registersmmx:=max(registersmmx,left.registersmmx);
  371. {$endif SUPPORT_MMX}
  372. end;
  373. end;
  374. labelsym :
  375. ;
  376. else
  377. internalerror(200104143);
  378. end;
  379. end;
  380. function tloadnode.docompare(p: tnode): boolean;
  381. begin
  382. docompare :=
  383. inherited docompare(p) and
  384. (symtableentry = tloadnode(p).symtableentry) and
  385. (procdef = tloadnode(p).procdef) and
  386. (symtable = tloadnode(p).symtable);
  387. end;
  388. procedure Tloadnode.printnodedata(var t:text);
  389. begin
  390. inherited printnodedata(t);
  391. write(t,printnodeindention,'symbol = ',symtableentry.name);
  392. if symtableentry.typ=procsym then
  393. write(t,printnodeindention,'procdef = ',procdef.mangledname);
  394. writeln(t,'');
  395. end;
  396. {*****************************************************************************
  397. TASSIGNMENTNODE
  398. *****************************************************************************}
  399. constructor tassignmentnode.create(l,r : tnode);
  400. begin
  401. inherited create(assignn,l,r);
  402. l.mark_write;
  403. assigntype:=at_normal;
  404. end;
  405. constructor tassignmentnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  406. begin
  407. inherited ppuload(t,ppufile);
  408. assigntype:=tassigntype(ppufile.getbyte);
  409. end;
  410. procedure tassignmentnode.ppuwrite(ppufile:tcompilerppufile);
  411. begin
  412. inherited ppuwrite(ppufile);
  413. ppufile.putbyte(byte(assigntype));
  414. end;
  415. function tassignmentnode.getcopy : tnode;
  416. var
  417. n : tassignmentnode;
  418. begin
  419. n:=tassignmentnode(inherited getcopy);
  420. n.assigntype:=assigntype;
  421. getcopy:=n;
  422. end;
  423. function tassignmentnode.det_resulttype:tnode;
  424. var
  425. hp : tnode;
  426. useshelper : boolean;
  427. original_size : longint;
  428. begin
  429. result:=nil;
  430. resulttype:=voidtype;
  431. original_size := 0;
  432. { must be made unique }
  433. set_unique(left);
  434. resulttypepass(left);
  435. if is_ansistring(left.resulttype.def) then
  436. begin
  437. { fold <ansistring>:=<ansistring>+<char|shortstring|ansistring> }
  438. if (right.nodetype=addn) and
  439. left.isequal(tbinarynode(right).left) and
  440. { don't fold multiple concatenations else we could get trouble
  441. with multiple uses of s
  442. }
  443. (tbinarynode(right).left.nodetype<>addn) and
  444. (tbinarynode(right).right.nodetype<>addn) then
  445. begin
  446. { don't do a resulttypepass(right), since then the addnode }
  447. { may insert typeconversions that make this optimization }
  448. { opportunity quite difficult to detect (JM) }
  449. resulttypepass(tbinarynode(right).left);
  450. resulttypepass(tbinarynode(right).right);
  451. if (is_char(tbinarynode(right).right.resulttype.def) or
  452. is_shortstring(tbinarynode(right).right.resulttype.def) or
  453. is_ansistring(tbinarynode(right).right.resulttype.def)) then
  454. begin
  455. { remove property flag so it'll not trigger an error }
  456. exclude(left.flags,nf_isproperty);
  457. { generate call to helper }
  458. hp:=ccallparanode.create(tbinarynode(right).right,
  459. ccallparanode.create(left,nil));
  460. if is_char(tbinarynode(right).right.resulttype.def) then
  461. result:=ccallnode.createintern('fpc_'+Tstringdef(left.resulttype.def).stringtypname+'_append_char',hp)
  462. else if is_shortstring(tbinarynode(right).right.resulttype.def) then
  463. result:=ccallnode.createintern('fpc_'+Tstringdef(left.resulttype.def).stringtypname+'_append_shortstring',hp)
  464. else
  465. result:=ccallnode.createintern('fpc_'+Tstringdef(left.resulttype.def).stringtypname+'_append_ansistring',hp);
  466. tbinarynode(right).right:=nil;
  467. left:=nil;
  468. exit;
  469. end;
  470. end;
  471. end
  472. else
  473. if is_shortstring(left.resulttype.def) then
  474. begin
  475. { fold <shortstring>:=<shortstring>+<shortstring>,
  476. <shortstring>+<char> is handled by an optimized node }
  477. if (right.nodetype=addn) and
  478. left.isequal(tbinarynode(right).left) and
  479. { don't fold multiple concatenations else we could get trouble
  480. with multiple uses of s }
  481. (tbinarynode(right).left.nodetype<>addn) and
  482. (tbinarynode(right).right.nodetype<>addn) then
  483. begin
  484. { don't do a resulttypepass(right), since then the addnode }
  485. { may insert typeconversions that make this optimization }
  486. { opportunity quite difficult to detect (JM) }
  487. resulttypepass(tbinarynode(right).left);
  488. resulttypepass(tbinarynode(right).right);
  489. if is_shortstring(tbinarynode(right).right.resulttype.def) then
  490. begin
  491. { remove property flag so it'll not trigger an error }
  492. exclude(left.flags,nf_isproperty);
  493. { generate call to helper }
  494. hp:=ccallparanode.create(tbinarynode(right).right,
  495. ccallparanode.create(left,nil));
  496. if is_shortstring(tbinarynode(right).right.resulttype.def) then
  497. result:=ccallnode.createintern('fpc_shortstr_append_shortstr',hp);
  498. tbinarynode(right).right:=nil;
  499. left:=nil;
  500. exit;
  501. end;
  502. end;
  503. end;
  504. resulttypepass(right);
  505. set_varstate(left,vs_assigned,false);
  506. set_varstate(right,vs_used,true);
  507. if codegenerror then
  508. exit;
  509. { tp procvar support, when we don't expect a procvar
  510. then we need to call the procvar }
  511. if (left.resulttype.def.deftype<>procvardef) then
  512. maybe_call_procvar(right,true);
  513. { assignments to formaldefs and open arrays aren't allowed }
  514. if (left.resulttype.def.deftype=formaldef) or
  515. is_open_array(left.resulttype.def) then
  516. CGMessage(type_e_operator_not_allowed);
  517. { test if node can be assigned, properties are allowed }
  518. valid_for_assignment(left);
  519. { assigning nil to a dynamic array clears the array }
  520. if is_dynamic_array(left.resulttype.def) and
  521. (right.nodetype=niln) then
  522. begin
  523. hp:=ccallparanode.create(caddrnode.create_internal
  524. (crttinode.create(tstoreddef(left.resulttype.def),initrtti)),
  525. ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil));
  526. result := ccallnode.createintern('fpc_dynarray_clear',hp);
  527. left:=nil;
  528. exit;
  529. end;
  530. { shortstring helpers can do the conversion directly,
  531. so treat them separatly }
  532. if (is_shortstring(left.resulttype.def)) then
  533. begin
  534. { insert typeconv, except for chars that are handled in
  535. secondpass and except for ansi/wide string that can
  536. be converted immediatly }
  537. if not(is_char(right.resulttype.def) or
  538. (right.resulttype.def.deftype=stringdef)) then
  539. inserttypeconv(right,left.resulttype);
  540. if right.resulttype.def.deftype=stringdef then
  541. begin
  542. useshelper:=true;
  543. { convert constant strings to shortstrings. But
  544. skip empty constant strings, that will be handled
  545. in secondpass }
  546. if (right.nodetype=stringconstn) then
  547. begin
  548. { verify if range fits within shortstring }
  549. { just emit a warning, delphi gives an }
  550. { error, only if the type definition of }
  551. { of the string is less < 255 characters }
  552. if not is_open_string(left.resulttype.def) and
  553. (tstringconstnode(right).len > tstringdef(left.resulttype.def).len) then
  554. cgmessage(type_w_string_too_long);
  555. inserttypeconv(right,left.resulttype);
  556. if (tstringconstnode(right).len=0) then
  557. useshelper:=false;
  558. end;
  559. { rest is done in pass 1 (JM) }
  560. if useshelper then
  561. exit;
  562. end
  563. end
  564. else
  565. begin
  566. { get the size before the type conversion - check for all nodes }
  567. if assigned(right.resulttype.def) and
  568. (right.resulttype.def.deftype in [enumdef,orddef,floatdef]) and
  569. (right.nodetype in [loadn,vecn,calln]) then
  570. original_size := right.resulttype.def.size;
  571. inserttypeconv(right,left.resulttype);
  572. end;
  573. { check if the assignment may cause a range check error }
  574. { if its not explicit, and only if the values are }
  575. { ordinals, enumdef and floatdef }
  576. if (right.nodetype = typeconvn) and
  577. not (nf_explicit in ttypeconvnode(right).flags) then
  578. begin
  579. if assigned(left.resulttype.def) and
  580. (left.resulttype.def.deftype in [enumdef,orddef,floatdef]) then
  581. begin
  582. if (original_size <> 0) and (left.resulttype.def.size < original_size) then
  583. begin
  584. if (cs_check_range in aktlocalswitches) then
  585. Message(type_w_smaller_possible_range_check)
  586. else
  587. Message(type_h_smaller_possible_range_check);
  588. end;
  589. end;
  590. end;
  591. { call helpers for interface }
  592. if is_interfacecom(left.resulttype.def) then
  593. begin
  594. hp:=ccallparanode.create(ctypeconvnode.create_internal
  595. (right,voidpointertype),
  596. ccallparanode.create(ctypeconvnode.create_internal
  597. (left,voidpointertype),nil));
  598. result:=ccallnode.createintern('fpc_intf_assign',hp);
  599. left:=nil;
  600. right:=nil;
  601. exit;
  602. end;
  603. { check if local proc/func is assigned to procvar }
  604. if right.resulttype.def.deftype=procvardef then
  605. test_local_to_procvar(tprocvardef(right.resulttype.def),left.resulttype.def);
  606. end;
  607. function tassignmentnode.pass_1 : tnode;
  608. var
  609. hp: tnode;
  610. begin
  611. result:=nil;
  612. expectloc:=LOC_VOID;
  613. firstpass(left);
  614. firstpass(right);
  615. { assignment to refcounted variable -> inc/decref }
  616. if (not is_class(left.resulttype.def) and
  617. left.resulttype.def.needs_inittable) then
  618. include(current_procinfo.flags,pi_do_call);
  619. if codegenerror then
  620. exit;
  621. if (is_shortstring(left.resulttype.def)) then
  622. begin
  623. if right.resulttype.def.deftype=stringdef then
  624. begin
  625. if (right.nodetype<>stringconstn) or
  626. (tstringconstnode(right).len<>0) then
  627. begin
  628. if (cs_optimize in aktglobalswitches) and
  629. (right.nodetype in [calln,blockn]) and
  630. (left.nodetype = temprefn) and
  631. is_shortstring(right.resulttype.def) and
  632. not is_open_string(left.resulttype.def) and
  633. (tstringdef(left.resulttype.def).len = 255) then
  634. begin
  635. { the blocknode case is handled in pass_2 at the temp }
  636. { reference level (mainly for callparatemp) (JM) }
  637. if (right.nodetype = calln) then
  638. begin
  639. tcallnode(right).funcretnode := left;
  640. result := right;
  641. end
  642. else
  643. exit;
  644. end
  645. else
  646. begin
  647. hp:=ccallparanode.create
  648. (right,
  649. ccallparanode.create(cinlinenode.create
  650. (in_high_x,false,left.getcopy),nil));
  651. result:=ccallnode.createinternreturn('fpc_'+tstringdef(right.resulttype.def).stringtypname+'_to_shortstr',hp,left);
  652. firstpass(result);
  653. end;
  654. left:=nil;
  655. right:=nil;
  656. exit;
  657. end;
  658. end;
  659. end;
  660. if (cs_optimize in aktglobalswitches) and
  661. (right.nodetype = calln) and
  662. { left must be a temp, since otherwise as soon as you modify the }
  663. { result, the current left node is modified and that one may }
  664. { still be an argument to the function or even accessed in the }
  665. { function }
  666. (((left.nodetype = temprefn) and
  667. paramanager.ret_in_param(right.resulttype.def,
  668. tcallnode(right).procdefinition.proccalloption)) or
  669. { there's special support for ansi/widestrings in the callnode }
  670. is_ansistring(right.resulttype.def) or
  671. is_widestring(right.resulttype.def)) then
  672. begin
  673. tcallnode(right).funcretnode := left;
  674. result := right;
  675. left := nil;
  676. right := nil;
  677. exit;
  678. end;
  679. registersint:=left.registersint+right.registersint;
  680. registersfpu:=max(left.registersfpu,right.registersfpu);
  681. {$ifdef SUPPORT_MMX}
  682. registersmmx:=max(left.registersmmx,right.registersmmx);
  683. {$endif SUPPORT_MMX}
  684. end;
  685. function tassignmentnode.docompare(p: tnode): boolean;
  686. begin
  687. docompare :=
  688. inherited docompare(p) and
  689. (assigntype = tassignmentnode(p).assigntype);
  690. end;
  691. {$ifdef state_tracking}
  692. function Tassignmentnode.track_state_pass(exec_known:boolean):boolean;
  693. var se:Tstate_entry;
  694. begin
  695. track_state_pass:=false;
  696. if exec_known then
  697. begin
  698. track_state_pass:=right.track_state_pass(exec_known);
  699. {Force a new resulttype pass.}
  700. right.resulttype.def:=nil;
  701. do_resulttypepass(right);
  702. resulttypepass(right);
  703. aktstate.store_fact(left.getcopy,right.getcopy);
  704. end
  705. else
  706. aktstate.delete_fact(left);
  707. end;
  708. {$endif}
  709. {*****************************************************************************
  710. TARRAYCONSTRUCTORRANGENODE
  711. *****************************************************************************}
  712. constructor tarrayconstructorrangenode.create(l,r : tnode);
  713. begin
  714. inherited create(arrayconstructorrangen,l,r);
  715. end;
  716. function tarrayconstructorrangenode.det_resulttype:tnode;
  717. begin
  718. result:=nil;
  719. resulttypepass(left);
  720. resulttypepass(right);
  721. set_varstate(left,vs_used,true);
  722. set_varstate(right,vs_used,true);
  723. if codegenerror then
  724. exit;
  725. resulttype:=left.resulttype;
  726. end;
  727. function tarrayconstructorrangenode.pass_1 : tnode;
  728. begin
  729. firstpass(left);
  730. firstpass(right);
  731. expectloc:=LOC_CREFERENCE;
  732. calcregisters(self,0,0,0);
  733. result:=nil;
  734. end;
  735. {****************************************************************************
  736. TARRAYCONSTRUCTORNODE
  737. *****************************************************************************}
  738. constructor tarrayconstructornode.create(l,r : tnode);
  739. begin
  740. inherited create(arrayconstructorn,l,r);
  741. end;
  742. function tarrayconstructornode.getcopy : tnode;
  743. var
  744. n : tarrayconstructornode;
  745. begin
  746. n:=tarrayconstructornode(inherited getcopy);
  747. result:=n;
  748. end;
  749. function tarrayconstructornode.det_resulttype:tnode;
  750. var
  751. htype : ttype;
  752. hp : tarrayconstructornode;
  753. len : longint;
  754. varia : boolean;
  755. begin
  756. result:=nil;
  757. { are we allowing array constructor? Then convert it to a set }
  758. if not allow_array_constructor then
  759. begin
  760. hp:=tarrayconstructornode(getcopy);
  761. arrayconstructor_to_set(tnode(hp));
  762. result:=hp;
  763. exit;
  764. end;
  765. { only pass left tree, right tree contains next construct if any }
  766. htype.reset;
  767. len:=0;
  768. varia:=false;
  769. if assigned(left) then
  770. begin
  771. hp:=self;
  772. while assigned(hp) do
  773. begin
  774. resulttypepass(hp.left);
  775. set_varstate(hp.left,vs_used,true);
  776. if (htype.def=nil) then
  777. htype:=hp.left.resulttype
  778. else
  779. begin
  780. if ((nf_novariaallowed in flags) or (not varia)) and
  781. (not equal_defs(htype.def,hp.left.resulttype.def)) then
  782. begin
  783. varia:=true;
  784. end;
  785. end;
  786. inc(len);
  787. hp:=tarrayconstructornode(hp.right);
  788. end;
  789. end;
  790. if not assigned(htype.def) then
  791. htype:=voidtype;
  792. resulttype.setdef(tarraydef.create(0,len-1,s32inttype));
  793. tarraydef(resulttype.def).setelementtype(htype);
  794. tarraydef(resulttype.def).IsConstructor:=true;
  795. tarraydef(resulttype.def).IsVariant:=varia;
  796. end;
  797. procedure tarrayconstructornode.force_type(tt:ttype);
  798. var
  799. hp : tarrayconstructornode;
  800. begin
  801. tarraydef(resulttype.def).setelementtype(tt);
  802. tarraydef(resulttype.def).IsConstructor:=true;
  803. tarraydef(resulttype.def).IsVariant:=false;
  804. if assigned(left) then
  805. begin
  806. hp:=self;
  807. while assigned(hp) do
  808. begin
  809. inserttypeconv(hp.left,tt);
  810. hp:=tarrayconstructornode(hp.right);
  811. end;
  812. end;
  813. end;
  814. procedure tarrayconstructornode.insert_typeconvs;
  815. var
  816. hp : tarrayconstructornode;
  817. dovariant : boolean;
  818. begin
  819. dovariant:=(nf_forcevaria in flags) or tarraydef(resulttype.def).isvariant;
  820. { only pass left tree, right tree contains next construct if any }
  821. if assigned(left) then
  822. begin
  823. hp:=self;
  824. while assigned(hp) do
  825. begin
  826. resulttypepass(hp.left);
  827. { Insert typeconvs for array of const }
  828. if dovariant then
  829. begin
  830. case hp.left.resulttype.def.deftype of
  831. enumdef :
  832. hp.left:=ctypeconvnode.create_internal(hp.left,s32inttype);
  833. arraydef :
  834. begin
  835. if is_chararray(hp.left.resulttype.def) then
  836. hp.left:=ctypeconvnode.create_internal(hp.left,charpointertype)
  837. else
  838. if is_widechararray(hp.left.resulttype.def) then
  839. hp.left:=ctypeconvnode.create_internal(hp.left,widecharpointertype)
  840. else
  841. CGMessagePos1(hp.left.fileinfo,type_e_wrong_type_in_array_constructor,hp.left.resulttype.def.typename);
  842. end;
  843. orddef :
  844. begin
  845. if is_integer(hp.left.resulttype.def) and
  846. not(is_64bitint(hp.left.resulttype.def)) then
  847. hp.left:=ctypeconvnode.create(hp.left,s32inttype);
  848. end;
  849. floatdef :
  850. hp.left:=ctypeconvnode.create(hp.left,pbestrealtype^);
  851. procvardef :
  852. hp.left:=ctypeconvnode.create(hp.left,voidpointertype);
  853. stringdef,
  854. variantdef,
  855. pointerdef,
  856. classrefdef,
  857. objectdef : ;
  858. else
  859. CGMessagePos1(hp.left.fileinfo,type_e_wrong_type_in_array_constructor,hp.left.resulttype.def.typename);
  860. end;
  861. end;
  862. resulttypepass(hp.left);
  863. hp:=tarrayconstructornode(hp.right);
  864. end;
  865. end;
  866. end;
  867. function tarrayconstructornode.pass_1 : tnode;
  868. var
  869. hp : tarrayconstructornode;
  870. do_variant:boolean;
  871. begin
  872. do_variant:=(nf_forcevaria in flags) or tarraydef(resulttype.def).isvariant;
  873. result:=nil;
  874. { Insert required type convs, this must be
  875. done in pass 1, because the call must be
  876. resulttypepassed already }
  877. if assigned(left) then
  878. begin
  879. insert_typeconvs;
  880. { call firstpass for all nodes }
  881. hp:=self;
  882. while assigned(hp) do
  883. begin
  884. if hp.left<>nil then
  885. begin
  886. {This check is pessimistic; a call will happen depending
  887. on the location in which the elements will be found in
  888. pass 2.}
  889. if not do_variant then
  890. include(current_procinfo.flags,pi_do_call);
  891. firstpass(hp.left);
  892. end;
  893. hp:=tarrayconstructornode(hp.right);
  894. end;
  895. end;
  896. expectloc:=LOC_CREFERENCE;
  897. calcregisters(self,0,0,0);
  898. end;
  899. function tarrayconstructornode.docompare(p: tnode): boolean;
  900. begin
  901. docompare:=inherited docompare(p);
  902. end;
  903. {*****************************************************************************
  904. TTYPENODE
  905. *****************************************************************************}
  906. constructor ttypenode.create(t : ttype);
  907. begin
  908. inherited create(typen);
  909. restype:=t;
  910. allowed:=false;
  911. end;
  912. constructor ttypenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  913. begin
  914. inherited ppuload(t,ppufile);
  915. ppufile.gettype(restype);
  916. allowed:=boolean(ppufile.getbyte);
  917. end;
  918. procedure ttypenode.ppuwrite(ppufile:tcompilerppufile);
  919. begin
  920. inherited ppuwrite(ppufile);
  921. ppufile.puttype(restype);
  922. ppufile.putbyte(byte(allowed));
  923. end;
  924. procedure ttypenode.buildderefimpl;
  925. begin
  926. inherited buildderefimpl;
  927. restype.buildderef;
  928. end;
  929. procedure ttypenode.derefimpl;
  930. begin
  931. inherited derefimpl;
  932. restype.resolve;
  933. end;
  934. function ttypenode.det_resulttype:tnode;
  935. begin
  936. result:=nil;
  937. resulttype:=restype;
  938. { check if it's valid }
  939. if restype.def.deftype = errordef then
  940. CGMessage(parser_e_illegal_expression);
  941. end;
  942. function ttypenode.pass_1 : tnode;
  943. begin
  944. result:=nil;
  945. expectloc:=LOC_VOID;
  946. { a typenode can't generate code, so we give here
  947. an error. Else it'll be an abstract error in pass_2.
  948. Only when the allowed flag is set we don't generate
  949. an error }
  950. if not allowed then
  951. Message(parser_e_no_type_not_allowed_here);
  952. end;
  953. function ttypenode.docompare(p: tnode): boolean;
  954. begin
  955. docompare :=
  956. inherited docompare(p);
  957. end;
  958. {*****************************************************************************
  959. TRTTINODE
  960. *****************************************************************************}
  961. constructor trttinode.create(def:tstoreddef;rt:trttitype);
  962. begin
  963. inherited create(rttin);
  964. rttidef:=def;
  965. rttitype:=rt;
  966. end;
  967. constructor trttinode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  968. begin
  969. inherited ppuload(t,ppufile);
  970. ppufile.getderef(rttidefderef);
  971. rttitype:=trttitype(ppufile.getbyte);
  972. end;
  973. procedure trttinode.ppuwrite(ppufile:tcompilerppufile);
  974. begin
  975. inherited ppuwrite(ppufile);
  976. ppufile.putderef(rttidefderef);
  977. ppufile.putbyte(byte(rttitype));
  978. end;
  979. procedure trttinode.buildderefimpl;
  980. begin
  981. inherited buildderefimpl;
  982. rttidefderef.build(rttidef);
  983. end;
  984. procedure trttinode.derefimpl;
  985. begin
  986. inherited derefimpl;
  987. rttidef:=tstoreddef(rttidefderef.resolve);
  988. end;
  989. function trttinode.getcopy : tnode;
  990. var
  991. n : trttinode;
  992. begin
  993. n:=trttinode(inherited getcopy);
  994. n.rttidef:=rttidef;
  995. n.rttitype:=rttitype;
  996. result:=n;
  997. end;
  998. function trttinode.det_resulttype:tnode;
  999. begin
  1000. { rtti information will be returned as a void pointer }
  1001. result:=nil;
  1002. resulttype:=voidpointertype;
  1003. end;
  1004. function trttinode.pass_1 : tnode;
  1005. begin
  1006. result:=nil;
  1007. expectloc:=LOC_CREFERENCE;
  1008. end;
  1009. function trttinode.docompare(p: tnode): boolean;
  1010. begin
  1011. docompare :=
  1012. inherited docompare(p) and
  1013. (rttidef = trttinode(p).rttidef) and
  1014. (rttitype = trttinode(p).rttitype);
  1015. end;
  1016. begin
  1017. cloadnode:=tloadnode;
  1018. cassignmentnode:=tassignmentnode;
  1019. carrayconstructorrangenode:=tarrayconstructorrangenode;
  1020. carrayconstructornode:=tarrayconstructornode;
  1021. ctypenode:=ttypenode;
  1022. crttinode:=trttinode;
  1023. end.
  1024. {
  1025. $Log$
  1026. Revision 1.142 2004-12-26 16:21:39 peter
  1027. * resourcestring does not need exception frame
  1028. Revision 1.141 2004/12/07 13:52:54 michael
  1029. * Convert array of widechar to pwidechar instead of pchar
  1030. Revision 1.140 2004/12/05 12:28:11 peter
  1031. * procvar handling for tp procvar mode fixed
  1032. * proc to procvar moved from addrnode to typeconvnode
  1033. * inlininginfo is now allocated only for inline routines that
  1034. can be inlined, introduced a new flag po_has_inlining_info
  1035. Revision 1.139 2004/11/08 22:09:59 peter
  1036. * tvarsym splitted
  1037. Revision 1.138 2004/11/02 12:55:16 peter
  1038. * nf_internal flag for internal inserted typeconvs. This will
  1039. supress the generation of warning/hints
  1040. Revision 1.137 2004/11/01 15:32:12 peter
  1041. * support @labelsym
  1042. Revision 1.136 2004/10/31 21:45:03 peter
  1043. * generic tlocation
  1044. * move tlocation to cgutils
  1045. Revision 1.135 2004/10/24 11:44:28 peter
  1046. * small regvar fixes
  1047. * loadref parameter removed from concatcopy,incrrefcount,etc
  1048. Revision 1.134 2004/10/12 14:35:14 peter
  1049. * fixed crash when current_procinfo was not yet available
  1050. Revision 1.133 2004/10/11 15:48:15 peter
  1051. * small regvar for para fixes
  1052. * function tvarsym.is_regvar added
  1053. * tvarsym.getvaluesize removed, use getsize instead
  1054. Revision 1.132 2004/10/10 20:22:53 peter
  1055. * symtable allocation rewritten
  1056. * loading of parameters to local temps/regs cleanup
  1057. * regvar support for parameters
  1058. * regvar support for staticsymtable (main body)
  1059. Revision 1.131 2004/10/08 17:09:43 peter
  1060. * tvarsym.varregable added, split vo_regable from varoptions
  1061. Revision 1.130 2004/10/06 19:26:50 jonas
  1062. * regvar fixes from Peter
  1063. Revision 1.129 2004/09/26 17:45:30 peter
  1064. * simple regvar support, not yet finished
  1065. Revision 1.128 2004/06/20 08:55:29 florian
  1066. * logs truncated
  1067. Revision 1.127 2004/06/16 20:07:08 florian
  1068. * dwarf branch merged
  1069. Revision 1.126 2004/04/29 19:56:37 daniel
  1070. * Prepare compiler infrastructure for multiple ansistring types
  1071. Revision 1.125.2.1 2004/04/28 19:55:51 peter
  1072. * new warning for ordinal-pointer when size is different
  1073. * fixed some cg_e_ messages to the correct section type_e_ or parser_e_
  1074. Revision 1.125 2004/03/02 17:32:12 florian
  1075. * make cycle fixed
  1076. + pic support for darwin
  1077. + support of importing vars from shared libs on darwin implemented
  1078. Revision 1.124 2004/02/20 22:15:26 peter
  1079. * fixed compiler err
  1080. }