nmem.pas 42 KB

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