nmem.pas 39 KB

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