nmem.pas 44 KB

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