tcmem.pas 20 KB

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