nmem.pas 36 KB

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