tcld.pas 20 KB

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