tcld.pas 16 KB

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