tcld.pas 17 KB

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