nmem.pas 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375
  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. tloadparentfpkind = (
  39. { as parameter to a nested routine (current routine's frame) }
  40. lpf_forpara,
  41. { to load a local from a parent routine in the current nested routine
  42. (some parent routine's frame) }
  43. lpf_forload
  44. );
  45. tloadparentfpnode = class(tunarynode)
  46. parentpd : tprocdef;
  47. parentpdderef : tderef;
  48. kind: tloadparentfpkind;
  49. constructor create(pd: tprocdef; fpkind: tloadparentfpkind);virtual;
  50. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  51. procedure ppuwrite(ppufile:tcompilerppufile);override;
  52. procedure buildderefimpl;override;
  53. procedure derefimpl;override;
  54. function pass_1 : tnode;override;
  55. function pass_typecheck:tnode;override;
  56. function docompare(p: tnode): boolean; override;
  57. function dogetcopy : tnode;override;
  58. end;
  59. tloadparentfpnodeclass = class of tloadparentfpnode;
  60. taddrnodeflag = (
  61. { generated by the Ofs() internal function }
  62. anf_ofs,
  63. anf_typedaddr
  64. );
  65. taddrnodeflags = set of taddrnodeflag;
  66. taddrnode = class(tunarynode)
  67. getprocvardef : tprocvardef;
  68. getprocvardefderef : tderef;
  69. addrnodeflags : taddrnodeflags;
  70. constructor create(l : tnode);virtual;
  71. constructor create_internal(l : tnode); virtual;
  72. constructor create_internal_nomark(l : tnode); virtual;
  73. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  74. procedure ppuwrite(ppufile:tcompilerppufile);override;
  75. procedure mark_write;override;
  76. procedure buildderefimpl;override;
  77. procedure derefimpl;override;
  78. procedure printnodeinfo(var t: text); override;
  79. {$ifdef DEBUG_NODE_XML}
  80. procedure XMLPrintNodeInfo(var T: Text); override;
  81. {$endif DEBUG_NODE_XML}
  82. function docompare(p: tnode): boolean; override;
  83. function dogetcopy : tnode;override;
  84. function pass_1 : tnode;override;
  85. function pass_typecheck:tnode;override;
  86. function simplify(forinline : boolean) : tnode; override;
  87. protected
  88. mark_read_written: boolean;
  89. procedure set_labelsym_resultdef; virtual;
  90. function typecheck_non_proc(realsource: tnode; out res: tnode): boolean; virtual;
  91. end;
  92. taddrnodeclass = class of taddrnode;
  93. tderefnode = class(tunarynode)
  94. constructor create(l : tnode);virtual;
  95. function pass_1 : tnode;override;
  96. function pass_typecheck:tnode;override;
  97. procedure mark_write;override;
  98. end;
  99. tderefnodeclass = class of tderefnode;
  100. tsubscriptnode = class(tunarynode)
  101. vs : tfieldvarsym;
  102. vsderef : tderef;
  103. constructor create(varsym : tsym;l : tnode);virtual;
  104. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  105. procedure ppuwrite(ppufile:tcompilerppufile);override;
  106. procedure buildderefimpl;override;
  107. procedure derefimpl;override;
  108. function dogetcopy : tnode;override;
  109. function pass_1 : tnode;override;
  110. function docompare(p: tnode): boolean; override;
  111. function pass_typecheck:tnode;override;
  112. procedure mark_write;override;
  113. {$ifdef DEBUG_NODE_XML}
  114. procedure XMLPrintNodeData(var T: Text); override;
  115. {$endif DEBUG_NODE_XML}
  116. end;
  117. tsubscriptnodeclass = class of tsubscriptnode;
  118. tvecnode = class(tbinarynode)
  119. protected
  120. function first_arraydef: tnode; virtual;
  121. function gen_array_rangecheck: tnode; virtual;
  122. public
  123. constructor create(l,r : tnode);virtual;
  124. function pass_1 : tnode;override;
  125. function pass_typecheck:tnode;override;
  126. procedure mark_write;override;
  127. {$ifdef DEBUG_NODE_XML}
  128. procedure XMLPrintNodeData(var T: Text); override;
  129. {$endif DEBUG_NODE_XML}
  130. end;
  131. tvecnodeclass = class of tvecnode;
  132. var
  133. cloadvmtaddrnode : tloadvmtaddrnodeclass= tloadvmtaddrnode;
  134. caddrnode : taddrnodeclass= taddrnode;
  135. cderefnode : tderefnodeclass= tderefnode;
  136. csubscriptnode : tsubscriptnodeclass= tsubscriptnode;
  137. cvecnode : tvecnodeclass= tvecnode;
  138. cloadparentfpnode : tloadparentfpnodeclass = tloadparentfpnode;
  139. function is_big_untyped_addrnode(p: tnode): boolean;
  140. implementation
  141. uses
  142. globtype,systems,constexp,
  143. cutils,verbose,globals,ppu,
  144. symconst,defutil,defcmp,
  145. nadd,nbas,nflw,nutils,objcutil,
  146. wpobase,
  147. {$ifdef i8086}
  148. cpuinfo,
  149. {$endif i8086}
  150. htypechk,pass_1,ncal,nld,ncon,ncnv,cgbase,procinfo
  151. ;
  152. {*****************************************************************************
  153. TLOADVMTADDRNODE
  154. *****************************************************************************}
  155. constructor tloadvmtaddrnode.create(l : tnode);
  156. begin
  157. inherited create(loadvmtaddrn,l);
  158. end;
  159. function tloadvmtaddrnode.pass_typecheck:tnode;
  160. var
  161. defaultresultdef : boolean;
  162. begin
  163. result:=nil;
  164. typecheckpass(left);
  165. if codegenerror then
  166. exit;
  167. case left.resultdef.typ of
  168. classrefdef :
  169. resultdef:=left.resultdef;
  170. recorddef,
  171. objectdef:
  172. begin
  173. if (left.resultdef.typ=objectdef) or
  174. ((target_info.system in systems_jvm) and
  175. (left.resultdef.typ=recorddef)) then
  176. begin
  177. { access to the classtype while specializing? }
  178. if tstoreddef(left.resultdef).is_generic then
  179. begin
  180. defaultresultdef:=true;
  181. if assigned(current_structdef) then
  182. begin
  183. if assigned(current_structdef.genericdef) then
  184. if current_structdef.genericdef=left.resultdef then
  185. begin
  186. resultdef:=cclassrefdef.create(current_structdef);
  187. defaultresultdef:=false;
  188. end
  189. else
  190. CGMessage(parser_e_cant_create_generics_of_this_type);
  191. end
  192. else
  193. message(parser_e_cant_create_generics_of_this_type);
  194. if defaultresultdef then
  195. resultdef:=cclassrefdef.create(left.resultdef);
  196. end
  197. else
  198. resultdef:=cclassrefdef.create(left.resultdef);
  199. end
  200. else
  201. CGMessage(parser_e_pointer_to_class_expected);
  202. end
  203. else
  204. CGMessage(parser_e_pointer_to_class_expected);
  205. end;
  206. end;
  207. function tloadvmtaddrnode.docompare(p: tnode): boolean;
  208. begin
  209. result:=inherited docompare(p);
  210. if result then
  211. result:=forcall=tloadvmtaddrnode(p).forcall;
  212. end;
  213. function tloadvmtaddrnode.dogetcopy: tnode;
  214. begin
  215. result:=inherited dogetcopy;
  216. tloadvmtaddrnode(result).forcall:=forcall;
  217. end;
  218. function tloadvmtaddrnode.pass_1 : tnode;
  219. var
  220. vs: tsym;
  221. begin
  222. result:=nil;
  223. expectloc:=LOC_REGISTER;
  224. if left.nodetype<>typen then
  225. begin
  226. if (is_objc_class_or_protocol(left.resultdef) or
  227. is_objcclassref(left.resultdef)) then
  228. begin
  229. { on non-fragile ABI platforms, the ISA pointer may be opaque
  230. and we must call Object_getClass to obtain the real ISA
  231. pointer }
  232. if target_info.system in systems_objc_nfabi then
  233. begin
  234. result:=ccallnode.createinternfromunit('OBJC','OBJECT_GETCLASS',ccallparanode.create(left,nil));
  235. inserttypeconv_explicit(result,resultdef);
  236. end
  237. else
  238. result:=objcloadbasefield(left,'ISA');
  239. end
  240. else
  241. result:=ctypeconvnode.create_internal(load_vmt_for_self_node(left),resultdef);
  242. { reused }
  243. left:=nil;
  244. end
  245. else if not is_objcclass(left.resultdef) and
  246. not is_objcclassref(left.resultdef) then
  247. begin
  248. if not(nf_ignore_for_wpo in flags) and
  249. (not assigned(current_procinfo) or
  250. (po_inline in current_procinfo.procdef.procoptions) or
  251. wpoinfomanager.symbol_live(current_procinfo.procdef.mangledname)) then
  252. begin
  253. { keep track of which classes might be instantiated via a classrefdef }
  254. if (left.resultdef.typ=classrefdef) then
  255. tobjectdef(tclassrefdef(left.resultdef).pointeddef).register_maybe_created_object_type
  256. else if (left.resultdef.typ=objectdef) then
  257. tobjectdef(left.resultdef).register_maybe_created_object_type
  258. end
  259. end
  260. else if is_objcclass(left.resultdef) and
  261. not(forcall) then
  262. begin
  263. { call "class" method (= "classclass" in FPC), because otherwise
  264. we may use the class information before it has been
  265. initialized }
  266. vs:=search_struct_member(tobjectdef(left.resultdef),'CLASSCLASS');
  267. if not assigned(vs) or
  268. (vs.typ<>procsym) then
  269. internalerror(2011080601);
  270. { can't reuse "self", because it will be freed when we return }
  271. result:=ccallnode.create(nil,tprocsym(vs),vs.owner,self.getcopy,[],nil);
  272. end;
  273. end;
  274. {*****************************************************************************
  275. TLOADPARENTFPNODE
  276. *****************************************************************************}
  277. constructor tloadparentfpnode.create(pd: tprocdef; fpkind: tloadparentfpkind);
  278. begin
  279. inherited create(loadparentfpn,nil);
  280. if not assigned(pd) then
  281. internalerror(200309288);
  282. if (pd.parast.symtablelevel>current_procinfo.procdef.parast.symtablelevel) then
  283. internalerror(200309284);
  284. parentpd:=pd;
  285. kind:=fpkind;
  286. end;
  287. constructor tloadparentfpnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  288. begin
  289. inherited ppuload(t,ppufile);
  290. ppufile.getderef(parentpdderef);
  291. kind:=tloadparentfpkind(ppufile.getbyte);
  292. end;
  293. procedure tloadparentfpnode.ppuwrite(ppufile:tcompilerppufile);
  294. begin
  295. inherited ppuwrite(ppufile);
  296. ppufile.putderef(parentpdderef);
  297. ppufile.putbyte(byte(kind));
  298. end;
  299. procedure tloadparentfpnode.buildderefimpl;
  300. begin
  301. inherited buildderefimpl;
  302. parentpdderef.build(parentpd);
  303. end;
  304. procedure tloadparentfpnode.derefimpl;
  305. begin
  306. inherited derefimpl;
  307. parentpd:=tprocdef(parentpdderef.resolve);
  308. end;
  309. function tloadparentfpnode.docompare(p: tnode): boolean;
  310. begin
  311. result:=
  312. inherited docompare(p) and
  313. (tloadparentfpnode(p).parentpd=parentpd) and
  314. (tloadparentfpnode(p).kind=kind);
  315. end;
  316. function tloadparentfpnode.dogetcopy : tnode;
  317. var
  318. p : tloadparentfpnode;
  319. begin
  320. p:=tloadparentfpnode(inherited dogetcopy);
  321. p.parentpd:=parentpd;
  322. p.kind:=kind;
  323. dogetcopy:=p;
  324. end;
  325. function tloadparentfpnode.pass_typecheck:tnode;
  326. {$ifdef dummy}
  327. var
  328. currpi : tprocinfo;
  329. hsym : tparavarsym;
  330. {$endif dummy}
  331. begin
  332. result:=nil;
  333. resultdef:=parentfpvoidpointertype;
  334. {$ifdef dummy}
  335. { currently parentfps are never loaded in registers (FK) }
  336. if (current_procinfo.procdef.parast.symtablelevel<>parentpd.parast.symtablelevel) then
  337. begin
  338. currpi:=current_procinfo;
  339. { walk parents }
  340. while (currpi.procdef.owner.symtablelevel>parentpd.parast.symtablelevel) do
  341. begin
  342. currpi:=currpi.parent;
  343. if not assigned(currpi) then
  344. internalerror(2005040602);
  345. hsym:=tparavarsym(currpi.procdef.parast.Find('parentfp'));
  346. if not assigned(hsym) then
  347. internalerror(2005040601);
  348. hsym.varregable:=vr_none;
  349. end;
  350. end;
  351. {$endif dummy}
  352. end;
  353. function tloadparentfpnode.pass_1 : tnode;
  354. begin
  355. result:=nil;
  356. expectloc:=LOC_REGISTER;
  357. end;
  358. {*****************************************************************************
  359. TADDRNODE
  360. *****************************************************************************}
  361. constructor taddrnode.create(l : tnode);
  362. begin
  363. inherited create(addrn,l);
  364. getprocvardef:=nil;
  365. addrnodeflags:=[];
  366. mark_read_written := true;
  367. end;
  368. constructor taddrnode.create_internal(l : tnode);
  369. begin
  370. self.create(l);
  371. include(flags,nf_internal);
  372. end;
  373. constructor taddrnode.create_internal_nomark(l : tnode);
  374. begin
  375. self.create_internal(l);
  376. mark_read_written := false;
  377. end;
  378. constructor taddrnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  379. begin
  380. inherited ppuload(t,ppufile);
  381. ppufile.getderef(getprocvardefderef);
  382. ppufile.getset(tppuset1(addrnodeflags));
  383. end;
  384. procedure taddrnode.ppuwrite(ppufile:tcompilerppufile);
  385. begin
  386. inherited ppuwrite(ppufile);
  387. ppufile.putderef(getprocvardefderef);
  388. ppufile.putset(tppuset1(addrnodeflags));
  389. end;
  390. procedure Taddrnode.mark_write;
  391. begin
  392. {@procvar:=nil is legal in Delphi mode.}
  393. left.mark_write;
  394. end;
  395. procedure taddrnode.buildderefimpl;
  396. begin
  397. inherited buildderefimpl;
  398. getprocvardefderef.build(getprocvardef);
  399. end;
  400. procedure taddrnode.derefimpl;
  401. begin
  402. inherited derefimpl;
  403. getprocvardef:=tprocvardef(getprocvardefderef.resolve);
  404. end;
  405. procedure taddrnode.printnodeinfo(var t: text);
  406. var
  407. first: Boolean;
  408. i: taddrnodeflag;
  409. begin
  410. inherited printnodeinfo(t);
  411. write(t,', addrnodeflags = [');
  412. first:=true;
  413. for i:=low(taddrnodeflag) to high(taddrnodeflag) do
  414. if i in addrnodeflags then
  415. begin
  416. if not first then
  417. write(t,',')
  418. else
  419. first:=false;
  420. write(t,i);
  421. end;
  422. write(t,']');
  423. end;
  424. {$ifdef DEBUG_NODE_XML}
  425. procedure TAddrNode.XMLPrintNodeInfo(var T: Text);
  426. var
  427. First: Boolean;
  428. i: TAddrNodeFlag;
  429. begin
  430. inherited XMLPrintNodeInfo(t);
  431. First := True;
  432. for i := Low(TAddrNodeFlag) to High(TAddrNodeFlag) do
  433. if i in addrnodeflags then
  434. begin
  435. if First then
  436. begin
  437. Write(T, ' addrnodeflags="', i);
  438. First := False;
  439. end
  440. else
  441. Write(T, ',', i);
  442. end;
  443. if not First then
  444. Write(T, '"');
  445. end;
  446. {$endif DEBUG_NODE_XML}
  447. function taddrnode.docompare(p: tnode): boolean;
  448. begin
  449. result:=
  450. inherited docompare(p) and
  451. (taddrnode(p).getprocvardef=getprocvardef) and
  452. (taddrnode(p).addrnodeflags=addrnodeflags);
  453. end;
  454. function taddrnode.dogetcopy : tnode;
  455. var
  456. p : taddrnode;
  457. begin
  458. p:=taddrnode(inherited dogetcopy);
  459. p.getprocvardef:=getprocvardef;
  460. p.addrnodeflags:=addrnodeflags;
  461. dogetcopy:=p;
  462. end;
  463. function taddrnode.pass_typecheck:tnode;
  464. procedure check_mark_read_written;
  465. begin
  466. if mark_read_written then
  467. begin
  468. { This is actually only "read", but treat it nevertheless as
  469. modified due to the possible use of pointers
  470. To avoid false positives regarding "uninitialised"
  471. warnings when using arrays, perform it in two steps }
  472. set_varstate(left,vs_written,[]);
  473. { vsf_must_be_valid so it doesn't get changed into
  474. vsf_referred_not_inited }
  475. set_varstate(left,vs_read,[vsf_must_be_valid]);
  476. end;
  477. end;
  478. var
  479. hp : tnode;
  480. hsym : tfieldvarsym;
  481. isprocvar,need_conv_to_voidptr: boolean;
  482. procpointertype: tdef;
  483. begin
  484. result:=nil;
  485. typecheckpass(left);
  486. if codegenerror then
  487. exit;
  488. make_not_regable(left,[ra_addr_regable,ra_addr_taken]);
  489. { don't allow constants, for internal use we also
  490. allow taking the address of strings and sets }
  491. if is_constnode(left) and
  492. not(
  493. (nf_internal in flags) and
  494. (left.nodetype in [stringconstn,setconstn])
  495. ) then
  496. begin
  497. CGMessagePos(left.fileinfo,type_e_no_addr_of_constant);
  498. exit;
  499. end;
  500. { Handle @proc special, also @procvar in tp-mode needs
  501. special handling }
  502. if (left.resultdef.typ=procdef) or
  503. (
  504. { in case of nf_internal, follow the normal FPC semantics so that
  505. we can easily get the actual address of a procvar }
  506. not(nf_internal in flags) and
  507. (left.resultdef.typ=procvardef) and
  508. ((m_tp_procvar in current_settings.modeswitches) or
  509. (m_mac_procvar in current_settings.modeswitches))
  510. ) then
  511. begin
  512. isprocvar:=(left.resultdef.typ=procvardef);
  513. need_conv_to_voidptr:=
  514. (m_tp_procvar in current_settings.modeswitches) or
  515. (m_mac_procvar in current_settings.modeswitches);
  516. if not isprocvar then
  517. begin
  518. left:=ctypeconvnode.create_proc_to_procvar(left);
  519. if need_conv_to_voidptr then
  520. include(ttypeconvnode(left).convnodeflags,tcnf_proc_2_procvar_2_voidpointer);
  521. if anf_ofs in addrnodeflags then
  522. include(ttypeconvnode(left).convnodeflags,tcnf_proc_2_procvar_get_offset_only);
  523. left.fileinfo:=fileinfo;
  524. typecheckpass(left);
  525. end;
  526. { In tp procvar mode the result is always a voidpointer. Insert
  527. a typeconversion to voidpointer. For methodpointers we need
  528. to load the proc field }
  529. if need_conv_to_voidptr then
  530. begin
  531. if tabstractprocdef(left.resultdef).is_addressonly then
  532. begin
  533. if anf_ofs in addrnodeflags then
  534. result:=ctypeconvnode.create_internal(left,tabstractprocdef(left.resultdef).ofs_address_type)
  535. else
  536. result:=ctypeconvnode.create_internal(left,voidcodepointertype);
  537. include(result.flags,nf_load_procvar);
  538. left:=nil;
  539. end
  540. else
  541. begin
  542. { For procvars and for nested routines we need to return
  543. the proc field of the methodpointer }
  544. if isprocvar or
  545. is_nested_pd(tabstractprocdef(left.resultdef)) then
  546. begin
  547. if tabstractprocdef(left.resultdef).is_methodpointer then
  548. procpointertype:=methodpointertype
  549. else
  550. procpointertype:=nestedprocpointertype;
  551. { find proc field in methodpointer record }
  552. hsym:=tfieldvarsym(trecorddef(procpointertype).symtable.Find('proc'));
  553. if not assigned(hsym) then
  554. internalerror(200412041);
  555. { Load tmehodpointer(left).proc }
  556. result:=csubscriptnode.create(
  557. hsym,
  558. ctypeconvnode.create_internal(left,procpointertype));
  559. left:=nil;
  560. end
  561. else
  562. CGMessage(type_e_variable_id_expected);
  563. end;
  564. end
  565. else
  566. begin
  567. check_mark_read_written;
  568. { Return the typeconvn only }
  569. result:=left;
  570. left:=nil;
  571. exit;
  572. end;
  573. end
  574. else
  575. begin
  576. hp:=left;
  577. while assigned(hp) and (hp.nodetype in [typeconvn,derefn,subscriptn]) do
  578. hp:=tunarynode(hp).left;
  579. if not assigned(hp) then
  580. internalerror(200412042);
  581. if typecheck_non_proc(hp,result) then
  582. begin
  583. if assigned(result) then
  584. exit;
  585. end
  586. else
  587. CGMessage(type_e_variable_id_expected);
  588. end;
  589. check_mark_read_written;
  590. if not(assigned(result)) then
  591. result:=simplify(false);
  592. end;
  593. function taddrnode.simplify(forinline : boolean) : tnode;
  594. var
  595. hsym : tfieldvarsym;
  596. begin
  597. result:=nil;
  598. if ((left.nodetype=subscriptn) and
  599. (tsubscriptnode(left).left.nodetype=derefn) and
  600. (tsubscriptnode(left).left.resultdef.typ=recorddef) and
  601. (tderefnode(tsubscriptnode(left).left).left.nodetype=niln)) or
  602. ((left.nodetype=subscriptn) and
  603. (tsubscriptnode(left).left.nodetype=typeconvn) and
  604. (tsubscriptnode(left).left.resultdef.typ=recorddef) and
  605. (ttypeconvnode(tsubscriptnode(left).left).left.nodetype=derefn) and
  606. (tderefnode(ttypeconvnode(tsubscriptnode(left).left).left).left.nodetype=niln)) then
  607. begin
  608. hsym:=tsubscriptnode(left).vs;
  609. if tabstractrecordsymtable(hsym.owner).is_packed then
  610. result:=cpointerconstnode.create(hsym.fieldoffset div 8,resultdef)
  611. else
  612. result:=cpointerconstnode.create(hsym.fieldoffset,resultdef);
  613. end;
  614. end;
  615. procedure taddrnode.set_labelsym_resultdef;
  616. begin
  617. resultdef:=voidcodepointertype;
  618. end;
  619. function taddrnode.typecheck_non_proc(realsource: tnode; out res: tnode): boolean;
  620. var
  621. hp : tnode;
  622. hsym : tfieldvarsym;
  623. offset: asizeint;
  624. begin
  625. result:=false;
  626. res:=nil;
  627. if (realsource.nodetype=loadn) and
  628. (tloadnode(realsource).symtableentry.typ=labelsym) then
  629. begin
  630. set_labelsym_resultdef;
  631. result:=true;
  632. end
  633. else if (realsource.nodetype=loadn) and
  634. (tloadnode(realsource).symtableentry.typ=absolutevarsym) and
  635. (tabsolutevarsym(tloadnode(realsource).symtableentry).abstyp=toaddr) then
  636. begin
  637. offset:=tabsolutevarsym(tloadnode(realsource).symtableentry).addroffset;
  638. hp:=left;
  639. while assigned(hp)and(hp.nodetype=subscriptn) do
  640. begin
  641. hsym:=tsubscriptnode(hp).vs;
  642. if tabstractrecordsymtable(hsym.owner).is_packed then
  643. begin
  644. { can't calculate the address of a non-byte aligned field }
  645. if (hsym.fieldoffset mod 8)<>0 then
  646. begin
  647. CGMessagePos(hp.fileinfo,parser_e_packed_element_no_var_addr);
  648. exit
  649. end;
  650. inc(offset,hsym.fieldoffset div 8)
  651. end
  652. else
  653. inc(offset,hsym.fieldoffset);
  654. hp:=tunarynode(hp).left;
  655. end;
  656. if anf_typedaddr in addrnodeflags then
  657. res:=cpointerconstnode.create(offset,cpointerdef.getreusable(left.resultdef))
  658. else
  659. res:=cpointerconstnode.create(offset,voidpointertype);
  660. result:=true;
  661. end
  662. else if (nf_internal in flags) or
  663. valid_for_addr(left,true) then
  664. begin
  665. if not(anf_typedaddr in addrnodeflags) then
  666. resultdef:=voidpointertype
  667. else
  668. resultdef:=cpointerdef.getreusable(left.resultdef);
  669. result:=true;
  670. end
  671. end;
  672. function taddrnode.pass_1 : tnode;
  673. begin
  674. result:=nil;
  675. firstpass(left);
  676. if codegenerror then
  677. exit;
  678. { is this right for object of methods ?? }
  679. expectloc:=LOC_REGISTER;
  680. end;
  681. {*****************************************************************************
  682. TDEREFNODE
  683. *****************************************************************************}
  684. constructor tderefnode.create(l : tnode);
  685. begin
  686. inherited create(derefn,l);
  687. end;
  688. function tderefnode.pass_typecheck:tnode;
  689. begin
  690. result:=nil;
  691. typecheckpass(left);
  692. set_varstate(left,vs_read,[vsf_must_be_valid]);
  693. if codegenerror then
  694. exit;
  695. { tp procvar support }
  696. maybe_call_procvar(left,true);
  697. if left.resultdef.typ=pointerdef then
  698. resultdef:=tpointerdef(left.resultdef).pointeddef
  699. else if left.resultdef.typ=undefineddef then
  700. resultdef:=cundefineddef.create(true)
  701. else
  702. CGMessage(parser_e_invalid_qualifier);
  703. end;
  704. procedure Tderefnode.mark_write;
  705. begin
  706. include(flags,nf_write);
  707. end;
  708. function tderefnode.pass_1 : tnode;
  709. begin
  710. result:=nil;
  711. firstpass(left);
  712. if codegenerror then
  713. exit;
  714. expectloc:=LOC_REFERENCE;
  715. end;
  716. {*****************************************************************************
  717. TSUBSCRIPTNODE
  718. *****************************************************************************}
  719. constructor tsubscriptnode.create(varsym : tsym;l : tnode);
  720. begin
  721. inherited create(subscriptn,l);
  722. { vs should be changed to tsym! }
  723. vs:=tfieldvarsym(varsym);
  724. end;
  725. constructor tsubscriptnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  726. begin
  727. inherited ppuload(t,ppufile);
  728. ppufile.getderef(vsderef);
  729. end;
  730. procedure tsubscriptnode.ppuwrite(ppufile:tcompilerppufile);
  731. begin
  732. inherited ppuwrite(ppufile);
  733. ppufile.putderef(vsderef);
  734. end;
  735. procedure tsubscriptnode.buildderefimpl;
  736. begin
  737. inherited buildderefimpl;
  738. vsderef.build(vs);
  739. end;
  740. procedure tsubscriptnode.derefimpl;
  741. begin
  742. inherited derefimpl;
  743. vs:=tfieldvarsym(vsderef.resolve);
  744. end;
  745. function tsubscriptnode.dogetcopy : tnode;
  746. var
  747. p : tsubscriptnode;
  748. begin
  749. p:=tsubscriptnode(inherited dogetcopy);
  750. p.vs:=vs;
  751. dogetcopy:=p;
  752. end;
  753. function tsubscriptnode.pass_typecheck:tnode;
  754. begin
  755. result:=nil;
  756. typecheckpass(left);
  757. { tp procvar support }
  758. maybe_call_procvar(left,true);
  759. resultdef:=vs.vardef;
  760. // don't put records from which we load float fields
  761. // in integer registers
  762. if (left.resultdef.typ=recorddef) and
  763. (resultdef.typ=floatdef) then
  764. make_not_regable(left,[ra_addr_regable]);
  765. end;
  766. procedure Tsubscriptnode.mark_write;
  767. begin
  768. include(flags,nf_write);
  769. { if an element of a record is written, then the whole record is changed/it is written to it,
  770. for data types being implicit pointers this does not apply as the object itself does not change }
  771. if not(is_implicit_pointer_object_type(left.resultdef)) then
  772. left.mark_write;
  773. end;
  774. function tsubscriptnode.pass_1 : tnode;
  775. begin
  776. result:=nil;
  777. firstpass(left);
  778. if codegenerror then
  779. exit;
  780. { several object types must be dereferenced implicitly }
  781. if is_implicit_pointer_object_type(left.resultdef) then
  782. expectloc:=LOC_REFERENCE
  783. else
  784. begin
  785. case left.expectloc of
  786. { if a floating point value is casted into a record, it
  787. can happen that we get here an fpu or mm register }
  788. LOC_CMMREGISTER,
  789. LOC_CFPUREGISTER,
  790. LOC_MMREGISTER,
  791. LOC_FPUREGISTER,
  792. LOC_CONSTANT,
  793. LOC_REGISTER,
  794. LOC_SUBSETREG:
  795. // can happen for function results on win32 and darwin/x86
  796. if (left.resultdef.size > sizeof(pint)) then
  797. expectloc:=LOC_REFERENCE
  798. else
  799. expectloc:=LOC_SUBSETREG;
  800. LOC_CREGISTER,
  801. LOC_CSUBSETREG:
  802. expectloc:=LOC_CSUBSETREG;
  803. LOC_REFERENCE,
  804. LOC_CREFERENCE:
  805. expectloc:=left.expectloc;
  806. else internalerror(20060521);
  807. end;
  808. end;
  809. end;
  810. function tsubscriptnode.docompare(p: tnode): boolean;
  811. begin
  812. docompare :=
  813. inherited docompare(p) and
  814. (vs = tsubscriptnode(p).vs);
  815. end;
  816. {$ifdef DEBUG_NODE_XML}
  817. procedure TSubscriptNode.XMLPrintNodeData(var T: Text);
  818. begin
  819. inherited XMLPrintNodeData(T);
  820. WriteLn(T, PrintNodeIndention, '<field>', vs.Name, '</field>');
  821. end;
  822. {$endif DEBUG_NODE_XML}
  823. {*****************************************************************************
  824. TVECNODE
  825. *****************************************************************************}
  826. constructor tvecnode.create(l,r : tnode);
  827. begin
  828. inherited create(vecn,l,r);
  829. end;
  830. function tvecnode.pass_typecheck:tnode;
  831. var
  832. htype,elementdef,elementptrdef : tdef;
  833. newordtyp: tordtype;
  834. valid : boolean;
  835. begin
  836. result:=nil;
  837. typecheckpass(left);
  838. typecheckpass(right);
  839. { implicitly convert stringconstant to stringdef,
  840. see tbs/tb0476.pp for a test }
  841. if (left.nodetype=stringconstn) and
  842. (tstringconstnode(left).cst_type=cst_conststring) then
  843. begin
  844. if tstringconstnode(left).len>255 then
  845. inserttypeconv(left,getansistringdef)
  846. else
  847. inserttypeconv(left,cshortstringtype);
  848. end;
  849. { In p[1] p is always valid, it is not possible to
  850. declared a shortstring or normal array that has
  851. undefined number of elements. Dynamic array and
  852. ansi/widestring needs to be valid }
  853. valid:=is_dynamic_array(left.resultdef) or
  854. is_ansistring(left.resultdef) or
  855. is_wide_or_unicode_string(left.resultdef) or
  856. { implicit pointer dereference -> pointer is read }
  857. (left.resultdef.typ = pointerdef);
  858. if valid then
  859. set_varstate(left,vs_read,[vsf_must_be_valid]);
  860. {
  861. A vecn is, just like a loadn, always part of an expression with its
  862. own read/write and must_be_valid semantics. Therefore we don't have
  863. to do anything else here, just like for loadn's
  864. }
  865. set_varstate(right,vs_read,[vsf_must_be_valid]);
  866. if codegenerror then
  867. exit;
  868. { maybe type conversion for the index value, but
  869. do not convert range nodes }
  870. if (right.nodetype<>rangen) then
  871. case left.resultdef.typ of
  872. arraydef:
  873. begin
  874. htype:=Tarraydef(left.resultdef).rangedef;
  875. if ado_isvariant in Tarraydef(left.resultdef).arrayoptions then
  876. {Variant arrays are a special array, can have negative indexes and would therefore
  877. need s32bit. However, they should not appear in a vecn, as they are handled in
  878. handle_variantarray in pexpr.pas. Therefore, encountering a variant array is an
  879. internal error... }
  880. internalerror(200707031)
  881. { open array and array constructor range checking is handled
  882. below at the node level, where the validity of the index
  883. will be checked -> use a regular type conversion to either
  884. the signed or unsigned native int type to prevent another
  885. range check from getting inserted here (unless the type is
  886. larger than the int type). Exception: if it's an ordinal
  887. constant, because then this check should be performed at
  888. compile time }
  889. else if is_open_array(left.resultdef) or
  890. is_array_constructor(left.resultdef) then
  891. begin
  892. if is_signed(right.resultdef) and
  893. not is_constnode(right) then
  894. inserttypeconv(right,sizesinttype)
  895. else
  896. inserttypeconv(right,sizeuinttype)
  897. end
  898. else if is_special_array(left.resultdef) then
  899. {Arrays without a high bound (dynamic arrays, open arrays) are zero based,
  900. convert indexes into these arrays to aword.}
  901. inserttypeconv(right,uinttype)
  902. { note: <> rather than </>, because indexing e.g. an array 0..0
  903. must not result in truncating the indexing value from 2/4/8
  904. bytes to 1 byte (with range checking off, the full index
  905. value must be used) }
  906. else if (htype.typ=enumdef) and
  907. (right.resultdef.typ=enumdef) and
  908. (tenumdef(htype).basedef=tenumdef(right.resultdef).basedef) and
  909. ((tarraydef(left.resultdef).lowrange<>tenumdef(htype).min) or
  910. (tarraydef(left.resultdef).highrange<>tenumdef(htype).max)) then
  911. {Convert array indexes to low_bound..high_bound.}
  912. inserttypeconv(right,cenumdef.create_subrange(tenumdef(right.resultdef),
  913. asizeint(Tarraydef(left.resultdef).lowrange),
  914. asizeint(Tarraydef(left.resultdef).highrange)
  915. ))
  916. else if (htype.typ=orddef) and
  917. { right can also be a variant or another type with
  918. overloaded assignment }
  919. (right.resultdef.typ=orddef) and
  920. { don't try to create boolean types with custom ranges }
  921. not is_boolean(right.resultdef) and
  922. { ordtype determines the size of the loaded value -> make
  923. sure we don't truncate }
  924. ((Torddef(right.resultdef).ordtype<>torddef(htype).ordtype) or
  925. (tarraydef(left.resultdef).lowrange<>torddef(htype).low) or
  926. (tarraydef(left.resultdef).highrange<>torddef(htype).high)) then
  927. {Convert array indexes to low_bound..high_bound.}
  928. begin
  929. if (right.resultdef.typ=orddef)
  930. {$ifndef cpu64bitaddr}
  931. { do truncate 64 bit values on 32 bit cpus, since
  932. a) the arrays cannot be > 32 bit anyway
  933. b) their code generators can't directly handle 64 bit
  934. loads
  935. }
  936. and not is_64bit(right.resultdef)
  937. {$endif not cpu64bitaddr}
  938. then
  939. newordtyp:=Torddef(right.resultdef).ordtype
  940. else
  941. newordtyp:=torddef(sizesinttype).ordtype;
  942. inserttypeconv(right,corddef.create(newordtyp,
  943. int64(Tarraydef(left.resultdef).lowrange),
  944. int64(Tarraydef(left.resultdef).highrange),
  945. true
  946. ))
  947. end
  948. else
  949. inserttypeconv(right,htype)
  950. end;
  951. stringdef:
  952. if is_open_string(left.resultdef) then
  953. inserttypeconv(right,u8inttype)
  954. else if is_shortstring(left.resultdef) then
  955. {Convert shortstring indexes to 0..length.}
  956. inserttypeconv(right,corddef.create(u8bit,0,int64(Tstringdef(left.resultdef).len),true))
  957. else
  958. {Convert indexes into dynamically allocated strings to aword.}
  959. inserttypeconv(right,uinttype);
  960. pointerdef:
  961. inserttypeconv(right,tpointerdef(left.resultdef).pointer_arithmetic_int_type);
  962. else
  963. {Others, (are there any?) indexes to aint.}
  964. inserttypeconv(right,sinttype);
  965. end;
  966. { although we never put regular arrays or shortstrings in registers,
  967. it's possible that another type was typecasted to a small record
  968. that has a field of one of these types -> in that case the record
  969. can't be a regvar either }
  970. if ((left.resultdef.typ=arraydef) and
  971. not is_special_array(left.resultdef) and
  972. { arrays with elements equal to the alu size and with a constant index can be kept in register }
  973. not(is_constnode(right) and (tarraydef(left.resultdef).elementdef.size=alusinttype.size))) or
  974. ((left.resultdef.typ=stringdef) and
  975. (tstringdef(left.resultdef).stringtype in [st_shortstring,st_longstring])) then
  976. make_not_regable(left,[ra_addr_regable]);
  977. case left.resultdef.typ of
  978. arraydef :
  979. begin
  980. { check type of the index value }
  981. if (compare_defs(right.resultdef,tarraydef(left.resultdef).rangedef,right.nodetype)=te_incompatible) then
  982. IncompatibleTypes(right.resultdef,tarraydef(left.resultdef).rangedef);
  983. if right.nodetype=rangen then
  984. resultdef:=left.resultdef
  985. else
  986. resultdef:=Tarraydef(left.resultdef).elementdef;
  987. result:=gen_array_rangecheck;
  988. if assigned(result) then
  989. exit;
  990. { in case of a bitpacked array of enums that are size 2 (due to
  991. packenum 2) but whose values all fit in one byte, the size of
  992. bitpacked array elements will be 1 byte while the resultdef of
  993. will currently say it's two bytes) -> create a temp enumdef
  994. with packenum=1 for the resultdef as subtype of the main
  995. enumdef }
  996. if is_enum(resultdef) and
  997. is_packed_array(left.resultdef) and
  998. ((tarraydef(left.resultdef).elepackedbitsize div 8) <> resultdef.size) then
  999. begin
  1000. resultdef:=cenumdef.create_subrange(tenumdef(resultdef),tenumdef(resultdef).min,tenumdef(resultdef).max);
  1001. tenumdef(resultdef).calcsavesize(1);
  1002. end
  1003. end;
  1004. pointerdef :
  1005. begin
  1006. { are we accessing a pointer[], then convert the pointer to
  1007. an array first, in FPC this is allowed for all pointers
  1008. (except voidpointer) in delphi/tp7 it's only allowed for pchars. }
  1009. if not is_voidpointer(left.resultdef) and
  1010. (
  1011. (cs_pointermath in current_settings.localswitches) or
  1012. tpointerdef(left.resultdef).has_pointer_math or
  1013. is_pchar(left.resultdef) or
  1014. is_pwidechar(left.resultdef)
  1015. ) then
  1016. begin
  1017. { convert pointer to array }
  1018. htype:=carraydef.create_from_pointer(tpointerdef(left.resultdef));
  1019. inserttypeconv(left,htype);
  1020. if right.nodetype=rangen then
  1021. resultdef:=htype
  1022. else
  1023. resultdef:=tarraydef(htype).elementdef;
  1024. end
  1025. else
  1026. CGMessage(type_e_array_required);
  1027. end;
  1028. stringdef :
  1029. begin
  1030. case tstringdef(left.resultdef).stringtype of
  1031. st_unicodestring,
  1032. st_widestring :
  1033. begin
  1034. elementdef:=cwidechartype;
  1035. elementptrdef:=widecharpointertype;
  1036. end;
  1037. st_ansistring,
  1038. st_longstring,
  1039. st_shortstring :
  1040. begin
  1041. elementdef:=cansichartype;
  1042. elementptrdef:=charpointertype;
  1043. end;
  1044. end;
  1045. if right.nodetype=rangen then
  1046. begin
  1047. htype:=carraydef.create_from_pointer(tpointerdef(elementptrdef));
  1048. resultdef:=htype;
  1049. end
  1050. else
  1051. begin
  1052. { indexed access to 0 element is only allowed for shortstrings or if
  1053. zero based strings is turned on }
  1054. if (right.nodetype=ordconstn) and
  1055. (Tordconstnode(right).value.svalue=0) and
  1056. not is_shortstring(left.resultdef) and
  1057. not(cs_zerobasedstrings in current_settings.localswitches) then
  1058. CGMessage(cg_e_can_access_element_zero);
  1059. resultdef:=elementdef;
  1060. end;
  1061. end;
  1062. variantdef :
  1063. resultdef:=cvarianttype;
  1064. else
  1065. CGMessage(type_e_array_required);
  1066. end;
  1067. end;
  1068. procedure Tvecnode.mark_write;
  1069. begin
  1070. include(flags,nf_write);
  1071. { see comment in tsubscriptnode.mark_write }
  1072. if not(is_implicit_array_pointer(left.resultdef)) then
  1073. left.mark_write;
  1074. end;
  1075. function tvecnode.pass_1 : tnode;
  1076. begin
  1077. result:=nil;
  1078. firstpass(left);
  1079. firstpass(right);
  1080. if codegenerror then
  1081. exit;
  1082. if (nf_callunique in flags) and
  1083. (is_ansistring(left.resultdef) or
  1084. is_unicodestring(left.resultdef) or
  1085. (is_widestring(left.resultdef) and not(tf_winlikewidestring in target_info.flags))) then
  1086. begin
  1087. left := ctypeconvnode.create_internal(ccallnode.createintern('fpc_'+tstringdef(left.resultdef).stringtypname+'_unique',
  1088. ccallparanode.create(
  1089. ctypeconvnode.create_internal(left,voidpointertype),nil)),
  1090. left.resultdef);
  1091. firstpass(left);
  1092. { double resultdef passes somwhere else may cause this to be }
  1093. { reset though :/ }
  1094. exclude(flags,nf_callunique);
  1095. end
  1096. else if is_widestring(left.resultdef) and (tf_winlikewidestring in target_info.flags) then
  1097. exclude(flags,nf_callunique);
  1098. { a range node as array index can only appear in function calls, and
  1099. those convert the range node into something else in
  1100. tcallnode.gen_high_tree }
  1101. if (right.nodetype=rangen) then
  1102. CGMessagePos(right.fileinfo,parser_e_illegal_expression)
  1103. else if left.resultdef.typ=arraydef then
  1104. result:=first_arraydef
  1105. else
  1106. begin
  1107. if left.expectloc=LOC_CREFERENCE then
  1108. expectloc:=LOC_CREFERENCE
  1109. else
  1110. expectloc:=LOC_REFERENCE
  1111. end;
  1112. end;
  1113. function tvecnode.first_arraydef: tnode;
  1114. begin
  1115. result:=nil;
  1116. if (not is_packed_array(left.resultdef)) or
  1117. ((tarraydef(left.resultdef).elepackedbitsize mod 8) = 0) then
  1118. if left.expectloc=LOC_CREFERENCE then
  1119. expectloc:=LOC_CREFERENCE
  1120. else
  1121. expectloc:=LOC_REFERENCE
  1122. else
  1123. if left.expectloc=LOC_CREFERENCE then
  1124. expectloc:=LOC_CSUBSETREF
  1125. else
  1126. expectloc:=LOC_SUBSETREF;
  1127. end;
  1128. function tvecnode.gen_array_rangecheck: tnode;
  1129. var
  1130. htype: tdef;
  1131. temp: ttempcreatenode;
  1132. stat: tstatementnode;
  1133. indextree: tnode;
  1134. hightree: tnode;
  1135. begin
  1136. result:=nil;
  1137. { Range checking an array of const/open array/dynamic array is
  1138. more complicated than regular arrays, because the bounds must
  1139. be checked dynamically. Additionally, in case of array of const
  1140. and open array we need the high parameter, which must not be
  1141. made a regvar in case this is a nested rountine relative to the
  1142. array parameter -> generate te check at the node tree level
  1143. rather than in the code generator }
  1144. if (cs_check_range in current_settings.localswitches) and
  1145. (is_open_array(left.resultdef) or
  1146. is_array_of_const(left.resultdef)) and
  1147. (right.nodetype<>rangen) then
  1148. begin
  1149. { expect to find the load node }
  1150. if get_open_const_array(left).nodetype<>loadn then
  1151. internalerror(2014040601);
  1152. { cdecl functions don't have high() so we can not check the range }
  1153. { (can't use current_procdef, since it may be a nested procedure) }
  1154. if not(tprocdef(tparasymtable(tparavarsym(tloadnode(get_open_const_array(left)).symtableentry).owner).defowner).proccalloption in cdecl_pocalls) then
  1155. begin
  1156. temp:=nil;
  1157. result:=internalstatements(stat);
  1158. { can't use node_complexity here, assumes that the code has
  1159. already been firstpassed }
  1160. if not is_const(right) then
  1161. begin
  1162. temp:=ctempcreatenode.create(right.resultdef,right.resultdef.size,tt_persistent,true);
  1163. addstatement(stat,temp);
  1164. { needed so we can typecheck its temprefnodes }
  1165. typecheckpass(tnode(temp));
  1166. addstatement(stat,cassignmentnode.create(
  1167. ctemprefnode.create(temp),right)
  1168. );
  1169. right:=ctemprefnode.create(temp);
  1170. { right.resultdef is used below }
  1171. typecheckpass(right);
  1172. end;
  1173. { range check will be made explicit here }
  1174. exclude(localswitches,cs_check_range);
  1175. hightree:=load_high_value_node(tparavarsym(tloadnode(
  1176. get_open_const_array(left)).symtableentry));
  1177. { make index unsigned so we only need one comparison;
  1178. lower bound is always zero for these arrays, but
  1179. hightree can be -1 in case the array was empty ->
  1180. add 1 before comparing (ignoring overflows) }
  1181. htype:=get_unsigned_inttype(right.resultdef);
  1182. inserttypeconv_explicit(hightree,htype);
  1183. hightree:=caddnode.create(addn,hightree,genintconstnode(1));
  1184. hightree.localswitches:=hightree.localswitches-[cs_check_range,
  1185. cs_check_overflow];
  1186. indextree:=ctypeconvnode.create_explicit(right.getcopy,htype);
  1187. { range error if index >= hightree+1 }
  1188. addstatement(stat,
  1189. cifnode.create_internal(
  1190. caddnode.create_internal(gten,indextree,hightree),
  1191. ccallnode.createintern('fpc_rangeerror',nil),
  1192. nil
  1193. )
  1194. );
  1195. if assigned(temp) then
  1196. addstatement(stat,ctempdeletenode.create_normal_temp(temp));
  1197. addstatement(stat,self.getcopy);
  1198. end;
  1199. end;
  1200. end;
  1201. {$ifdef DEBUG_NODE_XML}
  1202. procedure TVecNode.XMLPrintNodeData(var T: Text);
  1203. begin
  1204. XMLPrintNode(T, Left);
  1205. { The right node is the index }
  1206. WriteLn(T, PrintNodeIndention, '<index>');
  1207. PrintNodeIndent;
  1208. XMLPrintNode(T, Right);
  1209. PrintNodeUnindent;
  1210. WriteLn(T, PrintNodeIndention, '</index>');
  1211. PrintNodeUnindent;
  1212. WriteLn(T, PrintNodeIndention, '</', nodetype2str[nodetype], '>');
  1213. end;
  1214. {$endif DEBUG_NODE_XML}
  1215. function is_big_untyped_addrnode(p: tnode): boolean;
  1216. begin
  1217. is_big_untyped_addrnode:=(p.nodetype=addrn) and
  1218. not (anf_typedaddr in taddrnode(p).addrnodeflags) and
  1219. (taddrnode(p).left.resultdef.size > 1);
  1220. end;
  1221. end.