nmem.pas 41 KB

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