nmem.pas 41 KB

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