nmem.pas 50 KB

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