tcmem.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. if not(cs_typed_addresses in aktlocalswitches) then
  180. p^.resulttype:=voidpointerdef
  181. else p^.resulttype:=new(ppointerdef,init(p^.left^.resulttype));
  182. end;
  183. end;
  184. store_valid:=must_be_valid;
  185. must_be_valid:=false;
  186. firstpass(p^.left);
  187. must_be_valid:=store_valid;
  188. if codegenerror then
  189. exit;
  190. { we should allow loc_mem for @string }
  191. if (p^.left^.location.loc<>LOC_REFERENCE) and
  192. (p^.left^.location.loc<>LOC_MEM) then
  193. CGMessage(cg_e_illegal_expression);
  194. p^.registers32:=p^.left^.registers32;
  195. p^.registersfpu:=p^.left^.registersfpu;
  196. {$ifdef SUPPORT_MMX}
  197. p^.registersmmx:=p^.left^.registersmmx;
  198. {$endif SUPPORT_MMX}
  199. if p^.registers32<1 then
  200. p^.registers32:=1;
  201. p^.location.loc:=LOC_REGISTER;
  202. end;
  203. {*****************************************************************************
  204. FirstDoubleAddr
  205. *****************************************************************************}
  206. procedure firstdoubleaddr(var p : ptree);
  207. begin
  208. make_not_regable(p^.left);
  209. firstpass(p^.left);
  210. if p^.resulttype=nil then
  211. p^.resulttype:=voidpointerdef;
  212. if codegenerror then
  213. exit;
  214. if (p^.left^.resulttype^.deftype)<>procvardef then
  215. CGMessage(cg_e_illegal_expression);
  216. if (p^.left^.location.loc<>LOC_REFERENCE) then
  217. CGMessage(cg_e_illegal_expression);
  218. p^.registers32:=p^.left^.registers32;
  219. p^.registersfpu:=p^.left^.registersfpu;
  220. {$ifdef SUPPORT_MMX}
  221. p^.registersmmx:=p^.left^.registersmmx;
  222. {$endif SUPPORT_MMX}
  223. if p^.registers32<1 then
  224. p^.registers32:=1;
  225. p^.location.loc:=LOC_REGISTER;
  226. end;
  227. {*****************************************************************************
  228. FirstDeRef
  229. *****************************************************************************}
  230. procedure firstderef(var p : ptree);
  231. begin
  232. firstpass(p^.left);
  233. if codegenerror then
  234. begin
  235. p^.resulttype:=generrordef;
  236. exit;
  237. end;
  238. p^.registers32:=max(p^.left^.registers32,1);
  239. p^.registersfpu:=p^.left^.registersfpu;
  240. {$ifdef SUPPORT_MMX}
  241. p^.registersmmx:=p^.left^.registersmmx;
  242. {$endif SUPPORT_MMX}
  243. if p^.left^.resulttype^.deftype<>pointerdef then
  244. CGMessage(cg_e_invalid_qualifier);
  245. p^.resulttype:=ppointerdef(p^.left^.resulttype)^.definition;
  246. p^.location.loc:=LOC_REFERENCE;
  247. end;
  248. {*****************************************************************************
  249. FirstSubScript
  250. *****************************************************************************}
  251. procedure firstsubscript(var p : ptree);
  252. begin
  253. firstpass(p^.left);
  254. if codegenerror then
  255. begin
  256. p^.resulttype:=generrordef;
  257. exit;
  258. end;
  259. p^.resulttype:=p^.vs^.definition;
  260. { this must be done in the parser
  261. if count_ref and not must_be_valid then
  262. if (p^.vs^.properties and sp_protected)<>0 then
  263. CGMessage(parser_e_cant_write_protected_member);
  264. }
  265. p^.registers32:=p^.left^.registers32;
  266. p^.registersfpu:=p^.left^.registersfpu;
  267. {$ifdef SUPPORT_MMX}
  268. p^.registersmmx:=p^.left^.registersmmx;
  269. {$endif SUPPORT_MMX}
  270. { classes must be dereferenced implicit }
  271. if (p^.left^.resulttype^.deftype=objectdef) and
  272. pobjectdef(p^.left^.resulttype)^.isclass then
  273. begin
  274. if p^.registers32=0 then
  275. p^.registers32:=1;
  276. p^.location.loc:=LOC_REFERENCE;
  277. end
  278. else
  279. begin
  280. if (p^.left^.location.loc<>LOC_MEM) and
  281. (p^.left^.location.loc<>LOC_REFERENCE) then
  282. CGMessage(cg_e_illegal_expression);
  283. set_location(p^.location,p^.left^.location);
  284. end;
  285. end;
  286. {*****************************************************************************
  287. FirstVec
  288. *****************************************************************************}
  289. procedure firstvec(var p : ptree);
  290. var
  291. harr : pdef;
  292. ct : tconverttype;
  293. begin
  294. firstpass(p^.left);
  295. firstpass(p^.right);
  296. if codegenerror then
  297. exit;
  298. { range check only for arrays }
  299. if (p^.left^.resulttype^.deftype=arraydef) then
  300. begin
  301. if not(isconvertable(p^.right^.resulttype,
  302. parraydef(p^.left^.resulttype)^.rangedef,
  303. ct,ordconstn,false)) and
  304. not(is_equal(p^.right^.resulttype,
  305. parraydef(p^.left^.resulttype)^.rangedef)) then
  306. CGMessage(type_e_mismatch);
  307. end;
  308. { Never convert a boolean or a char !}
  309. { maybe type conversion }
  310. if (p^.right^.resulttype^.deftype<>enumdef) and
  311. not ((p^.right^.resulttype^.deftype=orddef) and
  312. (Porddef(p^.right^.resulttype)^.typ in [bool8bit,bool16bit,bool32bit,uchar])) then
  313. begin
  314. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  315. { once more firstpass }
  316. {?? It's better to only firstpass when the tree has
  317. changed, isn't it ?}
  318. firstpass(p^.right);
  319. end;
  320. if codegenerror then
  321. exit;
  322. { determine return type }
  323. if not assigned(p^.resulttype) then
  324. if p^.left^.resulttype^.deftype=arraydef then
  325. p^.resulttype:=parraydef(p^.left^.resulttype)^.definition
  326. else if (p^.left^.resulttype^.deftype=pointerdef) then
  327. begin
  328. { convert pointer to array }
  329. harr:=new(parraydef,init(0,$7fffffff,s32bitdef));
  330. parraydef(harr)^.definition:=ppointerdef(p^.left^.resulttype)^.definition;
  331. p^.left:=gentypeconvnode(p^.left,harr);
  332. firstpass(p^.left);
  333. if codegenerror then
  334. exit;
  335. p^.resulttype:=parraydef(harr)^.definition
  336. end
  337. else if p^.left^.resulttype^.deftype=stringdef then
  338. begin
  339. { indexed access to strings }
  340. case pstringdef(p^.left^.resulttype)^.string_typ of
  341. {
  342. st_widestring : p^.resulttype:=cwchardef;
  343. }
  344. st_ansistring : p^.resulttype:=cchardef;
  345. st_longstring : p^.resulttype:=cchardef;
  346. st_shortstring : p^.resulttype:=cchardef;
  347. end;
  348. end
  349. else
  350. CGMessage(type_e_mismatch);
  351. { the register calculation is easy if a const index is used }
  352. if p^.right^.treetype=ordconstn then
  353. begin
  354. p^.registers32:=p^.left^.registers32;
  355. { for ansi/wide strings, we need at least one register }
  356. if is_ansistring(p^.left^.resulttype) or
  357. is_widestring(p^.left^.resulttype) then
  358. p^.registers32:=max(p^.registers32,1);
  359. end
  360. else
  361. begin
  362. { this rules are suboptimal, but they should give }
  363. { good results }
  364. p^.registers32:=max(p^.left^.registers32,p^.right^.registers32);
  365. { for ansi/wide strings, we need at least one register }
  366. if is_ansistring(p^.left^.resulttype) or
  367. is_widestring(p^.left^.resulttype) then
  368. p^.registers32:=max(p^.registers32,1);
  369. { need we an extra register when doing the restore ? }
  370. if (p^.left^.registers32<=p^.right^.registers32) and
  371. { only if the node needs less than 3 registers }
  372. { two for the right node and one for the }
  373. { left address }
  374. (p^.registers32<3) then
  375. inc(p^.registers32);
  376. { need we an extra register for the index ? }
  377. if (p^.right^.location.loc<>LOC_REGISTER)
  378. { only if the right node doesn't need a register }
  379. and (p^.right^.registers32<1) then
  380. inc(p^.registers32);
  381. { not correct, but what works better ?
  382. if p^.left^.registers32>0 then
  383. p^.registers32:=max(p^.registers32,2)
  384. else
  385. min. one register
  386. p^.registers32:=max(p^.registers32,1);
  387. }
  388. end;
  389. p^.registersfpu:=max(p^.left^.registersfpu,p^.right^.registersfpu);
  390. {$ifdef SUPPORT_MMX}
  391. p^.registersmmx:=max(p^.left^.registersmmx,p^.right^.registersmmx);
  392. {$endif SUPPORT_MMX}
  393. p^.location.loc:=p^.left^.location.loc;
  394. end;
  395. {*****************************************************************************
  396. FirstSelf
  397. *****************************************************************************}
  398. procedure firstself(var p : ptree);
  399. begin
  400. if (p^.resulttype^.deftype=classrefdef) or
  401. ((p^.resulttype^.deftype=objectdef)
  402. and pobjectdef(p^.resulttype)^.isclass
  403. ) then
  404. p^.location.loc:=LOC_CREGISTER
  405. else
  406. p^.location.loc:=LOC_REFERENCE;
  407. end;
  408. {*****************************************************************************
  409. FirstWithN
  410. *****************************************************************************}
  411. procedure firstwith(var p : ptree);
  412. begin
  413. if assigned(p^.left) and assigned(p^.right) then
  414. begin
  415. firstpass(p^.left);
  416. if codegenerror then
  417. exit;
  418. firstpass(p^.right);
  419. if codegenerror then
  420. exit;
  421. left_right_max(p);
  422. p^.resulttype:=voiddef;
  423. end
  424. else
  425. begin
  426. { optimization }
  427. disposetree(p);
  428. p:=nil;
  429. end;
  430. end;
  431. end.
  432. {
  433. $Log$
  434. Revision 1.5 1998-12-11 00:03:57 peter
  435. + globtype,tokens,version unit splitted from globals
  436. Revision 1.4 1998/11/25 19:12:53 pierre
  437. * var:=new(pointer_type) support added
  438. Revision 1.3 1998/09/26 15:03:05 florian
  439. * small problems with DOM and excpetions fixed (code generation
  440. of raise was wrong and self was sometimes destroyed :()
  441. Revision 1.2 1998/09/24 23:49:24 peter
  442. + aktmodeswitches
  443. Revision 1.1 1998/09/23 20:42:24 peter
  444. * splitted pass_1
  445. }