tcld.pas 17 KB

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