nmem.pas 43 KB

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