tcmem.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. {$ifdef ag386bin}
  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. if p^.left^.treetype=calln then
  150. begin
  151. { it could also be a procvar, not only pprocsym ! }
  152. if p^.left^.symtableprocentry^.typ=varsym then
  153. hp:=genloadnode(pvarsym(p^.left^.symtableprocentry),p^.left^.symtableproc)
  154. else
  155. hp:=genloadcallnode(pprocsym(p^.left^.symtableprocentry),p^.left^.symtableproc);
  156. { result is a procedure variable }
  157. { No, to be TP compatible, you must return a pointer to
  158. the procedure that is stored in the procvar.}
  159. if not(m_tp_procvar in aktmodeswitches) then
  160. begin
  161. p^.resulttype:=new(pprocvardef,init);
  162. { it could also be a procvar, not only pprocsym ! }
  163. if p^.left^.symtableprocentry^.typ=varsym then
  164. hp3:=pabstractprocdef(pvarsym(p^.left^.symtableprocentry)^.definition)
  165. else
  166. hp3:=pabstractprocdef(pprocsym(p^.left^.symtableprocentry)^.definition);
  167. pprocvardef(p^.resulttype)^.options:=hp3^.options;
  168. pprocvardef(p^.resulttype)^.retdef:=hp3^.retdef;
  169. hp2:=hp3^.para1;
  170. while assigned(hp2) do
  171. begin
  172. pprocvardef(p^.resulttype)^.concatdef(hp2^.data,hp2^.paratyp);
  173. hp2:=hp2^.next;
  174. end;
  175. end
  176. else
  177. p^.resulttype:=voidpointerdef;
  178. disposetree(p^.left);
  179. p^.left:=hp;
  180. end
  181. else
  182. begin
  183. { what are we getting the address from an absolute sym? }
  184. hp:=p^.left;
  185. while assigned(hp) and (hp^.treetype in [vecn,subscriptn]) do
  186. hp:=hp^.left;
  187. if assigned(hp) and (hp^.treetype=loadn) and
  188. ((hp^.symtableentry^.typ=absolutesym) and
  189. pabsolutesym(hp^.symtableentry)^.absseg) then
  190. begin
  191. if not(cs_typed_addresses in aktlocalswitches) then
  192. p^.resulttype:=voidfarpointerdef
  193. else
  194. p^.resulttype:=new(pfarpointerdef,init(p^.left^.resulttype));
  195. end
  196. else
  197. begin
  198. if not(cs_typed_addresses in aktlocalswitches) then
  199. p^.resulttype:=voidpointerdef
  200. else
  201. p^.resulttype:=new(ppointerdef,init(p^.left^.resulttype));
  202. end;
  203. end;
  204. end;
  205. store_valid:=must_be_valid;
  206. must_be_valid:=false;
  207. firstpass(p^.left);
  208. must_be_valid:=store_valid;
  209. if codegenerror then
  210. exit;
  211. { we should allow loc_mem for @string }
  212. if not(p^.left^.location.loc in [LOC_MEM,LOC_REFERENCE]) then
  213. CGMessage(cg_e_illegal_expression);
  214. p^.registers32:=p^.left^.registers32;
  215. p^.registersfpu:=p^.left^.registersfpu;
  216. {$ifdef SUPPORT_MMX}
  217. p^.registersmmx:=p^.left^.registersmmx;
  218. {$endif SUPPORT_MMX}
  219. if p^.registers32<1 then
  220. p^.registers32:=1;
  221. p^.location.loc:=LOC_REGISTER;
  222. end;
  223. {*****************************************************************************
  224. FirstDoubleAddr
  225. *****************************************************************************}
  226. procedure firstdoubleaddr(var p : ptree);
  227. begin
  228. make_not_regable(p^.left);
  229. firstpass(p^.left);
  230. if p^.resulttype=nil then
  231. p^.resulttype:=voidpointerdef;
  232. if codegenerror then
  233. exit;
  234. if (p^.left^.resulttype^.deftype)<>procvardef then
  235. CGMessage(cg_e_illegal_expression);
  236. if (p^.left^.location.loc<>LOC_REFERENCE) then
  237. CGMessage(cg_e_illegal_expression);
  238. p^.registers32:=p^.left^.registers32;
  239. p^.registersfpu:=p^.left^.registersfpu;
  240. {$ifdef SUPPORT_MMX}
  241. p^.registersmmx:=p^.left^.registersmmx;
  242. {$endif SUPPORT_MMX}
  243. if p^.registers32<1 then
  244. p^.registers32:=1;
  245. p^.location.loc:=LOC_REGISTER;
  246. end;
  247. {*****************************************************************************
  248. FirstDeRef
  249. *****************************************************************************}
  250. procedure firstderef(var p : ptree);
  251. begin
  252. firstpass(p^.left);
  253. if codegenerror then
  254. begin
  255. p^.resulttype:=generrordef;
  256. exit;
  257. end;
  258. p^.registers32:=max(p^.left^.registers32,1);
  259. p^.registersfpu:=p^.left^.registersfpu;
  260. {$ifdef SUPPORT_MMX}
  261. p^.registersmmx:=p^.left^.registersmmx;
  262. {$endif SUPPORT_MMX}
  263. if not(p^.left^.resulttype^.deftype in [pointerdef,farpointerdef]) then
  264. CGMessage(cg_e_invalid_qualifier);
  265. p^.resulttype:=ppointerdef(p^.left^.resulttype)^.definition;
  266. p^.location.loc:=LOC_REFERENCE;
  267. end;
  268. {*****************************************************************************
  269. FirstSubScript
  270. *****************************************************************************}
  271. procedure firstsubscript(var p : ptree);
  272. begin
  273. firstpass(p^.left);
  274. if codegenerror then
  275. begin
  276. p^.resulttype:=generrordef;
  277. exit;
  278. end;
  279. p^.resulttype:=p^.vs^.definition;
  280. { this must be done in the parser
  281. if count_ref and not must_be_valid then
  282. if (p^.vs^.properties and sp_protected)<>0 then
  283. CGMessage(parser_e_cant_write_protected_member);
  284. }
  285. p^.registers32:=p^.left^.registers32;
  286. p^.registersfpu:=p^.left^.registersfpu;
  287. {$ifdef SUPPORT_MMX}
  288. p^.registersmmx:=p^.left^.registersmmx;
  289. {$endif SUPPORT_MMX}
  290. { classes must be dereferenced implicit }
  291. if (p^.left^.resulttype^.deftype=objectdef) and
  292. pobjectdef(p^.left^.resulttype)^.isclass then
  293. begin
  294. if p^.registers32=0 then
  295. p^.registers32:=1;
  296. p^.location.loc:=LOC_REFERENCE;
  297. end
  298. else
  299. begin
  300. if (p^.left^.location.loc<>LOC_MEM) and
  301. (p^.left^.location.loc<>LOC_REFERENCE) then
  302. CGMessage(cg_e_illegal_expression);
  303. set_location(p^.location,p^.left^.location);
  304. end;
  305. end;
  306. {*****************************************************************************
  307. FirstVec
  308. *****************************************************************************}
  309. procedure firstvec(var p : ptree);
  310. var
  311. harr : pdef;
  312. ct : tconverttype;
  313. {$ifdef consteval}
  314. tcsym : ptypedconstsym;
  315. {$endif}
  316. begin
  317. firstpass(p^.left);
  318. firstpass(p^.right);
  319. if codegenerror then
  320. exit;
  321. { range check only for arrays }
  322. if (p^.left^.resulttype^.deftype=arraydef) then
  323. begin
  324. if (isconvertable(p^.right^.resulttype,parraydef(p^.left^.resulttype)^.rangedef,
  325. ct,ordconstn,false)=0) and
  326. not(is_equal(p^.right^.resulttype,parraydef(p^.left^.resulttype)^.rangedef)) then
  327. CGMessage(type_e_mismatch);
  328. end;
  329. { Never convert a boolean or a char !}
  330. { maybe type conversion }
  331. if (p^.right^.resulttype^.deftype<>enumdef) and
  332. not(is_char(p^.right^.resulttype)) and
  333. not(is_boolean(p^.right^.resulttype)) then
  334. begin
  335. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  336. firstpass(p^.right);
  337. if codegenerror then
  338. exit;
  339. end;
  340. { determine return type }
  341. if not assigned(p^.resulttype) then
  342. if p^.left^.resulttype^.deftype=arraydef then
  343. p^.resulttype:=parraydef(p^.left^.resulttype)^.definition
  344. else if (p^.left^.resulttype^.deftype=pointerdef) then
  345. begin
  346. { convert pointer to array }
  347. harr:=new(parraydef,init(0,$7fffffff,s32bitdef));
  348. parraydef(harr)^.definition:=ppointerdef(p^.left^.resulttype)^.definition;
  349. p^.left:=gentypeconvnode(p^.left,harr);
  350. firstpass(p^.left);
  351. if codegenerror then
  352. exit;
  353. p^.resulttype:=parraydef(harr)^.definition
  354. end
  355. else if p^.left^.resulttype^.deftype=stringdef then
  356. begin
  357. { indexed access to strings }
  358. case pstringdef(p^.left^.resulttype)^.string_typ of
  359. {
  360. st_widestring : p^.resulttype:=cwchardef;
  361. }
  362. st_ansistring : p^.resulttype:=cchardef;
  363. st_longstring : p^.resulttype:=cchardef;
  364. st_shortstring : p^.resulttype:=cchardef;
  365. end;
  366. end
  367. else
  368. CGMessage(type_e_mismatch);
  369. { the register calculation is easy if a const index is used }
  370. if p^.right^.treetype=ordconstn then
  371. begin
  372. {$ifdef consteval}
  373. { constant evaluation }
  374. if (p^.left^.treetype=loadn) and
  375. (p^.left^.symtableentry^.typ=typedconstsym) then
  376. begin
  377. tcsym:=ptypedconstsym(p^.left^.symtableentry);
  378. if tcsym^.defintion^.typ=stringdef then
  379. begin
  380. end;
  381. end;
  382. {$endif}
  383. p^.registers32:=p^.left^.registers32;
  384. { for ansi/wide strings, we need at least one register }
  385. if is_ansistring(p^.left^.resulttype) or
  386. is_widestring(p^.left^.resulttype) then
  387. p^.registers32:=max(p^.registers32,1);
  388. end
  389. else
  390. begin
  391. { this rules are suboptimal, but they should give }
  392. { good results }
  393. p^.registers32:=max(p^.left^.registers32,p^.right^.registers32);
  394. { for ansi/wide strings, we need at least one register }
  395. if is_ansistring(p^.left^.resulttype) or
  396. is_widestring(p^.left^.resulttype) then
  397. p^.registers32:=max(p^.registers32,1);
  398. { need we an extra register when doing the restore ? }
  399. if (p^.left^.registers32<=p^.right^.registers32) and
  400. { only if the node needs less than 3 registers }
  401. { two for the right node and one for the }
  402. { left address }
  403. (p^.registers32<3) then
  404. inc(p^.registers32);
  405. { need we an extra register for the index ? }
  406. if (p^.right^.location.loc<>LOC_REGISTER)
  407. { only if the right node doesn't need a register }
  408. and (p^.right^.registers32<1) then
  409. inc(p^.registers32);
  410. { not correct, but what works better ?
  411. if p^.left^.registers32>0 then
  412. p^.registers32:=max(p^.registers32,2)
  413. else
  414. min. one register
  415. p^.registers32:=max(p^.registers32,1);
  416. }
  417. end;
  418. p^.registersfpu:=max(p^.left^.registersfpu,p^.right^.registersfpu);
  419. {$ifdef SUPPORT_MMX}
  420. p^.registersmmx:=max(p^.left^.registersmmx,p^.right^.registersmmx);
  421. {$endif SUPPORT_MMX}
  422. if p^.left^.location.loc in [LOC_CREGISTER,LOC_REFERENCE] then
  423. p^.location.loc:=LOC_REFERENCE
  424. else
  425. p^.location.loc:=LOC_MEM;
  426. end;
  427. {*****************************************************************************
  428. FirstSelf
  429. *****************************************************************************}
  430. procedure firstself(var p : ptree);
  431. begin
  432. if (p^.resulttype^.deftype=classrefdef) or
  433. ((p^.resulttype^.deftype=objectdef)
  434. and pobjectdef(p^.resulttype)^.isclass
  435. ) then
  436. p^.location.loc:=LOC_CREGISTER
  437. else
  438. p^.location.loc:=LOC_REFERENCE;
  439. end;
  440. {*****************************************************************************
  441. FirstWithN
  442. *****************************************************************************}
  443. procedure firstwith(var p : ptree);
  444. var
  445. symtable : pwithsymtable;
  446. i : longint;
  447. begin
  448. if assigned(p^.left) and assigned(p^.right) then
  449. begin
  450. firstpass(p^.left);
  451. if codegenerror then
  452. exit;
  453. {$ifndef NODIRECTWITH}
  454. symtable:=p^.withsymtable;
  455. for i:=1 to p^.tablecount do
  456. begin
  457. if (p^.left^.treetype=loadn) and
  458. (p^.left^.symtable=aktprocsym^.definition^.localst) then
  459. symtable^.direct_with:=true;
  460. symtable^.withnode:=p;
  461. symtable:=pwithsymtable(symtable^.next);
  462. end;
  463. {$endif ndef NODIRECTWITH}
  464. firstpass(p^.right);
  465. if codegenerror then
  466. exit;
  467. left_right_max(p);
  468. p^.resulttype:=voiddef;
  469. end
  470. else
  471. begin
  472. { optimization }
  473. disposetree(p);
  474. p:=nil;
  475. end;
  476. end;
  477. end.
  478. {
  479. $Log$
  480. Revision 1.12 1999-03-02 18:24:24 peter
  481. * fixed overloading of array of char
  482. Revision 1.11 1999/02/22 02:15:54 peter
  483. * updates for ag386bin
  484. Revision 1.10 1999/02/04 11:44:47 florian
  485. * fixed indexed access of ansistrings to temp. ansistring, i.e.
  486. c:=(s1+s2)[i], the temp is now correctly remove and the generated
  487. code is also fixed
  488. Revision 1.9 1999/01/22 12:18:34 pierre
  489. * with bug introduced with DIRECTWITH removed
  490. Revision 1.8 1999/01/21 16:41:08 pierre
  491. * fix for constructor inside with statements
  492. Revision 1.7 1998/12/30 22:15:59 peter
  493. + farpointer type
  494. * absolutesym now also stores if its far
  495. Revision 1.6 1998/12/15 17:16:02 peter
  496. * fixed const s : ^string
  497. * first things for const pchar : @string[1]
  498. Revision 1.5 1998/12/11 00:03:57 peter
  499. + globtype,tokens,version unit splitted from globals
  500. Revision 1.4 1998/11/25 19:12:53 pierre
  501. * var:=new(pointer_type) support added
  502. Revision 1.3 1998/09/26 15:03:05 florian
  503. * small problems with DOM and excpetions fixed (code generation
  504. of raise was wrong and self was sometimes destroyed :()
  505. Revision 1.2 1998/09/24 23:49:24 peter
  506. + aktmodeswitches
  507. Revision 1.1 1998/09/23 20:42:24 peter
  508. * splitted pass_1
  509. }