nmem.pas 37 KB

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