2
0

nmem.pas 43 KB

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