nmem.pas 42 KB

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