nmem.pas 37 KB

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