nmem.pas 51 KB

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