nmem.pas 36 KB

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