tcld.pas 20 KB

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