nmem.pas 44 KB

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