nmem.pas 43 KB

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