tcld.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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,tgenm68k
  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. firstpass(p^.left);
  158. if codegenerror then
  159. exit;
  160. { assignements to open arrays aren't allowed }
  161. if is_open_array(p^.left^.resulttype) then
  162. CGMessage(type_e_mismatch);
  163. { test if we can avoid copying string to temp
  164. as in s:=s+...; (PM) }
  165. {$ifdef dummyi386}
  166. if ((p^.right^.treetype=addn) or (p^.right^.treetype=subn)) and
  167. equal_trees(p^.left,p^.right^.left) and
  168. (ret_in_acc(p^.left^.resulttype)) and
  169. (not cs_rangechecking in aktmoduleswitches^) then
  170. begin
  171. disposetree(p^.right^.left);
  172. hp:=p^.right;
  173. p^.right:=p^.right^.right;
  174. if hp^.treetype=addn then
  175. p^.assigntyp:=at_plus
  176. else
  177. p^.assigntyp:=at_minus;
  178. putnode(hp);
  179. end;
  180. if p^.assigntyp<>at_normal then
  181. begin
  182. { for fpu type there is no faster way }
  183. if is_fpu(p^.left^.resulttype) then
  184. case p^.assigntyp of
  185. at_plus : p^.right:=gennode(addn,getcopy(p^.left),p^.right);
  186. at_minus : p^.right:=gennode(subn,getcopy(p^.left),p^.right);
  187. at_star : p^.right:=gennode(muln,getcopy(p^.left),p^.right);
  188. at_slash : p^.right:=gennode(slashn,getcopy(p^.left),p^.right);
  189. end;
  190. end;
  191. {$endif i386}
  192. must_be_valid:=true;
  193. firstpass(p^.right);
  194. must_be_valid:=store_valid;
  195. if codegenerror then
  196. exit;
  197. { some string functions don't need conversion, so treat them separatly }
  198. if is_shortstring(p^.left^.resulttype) and (assigned(p^.right^.resulttype)) then
  199. begin
  200. if not (is_shortstring(p^.right^.resulttype) or
  201. is_ansistring(p^.right^.resulttype) or
  202. ((p^.right^.resulttype^.deftype=orddef) and (porddef(p^.right^.resulttype)^.typ=uchar))) then
  203. begin
  204. p^.right:=gentypeconvnode(p^.right,p^.left^.resulttype);
  205. firstpass(p^.right);
  206. if codegenerror then
  207. exit;
  208. end;
  209. { we call STRCOPY }
  210. procinfo.flags:=procinfo.flags or pi_do_call;
  211. hp:=p^.right;
  212. { test for s:=s+anything ... }
  213. { the problem is for
  214. s:=s+s+s;
  215. this is broken here !! }
  216. { while hp^.treetype=addn do hp:=hp^.left;
  217. if equal_trees(p^.left,hp) then
  218. begin
  219. p^.concat_string:=true;
  220. hp:=p^.right;
  221. while hp^.treetype=addn do
  222. begin
  223. hp^.use_strconcat:=true;
  224. hp:=hp^.left;
  225. end;
  226. end; }
  227. end
  228. else
  229. begin
  230. if (p^.right^.treetype=realconstn) then
  231. begin
  232. if p^.left^.resulttype^.deftype=floatdef then
  233. begin
  234. case pfloatdef(p^.left^.resulttype)^.typ of
  235. s32real : p^.right^.realtyp:=ait_real_32bit;
  236. s64real : p^.right^.realtyp:=ait_real_64bit;
  237. s80real : p^.right^.realtyp:=ait_real_extended;
  238. { what about f32bit and s64bit }
  239. else
  240. begin
  241. p^.right:=gentypeconvnode(p^.right,p^.left^.resulttype);
  242. { nochmal firstpass wegen der Typkonvertierung aufrufen }
  243. firstpass(p^.right);
  244. if codegenerror then
  245. exit;
  246. end;
  247. end;
  248. end;
  249. end
  250. else
  251. begin
  252. p^.right:=gentypeconvnode(p^.right,p^.left^.resulttype);
  253. firstpass(p^.right);
  254. if codegenerror then
  255. exit;
  256. end;
  257. end;
  258. p^.resulttype:=voiddef;
  259. {
  260. p^.registers32:=max(p^.left^.registers32,p^.right^.registers32);
  261. p^.registersfpu:=max(p^.left^.registersfpu,p^.right^.registersfpu);
  262. }
  263. p^.registers32:=p^.left^.registers32+p^.right^.registers32;
  264. p^.registersfpu:=max(p^.left^.registersfpu,p^.right^.registersfpu);
  265. {$ifdef SUPPORT_MMX}
  266. p^.registersmmx:=max(p^.left^.registersmmx,p^.right^.registersmmx);
  267. {$endif SUPPORT_MMX}
  268. end;
  269. {*****************************************************************************
  270. FirstFuncRet
  271. *****************************************************************************}
  272. procedure firstfuncret(var p : ptree);
  273. begin
  274. p^.resulttype:=p^.retdef;
  275. p^.location.loc:=LOC_REFERENCE;
  276. if ret_in_param(p^.retdef) or
  277. (@procinfo<>pprocinfo(p^.funcretprocinfo)) then
  278. p^.registers32:=1;
  279. { no claim if setting higher return value_str }
  280. if must_be_valid and
  281. (@procinfo=pprocinfo(p^.funcretprocinfo)) and
  282. not procinfo.funcret_is_valid then
  283. CGMessage(sym_w_function_result_not_set);
  284. if count_ref then
  285. pprocinfo(p^.funcretprocinfo)^.funcret_is_valid:=true;
  286. end;
  287. {*****************************************************************************
  288. FirstArrayConstruct
  289. *****************************************************************************}
  290. procedure firstarrayconstruct(var p : ptree);
  291. var
  292. pd : pdef;
  293. hp : ptree;
  294. len : longint;
  295. varia : boolean;
  296. begin
  297. { are we allowing array constructor? Then convert it to a set }
  298. if not allow_array_constructor then
  299. begin
  300. arrayconstructor_to_set(p);
  301. firstpass(p);
  302. exit;
  303. end;
  304. { only pass left tree, right tree contains next construct if any }
  305. pd:=nil;
  306. len:=0;
  307. varia:=false;
  308. if assigned(p^.left) then
  309. begin
  310. hp:=p;
  311. while assigned(hp) do
  312. begin
  313. firstpass(hp^.left);
  314. case hp^.left^.resulttype^.deftype of
  315. floatdef : begin
  316. gentypeconvnode(hp^.left,s80floatdef);
  317. firstpass(hp^.left);
  318. end;
  319. end;
  320. if (pd=nil) then
  321. pd:=hp^.left^.resulttype
  322. else
  323. if (not varia) and (not is_equal(pd,hp^.left^.resulttype)) then
  324. begin
  325. varia:=true;
  326. Comment(V_Warning,'Variant type found !!');
  327. end;
  328. inc(len);
  329. hp:=hp^.right;
  330. end;
  331. end;
  332. calcregisters(p,0,0,0);
  333. p^.resulttype:=new(parraydef,init(0,len-1,pd));
  334. parraydef(p^.resulttype)^.IsConstructor:=true;
  335. parraydef(p^.resulttype)^.IsVariant:=varia;
  336. p^.location.loc:=LOC_REFERENCE;
  337. end;
  338. {*****************************************************************************
  339. FirstArrayConstructRange
  340. *****************************************************************************}
  341. procedure firstarrayconstructrange(var p : ptree);
  342. begin
  343. { This is not allowed, it's only to support sets when parsing the [a..b] }
  344. Internalerror(4236);
  345. Codegenerror:=true;
  346. end;
  347. {*****************************************************************************
  348. Type
  349. *****************************************************************************}
  350. procedure firsttype(var p : ptree);
  351. begin
  352. { do nothing, p^.resulttype is already set }
  353. end;
  354. end.
  355. {
  356. $Log$
  357. Revision 1.2 1998-09-24 15:13:48 peter
  358. * fixed type node which was always set to void :(
  359. Revision 1.1 1998/09/23 20:42:24 peter
  360. * splitted pass_1
  361. }