nmem.pas 42 KB

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