nmem.pas 42 KB

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