tcld.pas 19 KB

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