tcmem.pas 22 KB

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