nmem.pas 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. {
  2. $Id$
  3. Copyright (c) 2000 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 defines.inc}
  20. interface
  21. uses
  22. node,symtable,cpubase;
  23. type
  24. tloadvmtnode = class(tnode)
  25. constructor create;virtual;
  26. function pass_1 : tnode;override;
  27. end;
  28. thnewnode = class(tnode)
  29. constructor create;virtual;
  30. function pass_1 : tnode;override;
  31. end;
  32. tnewnode = class(tunarynode)
  33. constructor create(l : tnode);virtual;
  34. function pass_1 : tnode;override;
  35. end;
  36. thdisposenode = class(tunarynode)
  37. constructor create(l : tnode);virtual;
  38. function pass_1 : tnode;override;
  39. end;
  40. tsimplenewdisposenode = class(tunarynode)
  41. constructor create(n : tnodetype;l : tnode);
  42. function pass_1 : tnode;override;
  43. end;
  44. taddrnode = class(tunarynode)
  45. constructor create(l : tnode);virtual;
  46. function pass_1 : tnode;override;
  47. end;
  48. tdoubleaddrnode = class(tunarynode)
  49. constructor create(l : tnode);virtual;
  50. function pass_1 : tnode;override;
  51. end;
  52. tderefnode = class(tunarynode)
  53. constructor create(l : tnode);virtual;
  54. function pass_1 : tnode;override;
  55. end;
  56. tsubscriptnode = class(tunarynode)
  57. vs : pvarsym;
  58. constructor create(varsym : psym;l : tnode);virtual;
  59. function getcopy : tnode;
  60. function pass_1 : tnode;override;
  61. end;
  62. tvecnode = class(tbinarynode)
  63. constructor create(l,r : tnode);virtual;
  64. function pass_1 : tnode;override;
  65. end;
  66. tselfnode = class(tnode)
  67. constructor create(_class : pdef);virtual;
  68. function pass_1 : tnode;override;
  69. end;
  70. twithnode = class(tbinarynode)
  71. {$IFDEF NEWST}
  72. withsymtables : Pcollection;
  73. withreference : preference;
  74. {$ELSE}
  75. withsymtable : pwithsymtable;
  76. tablecount : longint;
  77. withreference:preference;
  78. {$ENDIF NEWST}
  79. constructor create(symtable : pwithsymtable;l,r : tnode;count : longint);virtual;
  80. function getcopy : tnode;
  81. function pass_1 : tnode;override;
  82. end;
  83. function gensubscriptnode(varsym : pvarsym;l : tnode) : tsubscriptnode;
  84. function genselfnode(_class : pdef) : tselfnode;
  85. {$IFDEF NEWST}
  86. function genwithnode(symtables:Pcollection;l,r : tnode) : twithnode;
  87. {$ELSE}
  88. function genwithnode(symtable:pwithsymtable;l,r : tnode;count : longint) : twithnode;
  89. {$ENDIF NEWST}
  90. var
  91. cloadvmtnode : class of tloadvmtnode;
  92. chnewnode : class of thnewnode;
  93. cnewnode : class of tnewnode;
  94. chdisposenode : class of thdisposenode;
  95. csimplenewdisposenode : class of tsimplenewdisposenode;
  96. caddrnode : class of taddrnode;
  97. cdoubleaddrnode : class of tdoubleaddrnode;
  98. cderefnode : class of tderefnode;
  99. csubscriptnode : class of tsubscriptnode;
  100. cvecnode : class of tvecnode;
  101. cselfnode : class of tselfnode;
  102. cwithnode : class of twithnode;
  103. implementation
  104. uses
  105. globtype,systems,
  106. cutils,cobjects,verbose,globals,
  107. symconst,aasm,types,
  108. htypechk,pass_1,ncal,nld,ncon,ncnv
  109. {$ifdef newcg}
  110. ,cgbase
  111. {$else newcg}
  112. ,hcodegen
  113. {$endif newcg}
  114. ;
  115. function genselfnode(_class : pdef) : tselfnode;
  116. var
  117. p : tnode;
  118. begin
  119. genselfnode:=cselfnode.create(_class);
  120. end;
  121. {$IFDEF NEWST}
  122. function genwithnode(symtables:Pcollection;l,r : tnode) : tnode;
  123. var
  124. p : tnode;
  125. begin
  126. !!!!!!!!! fixme
  127. p:=getnode;
  128. disposetyp:=dt_with;
  129. nodetype:=withn;
  130. left:=l;
  131. right:=r;
  132. registers32:=0;
  133. {$ifdef SUPPORT_MMX}
  134. registersmmx:=0;
  135. {$endif SUPPORT_MMX}
  136. resulttype:=nil;
  137. withsymtables:=symtables;
  138. withreference:=nil;
  139. set_file_line(l,p);
  140. genwithnode:=p;
  141. end;
  142. {$ELSE}
  143. function genwithnode(symtable : pwithsymtable;l,r : tnode;count : longint) : twithnode;
  144. begin
  145. genwithnode:=cwithnode.create(symtable,l,r,count);
  146. end;
  147. {$ENDIF NEWST}
  148. function gensubscriptnode(varsym : pvarsym;l : tnode) : tsubscriptnode;
  149. begin
  150. gensubscriptnode:=csubscriptnode.create(varsym,l);
  151. end;
  152. {*****************************************************************************
  153. TLOADVMTNODE
  154. *****************************************************************************}
  155. constructor tloadvmtnode.create;
  156. begin
  157. inherited create(loadvmtn);
  158. end;
  159. function tloadvmtnode.pass_1 : tnode;
  160. begin
  161. pass_1:=nil;
  162. registers32:=1;
  163. location.loc:=LOC_REGISTER;
  164. end;
  165. {*****************************************************************************
  166. THNEWNODE
  167. *****************************************************************************}
  168. constructor thnewnode.create;
  169. begin
  170. inherited create(hnewn);
  171. end;
  172. function thnewnode.pass_1 : tnode;
  173. begin
  174. pass_1:=nil;
  175. end;
  176. {*****************************************************************************
  177. TNEWNODE
  178. *****************************************************************************}
  179. constructor tnewnode.create(l : tnode);
  180. begin
  181. inherited create(newn,l);
  182. end;
  183. function tnewnode.pass_1 : tnode;
  184. begin
  185. pass_1:=nil;
  186. if assigned(left) then
  187. firstpass(left);
  188. if codegenerror then
  189. exit;
  190. if assigned(left) then
  191. begin
  192. registers32:=left.registers32;
  193. registersfpu:=left.registersfpu;
  194. {$ifdef SUPPORT_MMX}
  195. registersmmx:=left.registersmmx;
  196. {$endif SUPPORT_MMX}
  197. end;
  198. { result type is already set }
  199. procinfo^.flags:=procinfo^.flags or pi_do_call;
  200. if assigned(left) then
  201. location.loc:=LOC_REGISTER
  202. else
  203. location.loc:=LOC_REFERENCE;
  204. end;
  205. {*****************************************************************************
  206. THDISPOSENODE
  207. *****************************************************************************}
  208. constructor thdisposenode.create(l : tnode);
  209. begin
  210. inherited create(hdisposen,l);
  211. end;
  212. function thdisposenode.pass_1 : tnode;
  213. begin
  214. pass_1:=nil;
  215. firstpass(left);
  216. if codegenerror then
  217. exit;
  218. registers32:=left.registers32;
  219. registersfpu:=left.registersfpu;
  220. {$ifdef SUPPORT_MMX}
  221. registersmmx:=left.registersmmx;
  222. {$endif SUPPORT_MMX}
  223. if registers32<1 then
  224. registers32:=1;
  225. {
  226. if left.location.loc<>LOC_REFERENCE then
  227. CGMessage(cg_e_illegal_expression);
  228. }
  229. if left.location.loc=LOC_CREGISTER then
  230. inc(registers32);
  231. location.loc:=LOC_REFERENCE;
  232. resulttype:=ppointerdef(left.resulttype)^.pointertype.def;
  233. end;
  234. {*****************************************************************************
  235. TSIMPLENEWDISPOSENODE
  236. *****************************************************************************}
  237. constructor tsimplenewdisposenode.create(n : tnodetype;l : tnode);
  238. begin
  239. inherited create(n,l);
  240. end;
  241. function tsimplenewdisposenode.pass_1 : tnode;
  242. begin
  243. pass_1:=nil;
  244. { this cannot be in a register !! }
  245. make_not_regable(left);
  246. firstpass(left);
  247. if codegenerror then
  248. exit;
  249. { check the type }
  250. if left.resulttype=nil then
  251. left.resulttype:=generrordef;
  252. if (left.resulttype^.deftype<>pointerdef) then
  253. CGMessage1(type_e_pointer_type_expected,left.resulttype^.typename);
  254. if (left.location.loc<>LOC_REFERENCE) {and
  255. (left.location.loc<>LOC_CREGISTER)} then
  256. CGMessage(cg_e_illegal_expression);
  257. registers32:=left.registers32;
  258. registersfpu:=left.registersfpu;
  259. {$ifdef SUPPORT_MMX}
  260. registersmmx:=left.registersmmx;
  261. {$endif SUPPORT_MMX}
  262. resulttype:=voiddef;
  263. procinfo^.flags:=procinfo^.flags or pi_do_call;
  264. end;
  265. {*****************************************************************************
  266. TADDRNODE
  267. *****************************************************************************}
  268. constructor taddrnode.create(l : tnode);
  269. begin
  270. inherited create(addrn,l);
  271. end;
  272. function taddrnode.pass_1 : tnode;
  273. var
  274. hp : tnode;
  275. hp2 : pparaitem;
  276. hp3 : pabstractprocdef;
  277. begin
  278. pass_1:=nil;
  279. make_not_regable(left);
  280. if not(assigned(resulttype)) then
  281. begin
  282. { tp @procvar support (type of @procvar is a void pointer)
  283. Note: we need to leave the addrn in the tree,
  284. else we can't see the difference between @procvar and procvar.
  285. we set the procvarload flag so a secondpass does nothing for
  286. this node (PFV) }
  287. if (m_tp_procvar in aktmodeswitches) then
  288. begin
  289. hp:=left;
  290. case hp.nodetype of
  291. calln :
  292. begin
  293. { is it a procvar? }
  294. hp:=tcallnode(hp).right;
  295. if assigned(hp) then
  296. begin
  297. { remove calln node }
  298. tcallnode(left).right:=nil;
  299. left.free;
  300. left:=hp;
  301. firstpass(hp);
  302. include(flags,nf_procvarload);
  303. end;
  304. end;
  305. loadn,
  306. subscriptn,
  307. typeconvn,
  308. vecn,
  309. derefn :
  310. begin
  311. firstpass(hp);
  312. if codegenerror then
  313. exit;
  314. if hp.resulttype^.deftype=procvardef then
  315. include(flags,nf_procvarload);
  316. end;
  317. end;
  318. end;
  319. if nf_procvarload in flags then
  320. begin
  321. registers32:=left.registers32;
  322. registersfpu:=left.registersfpu;
  323. {$ifdef SUPPORT_MMX}
  324. registersmmx:=left.registersmmx;
  325. {$endif SUPPORT_MMX}
  326. if registers32<1 then
  327. registers32:=1;
  328. location.loc:=left.location.loc;
  329. resulttype:=voidpointerdef;
  330. exit;
  331. end;
  332. { proc 2 procvar ? }
  333. if left.nodetype=calln then
  334. begin
  335. { generate a methodcallnode or proccallnode }
  336. { we shouldn't convert things like @tcollection.load }
  337. if (tcallnode(left).symtableprocentry^.owner^.symtabletype=objectsymtable) and
  338. not(assigned(tcallnode(left).methodpointer) and (tcallnode(left).methodpointer.nodetype=typen)) then
  339. begin
  340. hp:=genloadmethodcallnode(pprocsym(tcallnode(left).symtableprocentry),tcallnode(left).symtableproc,
  341. tcallnode(left).methodpointer.getcopy);
  342. firstpass(hp);
  343. pass_1:=hp;
  344. exit;
  345. end
  346. else
  347. hp:=genloadcallnode(pprocsym(tcallnode(left).symtableprocentry),
  348. tcallnode(left).symtableproc);
  349. { result is a procedure variable }
  350. { No, to be TP compatible, you must return a pointer to
  351. the procedure that is stored in the procvar.}
  352. if not(m_tp_procvar in aktmodeswitches) then
  353. begin
  354. resulttype:=new(pprocvardef,init);
  355. { it could also be a procvar, not only pprocsym ! }
  356. if tcallnode(left).symtableprocentry^.typ=varsym then
  357. hp3:=pabstractprocdef(pvarsym(tloadnode(left).symtableentry)^.vartype.def)
  358. else
  359. hp3:=pabstractprocdef(pprocsym(tcallnode(left).symtableprocentry)^.definition);
  360. pprocvardef(resulttype)^.proctypeoption:=hp3^.proctypeoption;
  361. pprocvardef(resulttype)^.proccalloptions:=hp3^.proccalloptions;
  362. pprocvardef(resulttype)^.procoptions:=hp3^.procoptions;
  363. pprocvardef(resulttype)^.rettype:=hp3^.rettype;
  364. pprocvardef(resulttype)^.symtablelevel:=hp3^.symtablelevel;
  365. { method ? then set the methodpointer flag }
  366. if (hp3^.owner^.symtabletype=objectsymtable) and
  367. (pobjectdef(hp3^.owner^.defowner)^.is_class) then
  368. include(pprocvardef(resulttype)^.procoptions,po_methodpointer);
  369. { we need to process the parameters reverse so they are inserted
  370. in the correct right2left order (PFV) }
  371. hp2:=pparaitem(hp3^.para^.last);
  372. while assigned(hp2) do
  373. begin
  374. pprocvardef(resulttype)^.concatpara(hp2^.paratype,hp2^.paratyp,hp2^.defaultvalue);
  375. hp2:=pparaitem(hp2^.previous);
  376. end;
  377. end
  378. else
  379. resulttype:=voidpointerdef;
  380. left.free;
  381. left:=hp;
  382. end
  383. else
  384. begin
  385. firstpass(left);
  386. { what are we getting the address from an absolute sym? }
  387. hp:=left;
  388. while assigned(hp) and (hp.nodetype in [vecn,derefn,subscriptn]) do
  389. hp:=tunarynode(hp).left;
  390. if assigned(hp) and (hp.nodetype=loadn) and
  391. ((tloadnode(hp).symtableentry^.typ=absolutesym) and
  392. pabsolutesym(tloadnode(hp).symtableentry)^.absseg) then
  393. begin
  394. if not(cs_typed_addresses in aktlocalswitches) then
  395. resulttype:=voidfarpointerdef
  396. else
  397. resulttype:=new(ppointerdef,initfardef(left.resulttype));
  398. end
  399. else
  400. begin
  401. if not(cs_typed_addresses in aktlocalswitches) then
  402. resulttype:=voidpointerdef
  403. else
  404. resulttype:=new(ppointerdef,initdef(left.resulttype));
  405. end;
  406. end;
  407. end;
  408. firstpass(left);
  409. { this is like the function addr }
  410. inc(parsing_para_level);
  411. left.set_varstate(false);
  412. dec(parsing_para_level);
  413. if codegenerror then
  414. exit;
  415. { don't allow constants }
  416. if is_constnode(left) then
  417. begin
  418. aktfilepos:=left.fileinfo;
  419. CGMessage(type_e_no_addr_of_constant);
  420. end
  421. else
  422. begin
  423. { we should allow loc_mem for @string }
  424. if not(left.location.loc in [LOC_MEM,LOC_REFERENCE]) then
  425. begin
  426. aktfilepos:=left.fileinfo;
  427. CGMessage(cg_e_illegal_expression);
  428. end;
  429. end;
  430. registers32:=left.registers32;
  431. registersfpu:=left.registersfpu;
  432. {$ifdef SUPPORT_MMX}
  433. registersmmx:=left.registersmmx;
  434. {$endif SUPPORT_MMX}
  435. if registers32<1 then
  436. registers32:=1;
  437. { is this right for object of methods ?? }
  438. location.loc:=LOC_REGISTER;
  439. end;
  440. {*****************************************************************************
  441. TDOUBLEADDRNODE
  442. *****************************************************************************}
  443. constructor tdoubleaddrnode.create(l : tnode);
  444. begin
  445. inherited create(doubleaddrn,l);
  446. end;
  447. function tdoubleaddrnode.pass_1 : tnode;
  448. begin
  449. pass_1:=nil;
  450. make_not_regable(left);
  451. firstpass(left);
  452. inc(parsing_para_level);
  453. left.set_varstate(false);
  454. dec(parsing_para_level);
  455. if resulttype=nil then
  456. resulttype:=voidpointerdef;
  457. if codegenerror then
  458. exit;
  459. if (left.resulttype^.deftype)<>procvardef then
  460. CGMessage(cg_e_illegal_expression);
  461. if (left.location.loc<>LOC_REFERENCE) then
  462. CGMessage(cg_e_illegal_expression);
  463. registers32:=left.registers32;
  464. registersfpu:=left.registersfpu;
  465. {$ifdef SUPPORT_MMX}
  466. registersmmx:=left.registersmmx;
  467. {$endif SUPPORT_MMX}
  468. if registers32<1 then
  469. registers32:=1;
  470. location.loc:=LOC_REGISTER;
  471. end;
  472. {*****************************************************************************
  473. TDEREFNODE
  474. *****************************************************************************}
  475. constructor tderefnode.create(l : tnode);
  476. begin
  477. inherited create(derefn,l);
  478. end;
  479. function tderefnode.pass_1 : tnode;
  480. begin
  481. pass_1:=nil;
  482. firstpass(left);
  483. left.set_varstate(true);
  484. if codegenerror then
  485. begin
  486. resulttype:=generrordef;
  487. exit;
  488. end;
  489. registers32:=max(left.registers32,1);
  490. registersfpu:=left.registersfpu;
  491. {$ifdef SUPPORT_MMX}
  492. registersmmx:=left.registersmmx;
  493. {$endif SUPPORT_MMX}
  494. if left.resulttype^.deftype<>pointerdef then
  495. CGMessage(cg_e_invalid_qualifier);
  496. resulttype:=ppointerdef(left.resulttype)^.pointertype.def;
  497. location.loc:=LOC_REFERENCE;
  498. end;
  499. {*****************************************************************************
  500. TSUBSCRIPTNODE
  501. *****************************************************************************}
  502. constructor tsubscriptnode.create(varsym : psym;l : tnode);
  503. begin
  504. inherited create(subscriptn,l);
  505. { vs should be changed to psym! }
  506. vs:=pvarsym(varsym);
  507. end;
  508. function tsubscriptnode.getcopy : tnode;
  509. var
  510. p : tsubscriptnode;
  511. begin
  512. p:=tsubscriptnode(inherited getcopy);
  513. p.vs:=vs;
  514. getcopy:=p;
  515. end;
  516. function tsubscriptnode.pass_1 : tnode;
  517. begin
  518. pass_1:=nil;
  519. firstpass(left);
  520. if codegenerror then
  521. begin
  522. resulttype:=generrordef;
  523. exit;
  524. end;
  525. resulttype:=vs^.vartype.def;
  526. registers32:=left.registers32;
  527. registersfpu:=left.registersfpu;
  528. {$ifdef SUPPORT_MMX}
  529. registersmmx:=left.registersmmx;
  530. {$endif SUPPORT_MMX}
  531. { classes must be dereferenced implicit }
  532. if (left.resulttype^.deftype=objectdef) and
  533. pobjectdef(left.resulttype)^.is_class then
  534. begin
  535. if registers32=0 then
  536. registers32:=1;
  537. location.loc:=LOC_REFERENCE;
  538. end
  539. else
  540. begin
  541. if (left.location.loc<>LOC_MEM) and
  542. (left.location.loc<>LOC_REFERENCE) then
  543. CGMessage(cg_e_illegal_expression);
  544. set_location(location,left.location);
  545. end;
  546. end;
  547. {*****************************************************************************
  548. TVECNODE
  549. *****************************************************************************}
  550. constructor tvecnode.create(l,r : tnode);
  551. begin
  552. inherited create(vecn,l,r);
  553. end;
  554. function tvecnode.pass_1 : tnode;
  555. var
  556. harr : pdef;
  557. ct : tconverttype;
  558. {$ifdef consteval}
  559. tcsym : ptypedconstsym;
  560. {$endif}
  561. begin
  562. pass_1:=nil;
  563. firstpass(left);
  564. firstpass(right);
  565. if codegenerror then
  566. exit;
  567. { range check only for arrays }
  568. if (left.resulttype^.deftype=arraydef) then
  569. begin
  570. if (isconvertable(right.resulttype,parraydef(left.resulttype)^.rangetype.def,
  571. ct,ordconstn,false)=0) and
  572. not(is_equal(right.resulttype,parraydef(left.resulttype)^.rangetype.def)) then
  573. CGMessage(type_e_mismatch);
  574. end;
  575. { Never convert a boolean or a char !}
  576. { maybe type conversion }
  577. if (right.resulttype^.deftype<>enumdef) and
  578. not(is_char(right.resulttype)) and
  579. not(is_boolean(right.resulttype)) then
  580. begin
  581. right:=gentypeconvnode(right,s32bitdef);
  582. firstpass(right);
  583. if codegenerror then
  584. exit;
  585. end;
  586. { are we accessing a pointer[], then convert the pointer to
  587. an array first, in FPC this is allowed for all pointers in
  588. delphi/tp7 it's only allowed for pchars }
  589. if (left.resulttype^.deftype=pointerdef) and
  590. ((m_fpc in aktmodeswitches) or
  591. is_pchar(left.resulttype)) then
  592. begin
  593. { convert pointer to array }
  594. harr:=new(parraydef,init(0,$7fffffff,s32bitdef));
  595. parraydef(harr)^.elementtype.def:=ppointerdef(left.resulttype)^.pointertype.def;
  596. left:=gentypeconvnode(left,harr);
  597. firstpass(left);
  598. if codegenerror then
  599. exit;
  600. resulttype:=parraydef(harr)^.elementtype.def
  601. end;
  602. { determine return type }
  603. if not assigned(resulttype) then
  604. if left.resulttype^.deftype=arraydef then
  605. resulttype:=parraydef(left.resulttype)^.elementtype.def
  606. else if left.resulttype^.deftype=stringdef then
  607. begin
  608. { indexed access to strings }
  609. case pstringdef(left.resulttype)^.string_typ of
  610. {
  611. st_widestring : resulttype:=cwchardef;
  612. }
  613. st_ansistring : resulttype:=cchardef;
  614. st_longstring : resulttype:=cchardef;
  615. st_shortstring : resulttype:=cchardef;
  616. end;
  617. end
  618. else
  619. CGMessage(type_e_array_required);
  620. { the register calculation is easy if a const index is used }
  621. if right.nodetype=ordconstn then
  622. begin
  623. {$ifdef consteval}
  624. { constant evaluation }
  625. if (left.nodetype=loadn) and
  626. (left.symtableentry^.typ=typedconstsym) then
  627. begin
  628. tcsym:=ptypedconstsym(left.symtableentry);
  629. if tcsym^.defintion^.typ=stringdef then
  630. begin
  631. end;
  632. end;
  633. {$endif}
  634. registers32:=left.registers32;
  635. { for ansi/wide strings, we need at least one register }
  636. if is_ansistring(left.resulttype) or
  637. is_widestring(left.resulttype) then
  638. registers32:=max(registers32,1);
  639. end
  640. else
  641. begin
  642. { this rules are suboptimal, but they should give }
  643. { good results }
  644. registers32:=max(left.registers32,right.registers32);
  645. { for ansi/wide strings, we need at least one register }
  646. if is_ansistring(left.resulttype) or
  647. is_widestring(left.resulttype) then
  648. registers32:=max(registers32,1);
  649. { need we an extra register when doing the restore ? }
  650. if (left.registers32<=right.registers32) and
  651. { only if the node needs less than 3 registers }
  652. { two for the right node and one for the }
  653. { left address }
  654. (registers32<3) then
  655. inc(registers32);
  656. { need we an extra register for the index ? }
  657. if (right.location.loc<>LOC_REGISTER)
  658. { only if the right node doesn't need a register }
  659. and (right.registers32<1) then
  660. inc(registers32);
  661. { not correct, but what works better ?
  662. if left.registers32>0 then
  663. registers32:=max(registers32,2)
  664. else
  665. min. one register
  666. registers32:=max(registers32,1);
  667. }
  668. end;
  669. registersfpu:=max(left.registersfpu,right.registersfpu);
  670. {$ifdef SUPPORT_MMX}
  671. registersmmx:=max(left.registersmmx,right.registersmmx);
  672. {$endif SUPPORT_MMX}
  673. if left.location.loc in [LOC_CREGISTER,LOC_REFERENCE] then
  674. location.loc:=LOC_REFERENCE
  675. else
  676. location.loc:=LOC_MEM;
  677. end;
  678. {*****************************************************************************
  679. TSELFNODE
  680. *****************************************************************************}
  681. constructor tselfnode.create(_class : pdef);
  682. begin
  683. inherited create(selfn);
  684. resulttype:=_class;
  685. end;
  686. function tselfnode.pass_1 : tnode;
  687. begin
  688. pass_1:=nil;
  689. if (resulttype^.deftype=classrefdef) or
  690. ((resulttype^.deftype=objectdef)
  691. and pobjectdef(resulttype)^.is_class
  692. ) then
  693. location.loc:=LOC_CREGISTER
  694. else
  695. location.loc:=LOC_REFERENCE;
  696. end;
  697. {*****************************************************************************
  698. TWITHNODE
  699. *****************************************************************************}
  700. constructor twithnode.create(symtable : pwithsymtable;l,r : tnode;count : longint);
  701. begin
  702. inherited create(withn,l,r);
  703. withsymtable:=symtable;
  704. tablecount:=count;
  705. withreference:=nil;
  706. set_file_line(l);
  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. p.withreference:=withreference;
  716. end;
  717. function twithnode.pass_1 : tnode;
  718. var
  719. symtable : pwithsymtable;
  720. i : longint;
  721. begin
  722. pass_1:=nil;
  723. if assigned(left) and assigned(right) then
  724. begin
  725. firstpass(left);
  726. left.unset_varstate;
  727. left.set_varstate(true);
  728. if codegenerror then
  729. exit;
  730. symtable:=withsymtable;
  731. for i:=1 to tablecount do
  732. begin
  733. if (left.nodetype=loadn) and
  734. (tloadnode(left).symtable=aktprocsym^.definition^.localst) then
  735. symtable^.direct_with:=true;
  736. symtable^.withnode:=self;
  737. symtable:=pwithsymtable(symtable^.next);
  738. end;
  739. firstpass(right);
  740. if codegenerror then
  741. exit;
  742. left_right_max;
  743. resulttype:=voiddef;
  744. end
  745. else
  746. begin
  747. { optimization }
  748. pass_1:=nil;
  749. end;
  750. end;
  751. end.
  752. {
  753. $Log$
  754. Revision 1.4 2000-09-28 19:49:52 florian
  755. *** empty log message ***
  756. Revision 1.3 2000/09/25 15:37:14 florian
  757. * more fixes
  758. Revision 1.2 2000/09/25 15:05:25 florian
  759. * some updates
  760. Revision 1.1 2000/09/25 09:58:22 florian
  761. * first revision for testing purpose
  762. }