nmem.pas 40 KB

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