tcld.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 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,globals,systems,
  31. symtable,aasm,types,
  32. hcodegen,htypechk,pass_1,
  33. tccnv
  34. {$ifdef i386}
  35. ,i386,tgeni386
  36. {$endif}
  37. {$ifdef m68k}
  38. ,m68k,tgen68k
  39. {$endif}
  40. ;
  41. {*****************************************************************************
  42. FirstLoad
  43. *****************************************************************************}
  44. procedure firstload(var p : ptree);
  45. var
  46. p1 : ptree;
  47. begin
  48. {$ifndef NODIRECTWITH}
  49. if (p^.symtable^.symtabletype=withsymtable) and
  50. (pwithsymtable(p^.symtable)^.direct_with) then
  51. begin
  52. p1:=getcopy(ptree(pwithsymtable(p^.symtable)^.withnode)^.left);
  53. p1:=gensubscriptnode(pvarsym(p^.symtableentry),p1);
  54. putnode(p);
  55. p:=p1;
  56. firstpass(p1);
  57. exit;
  58. end;
  59. {$endif ndef NODIRECTWITH}
  60. p^.location.loc:=LOC_REFERENCE;
  61. p^.registers32:=0;
  62. p^.registersfpu:=0;
  63. {$ifdef SUPPORT_MMX}
  64. p^.registersmmx:=0;
  65. {$endif SUPPORT_MMX}
  66. clear_reference(p^.location.reference);
  67. if p^.symtableentry^.typ=funcretsym then
  68. begin
  69. putnode(p);
  70. p:=genzeronode(funcretn);
  71. p^.funcretprocinfo:=pprocinfo(pfuncretsym(p^.symtableentry)^.funcretprocinfo);
  72. p^.retdef:=pfuncretsym(p^.symtableentry)^.funcretdef;
  73. firstpass(p);
  74. exit;
  75. end;
  76. if p^.symtableentry^.typ=absolutesym then
  77. begin
  78. p^.resulttype:=pabsolutesym(p^.symtableentry)^.definition;
  79. if pabsolutesym(p^.symtableentry)^.abstyp=tovar then
  80. p^.symtableentry:=pabsolutesym(p^.symtableentry)^.ref;
  81. p^.symtable:=p^.symtableentry^.owner;
  82. p^.is_absolute:=true;
  83. end;
  84. case p^.symtableentry^.typ of
  85. absolutesym :;
  86. varsym :
  87. begin
  88. if not(p^.is_absolute) and (p^.resulttype=nil) then
  89. p^.resulttype:=pvarsym(p^.symtableentry)^.definition;
  90. if (p^.symtable^.symtabletype in [parasymtable,localsymtable]) and
  91. (lexlevel>p^.symtable^.symtablelevel) then
  92. begin
  93. { if the variable is in an other stackframe then we need
  94. a register to dereference }
  95. if (p^.symtable^.symtablelevel)>0 then
  96. begin
  97. p^.registers32:=1;
  98. { further, the variable can't be put into a register }
  99. pvarsym(p^.symtableentry)^.var_options:=
  100. pvarsym(p^.symtableentry)^.var_options and not vo_regable;
  101. end;
  102. end;
  103. if (pvarsym(p^.symtableentry)^.varspez=vs_const) then
  104. p^.location.loc:=LOC_MEM;
  105. { we need a register for call by reference parameters }
  106. if (pvarsym(p^.symtableentry)^.varspez=vs_var) or
  107. ((pvarsym(p^.symtableentry)^.varspez=vs_const) and
  108. push_addr_param(pvarsym(p^.symtableentry)^.definition)) or
  109. { call by value open arrays are also indirect addressed }
  110. is_open_array(pvarsym(p^.symtableentry)^.definition) then
  111. p^.registers32:=1;
  112. if p^.symtable^.symtabletype=withsymtable then
  113. inc(p^.registers32);
  114. { a class variable is a pointer !!!
  115. yes, but we have to resolve the reference in an
  116. appropriate tree node (FK)
  117. if (pvarsym(p^.symtableentry)^.definition^.deftype=objectdef) and
  118. ((pobjectdef(pvarsym(p^.symtableentry)^.definition)^.options and oo_is_class)<>0) then
  119. p^.registers32:=1;
  120. }
  121. { count variable references }
  122. if must_be_valid and p^.is_first then
  123. begin
  124. if pvarsym(p^.symtableentry)^.is_valid=2 then
  125. if (assigned(pvarsym(p^.symtableentry)^.owner) and assigned(aktprocsym)
  126. and (pvarsym(p^.symtableentry)^.owner = aktprocsym^.definition^.localst)) then
  127. CGMessage1(sym_n_uninitialized_local_variable,pvarsym(p^.symtableentry)^.name);
  128. end;
  129. if count_ref then
  130. begin
  131. if (p^.is_first) then
  132. begin
  133. if (pvarsym(p^.symtableentry)^.is_valid=2) then
  134. pvarsym(p^.symtableentry)^.is_valid:=1;
  135. p^.is_first:=false;
  136. end;
  137. end;
  138. { this will create problem with local var set by
  139. under_procedures
  140. if (assigned(pvarsym(p^.symtableentry)^.owner) and assigned(aktprocsym)
  141. and ((pvarsym(p^.symtableentry)^.owner = aktprocsym^.definition^.localst)
  142. or (pvarsym(p^.symtableentry)^.owner = aktprocsym^.definition^.localst))) then }
  143. if t_times<1 then
  144. inc(pvarsym(p^.symtableentry)^.refs)
  145. else
  146. inc(pvarsym(p^.symtableentry)^.refs,t_times);
  147. end;
  148. typedconstsym :
  149. if not p^.is_absolute then
  150. p^.resulttype:=ptypedconstsym(p^.symtableentry)^.definition;
  151. procsym :
  152. begin
  153. if assigned(pprocsym(p^.symtableentry)^.definition^.nextoverloaded) then
  154. CGMessage(parser_e_no_overloaded_procvars);
  155. p^.resulttype:=pprocsym(p^.symtableentry)^.definition;
  156. end;
  157. else internalerror(3);
  158. end;
  159. end;
  160. {*****************************************************************************
  161. FirstAssignment
  162. *****************************************************************************}
  163. procedure firstassignment(var p : ptree);
  164. var
  165. store_valid : boolean;
  166. hp : ptree;
  167. begin
  168. store_valid:=must_be_valid;
  169. must_be_valid:=false;
  170. { must be made unique }
  171. set_unique(p^.left);
  172. firstpass(p^.left);
  173. if codegenerror then
  174. exit;
  175. { assignements to open arrays aren't allowed }
  176. if is_open_array(p^.left^.resulttype) then
  177. CGMessage(type_e_mismatch);
  178. { test if we can avoid copying string to temp
  179. as in s:=s+...; (PM) }
  180. {$ifdef dummyi386}
  181. if ((p^.right^.treetype=addn) or (p^.right^.treetype=subn)) and
  182. equal_trees(p^.left,p^.right^.left) and
  183. (ret_in_acc(p^.left^.resulttype)) and
  184. (not cs_rangechecking in aktmoduleswitches^) then
  185. begin
  186. disposetree(p^.right^.left);
  187. hp:=p^.right;
  188. p^.right:=p^.right^.right;
  189. if hp^.treetype=addn then
  190. p^.assigntyp:=at_plus
  191. else
  192. p^.assigntyp:=at_minus;
  193. putnode(hp);
  194. end;
  195. if p^.assigntyp<>at_normal then
  196. begin
  197. { for fpu type there is no faster way }
  198. if is_fpu(p^.left^.resulttype) then
  199. case p^.assigntyp of
  200. at_plus : p^.right:=gennode(addn,getcopy(p^.left),p^.right);
  201. at_minus : p^.right:=gennode(subn,getcopy(p^.left),p^.right);
  202. at_star : p^.right:=gennode(muln,getcopy(p^.left),p^.right);
  203. at_slash : p^.right:=gennode(slashn,getcopy(p^.left),p^.right);
  204. end;
  205. end;
  206. {$endif i386}
  207. must_be_valid:=true;
  208. firstpass(p^.right);
  209. must_be_valid:=store_valid;
  210. if codegenerror then
  211. exit;
  212. { some string functions don't need conversion, so treat them separatly }
  213. if is_shortstring(p^.left^.resulttype) and (assigned(p^.right^.resulttype)) then
  214. begin
  215. if not (is_shortstring(p^.right^.resulttype) or
  216. is_ansistring(p^.right^.resulttype) or
  217. is_char(p^.right^.resulttype)) then
  218. begin
  219. p^.right:=gentypeconvnode(p^.right,p^.left^.resulttype);
  220. firstpass(p^.right);
  221. if codegenerror then
  222. exit;
  223. end;
  224. { we call STRCOPY }
  225. procinfo.flags:=procinfo.flags or pi_do_call;
  226. hp:=p^.right;
  227. { test for s:=s+anything ... }
  228. { the problem is for
  229. s:=s+s+s;
  230. this is broken here !! }
  231. { while hp^.treetype=addn do hp:=hp^.left;
  232. if equal_trees(p^.left,hp) then
  233. begin
  234. p^.concat_string:=true;
  235. hp:=p^.right;
  236. while hp^.treetype=addn do
  237. begin
  238. hp^.use_strconcat:=true;
  239. hp:=hp^.left;
  240. end;
  241. end; }
  242. end
  243. else
  244. begin
  245. if (p^.right^.treetype=realconstn) then
  246. begin
  247. if p^.left^.resulttype^.deftype=floatdef then
  248. begin
  249. case pfloatdef(p^.left^.resulttype)^.typ of
  250. s32real : p^.right^.realtyp:=ait_real_32bit;
  251. s64real : p^.right^.realtyp:=ait_real_64bit;
  252. s80real : p^.right^.realtyp:=ait_real_extended;
  253. { what about f32bit and s64bit }
  254. else
  255. begin
  256. p^.right:=gentypeconvnode(p^.right,p^.left^.resulttype);
  257. { nochmal firstpass wegen der Typkonvertierung aufrufen }
  258. firstpass(p^.right);
  259. if codegenerror then
  260. exit;
  261. end;
  262. end;
  263. end;
  264. end
  265. else
  266. begin
  267. p^.right:=gentypeconvnode(p^.right,p^.left^.resulttype);
  268. firstpass(p^.right);
  269. if codegenerror then
  270. exit;
  271. end;
  272. end;
  273. p^.resulttype:=voiddef;
  274. {
  275. p^.registers32:=max(p^.left^.registers32,p^.right^.registers32);
  276. p^.registersfpu:=max(p^.left^.registersfpu,p^.right^.registersfpu);
  277. }
  278. p^.registers32:=p^.left^.registers32+p^.right^.registers32;
  279. p^.registersfpu:=max(p^.left^.registersfpu,p^.right^.registersfpu);
  280. {$ifdef SUPPORT_MMX}
  281. p^.registersmmx:=max(p^.left^.registersmmx,p^.right^.registersmmx);
  282. {$endif SUPPORT_MMX}
  283. end;
  284. {*****************************************************************************
  285. FirstFuncRet
  286. *****************************************************************************}
  287. procedure firstfuncret(var p : ptree);
  288. begin
  289. p^.resulttype:=p^.retdef;
  290. p^.location.loc:=LOC_REFERENCE;
  291. if ret_in_param(p^.retdef) or
  292. (@procinfo<>pprocinfo(p^.funcretprocinfo)) then
  293. p^.registers32:=1;
  294. { no claim if setting higher return value_str }
  295. if must_be_valid and
  296. (@procinfo=pprocinfo(p^.funcretprocinfo)) and
  297. not procinfo.funcret_is_valid then
  298. CGMessage(sym_w_function_result_not_set);
  299. if count_ref then
  300. pprocinfo(p^.funcretprocinfo)^.funcret_is_valid:=true;
  301. end;
  302. {*****************************************************************************
  303. FirstArrayConstructRange
  304. *****************************************************************************}
  305. procedure firstarrayconstructrange(var p:ptree);
  306. begin
  307. firstpass(p^.left);
  308. firstpass(p^.right);
  309. calcregisters(p,0,0,0);
  310. p^.resulttype:=p^.left^.resulttype;
  311. end;
  312. {*****************************************************************************
  313. FirstArrayConstruct
  314. *****************************************************************************}
  315. procedure firstarrayconstruct(var p : ptree);
  316. var
  317. pd : pdef;
  318. thp,
  319. chp,
  320. hp : ptree;
  321. len : longint;
  322. varia : boolean;
  323. begin
  324. { are we allowing array constructor? Then convert it to a set }
  325. if not allow_array_constructor then
  326. begin
  327. arrayconstructor_to_set(p);
  328. firstpass(p);
  329. exit;
  330. end;
  331. { only pass left tree, right tree contains next construct if any }
  332. pd:=nil;
  333. len:=0;
  334. varia:=false;
  335. if assigned(p^.left) then
  336. begin
  337. hp:=p;
  338. while assigned(hp) do
  339. begin
  340. firstpass(hp^.left);
  341. case hp^.left^.resulttype^.deftype of
  342. floatdef : begin
  343. hp^.left:=gentypeconvnode(hp^.left,s80floatdef);
  344. firstpass(hp^.left);
  345. end;
  346. stringdef : begin
  347. if p^.cargs then
  348. begin
  349. hp^.left:=gentypeconvnode(hp^.left,charpointerdef);
  350. firstpass(hp^.left);
  351. end;
  352. end;
  353. end;
  354. if (pd=nil) then
  355. pd:=hp^.left^.resulttype
  356. else
  357. if (not varia) and (not is_equal(pd,hp^.left^.resulttype)) then
  358. varia:=true;
  359. inc(len);
  360. hp:=hp^.right;
  361. end;
  362. { swap the tree for cargs }
  363. if p^.cargs and (not p^.cargswap) then
  364. begin
  365. chp:=nil;
  366. hp:=p;
  367. while assigned(hp) do
  368. begin
  369. thp:=hp^.right;
  370. hp^.right:=chp;
  371. chp:=hp;
  372. hp:=thp;
  373. end;
  374. p:=chp;
  375. p^.cargs:=true;
  376. p^.cargswap:=true;
  377. end;
  378. end;
  379. calcregisters(p,0,0,0);
  380. p^.resulttype:=new(parraydef,init(0,len-1,pd));
  381. parraydef(p^.resulttype)^.IsConstructor:=true;
  382. parraydef(p^.resulttype)^.IsVariant:=varia;
  383. p^.location.loc:=LOC_REFERENCE;
  384. end;
  385. {*****************************************************************************
  386. Type
  387. *****************************************************************************}
  388. procedure firsttype(var p : ptree);
  389. begin
  390. { do nothing, p^.resulttype is already set }
  391. end;
  392. end.
  393. {
  394. $Log$
  395. Revision 1.13 1999-01-21 16:41:07 pierre
  396. * fix for constructor inside with statements
  397. Revision 1.12 1998/12/30 13:41:19 peter
  398. * released valuepara
  399. Revision 1.11 1998/11/18 17:45:28 peter
  400. * fixes for VALUEPARA
  401. Revision 1.10 1998/11/18 15:44:23 peter
  402. * VALUEPARA for tp7 compatible value parameters
  403. Revision 1.9 1998/11/17 00:36:49 peter
  404. * more ansistring fixes
  405. Revision 1.8 1998/11/10 10:09:18 peter
  406. * va_list -> array of const
  407. Revision 1.7 1998/11/05 14:26:48 peter
  408. * fixed variant warning with was sometimes said with sets
  409. Revision 1.6 1998/10/19 08:55:12 pierre
  410. * wrong stabs info corrected once again !!
  411. + variable vmt offset with vmt field only if required
  412. implemented now !!!
  413. Revision 1.5 1998/10/06 20:49:12 peter
  414. * m68k compiler compiles again
  415. Revision 1.4 1998/09/28 11:07:40 peter
  416. + floatdef support for array of const
  417. Revision 1.3 1998/09/27 10:16:27 florian
  418. * type casts pchar<->ansistring fixed
  419. * ansistring[..] calls does now an unique call
  420. Revision 1.2 1998/09/24 15:13:48 peter
  421. * fixed type node which was always set to void :(
  422. Revision 1.1 1998/09/23 20:42:24 peter
  423. * splitted pass_1
  424. }