nmem.pas 39 KB

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