tcmem.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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. {$ifndef OLDASM}
  42. ,i386base
  43. {$else}
  44. ,i386
  45. {$endif}
  46. {$endif}
  47. {$ifdef m68k}
  48. ,m68k
  49. {$endif}
  50. ;
  51. {*****************************************************************************
  52. FirstLoadVMT
  53. *****************************************************************************}
  54. procedure firstloadvmt(var p : ptree);
  55. begin
  56. p^.registers32:=1;
  57. p^.location.loc:=LOC_REGISTER;
  58. end;
  59. {*****************************************************************************
  60. FirstHNew
  61. *****************************************************************************}
  62. procedure firsthnew(var p : ptree);
  63. begin
  64. end;
  65. {*****************************************************************************
  66. FirstNewN
  67. *****************************************************************************}
  68. procedure firstnew(var p : ptree);
  69. begin
  70. { Standardeinleitung }
  71. if assigned(p^.left) then
  72. firstpass(p^.left);
  73. if codegenerror then
  74. exit;
  75. if assigned(p^.left) then
  76. begin
  77. p^.registers32:=p^.left^.registers32;
  78. p^.registersfpu:=p^.left^.registersfpu;
  79. {$ifdef SUPPORT_MMX}
  80. p^.registersmmx:=p^.left^.registersmmx;
  81. {$endif SUPPORT_MMX}
  82. end;
  83. { result type is already set }
  84. procinfo.flags:=procinfo.flags or pi_do_call;
  85. if assigned(p^.left) then
  86. p^.location.loc:=LOC_REGISTER
  87. else
  88. p^.location.loc:=LOC_REFERENCE;
  89. end;
  90. {*****************************************************************************
  91. FirstDispose
  92. *****************************************************************************}
  93. procedure firsthdispose(var p : ptree);
  94. begin
  95. firstpass(p^.left);
  96. if codegenerror then
  97. exit;
  98. p^.registers32:=p^.left^.registers32;
  99. p^.registersfpu:=p^.left^.registersfpu;
  100. {$ifdef SUPPORT_MMX}
  101. p^.registersmmx:=p^.left^.registersmmx;
  102. {$endif SUPPORT_MMX}
  103. if p^.registers32<1 then
  104. p^.registers32:=1;
  105. {
  106. if p^.left^.location.loc<>LOC_REFERENCE then
  107. CGMessage(cg_e_illegal_expression);
  108. }
  109. p^.location.loc:=LOC_REFERENCE;
  110. p^.resulttype:=ppointerdef(p^.left^.resulttype)^.definition;
  111. end;
  112. {*****************************************************************************
  113. FirstSimpleNewDispose
  114. *****************************************************************************}
  115. procedure firstsimplenewdispose(var p : ptree);
  116. begin
  117. { this cannot be in a register !! }
  118. make_not_regable(p^.left);
  119. firstpass(p^.left);
  120. if codegenerror then
  121. exit;
  122. { check the type }
  123. if (p^.left^.resulttype=nil) or (p^.left^.resulttype^.deftype<>pointerdef) then
  124. CGMessage(type_e_pointer_type_expected);
  125. if (p^.left^.location.loc<>LOC_REFERENCE) {and
  126. (p^.left^.location.loc<>LOC_CREGISTER)} then
  127. CGMessage(cg_e_illegal_expression);
  128. p^.registers32:=p^.left^.registers32;
  129. p^.registersfpu:=p^.left^.registersfpu;
  130. {$ifdef SUPPORT_MMX}
  131. p^.registersmmx:=p^.left^.registersmmx;
  132. {$endif SUPPORT_MMX}
  133. p^.resulttype:=voiddef;
  134. procinfo.flags:=procinfo.flags or pi_do_call;
  135. end;
  136. {*****************************************************************************
  137. FirstAddr
  138. *****************************************************************************}
  139. procedure firstaddr(var p : ptree);
  140. var
  141. hp : ptree;
  142. hp2 : pdefcoll;
  143. store_valid : boolean;
  144. hp3 : pabstractprocdef;
  145. begin
  146. make_not_regable(p^.left);
  147. if not(assigned(p^.resulttype)) then
  148. begin
  149. { proc/procvar 2 procvar ? }
  150. if p^.left^.treetype=calln then
  151. begin
  152. { it could also be a procvar, not only pprocsym ! }
  153. if p^.left^.symtableprocentry^.typ=varsym then
  154. hp:=genloadnode(pvarsym(p^.left^.symtableprocentry),p^.left^.symtableproc)
  155. else
  156. begin
  157. if assigned(p^.left^.methodpointer) and
  158. (p^.left^.methodpointer^.resulttype^.deftype=objectdef) and
  159. (pobjectdef(p^.left^.methodpointer^.resulttype)^.isclass) then
  160. begin
  161. hp:=genloadmethodcallnode(pprocsym(p^.left^.symtableprocentry),p^.left^.symtableproc,
  162. getcopy(p^.left^.methodpointer));
  163. disposetree(p);
  164. firstpass(hp);
  165. p:=hp;
  166. exit;
  167. end
  168. else
  169. hp:=genloadcallnode(pprocsym(p^.left^.symtableprocentry),p^.left^.symtableproc);
  170. end;
  171. { result is a procedure variable }
  172. { No, to be TP compatible, you must return a pointer to
  173. the procedure that is stored in the procvar.}
  174. if not(m_tp_procvar in aktmodeswitches) then
  175. begin
  176. p^.resulttype:=new(pprocvardef,init);
  177. { it could also be a procvar, not only pprocsym ! }
  178. if p^.left^.symtableprocentry^.typ=varsym then
  179. hp3:=pabstractprocdef(pvarsym(p^.left^.symtableprocentry)^.definition)
  180. else
  181. hp3:=pabstractprocdef(pprocsym(p^.left^.symtableprocentry)^.definition);
  182. pprocvardef(p^.resulttype)^.options:=hp3^.options;
  183. pprocvardef(p^.resulttype)^.retdef:=hp3^.retdef;
  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.16 1999-05-18 09:52:21 peter
  494. * procedure of object and addrn fixes
  495. Revision 1.15 1999/05/17 23:51:46 peter
  496. * with temp vars now use a reference with a persistant temp instead
  497. of setting datasize
  498. Revision 1.14 1999/05/01 13:24:57 peter
  499. * merged nasm compiler
  500. * old asm moved to oldasm/
  501. Revision 1.13 1999/04/26 18:30:05 peter
  502. * farpointerdef moved into pointerdef.is_far
  503. Revision 1.12 1999/03/02 18:24:24 peter
  504. * fixed overloading of array of char
  505. Revision 1.11 1999/02/22 02:15:54 peter
  506. * updates for ag386bin
  507. Revision 1.10 1999/02/04 11:44:47 florian
  508. * fixed indexed access of ansistrings to temp. ansistring, i.e.
  509. c:=(s1+s2)[i], the temp is now correctly remove and the generated
  510. code is also fixed
  511. Revision 1.9 1999/01/22 12:18:34 pierre
  512. * with bug introduced with DIRECTWITH removed
  513. Revision 1.8 1999/01/21 16:41:08 pierre
  514. * fix for constructor inside with statements
  515. Revision 1.7 1998/12/30 22:15:59 peter
  516. + farpointer type
  517. * absolutesym now also stores if its far
  518. Revision 1.6 1998/12/15 17:16:02 peter
  519. * fixed const s : ^string
  520. * first things for const pchar : @string[1]
  521. Revision 1.5 1998/12/11 00:03:57 peter
  522. + globtype,tokens,version unit splitted from globals
  523. Revision 1.4 1998/11/25 19:12:53 pierre
  524. * var:=new(pointer_type) support added
  525. Revision 1.3 1998/09/26 15:03:05 florian
  526. * small problems with DOM and excpetions fixed (code generation
  527. of raise was wrong and self was sometimes destroyed :()
  528. Revision 1.2 1998/09/24 23:49:24 peter
  529. + aktmodeswitches
  530. Revision 1.1 1998/09/23 20:42:24 peter
  531. * splitted pass_1
  532. }