tcmem.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. cobjects,verbose,globals,systems,
  37. symtable,aasm,types,
  38. hcodegen,htypechk,pass_1
  39. {$ifdef i386}
  40. ,i386
  41. {$endif}
  42. {$ifdef m68k}
  43. ,m68k
  44. {$endif}
  45. ;
  46. {*****************************************************************************
  47. FirstLoadVMT
  48. *****************************************************************************}
  49. procedure firstloadvmt(var p : ptree);
  50. begin
  51. p^.registers32:=1;
  52. p^.location.loc:=LOC_REGISTER;
  53. end;
  54. {*****************************************************************************
  55. FirstHNew
  56. *****************************************************************************}
  57. procedure firsthnew(var p : ptree);
  58. begin
  59. end;
  60. {*****************************************************************************
  61. FirstNewN
  62. *****************************************************************************}
  63. procedure firstnew(var p : ptree);
  64. begin
  65. { Standardeinleitung }
  66. firstpass(p^.left);
  67. if codegenerror then
  68. exit;
  69. p^.registers32:=p^.left^.registers32;
  70. p^.registersfpu:=p^.left^.registersfpu;
  71. {$ifdef SUPPORT_MMX}
  72. p^.registersmmx:=p^.left^.registersmmx;
  73. {$endif SUPPORT_MMX}
  74. { result type is already set }
  75. procinfo.flags:=procinfo.flags or pi_do_call;
  76. p^.location.loc:=LOC_REGISTER;
  77. end;
  78. {*****************************************************************************
  79. FirstDispose
  80. *****************************************************************************}
  81. procedure firsthdispose(var p : ptree);
  82. begin
  83. firstpass(p^.left);
  84. if codegenerror then
  85. exit;
  86. p^.registers32:=p^.left^.registers32;
  87. p^.registersfpu:=p^.left^.registersfpu;
  88. {$ifdef SUPPORT_MMX}
  89. p^.registersmmx:=p^.left^.registersmmx;
  90. {$endif SUPPORT_MMX}
  91. if p^.registers32<1 then
  92. p^.registers32:=1;
  93. {
  94. if p^.left^.location.loc<>LOC_REFERENCE then
  95. CGMessage(cg_e_illegal_expression);
  96. }
  97. p^.location.loc:=LOC_REFERENCE;
  98. p^.resulttype:=ppointerdef(p^.left^.resulttype)^.definition;
  99. end;
  100. {*****************************************************************************
  101. FirstSimpleNewDispose
  102. *****************************************************************************}
  103. procedure firstsimplenewdispose(var p : ptree);
  104. begin
  105. { this cannot be in a register !! }
  106. make_not_regable(p^.left);
  107. firstpass(p^.left);
  108. if codegenerror then
  109. exit;
  110. { check the type }
  111. if (p^.left^.resulttype=nil) or (p^.left^.resulttype^.deftype<>pointerdef) then
  112. CGMessage(type_e_pointer_type_expected);
  113. if (p^.left^.location.loc<>LOC_REFERENCE) {and
  114. (p^.left^.location.loc<>LOC_CREGISTER)} then
  115. CGMessage(cg_e_illegal_expression);
  116. p^.registers32:=p^.left^.registers32;
  117. p^.registersfpu:=p^.left^.registersfpu;
  118. {$ifdef SUPPORT_MMX}
  119. p^.registersmmx:=p^.left^.registersmmx;
  120. {$endif SUPPORT_MMX}
  121. p^.resulttype:=voiddef;
  122. procinfo.flags:=procinfo.flags or pi_do_call;
  123. end;
  124. {*****************************************************************************
  125. FirstAddr
  126. *****************************************************************************}
  127. procedure firstaddr(var p : ptree);
  128. var
  129. hp : ptree;
  130. hp2 : pdefcoll;
  131. store_valid : boolean;
  132. hp3 : pabstractprocdef;
  133. begin
  134. make_not_regable(p^.left);
  135. if not(assigned(p^.resulttype)) then
  136. begin
  137. if p^.left^.treetype=calln then
  138. begin
  139. { it could also be a procvar, not only pprocsym ! }
  140. if p^.left^.symtableprocentry^.typ=varsym then
  141. hp:=genloadnode(pvarsym(p^.left^.symtableprocentry),p^.left^.symtableproc)
  142. else
  143. hp:=genloadcallnode(pprocsym(p^.left^.symtableprocentry),p^.left^.symtableproc);
  144. { result is a procedure variable }
  145. { No, to be TP compatible, you must return a pointer to
  146. the procedure that is stored in the procvar.}
  147. if not(m_tp_procvar in aktmodeswitches) then
  148. begin
  149. p^.resulttype:=new(pprocvardef,init);
  150. { it could also be a procvar, not only pprocsym ! }
  151. if p^.left^.symtableprocentry^.typ=varsym then
  152. hp3:=pabstractprocdef(pvarsym(p^.left^.symtableprocentry)^.definition)
  153. else
  154. hp3:=pabstractprocdef(pprocsym(p^.left^.symtableprocentry)^.definition);
  155. pprocvardef(p^.resulttype)^.options:=hp3^.options;
  156. pprocvardef(p^.resulttype)^.retdef:=hp3^.retdef;
  157. hp2:=hp3^.para1;
  158. while assigned(hp2) do
  159. begin
  160. pprocvardef(p^.resulttype)^.concatdef(hp2^.data,hp2^.paratyp);
  161. hp2:=hp2^.next;
  162. end;
  163. end
  164. else
  165. p^.resulttype:=voidpointerdef;
  166. disposetree(p^.left);
  167. p^.left:=hp;
  168. end
  169. else
  170. begin
  171. if not(cs_typed_addresses in aktlocalswitches) then
  172. p^.resulttype:=voidpointerdef
  173. else p^.resulttype:=new(ppointerdef,init(p^.left^.resulttype));
  174. end;
  175. end;
  176. store_valid:=must_be_valid;
  177. must_be_valid:=false;
  178. firstpass(p^.left);
  179. must_be_valid:=store_valid;
  180. if codegenerror then
  181. exit;
  182. { we should allow loc_mem for @string }
  183. if (p^.left^.location.loc<>LOC_REFERENCE) and
  184. (p^.left^.location.loc<>LOC_MEM) then
  185. CGMessage(cg_e_illegal_expression);
  186. p^.registers32:=p^.left^.registers32;
  187. p^.registersfpu:=p^.left^.registersfpu;
  188. {$ifdef SUPPORT_MMX}
  189. p^.registersmmx:=p^.left^.registersmmx;
  190. {$endif SUPPORT_MMX}
  191. if p^.registers32<1 then
  192. p^.registers32:=1;
  193. p^.location.loc:=LOC_REGISTER;
  194. end;
  195. {*****************************************************************************
  196. FirstDoubleAddr
  197. *****************************************************************************}
  198. procedure firstdoubleaddr(var p : ptree);
  199. begin
  200. make_not_regable(p^.left);
  201. firstpass(p^.left);
  202. if p^.resulttype=nil then
  203. p^.resulttype:=voidpointerdef;
  204. if codegenerror then
  205. exit;
  206. if (p^.left^.resulttype^.deftype)<>procvardef then
  207. CGMessage(cg_e_illegal_expression);
  208. if (p^.left^.location.loc<>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. FirstDeRef
  221. *****************************************************************************}
  222. procedure firstderef(var p : ptree);
  223. begin
  224. firstpass(p^.left);
  225. if codegenerror then
  226. begin
  227. p^.resulttype:=generrordef;
  228. exit;
  229. end;
  230. p^.registers32:=max(p^.left^.registers32,1);
  231. p^.registersfpu:=p^.left^.registersfpu;
  232. {$ifdef SUPPORT_MMX}
  233. p^.registersmmx:=p^.left^.registersmmx;
  234. {$endif SUPPORT_MMX}
  235. if p^.left^.resulttype^.deftype<>pointerdef then
  236. CGMessage(cg_e_invalid_qualifier);
  237. p^.resulttype:=ppointerdef(p^.left^.resulttype)^.definition;
  238. p^.location.loc:=LOC_REFERENCE;
  239. end;
  240. {*****************************************************************************
  241. FirstSubScript
  242. *****************************************************************************}
  243. procedure firstsubscript(var p : ptree);
  244. begin
  245. firstpass(p^.left);
  246. if codegenerror then
  247. begin
  248. p^.resulttype:=generrordef;
  249. exit;
  250. end;
  251. p^.resulttype:=p^.vs^.definition;
  252. { this must be done in the parser
  253. if count_ref and not must_be_valid then
  254. if (p^.vs^.properties and sp_protected)<>0 then
  255. CGMessage(parser_e_cant_write_protected_member);
  256. }
  257. p^.registers32:=p^.left^.registers32;
  258. p^.registersfpu:=p^.left^.registersfpu;
  259. {$ifdef SUPPORT_MMX}
  260. p^.registersmmx:=p^.left^.registersmmx;
  261. {$endif SUPPORT_MMX}
  262. { classes must be dereferenced implicit }
  263. if (p^.left^.resulttype^.deftype=objectdef) and
  264. pobjectdef(p^.left^.resulttype)^.isclass then
  265. begin
  266. if p^.registers32=0 then
  267. p^.registers32:=1;
  268. p^.location.loc:=LOC_REFERENCE;
  269. end
  270. else
  271. begin
  272. if (p^.left^.location.loc<>LOC_MEM) and
  273. (p^.left^.location.loc<>LOC_REFERENCE) then
  274. CGMessage(cg_e_illegal_expression);
  275. set_location(p^.location,p^.left^.location);
  276. end;
  277. end;
  278. {*****************************************************************************
  279. FirstVec
  280. *****************************************************************************}
  281. procedure firstvec(var p : ptree);
  282. var
  283. harr : pdef;
  284. ct : tconverttype;
  285. begin
  286. firstpass(p^.left);
  287. firstpass(p^.right);
  288. if codegenerror then
  289. exit;
  290. { range check only for arrays }
  291. if (p^.left^.resulttype^.deftype=arraydef) then
  292. begin
  293. if not(isconvertable(p^.right^.resulttype,
  294. parraydef(p^.left^.resulttype)^.rangedef,
  295. ct,ordconstn,false)) and
  296. not(is_equal(p^.right^.resulttype,
  297. parraydef(p^.left^.resulttype)^.rangedef)) then
  298. CGMessage(type_e_mismatch);
  299. end;
  300. { Never convert a boolean or a char !}
  301. { maybe type conversion }
  302. if (p^.right^.resulttype^.deftype<>enumdef) and
  303. not ((p^.right^.resulttype^.deftype=orddef) and
  304. (Porddef(p^.right^.resulttype)^.typ in [bool8bit,bool16bit,bool32bit,uchar])) then
  305. begin
  306. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  307. { once more firstpass }
  308. {?? It's better to only firstpass when the tree has
  309. changed, isn't it ?}
  310. firstpass(p^.right);
  311. end;
  312. if codegenerror then
  313. exit;
  314. { determine return type }
  315. if not assigned(p^.resulttype) then
  316. if p^.left^.resulttype^.deftype=arraydef then
  317. p^.resulttype:=parraydef(p^.left^.resulttype)^.definition
  318. else if (p^.left^.resulttype^.deftype=pointerdef) then
  319. begin
  320. { convert pointer to array }
  321. harr:=new(parraydef,init(0,$7fffffff,s32bitdef));
  322. parraydef(harr)^.definition:=ppointerdef(p^.left^.resulttype)^.definition;
  323. p^.left:=gentypeconvnode(p^.left,harr);
  324. firstpass(p^.left);
  325. if codegenerror then
  326. exit;
  327. p^.resulttype:=parraydef(harr)^.definition
  328. end
  329. else if p^.left^.resulttype^.deftype=stringdef then
  330. begin
  331. { indexed access to strings }
  332. case pstringdef(p^.left^.resulttype)^.string_typ of
  333. {
  334. st_widestring : p^.resulttype:=cwchardef;
  335. }
  336. st_ansistring : p^.resulttype:=cchardef;
  337. st_longstring : p^.resulttype:=cchardef;
  338. st_shortstring : p^.resulttype:=cchardef;
  339. end;
  340. end
  341. else
  342. CGMessage(type_e_mismatch);
  343. { the register calculation is easy if a const index is used }
  344. if p^.right^.treetype=ordconstn then
  345. begin
  346. p^.registers32:=p^.left^.registers32;
  347. { for ansi/wide strings, we need at least one register }
  348. if is_ansistring(p^.left^.resulttype) or
  349. is_widestring(p^.left^.resulttype) then
  350. p^.registers32:=max(p^.registers32,1);
  351. end
  352. else
  353. begin
  354. { this rules are suboptimal, but they should give }
  355. { good results }
  356. p^.registers32:=max(p^.left^.registers32,p^.right^.registers32);
  357. { for ansi/wide strings, we need at least one register }
  358. if is_ansistring(p^.left^.resulttype) or
  359. is_widestring(p^.left^.resulttype) then
  360. p^.registers32:=max(p^.registers32,1);
  361. { need we an extra register when doing the restore ? }
  362. if (p^.left^.registers32<=p^.right^.registers32) and
  363. { only if the node needs less than 3 registers }
  364. { two for the right node and one for the }
  365. { left address }
  366. (p^.registers32<3) then
  367. inc(p^.registers32);
  368. { need we an extra register for the index ? }
  369. if (p^.right^.location.loc<>LOC_REGISTER)
  370. { only if the right node doesn't need a register }
  371. and (p^.right^.registers32<1) then
  372. inc(p^.registers32);
  373. { not correct, but what works better ?
  374. if p^.left^.registers32>0 then
  375. p^.registers32:=max(p^.registers32,2)
  376. else
  377. min. one register
  378. p^.registers32:=max(p^.registers32,1);
  379. }
  380. end;
  381. p^.registersfpu:=max(p^.left^.registersfpu,p^.right^.registersfpu);
  382. {$ifdef SUPPORT_MMX}
  383. p^.registersmmx:=max(p^.left^.registersmmx,p^.right^.registersmmx);
  384. {$endif SUPPORT_MMX}
  385. p^.location.loc:=p^.left^.location.loc;
  386. end;
  387. {*****************************************************************************
  388. FirstSelf
  389. *****************************************************************************}
  390. procedure firstself(var p : ptree);
  391. begin
  392. if (p^.resulttype^.deftype=classrefdef) or
  393. ((p^.resulttype^.deftype=objectdef)
  394. and pobjectdef(p^.resulttype)^.isclass
  395. ) then
  396. p^.location.loc:=LOC_CREGISTER
  397. else
  398. p^.location.loc:=LOC_REFERENCE;
  399. end;
  400. {*****************************************************************************
  401. FirstWithN
  402. *****************************************************************************}
  403. procedure firstwith(var p : ptree);
  404. begin
  405. if assigned(p^.left) and assigned(p^.right) then
  406. begin
  407. firstpass(p^.left);
  408. if codegenerror then
  409. exit;
  410. firstpass(p^.right);
  411. if codegenerror then
  412. exit;
  413. left_right_max(p);
  414. p^.resulttype:=voiddef;
  415. end
  416. else
  417. begin
  418. { optimization }
  419. disposetree(p);
  420. p:=nil;
  421. end;
  422. end;
  423. end.
  424. {
  425. $Log$
  426. Revision 1.3 1998-09-26 15:03:05 florian
  427. * small problems with DOM and excpetions fixed (code generation
  428. of raise was wrong and self was sometimes destroyed :()
  429. Revision 1.2 1998/09/24 23:49:24 peter
  430. + aktmodeswitches
  431. Revision 1.1 1998/09/23 20:42:24 peter
  432. * splitted pass_1
  433. }