tcld.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. { method pointer ? }
  157. if assigned(p^.left) then
  158. begin
  159. firstpass(p^.left);
  160. p^.registers32:=max(p^.registers32,p^.left^.registers32);
  161. p^.registersfpu:=max(p^.registersfpu,p^.left^.registersfpu);
  162. {$ifdef SUPPORT_MMX}
  163. p^.registersmmx:=max(p^.registersmmx,p^.left^.registersmmx);
  164. {$endif SUPPORT_MMX}
  165. end;
  166. end;
  167. else internalerror(3);
  168. end;
  169. end;
  170. {*****************************************************************************
  171. FirstAssignment
  172. *****************************************************************************}
  173. procedure firstassignment(var p : ptree);
  174. var
  175. store_valid : boolean;
  176. hp : ptree;
  177. begin
  178. store_valid:=must_be_valid;
  179. must_be_valid:=false;
  180. { must be made unique }
  181. set_unique(p^.left);
  182. firstpass(p^.left);
  183. if codegenerror then
  184. exit;
  185. { assignements to open arrays aren't allowed }
  186. if is_open_array(p^.left^.resulttype) then
  187. CGMessage(type_e_mismatch);
  188. { test if we can avoid copying string to temp
  189. as in s:=s+...; (PM) }
  190. {$ifdef dummyi386}
  191. if ((p^.right^.treetype=addn) or (p^.right^.treetype=subn)) and
  192. equal_trees(p^.left,p^.right^.left) and
  193. (ret_in_acc(p^.left^.resulttype)) and
  194. (not cs_rangechecking in aktmoduleswitches^) then
  195. begin
  196. disposetree(p^.right^.left);
  197. hp:=p^.right;
  198. p^.right:=p^.right^.right;
  199. if hp^.treetype=addn then
  200. p^.assigntyp:=at_plus
  201. else
  202. p^.assigntyp:=at_minus;
  203. putnode(hp);
  204. end;
  205. if p^.assigntyp<>at_normal then
  206. begin
  207. { for fpu type there is no faster way }
  208. if is_fpu(p^.left^.resulttype) then
  209. case p^.assigntyp of
  210. at_plus : p^.right:=gennode(addn,getcopy(p^.left),p^.right);
  211. at_minus : p^.right:=gennode(subn,getcopy(p^.left),p^.right);
  212. at_star : p^.right:=gennode(muln,getcopy(p^.left),p^.right);
  213. at_slash : p^.right:=gennode(slashn,getcopy(p^.left),p^.right);
  214. end;
  215. end;
  216. {$endif i386}
  217. must_be_valid:=true;
  218. firstpass(p^.right);
  219. must_be_valid:=store_valid;
  220. if codegenerror then
  221. exit;
  222. { some string functions don't need conversion, so treat them separatly }
  223. if is_shortstring(p^.left^.resulttype) and (assigned(p^.right^.resulttype)) then
  224. begin
  225. if not (is_shortstring(p^.right^.resulttype) or
  226. is_ansistring(p^.right^.resulttype) or
  227. is_char(p^.right^.resulttype)) then
  228. begin
  229. p^.right:=gentypeconvnode(p^.right,p^.left^.resulttype);
  230. firstpass(p^.right);
  231. if codegenerror then
  232. exit;
  233. end;
  234. { we call STRCOPY }
  235. procinfo.flags:=procinfo.flags or pi_do_call;
  236. hp:=p^.right;
  237. { test for s:=s+anything ... }
  238. { the problem is for
  239. s:=s+s+s;
  240. this is broken here !! }
  241. { while hp^.treetype=addn do hp:=hp^.left;
  242. if equal_trees(p^.left,hp) then
  243. begin
  244. p^.concat_string:=true;
  245. hp:=p^.right;
  246. while hp^.treetype=addn do
  247. begin
  248. hp^.use_strconcat:=true;
  249. hp:=hp^.left;
  250. end;
  251. end; }
  252. end
  253. else
  254. begin
  255. if (p^.right^.treetype=realconstn) then
  256. begin
  257. if p^.left^.resulttype^.deftype=floatdef then
  258. begin
  259. case pfloatdef(p^.left^.resulttype)^.typ of
  260. s32real : p^.right^.realtyp:=ait_real_32bit;
  261. s64real : p^.right^.realtyp:=ait_real_64bit;
  262. s80real : p^.right^.realtyp:=ait_real_extended;
  263. { what about f32bit and s64bit }
  264. else
  265. begin
  266. p^.right:=gentypeconvnode(p^.right,p^.left^.resulttype);
  267. { nochmal firstpass wegen der Typkonvertierung aufrufen }
  268. firstpass(p^.right);
  269. if codegenerror then
  270. exit;
  271. end;
  272. end;
  273. end;
  274. end
  275. else
  276. begin
  277. p^.right:=gentypeconvnode(p^.right,p^.left^.resulttype);
  278. firstpass(p^.right);
  279. if codegenerror then
  280. exit;
  281. end;
  282. end;
  283. p^.resulttype:=voiddef;
  284. {
  285. p^.registers32:=max(p^.left^.registers32,p^.right^.registers32);
  286. p^.registersfpu:=max(p^.left^.registersfpu,p^.right^.registersfpu);
  287. }
  288. p^.registers32:=p^.left^.registers32+p^.right^.registers32;
  289. p^.registersfpu:=max(p^.left^.registersfpu,p^.right^.registersfpu);
  290. {$ifdef SUPPORT_MMX}
  291. p^.registersmmx:=max(p^.left^.registersmmx,p^.right^.registersmmx);
  292. {$endif SUPPORT_MMX}
  293. end;
  294. {*****************************************************************************
  295. FirstFuncRet
  296. *****************************************************************************}
  297. procedure firstfuncret(var p : ptree);
  298. begin
  299. p^.resulttype:=p^.retdef;
  300. p^.location.loc:=LOC_REFERENCE;
  301. if ret_in_param(p^.retdef) or
  302. (@procinfo<>pprocinfo(p^.funcretprocinfo)) then
  303. p^.registers32:=1;
  304. { no claim if setting higher return value_str }
  305. if must_be_valid and
  306. (@procinfo=pprocinfo(p^.funcretprocinfo)) and
  307. not procinfo.funcret_is_valid then
  308. CGMessage(sym_w_function_result_not_set);
  309. if count_ref then
  310. pprocinfo(p^.funcretprocinfo)^.funcret_is_valid:=true;
  311. end;
  312. {*****************************************************************************
  313. FirstArrayConstructRange
  314. *****************************************************************************}
  315. procedure firstarrayconstructrange(var p:ptree);
  316. begin
  317. firstpass(p^.left);
  318. firstpass(p^.right);
  319. calcregisters(p,0,0,0);
  320. p^.resulttype:=p^.left^.resulttype;
  321. end;
  322. {*****************************************************************************
  323. FirstArrayConstruct
  324. *****************************************************************************}
  325. procedure firstarrayconstruct(var p : ptree);
  326. var
  327. pd : pdef;
  328. thp,
  329. chp,
  330. hp : ptree;
  331. len : longint;
  332. varia : boolean;
  333. begin
  334. { are we allowing array constructor? Then convert it to a set }
  335. if not allow_array_constructor then
  336. begin
  337. arrayconstructor_to_set(p);
  338. firstpass(p);
  339. exit;
  340. end;
  341. { only pass left tree, right tree contains next construct if any }
  342. pd:=nil;
  343. len:=0;
  344. varia:=false;
  345. if assigned(p^.left) then
  346. begin
  347. hp:=p;
  348. while assigned(hp) do
  349. begin
  350. firstpass(hp^.left);
  351. case hp^.left^.resulttype^.deftype of
  352. floatdef : begin
  353. hp^.left:=gentypeconvnode(hp^.left,s80floatdef);
  354. firstpass(hp^.left);
  355. end;
  356. stringdef : begin
  357. if p^.cargs then
  358. begin
  359. hp^.left:=gentypeconvnode(hp^.left,charpointerdef);
  360. firstpass(hp^.left);
  361. end;
  362. end;
  363. end;
  364. if (pd=nil) then
  365. pd:=hp^.left^.resulttype
  366. else
  367. if (not varia) and (not is_equal(pd,hp^.left^.resulttype)) then
  368. varia:=true;
  369. inc(len);
  370. hp:=hp^.right;
  371. end;
  372. { swap the tree for cargs }
  373. if p^.cargs and (not p^.cargswap) then
  374. begin
  375. chp:=nil;
  376. hp:=p;
  377. while assigned(hp) do
  378. begin
  379. thp:=hp^.right;
  380. hp^.right:=chp;
  381. chp:=hp;
  382. hp:=thp;
  383. end;
  384. p:=chp;
  385. p^.cargs:=true;
  386. p^.cargswap:=true;
  387. end;
  388. end;
  389. calcregisters(p,0,0,0);
  390. p^.resulttype:=new(parraydef,init(0,len-1,pd));
  391. parraydef(p^.resulttype)^.IsConstructor:=true;
  392. parraydef(p^.resulttype)^.IsVariant:=varia;
  393. p^.location.loc:=LOC_REFERENCE;
  394. end;
  395. {*****************************************************************************
  396. Type
  397. *****************************************************************************}
  398. procedure firsttype(var p : ptree);
  399. begin
  400. { do nothing, p^.resulttype is already set }
  401. end;
  402. end.
  403. {
  404. $Log$
  405. Revision 1.14 1999-01-27 00:13:58 florian
  406. * "procedure of object"-stuff fixed
  407. Revision 1.13 1999/01/21 16:41:07 pierre
  408. * fix for constructor inside with statements
  409. Revision 1.12 1998/12/30 13:41:19 peter
  410. * released valuepara
  411. Revision 1.11 1998/11/18 17:45:28 peter
  412. * fixes for VALUEPARA
  413. Revision 1.10 1998/11/18 15:44:23 peter
  414. * VALUEPARA for tp7 compatible value parameters
  415. Revision 1.9 1998/11/17 00:36:49 peter
  416. * more ansistring fixes
  417. Revision 1.8 1998/11/10 10:09:18 peter
  418. * va_list -> array of const
  419. Revision 1.7 1998/11/05 14:26:48 peter
  420. * fixed variant warning with was sometimes said with sets
  421. Revision 1.6 1998/10/19 08:55:12 pierre
  422. * wrong stabs info corrected once again !!
  423. + variable vmt offset with vmt field only if required
  424. implemented now !!!
  425. Revision 1.5 1998/10/06 20:49:12 peter
  426. * m68k compiler compiles again
  427. Revision 1.4 1998/09/28 11:07:40 peter
  428. + floatdef support for array of const
  429. Revision 1.3 1998/09/27 10:16:27 florian
  430. * type casts pchar<->ansistring fixed
  431. * ansistring[..] calls does now an unique call
  432. Revision 1.2 1998/09/24 15:13:48 peter
  433. * fixed type node which was always set to void :(
  434. Revision 1.1 1998/09/23 20:42:24 peter
  435. * splitted pass_1
  436. }