tcld.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 firstarrayconstruct(var p : ptree);
  26. procedure firsttype(var p : ptree);
  27. implementation
  28. uses
  29. cobjects,verbose,globals,systems,
  30. symtable,aasm,types,
  31. hcodegen,htypechk,pass_1,
  32. tccnv
  33. {$ifdef i386}
  34. ,i386,tgeni386
  35. {$endif}
  36. {$ifdef m68k}
  37. ,m68k,tgen68k
  38. {$endif}
  39. ;
  40. {*****************************************************************************
  41. FirstLoad
  42. *****************************************************************************}
  43. procedure firstload(var p : ptree);
  44. begin
  45. p^.location.loc:=LOC_REFERENCE;
  46. p^.registers32:=0;
  47. p^.registersfpu:=0;
  48. {$ifdef SUPPORT_MMX}
  49. p^.registersmmx:=0;
  50. {$endif SUPPORT_MMX}
  51. clear_reference(p^.location.reference);
  52. if p^.symtableentry^.typ=funcretsym then
  53. begin
  54. putnode(p);
  55. p:=genzeronode(funcretn);
  56. p^.funcretprocinfo:=pprocinfo(pfuncretsym(p^.symtableentry)^.funcretprocinfo);
  57. p^.retdef:=pfuncretsym(p^.symtableentry)^.funcretdef;
  58. firstpass(p);
  59. exit;
  60. end;
  61. if p^.symtableentry^.typ=absolutesym then
  62. begin
  63. p^.resulttype:=pabsolutesym(p^.symtableentry)^.definition;
  64. if pabsolutesym(p^.symtableentry)^.abstyp=tovar then
  65. p^.symtableentry:=pabsolutesym(p^.symtableentry)^.ref;
  66. p^.symtable:=p^.symtableentry^.owner;
  67. p^.is_absolute:=true;
  68. end;
  69. case p^.symtableentry^.typ of
  70. absolutesym :;
  71. varsym :
  72. begin
  73. if not(p^.is_absolute) and (p^.resulttype=nil) then
  74. p^.resulttype:=pvarsym(p^.symtableentry)^.definition;
  75. if ((p^.symtable^.symtabletype=parasymtable) or
  76. (p^.symtable^.symtabletype=localsymtable)) and
  77. (lexlevel>p^.symtable^.symtablelevel) then
  78. begin
  79. { sollte sich die Variable in einem anderen Stackframe }
  80. { befinden, so brauchen wir ein Register zum Dereferenceieren }
  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. dont_copy_const_param(pvarsym(p^.symtableentry)^.definition)
  95. ) or
  96. { call by value open arrays are also indirect addressed }
  97. is_open_array(pvarsym(p^.symtableentry)^.definition) then
  98. p^.registers32:=1;
  99. if p^.symtable^.symtabletype=withsymtable then
  100. inc(p^.registers32);
  101. { a class variable is a pointer !!!
  102. yes, but we have to resolve the reference in an
  103. appropriate tree node (FK)
  104. if (pvarsym(p^.symtableentry)^.definition^.deftype=objectdef) and
  105. ((pobjectdef(pvarsym(p^.symtableentry)^.definition)^.options and oois_class)<>0) then
  106. p^.registers32:=1;
  107. }
  108. { count variable references }
  109. if must_be_valid and p^.is_first then
  110. begin
  111. if pvarsym(p^.symtableentry)^.is_valid=2 then
  112. if (assigned(pvarsym(p^.symtableentry)^.owner) and assigned(aktprocsym)
  113. and (pvarsym(p^.symtableentry)^.owner = aktprocsym^.definition^.localst)) then
  114. CGMessage1(sym_n_uninitialized_local_variable,pvarsym(p^.symtableentry)^.name);
  115. end;
  116. if count_ref then
  117. begin
  118. if (p^.is_first) then
  119. begin
  120. if (pvarsym(p^.symtableentry)^.is_valid=2) then
  121. pvarsym(p^.symtableentry)^.is_valid:=1;
  122. p^.is_first:=false;
  123. end;
  124. end;
  125. { this will create problem with local var set by
  126. under_procedures
  127. if (assigned(pvarsym(p^.symtableentry)^.owner) and assigned(aktprocsym)
  128. and ((pvarsym(p^.symtableentry)^.owner = aktprocsym^.definition^.localst)
  129. or (pvarsym(p^.symtableentry)^.owner = aktprocsym^.definition^.localst))) then }
  130. if t_times<1 then
  131. inc(pvarsym(p^.symtableentry)^.refs)
  132. else
  133. inc(pvarsym(p^.symtableentry)^.refs,t_times);
  134. end;
  135. typedconstsym :
  136. if not p^.is_absolute then
  137. p^.resulttype:=ptypedconstsym(p^.symtableentry)^.definition;
  138. procsym :
  139. begin
  140. if assigned(pprocsym(p^.symtableentry)^.definition^.nextoverloaded) then
  141. CGMessage(parser_e_no_overloaded_procvars);
  142. p^.resulttype:=pprocsym(p^.symtableentry)^.definition;
  143. end;
  144. else internalerror(3);
  145. end;
  146. end;
  147. {*****************************************************************************
  148. FirstAssignment
  149. *****************************************************************************}
  150. procedure firstassignment(var p : ptree);
  151. var
  152. store_valid : boolean;
  153. hp : ptree;
  154. begin
  155. store_valid:=must_be_valid;
  156. must_be_valid:=false;
  157. { must be made unique }
  158. set_unique(p^.left);
  159. firstpass(p^.left);
  160. if codegenerror then
  161. exit;
  162. { assignements to open arrays aren't allowed }
  163. if is_open_array(p^.left^.resulttype) then
  164. CGMessage(type_e_mismatch);
  165. { test if we can avoid copying string to temp
  166. as in s:=s+...; (PM) }
  167. {$ifdef dummyi386}
  168. if ((p^.right^.treetype=addn) or (p^.right^.treetype=subn)) and
  169. equal_trees(p^.left,p^.right^.left) and
  170. (ret_in_acc(p^.left^.resulttype)) and
  171. (not cs_rangechecking in aktmoduleswitches^) then
  172. begin
  173. disposetree(p^.right^.left);
  174. hp:=p^.right;
  175. p^.right:=p^.right^.right;
  176. if hp^.treetype=addn then
  177. p^.assigntyp:=at_plus
  178. else
  179. p^.assigntyp:=at_minus;
  180. putnode(hp);
  181. end;
  182. if p^.assigntyp<>at_normal then
  183. begin
  184. { for fpu type there is no faster way }
  185. if is_fpu(p^.left^.resulttype) then
  186. case p^.assigntyp of
  187. at_plus : p^.right:=gennode(addn,getcopy(p^.left),p^.right);
  188. at_minus : p^.right:=gennode(subn,getcopy(p^.left),p^.right);
  189. at_star : p^.right:=gennode(muln,getcopy(p^.left),p^.right);
  190. at_slash : p^.right:=gennode(slashn,getcopy(p^.left),p^.right);
  191. end;
  192. end;
  193. {$endif i386}
  194. must_be_valid:=true;
  195. firstpass(p^.right);
  196. must_be_valid:=store_valid;
  197. if codegenerror then
  198. exit;
  199. { some string functions don't need conversion, so treat them separatly }
  200. if is_shortstring(p^.left^.resulttype) and (assigned(p^.right^.resulttype)) then
  201. begin
  202. if not (is_shortstring(p^.right^.resulttype) or
  203. is_ansistring(p^.right^.resulttype) or
  204. ((p^.right^.resulttype^.deftype=orddef) and (porddef(p^.right^.resulttype)^.typ=uchar))) then
  205. begin
  206. p^.right:=gentypeconvnode(p^.right,p^.left^.resulttype);
  207. firstpass(p^.right);
  208. if codegenerror then
  209. exit;
  210. end;
  211. { we call STRCOPY }
  212. procinfo.flags:=procinfo.flags or pi_do_call;
  213. hp:=p^.right;
  214. { test for s:=s+anything ... }
  215. { the problem is for
  216. s:=s+s+s;
  217. this is broken here !! }
  218. { while hp^.treetype=addn do hp:=hp^.left;
  219. if equal_trees(p^.left,hp) then
  220. begin
  221. p^.concat_string:=true;
  222. hp:=p^.right;
  223. while hp^.treetype=addn do
  224. begin
  225. hp^.use_strconcat:=true;
  226. hp:=hp^.left;
  227. end;
  228. end; }
  229. end
  230. else
  231. begin
  232. if (p^.right^.treetype=realconstn) then
  233. begin
  234. if p^.left^.resulttype^.deftype=floatdef then
  235. begin
  236. case pfloatdef(p^.left^.resulttype)^.typ of
  237. s32real : p^.right^.realtyp:=ait_real_32bit;
  238. s64real : p^.right^.realtyp:=ait_real_64bit;
  239. s80real : p^.right^.realtyp:=ait_real_extended;
  240. { what about f32bit and s64bit }
  241. else
  242. begin
  243. p^.right:=gentypeconvnode(p^.right,p^.left^.resulttype);
  244. { nochmal firstpass wegen der Typkonvertierung aufrufen }
  245. firstpass(p^.right);
  246. if codegenerror then
  247. exit;
  248. end;
  249. end;
  250. end;
  251. end
  252. else
  253. begin
  254. p^.right:=gentypeconvnode(p^.right,p^.left^.resulttype);
  255. firstpass(p^.right);
  256. if codegenerror then
  257. exit;
  258. end;
  259. end;
  260. p^.resulttype:=voiddef;
  261. {
  262. p^.registers32:=max(p^.left^.registers32,p^.right^.registers32);
  263. p^.registersfpu:=max(p^.left^.registersfpu,p^.right^.registersfpu);
  264. }
  265. p^.registers32:=p^.left^.registers32+p^.right^.registers32;
  266. p^.registersfpu:=max(p^.left^.registersfpu,p^.right^.registersfpu);
  267. {$ifdef SUPPORT_MMX}
  268. p^.registersmmx:=max(p^.left^.registersmmx,p^.right^.registersmmx);
  269. {$endif SUPPORT_MMX}
  270. end;
  271. {*****************************************************************************
  272. FirstFuncRet
  273. *****************************************************************************}
  274. procedure firstfuncret(var p : ptree);
  275. begin
  276. p^.resulttype:=p^.retdef;
  277. p^.location.loc:=LOC_REFERENCE;
  278. if ret_in_param(p^.retdef) or
  279. (@procinfo<>pprocinfo(p^.funcretprocinfo)) then
  280. p^.registers32:=1;
  281. { no claim if setting higher return value_str }
  282. if must_be_valid and
  283. (@procinfo=pprocinfo(p^.funcretprocinfo)) and
  284. not procinfo.funcret_is_valid then
  285. CGMessage(sym_w_function_result_not_set);
  286. if count_ref then
  287. pprocinfo(p^.funcretprocinfo)^.funcret_is_valid:=true;
  288. end;
  289. {*****************************************************************************
  290. FirstArrayConstruct
  291. *****************************************************************************}
  292. procedure firstarrayconstruct(var p : ptree);
  293. var
  294. pd : pdef;
  295. hp : ptree;
  296. len : longint;
  297. varia : boolean;
  298. begin
  299. { are we allowing array constructor? Then convert it to a set }
  300. if not allow_array_constructor then
  301. begin
  302. arrayconstructor_to_set(p);
  303. firstpass(p);
  304. exit;
  305. end;
  306. { only pass left tree, right tree contains next construct if any }
  307. pd:=nil;
  308. len:=0;
  309. varia:=false;
  310. if assigned(p^.left) then
  311. begin
  312. hp:=p;
  313. while assigned(hp) do
  314. begin
  315. firstpass(hp^.left);
  316. case hp^.left^.resulttype^.deftype of
  317. floatdef : begin
  318. hp^.left:=gentypeconvnode(hp^.left,s80floatdef);
  319. firstpass(hp^.left);
  320. end;
  321. end;
  322. if (pd=nil) then
  323. pd:=hp^.left^.resulttype
  324. else
  325. if (not varia) and (not is_equal(pd,hp^.left^.resulttype)) then
  326. begin
  327. varia:=true;
  328. Comment(V_Warning,'Variant type found !!');
  329. end;
  330. inc(len);
  331. hp:=hp^.right;
  332. end;
  333. end;
  334. calcregisters(p,0,0,0);
  335. p^.resulttype:=new(parraydef,init(0,len-1,pd));
  336. parraydef(p^.resulttype)^.IsConstructor:=true;
  337. parraydef(p^.resulttype)^.IsVariant:=varia;
  338. p^.location.loc:=LOC_REFERENCE;
  339. end;
  340. {*****************************************************************************
  341. FirstArrayConstructRange
  342. *****************************************************************************}
  343. procedure firstarrayconstructrange(var p : ptree);
  344. begin
  345. { This is not allowed, it's only to support sets when parsing the [a..b] }
  346. Internalerror(4236);
  347. Codegenerror:=true;
  348. end;
  349. {*****************************************************************************
  350. Type
  351. *****************************************************************************}
  352. procedure firsttype(var p : ptree);
  353. begin
  354. { do nothing, p^.resulttype is already set }
  355. end;
  356. end.
  357. {
  358. $Log$
  359. Revision 1.5 1998-10-06 20:49:12 peter
  360. * m68k compiler compiles again
  361. Revision 1.4 1998/09/28 11:07:40 peter
  362. + floatdef support for array of const
  363. Revision 1.3 1998/09/27 10:16:27 florian
  364. * type casts pchar<->ansistring fixed
  365. * ansistring[..] calls does now an unique call
  366. Revision 1.2 1998/09/24 15:13:48 peter
  367. * fixed type node which was always set to void :(
  368. Revision 1.1 1998/09/23 20:42:24 peter
  369. * splitted pass_1
  370. }