nmem.pas 40 KB

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