nmem.pas 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  1. {
  2. $Id$
  3. Copyright (c) 2000-2002 by Florian Klaempfl
  4. Type checking and register allocation for memory related nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit nmem;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,
  23. symtype,symppu,symdef,symsym,symtable,
  24. cpubase;
  25. type
  26. tloadvmtaddrnode = class(tunarynode)
  27. constructor create(l : tnode);virtual;
  28. function pass_1 : tnode;override;
  29. function det_resulttype:tnode;override;
  30. end;
  31. tloadvmtaddrnodeclass = class of tloadvmtaddrnode;
  32. tloadparentfpnode = class(tunarynode)
  33. parentpd : tprocdef;
  34. constructor create(pd:tprocdef);virtual;
  35. function pass_1 : tnode;override;
  36. function det_resulttype:tnode;override;
  37. function getcopy : tnode;override;
  38. end;
  39. tloadparentfpnodeclass = class of tloadparentfpnode;
  40. taddrnode = class(tunarynode)
  41. getprocvardef : tprocvardef;
  42. getprocvardefderef : tderef;
  43. constructor create(l : tnode);virtual;
  44. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  45. procedure ppuwrite(ppufile:tcompilerppufile);override;
  46. procedure mark_write;override;
  47. procedure buildderefimpl;override;
  48. procedure derefimpl;override;
  49. function getcopy : tnode;override;
  50. function pass_1 : tnode;override;
  51. function det_resulttype:tnode;override;
  52. end;
  53. taddrnodeclass = class of taddrnode;
  54. tderefnode = class(tunarynode)
  55. constructor create(l : tnode);virtual;
  56. function pass_1 : tnode;override;
  57. function det_resulttype:tnode;override;
  58. procedure mark_write;override;
  59. end;
  60. tderefnodeclass = class of tderefnode;
  61. tsubscriptnode = class(tunarynode)
  62. vs : tvarsym;
  63. vsderef : tderef;
  64. constructor create(varsym : tsym;l : tnode);virtual;
  65. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  66. procedure ppuwrite(ppufile:tcompilerppufile);override;
  67. procedure buildderefimpl;override;
  68. procedure derefimpl;override;
  69. function getcopy : tnode;override;
  70. function pass_1 : tnode;override;
  71. function docompare(p: tnode): boolean; override;
  72. function det_resulttype:tnode;override;
  73. procedure mark_write;override;
  74. end;
  75. tsubscriptnodeclass = class of tsubscriptnode;
  76. tvecnode = class(tbinarynode)
  77. constructor create(l,r : tnode);virtual;
  78. function pass_1 : tnode;override;
  79. function det_resulttype:tnode;override;
  80. procedure mark_write;override;
  81. end;
  82. tvecnodeclass = class of tvecnode;
  83. twithnode = class(tunarynode)
  84. withsymtable : twithsymtable;
  85. tablecount : longint;
  86. withrefnode : tnode;
  87. constructor create(l:tnode;symtable:twithsymtable;count:longint;r:tnode);
  88. destructor destroy;override;
  89. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  90. procedure ppuwrite(ppufile:tcompilerppufile);override;
  91. function getcopy : tnode;override;
  92. function pass_1 : tnode;override;
  93. function docompare(p: tnode): boolean; override;
  94. function det_resulttype:tnode;override;
  95. end;
  96. twithnodeclass = class of twithnode;
  97. var
  98. cloadvmtaddrnode : tloadvmtaddrnodeclass;
  99. cloadparentfpnode : tloadparentfpnodeclass;
  100. caddrnode : taddrnodeclass;
  101. cderefnode : tderefnodeclass;
  102. csubscriptnode : tsubscriptnodeclass;
  103. cvecnode : tvecnodeclass;
  104. cwithnode : twithnodeclass;
  105. implementation
  106. uses
  107. globtype,systems,
  108. cutils,verbose,globals,
  109. symconst,symbase,defutil,defcmp,
  110. nbas,
  111. htypechk,pass_1,ncal,nld,ncon,ncnv,cgbase,procinfo
  112. ;
  113. {*****************************************************************************
  114. TLOADVMTADDRNODE
  115. *****************************************************************************}
  116. constructor tloadvmtaddrnode.create(l : tnode);
  117. begin
  118. inherited create(loadvmtaddrn,l);
  119. end;
  120. function tloadvmtaddrnode.det_resulttype:tnode;
  121. begin
  122. result:=nil;
  123. resulttypepass(left);
  124. if codegenerror then
  125. exit;
  126. case left.resulttype.def.deftype of
  127. classrefdef :
  128. resulttype:=left.resulttype;
  129. objectdef :
  130. resulttype.setdef(tclassrefdef.create(left.resulttype));
  131. else
  132. Message(parser_e_pointer_to_class_expected);
  133. end;
  134. end;
  135. function tloadvmtaddrnode.pass_1 : tnode;
  136. begin
  137. result:=nil;
  138. expectloc:=LOC_REGISTER;
  139. if left.nodetype<>typen then
  140. begin
  141. firstpass(left);
  142. registers32:=left.registers32;
  143. end;
  144. if registers32<1 then
  145. registers32:=1;
  146. end;
  147. {*****************************************************************************
  148. TLOADPARENTFPNODE
  149. *****************************************************************************}
  150. constructor tloadparentfpnode.create(pd:tprocdef);
  151. begin
  152. inherited create(loadparentfpn,nil);
  153. if not assigned(pd) then
  154. internalerror(200309288);
  155. if (pd.parast.symtablelevel>current_procinfo.procdef.parast.symtablelevel) then
  156. internalerror(200309284);
  157. parentpd:=pd;
  158. end;
  159. function tloadparentfpnode.getcopy : tnode;
  160. var
  161. p : tloadparentfpnode;
  162. begin
  163. p:=tloadparentfpnode(inherited getcopy);
  164. p.parentpd:=parentpd;
  165. getcopy:=p;
  166. end;
  167. function tloadparentfpnode.det_resulttype:tnode;
  168. begin
  169. result:=nil;
  170. resulttype:=voidpointertype;
  171. end;
  172. function tloadparentfpnode.pass_1 : tnode;
  173. begin
  174. result:=nil;
  175. expectloc:=LOC_REGISTER;
  176. registers32:=1;
  177. end;
  178. {*****************************************************************************
  179. TADDRNODE
  180. *****************************************************************************}
  181. constructor taddrnode.create(l : tnode);
  182. begin
  183. inherited create(addrn,l);
  184. getprocvardef:=nil;
  185. end;
  186. constructor taddrnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  187. begin
  188. inherited ppuload(t,ppufile);
  189. ppufile.getderef(getprocvardefderef);
  190. end;
  191. procedure taddrnode.ppuwrite(ppufile:tcompilerppufile);
  192. begin
  193. inherited ppuwrite(ppufile);
  194. ppufile.putderef(getprocvardefderef);
  195. end;
  196. procedure Taddrnode.mark_write;
  197. begin
  198. {@procvar:=nil is legal in Delphi mode.}
  199. left.mark_write;
  200. end;
  201. procedure taddrnode.buildderefimpl;
  202. begin
  203. inherited buildderefimpl;
  204. getprocvardefderef.build(getprocvardef);
  205. end;
  206. procedure taddrnode.derefimpl;
  207. begin
  208. inherited derefimpl;
  209. getprocvardef:=tprocvardef(getprocvardefderef.resolve);
  210. end;
  211. function taddrnode.getcopy : tnode;
  212. var
  213. p : taddrnode;
  214. begin
  215. p:=taddrnode(inherited getcopy);
  216. p.getprocvardef:=getprocvardef;
  217. getcopy:=p;
  218. end;
  219. function taddrnode.det_resulttype:tnode;
  220. var
  221. hp : tnode;
  222. hp2 : TParaItem;
  223. hp3 : tabstractprocdef;
  224. begin
  225. result:=nil;
  226. resulttypepass(left);
  227. if codegenerror then
  228. exit;
  229. { don't allow constants }
  230. if is_constnode(left) then
  231. begin
  232. aktfilepos:=left.fileinfo;
  233. CGMessage(type_e_no_addr_of_constant);
  234. exit;
  235. end;
  236. { tp @procvar support (type of @procvar is a void pointer)
  237. Note: we need to leave the addrn in the tree,
  238. else we can't see the difference between @procvar and procvar.
  239. we set the procvarload flag so a secondpass does nothing for
  240. this node (PFV) }
  241. if (m_tp_procvar in aktmodeswitches) then
  242. begin
  243. case left.nodetype of
  244. calln :
  245. begin
  246. { a load of a procvar can't have parameters }
  247. if assigned(tcallnode(left).left) then
  248. CGMessage(cg_e_illegal_expression);
  249. { is it a procvar? }
  250. hp:=tcallnode(left).right;
  251. if assigned(hp) then
  252. begin
  253. { remove calln node }
  254. tcallnode(left).right:=nil;
  255. left.free;
  256. left:=hp;
  257. include(flags,nf_procvarload);
  258. end;
  259. end;
  260. loadn,
  261. subscriptn,
  262. typeconvn,
  263. vecn,
  264. derefn :
  265. begin
  266. if left.resulttype.def.deftype=procvardef then
  267. include(flags,nf_procvarload);
  268. end;
  269. end;
  270. if nf_procvarload in flags then
  271. begin
  272. resulttype:=voidpointertype;
  273. exit;
  274. end;
  275. end;
  276. { proc 2 procvar ? }
  277. if left.nodetype=calln then
  278. { if it were a valid construct, the addr node would already have }
  279. { been removed in the parser. This happens for (in FPC mode) }
  280. { procvar1 := @procvar2(parameters); }
  281. CGMessage(cg_e_illegal_expression)
  282. else
  283. if (left.nodetype=loadn) and (tloadnode(left).symtableentry.typ=procsym) then
  284. begin
  285. { the address is already available when loading a procedure of object }
  286. if assigned(tloadnode(left).left) then
  287. include(flags,nf_procvarload);
  288. { result is a procedure variable }
  289. { No, to be TP compatible, you must return a voidpointer to
  290. the procedure that is stored in the procvar.}
  291. if not(m_tp_procvar in aktmodeswitches) then
  292. begin
  293. if assigned(getprocvardef) and
  294. (tprocsym(tloadnode(left).symtableentry).procdef_count>1) then
  295. begin
  296. hp3:=tprocsym(tloadnode(left).symtableentry).search_procdef_byprocvardef(getprocvardef);
  297. if not assigned(hp3) then
  298. begin
  299. IncompatibleTypes(tprocsym(tloadnode(left).symtableentry).first_procdef,getprocvardef);
  300. exit;
  301. end;
  302. end
  303. else
  304. hp3:=tabstractprocdef(tprocsym(tloadnode(left).symtableentry).first_procdef);
  305. { create procvardef }
  306. resulttype.setdef(tprocvardef.create(hp3.parast.symtablelevel));
  307. tprocvardef(resulttype.def).proctypeoption:=hp3.proctypeoption;
  308. tprocvardef(resulttype.def).proccalloption:=hp3.proccalloption;
  309. tprocvardef(resulttype.def).procoptions:=hp3.procoptions;
  310. tprocvardef(resulttype.def).rettype:=hp3.rettype;
  311. { method ? then set the methodpointer flag }
  312. if (hp3.owner.symtabletype=objectsymtable) then
  313. include(tprocvardef(resulttype.def).procoptions,po_methodpointer);
  314. { only need the address of the method? this is needed
  315. for @tobject.create }
  316. if not assigned(tloadnode(left).left) then
  317. include(tprocvardef(resulttype.def).procoptions,po_addressonly);
  318. { Add parameters in left to right order }
  319. hp2:=TParaItem(hp3.Para.first);
  320. while assigned(hp2) do
  321. begin
  322. tprocvardef(resulttype.def).concatpara(nil,hp2.paratype,hp2.parasym,
  323. hp2.defaultvalue,hp2.is_hidden);
  324. hp2:=TParaItem(hp2.next);
  325. end;
  326. end
  327. else
  328. resulttype:=voidpointertype;
  329. end
  330. else
  331. begin
  332. { what are we getting the address from an absolute sym? }
  333. hp:=left;
  334. while assigned(hp) and (hp.nodetype in [vecn,derefn,subscriptn]) do
  335. hp:=tunarynode(hp).left;
  336. if assigned(hp) and (hp.nodetype=loadn) and
  337. ((tloadnode(hp).symtableentry.typ=absolutesym) and
  338. tabsolutesym(tloadnode(hp).symtableentry).absseg) then
  339. begin
  340. if not(cs_typed_addresses in aktlocalswitches) then
  341. resulttype:=voidfarpointertype
  342. else
  343. resulttype.setdef(tpointerdef.createfar(left.resulttype));
  344. end
  345. else
  346. begin
  347. if not(cs_typed_addresses in aktlocalswitches) then
  348. resulttype:=voidpointertype
  349. else
  350. resulttype.setdef(tpointerdef.create(left.resulttype));
  351. end;
  352. end;
  353. { this is like the function addr }
  354. inc(parsing_para_level);
  355. set_varstate(left,vs_used,false);
  356. dec(parsing_para_level);
  357. end;
  358. function taddrnode.pass_1 : tnode;
  359. begin
  360. result:=nil;
  361. firstpass(left);
  362. if codegenerror then
  363. exit;
  364. make_not_regable(left);
  365. if nf_procvarload in flags then
  366. begin
  367. registers32:=left.registers32;
  368. registersfpu:=left.registersfpu;
  369. {$ifdef SUPPORT_MMX}
  370. registersmmx:=left.registersmmx;
  371. {$endif SUPPORT_MMX}
  372. if registers32<1 then
  373. registers32:=1;
  374. expectloc:=left.expectloc;
  375. exit;
  376. end;
  377. { we should allow loc_mem for @string }
  378. if not(left.expectloc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  379. begin
  380. aktfilepos:=left.fileinfo;
  381. CGMessage(cg_e_illegal_expression);
  382. end;
  383. registers32:=left.registers32;
  384. registersfpu:=left.registersfpu;
  385. {$ifdef SUPPORT_MMX}
  386. registersmmx:=left.registersmmx;
  387. {$endif SUPPORT_MMX}
  388. if registers32<1 then
  389. registers32:=1;
  390. { is this right for object of methods ?? }
  391. expectloc:=LOC_REGISTER;
  392. end;
  393. {*****************************************************************************
  394. TDEREFNODE
  395. *****************************************************************************}
  396. constructor tderefnode.create(l : tnode);
  397. begin
  398. inherited create(derefn,l);
  399. end;
  400. function tderefnode.det_resulttype:tnode;
  401. begin
  402. result:=nil;
  403. resulttypepass(left);
  404. set_varstate(left,vs_used,true);
  405. if codegenerror then
  406. exit;
  407. if left.resulttype.def.deftype=pointerdef then
  408. resulttype:=tpointerdef(left.resulttype.def).pointertype
  409. else
  410. CGMessage(cg_e_invalid_qualifier);
  411. end;
  412. procedure Tderefnode.mark_write;
  413. begin
  414. include(flags,nf_write);
  415. end;
  416. function tderefnode.pass_1 : tnode;
  417. begin
  418. result:=nil;
  419. firstpass(left);
  420. if codegenerror then
  421. exit;
  422. registers32:=max(left.registers32,1);
  423. registersfpu:=left.registersfpu;
  424. {$ifdef SUPPORT_MMX}
  425. registersmmx:=left.registersmmx;
  426. {$endif SUPPORT_MMX}
  427. expectloc:=LOC_REFERENCE;
  428. end;
  429. {*****************************************************************************
  430. TSUBSCRIPTNODE
  431. *****************************************************************************}
  432. constructor tsubscriptnode.create(varsym : tsym;l : tnode);
  433. begin
  434. inherited create(subscriptn,l);
  435. { vs should be changed to tsym! }
  436. vs:=tvarsym(varsym);
  437. end;
  438. constructor tsubscriptnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  439. begin
  440. inherited ppuload(t,ppufile);
  441. ppufile.getderef(vsderef);
  442. end;
  443. procedure tsubscriptnode.ppuwrite(ppufile:tcompilerppufile);
  444. begin
  445. inherited ppuwrite(ppufile);
  446. ppufile.putderef(vsderef);
  447. end;
  448. procedure tsubscriptnode.buildderefimpl;
  449. begin
  450. inherited buildderefimpl;
  451. vsderef.build(vs);
  452. end;
  453. procedure tsubscriptnode.derefimpl;
  454. begin
  455. inherited derefimpl;
  456. vs:=tvarsym(vsderef.resolve);
  457. end;
  458. function tsubscriptnode.getcopy : tnode;
  459. var
  460. p : tsubscriptnode;
  461. begin
  462. p:=tsubscriptnode(inherited getcopy);
  463. p.vs:=vs;
  464. getcopy:=p;
  465. end;
  466. function tsubscriptnode.det_resulttype:tnode;
  467. begin
  468. result:=nil;
  469. resulttypepass(left);
  470. resulttype:=vs.vartype;
  471. end;
  472. procedure Tsubscriptnode.mark_write;
  473. begin
  474. include(flags,nf_write);
  475. end;
  476. function tsubscriptnode.pass_1 : tnode;
  477. begin
  478. result:=nil;
  479. firstpass(left);
  480. if codegenerror then
  481. exit;
  482. registers32:=left.registers32;
  483. registersfpu:=left.registersfpu;
  484. {$ifdef SUPPORT_MMX}
  485. registersmmx:=left.registersmmx;
  486. {$endif SUPPORT_MMX}
  487. { classes must be dereferenced implicit }
  488. if is_class_or_interface(left.resulttype.def) then
  489. begin
  490. if registers32=0 then
  491. registers32:=1;
  492. expectloc:=LOC_REFERENCE;
  493. end
  494. else
  495. begin
  496. if (left.expectloc<>LOC_CREFERENCE) and
  497. (left.expectloc<>LOC_REFERENCE) then
  498. CGMessage(cg_e_illegal_expression);
  499. expectloc:=left.expectloc;
  500. end;
  501. end;
  502. function tsubscriptnode.docompare(p: tnode): boolean;
  503. begin
  504. docompare :=
  505. inherited docompare(p) and
  506. (vs = tsubscriptnode(p).vs);
  507. end;
  508. {*****************************************************************************
  509. TVECNODE
  510. *****************************************************************************}
  511. constructor tvecnode.create(l,r : tnode);
  512. begin
  513. inherited create(vecn,l,r);
  514. end;
  515. function tvecnode.det_resulttype:tnode;
  516. var
  517. htype : ttype;
  518. begin
  519. result:=nil;
  520. resulttypepass(left);
  521. resulttypepass(right);
  522. if codegenerror then
  523. exit;
  524. { maybe type conversion for the index value, but
  525. do not convert enums,booleans,char }
  526. if (right.resulttype.def.deftype<>enumdef) and
  527. not(is_char(right.resulttype.def)) and
  528. not(is_boolean(right.resulttype.def)) then
  529. begin
  530. inserttypeconv(right,s32bittype);
  531. end;
  532. case left.resulttype.def.deftype of
  533. arraydef :
  534. begin
  535. { check type of the index value }
  536. if (compare_defs(right.resulttype.def,tarraydef(left.resulttype.def).rangetype.def,right.nodetype)=te_incompatible) then
  537. CGMessage(type_e_mismatch);
  538. resulttype:=tarraydef(left.resulttype.def).elementtype;
  539. end;
  540. pointerdef :
  541. begin
  542. { are we accessing a pointer[], then convert the pointer to
  543. an array first, in FPC this is allowed for all pointers in
  544. delphi/tp7 it's only allowed for pchars }
  545. if (m_fpc in aktmodeswitches) or
  546. is_pchar(left.resulttype.def) or
  547. is_pwidechar(left.resulttype.def) then
  548. begin
  549. { convert pointer to array }
  550. htype.setdef(tarraydef.create_from_pointer(tpointerdef(left.resulttype.def).pointertype));
  551. inserttypeconv(left,htype);
  552. resulttype:=tarraydef(htype.def).elementtype;
  553. end
  554. else
  555. CGMessage(type_e_array_required);
  556. end;
  557. stringdef :
  558. begin
  559. { indexed access to 0 element is only allowed for shortstrings }
  560. if (right.nodetype=ordconstn) and
  561. (tordconstnode(right).value=0) and
  562. not(is_shortstring(left.resulttype.def)) then
  563. CGMessage(cg_e_can_access_element_zero);
  564. case tstringdef(left.resulttype.def).string_typ of
  565. st_widestring :
  566. resulttype:=cwidechartype;
  567. st_ansistring :
  568. resulttype:=cchartype;
  569. st_longstring :
  570. resulttype:=cchartype;
  571. st_shortstring :
  572. resulttype:=cchartype;
  573. end;
  574. end
  575. else
  576. CGMessage(type_e_array_required);
  577. end;
  578. end;
  579. procedure Tvecnode.mark_write;
  580. begin
  581. include(flags,nf_write);
  582. end;
  583. function tvecnode.pass_1 : tnode;
  584. {$ifdef consteval}
  585. var
  586. tcsym : ttypedconstsym;
  587. {$endif}
  588. begin
  589. result:=nil;
  590. firstpass(left);
  591. firstpass(right);
  592. if codegenerror then
  593. exit;
  594. if (nf_callunique in flags) and
  595. (is_ansistring(left.resulttype.def) or
  596. is_widestring(left.resulttype.def)) then
  597. begin
  598. left := ctypeconvnode.create_explicit(ccallnode.createintern('fpc_'+tstringdef(left.resulttype.def).stringtypname+'_unique',
  599. ccallparanode.create(
  600. ctypeconvnode.create_explicit(left,voidpointertype),nil)),
  601. left.resulttype);
  602. firstpass(left);
  603. { double resulttype passes somwhere else may cause this to be }
  604. { reset though :/ }
  605. exclude(flags,nf_callunique);
  606. end;
  607. { the register calculation is easy if a const index is used }
  608. if right.nodetype=ordconstn then
  609. begin
  610. {$ifdef consteval}
  611. { constant evaluation }
  612. if (left.nodetype=loadn) and
  613. (left.symtableentry.typ=typedconstsym) then
  614. begin
  615. tcsym:=ttypedconstsym(left.symtableentry);
  616. if tcsym.defintion^.typ=stringdef then
  617. begin
  618. end;
  619. end;
  620. {$endif}
  621. registers32:=left.registers32;
  622. { for ansi/wide strings, we need at least one register }
  623. if is_ansistring(left.resulttype.def) or
  624. is_widestring(left.resulttype.def) or
  625. { ... as well as for dynamic arrays }
  626. is_dynamic_array(left.resulttype.def) then
  627. registers32:=max(registers32,1);
  628. end
  629. else
  630. begin
  631. { this rules are suboptimal, but they should give }
  632. { good results }
  633. registers32:=max(left.registers32,right.registers32);
  634. { for ansi/wide strings, we need at least one register }
  635. if is_ansistring(left.resulttype.def) or
  636. is_widestring(left.resulttype.def) or
  637. { ... as well as for dynamic arrays }
  638. is_dynamic_array(left.resulttype.def) then
  639. registers32:=max(registers32,1);
  640. { need we an extra register when doing the restore ? }
  641. if (left.registers32<=right.registers32) and
  642. { only if the node needs less than 3 registers }
  643. { two for the right node and one for the }
  644. { left address }
  645. (registers32<3) then
  646. inc(registers32);
  647. { need we an extra register for the index ? }
  648. if (right.expectloc<>LOC_REGISTER)
  649. { only if the right node doesn't need a register }
  650. and (right.registers32<1) then
  651. inc(registers32);
  652. { not correct, but what works better ?
  653. if left.registers32>0 then
  654. registers32:=max(registers32,2)
  655. else
  656. min. one register
  657. registers32:=max(registers32,1);
  658. }
  659. end;
  660. registersfpu:=max(left.registersfpu,right.registersfpu);
  661. {$ifdef SUPPORT_MMX}
  662. registersmmx:=max(left.registersmmx,right.registersmmx);
  663. {$endif SUPPORT_MMX}
  664. if left.expectloc=LOC_CREFERENCE then
  665. expectloc:=LOC_CREFERENCE
  666. else
  667. expectloc:=LOC_REFERENCE;
  668. end;
  669. {*****************************************************************************
  670. TWITHNODE
  671. *****************************************************************************}
  672. constructor twithnode.create(l:tnode;symtable:twithsymtable;count:longint;r:tnode);
  673. begin
  674. inherited create(withn,l);
  675. withrefnode:=r;
  676. withsymtable:=symtable;
  677. tablecount:=count;
  678. set_file_line(l);
  679. end;
  680. destructor twithnode.destroy;
  681. var
  682. hsymt,
  683. symt : tsymtable;
  684. i : longint;
  685. begin
  686. symt:=withsymtable;
  687. for i:=1 to tablecount do
  688. begin
  689. if assigned(symt) then
  690. begin
  691. hsymt:=symt.next;
  692. symt.free;
  693. symt:=hsymt;
  694. end;
  695. end;
  696. inherited destroy;
  697. end;
  698. constructor twithnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  699. begin
  700. inherited ppuload(t,ppufile);
  701. internalerror(200208192);
  702. end;
  703. procedure twithnode.ppuwrite(ppufile:tcompilerppufile);
  704. begin
  705. inherited ppuwrite(ppufile);
  706. internalerror(200208193);
  707. end;
  708. function twithnode.getcopy : tnode;
  709. var
  710. p : twithnode;
  711. begin
  712. p:=twithnode(inherited getcopy);
  713. p.withsymtable:=withsymtable;
  714. p.tablecount:=tablecount;
  715. if assigned(p.withrefnode) then
  716. p.withrefnode:=withrefnode.getcopy
  717. else
  718. p.withrefnode:=nil;
  719. result:=p;
  720. end;
  721. function twithnode.det_resulttype:tnode;
  722. begin
  723. result:=nil;
  724. resulttype:=voidtype;
  725. resulttypepass(withrefnode);
  726. //unset_varstate(withrefnode);
  727. set_varstate(withrefnode,vs_used,true);
  728. if codegenerror then
  729. exit;
  730. if (withrefnode.nodetype=vecn) and
  731. (nf_memseg in withrefnode.flags) then
  732. CGMessage(parser_e_no_with_for_variable_in_other_segments);
  733. if assigned(left) then
  734. resulttypepass(left);
  735. end;
  736. function twithnode.pass_1 : tnode;
  737. begin
  738. result:=nil;
  739. expectloc:=LOC_VOID;
  740. if assigned(left) then
  741. begin
  742. firstpass(left);
  743. registers32:=left.registers32;
  744. registersfpu:=left.registersfpu;
  745. {$ifdef SUPPORT_MMX}
  746. registersmmx:=left.registersmmx;
  747. {$endif SUPPORT_MMX}
  748. end;
  749. if assigned(withrefnode) then
  750. begin
  751. firstpass(withrefnode);
  752. if withrefnode.registers32 > registers32 then
  753. registers32:=withrefnode.registers32;
  754. if withrefnode.registersfpu > registersfpu then
  755. registers32:=withrefnode.registersfpu;
  756. {$ifdef SUPPORT_MMX}
  757. if withrefnode.registersmmx > registersmmx then
  758. registersmmx:=withrefnode.registersmmx;
  759. {$endif SUPPORT_MMX}
  760. end;
  761. end;
  762. function twithnode.docompare(p: tnode): boolean;
  763. begin
  764. docompare :=
  765. inherited docompare(p) and
  766. (withsymtable = twithnode(p).withsymtable) and
  767. (tablecount = twithnode(p).tablecount) and
  768. (withrefnode.isequal(twithnode(p).withrefnode));
  769. end;
  770. begin
  771. cloadvmtaddrnode := tloadvmtaddrnode;
  772. caddrnode := taddrnode;
  773. cderefnode := tderefnode;
  774. csubscriptnode := tsubscriptnode;
  775. cvecnode := tvecnode;
  776. cwithnode := twithnode;
  777. end.
  778. {
  779. $Log$
  780. Revision 1.71 2003-11-05 14:18:03 marco
  781. * fix from Peter arraysize warning (nav Newsgroup msg)
  782. Revision 1.70 2003/10/31 18:44:18 peter
  783. * don't search for compatible procvars when the proc is not
  784. overloaded
  785. Revision 1.69 2003/10/31 15:52:58 peter
  786. * support creating classes using <class of tobject>.create
  787. Revision 1.68 2003/10/23 14:44:07 peter
  788. * splitted buildderef and buildderefimpl to fix interface crc
  789. calculation
  790. Revision 1.67 2003/10/22 20:40:00 peter
  791. * write derefdata in a separate ppu entry
  792. Revision 1.66 2003/10/21 18:16:13 peter
  793. * IncompatibleTypes() added that will include unit names when
  794. the typenames are the same
  795. Revision 1.65 2003/10/08 19:19:45 peter
  796. * set_varstate cleanup
  797. Revision 1.64 2003/10/01 20:34:49 peter
  798. * procinfo unit contains tprocinfo
  799. * cginfo renamed to cgbase
  800. * moved cgmessage to verbose
  801. * fixed ppc and sparc compiles
  802. Revision 1.63 2003/09/28 17:55:04 peter
  803. * parent framepointer changed to hidden parameter
  804. * tloadparentfpnode added
  805. Revision 1.62 2003/09/06 22:27:08 florian
  806. * fixed web bug 2669
  807. * cosmetic fix in printnode
  808. * tobjectdef.gettypename implemented
  809. Revision 1.61 2003/09/03 11:18:37 florian
  810. * fixed arm concatcopy
  811. + arm support in the common compiler sources added
  812. * moved some generic cg code around
  813. + tfputype added
  814. * ...
  815. Revision 1.60 2003/08/10 17:25:23 peter
  816. * fixed some reported bugs
  817. Revision 1.59 2003/06/17 19:24:08 jonas
  818. * fixed conversion of fpc_*str_unique to compilerproc
  819. Revision 1.58 2003/06/17 16:34:44 jonas
  820. * lots of newra fixes (need getfuncretparaloc implementation for i386)!
  821. * renamed all_intregisters to volatile_intregisters and made it
  822. processor dependent
  823. Revision 1.57 2003/06/07 20:26:32 peter
  824. * re-resolving added instead of reloading from ppu
  825. * tderef object added to store deref info for resolving
  826. Revision 1.56 2003/06/07 18:57:04 jonas
  827. + added freeintparaloc
  828. * ppc get/freeintparaloc now check whether the parameter regs are
  829. properly allocated/deallocated (and get an extra list para)
  830. * ppc a_call_* now internalerrors if pi_do_call is not yet set
  831. * fixed lot of missing pi_do_call's
  832. Revision 1.55 2003/05/24 17:15:24 jonas
  833. * added missing firstpass for withrefnode
  834. Revision 1.54 2003/05/11 14:45:12 peter
  835. * tloadnode does not support objectsymtable,withsymtable anymore
  836. * withnode cleanup
  837. * direct with rewritten to use temprefnode
  838. Revision 1.53 2003/05/09 17:47:02 peter
  839. * self moved to hidden parameter
  840. * removed hdisposen,hnewn,selfn
  841. Revision 1.52 2003/05/05 14:53:16 peter
  842. * vs_hidden replaced by is_hidden boolean
  843. Revision 1.51 2003/04/27 11:21:33 peter
  844. * aktprocdef renamed to current_procdef
  845. * procinfo renamed to current_procinfo
  846. * procinfo will now be stored in current_module so it can be
  847. cleaned up properly
  848. * gen_main_procsym changed to create_main_proc and release_main_proc
  849. to also generate a tprocinfo structure
  850. * fixed unit implicit initfinal
  851. Revision 1.50 2003/04/27 07:29:50 peter
  852. * current_procdef cleanup, current_procdef is now always nil when parsing
  853. a new procdef declaration
  854. * aktprocsym removed
  855. * lexlevel removed, use symtable.symtablelevel instead
  856. * implicit init/final code uses the normal genentry/genexit
  857. * funcret state checking updated for new funcret handling
  858. Revision 1.49 2003/04/23 10:10:54 peter
  859. * procvar is not compared in addrn
  860. Revision 1.48 2003/04/22 23:50:23 peter
  861. * firstpass uses expectloc
  862. * checks if there are differences between the expectloc and
  863. location.loc from secondpass in EXTDEBUG
  864. Revision 1.47 2003/04/10 17:57:52 peter
  865. * vs_hidden released
  866. Revision 1.46 2003/01/30 21:46:57 peter
  867. * self fixes for static methods (merged)
  868. Revision 1.45 2003/01/09 21:52:37 peter
  869. * merged some verbosity options.
  870. * V_LineInfo is a verbosity flag to include line info
  871. Revision 1.44 2003/01/06 21:16:52 peter
  872. * po_addressonly added to retrieve the address of a methodpointer
  873. only, this is used for @tclass.method which has no self pointer
  874. Revision 1.43 2003/01/04 15:54:03 daniel
  875. * Fixed mark_write for @ operator
  876. (can happen when compiling @procvar:=nil (Delphi mode construction))
  877. Revision 1.42 2003/01/03 12:15:56 daniel
  878. * Removed ifdefs around notifications
  879. ifdefs around for loop optimizations remain
  880. Revision 1.41 2002/11/25 17:43:20 peter
  881. * splitted defbase in defutil,symutil,defcmp
  882. * merged isconvertable and is_equal into compare_defs(_ext)
  883. * made operator search faster by walking the list only once
  884. Revision 1.40 2002/09/27 21:13:28 carl
  885. * low-highval always checked if limit ober 2GB is reached (to avoid overflow)
  886. Revision 1.39 2002/09/01 18:44:17 peter
  887. * cleanup of tvecnode.det_resulttype
  888. * move 0 element of string access check to resulttype
  889. Revision 1.38 2002/09/01 13:28:38 daniel
  890. - write_access fields removed in favor of a flag
  891. Revision 1.37 2002/09/01 08:01:16 daniel
  892. * Removed sets from Tcallnode.det_resulttype
  893. + Added read/write notifications of variables. These will be usefull
  894. for providing information for several optimizations. For example
  895. the value of the loop variable of a for loop does matter is the
  896. variable is read after the for loop, but if it's no longer used
  897. or written, it doesn't matter and this can be used to optimize
  898. the loop code generation.
  899. Revision 1.36 2002/08/19 19:36:43 peter
  900. * More fixes for cross unit inlining, all tnodes are now implemented
  901. * Moved pocall_internconst to po_internconst because it is not a
  902. calling type at all and it conflicted when inlining of these small
  903. functions was requested
  904. Revision 1.35 2002/07/23 09:51:23 daniel
  905. * Tried to make Tprocsym.defs protected. I didn't succeed but the cleanups
  906. are worth comitting.
  907. Revision 1.34 2002/07/20 11:57:54 florian
  908. * types.pas renamed to defbase.pas because D6 contains a types
  909. unit so this would conflicts if D6 programms are compiled
  910. + Willamette/SSE2 instructions to assembler added
  911. Revision 1.33 2002/05/18 13:34:10 peter
  912. * readded missing revisions
  913. Revision 1.32 2002/05/16 19:46:39 carl
  914. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  915. + try to fix temp allocation (still in ifdef)
  916. + generic constructor calls
  917. + start of tassembler / tmodulebase class cleanup
  918. Revision 1.30 2002/05/12 16:53:07 peter
  919. * moved entry and exitcode to ncgutil and cgobj
  920. * foreach gets extra argument for passing local data to the
  921. iterator function
  922. * -CR checks also class typecasts at runtime by changing them
  923. into as
  924. * fixed compiler to cycle with the -CR option
  925. * fixed stabs with elf writer, finally the global variables can
  926. be watched
  927. * removed a lot of routines from cga unit and replaced them by
  928. calls to cgobj
  929. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  930. u32bit then the other is typecasted also to u32bit without giving
  931. a rangecheck warning/error.
  932. * fixed pascal calling method with reversing also the high tree in
  933. the parast, detected by tcalcst3 test
  934. Revision 1.29 2002/04/21 19:02:04 peter
  935. * removed newn and disposen nodes, the code is now directly
  936. inlined from pexpr
  937. * -an option that will write the secondpass nodes to the .s file, this
  938. requires EXTDEBUG define to actually write the info
  939. * fixed various internal errors and crashes due recent code changes
  940. Revision 1.28 2002/04/20 21:32:23 carl
  941. + generic FPC_CHECKPOINTER
  942. + first parameter offset in stack now portable
  943. * rename some constants
  944. + move some cpu stuff to other units
  945. - remove unused constents
  946. * fix stacksize for some targets
  947. * fix generic size problems which depend now on EXTEND_SIZE constant
  948. Revision 1.27 2002/04/02 17:11:29 peter
  949. * tlocation,treference update
  950. * LOC_CONSTANT added for better constant handling
  951. * secondadd splitted in multiple routines
  952. * location_force_reg added for loading a location to a register
  953. of a specified size
  954. * secondassignment parses now first the right and then the left node
  955. (this is compatible with Kylix). This saves a lot of push/pop especially
  956. with string operations
  957. * adapted some routines to use the new cg methods
  958. Revision 1.26 2002/04/01 20:57:13 jonas
  959. * fixed web bug 1907
  960. * fixed some other procvar related bugs (all related to accepting procvar
  961. constructs with either too many or too little parameters)
  962. (both merged, includes second typo fix of pexpr.pas)
  963. }