tcmem.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 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 tcmem;
  19. interface
  20. uses
  21. tree;
  22. procedure firstloadvmt(var p : ptree);
  23. procedure firsthnew(var p : ptree);
  24. procedure firstnew(var p : ptree);
  25. procedure firsthdispose(var p : ptree);
  26. procedure firstsimplenewdispose(var p : ptree);
  27. procedure firstaddr(var p : ptree);
  28. procedure firstdoubleaddr(var p : ptree);
  29. procedure firstderef(var p : ptree);
  30. procedure firstsubscript(var p : ptree);
  31. procedure firstvec(var p : ptree);
  32. procedure firstself(var p : ptree);
  33. procedure firstwith(var p : ptree);
  34. implementation
  35. uses
  36. globtype,systems,
  37. cobjects,verbose,globals,
  38. symtable,aasm,types,
  39. hcodegen,htypechk,pass_1
  40. {$ifdef i386}
  41. ,i386base
  42. {$endif}
  43. {$ifdef m68k}
  44. ,m68k
  45. {$endif}
  46. ;
  47. {*****************************************************************************
  48. FirstLoadVMT
  49. *****************************************************************************}
  50. procedure firstloadvmt(var p : ptree);
  51. begin
  52. p^.registers32:=1;
  53. p^.location.loc:=LOC_REGISTER;
  54. end;
  55. {*****************************************************************************
  56. FirstHNew
  57. *****************************************************************************}
  58. procedure firsthnew(var p : ptree);
  59. begin
  60. end;
  61. {*****************************************************************************
  62. FirstNewN
  63. *****************************************************************************}
  64. procedure firstnew(var p : ptree);
  65. begin
  66. { Standardeinleitung }
  67. if assigned(p^.left) then
  68. firstpass(p^.left);
  69. if codegenerror then
  70. exit;
  71. if assigned(p^.left) then
  72. begin
  73. p^.registers32:=p^.left^.registers32;
  74. p^.registersfpu:=p^.left^.registersfpu;
  75. {$ifdef SUPPORT_MMX}
  76. p^.registersmmx:=p^.left^.registersmmx;
  77. {$endif SUPPORT_MMX}
  78. end;
  79. { result type is already set }
  80. procinfo.flags:=procinfo.flags or pi_do_call;
  81. if assigned(p^.left) then
  82. p^.location.loc:=LOC_REGISTER
  83. else
  84. p^.location.loc:=LOC_REFERENCE;
  85. end;
  86. {*****************************************************************************
  87. FirstDispose
  88. *****************************************************************************}
  89. procedure firsthdispose(var p : ptree);
  90. begin
  91. firstpass(p^.left);
  92. if codegenerror then
  93. exit;
  94. p^.registers32:=p^.left^.registers32;
  95. p^.registersfpu:=p^.left^.registersfpu;
  96. {$ifdef SUPPORT_MMX}
  97. p^.registersmmx:=p^.left^.registersmmx;
  98. {$endif SUPPORT_MMX}
  99. if p^.registers32<1 then
  100. p^.registers32:=1;
  101. {
  102. if p^.left^.location.loc<>LOC_REFERENCE then
  103. CGMessage(cg_e_illegal_expression);
  104. }
  105. p^.location.loc:=LOC_REFERENCE;
  106. p^.resulttype:=ppointerdef(p^.left^.resulttype)^.definition;
  107. end;
  108. {*****************************************************************************
  109. FirstSimpleNewDispose
  110. *****************************************************************************}
  111. procedure firstsimplenewdispose(var p : ptree);
  112. begin
  113. { this cannot be in a register !! }
  114. make_not_regable(p^.left);
  115. firstpass(p^.left);
  116. if codegenerror then
  117. exit;
  118. { check the type }
  119. if (p^.left^.resulttype=nil) or (p^.left^.resulttype^.deftype<>pointerdef) then
  120. CGMessage(type_e_pointer_type_expected);
  121. if (p^.left^.location.loc<>LOC_REFERENCE) {and
  122. (p^.left^.location.loc<>LOC_CREGISTER)} then
  123. CGMessage(cg_e_illegal_expression);
  124. p^.registers32:=p^.left^.registers32;
  125. p^.registersfpu:=p^.left^.registersfpu;
  126. {$ifdef SUPPORT_MMX}
  127. p^.registersmmx:=p^.left^.registersmmx;
  128. {$endif SUPPORT_MMX}
  129. p^.resulttype:=voiddef;
  130. procinfo.flags:=procinfo.flags or pi_do_call;
  131. end;
  132. {*****************************************************************************
  133. FirstAddr
  134. *****************************************************************************}
  135. procedure firstaddr(var p : ptree);
  136. var
  137. hp : ptree;
  138. hp2 : pdefcoll;
  139. store_valid : boolean;
  140. hp3 : pabstractprocdef;
  141. begin
  142. make_not_regable(p^.left);
  143. if not(assigned(p^.resulttype)) then
  144. begin
  145. { proc/procvar 2 procvar ? }
  146. if p^.left^.treetype=calln then
  147. begin
  148. { it could also be a procvar, not only pprocsym ! }
  149. if p^.left^.symtableprocentry^.typ=varsym then
  150. hp:=genloadnode(pvarsym(p^.left^.symtableentry),p^.left^.symtableproc)
  151. else
  152. begin
  153. { generate a methodcallnode or proccallnode }
  154. if (p^.left^.symtableprocentry^.owner^.symtabletype=objectsymtable) and
  155. (pobjectdef(p^.left^.symtableprocentry^.owner^.defowner)^.isclass) then
  156. begin
  157. hp:=genloadmethodcallnode(pprocsym(p^.left^.symtableprocentry),p^.left^.symtableproc,
  158. getcopy(p^.left^.methodpointer));
  159. disposetree(p);
  160. firstpass(hp);
  161. p:=hp;
  162. exit;
  163. end
  164. else
  165. hp:=genloadcallnode(pprocsym(p^.left^.symtableprocentry),p^.left^.symtableproc);
  166. end;
  167. { result is a procedure variable }
  168. { No, to be TP compatible, you must return a pointer to
  169. the procedure that is stored in the procvar.}
  170. if not(m_tp_procvar in aktmodeswitches) then
  171. begin
  172. p^.resulttype:=new(pprocvardef,init);
  173. { it could also be a procvar, not only pprocsym ! }
  174. if p^.left^.symtableprocentry^.typ=varsym then
  175. hp3:=pabstractprocdef(pvarsym(p^.left^.symtableentry)^.definition)
  176. else
  177. hp3:=pabstractprocdef(pprocsym(p^.left^.symtableprocentry)^.definition);
  178. pprocvardef(p^.resulttype)^.options:=hp3^.options;
  179. pprocvardef(p^.resulttype)^.retdef:=hp3^.retdef;
  180. { method ? then set the methodpointer flag }
  181. if (hp3^.owner^.symtabletype=objectsymtable) and
  182. (pobjectdef(hp3^.owner^.defowner)^.isclass) then
  183. pprocvardef(p^.resulttype)^.options:=pprocvardef(p^.resulttype)^.options or pomethodpointer;
  184. hp2:=hp3^.para1;
  185. while assigned(hp2) do
  186. begin
  187. pprocvardef(p^.resulttype)^.concatdef(hp2^.data,hp2^.paratyp);
  188. hp2:=hp2^.next;
  189. end;
  190. end
  191. else
  192. p^.resulttype:=voidpointerdef;
  193. disposetree(p^.left);
  194. p^.left:=hp;
  195. end
  196. else
  197. begin
  198. { what are we getting the address from an absolute sym? }
  199. hp:=p^.left;
  200. while assigned(hp) and (hp^.treetype in [vecn,subscriptn]) do
  201. hp:=hp^.left;
  202. if assigned(hp) and (hp^.treetype=loadn) and
  203. ((hp^.symtableentry^.typ=absolutesym) and
  204. pabsolutesym(hp^.symtableentry)^.absseg) then
  205. begin
  206. if not(cs_typed_addresses in aktlocalswitches) then
  207. p^.resulttype:=voidfarpointerdef
  208. else
  209. p^.resulttype:=new(ppointerdef,initfar(p^.left^.resulttype));
  210. end
  211. else
  212. begin
  213. if not(cs_typed_addresses in aktlocalswitches) then
  214. p^.resulttype:=voidpointerdef
  215. else
  216. p^.resulttype:=new(ppointerdef,init(p^.left^.resulttype));
  217. end;
  218. end;
  219. end;
  220. store_valid:=must_be_valid;
  221. must_be_valid:=false;
  222. firstpass(p^.left);
  223. must_be_valid:=store_valid;
  224. if codegenerror then
  225. exit;
  226. { we should allow loc_mem for @string }
  227. if not(p^.left^.location.loc in [LOC_MEM,LOC_REFERENCE]) then
  228. CGMessage(cg_e_illegal_expression);
  229. p^.registers32:=p^.left^.registers32;
  230. p^.registersfpu:=p^.left^.registersfpu;
  231. {$ifdef SUPPORT_MMX}
  232. p^.registersmmx:=p^.left^.registersmmx;
  233. {$endif SUPPORT_MMX}
  234. if p^.registers32<1 then
  235. p^.registers32:=1;
  236. p^.location.loc:=LOC_REGISTER;
  237. end;
  238. {*****************************************************************************
  239. FirstDoubleAddr
  240. *****************************************************************************}
  241. procedure firstdoubleaddr(var p : ptree);
  242. begin
  243. make_not_regable(p^.left);
  244. firstpass(p^.left);
  245. if p^.resulttype=nil then
  246. p^.resulttype:=voidpointerdef;
  247. if codegenerror then
  248. exit;
  249. if (p^.left^.resulttype^.deftype)<>procvardef then
  250. CGMessage(cg_e_illegal_expression);
  251. if (p^.left^.location.loc<>LOC_REFERENCE) then
  252. CGMessage(cg_e_illegal_expression);
  253. p^.registers32:=p^.left^.registers32;
  254. p^.registersfpu:=p^.left^.registersfpu;
  255. {$ifdef SUPPORT_MMX}
  256. p^.registersmmx:=p^.left^.registersmmx;
  257. {$endif SUPPORT_MMX}
  258. if p^.registers32<1 then
  259. p^.registers32:=1;
  260. p^.location.loc:=LOC_REGISTER;
  261. end;
  262. {*****************************************************************************
  263. FirstDeRef
  264. *****************************************************************************}
  265. procedure firstderef(var p : ptree);
  266. begin
  267. firstpass(p^.left);
  268. if codegenerror then
  269. begin
  270. p^.resulttype:=generrordef;
  271. exit;
  272. end;
  273. p^.registers32:=max(p^.left^.registers32,1);
  274. p^.registersfpu:=p^.left^.registersfpu;
  275. {$ifdef SUPPORT_MMX}
  276. p^.registersmmx:=p^.left^.registersmmx;
  277. {$endif SUPPORT_MMX}
  278. if p^.left^.resulttype^.deftype<>pointerdef then
  279. CGMessage(cg_e_invalid_qualifier);
  280. p^.resulttype:=ppointerdef(p^.left^.resulttype)^.definition;
  281. p^.location.loc:=LOC_REFERENCE;
  282. end;
  283. {*****************************************************************************
  284. FirstSubScript
  285. *****************************************************************************}
  286. procedure firstsubscript(var p : ptree);
  287. begin
  288. firstpass(p^.left);
  289. if codegenerror then
  290. begin
  291. p^.resulttype:=generrordef;
  292. exit;
  293. end;
  294. p^.resulttype:=p^.vs^.definition;
  295. { this must be done in the parser
  296. if count_ref and not must_be_valid then
  297. if (p^.vs^.properties and sp_protected)<>0 then
  298. CGMessage(parser_e_cant_write_protected_member);
  299. }
  300. p^.registers32:=p^.left^.registers32;
  301. p^.registersfpu:=p^.left^.registersfpu;
  302. {$ifdef SUPPORT_MMX}
  303. p^.registersmmx:=p^.left^.registersmmx;
  304. {$endif SUPPORT_MMX}
  305. { classes must be dereferenced implicit }
  306. if (p^.left^.resulttype^.deftype=objectdef) and
  307. pobjectdef(p^.left^.resulttype)^.isclass then
  308. begin
  309. if p^.registers32=0 then
  310. p^.registers32:=1;
  311. p^.location.loc:=LOC_REFERENCE;
  312. end
  313. else
  314. begin
  315. if (p^.left^.location.loc<>LOC_MEM) and
  316. (p^.left^.location.loc<>LOC_REFERENCE) then
  317. CGMessage(cg_e_illegal_expression);
  318. set_location(p^.location,p^.left^.location);
  319. end;
  320. end;
  321. {*****************************************************************************
  322. FirstVec
  323. *****************************************************************************}
  324. procedure firstvec(var p : ptree);
  325. var
  326. harr : pdef;
  327. ct : tconverttype;
  328. {$ifdef consteval}
  329. tcsym : ptypedconstsym;
  330. {$endif}
  331. begin
  332. firstpass(p^.left);
  333. firstpass(p^.right);
  334. if codegenerror then
  335. exit;
  336. { range check only for arrays }
  337. if (p^.left^.resulttype^.deftype=arraydef) then
  338. begin
  339. if (isconvertable(p^.right^.resulttype,parraydef(p^.left^.resulttype)^.rangedef,
  340. ct,ordconstn,false)=0) and
  341. not(is_equal(p^.right^.resulttype,parraydef(p^.left^.resulttype)^.rangedef)) then
  342. CGMessage(type_e_mismatch);
  343. end;
  344. { Never convert a boolean or a char !}
  345. { maybe type conversion }
  346. if (p^.right^.resulttype^.deftype<>enumdef) and
  347. not(is_char(p^.right^.resulttype)) and
  348. not(is_boolean(p^.right^.resulttype)) then
  349. begin
  350. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  351. firstpass(p^.right);
  352. if codegenerror then
  353. exit;
  354. end;
  355. { determine return type }
  356. if not assigned(p^.resulttype) then
  357. if p^.left^.resulttype^.deftype=arraydef then
  358. p^.resulttype:=parraydef(p^.left^.resulttype)^.definition
  359. else if (p^.left^.resulttype^.deftype=pointerdef) then
  360. begin
  361. { convert pointer to array }
  362. harr:=new(parraydef,init(0,$7fffffff,s32bitdef));
  363. parraydef(harr)^.definition:=ppointerdef(p^.left^.resulttype)^.definition;
  364. p^.left:=gentypeconvnode(p^.left,harr);
  365. firstpass(p^.left);
  366. if codegenerror then
  367. exit;
  368. p^.resulttype:=parraydef(harr)^.definition
  369. end
  370. else if p^.left^.resulttype^.deftype=stringdef then
  371. begin
  372. { indexed access to strings }
  373. case pstringdef(p^.left^.resulttype)^.string_typ of
  374. {
  375. st_widestring : p^.resulttype:=cwchardef;
  376. }
  377. st_ansistring : p^.resulttype:=cchardef;
  378. st_longstring : p^.resulttype:=cchardef;
  379. st_shortstring : p^.resulttype:=cchardef;
  380. end;
  381. end
  382. else
  383. CGMessage(type_e_mismatch);
  384. { the register calculation is easy if a const index is used }
  385. if p^.right^.treetype=ordconstn then
  386. begin
  387. {$ifdef consteval}
  388. { constant evaluation }
  389. if (p^.left^.treetype=loadn) and
  390. (p^.left^.symtableentry^.typ=typedconstsym) then
  391. begin
  392. tcsym:=ptypedconstsym(p^.left^.symtableentry);
  393. if tcsym^.defintion^.typ=stringdef then
  394. begin
  395. end;
  396. end;
  397. {$endif}
  398. p^.registers32:=p^.left^.registers32;
  399. { for ansi/wide strings, we need at least one register }
  400. if is_ansistring(p^.left^.resulttype) or
  401. is_widestring(p^.left^.resulttype) then
  402. p^.registers32:=max(p^.registers32,1);
  403. end
  404. else
  405. begin
  406. { this rules are suboptimal, but they should give }
  407. { good results }
  408. p^.registers32:=max(p^.left^.registers32,p^.right^.registers32);
  409. { for ansi/wide strings, we need at least one register }
  410. if is_ansistring(p^.left^.resulttype) or
  411. is_widestring(p^.left^.resulttype) then
  412. p^.registers32:=max(p^.registers32,1);
  413. { need we an extra register when doing the restore ? }
  414. if (p^.left^.registers32<=p^.right^.registers32) and
  415. { only if the node needs less than 3 registers }
  416. { two for the right node and one for the }
  417. { left address }
  418. (p^.registers32<3) then
  419. inc(p^.registers32);
  420. { need we an extra register for the index ? }
  421. if (p^.right^.location.loc<>LOC_REGISTER)
  422. { only if the right node doesn't need a register }
  423. and (p^.right^.registers32<1) then
  424. inc(p^.registers32);
  425. { not correct, but what works better ?
  426. if p^.left^.registers32>0 then
  427. p^.registers32:=max(p^.registers32,2)
  428. else
  429. min. one register
  430. p^.registers32:=max(p^.registers32,1);
  431. }
  432. end;
  433. p^.registersfpu:=max(p^.left^.registersfpu,p^.right^.registersfpu);
  434. {$ifdef SUPPORT_MMX}
  435. p^.registersmmx:=max(p^.left^.registersmmx,p^.right^.registersmmx);
  436. {$endif SUPPORT_MMX}
  437. if p^.left^.location.loc in [LOC_CREGISTER,LOC_REFERENCE] then
  438. p^.location.loc:=LOC_REFERENCE
  439. else
  440. p^.location.loc:=LOC_MEM;
  441. end;
  442. {*****************************************************************************
  443. FirstSelf
  444. *****************************************************************************}
  445. procedure firstself(var p : ptree);
  446. begin
  447. if (p^.resulttype^.deftype=classrefdef) or
  448. ((p^.resulttype^.deftype=objectdef)
  449. and pobjectdef(p^.resulttype)^.isclass
  450. ) then
  451. p^.location.loc:=LOC_CREGISTER
  452. else
  453. p^.location.loc:=LOC_REFERENCE;
  454. end;
  455. {*****************************************************************************
  456. FirstWithN
  457. *****************************************************************************}
  458. procedure firstwith(var p : ptree);
  459. var
  460. symtable : pwithsymtable;
  461. i : longint;
  462. begin
  463. if assigned(p^.left) and assigned(p^.right) then
  464. begin
  465. firstpass(p^.left);
  466. if codegenerror then
  467. exit;
  468. symtable:=p^.withsymtable;
  469. for i:=1 to p^.tablecount do
  470. begin
  471. if (p^.left^.treetype=loadn) and
  472. (p^.left^.symtable=aktprocsym^.definition^.localst) then
  473. symtable^.direct_with:=true;
  474. symtable^.withnode:=p;
  475. symtable:=pwithsymtable(symtable^.next);
  476. end;
  477. firstpass(p^.right);
  478. if codegenerror then
  479. exit;
  480. left_right_max(p);
  481. p^.resulttype:=voiddef;
  482. end
  483. else
  484. begin
  485. { optimization }
  486. disposetree(p);
  487. p:=nil;
  488. end;
  489. end;
  490. end.
  491. {
  492. $Log$
  493. Revision 1.18 1999-06-03 09:34:12 peter
  494. * better methodpointer check for proc->procvar
  495. Revision 1.17 1999/05/27 19:45:24 peter
  496. * removed oldasm
  497. * plabel -> pasmlabel
  498. * -a switches to source writing automaticly
  499. * assembler readers OOPed
  500. * asmsymbol automaticly external
  501. * jumptables and other label fixes for asm readers
  502. Revision 1.16 1999/05/18 09:52:21 peter
  503. * procedure of object and addrn fixes
  504. Revision 1.15 1999/05/17 23:51:46 peter
  505. * with temp vars now use a reference with a persistant temp instead
  506. of setting datasize
  507. Revision 1.14 1999/05/01 13:24:57 peter
  508. * merged nasm compiler
  509. * old asm moved to oldasm/
  510. Revision 1.13 1999/04/26 18:30:05 peter
  511. * farpointerdef moved into pointerdef.is_far
  512. Revision 1.12 1999/03/02 18:24:24 peter
  513. * fixed overloading of array of char
  514. Revision 1.11 1999/02/22 02:15:54 peter
  515. * updates for ag386bin
  516. Revision 1.10 1999/02/04 11:44:47 florian
  517. * fixed indexed access of ansistrings to temp. ansistring, i.e.
  518. c:=(s1+s2)[i], the temp is now correctly remove and the generated
  519. code is also fixed
  520. Revision 1.9 1999/01/22 12:18:34 pierre
  521. * with bug introduced with DIRECTWITH removed
  522. Revision 1.8 1999/01/21 16:41:08 pierre
  523. * fix for constructor inside with statements
  524. Revision 1.7 1998/12/30 22:15:59 peter
  525. + farpointer type
  526. * absolutesym now also stores if its far
  527. Revision 1.6 1998/12/15 17:16:02 peter
  528. * fixed const s : ^string
  529. * first things for const pchar : @string[1]
  530. Revision 1.5 1998/12/11 00:03:57 peter
  531. + globtype,tokens,version unit splitted from globals
  532. Revision 1.4 1998/11/25 19:12:53 pierre
  533. * var:=new(pointer_type) support added
  534. Revision 1.3 1998/09/26 15:03:05 florian
  535. * small problems with DOM and excpetions fixed (code generation
  536. of raise was wrong and self was sometimes destroyed :()
  537. Revision 1.2 1998/09/24 23:49:24 peter
  538. + aktmodeswitches
  539. Revision 1.1 1998/09/23 20:42:24 peter
  540. * splitted pass_1
  541. }