tcld.pas 20 KB

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