nmem.pas 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  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 }
  406. if is_constnode(left) and
  407. not(
  408. (nf_internal in flags) and
  409. (left.nodetype in [stringconstn])
  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. (left.resultdef.typ=procvardef) and
  420. ((m_tp_procvar in current_settings.modeswitches) or
  421. (m_mac_procvar in current_settings.modeswitches))
  422. ) then
  423. begin
  424. isprocvar:=(left.resultdef.typ=procvardef);
  425. if not isprocvar then
  426. begin
  427. left:=ctypeconvnode.create_proc_to_procvar(left);
  428. left.fileinfo:=fileinfo;
  429. typecheckpass(left);
  430. end;
  431. { In tp procvar mode the result is always a voidpointer. Insert
  432. a typeconversion to voidpointer. For methodpointers we need
  433. to load the proc field }
  434. if (m_tp_procvar in current_settings.modeswitches) or
  435. (m_mac_procvar in current_settings.modeswitches) then
  436. begin
  437. if tabstractprocdef(left.resultdef).is_addressonly then
  438. begin
  439. result:=ctypeconvnode.create_internal(left,voidpointertype);
  440. include(result.flags,nf_load_procvar);
  441. left:=nil;
  442. end
  443. else
  444. begin
  445. { For procvars and for nested routines we need to return
  446. the proc field of the methodpointer }
  447. if isprocvar or
  448. is_nested_pd(tabstractprocdef(left.resultdef)) then
  449. begin
  450. { find proc field in methodpointer record }
  451. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  452. if not assigned(hsym) then
  453. internalerror(200412041);
  454. { Load tmehodpointer(left).proc }
  455. result:=csubscriptnode.create(
  456. hsym,
  457. ctypeconvnode.create_internal(left,methodpointertype));
  458. left:=nil;
  459. end
  460. else
  461. CGMessage(type_e_variable_id_expected);
  462. end;
  463. end
  464. else
  465. begin
  466. { Return the typeconvn only }
  467. result:=left;
  468. left:=nil;
  469. end;
  470. end
  471. else
  472. begin
  473. { what are we getting the address from an absolute sym? }
  474. hp:=left;
  475. while assigned(hp) and (hp.nodetype in [typeconvn,vecn,derefn,subscriptn]) do
  476. hp:=tunarynode(hp).left;
  477. if not assigned(hp) then
  478. internalerror(200412042);
  479. {$ifdef i386}
  480. if (hp.nodetype=loadn) and
  481. ((tloadnode(hp).symtableentry.typ=absolutevarsym) and
  482. tabsolutevarsym(tloadnode(hp).symtableentry).absseg) then
  483. begin
  484. if not(nf_typedaddr in flags) then
  485. resultdef:=voidfarpointertype
  486. else
  487. resultdef:=tpointerdef.createfar(left.resultdef);
  488. end
  489. else
  490. {$endif i386}
  491. if (hp.nodetype=loadn) and
  492. (tloadnode(hp).symtableentry.typ=absolutevarsym) and
  493. {$ifdef i386}
  494. not(tabsolutevarsym(tloadnode(hp).symtableentry).absseg) and
  495. {$endif i386}
  496. (tabsolutevarsym(tloadnode(hp).symtableentry).abstyp=toaddr) then
  497. begin
  498. if nf_typedaddr in flags then
  499. result:=cpointerconstnode.create(tabsolutevarsym(tloadnode(hp).symtableentry).addroffset,tpointerdef.create(left.resultdef))
  500. else
  501. result:=cpointerconstnode.create(tabsolutevarsym(tloadnode(hp).symtableentry).addroffset,voidpointertype);
  502. exit;
  503. end
  504. else if (nf_internal in flags) or
  505. valid_for_addr(left,true) then
  506. begin
  507. if not(nf_typedaddr in flags) then
  508. resultdef:=voidpointertype
  509. else
  510. resultdef:=tpointerdef.create(left.resultdef);
  511. end
  512. else
  513. CGMessage(type_e_variable_id_expected);
  514. end;
  515. if mark_read_written then
  516. begin
  517. { This is actually only "read", but treat it nevertheless as }
  518. { modified due to the possible use of pointers }
  519. { To avoid false positives regarding "uninitialised" }
  520. { warnings when using arrays, perform it in two steps }
  521. set_varstate(left,vs_written,[]);
  522. { vsf_must_be_valid so it doesn't get changed into }
  523. { vsf_referred_not_inited }
  524. set_varstate(left,vs_read,[vsf_must_be_valid]);
  525. end;
  526. end;
  527. function taddrnode.pass_1 : tnode;
  528. begin
  529. result:=nil;
  530. firstpass(left);
  531. if codegenerror then
  532. exit;
  533. { is this right for object of methods ?? }
  534. expectloc:=LOC_REGISTER;
  535. end;
  536. {*****************************************************************************
  537. TDEREFNODE
  538. *****************************************************************************}
  539. constructor tderefnode.create(l : tnode);
  540. begin
  541. inherited create(derefn,l);
  542. end;
  543. function tderefnode.pass_typecheck:tnode;
  544. begin
  545. result:=nil;
  546. typecheckpass(left);
  547. set_varstate(left,vs_read,[vsf_must_be_valid]);
  548. if codegenerror then
  549. exit;
  550. { tp procvar support }
  551. maybe_call_procvar(left,true);
  552. if left.resultdef.typ=pointerdef then
  553. resultdef:=tpointerdef(left.resultdef).pointeddef
  554. else
  555. CGMessage(parser_e_invalid_qualifier);
  556. end;
  557. procedure Tderefnode.mark_write;
  558. begin
  559. include(flags,nf_write);
  560. end;
  561. function tderefnode.pass_1 : tnode;
  562. begin
  563. result:=nil;
  564. firstpass(left);
  565. if codegenerror then
  566. exit;
  567. expectloc:=LOC_REFERENCE;
  568. end;
  569. {*****************************************************************************
  570. TSUBSCRIPTNODE
  571. *****************************************************************************}
  572. constructor tsubscriptnode.create(varsym : tsym;l : tnode);
  573. begin
  574. inherited create(subscriptn,l);
  575. { vs should be changed to tsym! }
  576. vs:=tfieldvarsym(varsym);
  577. end;
  578. constructor tsubscriptnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  579. begin
  580. inherited ppuload(t,ppufile);
  581. ppufile.getderef(vsderef);
  582. end;
  583. procedure tsubscriptnode.ppuwrite(ppufile:tcompilerppufile);
  584. begin
  585. inherited ppuwrite(ppufile);
  586. ppufile.putderef(vsderef);
  587. end;
  588. procedure tsubscriptnode.buildderefimpl;
  589. begin
  590. inherited buildderefimpl;
  591. vsderef.build(vs);
  592. end;
  593. procedure tsubscriptnode.derefimpl;
  594. begin
  595. inherited derefimpl;
  596. vs:=tfieldvarsym(vsderef.resolve);
  597. end;
  598. function tsubscriptnode.dogetcopy : tnode;
  599. var
  600. p : tsubscriptnode;
  601. begin
  602. p:=tsubscriptnode(inherited dogetcopy);
  603. p.vs:=vs;
  604. dogetcopy:=p;
  605. end;
  606. function tsubscriptnode.pass_typecheck:tnode;
  607. begin
  608. result:=nil;
  609. typecheckpass(left);
  610. { tp procvar support }
  611. maybe_call_procvar(left,true);
  612. resultdef:=vs.vardef;
  613. // don't put records from which we load float fields
  614. // in integer registers
  615. if (left.resultdef.typ=recorddef) and
  616. (resultdef.typ=floatdef) then
  617. make_not_regable(left,[ra_addr_regable]);
  618. end;
  619. procedure Tsubscriptnode.mark_write;
  620. begin
  621. include(flags,nf_write);
  622. end;
  623. function tsubscriptnode.pass_1 : tnode;
  624. begin
  625. result:=nil;
  626. firstpass(left);
  627. if codegenerror then
  628. exit;
  629. { several object types must be dereferenced implicitly }
  630. if is_implicit_pointer_object_type(left.resultdef) then
  631. expectloc:=LOC_REFERENCE
  632. else
  633. begin
  634. case left.expectloc of
  635. LOC_REGISTER,
  636. LOC_SUBSETREG:
  637. // can happen for function results on win32 and darwin/x86
  638. if (left.resultdef.size > sizeof(pint)) then
  639. expectloc:=LOC_REFERENCE
  640. else
  641. expectloc:=LOC_SUBSETREG;
  642. LOC_CREGISTER,
  643. LOC_CSUBSETREG:
  644. expectloc:=LOC_CSUBSETREG;
  645. LOC_REFERENCE,
  646. LOC_CREFERENCE:
  647. expectloc:=left.expectloc;
  648. else internalerror(20060521);
  649. end;
  650. end;
  651. end;
  652. function tsubscriptnode.docompare(p: tnode): boolean;
  653. begin
  654. docompare :=
  655. inherited docompare(p) and
  656. (vs = tsubscriptnode(p).vs);
  657. end;
  658. {*****************************************************************************
  659. TVECNODE
  660. *****************************************************************************}
  661. constructor tvecnode.create(l,r : tnode);
  662. begin
  663. inherited create(vecn,l,r);
  664. end;
  665. function tvecnode.pass_typecheck:tnode;
  666. var
  667. hightree: tnode;
  668. htype,elementdef : tdef;
  669. valid : boolean;
  670. begin
  671. result:=nil;
  672. typecheckpass(left);
  673. typecheckpass(right);
  674. { implicitly convert stringconstant to stringdef,
  675. see tbs/tb0476.pp for a test }
  676. if (left.nodetype=stringconstn) and
  677. (tstringconstnode(left).cst_type=cst_conststring) then
  678. begin
  679. if tstringconstnode(left).len>255 then
  680. inserttypeconv(left,cansistringtype)
  681. else
  682. inserttypeconv(left,cshortstringtype);
  683. end;
  684. { In p[1] p is always valid, it is not possible to
  685. declared a shortstring or normal array that has
  686. undefined number of elements. Dynamic array and
  687. ansi/widestring needs to be valid }
  688. valid:=is_dynamic_array(left.resultdef) or
  689. is_ansistring(left.resultdef) or
  690. is_wide_or_unicode_string(left.resultdef) or
  691. { implicit pointer dereference -> pointer is read }
  692. (left.resultdef.typ = pointerdef);
  693. if valid then
  694. set_varstate(left,vs_read,[vsf_must_be_valid]);
  695. {
  696. A vecn is, just like a loadn, always part of an expression with its
  697. own read/write and must_be_valid semantics. Therefore we don't have
  698. to do anything else here, just like for loadn's
  699. }
  700. set_varstate(right,vs_read,[vsf_must_be_valid]);
  701. if codegenerror then
  702. exit;
  703. { maybe type conversion for the index value, but
  704. do not convert enums, char (why not? (JM))
  705. and do not convert range nodes }
  706. if (right.nodetype<>rangen) and (is_integer(right.resultdef) or is_boolean(right.resultdef) or (left.resultdef.typ<>arraydef)) then
  707. case left.resultdef.typ of
  708. arraydef:
  709. if ado_isvariant in Tarraydef(left.resultdef).arrayoptions then
  710. {Variant arrays are a special array, can have negative indexes and would therefore
  711. need s32bit. However, they should not appear in a vecn, as they are handled in
  712. handle_variantarray in pexpr.pas. Therefore, encountering a variant array is an
  713. internal error... }
  714. internalerror(200707031)
  715. else if is_special_array(left.resultdef) then
  716. {Arrays without a high bound (dynamic arrays, open arrays) are zero based,
  717. convert indexes into these arrays to aword.}
  718. inserttypeconv(right,uinttype)
  719. { convert between pasbool and cbool if necessary }
  720. else if is_boolean(right.resultdef) then
  721. inserttypeconv(right,tarraydef(left.resultdef).rangedef)
  722. else
  723. {Convert array indexes to low_bound..high_bound.}
  724. inserttypeconv(right,Torddef.create(Torddef(sinttype).ordtype,
  725. int64(Tarraydef(left.resultdef).lowrange),
  726. int64(Tarraydef(left.resultdef).highrange)
  727. ));
  728. stringdef:
  729. if is_open_string(left.resultdef) then
  730. inserttypeconv(right,u8inttype)
  731. else if is_shortstring(left.resultdef) then
  732. {Convert shortstring indexes to 0..length.}
  733. inserttypeconv(right,Torddef.create(u8bit,0,int64(Tstringdef(left.resultdef).len)))
  734. else
  735. {Convert indexes into dynamically allocated strings to aword.}
  736. inserttypeconv(right,uinttype);
  737. else
  738. {Others, i.e. pointer indexes to aint.}
  739. inserttypeconv(right,sinttype);
  740. end;
  741. { although we never put regular arrays or shortstrings in registers,
  742. it's possible that another type was typecasted to a small record
  743. that has a field of one of these types -> in that case the record
  744. can't be a regvar either }
  745. if ((left.resultdef.typ=arraydef) and
  746. not is_special_array(left.resultdef)) or
  747. ((left.resultdef.typ=stringdef) and
  748. (tstringdef(left.resultdef).stringtype in [st_shortstring,st_longstring])) then
  749. make_not_regable(left,[ra_addr_regable]);
  750. case left.resultdef.typ of
  751. arraydef :
  752. begin
  753. { check type of the index value }
  754. if (compare_defs(right.resultdef,tarraydef(left.resultdef).rangedef,right.nodetype)=te_incompatible) then
  755. IncompatibleTypes(right.resultdef,tarraydef(left.resultdef).rangedef);
  756. if right.nodetype=rangen then
  757. resultdef:=left.resultdef
  758. else
  759. resultdef:=Tarraydef(left.resultdef).elementdef;
  760. { if we are range checking an open array or array of const, we }
  761. { need to load the high parameter. If the current procedure is }
  762. { nested inside the procedure to which the open array/of const }
  763. { was passed, then the high parameter must not be a regvar. }
  764. { So create a loadnode for the high parameter here and }
  765. { typecheck it, then the loadnode will make the high parameter }
  766. { not regable. Otherwise this would only happen inside pass_2, }
  767. { which is too late since by then the regvars are already }
  768. { assigned (pass_1 is also already too late, because then the }
  769. { regvars of the parent are also already assigned). }
  770. { webtbs/tw8975 }
  771. if (cs_check_range in current_settings.localswitches) and
  772. (is_open_array(left.resultdef) or
  773. is_array_of_const(left.resultdef)) and
  774. { cdecl functions don't have high() so we can not check the range }
  775. { (can't use current_procdef, since it may be a nested procedure) }
  776. not(tprocdef(tparasymtable(tparavarsym(tloadnode(left).symtableentry).owner).defowner).proccalloption in cdecl_pocalls) then
  777. begin
  778. { load_high_value_node already typechecks }
  779. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  780. hightree.free;
  781. end;
  782. end;
  783. pointerdef :
  784. begin
  785. { are we accessing a pointer[], then convert the pointer to
  786. an array first, in FPC this is allowed for all pointers
  787. (except voidpointer) in delphi/tp7 it's only allowed for pchars. }
  788. if not is_voidpointer(left.resultdef) and
  789. (
  790. (cs_pointermath in current_settings.localswitches) or
  791. tpointerdef(left.resultdef).has_pointer_math or
  792. is_pchar(left.resultdef) or
  793. is_pwidechar(left.resultdef)
  794. ) then
  795. begin
  796. { convert pointer to array }
  797. htype:=tarraydef.create_from_pointer(tpointerdef(left.resultdef).pointeddef);
  798. inserttypeconv(left,htype);
  799. if right.nodetype=rangen then
  800. resultdef:=htype
  801. else
  802. resultdef:=tarraydef(htype).elementdef;
  803. end
  804. else
  805. CGMessage(type_e_array_required);
  806. end;
  807. stringdef :
  808. begin
  809. case tstringdef(left.resultdef).stringtype of
  810. st_unicodestring,
  811. st_widestring :
  812. elementdef:=cwidechartype;
  813. st_ansistring :
  814. elementdef:=cchartype;
  815. st_longstring :
  816. elementdef:=cchartype;
  817. st_shortstring :
  818. elementdef:=cchartype;
  819. end;
  820. if right.nodetype=rangen then
  821. begin
  822. htype:=Tarraydef.create_from_pointer(elementdef);
  823. resultdef:=htype;
  824. end
  825. else
  826. begin
  827. { indexed access to 0 element is only allowed for shortstrings }
  828. if (right.nodetype=ordconstn) and
  829. (Tordconstnode(right).value.svalue=0) and
  830. not is_shortstring(left.resultdef) then
  831. CGMessage(cg_e_can_access_element_zero);
  832. resultdef:=elementdef;
  833. end;
  834. end;
  835. variantdef :
  836. resultdef:=cvarianttype;
  837. else
  838. CGMessage(type_e_array_required);
  839. end;
  840. end;
  841. procedure Tvecnode.mark_write;
  842. begin
  843. include(flags,nf_write);
  844. end;
  845. function tvecnode.pass_1 : tnode;
  846. begin
  847. result:=nil;
  848. firstpass(left);
  849. firstpass(right);
  850. if codegenerror then
  851. exit;
  852. if (nf_callunique in flags) and
  853. (is_ansistring(left.resultdef) or
  854. is_unicodestring(left.resultdef) or
  855. (is_widestring(left.resultdef) and not(tf_winlikewidestring in target_info.flags))) then
  856. begin
  857. left := ctypeconvnode.create_internal(ccallnode.createintern('fpc_'+tstringdef(left.resultdef).stringtypname+'_unique',
  858. ccallparanode.create(
  859. ctypeconvnode.create_internal(left,voidpointertype),nil)),
  860. left.resultdef);
  861. firstpass(left);
  862. { double resultdef passes somwhere else may cause this to be }
  863. { reset though :/ }
  864. exclude(flags,nf_callunique);
  865. end
  866. else if is_widestring(left.resultdef) and (tf_winlikewidestring in target_info.flags) then
  867. exclude(flags,nf_callunique);
  868. { a range node as array index can only appear in function calls, and
  869. those convert the range node into something else in
  870. tcallnode.gen_high_tree }
  871. if (right.nodetype=rangen) then
  872. CGMessagePos(right.fileinfo,parser_e_illegal_expression)
  873. else if (not is_packed_array(left.resultdef)) or
  874. ((tarraydef(left.resultdef).elepackedbitsize mod 8) = 0) then
  875. if left.expectloc=LOC_CREFERENCE then
  876. expectloc:=LOC_CREFERENCE
  877. else
  878. expectloc:=LOC_REFERENCE
  879. else
  880. if left.expectloc=LOC_CREFERENCE then
  881. expectloc:=LOC_CSUBSETREF
  882. else
  883. expectloc:=LOC_SUBSETREF;
  884. end;
  885. {*****************************************************************************
  886. TWITHNODE
  887. *****************************************************************************}
  888. constructor twithnode.create(l:tnode);
  889. begin
  890. inherited create(withn,l);
  891. fileinfo:=l.fileinfo;
  892. end;
  893. destructor twithnode.destroy;
  894. begin
  895. inherited destroy;
  896. end;
  897. constructor twithnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  898. begin
  899. inherited ppuload(t,ppufile);
  900. end;
  901. procedure twithnode.ppuwrite(ppufile:tcompilerppufile);
  902. begin
  903. inherited ppuwrite(ppufile);
  904. end;
  905. function twithnode.dogetcopy : tnode;
  906. var
  907. p : twithnode;
  908. begin
  909. p:=twithnode(inherited dogetcopy);
  910. result:=p;
  911. end;
  912. function twithnode.pass_typecheck:tnode;
  913. begin
  914. result:=nil;
  915. resultdef:=voidtype;
  916. if assigned(left) then
  917. typecheckpass(left);
  918. end;
  919. function twithnode.pass_1 : tnode;
  920. begin
  921. result:=nil;
  922. expectloc:=LOC_VOID;
  923. end;
  924. function twithnode.docompare(p: tnode): boolean;
  925. begin
  926. docompare :=
  927. inherited docompare(p);
  928. end;
  929. function is_big_untyped_addrnode(p: tnode): boolean;
  930. begin
  931. is_big_untyped_addrnode:=(p.nodetype=addrn) and
  932. not (nf_typedaddr in p.flags) and (taddrnode(p).left.resultdef.size > 1);
  933. end;
  934. end.