nmem.pas 37 KB

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