tcmem.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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 not(isconvertable(p^.right^.resulttype,
  325. parraydef(p^.left^.resulttype)^.rangedef,
  326. ct,ordconstn,false)) and
  327. not(is_equal(p^.right^.resulttype,
  328. parraydef(p^.left^.resulttype)^.rangedef)) then
  329. CGMessage(type_e_mismatch);
  330. end;
  331. { Never convert a boolean or a char !}
  332. { maybe type conversion }
  333. if (p^.right^.resulttype^.deftype<>enumdef) and
  334. not(is_char(p^.right^.resulttype)) and
  335. not(is_boolean(p^.right^.resulttype)) then
  336. begin
  337. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  338. firstpass(p^.right);
  339. if codegenerror then
  340. exit;
  341. end;
  342. { determine return type }
  343. if not assigned(p^.resulttype) then
  344. if p^.left^.resulttype^.deftype=arraydef then
  345. p^.resulttype:=parraydef(p^.left^.resulttype)^.definition
  346. else if (p^.left^.resulttype^.deftype=pointerdef) then
  347. begin
  348. { convert pointer to array }
  349. harr:=new(parraydef,init(0,$7fffffff,s32bitdef));
  350. parraydef(harr)^.definition:=ppointerdef(p^.left^.resulttype)^.definition;
  351. p^.left:=gentypeconvnode(p^.left,harr);
  352. firstpass(p^.left);
  353. if codegenerror then
  354. exit;
  355. p^.resulttype:=parraydef(harr)^.definition
  356. end
  357. else if p^.left^.resulttype^.deftype=stringdef then
  358. begin
  359. { indexed access to strings }
  360. case pstringdef(p^.left^.resulttype)^.string_typ of
  361. {
  362. st_widestring : p^.resulttype:=cwchardef;
  363. }
  364. st_ansistring : p^.resulttype:=cchardef;
  365. st_longstring : p^.resulttype:=cchardef;
  366. st_shortstring : p^.resulttype:=cchardef;
  367. end;
  368. end
  369. else
  370. CGMessage(type_e_mismatch);
  371. { the register calculation is easy if a const index is used }
  372. if p^.right^.treetype=ordconstn then
  373. begin
  374. {$ifdef consteval}
  375. { constant evaluation }
  376. if (p^.left^.treetype=loadn) and
  377. (p^.left^.symtableentry^.typ=typedconstsym) then
  378. begin
  379. tcsym:=ptypedconstsym(p^.left^.symtableentry);
  380. if tcsym^.defintion^.typ=stringdef then
  381. begin
  382. end;
  383. end;
  384. {$endif}
  385. p^.registers32:=p^.left^.registers32;
  386. { for ansi/wide strings, we need at least one register }
  387. if is_ansistring(p^.left^.resulttype) or
  388. is_widestring(p^.left^.resulttype) then
  389. p^.registers32:=max(p^.registers32,1);
  390. end
  391. else
  392. begin
  393. { this rules are suboptimal, but they should give }
  394. { good results }
  395. p^.registers32:=max(p^.left^.registers32,p^.right^.registers32);
  396. { for ansi/wide strings, we need at least one register }
  397. if is_ansistring(p^.left^.resulttype) or
  398. is_widestring(p^.left^.resulttype) then
  399. p^.registers32:=max(p^.registers32,1);
  400. { need we an extra register when doing the restore ? }
  401. if (p^.left^.registers32<=p^.right^.registers32) and
  402. { only if the node needs less than 3 registers }
  403. { two for the right node and one for the }
  404. { left address }
  405. (p^.registers32<3) then
  406. inc(p^.registers32);
  407. { need we an extra register for the index ? }
  408. if (p^.right^.location.loc<>LOC_REGISTER)
  409. { only if the right node doesn't need a register }
  410. and (p^.right^.registers32<1) then
  411. inc(p^.registers32);
  412. { not correct, but what works better ?
  413. if p^.left^.registers32>0 then
  414. p^.registers32:=max(p^.registers32,2)
  415. else
  416. min. one register
  417. p^.registers32:=max(p^.registers32,1);
  418. }
  419. end;
  420. p^.registersfpu:=max(p^.left^.registersfpu,p^.right^.registersfpu);
  421. {$ifdef SUPPORT_MMX}
  422. p^.registersmmx:=max(p^.left^.registersmmx,p^.right^.registersmmx);
  423. {$endif SUPPORT_MMX}
  424. if p^.left^.location.loc in [LOC_CREGISTER,LOC_REFERENCE] then
  425. p^.location.loc:=LOC_REFERENCE
  426. else
  427. p^.location.loc:=LOC_MEM;
  428. end;
  429. {*****************************************************************************
  430. FirstSelf
  431. *****************************************************************************}
  432. procedure firstself(var p : ptree);
  433. begin
  434. if (p^.resulttype^.deftype=classrefdef) or
  435. ((p^.resulttype^.deftype=objectdef)
  436. and pobjectdef(p^.resulttype)^.isclass
  437. ) then
  438. p^.location.loc:=LOC_CREGISTER
  439. else
  440. p^.location.loc:=LOC_REFERENCE;
  441. end;
  442. {*****************************************************************************
  443. FirstWithN
  444. *****************************************************************************}
  445. procedure firstwith(var p : ptree);
  446. var
  447. symtable : pwithsymtable;
  448. i : longint;
  449. begin
  450. if assigned(p^.left) and assigned(p^.right) then
  451. begin
  452. firstpass(p^.left);
  453. if codegenerror then
  454. exit;
  455. {$ifndef NODIRECTWITH}
  456. symtable:=p^.withsymtable;
  457. for i:=1 to p^.tablecount do
  458. begin
  459. if (p^.left^.treetype=loadn) and
  460. (p^.left^.symtable=aktprocsym^.definition^.localst) then
  461. symtable^.direct_with:=true;
  462. symtable^.withnode:=p;
  463. symtable:=pwithsymtable(symtable^.next);
  464. end;
  465. {$endif ndef NODIRECTWITH}
  466. firstpass(p^.right);
  467. if codegenerror then
  468. exit;
  469. left_right_max(p);
  470. p^.resulttype:=voiddef;
  471. end
  472. else
  473. begin
  474. { optimization }
  475. disposetree(p);
  476. p:=nil;
  477. end;
  478. end;
  479. end.
  480. {
  481. $Log$
  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. }