nmem.pas 42 KB

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