nld.pas 42 KB

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