nld.pas 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  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. if r.nodetype = typeconvn then
  406. ttypeconvnode(r).warn_pointer_to_signed:=false;
  407. end;
  408. constructor tassignmentnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  409. begin
  410. inherited ppuload(t,ppufile);
  411. assigntype:=tassigntype(ppufile.getbyte);
  412. end;
  413. procedure tassignmentnode.ppuwrite(ppufile:tcompilerppufile);
  414. begin
  415. inherited ppuwrite(ppufile);
  416. ppufile.putbyte(byte(assigntype));
  417. end;
  418. function tassignmentnode.dogetcopy : tnode;
  419. var
  420. n : tassignmentnode;
  421. begin
  422. n:=tassignmentnode(inherited dogetcopy);
  423. n.assigntype:=assigntype;
  424. result:=n;
  425. end;
  426. function tassignmentnode.simplify : tnode;
  427. begin
  428. result:=nil;
  429. { assignment nodes can perform several floating point }
  430. { type conversions directly, so no typeconversions }
  431. { are inserted in those cases. When inlining, a }
  432. { variable may be replaced by a constant which can be }
  433. { converted at compile time, so check for this case }
  434. if is_real(left.resultdef) and
  435. is_real(right.resultdef) and
  436. is_constrealnode(right) and
  437. not equal_defs(right.resultdef,left.resultdef) then
  438. inserttypeconv(right,left.resultdef);
  439. end;
  440. function tassignmentnode.pass_typecheck:tnode;
  441. var
  442. hp : tnode;
  443. useshelper : boolean;
  444. begin
  445. result:=nil;
  446. resultdef:=voidtype;
  447. { must be made unique }
  448. set_unique(left);
  449. typecheckpass(left);
  450. typecheckpass(right);
  451. set_varstate(right,vs_read,[vsf_must_be_valid]);
  452. set_varstate(left,vs_written,[]);
  453. if codegenerror then
  454. exit;
  455. { tp procvar support, when we don't expect a procvar
  456. then we need to call the procvar }
  457. if (left.resultdef.typ<>procvardef) then
  458. maybe_call_procvar(right,true);
  459. { assignments to formaldefs and open arrays aren't allowed }
  460. if (left.resultdef.typ=formaldef) or
  461. is_open_array(left.resultdef) then
  462. CGMessage(type_e_assignment_not_allowed);
  463. { test if node can be assigned, properties are allowed }
  464. valid_for_assignment(left,true);
  465. { assigning nil to a dynamic array clears the array }
  466. if is_dynamic_array(left.resultdef) and
  467. (right.nodetype=niln) then
  468. begin
  469. hp:=ccallparanode.create(caddrnode.create_internal
  470. (crttinode.create(tstoreddef(left.resultdef),initrtti)),
  471. ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil));
  472. result := ccallnode.createintern('fpc_dynarray_clear',hp);
  473. left:=nil;
  474. exit;
  475. end;
  476. { shortstring helpers can do the conversion directly,
  477. so treat them separatly }
  478. if (is_shortstring(left.resultdef)) then
  479. begin
  480. { insert typeconv, except for chars that are handled in
  481. secondpass and except for ansi/wide string that can
  482. be converted immediatly }
  483. if not(is_char(right.resultdef) or
  484. (right.resultdef.typ=stringdef)) then
  485. inserttypeconv(right,left.resultdef);
  486. if right.resultdef.typ=stringdef then
  487. begin
  488. useshelper:=true;
  489. { convert constant strings to shortstrings. But
  490. skip empty constant strings, that will be handled
  491. in secondpass }
  492. if (right.nodetype=stringconstn) then
  493. begin
  494. { verify if range fits within shortstring }
  495. { just emit a warning, delphi gives an }
  496. { error, only if the type definition of }
  497. { of the string is less < 255 characters }
  498. if not is_open_string(left.resultdef) and
  499. (tstringconstnode(right).len > tstringdef(left.resultdef).len) then
  500. cgmessage(type_w_string_too_long);
  501. inserttypeconv(right,left.resultdef);
  502. if (right.nodetype=stringconstn) and
  503. (tstringconstnode(right).len=0) then
  504. useshelper:=false;
  505. end;
  506. { rest is done in pass 1 (JM) }
  507. if useshelper then
  508. exit;
  509. end
  510. end
  511. { floating point assignments can also perform the conversion directly }
  512. else if is_real(left.resultdef) and is_real(right.resultdef) and
  513. not is_constrealnode(right)
  514. {$ifdef cpufpemu}
  515. { the emulator can't do this obviously }
  516. and not(current_settings.fputype in [fpu_libgcc,fpu_soft])
  517. {$endif cpufpemu}
  518. {$ifdef x86}
  519. { the assignment node code can't convert a double in an }
  520. { sse register to an extended value in memory more }
  521. { efficiently than a type conversion node, so don't }
  522. { bother implementing support for that }
  523. and (use_sse(left.resultdef) or not(use_sse(right.resultdef)))
  524. {$endif}
  525. then
  526. begin
  527. check_ranges(fileinfo,right,left.resultdef);
  528. end
  529. else
  530. begin
  531. { check if the assignment may cause a range check error }
  532. check_ranges(fileinfo,right,left.resultdef);
  533. inserttypeconv(right,left.resultdef);
  534. end;
  535. { call helpers for interface }
  536. if is_interfacecom(left.resultdef) then
  537. begin
  538. { Normal interface assignments are handled by the generic refcount incr/decr }
  539. if not right.resultdef.is_related(left.resultdef) then
  540. begin
  541. { remove property flag to avoid errors, see comments for }
  542. { tf_winlikewidestring assignments below }
  543. exclude(left.flags,nf_isproperty);
  544. hp:=
  545. ccallparanode.create(
  546. cguidconstnode.create(tobjectdef(left.resultdef).iidguid^),
  547. ccallparanode.create(
  548. ctypeconvnode.create_internal(right,voidpointertype),
  549. ccallparanode.create(
  550. ctypeconvnode.create_internal(left,voidpointertype),
  551. nil)));
  552. result:=ccallnode.createintern('fpc_intf_assign_by_iid',hp);
  553. left:=nil;
  554. right:=nil;
  555. exit;
  556. end;
  557. end;
  558. { check if local proc/func is assigned to procvar }
  559. if right.resultdef.typ=procvardef then
  560. test_local_to_procvar(tprocvardef(right.resultdef),left.resultdef);
  561. end;
  562. function tassignmentnode.pass_1 : tnode;
  563. var
  564. hp: tnode;
  565. oldassignmentnode : tassignmentnode;
  566. begin
  567. result:=nil;
  568. expectloc:=LOC_VOID;
  569. firstpass(left);
  570. { Optimize the reuse of the destination of the assingment in left.
  571. Allow the use of the left inside the tree generated on the right.
  572. This is especially usefull for string routines where the destination
  573. is pushed as a parameter. Using the final destination of left directly
  574. save a temp allocation and copy of data (PFV) }
  575. oldassignmentnode:=aktassignmentnode;
  576. if right.nodetype=addn then
  577. aktassignmentnode:=self
  578. else
  579. aktassignmentnode:=nil;
  580. firstpass(right);
  581. aktassignmentnode:=oldassignmentnode;
  582. if nf_assign_done_in_right in flags then
  583. begin
  584. result:=right;
  585. right:=nil;
  586. exit;
  587. end;
  588. if codegenerror then
  589. exit;
  590. { if right is a function call for which the address of the result }
  591. { is allocated by the caller and passed to the function via an }
  592. { invisible function result, try to pass the x in "x:=f(...)" as }
  593. { that function result instead. Condition: x cannot be accessible }
  594. { from within f. This is the case if x is a temp, or x is a local }
  595. { variable or value parameter of the current block and its address }
  596. { is not passed to f. One problem: what if someone takes the }
  597. { address of x, puts it in a pointer variable/field and then }
  598. { accesses it that way from within the function? This is solved }
  599. { (in a conservative way) using the ti_addr_taken/addr_taken flags }
  600. if (cs_opt_level1 in current_settings.optimizerswitches) and
  601. (right.nodetype = calln) and
  602. (right.resultdef=left.resultdef) and
  603. { left must be a temp, since otherwise as soon as you modify the }
  604. { result, the current left node is modified and that one may }
  605. { still be an argument to the function or even accessed in the }
  606. { function }
  607. (
  608. (
  609. (((left.nodetype = temprefn) and
  610. not(ti_addr_taken in ttemprefnode(left).tempinfo^.flags) and
  611. not(ti_may_be_in_reg in ttemprefnode(left).tempinfo^.flags)) or
  612. ((left.nodetype = loadn) and
  613. { nested procedures may access the current procedure's locals }
  614. (tcallnode(right).procdefinition.parast.symtablelevel=normal_function_level) and
  615. { must be a local variable or a value para }
  616. ((tloadnode(left).symtableentry.typ = localvarsym) or
  617. ((tloadnode(left).symtableentry.typ = paravarsym) and
  618. (tparavarsym(tloadnode(left).symtableentry).varspez = vs_value)
  619. )
  620. ) and
  621. { the address may not have been taken of the variable/parameter, because }
  622. { otherwise it's possible that the called function can access it via a }
  623. { global variable or other stored state }
  624. not(tabstractvarsym(tloadnode(left).symtableentry).addr_taken) and
  625. (tabstractvarsym(tloadnode(left).symtableentry).varregable in [vr_none,vr_addr])
  626. )
  627. ) and
  628. paramanager.ret_in_param(right.resultdef,tcallnode(right).procdefinition.proccalloption)
  629. ) or
  630. { there's special support for ansi/widestrings in the callnode }
  631. is_ansistring(right.resultdef) or
  632. is_widestring(right.resultdef)
  633. ) then
  634. begin
  635. if assigned(tcallnode(right).funcretnode) then
  636. internalerror(2007080201);
  637. tcallnode(right).funcretnode := left;
  638. result := right;
  639. left := nil;
  640. right := nil;
  641. exit;
  642. end;
  643. { assignment to refcounted variable -> inc/decref }
  644. if (not is_class(left.resultdef) and
  645. left.resultdef.needs_inittable) then
  646. include(current_procinfo.flags,pi_do_call);
  647. if (is_shortstring(left.resultdef)) then
  648. begin
  649. if right.resultdef.typ=stringdef then
  650. begin
  651. if (right.nodetype<>stringconstn) or
  652. (tstringconstnode(right).len<>0) then
  653. begin
  654. hp:=ccallparanode.create
  655. (right,
  656. ccallparanode.create(left,nil));
  657. result:=ccallnode.createintern('fpc_'+tstringdef(right.resultdef).stringtypname+'_to_shortstr',hp);
  658. firstpass(result);
  659. left:=nil;
  660. right:=nil;
  661. exit;
  662. end;
  663. end;
  664. end
  665. { call helpers for composite types containing automated types }
  666. else if (left.resultdef.needs_inittable) and
  667. (left.resultdef.typ in [arraydef,objectdef,recorddef]) and
  668. not is_interfacecom(left.resultdef) and
  669. not is_dynamic_array(left.resultdef) then
  670. begin
  671. hp:=ccallparanode.create(caddrnode.create_internal(
  672. crttinode.create(tstoreddef(left.resultdef),initrtti)),
  673. ccallparanode.create(ctypeconvnode.create_internal(
  674. caddrnode.create_internal(left),voidpointertype),
  675. ccallparanode.create(ctypeconvnode.create_internal(
  676. caddrnode.create_internal(right),voidpointertype),
  677. nil)));
  678. result:=ccallnode.createintern('fpc_copy_proc',hp);
  679. firstpass(result);
  680. left:=nil;
  681. right:=nil;
  682. exit;
  683. end
  684. { call helpers for variant, they can contain non ref. counted types like
  685. vararrays which must be really copied }
  686. else if left.resultdef.typ=variantdef then
  687. begin
  688. hp:=ccallparanode.create(ctypeconvnode.create_internal(
  689. caddrnode.create_internal(right),voidpointertype),
  690. ccallparanode.create(ctypeconvnode.create_internal(
  691. caddrnode.create_internal(left),voidpointertype),
  692. nil));
  693. result:=ccallnode.createintern('fpc_variant_copy',hp);
  694. firstpass(result);
  695. left:=nil;
  696. right:=nil;
  697. exit;
  698. end
  699. { call helpers for windows widestrings, they aren't ref. counted }
  700. else if (tf_winlikewidestring in target_info.flags) and is_widestring(left.resultdef) then
  701. begin
  702. { The first argument of fpc_widestr_assign is a var parameter. Properties cannot }
  703. { be passed to var or out parameters, because in that case setters/getters are not }
  704. { used. Further, if we would allow it in case there are no getters or setters, you }
  705. { would need source changes in case these are introduced later on, thus defeating }
  706. { part of the transparency advantages of properties. In this particular case, }
  707. { however: }
  708. { a) if there is a setter, this code will not be used since then the assignment }
  709. { will be converted to a procedure call }
  710. { b) the getter is irrelevant, because fpc_widestr_assign must always decrease }
  711. { the refcount of the field to which we are writing }
  712. { c) source code changes are not required if a setter is added/removed, because }
  713. { this transformation is handled at compile time }
  714. { -> we can remove the nf_isproperty flag (if any) from left, so that in case it }
  715. { is a property which refers to a field without a setter call, we will not get }
  716. { an error about trying to pass a property as a var parameter }
  717. exclude(left.flags,nf_isproperty);
  718. hp:=ccallparanode.create(ctypeconvnode.create_internal(right,voidpointertype),
  719. ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),
  720. nil));
  721. result:=ccallnode.createintern('fpc_widestr_assign',hp);
  722. firstpass(result);
  723. left:=nil;
  724. right:=nil;
  725. exit;
  726. end;
  727. registersint:=left.registersint+right.registersint;
  728. registersfpu:=max(left.registersfpu,right.registersfpu);
  729. {$ifdef SUPPORT_MMX}
  730. registersmmx:=max(left.registersmmx,right.registersmmx);
  731. {$endif SUPPORT_MMX}
  732. end;
  733. function tassignmentnode.docompare(p: tnode): boolean;
  734. begin
  735. docompare :=
  736. inherited docompare(p) and
  737. (assigntype = tassignmentnode(p).assigntype);
  738. end;
  739. {$ifdef state_tracking}
  740. function Tassignmentnode.track_state_pass(exec_known:boolean):boolean;
  741. var se:Tstate_entry;
  742. begin
  743. track_state_pass:=false;
  744. if exec_known then
  745. begin
  746. track_state_pass:=right.track_state_pass(exec_known);
  747. {Force a new resultdef pass.}
  748. right.resultdef:=nil;
  749. do_typecheckpass(right);
  750. typecheckpass(right);
  751. aktstate.store_fact(left.getcopy,right.getcopy);
  752. end
  753. else
  754. aktstate.delete_fact(left);
  755. end;
  756. {$endif}
  757. {*****************************************************************************
  758. TARRAYCONSTRUCTORRANGENODE
  759. *****************************************************************************}
  760. constructor tarrayconstructorrangenode.create(l,r : tnode);
  761. begin
  762. inherited create(arrayconstructorrangen,l,r);
  763. end;
  764. function tarrayconstructorrangenode.pass_typecheck:tnode;
  765. begin
  766. result:=nil;
  767. typecheckpass(left);
  768. typecheckpass(right);
  769. set_varstate(left,vs_read,[vsf_must_be_valid]);
  770. set_varstate(right,vs_read,[vsf_must_be_valid]);
  771. if codegenerror then
  772. exit;
  773. resultdef:=left.resultdef;
  774. end;
  775. function tarrayconstructorrangenode.pass_1 : tnode;
  776. begin
  777. firstpass(left);
  778. firstpass(right);
  779. expectloc:=LOC_CREFERENCE;
  780. calcregisters(self,0,0,0);
  781. result:=nil;
  782. end;
  783. {****************************************************************************
  784. TARRAYCONSTRUCTORNODE
  785. *****************************************************************************}
  786. constructor tarrayconstructornode.create(l,r : tnode);
  787. begin
  788. inherited create(arrayconstructorn,l,r);
  789. end;
  790. function tarrayconstructornode.dogetcopy : tnode;
  791. var
  792. n : tarrayconstructornode;
  793. begin
  794. n:=tarrayconstructornode(inherited dogetcopy);
  795. result:=n;
  796. end;
  797. function tarrayconstructornode.pass_typecheck:tnode;
  798. var
  799. hdef : tdef;
  800. hp : tarrayconstructornode;
  801. len : longint;
  802. varia : boolean;
  803. eq : tequaltype;
  804. hnodetype : tnodetype;
  805. begin
  806. result:=nil;
  807. { are we allowing array constructor? Then convert it to a set.
  808. Do this only if we didn't convert the arrayconstructor yet. This
  809. is needed for the cases where the resultdef is forced for a second
  810. run }
  811. if (not allow_array_constructor) then
  812. begin
  813. hp:=tarrayconstructornode(getcopy);
  814. arrayconstructor_to_set(tnode(hp));
  815. result:=hp;
  816. exit;
  817. end;
  818. { only pass left tree, right tree contains next construct if any }
  819. hdef:=nil;
  820. hnodetype:=errorn;
  821. len:=0;
  822. varia:=false;
  823. if assigned(left) then
  824. begin
  825. hp:=self;
  826. while assigned(hp) do
  827. begin
  828. typecheckpass(hp.left);
  829. set_varstate(hp.left,vs_read,[vsf_must_be_valid]);
  830. if (hdef=nil) then
  831. begin
  832. hdef:=hp.left.resultdef;
  833. hnodetype:=hp.left.nodetype;
  834. end
  835. else
  836. begin
  837. { If we got a niln we don't know the type yet and need to take the
  838. type of the next array element.
  839. This is to handle things like [nil,tclass,tclass], see also tw8371 (PFV) }
  840. if hnodetype=niln then
  841. begin
  842. eq:=compare_defs(hp.left.resultdef,hdef,hnodetype);
  843. if eq>te_incompatible then
  844. begin
  845. hdef:=hp.left.resultdef;
  846. hnodetype:=hp.left.nodetype;
  847. end;
  848. end
  849. else
  850. eq:=compare_defs(hdef,hp.left.resultdef,hp.left.nodetype);
  851. if (not varia) and (eq<te_equal) then
  852. begin
  853. { If both are integers we need to take the type that can hold both
  854. defs }
  855. if is_integer(hdef) and is_integer(hp.left.resultdef) then
  856. begin
  857. if is_in_limit(hdef,hp.left.resultdef) then
  858. hdef:=hp.left.resultdef;
  859. end
  860. else
  861. if (nf_novariaallowed in flags) then
  862. varia:=true;
  863. end;
  864. end;
  865. inc(len);
  866. hp:=tarrayconstructornode(hp.right);
  867. end;
  868. end;
  869. { Set the type of empty or varia arrays to void. Also
  870. do this if the type is array of const/open array
  871. because those can't be used with setelementdef }
  872. if not assigned(hdef) or
  873. varia or
  874. is_array_of_const(hdef) or
  875. is_open_array(hdef) then
  876. hdef:=voidtype;
  877. resultdef:=tarraydef.create(0,len-1,s32inttype);
  878. tarraydef(resultdef).elementdef:=hdef;
  879. include(tarraydef(resultdef).arrayoptions,ado_IsConstructor);
  880. if varia then
  881. include(tarraydef(resultdef).arrayoptions,ado_IsVariant);
  882. end;
  883. procedure tarrayconstructornode.force_type(def:tdef);
  884. var
  885. hp : tarrayconstructornode;
  886. begin
  887. tarraydef(resultdef).elementdef:=def;
  888. include(tarraydef(resultdef).arrayoptions,ado_IsConstructor);
  889. exclude(tarraydef(resultdef).arrayoptions,ado_IsVariant);
  890. if assigned(left) then
  891. begin
  892. hp:=self;
  893. while assigned(hp) do
  894. begin
  895. inserttypeconv(hp.left,def);
  896. hp:=tarrayconstructornode(hp.right);
  897. end;
  898. end;
  899. end;
  900. procedure tarrayconstructornode.insert_typeconvs;
  901. var
  902. hp : tarrayconstructornode;
  903. dovariant : boolean;
  904. begin
  905. dovariant:=(nf_forcevaria in flags) or (ado_isvariant in tarraydef(resultdef).arrayoptions);
  906. { only pass left tree, right tree contains next construct if any }
  907. if assigned(left) then
  908. begin
  909. hp:=self;
  910. while assigned(hp) do
  911. begin
  912. typecheckpass(hp.left);
  913. { Insert typeconvs for array of const }
  914. if dovariant then
  915. { at this time C varargs are no longer an arrayconstructornode }
  916. insert_varargstypeconv(hp.left,false);
  917. hp:=tarrayconstructornode(hp.right);
  918. end;
  919. end;
  920. end;
  921. function tarrayconstructornode.pass_1 : tnode;
  922. var
  923. hp : tarrayconstructornode;
  924. do_variant:boolean;
  925. begin
  926. do_variant:=(nf_forcevaria in flags) or (ado_isvariant in tarraydef(resultdef).arrayoptions);
  927. result:=nil;
  928. { Insert required type convs, this must be
  929. done in pass 1, because the call must be
  930. typecheckpassed already }
  931. if assigned(left) then
  932. begin
  933. insert_typeconvs;
  934. { call firstpass for all nodes }
  935. hp:=self;
  936. while assigned(hp) do
  937. begin
  938. if hp.left<>nil then
  939. begin
  940. {This check is pessimistic; a call will happen depending
  941. on the location in which the elements will be found in
  942. pass 2.}
  943. if not do_variant then
  944. include(current_procinfo.flags,pi_do_call);
  945. firstpass(hp.left);
  946. end;
  947. hp:=tarrayconstructornode(hp.right);
  948. end;
  949. end;
  950. expectloc:=LOC_CREFERENCE;
  951. calcregisters(self,0,0,0);
  952. end;
  953. function tarrayconstructornode.docompare(p: tnode): boolean;
  954. begin
  955. docompare:=inherited docompare(p);
  956. end;
  957. {*****************************************************************************
  958. TTYPENODE
  959. *****************************************************************************}
  960. constructor ttypenode.create(def:tdef);
  961. begin
  962. inherited create(typen);
  963. typedef:=def;
  964. allowed:=false;
  965. end;
  966. constructor ttypenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  967. begin
  968. inherited ppuload(t,ppufile);
  969. ppufile.getderef(typedefderef);
  970. allowed:=boolean(ppufile.getbyte);
  971. end;
  972. procedure ttypenode.ppuwrite(ppufile:tcompilerppufile);
  973. begin
  974. inherited ppuwrite(ppufile);
  975. ppufile.putderef(typedefderef);
  976. ppufile.putbyte(byte(allowed));
  977. end;
  978. procedure ttypenode.buildderefimpl;
  979. begin
  980. inherited buildderefimpl;
  981. typedefderef.build(typedef);
  982. end;
  983. procedure ttypenode.derefimpl;
  984. begin
  985. inherited derefimpl;
  986. typedef:=tdef(typedefderef.resolve);
  987. end;
  988. function ttypenode.pass_typecheck:tnode;
  989. begin
  990. result:=nil;
  991. resultdef:=typedef;
  992. { check if it's valid }
  993. if typedef.typ = errordef then
  994. CGMessage(parser_e_illegal_expression);
  995. end;
  996. function ttypenode.pass_1 : tnode;
  997. begin
  998. result:=nil;
  999. expectloc:=LOC_VOID;
  1000. { a typenode can't generate code, so we give here
  1001. an error. Else it'll be an abstract error in pass_generate_code.
  1002. Only when the allowed flag is set we don't generate
  1003. an error }
  1004. if not allowed then
  1005. Message(parser_e_no_type_not_allowed_here);
  1006. end;
  1007. function ttypenode.dogetcopy : tnode;
  1008. var
  1009. n : ttypenode;
  1010. begin
  1011. n:=ttypenode(inherited dogetcopy);
  1012. n.allowed:=allowed;
  1013. n.typedef:=typedef;
  1014. result:=n;
  1015. end;
  1016. function ttypenode.docompare(p: tnode): boolean;
  1017. begin
  1018. docompare :=
  1019. inherited docompare(p);
  1020. end;
  1021. {*****************************************************************************
  1022. TRTTINODE
  1023. *****************************************************************************}
  1024. constructor trttinode.create(def:tstoreddef;rt:trttitype);
  1025. begin
  1026. inherited create(rttin);
  1027. rttidef:=def;
  1028. rttitype:=rt;
  1029. end;
  1030. constructor trttinode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1031. begin
  1032. inherited ppuload(t,ppufile);
  1033. ppufile.getderef(rttidefderef);
  1034. rttitype:=trttitype(ppufile.getbyte);
  1035. end;
  1036. procedure trttinode.ppuwrite(ppufile:tcompilerppufile);
  1037. begin
  1038. inherited ppuwrite(ppufile);
  1039. ppufile.putderef(rttidefderef);
  1040. ppufile.putbyte(byte(rttitype));
  1041. end;
  1042. procedure trttinode.buildderefimpl;
  1043. begin
  1044. inherited buildderefimpl;
  1045. rttidefderef.build(rttidef);
  1046. end;
  1047. procedure trttinode.derefimpl;
  1048. begin
  1049. inherited derefimpl;
  1050. rttidef:=tstoreddef(rttidefderef.resolve);
  1051. end;
  1052. function trttinode.dogetcopy : tnode;
  1053. var
  1054. n : trttinode;
  1055. begin
  1056. n:=trttinode(inherited dogetcopy);
  1057. n.rttidef:=rttidef;
  1058. n.rttitype:=rttitype;
  1059. result:=n;
  1060. end;
  1061. function trttinode.pass_typecheck:tnode;
  1062. begin
  1063. { rtti information will be returned as a void pointer }
  1064. result:=nil;
  1065. resultdef:=voidpointertype;
  1066. end;
  1067. function trttinode.pass_1 : tnode;
  1068. begin
  1069. result:=nil;
  1070. expectloc:=LOC_CREFERENCE;
  1071. end;
  1072. function trttinode.docompare(p: tnode): boolean;
  1073. begin
  1074. docompare :=
  1075. inherited docompare(p) and
  1076. (rttidef = trttinode(p).rttidef) and
  1077. (rttitype = trttinode(p).rttitype);
  1078. end;
  1079. begin
  1080. cloadnode:=tloadnode;
  1081. cassignmentnode:=tassignmentnode;
  1082. carrayconstructorrangenode:=tarrayconstructorrangenode;
  1083. carrayconstructornode:=tarrayconstructornode;
  1084. ctypenode:=ttypenode;
  1085. crttinode:=trttinode;
  1086. end.