nmem.pas 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. Type checking and register allocation for memory related 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 nmem;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,
  22. symdef,symsym,symtable,symtype;
  23. type
  24. tloadvmtaddrnode = class(tunarynode)
  25. { unless this is for a call, we have to send the "class" message to
  26. the objctype because the type information only gets initialized
  27. after the first message has been sent -> crash if you pass an
  28. uninitialized type to e.g. class_getInstanceSize() or so. No need
  29. to save to/restore from ppu. }
  30. forcall: boolean;
  31. constructor create(l : tnode);virtual;
  32. function pass_1 : tnode;override;
  33. function pass_typecheck:tnode;override;
  34. function docompare(p: tnode): boolean; override;
  35. function dogetcopy: tnode; override;
  36. end;
  37. tloadvmtaddrnodeclass = class of tloadvmtaddrnode;
  38. tloadparentfpnode = class(tunarynode)
  39. parentpd : tprocdef;
  40. parentpdderef : tderef;
  41. constructor create(pd:tprocdef);virtual;
  42. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  43. procedure ppuwrite(ppufile:tcompilerppufile);override;
  44. procedure buildderefimpl;override;
  45. procedure derefimpl;override;
  46. function pass_1 : tnode;override;
  47. function pass_typecheck:tnode;override;
  48. function docompare(p: tnode): boolean; override;
  49. function dogetcopy : tnode;override;
  50. end;
  51. tloadparentfpnodeclass = class of tloadparentfpnode;
  52. taddrnode = class(tunarynode)
  53. getprocvardef : tprocvardef;
  54. getprocvardefderef : tderef;
  55. constructor create(l : tnode);virtual;
  56. constructor create_internal(l : tnode); virtual;
  57. constructor create_internal_nomark(l : tnode); virtual;
  58. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  59. procedure ppuwrite(ppufile:tcompilerppufile);override;
  60. procedure mark_write;override;
  61. procedure buildderefimpl;override;
  62. procedure derefimpl;override;
  63. function docompare(p: tnode): boolean; override;
  64. function dogetcopy : tnode;override;
  65. function pass_1 : tnode;override;
  66. function pass_typecheck:tnode;override;
  67. protected
  68. mark_read_written: boolean;
  69. end;
  70. taddrnodeclass = class of taddrnode;
  71. tderefnode = class(tunarynode)
  72. constructor create(l : tnode);virtual;
  73. function pass_1 : tnode;override;
  74. function pass_typecheck:tnode;override;
  75. procedure mark_write;override;
  76. end;
  77. tderefnodeclass = class of tderefnode;
  78. tsubscriptnode = class(tunarynode)
  79. vs : tfieldvarsym;
  80. vsderef : tderef;
  81. constructor create(varsym : tsym;l : tnode);virtual;
  82. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  83. procedure ppuwrite(ppufile:tcompilerppufile);override;
  84. procedure buildderefimpl;override;
  85. procedure derefimpl;override;
  86. function dogetcopy : tnode;override;
  87. function pass_1 : tnode;override;
  88. function docompare(p: tnode): boolean; override;
  89. function pass_typecheck:tnode;override;
  90. procedure mark_write;override;
  91. end;
  92. tsubscriptnodeclass = class of tsubscriptnode;
  93. tvecnode = class(tbinarynode)
  94. constructor create(l,r : tnode);virtual;
  95. function pass_1 : tnode;override;
  96. function pass_typecheck:tnode;override;
  97. procedure mark_write;override;
  98. end;
  99. tvecnodeclass = class of tvecnode;
  100. twithnode = class(tunarynode)
  101. constructor create(l:tnode);
  102. destructor destroy;override;
  103. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  104. procedure ppuwrite(ppufile:tcompilerppufile);override;
  105. function dogetcopy : tnode;override;
  106. function pass_1 : tnode;override;
  107. function docompare(p: tnode): boolean; override;
  108. function pass_typecheck:tnode;override;
  109. end;
  110. twithnodeclass = class of twithnode;
  111. var
  112. cloadvmtaddrnode : tloadvmtaddrnodeclass= tloadvmtaddrnode;
  113. caddrnode : taddrnodeclass= taddrnode;
  114. cderefnode : tderefnodeclass= tderefnode;
  115. csubscriptnode : tsubscriptnodeclass= tsubscriptnode;
  116. cvecnode : tvecnodeclass= tvecnode;
  117. cwithnode : twithnodeclass= twithnode;
  118. cloadparentfpnode : tloadparentfpnodeclass = tloadparentfpnode;
  119. function is_big_untyped_addrnode(p: tnode): boolean;
  120. implementation
  121. uses
  122. globtype,systems,constexp,
  123. cutils,verbose,globals,
  124. symconst,symbase,defutil,defcmp,
  125. nbas,nutils,
  126. wpobase,
  127. htypechk,pass_1,ncal,nld,ncon,ncnv,cgbase,procinfo
  128. ;
  129. {*****************************************************************************
  130. TLOADVMTADDRNODE
  131. *****************************************************************************}
  132. constructor tloadvmtaddrnode.create(l : tnode);
  133. begin
  134. inherited create(loadvmtaddrn,l);
  135. end;
  136. function tloadvmtaddrnode.pass_typecheck:tnode;
  137. var
  138. defaultresultdef : boolean;
  139. begin
  140. result:=nil;
  141. typecheckpass(left);
  142. if codegenerror then
  143. exit;
  144. case left.resultdef.typ of
  145. classrefdef :
  146. resultdef:=left.resultdef;
  147. objectdef,
  148. recorddef:
  149. { access to the classtype while specializing? }
  150. if (df_generic in left.resultdef.defoptions) then
  151. begin
  152. defaultresultdef:=true;
  153. if assigned(current_structdef) then
  154. begin
  155. if assigned(current_structdef.genericdef) then
  156. if current_structdef.genericdef=left.resultdef then
  157. begin
  158. resultdef:=tclassrefdef.create(current_structdef);
  159. defaultresultdef:=false;
  160. end
  161. else
  162. CGMessage(parser_e_cant_create_generics_of_this_type);
  163. end
  164. else
  165. message(parser_e_cant_create_generics_of_this_type);
  166. if defaultresultdef then
  167. resultdef:=tclassrefdef.create(left.resultdef);
  168. end
  169. else
  170. resultdef:=tclassrefdef.create(left.resultdef);
  171. else
  172. CGMessage(parser_e_pointer_to_class_expected);
  173. end;
  174. end;
  175. function tloadvmtaddrnode.docompare(p: tnode): boolean;
  176. begin
  177. result:=inherited docompare(p);
  178. if result then
  179. result:=forcall=tloadvmtaddrnode(p).forcall;
  180. end;
  181. function tloadvmtaddrnode.dogetcopy: tnode;
  182. begin
  183. result:=inherited dogetcopy;
  184. tloadvmtaddrnode(result).forcall:=forcall;
  185. end;
  186. function tloadvmtaddrnode.pass_1 : tnode;
  187. var
  188. vs: tsym;
  189. begin
  190. result:=nil;
  191. expectloc:=LOC_REGISTER;
  192. if left.nodetype<>typen then
  193. begin
  194. { make sure that the isa field is loaded correctly in case
  195. of the non-fragile ABI }
  196. if is_objcclass(left.resultdef) and
  197. (left.nodetype<>typen) then
  198. begin
  199. vs:=search_struct_member(tobjectdef(left.resultdef),'ISA');
  200. if not assigned(vs) or
  201. (tsym(vs).typ<>fieldvarsym) then
  202. internalerror(2009092502);
  203. result:=csubscriptnode.create(tfieldvarsym(vs),left);
  204. inserttypeconv_internal(result,resultdef);
  205. { reused }
  206. left:=nil;
  207. end
  208. else if is_javaclass(left.resultdef) and
  209. (left.nodetype<>typen) and
  210. (left.resultdef.typ<>classrefdef) then
  211. begin
  212. { call java.lang.Object.getClass() }
  213. vs:=search_struct_member(tobjectdef(left.resultdef),'GETCLASS');
  214. if not assigned(vs) or
  215. (tsym(vs).typ<>procsym) then
  216. internalerror(2011041901);
  217. result:=ccallnode.create(nil,tprocsym(vs),vs.owner,left,[]);
  218. inserttypeconv_explicit(result,resultdef);
  219. { reused }
  220. left:=nil;
  221. end
  222. else
  223. firstpass(left)
  224. end
  225. else if not is_objcclass(left.resultdef) and
  226. not is_objcclassref(left.resultdef) and
  227. not is_javaclass(left.resultdef) and
  228. not is_javaclassref(left.resultdef) then
  229. begin
  230. if not(nf_ignore_for_wpo in flags) and
  231. (not assigned(current_procinfo) or
  232. (po_inline in current_procinfo.procdef.procoptions) or
  233. wpoinfomanager.symbol_live(current_procinfo.procdef.mangledname)) then
  234. begin
  235. { keep track of which classes might be instantiated via a classrefdef }
  236. if (left.resultdef.typ=classrefdef) then
  237. tobjectdef(tclassrefdef(left.resultdef).pointeddef).register_maybe_created_object_type
  238. else if (left.resultdef.typ=objectdef) then
  239. tobjectdef(left.resultdef).register_maybe_created_object_type
  240. end
  241. end
  242. else if is_objcclass(left.resultdef) and
  243. not(forcall) then
  244. begin
  245. { call "class" method (= "classclass" in FPC), because otherwise
  246. we may use the class information before it has been
  247. initialized }
  248. vs:=search_struct_member(tobjectdef(left.resultdef),'CLASSCLASS');
  249. if not assigned(vs) or
  250. (vs.typ<>procsym) then
  251. internalerror(2011080601);
  252. { can't reuse "self", because it will be freed when we return }
  253. result:=ccallnode.create(nil,tprocsym(vs),vs.owner,self.getcopy,[]);
  254. end;
  255. end;
  256. {*****************************************************************************
  257. TLOADPARENTFPNODE
  258. *****************************************************************************}
  259. constructor tloadparentfpnode.create(pd:tprocdef);
  260. begin
  261. inherited create(loadparentfpn,nil);
  262. if not assigned(pd) then
  263. internalerror(200309288);
  264. if (pd.parast.symtablelevel>current_procinfo.procdef.parast.symtablelevel) then
  265. internalerror(200309284);
  266. parentpd:=pd;
  267. end;
  268. constructor tloadparentfpnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  269. begin
  270. inherited ppuload(t,ppufile);
  271. ppufile.getderef(parentpdderef);
  272. end;
  273. procedure tloadparentfpnode.ppuwrite(ppufile:tcompilerppufile);
  274. begin
  275. inherited ppuwrite(ppufile);
  276. ppufile.putderef(parentpdderef);
  277. end;
  278. procedure tloadparentfpnode.buildderefimpl;
  279. begin
  280. inherited buildderefimpl;
  281. parentpdderef.build(parentpd);
  282. end;
  283. procedure tloadparentfpnode.derefimpl;
  284. begin
  285. inherited derefimpl;
  286. parentpd:=tprocdef(parentpdderef.resolve);
  287. end;
  288. function tloadparentfpnode.docompare(p: tnode): boolean;
  289. begin
  290. result:=
  291. inherited docompare(p) and
  292. (tloadparentfpnode(p).parentpd=parentpd);
  293. end;
  294. function tloadparentfpnode.dogetcopy : tnode;
  295. var
  296. p : tloadparentfpnode;
  297. begin
  298. p:=tloadparentfpnode(inherited dogetcopy);
  299. p.parentpd:=parentpd;
  300. dogetcopy:=p;
  301. end;
  302. function tloadparentfpnode.pass_typecheck:tnode;
  303. {$ifdef dummy}
  304. var
  305. currpi : tprocinfo;
  306. hsym : tparavarsym;
  307. {$endif dummy}
  308. begin
  309. result:=nil;
  310. resultdef:=voidpointertype;
  311. {$ifdef dummy}
  312. { currently parentfps are never loaded in registers (FK) }
  313. if (current_procinfo.procdef.parast.symtablelevel<>parentpd.parast.symtablelevel) then
  314. begin
  315. currpi:=current_procinfo;
  316. { walk parents }
  317. while (currpi.procdef.owner.symtablelevel>parentpd.parast.symtablelevel) do
  318. begin
  319. currpi:=currpi.parent;
  320. if not assigned(currpi) then
  321. internalerror(2005040602);
  322. hsym:=tparavarsym(currpi.procdef.parast.Find('parentfp'));
  323. if not assigned(hsym) then
  324. internalerror(2005040601);
  325. hsym.varregable:=vr_none;
  326. end;
  327. end;
  328. {$endif dummy}
  329. end;
  330. function tloadparentfpnode.pass_1 : tnode;
  331. begin
  332. result:=nil;
  333. expectloc:=LOC_REGISTER;
  334. end;
  335. {*****************************************************************************
  336. TADDRNODE
  337. *****************************************************************************}
  338. constructor taddrnode.create(l : tnode);
  339. begin
  340. inherited create(addrn,l);
  341. getprocvardef:=nil;
  342. mark_read_written := true;
  343. end;
  344. constructor taddrnode.create_internal(l : tnode);
  345. begin
  346. self.create(l);
  347. include(flags,nf_internal);
  348. end;
  349. constructor taddrnode.create_internal_nomark(l : tnode);
  350. begin
  351. self.create_internal(l);
  352. mark_read_written := false;
  353. end;
  354. constructor taddrnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  355. begin
  356. inherited ppuload(t,ppufile);
  357. ppufile.getderef(getprocvardefderef);
  358. end;
  359. procedure taddrnode.ppuwrite(ppufile:tcompilerppufile);
  360. begin
  361. inherited ppuwrite(ppufile);
  362. ppufile.putderef(getprocvardefderef);
  363. end;
  364. procedure Taddrnode.mark_write;
  365. begin
  366. {@procvar:=nil is legal in Delphi mode.}
  367. left.mark_write;
  368. end;
  369. procedure taddrnode.buildderefimpl;
  370. begin
  371. inherited buildderefimpl;
  372. getprocvardefderef.build(getprocvardef);
  373. end;
  374. procedure taddrnode.derefimpl;
  375. begin
  376. inherited derefimpl;
  377. getprocvardef:=tprocvardef(getprocvardefderef.resolve);
  378. end;
  379. function taddrnode.docompare(p: tnode): boolean;
  380. begin
  381. result:=
  382. inherited docompare(p) and
  383. (taddrnode(p).getprocvardef=getprocvardef);
  384. end;
  385. function taddrnode.dogetcopy : tnode;
  386. var
  387. p : taddrnode;
  388. begin
  389. p:=taddrnode(inherited dogetcopy);
  390. p.getprocvardef:=getprocvardef;
  391. dogetcopy:=p;
  392. end;
  393. function taddrnode.pass_typecheck:tnode;
  394. var
  395. hp : tnode;
  396. hsym : tfieldvarsym;
  397. isprocvar : boolean;
  398. begin
  399. result:=nil;
  400. typecheckpass(left);
  401. if codegenerror then
  402. exit;
  403. make_not_regable(left,[ra_addr_regable,ra_addr_taken]);
  404. { don't allow constants, for internal use we also
  405. allow taking the address of strings and sets }
  406. if is_constnode(left) and
  407. not(
  408. (nf_internal in flags) and
  409. (left.nodetype in [stringconstn,setconstn])
  410. ) then
  411. begin
  412. CGMessagePos(left.fileinfo,type_e_no_addr_of_constant);
  413. exit;
  414. end;
  415. { Handle @proc special, also @procvar in tp-mode needs
  416. special handling }
  417. if (left.resultdef.typ=procdef) or
  418. (
  419. { in case of nf_internal, follow the normal FPC semantics so that
  420. we can easily get the actual address of a procvar }
  421. not(nf_internal in flags) and
  422. (left.resultdef.typ=procvardef) and
  423. ((m_tp_procvar in current_settings.modeswitches) or
  424. (m_mac_procvar in current_settings.modeswitches))
  425. ) then
  426. begin
  427. isprocvar:=(left.resultdef.typ=procvardef);
  428. if not isprocvar then
  429. begin
  430. left:=ctypeconvnode.create_proc_to_procvar(left);
  431. left.fileinfo:=fileinfo;
  432. typecheckpass(left);
  433. end;
  434. { In tp procvar mode the result is always a voidpointer. Insert
  435. a typeconversion to voidpointer. For methodpointers we need
  436. to load the proc field }
  437. if (m_tp_procvar in current_settings.modeswitches) or
  438. (m_mac_procvar in current_settings.modeswitches) then
  439. begin
  440. if tabstractprocdef(left.resultdef).is_addressonly then
  441. begin
  442. result:=ctypeconvnode.create_internal(left,voidpointertype);
  443. include(result.flags,nf_load_procvar);
  444. left:=nil;
  445. end
  446. else
  447. begin
  448. { For procvars and for nested routines we need to return
  449. the proc field of the methodpointer }
  450. if isprocvar or
  451. is_nested_pd(tabstractprocdef(left.resultdef)) then
  452. begin
  453. { find proc field in methodpointer record }
  454. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  455. if not assigned(hsym) then
  456. internalerror(200412041);
  457. { Load tmehodpointer(left).proc }
  458. result:=csubscriptnode.create(
  459. hsym,
  460. ctypeconvnode.create_internal(left,methodpointertype));
  461. left:=nil;
  462. end
  463. else
  464. CGMessage(type_e_variable_id_expected);
  465. end;
  466. end
  467. else
  468. begin
  469. { Return the typeconvn only }
  470. result:=left;
  471. left:=nil;
  472. end;
  473. end
  474. else
  475. begin
  476. { what are we getting the address from an absolute sym? }
  477. hp:=left;
  478. while assigned(hp) and (hp.nodetype in [typeconvn,vecn,derefn,subscriptn]) do
  479. hp:=tunarynode(hp).left;
  480. if not assigned(hp) then
  481. internalerror(200412042);
  482. {$ifdef i386}
  483. if (hp.nodetype=loadn) and
  484. ((tloadnode(hp).symtableentry.typ=absolutevarsym) and
  485. tabsolutevarsym(tloadnode(hp).symtableentry).absseg) then
  486. begin
  487. if not(nf_typedaddr in flags) then
  488. resultdef:=voidfarpointertype
  489. else
  490. resultdef:=tpointerdef.createfar(left.resultdef);
  491. end
  492. else
  493. {$endif i386}
  494. if (hp.nodetype=loadn) and
  495. (tloadnode(hp).symtableentry.typ=absolutevarsym) and
  496. {$ifdef i386}
  497. not(tabsolutevarsym(tloadnode(hp).symtableentry).absseg) and
  498. {$endif i386}
  499. (tabsolutevarsym(tloadnode(hp).symtableentry).abstyp=toaddr) then
  500. begin
  501. if nf_typedaddr in flags then
  502. result:=cpointerconstnode.create(tabsolutevarsym(tloadnode(hp).symtableentry).addroffset,getpointerdef(left.resultdef))
  503. else
  504. result:=cpointerconstnode.create(tabsolutevarsym(tloadnode(hp).symtableentry).addroffset,voidpointertype);
  505. exit;
  506. end
  507. else if (nf_internal in flags) or
  508. valid_for_addr(left,true) then
  509. begin
  510. if not(nf_typedaddr in flags) then
  511. resultdef:=voidpointertype
  512. else
  513. resultdef:=getpointerdef(left.resultdef);
  514. end
  515. else
  516. CGMessage(type_e_variable_id_expected);
  517. end;
  518. if mark_read_written then
  519. begin
  520. { This is actually only "read", but treat it nevertheless as }
  521. { modified due to the possible use of pointers }
  522. { To avoid false positives regarding "uninitialised" }
  523. { warnings when using arrays, perform it in two steps }
  524. set_varstate(left,vs_written,[]);
  525. { vsf_must_be_valid so it doesn't get changed into }
  526. { vsf_referred_not_inited }
  527. set_varstate(left,vs_read,[vsf_must_be_valid]);
  528. end;
  529. end;
  530. function taddrnode.pass_1 : tnode;
  531. begin
  532. result:=nil;
  533. firstpass(left);
  534. if codegenerror then
  535. exit;
  536. { is this right for object of methods ?? }
  537. expectloc:=LOC_REGISTER;
  538. end;
  539. {*****************************************************************************
  540. TDEREFNODE
  541. *****************************************************************************}
  542. constructor tderefnode.create(l : tnode);
  543. begin
  544. inherited create(derefn,l);
  545. end;
  546. function tderefnode.pass_typecheck:tnode;
  547. begin
  548. result:=nil;
  549. typecheckpass(left);
  550. set_varstate(left,vs_read,[vsf_must_be_valid]);
  551. if codegenerror then
  552. exit;
  553. { tp procvar support }
  554. maybe_call_procvar(left,true);
  555. if left.resultdef.typ=pointerdef then
  556. resultdef:=tpointerdef(left.resultdef).pointeddef
  557. else
  558. CGMessage(parser_e_invalid_qualifier);
  559. end;
  560. procedure Tderefnode.mark_write;
  561. begin
  562. include(flags,nf_write);
  563. end;
  564. function tderefnode.pass_1 : tnode;
  565. begin
  566. result:=nil;
  567. firstpass(left);
  568. if codegenerror then
  569. exit;
  570. expectloc:=LOC_REFERENCE;
  571. end;
  572. {*****************************************************************************
  573. TSUBSCRIPTNODE
  574. *****************************************************************************}
  575. constructor tsubscriptnode.create(varsym : tsym;l : tnode);
  576. begin
  577. inherited create(subscriptn,l);
  578. { vs should be changed to tsym! }
  579. vs:=tfieldvarsym(varsym);
  580. end;
  581. constructor tsubscriptnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  582. begin
  583. inherited ppuload(t,ppufile);
  584. ppufile.getderef(vsderef);
  585. end;
  586. procedure tsubscriptnode.ppuwrite(ppufile:tcompilerppufile);
  587. begin
  588. inherited ppuwrite(ppufile);
  589. ppufile.putderef(vsderef);
  590. end;
  591. procedure tsubscriptnode.buildderefimpl;
  592. begin
  593. inherited buildderefimpl;
  594. vsderef.build(vs);
  595. end;
  596. procedure tsubscriptnode.derefimpl;
  597. begin
  598. inherited derefimpl;
  599. vs:=tfieldvarsym(vsderef.resolve);
  600. end;
  601. function tsubscriptnode.dogetcopy : tnode;
  602. var
  603. p : tsubscriptnode;
  604. begin
  605. p:=tsubscriptnode(inherited dogetcopy);
  606. p.vs:=vs;
  607. dogetcopy:=p;
  608. end;
  609. function tsubscriptnode.pass_typecheck:tnode;
  610. begin
  611. result:=nil;
  612. typecheckpass(left);
  613. { tp procvar support }
  614. maybe_call_procvar(left,true);
  615. resultdef:=vs.vardef;
  616. // don't put records from which we load float fields
  617. // in integer registers
  618. if (left.resultdef.typ=recorddef) and
  619. (resultdef.typ=floatdef) then
  620. make_not_regable(left,[ra_addr_regable]);
  621. end;
  622. procedure Tsubscriptnode.mark_write;
  623. begin
  624. include(flags,nf_write);
  625. end;
  626. function tsubscriptnode.pass_1 : tnode;
  627. begin
  628. result:=nil;
  629. firstpass(left);
  630. if codegenerror then
  631. exit;
  632. { several object types must be dereferenced implicitly }
  633. if is_implicit_pointer_object_type(left.resultdef) then
  634. expectloc:=LOC_REFERENCE
  635. else
  636. begin
  637. case left.expectloc of
  638. LOC_REGISTER,
  639. LOC_SUBSETREG:
  640. // can happen for function results on win32 and darwin/x86
  641. if (left.resultdef.size > sizeof(pint)) then
  642. expectloc:=LOC_REFERENCE
  643. else
  644. expectloc:=LOC_SUBSETREG;
  645. LOC_CREGISTER,
  646. LOC_CSUBSETREG:
  647. expectloc:=LOC_CSUBSETREG;
  648. LOC_REFERENCE,
  649. LOC_CREFERENCE:
  650. expectloc:=left.expectloc;
  651. else internalerror(20060521);
  652. end;
  653. end;
  654. end;
  655. function tsubscriptnode.docompare(p: tnode): boolean;
  656. begin
  657. docompare :=
  658. inherited docompare(p) and
  659. (vs = tsubscriptnode(p).vs);
  660. end;
  661. {*****************************************************************************
  662. TVECNODE
  663. *****************************************************************************}
  664. constructor tvecnode.create(l,r : tnode);
  665. begin
  666. inherited create(vecn,l,r);
  667. end;
  668. function tvecnode.pass_typecheck:tnode;
  669. var
  670. hightree: tnode;
  671. htype,elementdef : tdef;
  672. valid : boolean;
  673. begin
  674. result:=nil;
  675. typecheckpass(left);
  676. typecheckpass(right);
  677. { implicitly convert stringconstant to stringdef,
  678. see tbs/tb0476.pp for a test }
  679. if (left.nodetype=stringconstn) and
  680. (tstringconstnode(left).cst_type=cst_conststring) then
  681. begin
  682. if tstringconstnode(left).len>255 then
  683. inserttypeconv(left,cansistringtype)
  684. else
  685. inserttypeconv(left,cshortstringtype);
  686. end;
  687. { In p[1] p is always valid, it is not possible to
  688. declared a shortstring or normal array that has
  689. undefined number of elements. Dynamic array and
  690. ansi/widestring needs to be valid }
  691. valid:=is_dynamic_array(left.resultdef) or
  692. is_ansistring(left.resultdef) or
  693. is_wide_or_unicode_string(left.resultdef) or
  694. { implicit pointer dereference -> pointer is read }
  695. (left.resultdef.typ = pointerdef);
  696. if valid then
  697. set_varstate(left,vs_read,[vsf_must_be_valid]);
  698. {
  699. A vecn is, just like a loadn, always part of an expression with its
  700. own read/write and must_be_valid semantics. Therefore we don't have
  701. to do anything else here, just like for loadn's
  702. }
  703. set_varstate(right,vs_read,[vsf_must_be_valid]);
  704. if codegenerror then
  705. exit;
  706. { maybe type conversion for the index value, but
  707. do not convert enums, char (why not? (JM))
  708. and do not convert range nodes }
  709. if (right.nodetype<>rangen) and (is_integer(right.resultdef) or is_boolean(right.resultdef) or (left.resultdef.typ<>arraydef)) then
  710. case left.resultdef.typ of
  711. arraydef:
  712. if ado_isvariant in Tarraydef(left.resultdef).arrayoptions then
  713. {Variant arrays are a special array, can have negative indexes and would therefore
  714. need s32bit. However, they should not appear in a vecn, as they are handled in
  715. handle_variantarray in pexpr.pas. Therefore, encountering a variant array is an
  716. internal error... }
  717. internalerror(200707031)
  718. else if is_special_array(left.resultdef) then
  719. {Arrays without a high bound (dynamic arrays, open arrays) are zero based,
  720. convert indexes into these arrays to aword.}
  721. inserttypeconv(right,uinttype)
  722. { convert between pasbool and cbool if necessary }
  723. else if is_boolean(right.resultdef) then
  724. inserttypeconv(right,tarraydef(left.resultdef).rangedef)
  725. else
  726. {Convert array indexes to low_bound..high_bound.}
  727. inserttypeconv(right,Torddef.create(Torddef(sinttype).ordtype,
  728. int64(Tarraydef(left.resultdef).lowrange),
  729. int64(Tarraydef(left.resultdef).highrange)
  730. ));
  731. stringdef:
  732. if is_open_string(left.resultdef) then
  733. inserttypeconv(right,u8inttype)
  734. else if is_shortstring(left.resultdef) then
  735. {Convert shortstring indexes to 0..length.}
  736. inserttypeconv(right,Torddef.create(u8bit,0,int64(Tstringdef(left.resultdef).len)))
  737. else
  738. {Convert indexes into dynamically allocated strings to aword.}
  739. inserttypeconv(right,uinttype);
  740. else
  741. {Others, i.e. pointer indexes to aint.}
  742. inserttypeconv(right,sinttype);
  743. end;
  744. { although we never put regular arrays or shortstrings in registers,
  745. it's possible that another type was typecasted to a small record
  746. that has a field of one of these types -> in that case the record
  747. can't be a regvar either }
  748. if ((left.resultdef.typ=arraydef) and
  749. not is_special_array(left.resultdef)) or
  750. ((left.resultdef.typ=stringdef) and
  751. (tstringdef(left.resultdef).stringtype in [st_shortstring,st_longstring])) then
  752. make_not_regable(left,[ra_addr_regable]);
  753. case left.resultdef.typ of
  754. arraydef :
  755. begin
  756. { check type of the index value }
  757. if (compare_defs(right.resultdef,tarraydef(left.resultdef).rangedef,right.nodetype)=te_incompatible) then
  758. IncompatibleTypes(right.resultdef,tarraydef(left.resultdef).rangedef);
  759. if right.nodetype=rangen then
  760. resultdef:=left.resultdef
  761. else
  762. resultdef:=Tarraydef(left.resultdef).elementdef;
  763. { if we are range checking an open array or array of const, we }
  764. { need to load the high parameter. If the current procedure is }
  765. { nested inside the procedure to which the open array/of const }
  766. { was passed, then the high parameter must not be a regvar. }
  767. { So create a loadnode for the high parameter here and }
  768. { typecheck it, then the loadnode will make the high parameter }
  769. { not regable. Otherwise this would only happen inside pass_2, }
  770. { which is too late since by then the regvars are already }
  771. { assigned (pass_1 is also already too late, because then the }
  772. { regvars of the parent are also already assigned). }
  773. { webtbs/tw8975 }
  774. if (cs_check_range in current_settings.localswitches) and
  775. (is_open_array(left.resultdef) or
  776. is_array_of_const(left.resultdef)) and
  777. { cdecl functions don't have high() so we can not check the range }
  778. { (can't use current_procdef, since it may be a nested procedure) }
  779. not(tprocdef(tparasymtable(tparavarsym(tloadnode(left).symtableentry).owner).defowner).proccalloption in cdecl_pocalls) then
  780. begin
  781. { load_high_value_node already typechecks }
  782. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  783. hightree.free;
  784. end;
  785. end;
  786. pointerdef :
  787. begin
  788. { are we accessing a pointer[], then convert the pointer to
  789. an array first, in FPC this is allowed for all pointers
  790. (except voidpointer) in delphi/tp7 it's only allowed for pchars. }
  791. if not is_voidpointer(left.resultdef) and
  792. (
  793. (cs_pointermath in current_settings.localswitches) or
  794. tpointerdef(left.resultdef).has_pointer_math or
  795. is_pchar(left.resultdef) or
  796. is_pwidechar(left.resultdef)
  797. ) then
  798. begin
  799. { convert pointer to array }
  800. htype:=tarraydef.create_from_pointer(tpointerdef(left.resultdef).pointeddef);
  801. inserttypeconv(left,htype);
  802. if right.nodetype=rangen then
  803. resultdef:=htype
  804. else
  805. resultdef:=tarraydef(htype).elementdef;
  806. end
  807. else
  808. CGMessage(type_e_array_required);
  809. end;
  810. stringdef :
  811. begin
  812. case tstringdef(left.resultdef).stringtype of
  813. st_unicodestring,
  814. st_widestring :
  815. elementdef:=cwidechartype;
  816. st_ansistring :
  817. elementdef:=cchartype;
  818. st_longstring :
  819. elementdef:=cchartype;
  820. st_shortstring :
  821. elementdef:=cchartype;
  822. end;
  823. if right.nodetype=rangen then
  824. begin
  825. htype:=Tarraydef.create_from_pointer(elementdef);
  826. resultdef:=htype;
  827. end
  828. else
  829. begin
  830. { indexed access to 0 element is only allowed for shortstrings }
  831. if (right.nodetype=ordconstn) and
  832. (Tordconstnode(right).value.svalue=0) and
  833. not is_shortstring(left.resultdef) then
  834. CGMessage(cg_e_can_access_element_zero);
  835. resultdef:=elementdef;
  836. end;
  837. end;
  838. variantdef :
  839. resultdef:=cvarianttype;
  840. else
  841. CGMessage(type_e_array_required);
  842. end;
  843. end;
  844. procedure Tvecnode.mark_write;
  845. begin
  846. include(flags,nf_write);
  847. end;
  848. function tvecnode.pass_1 : tnode;
  849. begin
  850. result:=nil;
  851. firstpass(left);
  852. firstpass(right);
  853. if codegenerror then
  854. exit;
  855. if (nf_callunique in flags) and
  856. (is_ansistring(left.resultdef) or
  857. is_unicodestring(left.resultdef) or
  858. (is_widestring(left.resultdef) and not(tf_winlikewidestring in target_info.flags))) then
  859. begin
  860. left := ctypeconvnode.create_internal(ccallnode.createintern('fpc_'+tstringdef(left.resultdef).stringtypname+'_unique',
  861. ccallparanode.create(
  862. ctypeconvnode.create_internal(left,voidpointertype),nil)),
  863. left.resultdef);
  864. firstpass(left);
  865. { double resultdef passes somwhere else may cause this to be }
  866. { reset though :/ }
  867. exclude(flags,nf_callunique);
  868. end
  869. else if is_widestring(left.resultdef) and (tf_winlikewidestring in target_info.flags) then
  870. exclude(flags,nf_callunique);
  871. { a range node as array index can only appear in function calls, and
  872. those convert the range node into something else in
  873. tcallnode.gen_high_tree }
  874. if (right.nodetype=rangen) then
  875. CGMessagePos(right.fileinfo,parser_e_illegal_expression)
  876. else if (not is_packed_array(left.resultdef)) or
  877. ((tarraydef(left.resultdef).elepackedbitsize mod 8) = 0) then
  878. if left.expectloc=LOC_CREFERENCE then
  879. expectloc:=LOC_CREFERENCE
  880. else
  881. expectloc:=LOC_REFERENCE
  882. else
  883. if left.expectloc=LOC_CREFERENCE then
  884. expectloc:=LOC_CSUBSETREF
  885. else
  886. expectloc:=LOC_SUBSETREF;
  887. end;
  888. {*****************************************************************************
  889. TWITHNODE
  890. *****************************************************************************}
  891. constructor twithnode.create(l:tnode);
  892. begin
  893. inherited create(withn,l);
  894. fileinfo:=l.fileinfo;
  895. end;
  896. destructor twithnode.destroy;
  897. begin
  898. inherited destroy;
  899. end;
  900. constructor twithnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  901. begin
  902. inherited ppuload(t,ppufile);
  903. end;
  904. procedure twithnode.ppuwrite(ppufile:tcompilerppufile);
  905. begin
  906. inherited ppuwrite(ppufile);
  907. end;
  908. function twithnode.dogetcopy : tnode;
  909. var
  910. p : twithnode;
  911. begin
  912. p:=twithnode(inherited dogetcopy);
  913. result:=p;
  914. end;
  915. function twithnode.pass_typecheck:tnode;
  916. begin
  917. result:=nil;
  918. resultdef:=voidtype;
  919. if assigned(left) then
  920. typecheckpass(left);
  921. end;
  922. function twithnode.pass_1 : tnode;
  923. begin
  924. result:=nil;
  925. expectloc:=LOC_VOID;
  926. end;
  927. function twithnode.docompare(p: tnode): boolean;
  928. begin
  929. docompare :=
  930. inherited docompare(p);
  931. end;
  932. function is_big_untyped_addrnode(p: tnode): boolean;
  933. begin
  934. is_big_untyped_addrnode:=(p.nodetype=addrn) and
  935. not (nf_typedaddr in p.flags) and (taddrnode(p).left.resultdef.size > 1);
  936. end;
  937. end.