nmem.pas 43 KB

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