nmem.pas 43 KB

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