nmem.pas 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  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. {$ifdef i386}
  503. if (hp.nodetype=loadn) and
  504. ((tloadnode(hp).symtableentry.typ=absolutevarsym) and
  505. tabsolutevarsym(tloadnode(hp).symtableentry).absseg) then
  506. begin
  507. if not(nf_typedaddr in flags) then
  508. resultdef:=voidnearfspointertype
  509. else
  510. resultdef:=tpointerdef.createx86(left.resultdef,x86pt_near_fs);
  511. end
  512. else
  513. {$endif i386}
  514. if (hp.nodetype=loadn) and
  515. (tloadnode(hp).symtableentry.typ=absolutevarsym) and
  516. {$ifdef i386}
  517. not(tabsolutevarsym(tloadnode(hp).symtableentry).absseg) and
  518. {$endif i386}
  519. (tabsolutevarsym(tloadnode(hp).symtableentry).abstyp=toaddr) then
  520. begin
  521. offset:=tabsolutevarsym(tloadnode(hp).symtableentry).addroffset;
  522. hp:=left;
  523. while assigned(hp)and(hp.nodetype=subscriptn) do
  524. begin
  525. hsym:=tsubscriptnode(hp).vs;
  526. if tabstractrecordsymtable(hsym.owner).is_packed then
  527. begin
  528. { can't calculate the address of a non-byte aligned field }
  529. if (hsym.fieldoffset mod 8)<>0 then
  530. exit;
  531. inc(offset,hsym.fieldoffset div 8)
  532. end
  533. else
  534. inc(offset,hsym.fieldoffset);
  535. hp:=tunarynode(hp).left;
  536. end;
  537. if nf_typedaddr in flags then
  538. result:=cpointerconstnode.create(offset,getpointerdef(left.resultdef))
  539. else
  540. result:=cpointerconstnode.create(offset,voidpointertype);
  541. exit;
  542. end
  543. {$ifdef i8086}
  544. else if (hp.nodetype=loadn) and
  545. (tloadnode(hp).symtableentry.typ=labelsym) then
  546. begin
  547. if current_settings.x86memorymodel in x86_far_code_models then
  548. resultdef:=voidfarpointertype
  549. else
  550. resultdef:=voidnearpointertype;
  551. end
  552. {$endif i8086}
  553. else if (nf_internal in flags) or
  554. valid_for_addr(left,true) then
  555. begin
  556. if not(nf_typedaddr in flags) then
  557. resultdef:=voidpointertype
  558. else
  559. resultdef:=getpointerdef(left.resultdef);
  560. end
  561. else
  562. CGMessage(type_e_variable_id_expected);
  563. end;
  564. if mark_read_written then
  565. begin
  566. { This is actually only "read", but treat it nevertheless as }
  567. { modified due to the possible use of pointers }
  568. { To avoid false positives regarding "uninitialised" }
  569. { warnings when using arrays, perform it in two steps }
  570. set_varstate(left,vs_written,[]);
  571. { vsf_must_be_valid so it doesn't get changed into }
  572. { vsf_referred_not_inited }
  573. set_varstate(left,vs_read,[vsf_must_be_valid]);
  574. end;
  575. end;
  576. function taddrnode.pass_1 : tnode;
  577. begin
  578. result:=nil;
  579. firstpass(left);
  580. if codegenerror then
  581. exit;
  582. { is this right for object of methods ?? }
  583. expectloc:=LOC_REGISTER;
  584. end;
  585. {*****************************************************************************
  586. TDEREFNODE
  587. *****************************************************************************}
  588. constructor tderefnode.create(l : tnode);
  589. begin
  590. inherited create(derefn,l);
  591. end;
  592. function tderefnode.pass_typecheck:tnode;
  593. begin
  594. result:=nil;
  595. typecheckpass(left);
  596. set_varstate(left,vs_read,[vsf_must_be_valid]);
  597. if codegenerror then
  598. exit;
  599. { tp procvar support }
  600. maybe_call_procvar(left,true);
  601. if left.resultdef.typ=pointerdef then
  602. resultdef:=tpointerdef(left.resultdef).pointeddef
  603. else
  604. CGMessage(parser_e_invalid_qualifier);
  605. end;
  606. procedure Tderefnode.mark_write;
  607. begin
  608. include(flags,nf_write);
  609. end;
  610. function tderefnode.pass_1 : tnode;
  611. begin
  612. result:=nil;
  613. firstpass(left);
  614. if codegenerror then
  615. exit;
  616. expectloc:=LOC_REFERENCE;
  617. end;
  618. {*****************************************************************************
  619. TSUBSCRIPTNODE
  620. *****************************************************************************}
  621. constructor tsubscriptnode.create(varsym : tsym;l : tnode);
  622. begin
  623. inherited create(subscriptn,l);
  624. { vs should be changed to tsym! }
  625. vs:=tfieldvarsym(varsym);
  626. end;
  627. constructor tsubscriptnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  628. begin
  629. inherited ppuload(t,ppufile);
  630. ppufile.getderef(vsderef);
  631. end;
  632. procedure tsubscriptnode.ppuwrite(ppufile:tcompilerppufile);
  633. begin
  634. inherited ppuwrite(ppufile);
  635. ppufile.putderef(vsderef);
  636. end;
  637. procedure tsubscriptnode.buildderefimpl;
  638. begin
  639. inherited buildderefimpl;
  640. vsderef.build(vs);
  641. end;
  642. procedure tsubscriptnode.derefimpl;
  643. begin
  644. inherited derefimpl;
  645. vs:=tfieldvarsym(vsderef.resolve);
  646. end;
  647. function tsubscriptnode.dogetcopy : tnode;
  648. var
  649. p : tsubscriptnode;
  650. begin
  651. p:=tsubscriptnode(inherited dogetcopy);
  652. p.vs:=vs;
  653. dogetcopy:=p;
  654. end;
  655. function tsubscriptnode.pass_typecheck:tnode;
  656. begin
  657. result:=nil;
  658. typecheckpass(left);
  659. { tp procvar support }
  660. maybe_call_procvar(left,true);
  661. resultdef:=vs.vardef;
  662. // don't put records from which we load float fields
  663. // in integer registers
  664. if (left.resultdef.typ=recorddef) and
  665. (resultdef.typ=floatdef) then
  666. make_not_regable(left,[ra_addr_regable]);
  667. end;
  668. procedure Tsubscriptnode.mark_write;
  669. begin
  670. include(flags,nf_write);
  671. end;
  672. function tsubscriptnode.pass_1 : tnode;
  673. begin
  674. result:=nil;
  675. firstpass(left);
  676. if codegenerror then
  677. exit;
  678. { several object types must be dereferenced implicitly }
  679. if is_implicit_pointer_object_type(left.resultdef) then
  680. expectloc:=LOC_REFERENCE
  681. else
  682. begin
  683. case left.expectloc of
  684. LOC_REGISTER,
  685. LOC_SUBSETREG:
  686. // can happen for function results on win32 and darwin/x86
  687. if (left.resultdef.size > sizeof(pint)) then
  688. expectloc:=LOC_REFERENCE
  689. else
  690. expectloc:=LOC_SUBSETREG;
  691. LOC_CREGISTER,
  692. LOC_CSUBSETREG:
  693. expectloc:=LOC_CSUBSETREG;
  694. LOC_REFERENCE,
  695. LOC_CREFERENCE:
  696. expectloc:=left.expectloc;
  697. else internalerror(20060521);
  698. end;
  699. end;
  700. end;
  701. function tsubscriptnode.docompare(p: tnode): boolean;
  702. begin
  703. docompare :=
  704. inherited docompare(p) and
  705. (vs = tsubscriptnode(p).vs);
  706. end;
  707. {*****************************************************************************
  708. TVECNODE
  709. *****************************************************************************}
  710. constructor tvecnode.create(l,r : tnode);
  711. begin
  712. inherited create(vecn,l,r);
  713. end;
  714. function tvecnode.pass_typecheck:tnode;
  715. var
  716. hightree: tnode;
  717. htype,elementdef : tdef;
  718. newordtyp: tordtype;
  719. valid : boolean;
  720. begin
  721. result:=nil;
  722. typecheckpass(left);
  723. typecheckpass(right);
  724. { implicitly convert stringconstant to stringdef,
  725. see tbs/tb0476.pp for a test }
  726. if (left.nodetype=stringconstn) and
  727. (tstringconstnode(left).cst_type=cst_conststring) then
  728. begin
  729. if tstringconstnode(left).len>255 then
  730. inserttypeconv(left,getansistringdef)
  731. else
  732. inserttypeconv(left,cshortstringtype);
  733. end;
  734. { In p[1] p is always valid, it is not possible to
  735. declared a shortstring or normal array that has
  736. undefined number of elements. Dynamic array and
  737. ansi/widestring needs to be valid }
  738. valid:=is_dynamic_array(left.resultdef) or
  739. is_ansistring(left.resultdef) or
  740. is_wide_or_unicode_string(left.resultdef) or
  741. { implicit pointer dereference -> pointer is read }
  742. (left.resultdef.typ = pointerdef);
  743. if valid then
  744. set_varstate(left,vs_read,[vsf_must_be_valid]);
  745. {
  746. A vecn is, just like a loadn, always part of an expression with its
  747. own read/write and must_be_valid semantics. Therefore we don't have
  748. to do anything else here, just like for loadn's
  749. }
  750. set_varstate(right,vs_read,[vsf_must_be_valid]);
  751. if codegenerror then
  752. exit;
  753. { maybe type conversion for the index value, but
  754. do not convert range nodes }
  755. if (right.nodetype<>rangen) then
  756. case left.resultdef.typ of
  757. arraydef:
  758. begin
  759. htype:=Tarraydef(left.resultdef).rangedef;
  760. if ado_isvariant in Tarraydef(left.resultdef).arrayoptions then
  761. {Variant arrays are a special array, can have negative indexes and would therefore
  762. need s32bit. However, they should not appear in a vecn, as they are handled in
  763. handle_variantarray in pexpr.pas. Therefore, encountering a variant array is an
  764. internal error... }
  765. internalerror(200707031)
  766. else if is_special_array(left.resultdef) then
  767. {Arrays without a high bound (dynamic arrays, open arrays) are zero based,
  768. convert indexes into these arrays to aword.}
  769. inserttypeconv(right,uinttype)
  770. { note: <> rather than </>, because indexing e.g. an array 0..0
  771. must not result in truncating the indexing value from 2/4/8
  772. bytes to 1 byte (with range checking off, the full index
  773. value must be used) }
  774. else if (htype.typ=enumdef) and
  775. (right.resultdef.typ=enumdef) and
  776. (tenumdef(htype).basedef=tenumdef(right.resultdef).basedef) and
  777. ((tarraydef(left.resultdef).lowrange<>tenumdef(htype).min) or
  778. (tarraydef(left.resultdef).highrange<>tenumdef(htype).max)) then
  779. {Convert array indexes to low_bound..high_bound.}
  780. inserttypeconv(right,tenumdef.create_subrange(tenumdef(right.resultdef),
  781. asizeint(Tarraydef(left.resultdef).lowrange),
  782. asizeint(Tarraydef(left.resultdef).highrange)
  783. ))
  784. else if (htype.typ=orddef) and
  785. { right can also be a variant or another type with
  786. overloaded assignment }
  787. (right.resultdef.typ=orddef) and
  788. { don't try to create boolean types with custom ranges }
  789. not is_boolean(right.resultdef) and
  790. { ordtype determines the size of the loaded value -> make
  791. sure we don't truncate }
  792. ((Torddef(right.resultdef).ordtype<>torddef(htype).ordtype) or
  793. (tarraydef(left.resultdef).lowrange<>torddef(htype).low) or
  794. (tarraydef(left.resultdef).highrange<>torddef(htype).high)) then
  795. {Convert array indexes to low_bound..high_bound.}
  796. begin
  797. if (right.resultdef.typ=orddef)
  798. {$ifndef cpu64bitaddr}
  799. { do truncate 64 bit values on 32 bit cpus, since
  800. a) the arrays cannot be > 32 bit anyway
  801. b) their code generators can't directly handle 64 bit
  802. loads
  803. }
  804. and not is_64bit(right.resultdef)
  805. {$endif not cpu64bitaddr}
  806. then
  807. newordtyp:=Torddef(right.resultdef).ordtype
  808. else
  809. newordtyp:=torddef(ptrsinttype).ordtype;
  810. inserttypeconv(right,Torddef.create(newordtyp,
  811. int64(Tarraydef(left.resultdef).lowrange),
  812. int64(Tarraydef(left.resultdef).highrange)
  813. ))
  814. end
  815. else
  816. inserttypeconv(right,htype)
  817. end;
  818. stringdef:
  819. if is_open_string(left.resultdef) then
  820. inserttypeconv(right,u8inttype)
  821. else if is_shortstring(left.resultdef) then
  822. {Convert shortstring indexes to 0..length.}
  823. inserttypeconv(right,Torddef.create(u8bit,0,int64(Tstringdef(left.resultdef).len)))
  824. else
  825. {Convert indexes into dynamically allocated strings to aword.}
  826. inserttypeconv(right,uinttype);
  827. else
  828. {Others, i.e. pointer indexes to aint.}
  829. inserttypeconv(right,sinttype);
  830. end;
  831. { although we never put regular arrays or shortstrings in registers,
  832. it's possible that another type was typecasted to a small record
  833. that has a field of one of these types -> in that case the record
  834. can't be a regvar either }
  835. if ((left.resultdef.typ=arraydef) and
  836. not is_special_array(left.resultdef)) or
  837. ((left.resultdef.typ=stringdef) and
  838. (tstringdef(left.resultdef).stringtype in [st_shortstring,st_longstring])) then
  839. make_not_regable(left,[ra_addr_regable]);
  840. case left.resultdef.typ of
  841. arraydef :
  842. begin
  843. { check type of the index value }
  844. if (compare_defs(right.resultdef,tarraydef(left.resultdef).rangedef,right.nodetype)=te_incompatible) then
  845. IncompatibleTypes(right.resultdef,tarraydef(left.resultdef).rangedef);
  846. if right.nodetype=rangen then
  847. resultdef:=left.resultdef
  848. else
  849. resultdef:=Tarraydef(left.resultdef).elementdef;
  850. { if we are range checking an open array or array of const, we }
  851. { need to load the high parameter. If the current procedure is }
  852. { nested inside the procedure to which the open array/of const }
  853. { was passed, then the high parameter must not be a regvar. }
  854. { So create a loadnode for the high parameter here and }
  855. { typecheck it, then the loadnode will make the high parameter }
  856. { not regable. Otherwise this would only happen inside pass_2, }
  857. { which is too late since by then the regvars are already }
  858. { assigned (pass_1 is also already too late, because then the }
  859. { regvars of the parent are also already assigned). }
  860. { webtbs/tw8975 }
  861. if (cs_check_range in current_settings.localswitches) and
  862. (is_open_array(left.resultdef) or
  863. is_array_of_const(left.resultdef)) and
  864. { cdecl functions don't have high() so we can not check the range }
  865. { (can't use current_procdef, since it may be a nested procedure) }
  866. not(tprocdef(tparasymtable(tparavarsym(tloadnode(left).symtableentry).owner).defowner).proccalloption in cdecl_pocalls) then
  867. begin
  868. { load_high_value_node already typechecks }
  869. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  870. hightree.free;
  871. end;
  872. end;
  873. pointerdef :
  874. begin
  875. { are we accessing a pointer[], then convert the pointer to
  876. an array first, in FPC this is allowed for all pointers
  877. (except voidpointer) in delphi/tp7 it's only allowed for pchars. }
  878. if not is_voidpointer(left.resultdef) and
  879. (
  880. (cs_pointermath in current_settings.localswitches) or
  881. tpointerdef(left.resultdef).has_pointer_math or
  882. is_pchar(left.resultdef) or
  883. is_pwidechar(left.resultdef)
  884. ) then
  885. begin
  886. { convert pointer to array }
  887. htype:=tarraydef.create_from_pointer(tpointerdef(left.resultdef).pointeddef);
  888. inserttypeconv(left,htype);
  889. if right.nodetype=rangen then
  890. resultdef:=htype
  891. else
  892. resultdef:=tarraydef(htype).elementdef;
  893. end
  894. else
  895. CGMessage(type_e_array_required);
  896. end;
  897. stringdef :
  898. begin
  899. case tstringdef(left.resultdef).stringtype of
  900. st_unicodestring,
  901. st_widestring :
  902. elementdef:=cwidechartype;
  903. st_ansistring :
  904. elementdef:=cansichartype;
  905. st_longstring :
  906. elementdef:=cansichartype;
  907. st_shortstring :
  908. elementdef:=cansichartype;
  909. end;
  910. if right.nodetype=rangen then
  911. begin
  912. htype:=Tarraydef.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.