nld.pas 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. Type checking and register allocation for load/assignment nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit nld;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,
  22. {$ifdef state_tracking}
  23. nstate,
  24. {$endif}
  25. symconst,symbase,symtype,symsym,symdef;
  26. type
  27. tloadnode = class(tunarynode)
  28. protected
  29. procdef : tprocdef;
  30. procdefderef : tderef;
  31. public
  32. symtableentry : tsym;
  33. symtableentryderef : tderef;
  34. symtable : TSymtable;
  35. constructor create(v : tsym;st : TSymtable);virtual;
  36. constructor create_procvar(v : tsym;d:tprocdef;st : TSymtable);virtual;
  37. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  38. procedure ppuwrite(ppufile:tcompilerppufile);override;
  39. procedure buildderefimpl;override;
  40. procedure derefimpl;override;
  41. procedure set_mp(p:tnode);
  42. function is_addr_param_load:boolean;
  43. function dogetcopy : tnode;override;
  44. function pass_1 : tnode;override;
  45. function pass_typecheck:tnode;override;
  46. procedure mark_write;override;
  47. function docompare(p: tnode): boolean; override;
  48. procedure printnodedata(var t:text);override;
  49. procedure setprocdef(p : tprocdef);
  50. end;
  51. tloadnodeclass = class of tloadnode;
  52. { different assignment types }
  53. tassigntype = (at_normal,at_plus,at_minus,at_star,at_slash);
  54. tassignmentnode = class(tbinarynode)
  55. assigntype : tassigntype;
  56. constructor create(l,r : tnode);virtual;
  57. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  58. procedure ppuwrite(ppufile:tcompilerppufile);override;
  59. function dogetcopy : tnode;override;
  60. function pass_1 : tnode;override;
  61. function pass_typecheck:tnode;override;
  62. function simplify : tnode;override;
  63. {$ifdef state_tracking}
  64. function track_state_pass(exec_known:boolean):boolean;override;
  65. {$endif state_tracking}
  66. function docompare(p: tnode): boolean; override;
  67. end;
  68. tassignmentnodeclass = class of tassignmentnode;
  69. tarrayconstructorrangenode = class(tbinarynode)
  70. constructor create(l,r : tnode);virtual;
  71. function pass_1 : tnode;override;
  72. function pass_typecheck:tnode;override;
  73. end;
  74. tarrayconstructorrangenodeclass = class of tarrayconstructorrangenode;
  75. tarrayconstructornode = class(tbinarynode)
  76. constructor create(l,r : tnode);virtual;
  77. function dogetcopy : tnode;override;
  78. function pass_1 : tnode;override;
  79. function pass_typecheck:tnode;override;
  80. function docompare(p: tnode): boolean; override;
  81. procedure force_type(def:tdef);
  82. procedure insert_typeconvs;
  83. end;
  84. tarrayconstructornodeclass = class of tarrayconstructornode;
  85. ttypenode = class(tnode)
  86. allowed : boolean;
  87. typedef : tdef;
  88. typedefderef : tderef;
  89. constructor create(def:tdef);virtual;
  90. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  91. procedure ppuwrite(ppufile:tcompilerppufile);override;
  92. procedure buildderefimpl;override;
  93. procedure derefimpl;override;
  94. function pass_1 : tnode;override;
  95. function pass_typecheck:tnode;override;
  96. function dogetcopy : tnode;override;
  97. function docompare(p: tnode): boolean; override;
  98. end;
  99. ttypenodeclass = class of ttypenode;
  100. trttinode = class(tnode)
  101. l1,l2 : longint;
  102. rttitype : trttitype;
  103. rttidef : tstoreddef;
  104. rttidefderef : tderef;
  105. constructor create(def:tstoreddef;rt:trttitype);virtual;
  106. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  107. procedure ppuwrite(ppufile:tcompilerppufile);override;
  108. procedure buildderefimpl;override;
  109. procedure derefimpl;override;
  110. function dogetcopy : tnode;override;
  111. function pass_1 : tnode;override;
  112. function pass_typecheck:tnode;override;
  113. function docompare(p: tnode): boolean; override;
  114. end;
  115. trttinodeclass = class of trttinode;
  116. var
  117. cloadnode : tloadnodeclass;
  118. cassignmentnode : tassignmentnodeclass;
  119. carrayconstructorrangenode : tarrayconstructorrangenodeclass;
  120. carrayconstructornode : tarrayconstructornodeclass;
  121. ctypenode : ttypenodeclass;
  122. crttinode : trttinodeclass;
  123. { Current assignment node }
  124. aktassignmentnode : tassignmentnode;
  125. implementation
  126. uses
  127. cutils,verbose,globtype,globals,systems,
  128. symnot,
  129. defutil,defcmp,
  130. htypechk,pass_1,procinfo,paramgr,
  131. cpuinfo,
  132. ncon,ninl,ncnv,nmem,ncal,nutils,nbas,
  133. cgobj,cgbase
  134. ;
  135. {*****************************************************************************
  136. TLOADNODE
  137. *****************************************************************************}
  138. constructor tloadnode.create(v : tsym;st : TSymtable);
  139. begin
  140. inherited create(loadn,nil);
  141. if not assigned(v) then
  142. internalerror(200108121);
  143. symtableentry:=v;
  144. symtable:=st;
  145. procdef:=nil;
  146. end;
  147. constructor tloadnode.create_procvar(v : tsym;d:tprocdef;st : TSymtable);
  148. begin
  149. inherited create(loadn,nil);
  150. if not assigned(v) then
  151. internalerror(200108121);
  152. symtableentry:=v;
  153. symtable:=st;
  154. procdef:=d;
  155. end;
  156. constructor tloadnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  157. begin
  158. inherited ppuload(t,ppufile);
  159. ppufile.getderef(symtableentryderef);
  160. symtable:=nil;
  161. ppufile.getderef(procdefderef);
  162. end;
  163. procedure tloadnode.ppuwrite(ppufile:tcompilerppufile);
  164. begin
  165. inherited ppuwrite(ppufile);
  166. ppufile.putderef(symtableentryderef);
  167. ppufile.putderef(procdefderef);
  168. end;
  169. procedure tloadnode.buildderefimpl;
  170. begin
  171. inherited buildderefimpl;
  172. symtableentryderef.build(symtableentry);
  173. procdefderef.build(procdef);
  174. end;
  175. procedure tloadnode.derefimpl;
  176. begin
  177. inherited derefimpl;
  178. symtableentry:=tsym(symtableentryderef.resolve);
  179. symtable:=symtableentry.owner;
  180. procdef:=tprocdef(procdefderef.resolve);
  181. end;
  182. procedure tloadnode.set_mp(p:tnode);
  183. begin
  184. { typen nodes should not be set }
  185. if p.nodetype=typen then
  186. internalerror(200301042);
  187. left:=p;
  188. end;
  189. function tloadnode.dogetcopy : tnode;
  190. var
  191. n : tloadnode;
  192. begin
  193. n:=tloadnode(inherited dogetcopy);
  194. n.symtable:=symtable;
  195. n.symtableentry:=symtableentry;
  196. n.procdef:=procdef;
  197. result:=n;
  198. end;
  199. function tloadnode.is_addr_param_load:boolean;
  200. begin
  201. result:=(symtable.symtabletype=parasymtable) and
  202. (symtableentry.typ=paravarsym) and
  203. not(vo_has_local_copy in tparavarsym(symtableentry).varoptions) and
  204. not(nf_load_self_pointer in flags) and
  205. paramanager.push_addr_param(tparavarsym(symtableentry).varspez,tparavarsym(symtableentry).vardef,tprocdef(symtable.defowner).proccalloption);
  206. end;
  207. function tloadnode.pass_typecheck:tnode;
  208. begin
  209. result:=nil;
  210. case symtableentry.typ of
  211. absolutevarsym :
  212. resultdef:=tabsolutevarsym(symtableentry).vardef;
  213. constsym:
  214. begin
  215. if tconstsym(symtableentry).consttyp=constresourcestring then
  216. resultdef:=cansistringtype
  217. else
  218. internalerror(22799);
  219. end;
  220. staticvarsym :
  221. begin
  222. tabstractvarsym(symtableentry).IncRefCountBy(1);
  223. { static variables referenced in procedures or from finalization,
  224. variable needs to be in memory.
  225. It is too hard and the benefit is too small to detect whether a
  226. variable is only used in the finalization to add support for it (PFV) }
  227. if assigned(current_procinfo) and
  228. (symtable.symtabletype=staticsymtable) and
  229. (
  230. (symtable.symtablelevel<>current_procinfo.procdef.localst.symtablelevel) or
  231. (current_procinfo.procdef.proctypeoption=potype_unitfinalize)
  232. ) then
  233. make_not_regable(self,[ra_addr_taken]);
  234. resultdef:=tabstractvarsym(symtableentry).vardef;
  235. end;
  236. paravarsym,
  237. localvarsym :
  238. begin
  239. tabstractvarsym(symtableentry).IncRefCountBy(1);
  240. { Nested variable? The we need to load the framepointer of
  241. the parent procedure }
  242. if assigned(current_procinfo) and
  243. (symtable.symtabletype in [localsymtable,parasymtable]) and
  244. (symtable.symtablelevel<>current_procinfo.procdef.parast.symtablelevel) then
  245. begin
  246. if assigned(left) then
  247. internalerror(200309289);
  248. left:=cloadparentfpnode.create(tprocdef(symtable.defowner));
  249. { we can't inline the referenced parent procedure }
  250. exclude(tprocdef(symtable.defowner).procoptions,po_inline);
  251. { reference in nested procedures, variable needs to be in memory }
  252. { and behaves as if its address escapes its parent block }
  253. make_not_regable(self,[ra_addr_taken]);
  254. end;
  255. { fix self type which is declared as voidpointer in the
  256. definition }
  257. if vo_is_self in tabstractvarsym(symtableentry).varoptions then
  258. begin
  259. resultdef:=tprocdef(symtableentry.owner.defowner)._class;
  260. if (po_classmethod in tprocdef(symtableentry.owner.defowner).procoptions) or
  261. (po_staticmethod in tprocdef(symtableentry.owner.defowner).procoptions) then
  262. resultdef:=tclassrefdef.create(resultdef)
  263. else if is_object(resultdef) and
  264. (nf_load_self_pointer in flags) then
  265. resultdef:=tpointerdef.create(resultdef);
  266. end
  267. else if vo_is_vmt in tabstractvarsym(symtableentry).varoptions then
  268. begin
  269. resultdef:=tprocdef(symtableentry.owner.defowner)._class;
  270. resultdef:=tclassrefdef.create(resultdef);
  271. end
  272. else
  273. resultdef:=tabstractvarsym(symtableentry).vardef;
  274. end;
  275. procsym :
  276. begin
  277. { Return the first procdef. In case of overlaoded
  278. procdefs the matching procdef will be choosen
  279. when the expected procvardef is known, see get_information
  280. in htypechk.pas (PFV) }
  281. if not assigned(procdef) then
  282. procdef:=tprocdef(tprocsym(symtableentry).ProcdefList[0])
  283. else if po_kylixlocal in procdef.procoptions then
  284. CGMessage(type_e_cant_take_address_of_local_subroutine);
  285. { the result is a procdef, addrn and proc_to_procvar
  286. typeconvn need this as resultdef so they know
  287. that the address needs to be returned }
  288. resultdef:=procdef;
  289. { process methodpointer }
  290. if assigned(left) then
  291. typecheckpass(left);
  292. end;
  293. labelsym:
  294. resultdef:=voidtype;
  295. else
  296. internalerror(200104141);
  297. end;
  298. end;
  299. procedure Tloadnode.mark_write;
  300. begin
  301. include(flags,nf_write);
  302. end;
  303. function tloadnode.pass_1 : tnode;
  304. begin
  305. result:=nil;
  306. expectloc:=LOC_REFERENCE;
  307. registersint:=0;
  308. registersfpu:=0;
  309. {$ifdef SUPPORT_MMX}
  310. registersmmx:=0;
  311. {$endif SUPPORT_MMX}
  312. if (cs_create_pic in current_settings.moduleswitches) and
  313. not(symtableentry.typ in [paravarsym,localvarsym]) then
  314. include(current_procinfo.flags,pi_needs_got);
  315. case symtableentry.typ of
  316. absolutevarsym :
  317. ;
  318. constsym:
  319. begin
  320. if tconstsym(symtableentry).consttyp=constresourcestring then
  321. expectloc:=LOC_CREFERENCE;
  322. end;
  323. staticvarsym,
  324. localvarsym,
  325. paravarsym :
  326. begin
  327. if assigned(left) then
  328. firstpass(left);
  329. if not is_addr_param_load and
  330. tabstractvarsym(symtableentry).is_regvar(is_addr_param_load) then
  331. expectloc:=tvarregable2tcgloc[tabstractvarsym(symtableentry).varregable]
  332. else
  333. if (tabstractvarsym(symtableentry).varspez=vs_const) then
  334. expectloc:=LOC_CREFERENCE;
  335. { we need a register for call by reference parameters }
  336. if paramanager.push_addr_param(tabstractvarsym(symtableentry).varspez,tabstractvarsym(symtableentry).vardef,pocall_default) then
  337. registersint:=1;
  338. if ([vo_is_thread_var,vo_is_dll_var]*tabstractvarsym(symtableentry).varoptions)<>[] then
  339. registersint:=1;
  340. if (target_info.system=system_powerpc_darwin) and
  341. ([vo_is_dll_var,vo_is_external] * tabstractvarsym(symtableentry).varoptions <> []) then
  342. include(current_procinfo.flags,pi_needs_got);
  343. { call to get address of threadvar }
  344. if (vo_is_thread_var in tabstractvarsym(symtableentry).varoptions) then
  345. include(current_procinfo.flags,pi_do_call);
  346. if nf_write in flags then
  347. Tabstractvarsym(symtableentry).trigger_notifications(vn_onwrite)
  348. else
  349. Tabstractvarsym(symtableentry).trigger_notifications(vn_onread);
  350. { count variable references }
  351. if cg.t_times>1 then
  352. tabstractvarsym(symtableentry).IncRefCountBy(cg.t_times-1);
  353. end;
  354. procsym :
  355. begin
  356. { method pointer ? }
  357. if assigned(left) then
  358. begin
  359. expectloc:=LOC_CREFERENCE;
  360. firstpass(left);
  361. registersint:=max(registersint,left.registersint);
  362. registersfpu:=max(registersfpu,left.registersfpu);
  363. {$ifdef SUPPORT_MMX}
  364. registersmmx:=max(registersmmx,left.registersmmx);
  365. {$endif SUPPORT_MMX}
  366. end;
  367. end;
  368. labelsym :
  369. ;
  370. else
  371. internalerror(200104143);
  372. end;
  373. end;
  374. function tloadnode.docompare(p: tnode): boolean;
  375. begin
  376. docompare :=
  377. inherited docompare(p) and
  378. (symtableentry = tloadnode(p).symtableentry) and
  379. (procdef = tloadnode(p).procdef) and
  380. (symtable = tloadnode(p).symtable);
  381. end;
  382. procedure tloadnode.printnodedata(var t:text);
  383. begin
  384. inherited printnodedata(t);
  385. write(t,printnodeindention,'symbol = ',symtableentry.name);
  386. if symtableentry.typ=procsym then
  387. write(t,printnodeindention,'procdef = ',procdef.mangledname);
  388. writeln(t,'');
  389. end;
  390. procedure tloadnode.setprocdef(p : tprocdef);
  391. begin
  392. procdef:=p;
  393. resultdef:=p;
  394. if po_local in p.procoptions then
  395. CGMessage(type_e_cant_take_address_of_local_subroutine);
  396. end;
  397. {*****************************************************************************
  398. TASSIGNMENTNODE
  399. *****************************************************************************}
  400. constructor tassignmentnode.create(l,r : tnode);
  401. begin
  402. inherited create(assignn,l,r);
  403. l.mark_write;
  404. assigntype:=at_normal;
  405. end;
  406. constructor tassignmentnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  407. begin
  408. inherited ppuload(t,ppufile);
  409. assigntype:=tassigntype(ppufile.getbyte);
  410. end;
  411. procedure tassignmentnode.ppuwrite(ppufile:tcompilerppufile);
  412. begin
  413. inherited ppuwrite(ppufile);
  414. ppufile.putbyte(byte(assigntype));
  415. end;
  416. function tassignmentnode.dogetcopy : tnode;
  417. var
  418. n : tassignmentnode;
  419. begin
  420. n:=tassignmentnode(inherited dogetcopy);
  421. n.assigntype:=assigntype;
  422. result:=n;
  423. end;
  424. function tassignmentnode.simplify : tnode;
  425. begin
  426. result:=nil;
  427. { assignment nodes can perform several floating point }
  428. { type conversions directly, so no typeconversions }
  429. { are inserted in those cases. When inlining, a }
  430. { variable may be replaced by a constant which can be }
  431. { converted at compile time, so check for this case }
  432. if is_real(left.resultdef) and
  433. is_real(right.resultdef) and
  434. is_constrealnode(right) and
  435. not equal_defs(right.resultdef,left.resultdef) then
  436. inserttypeconv(right,left.resultdef);
  437. end;
  438. function tassignmentnode.pass_typecheck:tnode;
  439. var
  440. hp : tnode;
  441. useshelper : boolean;
  442. begin
  443. result:=nil;
  444. resultdef:=voidtype;
  445. { must be made unique }
  446. set_unique(left);
  447. typecheckpass(left);
  448. typecheckpass(right);
  449. set_varstate(right,vs_read,[vsf_must_be_valid]);
  450. set_varstate(left,vs_written,[]);
  451. if codegenerror then
  452. exit;
  453. { tp procvar support, when we don't expect a procvar
  454. then we need to call the procvar }
  455. if (left.resultdef.typ<>procvardef) then
  456. maybe_call_procvar(right,true);
  457. { assignments to formaldefs and open arrays aren't allowed }
  458. if (left.resultdef.typ=formaldef) or
  459. is_open_array(left.resultdef) then
  460. CGMessage(type_e_assignment_not_allowed);
  461. { test if node can be assigned, properties are allowed }
  462. valid_for_assignment(left,true);
  463. { assigning nil to a dynamic array clears the array }
  464. if is_dynamic_array(left.resultdef) and
  465. (right.nodetype=niln) then
  466. begin
  467. hp:=ccallparanode.create(caddrnode.create_internal
  468. (crttinode.create(tstoreddef(left.resultdef),initrtti)),
  469. ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil));
  470. result := ccallnode.createintern('fpc_dynarray_clear',hp);
  471. left:=nil;
  472. exit;
  473. end;
  474. { shortstring helpers can do the conversion directly,
  475. so treat them separatly }
  476. if (is_shortstring(left.resultdef)) then
  477. begin
  478. { insert typeconv, except for chars that are handled in
  479. secondpass and except for ansi/wide string that can
  480. be converted immediatly }
  481. if not(is_char(right.resultdef) or
  482. (right.resultdef.typ=stringdef)) then
  483. inserttypeconv(right,left.resultdef);
  484. if right.resultdef.typ=stringdef then
  485. begin
  486. useshelper:=true;
  487. { convert constant strings to shortstrings. But
  488. skip empty constant strings, that will be handled
  489. in secondpass }
  490. if (right.nodetype=stringconstn) then
  491. begin
  492. { verify if range fits within shortstring }
  493. { just emit a warning, delphi gives an }
  494. { error, only if the type definition of }
  495. { of the string is less < 255 characters }
  496. if not is_open_string(left.resultdef) and
  497. (tstringconstnode(right).len > tstringdef(left.resultdef).len) then
  498. cgmessage(type_w_string_too_long);
  499. inserttypeconv(right,left.resultdef);
  500. if (right.nodetype=stringconstn) and
  501. (tstringconstnode(right).len=0) then
  502. useshelper:=false;
  503. end;
  504. { rest is done in pass 1 (JM) }
  505. if useshelper then
  506. exit;
  507. end
  508. end
  509. { floating point assignments can also perform the conversion directly }
  510. else if is_real(left.resultdef) and is_real(right.resultdef) and
  511. not is_constrealnode(right)
  512. {$ifdef cpufpemu}
  513. { the emulator can't do this obviously }
  514. and not(current_settings.fputype in [fpu_libgcc,fpu_soft])
  515. {$endif cpufpemu}
  516. {$ifdef x86}
  517. { the assignment node code can't convert a double in an }
  518. { sse register to an extended value in memory more }
  519. { efficiently than a type conversion node, so don't }
  520. { bother implementing support for that }
  521. and (use_sse(left.resultdef) or not(use_sse(right.resultdef)))
  522. {$endif}
  523. then
  524. begin
  525. check_ranges(fileinfo,right,left.resultdef);
  526. end
  527. else
  528. begin
  529. { check if the assignment may cause a range check error }
  530. check_ranges(fileinfo,right,left.resultdef);
  531. inserttypeconv(right,left.resultdef);
  532. end;
  533. { call helpers for interface }
  534. if is_interfacecom(left.resultdef) then
  535. begin
  536. { Normal interface assignments are handled by the generic refcount incr/decr }
  537. if not right.resultdef.is_related(left.resultdef) then
  538. begin
  539. { remove property flag to avoid errors, see comments for }
  540. { tf_winlikewidestring assignments below }
  541. exclude(left.flags,nf_isproperty);
  542. hp:=
  543. ccallparanode.create(
  544. cguidconstnode.create(tobjectdef(left.resultdef).iidguid^),
  545. ccallparanode.create(
  546. ctypeconvnode.create_internal(right,voidpointertype),
  547. ccallparanode.create(
  548. ctypeconvnode.create_internal(left,voidpointertype),
  549. nil)));
  550. result:=ccallnode.createintern('fpc_intf_assign_by_iid',hp);
  551. left:=nil;
  552. right:=nil;
  553. exit;
  554. end;
  555. end;
  556. { check if local proc/func is assigned to procvar }
  557. if right.resultdef.typ=procvardef then
  558. test_local_to_procvar(tprocvardef(right.resultdef),left.resultdef);
  559. end;
  560. function tassignmentnode.pass_1 : tnode;
  561. var
  562. hp: tnode;
  563. oldassignmentnode : tassignmentnode;
  564. begin
  565. result:=nil;
  566. expectloc:=LOC_VOID;
  567. firstpass(left);
  568. { Optimize the reuse of the destination of the assingment in left.
  569. Allow the use of the left inside the tree generated on the right.
  570. This is especially usefull for string routines where the destination
  571. is pushed as a parameter. Using the final destination of left directly
  572. save a temp allocation and copy of data (PFV) }
  573. oldassignmentnode:=aktassignmentnode;
  574. if right.nodetype=addn then
  575. aktassignmentnode:=self
  576. else
  577. aktassignmentnode:=nil;
  578. firstpass(right);
  579. aktassignmentnode:=oldassignmentnode;
  580. if nf_assign_done_in_right in flags then
  581. begin
  582. result:=right;
  583. right:=nil;
  584. exit;
  585. end;
  586. if codegenerror then
  587. exit;
  588. { if right is a function call for which the address of the result }
  589. { is allocated by the caller and passed to the function via an }
  590. { invisible function result, try to pass the x in "x:=f(...)" as }
  591. { that function result instead. Condition: x cannot be accessible }
  592. { from within f. This is the case if x is a temp, or x is a local }
  593. { variable or value parameter of the current block and its address }
  594. { is not passed to f. One problem: what if someone takes the }
  595. { address of x, puts it in a pointer variable/field and then }
  596. { accesses it that way from within the function? This is solved }
  597. { (in a conservative way) using the ti_addr_taken/addr_taken flags }
  598. if (cs_opt_level1 in current_settings.optimizerswitches) and
  599. (right.nodetype = calln) and
  600. (right.resultdef=left.resultdef) and
  601. { left must be a temp, since otherwise as soon as you modify the }
  602. { result, the current left node is modified and that one may }
  603. { still be an argument to the function or even accessed in the }
  604. { function }
  605. (
  606. (
  607. (((left.nodetype = temprefn) and
  608. not(ti_addr_taken in ttemprefnode(left).tempinfo^.flags) and
  609. not(ti_may_be_in_reg in ttemprefnode(left).tempinfo^.flags)) or
  610. ((left.nodetype = loadn) and
  611. { nested procedures may access the current procedure's locals }
  612. (tcallnode(right).procdefinition.parast.symtablelevel=normal_function_level) and
  613. { must be a local variable or a value para }
  614. ((tloadnode(left).symtableentry.typ = localvarsym) or
  615. ((tloadnode(left).symtableentry.typ = paravarsym) and
  616. (tparavarsym(tloadnode(left).symtableentry).varspez = vs_value)
  617. )
  618. ) and
  619. { the address may not have been taken of the variable/parameter, because }
  620. { otherwise it's possible that the called function can access it via a }
  621. { global variable or other stored state }
  622. not(tabstractvarsym(tloadnode(left).symtableentry).addr_taken) and
  623. (tabstractvarsym(tloadnode(left).symtableentry).varregable in [vr_none,vr_addr])
  624. )
  625. ) and
  626. paramanager.ret_in_param(right.resultdef,tcallnode(right).procdefinition.proccalloption)
  627. ) or
  628. { there's special support for ansi/widestrings in the callnode }
  629. is_ansistring(right.resultdef) or
  630. is_widestring(right.resultdef)
  631. ) then
  632. begin
  633. if assigned(tcallnode(right).funcretnode) then
  634. internalerror(2007080201);
  635. tcallnode(right).funcretnode := left;
  636. result := right;
  637. left := nil;
  638. right := nil;
  639. exit;
  640. end;
  641. { assignment to refcounted variable -> inc/decref }
  642. if (not is_class(left.resultdef) and
  643. left.resultdef.needs_inittable) then
  644. include(current_procinfo.flags,pi_do_call);
  645. if (is_shortstring(left.resultdef)) then
  646. begin
  647. if right.resultdef.typ=stringdef then
  648. begin
  649. if (right.nodetype<>stringconstn) or
  650. (tstringconstnode(right).len<>0) then
  651. begin
  652. hp:=ccallparanode.create
  653. (right,
  654. ccallparanode.create(left,nil));
  655. result:=ccallnode.createintern('fpc_'+tstringdef(right.resultdef).stringtypname+'_to_shortstr',hp);
  656. firstpass(result);
  657. left:=nil;
  658. right:=nil;
  659. exit;
  660. end;
  661. end;
  662. end
  663. { call helpers for composite types containing automated types }
  664. else if (left.resultdef.needs_inittable) and
  665. (left.resultdef.typ in [arraydef,objectdef,recorddef]) and
  666. not is_interfacecom(left.resultdef) and
  667. not is_dynamic_array(left.resultdef) then
  668. begin
  669. hp:=ccallparanode.create(caddrnode.create_internal(
  670. crttinode.create(tstoreddef(left.resultdef),initrtti)),
  671. ccallparanode.create(ctypeconvnode.create_internal(
  672. caddrnode.create_internal(left),voidpointertype),
  673. ccallparanode.create(ctypeconvnode.create_internal(
  674. caddrnode.create_internal(right),voidpointertype),
  675. nil)));
  676. result:=ccallnode.createintern('fpc_copy_proc',hp);
  677. firstpass(result);
  678. left:=nil;
  679. right:=nil;
  680. exit;
  681. end
  682. { call helpers for variant, they can contain non ref. counted types like
  683. vararrays which must be really copied }
  684. else if left.resultdef.typ=variantdef then
  685. begin
  686. hp:=ccallparanode.create(ctypeconvnode.create_internal(
  687. caddrnode.create_internal(right),voidpointertype),
  688. ccallparanode.create(ctypeconvnode.create_internal(
  689. caddrnode.create_internal(left),voidpointertype),
  690. nil));
  691. result:=ccallnode.createintern('fpc_variant_copy',hp);
  692. firstpass(result);
  693. left:=nil;
  694. right:=nil;
  695. exit;
  696. end
  697. { call helpers for windows widestrings, they aren't ref. counted }
  698. else if (tf_winlikewidestring in target_info.flags) and is_widestring(left.resultdef) then
  699. begin
  700. { The first argument of fpc_widestr_assign is a var parameter. Properties cannot }
  701. { be passed to var or out parameters, because in that case setters/getters are not }
  702. { used. Further, if we would allow it in case there are no getters or setters, you }
  703. { would need source changes in case these are introduced later on, thus defeating }
  704. { part of the transparency advantages of properties. In this particular case, }
  705. { however: }
  706. { a) if there is a setter, this code will not be used since then the assignment }
  707. { will be converted to a procedure call }
  708. { b) the getter is irrelevant, because fpc_widestr_assign must always decrease }
  709. { the refcount of the field to which we are writing }
  710. { c) source code changes are not required if a setter is added/removed, because }
  711. { this transformation is handled at compile time }
  712. { -> we can remove the nf_isproperty flag (if any) from left, so that in case it }
  713. { is a property which refers to a field without a setter call, we will not get }
  714. { an error about trying to pass a property as a var parameter }
  715. exclude(left.flags,nf_isproperty);
  716. hp:=ccallparanode.create(ctypeconvnode.create_internal(right,voidpointertype),
  717. ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),
  718. nil));
  719. result:=ccallnode.createintern('fpc_widestr_assign',hp);
  720. firstpass(result);
  721. left:=nil;
  722. right:=nil;
  723. exit;
  724. end;
  725. registersint:=left.registersint+right.registersint;
  726. registersfpu:=max(left.registersfpu,right.registersfpu);
  727. {$ifdef SUPPORT_MMX}
  728. registersmmx:=max(left.registersmmx,right.registersmmx);
  729. {$endif SUPPORT_MMX}
  730. end;
  731. function tassignmentnode.docompare(p: tnode): boolean;
  732. begin
  733. docompare :=
  734. inherited docompare(p) and
  735. (assigntype = tassignmentnode(p).assigntype);
  736. end;
  737. {$ifdef state_tracking}
  738. function Tassignmentnode.track_state_pass(exec_known:boolean):boolean;
  739. var se:Tstate_entry;
  740. begin
  741. track_state_pass:=false;
  742. if exec_known then
  743. begin
  744. track_state_pass:=right.track_state_pass(exec_known);
  745. {Force a new resultdef pass.}
  746. right.resultdef:=nil;
  747. do_typecheckpass(right);
  748. typecheckpass(right);
  749. aktstate.store_fact(left.getcopy,right.getcopy);
  750. end
  751. else
  752. aktstate.delete_fact(left);
  753. end;
  754. {$endif}
  755. {*****************************************************************************
  756. TARRAYCONSTRUCTORRANGENODE
  757. *****************************************************************************}
  758. constructor tarrayconstructorrangenode.create(l,r : tnode);
  759. begin
  760. inherited create(arrayconstructorrangen,l,r);
  761. end;
  762. function tarrayconstructorrangenode.pass_typecheck:tnode;
  763. begin
  764. result:=nil;
  765. typecheckpass(left);
  766. typecheckpass(right);
  767. set_varstate(left,vs_read,[vsf_must_be_valid]);
  768. set_varstate(right,vs_read,[vsf_must_be_valid]);
  769. if codegenerror then
  770. exit;
  771. resultdef:=left.resultdef;
  772. end;
  773. function tarrayconstructorrangenode.pass_1 : tnode;
  774. begin
  775. firstpass(left);
  776. firstpass(right);
  777. expectloc:=LOC_CREFERENCE;
  778. calcregisters(self,0,0,0);
  779. result:=nil;
  780. end;
  781. {****************************************************************************
  782. TARRAYCONSTRUCTORNODE
  783. *****************************************************************************}
  784. constructor tarrayconstructornode.create(l,r : tnode);
  785. begin
  786. inherited create(arrayconstructorn,l,r);
  787. end;
  788. function tarrayconstructornode.dogetcopy : tnode;
  789. var
  790. n : tarrayconstructornode;
  791. begin
  792. n:=tarrayconstructornode(inherited dogetcopy);
  793. result:=n;
  794. end;
  795. function tarrayconstructornode.pass_typecheck:tnode;
  796. var
  797. hdef : tdef;
  798. hp : tarrayconstructornode;
  799. len : longint;
  800. varia : boolean;
  801. eq : tequaltype;
  802. hnodetype : tnodetype;
  803. begin
  804. result:=nil;
  805. { are we allowing array constructor? Then convert it to a set.
  806. Do this only if we didn't convert the arrayconstructor yet. This
  807. is needed for the cases where the resultdef is forced for a second
  808. run }
  809. if (not allow_array_constructor) then
  810. begin
  811. hp:=tarrayconstructornode(getcopy);
  812. arrayconstructor_to_set(tnode(hp));
  813. result:=hp;
  814. exit;
  815. end;
  816. { only pass left tree, right tree contains next construct if any }
  817. hdef:=nil;
  818. hnodetype:=errorn;
  819. len:=0;
  820. varia:=false;
  821. if assigned(left) then
  822. begin
  823. hp:=self;
  824. while assigned(hp) do
  825. begin
  826. typecheckpass(hp.left);
  827. set_varstate(hp.left,vs_read,[vsf_must_be_valid]);
  828. if (hdef=nil) then
  829. begin
  830. hdef:=hp.left.resultdef;
  831. hnodetype:=hp.left.nodetype;
  832. end
  833. else
  834. begin
  835. { If we got a niln we don't know the type yet and need to take the
  836. type of the next array element.
  837. This is to handle things like [nil,tclass,tclass], see also tw8371 (PFV) }
  838. if hnodetype=niln then
  839. begin
  840. eq:=compare_defs(hp.left.resultdef,hdef,hnodetype);
  841. if eq>te_incompatible then
  842. begin
  843. hdef:=hp.left.resultdef;
  844. hnodetype:=hp.left.nodetype;
  845. end;
  846. end
  847. else
  848. eq:=compare_defs(hdef,hp.left.resultdef,hp.left.nodetype);
  849. if (not varia) and (eq<te_equal) then
  850. begin
  851. { If both are integers we need to take the type that can hold both
  852. defs }
  853. if is_integer(hdef) and is_integer(hp.left.resultdef) then
  854. begin
  855. if is_in_limit(hdef,hp.left.resultdef) then
  856. hdef:=hp.left.resultdef;
  857. end
  858. else
  859. if (nf_novariaallowed in flags) then
  860. varia:=true;
  861. end;
  862. end;
  863. inc(len);
  864. hp:=tarrayconstructornode(hp.right);
  865. end;
  866. end;
  867. { Set the type of empty or varia arrays to void. Also
  868. do this if the type is array of const/open array
  869. because those can't be used with setelementdef }
  870. if not assigned(hdef) or
  871. varia or
  872. is_array_of_const(hdef) or
  873. is_open_array(hdef) then
  874. hdef:=voidtype;
  875. resultdef:=tarraydef.create(0,len-1,s32inttype);
  876. tarraydef(resultdef).elementdef:=hdef;
  877. include(tarraydef(resultdef).arrayoptions,ado_IsConstructor);
  878. if varia then
  879. include(tarraydef(resultdef).arrayoptions,ado_IsVariant);
  880. end;
  881. procedure tarrayconstructornode.force_type(def:tdef);
  882. var
  883. hp : tarrayconstructornode;
  884. begin
  885. tarraydef(resultdef).elementdef:=def;
  886. include(tarraydef(resultdef).arrayoptions,ado_IsConstructor);
  887. exclude(tarraydef(resultdef).arrayoptions,ado_IsVariant);
  888. if assigned(left) then
  889. begin
  890. hp:=self;
  891. while assigned(hp) do
  892. begin
  893. inserttypeconv(hp.left,def);
  894. hp:=tarrayconstructornode(hp.right);
  895. end;
  896. end;
  897. end;
  898. procedure tarrayconstructornode.insert_typeconvs;
  899. var
  900. hp : tarrayconstructornode;
  901. dovariant : boolean;
  902. begin
  903. dovariant:=(nf_forcevaria in flags) or (ado_isvariant in tarraydef(resultdef).arrayoptions);
  904. { only pass left tree, right tree contains next construct if any }
  905. if assigned(left) then
  906. begin
  907. hp:=self;
  908. while assigned(hp) do
  909. begin
  910. typecheckpass(hp.left);
  911. { Insert typeconvs for array of const }
  912. if dovariant then
  913. { at this time C varargs are no longer an arrayconstructornode }
  914. insert_varargstypeconv(hp.left,false);
  915. hp:=tarrayconstructornode(hp.right);
  916. end;
  917. end;
  918. end;
  919. function tarrayconstructornode.pass_1 : tnode;
  920. var
  921. hp : tarrayconstructornode;
  922. do_variant:boolean;
  923. begin
  924. do_variant:=(nf_forcevaria in flags) or (ado_isvariant in tarraydef(resultdef).arrayoptions);
  925. result:=nil;
  926. { Insert required type convs, this must be
  927. done in pass 1, because the call must be
  928. typecheckpassed already }
  929. if assigned(left) then
  930. begin
  931. insert_typeconvs;
  932. { call firstpass for all nodes }
  933. hp:=self;
  934. while assigned(hp) do
  935. begin
  936. if hp.left<>nil then
  937. begin
  938. {This check is pessimistic; a call will happen depending
  939. on the location in which the elements will be found in
  940. pass 2.}
  941. if not do_variant then
  942. include(current_procinfo.flags,pi_do_call);
  943. firstpass(hp.left);
  944. end;
  945. hp:=tarrayconstructornode(hp.right);
  946. end;
  947. end;
  948. expectloc:=LOC_CREFERENCE;
  949. calcregisters(self,0,0,0);
  950. end;
  951. function tarrayconstructornode.docompare(p: tnode): boolean;
  952. begin
  953. docompare:=inherited docompare(p);
  954. end;
  955. {*****************************************************************************
  956. TTYPENODE
  957. *****************************************************************************}
  958. constructor ttypenode.create(def:tdef);
  959. begin
  960. inherited create(typen);
  961. typedef:=def;
  962. allowed:=false;
  963. end;
  964. constructor ttypenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  965. begin
  966. inherited ppuload(t,ppufile);
  967. ppufile.getderef(typedefderef);
  968. allowed:=boolean(ppufile.getbyte);
  969. end;
  970. procedure ttypenode.ppuwrite(ppufile:tcompilerppufile);
  971. begin
  972. inherited ppuwrite(ppufile);
  973. ppufile.putderef(typedefderef);
  974. ppufile.putbyte(byte(allowed));
  975. end;
  976. procedure ttypenode.buildderefimpl;
  977. begin
  978. inherited buildderefimpl;
  979. typedefderef.build(typedef);
  980. end;
  981. procedure ttypenode.derefimpl;
  982. begin
  983. inherited derefimpl;
  984. typedef:=tdef(typedefderef.resolve);
  985. end;
  986. function ttypenode.pass_typecheck:tnode;
  987. begin
  988. result:=nil;
  989. resultdef:=typedef;
  990. { check if it's valid }
  991. if typedef.typ = errordef then
  992. CGMessage(parser_e_illegal_expression);
  993. end;
  994. function ttypenode.pass_1 : tnode;
  995. begin
  996. result:=nil;
  997. expectloc:=LOC_VOID;
  998. { a typenode can't generate code, so we give here
  999. an error. Else it'll be an abstract error in pass_generate_code.
  1000. Only when the allowed flag is set we don't generate
  1001. an error }
  1002. if not allowed then
  1003. Message(parser_e_no_type_not_allowed_here);
  1004. end;
  1005. function ttypenode.dogetcopy : tnode;
  1006. var
  1007. n : ttypenode;
  1008. begin
  1009. n:=ttypenode(inherited dogetcopy);
  1010. n.allowed:=allowed;
  1011. n.typedef:=typedef;
  1012. result:=n;
  1013. end;
  1014. function ttypenode.docompare(p: tnode): boolean;
  1015. begin
  1016. docompare :=
  1017. inherited docompare(p);
  1018. end;
  1019. {*****************************************************************************
  1020. TRTTINODE
  1021. *****************************************************************************}
  1022. constructor trttinode.create(def:tstoreddef;rt:trttitype);
  1023. begin
  1024. inherited create(rttin);
  1025. rttidef:=def;
  1026. rttitype:=rt;
  1027. end;
  1028. constructor trttinode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1029. begin
  1030. inherited ppuload(t,ppufile);
  1031. ppufile.getderef(rttidefderef);
  1032. rttitype:=trttitype(ppufile.getbyte);
  1033. end;
  1034. procedure trttinode.ppuwrite(ppufile:tcompilerppufile);
  1035. begin
  1036. inherited ppuwrite(ppufile);
  1037. ppufile.putderef(rttidefderef);
  1038. ppufile.putbyte(byte(rttitype));
  1039. end;
  1040. procedure trttinode.buildderefimpl;
  1041. begin
  1042. inherited buildderefimpl;
  1043. rttidefderef.build(rttidef);
  1044. end;
  1045. procedure trttinode.derefimpl;
  1046. begin
  1047. inherited derefimpl;
  1048. rttidef:=tstoreddef(rttidefderef.resolve);
  1049. end;
  1050. function trttinode.dogetcopy : tnode;
  1051. var
  1052. n : trttinode;
  1053. begin
  1054. n:=trttinode(inherited dogetcopy);
  1055. n.rttidef:=rttidef;
  1056. n.rttitype:=rttitype;
  1057. result:=n;
  1058. end;
  1059. function trttinode.pass_typecheck:tnode;
  1060. begin
  1061. { rtti information will be returned as a void pointer }
  1062. result:=nil;
  1063. resultdef:=voidpointertype;
  1064. end;
  1065. function trttinode.pass_1 : tnode;
  1066. begin
  1067. result:=nil;
  1068. expectloc:=LOC_CREFERENCE;
  1069. end;
  1070. function trttinode.docompare(p: tnode): boolean;
  1071. begin
  1072. docompare :=
  1073. inherited docompare(p) and
  1074. (rttidef = trttinode(p).rttidef) and
  1075. (rttitype = trttinode(p).rttitype);
  1076. end;
  1077. begin
  1078. cloadnode:=tloadnode;
  1079. cassignmentnode:=tassignmentnode;
  1080. carrayconstructorrangenode:=tarrayconstructorrangenode;
  1081. carrayconstructornode:=tarrayconstructornode;
  1082. ctypenode:=ttypenode;
  1083. crttinode:=trttinode;
  1084. end.