nmem.pas 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  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. begin
  413. result:=nil;
  414. typecheckpass(left);
  415. if codegenerror then
  416. exit;
  417. make_not_regable(left,[ra_addr_regable,ra_addr_taken]);
  418. { don't allow constants, for internal use we also
  419. allow taking the address of strings and sets }
  420. if is_constnode(left) and
  421. not(
  422. (nf_internal in flags) and
  423. (left.nodetype in [stringconstn,setconstn])
  424. ) then
  425. begin
  426. CGMessagePos(left.fileinfo,type_e_no_addr_of_constant);
  427. exit;
  428. end;
  429. { Handle @proc special, also @procvar in tp-mode needs
  430. special handling }
  431. if (left.resultdef.typ=procdef) or
  432. (
  433. { in case of nf_internal, follow the normal FPC semantics so that
  434. we can easily get the actual address of a procvar }
  435. not(nf_internal in flags) and
  436. (left.resultdef.typ=procvardef) and
  437. ((m_tp_procvar in current_settings.modeswitches) or
  438. (m_mac_procvar in current_settings.modeswitches))
  439. ) then
  440. begin
  441. isprocvar:=(left.resultdef.typ=procvardef);
  442. if not isprocvar then
  443. begin
  444. left:=ctypeconvnode.create_proc_to_procvar(left);
  445. left.fileinfo:=fileinfo;
  446. typecheckpass(left);
  447. end;
  448. { In tp procvar mode the result is always a voidpointer. Insert
  449. a typeconversion to voidpointer. For methodpointers we need
  450. to load the proc field }
  451. if (m_tp_procvar in current_settings.modeswitches) or
  452. (m_mac_procvar in current_settings.modeswitches) then
  453. begin
  454. if tabstractprocdef(left.resultdef).is_addressonly then
  455. begin
  456. result:=ctypeconvnode.create_internal(left,tabstractprocdef(left.resultdef).address_type);
  457. include(result.flags,nf_load_procvar);
  458. left:=nil;
  459. end
  460. else
  461. begin
  462. { For procvars and for nested routines we need to return
  463. the proc field of the methodpointer }
  464. if isprocvar or
  465. is_nested_pd(tabstractprocdef(left.resultdef)) then
  466. begin
  467. { find proc field in methodpointer record }
  468. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  469. if not assigned(hsym) then
  470. internalerror(200412041);
  471. { Load tmehodpointer(left).proc }
  472. result:=csubscriptnode.create(
  473. hsym,
  474. ctypeconvnode.create_internal(left,methodpointertype));
  475. left:=nil;
  476. end
  477. else
  478. CGMessage(type_e_variable_id_expected);
  479. end;
  480. end
  481. else
  482. begin
  483. { Return the typeconvn only }
  484. result:=left;
  485. left:=nil;
  486. end;
  487. end
  488. else
  489. begin
  490. hp:=left;
  491. while assigned(hp) and (hp.nodetype in [typeconvn,vecn,derefn,subscriptn]) do
  492. hp:=tunarynode(hp).left;
  493. if not assigned(hp) then
  494. internalerror(200412042);
  495. if typecheck_non_proc(hp,result) then
  496. begin
  497. if assigned(result) then
  498. exit;
  499. end
  500. else
  501. CGMessage(type_e_variable_id_expected);
  502. end;
  503. if mark_read_written then
  504. begin
  505. { This is actually only "read", but treat it nevertheless as }
  506. { modified due to the possible use of pointers }
  507. { To avoid false positives regarding "uninitialised" }
  508. { warnings when using arrays, perform it in two steps }
  509. set_varstate(left,vs_written,[]);
  510. { vsf_must_be_valid so it doesn't get changed into }
  511. { vsf_referred_not_inited }
  512. set_varstate(left,vs_read,[vsf_must_be_valid]);
  513. end;
  514. end;
  515. function taddrnode.typecheck_non_proc(realsource: tnode; out res: tnode): boolean;
  516. var
  517. hp : tnode;
  518. hsym : tfieldvarsym;
  519. offset: asizeint;
  520. begin
  521. result:=false;
  522. res:=nil;
  523. if (realsource.nodetype=loadn) and
  524. (tloadnode(realsource).symtableentry.typ=absolutevarsym) and
  525. (tabsolutevarsym(tloadnode(realsource).symtableentry).abstyp=toaddr) then
  526. begin
  527. offset:=tabsolutevarsym(tloadnode(realsource).symtableentry).addroffset;
  528. hp:=left;
  529. while assigned(hp)and(hp.nodetype=subscriptn) do
  530. begin
  531. hsym:=tsubscriptnode(hp).vs;
  532. if tabstractrecordsymtable(hsym.owner).is_packed then
  533. begin
  534. { can't calculate the address of a non-byte aligned field }
  535. if (hsym.fieldoffset mod 8)<>0 then
  536. begin
  537. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_var_addr);
  538. exit
  539. end;
  540. inc(offset,hsym.fieldoffset div 8)
  541. end
  542. else
  543. inc(offset,hsym.fieldoffset);
  544. hp:=tunarynode(hp).left;
  545. end;
  546. if nf_typedaddr in flags then
  547. res:=cpointerconstnode.create(offset,getpointerdef(left.resultdef))
  548. else
  549. res:=cpointerconstnode.create(offset,voidpointertype);
  550. result:=true;
  551. end
  552. else if (nf_internal in flags) or
  553. valid_for_addr(left,true) then
  554. begin
  555. if not(nf_typedaddr in flags) then
  556. resultdef:=voidpointertype
  557. else
  558. resultdef:=getpointerdef(left.resultdef);
  559. result:=true;
  560. end
  561. end;
  562. function taddrnode.pass_1 : tnode;
  563. begin
  564. result:=nil;
  565. firstpass(left);
  566. if codegenerror then
  567. exit;
  568. { is this right for object of methods ?? }
  569. expectloc:=LOC_REGISTER;
  570. end;
  571. {*****************************************************************************
  572. TDEREFNODE
  573. *****************************************************************************}
  574. constructor tderefnode.create(l : tnode);
  575. begin
  576. inherited create(derefn,l);
  577. end;
  578. function tderefnode.pass_typecheck:tnode;
  579. begin
  580. result:=nil;
  581. typecheckpass(left);
  582. set_varstate(left,vs_read,[vsf_must_be_valid]);
  583. if codegenerror then
  584. exit;
  585. { tp procvar support }
  586. maybe_call_procvar(left,true);
  587. if left.resultdef.typ=pointerdef then
  588. resultdef:=tpointerdef(left.resultdef).pointeddef
  589. else
  590. CGMessage(parser_e_invalid_qualifier);
  591. end;
  592. procedure Tderefnode.mark_write;
  593. begin
  594. include(flags,nf_write);
  595. end;
  596. function tderefnode.pass_1 : tnode;
  597. begin
  598. result:=nil;
  599. firstpass(left);
  600. if codegenerror then
  601. exit;
  602. expectloc:=LOC_REFERENCE;
  603. end;
  604. {*****************************************************************************
  605. TSUBSCRIPTNODE
  606. *****************************************************************************}
  607. constructor tsubscriptnode.create(varsym : tsym;l : tnode);
  608. begin
  609. inherited create(subscriptn,l);
  610. { vs should be changed to tsym! }
  611. vs:=tfieldvarsym(varsym);
  612. end;
  613. constructor tsubscriptnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  614. begin
  615. inherited ppuload(t,ppufile);
  616. ppufile.getderef(vsderef);
  617. end;
  618. procedure tsubscriptnode.ppuwrite(ppufile:tcompilerppufile);
  619. begin
  620. inherited ppuwrite(ppufile);
  621. ppufile.putderef(vsderef);
  622. end;
  623. procedure tsubscriptnode.buildderefimpl;
  624. begin
  625. inherited buildderefimpl;
  626. vsderef.build(vs);
  627. end;
  628. procedure tsubscriptnode.derefimpl;
  629. begin
  630. inherited derefimpl;
  631. vs:=tfieldvarsym(vsderef.resolve);
  632. end;
  633. function tsubscriptnode.dogetcopy : tnode;
  634. var
  635. p : tsubscriptnode;
  636. begin
  637. p:=tsubscriptnode(inherited dogetcopy);
  638. p.vs:=vs;
  639. dogetcopy:=p;
  640. end;
  641. function tsubscriptnode.pass_typecheck:tnode;
  642. begin
  643. result:=nil;
  644. typecheckpass(left);
  645. { tp procvar support }
  646. maybe_call_procvar(left,true);
  647. resultdef:=vs.vardef;
  648. // don't put records from which we load float fields
  649. // in integer registers
  650. if (left.resultdef.typ=recorddef) and
  651. (resultdef.typ=floatdef) then
  652. make_not_regable(left,[ra_addr_regable]);
  653. end;
  654. procedure Tsubscriptnode.mark_write;
  655. begin
  656. include(flags,nf_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 : 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. else
  821. {Others, i.e. pointer indexes to aint.}
  822. inserttypeconv(right,sinttype);
  823. end;
  824. { although we never put regular arrays or shortstrings in registers,
  825. it's possible that another type was typecasted to a small record
  826. that has a field of one of these types -> in that case the record
  827. can't be a regvar either }
  828. if ((left.resultdef.typ=arraydef) and
  829. not is_special_array(left.resultdef)) or
  830. ((left.resultdef.typ=stringdef) and
  831. (tstringdef(left.resultdef).stringtype in [st_shortstring,st_longstring])) then
  832. make_not_regable(left,[ra_addr_regable]);
  833. case left.resultdef.typ of
  834. arraydef :
  835. begin
  836. { check type of the index value }
  837. if (compare_defs(right.resultdef,tarraydef(left.resultdef).rangedef,right.nodetype)=te_incompatible) then
  838. IncompatibleTypes(right.resultdef,tarraydef(left.resultdef).rangedef);
  839. if right.nodetype=rangen then
  840. resultdef:=left.resultdef
  841. else
  842. resultdef:=Tarraydef(left.resultdef).elementdef;
  843. { if we are range checking an open array or array of const, we }
  844. { need to load the high parameter. If the current procedure is }
  845. { nested inside the procedure to which the open array/of const }
  846. { was passed, then the high parameter must not be a regvar. }
  847. { So create a loadnode for the high parameter here and }
  848. { typecheck it, then the loadnode will make the high parameter }
  849. { not regable. Otherwise this would only happen inside pass_2, }
  850. { which is too late since by then the regvars are already }
  851. { assigned (pass_1 is also already too late, because then the }
  852. { regvars of the parent are also already assigned). }
  853. { webtbs/tw8975 }
  854. if (cs_check_range in current_settings.localswitches) and
  855. (is_open_array(left.resultdef) or
  856. is_array_of_const(left.resultdef)) then
  857. begin
  858. { expect to find the load node }
  859. if get_open_const_array(left).nodetype<>loadn then
  860. internalerror(2014040601);
  861. { cdecl functions don't have high() so we can not check the range }
  862. { (can't use current_procdef, since it may be a nested procedure) }
  863. if not(tprocdef(tparasymtable(tparavarsym(tloadnode(get_open_const_array(left)).symtableentry).owner).defowner).proccalloption in cdecl_pocalls) then
  864. begin
  865. { load_high_value_node already typechecks }
  866. hightree:=load_high_value_node(tparavarsym(tloadnode(get_open_const_array(left)).symtableentry));
  867. hightree.free;
  868. end;
  869. end;
  870. end;
  871. pointerdef :
  872. begin
  873. { are we accessing a pointer[], then convert the pointer to
  874. an array first, in FPC this is allowed for all pointers
  875. (except voidpointer) in delphi/tp7 it's only allowed for pchars. }
  876. if not is_voidpointer(left.resultdef) and
  877. (
  878. (cs_pointermath in current_settings.localswitches) or
  879. tpointerdef(left.resultdef).has_pointer_math or
  880. is_pchar(left.resultdef) or
  881. is_pwidechar(left.resultdef)
  882. ) then
  883. begin
  884. { convert pointer to array }
  885. htype:=carraydef.create_from_pointer(tpointerdef(left.resultdef).pointeddef);
  886. inserttypeconv(left,htype);
  887. if right.nodetype=rangen then
  888. resultdef:=htype
  889. else
  890. resultdef:=tarraydef(htype).elementdef;
  891. end
  892. else
  893. CGMessage(type_e_array_required);
  894. end;
  895. stringdef :
  896. begin
  897. case tstringdef(left.resultdef).stringtype of
  898. st_unicodestring,
  899. st_widestring :
  900. elementdef:=cwidechartype;
  901. st_ansistring :
  902. elementdef:=cansichartype;
  903. st_longstring :
  904. elementdef:=cansichartype;
  905. st_shortstring :
  906. elementdef:=cansichartype;
  907. else
  908. internalerror(2013112902);
  909. end;
  910. if right.nodetype=rangen then
  911. begin
  912. htype:=carraydef.create_from_pointer(elementdef);
  913. resultdef:=htype;
  914. end
  915. else
  916. begin
  917. { indexed access to 0 element is only allowed for shortstrings or if
  918. zero based strings is turned on }
  919. if (right.nodetype=ordconstn) and
  920. (Tordconstnode(right).value.svalue=0) and
  921. not is_shortstring(left.resultdef) and
  922. not(cs_zerobasedstrings in current_settings.localswitches) then
  923. CGMessage(cg_e_can_access_element_zero);
  924. resultdef:=elementdef;
  925. end;
  926. end;
  927. variantdef :
  928. resultdef:=cvarianttype;
  929. else
  930. CGMessage(type_e_array_required);
  931. end;
  932. end;
  933. procedure Tvecnode.mark_write;
  934. begin
  935. include(flags,nf_write);
  936. end;
  937. function tvecnode.pass_1 : tnode;
  938. begin
  939. result:=nil;
  940. firstpass(left);
  941. firstpass(right);
  942. if codegenerror then
  943. exit;
  944. if (nf_callunique in flags) and
  945. (is_ansistring(left.resultdef) or
  946. is_unicodestring(left.resultdef) or
  947. (is_widestring(left.resultdef) and not(tf_winlikewidestring in target_info.flags))) then
  948. begin
  949. left := ctypeconvnode.create_internal(ccallnode.createintern('fpc_'+tstringdef(left.resultdef).stringtypname+'_unique',
  950. ccallparanode.create(
  951. ctypeconvnode.create_internal(left,voidpointertype),nil)),
  952. left.resultdef);
  953. firstpass(left);
  954. { double resultdef passes somwhere else may cause this to be }
  955. { reset though :/ }
  956. exclude(flags,nf_callunique);
  957. end
  958. else if is_widestring(left.resultdef) and (tf_winlikewidestring in target_info.flags) then
  959. exclude(flags,nf_callunique);
  960. { a range node as array index can only appear in function calls, and
  961. those convert the range node into something else in
  962. tcallnode.gen_high_tree }
  963. if (right.nodetype=rangen) then
  964. CGMessagePos(right.fileinfo,parser_e_illegal_expression)
  965. else if (not is_packed_array(left.resultdef)) or
  966. ((tarraydef(left.resultdef).elepackedbitsize mod 8) = 0) then
  967. if left.expectloc=LOC_CREFERENCE then
  968. expectloc:=LOC_CREFERENCE
  969. else
  970. expectloc:=LOC_REFERENCE
  971. else
  972. if left.expectloc=LOC_CREFERENCE then
  973. expectloc:=LOC_CSUBSETREF
  974. else
  975. expectloc:=LOC_SUBSETREF;
  976. end;
  977. {*****************************************************************************
  978. TWITHNODE
  979. *****************************************************************************}
  980. constructor twithnode.create(l:tnode);
  981. begin
  982. inherited create(withn,l);
  983. fileinfo:=l.fileinfo;
  984. end;
  985. destructor twithnode.destroy;
  986. begin
  987. inherited destroy;
  988. end;
  989. constructor twithnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  990. begin
  991. inherited ppuload(t,ppufile);
  992. end;
  993. procedure twithnode.ppuwrite(ppufile:tcompilerppufile);
  994. begin
  995. inherited ppuwrite(ppufile);
  996. end;
  997. function twithnode.dogetcopy : tnode;
  998. var
  999. p : twithnode;
  1000. begin
  1001. p:=twithnode(inherited dogetcopy);
  1002. result:=p;
  1003. end;
  1004. function twithnode.pass_typecheck:tnode;
  1005. begin
  1006. result:=nil;
  1007. resultdef:=voidtype;
  1008. if assigned(left) then
  1009. typecheckpass(left);
  1010. end;
  1011. function twithnode.pass_1 : tnode;
  1012. begin
  1013. result:=nil;
  1014. expectloc:=LOC_VOID;
  1015. end;
  1016. function twithnode.docompare(p: tnode): boolean;
  1017. begin
  1018. docompare :=
  1019. inherited docompare(p);
  1020. end;
  1021. function is_big_untyped_addrnode(p: tnode): boolean;
  1022. begin
  1023. is_big_untyped_addrnode:=(p.nodetype=addrn) and
  1024. not (nf_typedaddr in p.flags) and (taddrnode(p).left.resultdef.size > 1);
  1025. end;
  1026. end.