tcld.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Type checking and register allocation for load/assignment 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 tcld;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. tree;
  23. procedure firstload(var p : ptree);
  24. procedure firstassignment(var p : ptree);
  25. procedure firstfuncret(var p : ptree);
  26. procedure firstarrayconstructrange(var p:ptree);
  27. procedure firstarrayconstruct(var p : ptree);
  28. procedure firsttype(var p : ptree);
  29. implementation
  30. uses
  31. cutils,cobjects,verbose,globtype,globals,systems,
  32. symconst,symtable,aasm,types,
  33. htypechk,pass_1,
  34. tccnv,cpubase
  35. {$ifdef newcg}
  36. ,cgbase
  37. ,tgobj
  38. ,tgcpu
  39. {$else newcg}
  40. ,hcodegen
  41. {$ifdef i386}
  42. ,tgeni386
  43. {$endif}
  44. {$endif newcg}
  45. ;
  46. {*****************************************************************************
  47. FirstLoad
  48. *****************************************************************************}
  49. procedure firstload(var p : ptree);
  50. var
  51. p1 : ptree;
  52. begin
  53. if (p^.symtable^.symtabletype=withsymtable) and
  54. (pwithsymtable(p^.symtable)^.direct_with) and
  55. (p^.symtableentry^.typ=varsym) then
  56. begin
  57. p1:=getcopy(ptree(pwithsymtable(p^.symtable)^.withrefnode));
  58. p1:=gensubscriptnode(pvarsym(p^.symtableentry),p1);
  59. putnode(p);
  60. p:=p1;
  61. firstpass(p);
  62. exit;
  63. end;
  64. p^.location.loc:=LOC_REFERENCE;
  65. p^.registers32:=0;
  66. p^.registersfpu:=0;
  67. {$ifdef SUPPORT_MMX}
  68. p^.registersmmx:=0;
  69. {$endif SUPPORT_MMX}
  70. { handle first absolute as it will replace the p^.symtableentry }
  71. if p^.symtableentry^.typ=absolutesym then
  72. begin
  73. p^.resulttype:=pabsolutesym(p^.symtableentry)^.vartype.def;
  74. { replace the symtableentry when it points to a var, else
  75. we are finished }
  76. if pabsolutesym(p^.symtableentry)^.abstyp=tovar then
  77. begin
  78. p^.symtableentry:=pabsolutesym(p^.symtableentry)^.ref;
  79. p^.symtable:=p^.symtableentry^.owner;
  80. p^.is_absolute:=true;
  81. end
  82. else
  83. exit;
  84. end;
  85. case p^.symtableentry^.typ of
  86. funcretsym :
  87. begin
  88. p1:=genzeronode(funcretn);
  89. p1^.funcretprocinfo:=pprocinfo(pfuncretsym(p^.symtableentry)^.funcretprocinfo);
  90. p1^.rettype:=pfuncretsym(p^.symtableentry)^.rettype;
  91. firstpass(p1);
  92. { if it's refered as absolute then we need to have the
  93. type of the absolute instead of the function return,
  94. the function return is then also assigned }
  95. if p^.is_absolute then
  96. begin
  97. pprocinfo(p1^.funcretprocinfo)^.funcret_state:=vs_assigned;
  98. p1^.resulttype:=p^.resulttype;
  99. end;
  100. putnode(p);
  101. p:=p1;
  102. end;
  103. constsym:
  104. begin
  105. if pconstsym(p^.symtableentry)^.consttyp=constresourcestring then
  106. begin
  107. p^.resulttype:=cansistringdef;
  108. { we use ansistrings so no fast exit here }
  109. if assigned(procinfo) then
  110. procinfo^.no_fast_exit:=true;
  111. p^.location.loc:=LOC_MEM;
  112. end
  113. else
  114. internalerror(22799);
  115. end;
  116. varsym :
  117. begin
  118. { if it's refered by absolute then it's used }
  119. if p^.is_absolute then
  120. pvarsym(p^.symtableentry)^.varstate:=vs_used
  121. else
  122. if (p^.resulttype=nil) then
  123. p^.resulttype:=pvarsym(p^.symtableentry)^.vartype.def;
  124. if (p^.symtable^.symtabletype in [parasymtable,localsymtable]) and
  125. (lexlevel>p^.symtable^.symtablelevel) then
  126. begin
  127. { if the variable is in an other stackframe then we need
  128. a register to dereference }
  129. if (p^.symtable^.symtablelevel)>0 then
  130. begin
  131. p^.registers32:=1;
  132. { further, the variable can't be put into a register }
  133. pvarsym(p^.symtableentry)^.varoptions:=
  134. pvarsym(p^.symtableentry)^.varoptions-[vo_fpuregable,vo_regable];
  135. end;
  136. end;
  137. if (pvarsym(p^.symtableentry)^.varspez=vs_const) then
  138. p^.location.loc:=LOC_MEM;
  139. { we need a register for call by reference parameters }
  140. if (pvarsym(p^.symtableentry)^.varspez in [vs_var,vs_out]) or
  141. ((pvarsym(p^.symtableentry)^.varspez=vs_const) and
  142. push_addr_param(pvarsym(p^.symtableentry)^.vartype.def)) or
  143. { call by value open arrays are also indirect addressed }
  144. is_open_array(pvarsym(p^.symtableentry)^.vartype.def) then
  145. p^.registers32:=1;
  146. if p^.symtable^.symtabletype=withsymtable then
  147. inc(p^.registers32);
  148. if ([vo_is_thread_var,vo_is_dll_var]*pvarsym(p^.symtableentry)^.varoptions)<>[] then
  149. p^.registers32:=1;
  150. { a class variable is a pointer !!!
  151. yes, but we have to resolve the reference in an
  152. appropriate tree node (FK)
  153. if (pvarsym(p^.symtableentry)^.definition^.deftype=objectdef) and
  154. ((pobjectdef(pvarsym(p^.symtableentry)^.definition)^.options and oo_is_class)<>0) then
  155. p^.registers32:=1;
  156. }
  157. { count variable references }
  158. { this will create problem with local var set by
  159. under_procedures
  160. if (assigned(pvarsym(p^.symtableentry)^.owner) and assigned(aktprocsym)
  161. and ((pvarsym(p^.symtableentry)^.owner = aktprocsym^.definition^.localst)
  162. or (pvarsym(p^.symtableentry)^.owner = aktprocsym^.definition^.localst))) then }
  163. if t_times<1 then
  164. inc(pvarsym(p^.symtableentry)^.refs)
  165. else
  166. inc(pvarsym(p^.symtableentry)^.refs,t_times);
  167. end;
  168. typedconstsym :
  169. if not p^.is_absolute then
  170. p^.resulttype:=ptypedconstsym(p^.symtableentry)^.typedconsttype.def;
  171. procsym :
  172. begin
  173. if assigned(pprocsym(p^.symtableentry)^.definition^.nextoverloaded) then
  174. CGMessage(parser_e_no_overloaded_procvars);
  175. p^.resulttype:=pprocsym(p^.symtableentry)^.definition;
  176. { if the owner of the procsym is a object, }
  177. { left must be set, if left isn't set }
  178. { it can be only self }
  179. { this code is only used in TP procvar mode }
  180. if (m_tp_procvar in aktmodeswitches) and
  181. not(assigned(p^.left)) and
  182. (pprocsym(p^.symtableentry)^.owner^.symtabletype=objectsymtable) then
  183. p^.left:=genselfnode(pobjectdef(p^.symtableentry^.owner^.defowner));
  184. { method pointer ? }
  185. if assigned(p^.left) then
  186. begin
  187. firstpass(p^.left);
  188. p^.registers32:=max(p^.registers32,p^.left^.registers32);
  189. p^.registersfpu:=max(p^.registersfpu,p^.left^.registersfpu);
  190. {$ifdef SUPPORT_MMX}
  191. p^.registersmmx:=max(p^.registersmmx,p^.left^.registersmmx);
  192. {$endif SUPPORT_MMX}
  193. end;
  194. end;
  195. else
  196. internalerror(3);
  197. end;
  198. end;
  199. {*****************************************************************************
  200. FirstAssignment
  201. *****************************************************************************}
  202. procedure firstassignment(var p : ptree);
  203. {$ifdef newoptimizations2}
  204. var
  205. hp : ptree;
  206. {$endif newoptimizations2}
  207. begin
  208. { must be made unique }
  209. set_unique(p^.left);
  210. { set we the function result? }
  211. set_funcret_is_valid(p^.left);
  212. firstpass(p^.left);
  213. set_varstate(p^.left,false);
  214. if codegenerror then
  215. exit;
  216. { assignements to open arrays aren't allowed }
  217. if is_open_array(p^.left^.resulttype) then
  218. CGMessage(type_e_mismatch);
  219. { test if we can avoid copying string to temp
  220. as in s:=s+...; (PM) }
  221. {$ifdef dummyi386}
  222. if ((p^.right^.treetype=addn) or (p^.right^.treetype=subn)) and
  223. equal_trees(p^.left,p^.right^.left) and
  224. (ret_in_acc(p^.left^.resulttype)) and
  225. (not cs_rangechecking in aktmoduleswitches^) then
  226. begin
  227. disposetree(p^.right^.left);
  228. hp:=p^.right;
  229. p^.right:=p^.right^.right;
  230. if hp^.treetype=addn then
  231. p^.assigntyp:=at_plus
  232. else
  233. p^.assigntyp:=at_minus;
  234. putnode(hp);
  235. end;
  236. if p^.assigntyp<>at_normal then
  237. begin
  238. { for fpu type there is no faster way }
  239. if is_fpu(p^.left^.resulttype) then
  240. case p^.assigntyp of
  241. at_plus : p^.right:=gennode(addn,getcopy(p^.left),p^.right);
  242. at_minus : p^.right:=gennode(subn,getcopy(p^.left),p^.right);
  243. at_star : p^.right:=gennode(muln,getcopy(p^.left),p^.right);
  244. at_slash : p^.right:=gennode(slashn,getcopy(p^.left),p^.right);
  245. end;
  246. end;
  247. {$endif i386}
  248. firstpass(p^.right);
  249. set_varstate(p^.right,true);
  250. if codegenerror then
  251. exit;
  252. { some string functions don't need conversion, so treat them separatly }
  253. if is_shortstring(p^.left^.resulttype) and (assigned(p^.right^.resulttype)) then
  254. begin
  255. if not (is_shortstring(p^.right^.resulttype) or
  256. is_ansistring(p^.right^.resulttype) or
  257. is_char(p^.right^.resulttype)) then
  258. begin
  259. p^.right:=gentypeconvnode(p^.right,p^.left^.resulttype);
  260. firstpass(p^.right);
  261. if codegenerror then
  262. exit;
  263. end;
  264. { we call STRCOPY }
  265. procinfo^.flags:=procinfo^.flags or pi_do_call;
  266. { test for s:=s+anything ... }
  267. { the problem is for
  268. s:=s+s+s;
  269. this is broken here !! }
  270. {$ifdef newoptimizations2}
  271. { the above is fixed now, but still problem with s := s + f(); if }
  272. { f modifies s (bad programming, so only enable if uncertain }
  273. { optimizations are on) (JM) }
  274. if (cs_UncertainOpts in aktglobalswitches) then
  275. begin
  276. hp := p^.right;
  277. while hp^.treetype=addn do hp:=hp^.left;
  278. if equal_trees(p^.left,hp) and
  279. not multiple_uses(p^.left,p^.right) then
  280. begin
  281. p^.concat_string:=true;
  282. hp:=p^.right;
  283. while hp^.treetype=addn do
  284. begin
  285. hp^.use_strconcat:=true;
  286. hp:=hp^.left;
  287. end;
  288. end;
  289. end;
  290. {$endif newoptimizations2}
  291. end
  292. else
  293. begin
  294. p^.right:=gentypeconvnode(p^.right,p^.left^.resulttype);
  295. firstpass(p^.right);
  296. if codegenerror then
  297. exit;
  298. end;
  299. { test if node can be assigned, properties are allowed }
  300. valid_for_assign(p^.left,true);
  301. { check if local proc/func is assigned to procvar }
  302. if p^.right^.resulttype^.deftype=procvardef then
  303. test_local_to_procvar(pprocvardef(p^.right^.resulttype),p^.left^.resulttype);
  304. p^.resulttype:=voiddef;
  305. {
  306. p^.registers32:=max(p^.left^.registers32,p^.right^.registers32);
  307. p^.registersfpu:=max(p^.left^.registersfpu,p^.right^.registersfpu);
  308. }
  309. p^.registers32:=p^.left^.registers32+p^.right^.registers32;
  310. p^.registersfpu:=max(p^.left^.registersfpu,p^.right^.registersfpu);
  311. {$ifdef SUPPORT_MMX}
  312. p^.registersmmx:=max(p^.left^.registersmmx,p^.right^.registersmmx);
  313. {$endif SUPPORT_MMX}
  314. end;
  315. {*****************************************************************************
  316. FirstFuncRet
  317. *****************************************************************************}
  318. procedure firstfuncret(var p : ptree);
  319. begin
  320. p^.resulttype:=p^.rettype.def;
  321. p^.location.loc:=LOC_REFERENCE;
  322. if ret_in_param(p^.rettype.def) or
  323. (procinfo<>pprocinfo(p^.funcretprocinfo)) then
  324. p^.registers32:=1;
  325. end;
  326. {*****************************************************************************
  327. FirstArrayConstructRange
  328. *****************************************************************************}
  329. procedure firstarrayconstructrange(var p:ptree);
  330. begin
  331. firstpass(p^.left);
  332. set_varstate(p^.left,true);
  333. firstpass(p^.right);
  334. set_varstate(p^.right,true);
  335. calcregisters(p,0,0,0);
  336. p^.resulttype:=p^.left^.resulttype;
  337. end;
  338. {*****************************************************************************
  339. FirstArrayConstruct
  340. *****************************************************************************}
  341. procedure firstarrayconstruct(var p : ptree);
  342. var
  343. pd : pdef;
  344. thp,
  345. chp,
  346. hp : ptree;
  347. len : longint;
  348. varia : boolean;
  349. begin
  350. { are we allowing array constructor? Then convert it to a set }
  351. if not allow_array_constructor then
  352. begin
  353. arrayconstructor_to_set(p);
  354. firstpass(p);
  355. exit;
  356. end;
  357. { only pass left tree, right tree contains next construct if any }
  358. pd:=p^.constructdef;
  359. len:=0;
  360. varia:=false;
  361. if assigned(p^.left) then
  362. begin
  363. hp:=p;
  364. while assigned(hp) do
  365. begin
  366. firstpass(hp^.left);
  367. set_varstate(hp^.left,true);
  368. if (not get_para_resulttype) and (not p^.novariaallowed) then
  369. begin
  370. case hp^.left^.resulttype^.deftype of
  371. enumdef :
  372. begin
  373. hp^.left:=gentypeconvnode(hp^.left,s32bitdef);
  374. firstpass(hp^.left);
  375. end;
  376. orddef :
  377. begin
  378. if is_integer(hp^.left^.resulttype) and
  379. not(is_64bitint(hp^.left^.resulttype)) then
  380. begin
  381. hp^.left:=gentypeconvnode(hp^.left,s32bitdef);
  382. firstpass(hp^.left);
  383. end;
  384. end;
  385. floatdef :
  386. begin
  387. hp^.left:=gentypeconvnode(hp^.left,bestrealdef^);
  388. firstpass(hp^.left);
  389. end;
  390. stringdef :
  391. begin
  392. if p^.cargs then
  393. begin
  394. hp^.left:=gentypeconvnode(hp^.left,charpointerdef);
  395. firstpass(hp^.left);
  396. end;
  397. end;
  398. procvardef :
  399. begin
  400. hp^.left:=gentypeconvnode(hp^.left,voidpointerdef);
  401. firstpass(hp^.left);
  402. end;
  403. pointerdef,
  404. classrefdef,
  405. objectdef : ;
  406. else
  407. CGMessagePos1(hp^.left^.fileinfo,type_e_wrong_type_in_array_constructor,hp^.left^.resulttype^.typename);
  408. end;
  409. end;
  410. if (pd=nil) then
  411. pd:=hp^.left^.resulttype
  412. else
  413. begin
  414. if ((p^.novariaallowed) or (not varia)) and
  415. (not is_equal(pd,hp^.left^.resulttype)) then
  416. begin
  417. { if both should be equal try inserting a conversion }
  418. if p^.novariaallowed then
  419. begin
  420. hp^.left:=gentypeconvnode(hp^.left,pd);
  421. firstpass(hp^.left);
  422. end;
  423. varia:=true;
  424. end;
  425. end;
  426. inc(len);
  427. hp:=hp^.right;
  428. end;
  429. { swap the tree for cargs }
  430. if p^.cargs and (not p^.cargswap) then
  431. begin
  432. chp:=nil;
  433. hp:=p;
  434. while assigned(hp) do
  435. begin
  436. thp:=hp^.right;
  437. hp^.right:=chp;
  438. chp:=hp;
  439. hp:=thp;
  440. end;
  441. p:=chp;
  442. p^.cargs:=true;
  443. p^.cargswap:=true;
  444. end;
  445. end;
  446. calcregisters(p,0,0,0);
  447. { looks a little bit dangerous to me }
  448. { len-1 gives problems with is_open_array if len=0, }
  449. { is_open_array checks now for isconstructor (FK) }
  450. { if no type is set then we set the type to voiddef to overcome a
  451. 0 addressing }
  452. if not assigned(pd) then
  453. pd:=voiddef;
  454. { skip if already done ! (PM) }
  455. if not assigned(p^.resulttype) or
  456. (p^.resulttype^.deftype<>arraydef) or
  457. not parraydef(p^.resulttype)^.IsConstructor or
  458. (parraydef(p^.resulttype)^.lowrange<>0) or
  459. (parraydef(p^.resulttype)^.highrange<>len-1) then
  460. p^.resulttype:=new(parraydef,init(0,len-1,s32bitdef));
  461. parraydef(p^.resulttype)^.elementtype.def:=pd;
  462. parraydef(p^.resulttype)^.IsConstructor:=true;
  463. parraydef(p^.resulttype)^.IsVariant:=varia;
  464. p^.location.loc:=LOC_MEM;
  465. end;
  466. {*****************************************************************************
  467. Type
  468. *****************************************************************************}
  469. procedure firsttype(var p : ptree);
  470. begin
  471. { do nothing, p^.resulttype is already set }
  472. end;
  473. end.
  474. {
  475. $Log$
  476. Revision 1.8 2000-09-24 21:19:53 peter
  477. * delphi compile fixes
  478. Revision 1.7 2000/08/27 16:11:55 peter
  479. * moved some util functions from globals,cobjects to cutils
  480. * splitted files into finput,fmodule
  481. Revision 1.6 2000/08/15 03:41:27 peter
  482. * previous commit was wrong file :(
  483. Revision 1.5 2000/08/13 19:21:13 peter
  484. * fix for absolute to mem address (merged)
  485. Revision 1.4 2000/08/13 08:42:59 peter
  486. * support absolute refering to funcret (merged)
  487. Revision 1.3 2000/07/13 12:08:28 michael
  488. + patched to 1.1.0 with former 1.09patch from peter
  489. Revision 1.2 2000/07/13 11:32:52 michael
  490. + removed logs
  491. }