nmem.pas 45 KB

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