nmem.pas 40 KB

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