tcld.pas 19 KB

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