tcld.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. p^.right:=gentypeconvnode(p^.right,p^.left^.resulttype);
  264. firstpass(p^.right);
  265. if codegenerror then
  266. exit;
  267. end;
  268. p^.resulttype:=voiddef;
  269. {
  270. p^.registers32:=max(p^.left^.registers32,p^.right^.registers32);
  271. p^.registersfpu:=max(p^.left^.registersfpu,p^.right^.registersfpu);
  272. }
  273. p^.registers32:=p^.left^.registers32+p^.right^.registers32;
  274. p^.registersfpu:=max(p^.left^.registersfpu,p^.right^.registersfpu);
  275. {$ifdef SUPPORT_MMX}
  276. p^.registersmmx:=max(p^.left^.registersmmx,p^.right^.registersmmx);
  277. {$endif SUPPORT_MMX}
  278. end;
  279. {*****************************************************************************
  280. FirstFuncRet
  281. *****************************************************************************}
  282. procedure firstfuncret(var p : ptree);
  283. begin
  284. p^.resulttype:=p^.retdef;
  285. p^.location.loc:=LOC_REFERENCE;
  286. if ret_in_param(p^.retdef) or
  287. (@procinfo<>pprocinfo(p^.funcretprocinfo)) then
  288. p^.registers32:=1;
  289. { no claim if setting higher return value_str }
  290. if must_be_valid and
  291. (@procinfo=pprocinfo(p^.funcretprocinfo)) and
  292. not procinfo.funcret_is_valid then
  293. CGMessage(sym_w_function_result_not_set);
  294. if count_ref then
  295. pprocinfo(p^.funcretprocinfo)^.funcret_is_valid:=true;
  296. end;
  297. {*****************************************************************************
  298. FirstArrayConstructRange
  299. *****************************************************************************}
  300. procedure firstarrayconstructrange(var p:ptree);
  301. begin
  302. firstpass(p^.left);
  303. firstpass(p^.right);
  304. calcregisters(p,0,0,0);
  305. p^.resulttype:=p^.left^.resulttype;
  306. end;
  307. {*****************************************************************************
  308. FirstArrayConstruct
  309. *****************************************************************************}
  310. procedure firstarrayconstruct(var p : ptree);
  311. var
  312. pd : pdef;
  313. thp,
  314. chp,
  315. hp : ptree;
  316. len : longint;
  317. varia : boolean;
  318. begin
  319. { are we allowing array constructor? Then convert it to a set }
  320. if not allow_array_constructor then
  321. begin
  322. arrayconstructor_to_set(p);
  323. firstpass(p);
  324. exit;
  325. end;
  326. { only pass left tree, right tree contains next construct if any }
  327. pd:=nil;
  328. len:=0;
  329. varia:=false;
  330. if assigned(p^.left) then
  331. begin
  332. hp:=p;
  333. while assigned(hp) do
  334. begin
  335. firstpass(hp^.left);
  336. if not get_para_resulttype then
  337. begin
  338. case hp^.left^.resulttype^.deftype of
  339. enumdef :
  340. begin
  341. hp^.left:=gentypeconvnode(hp^.left,s32bitdef);
  342. firstpass(hp^.left);
  343. end;
  344. orddef :
  345. begin
  346. if is_integer(hp^.left^.resulttype) then
  347. begin
  348. hp^.left:=gentypeconvnode(hp^.left,s32bitdef);
  349. firstpass(hp^.left);
  350. end;
  351. end;
  352. floatdef :
  353. begin
  354. hp^.left:=gentypeconvnode(hp^.left,bestrealdef^);
  355. firstpass(hp^.left);
  356. end;
  357. stringdef :
  358. begin
  359. if p^.cargs then
  360. begin
  361. hp^.left:=gentypeconvnode(hp^.left,charpointerdef);
  362. firstpass(hp^.left);
  363. end;
  364. end;
  365. recorddef,
  366. arraydef :
  367. CGMessage(type_e_wrong_type_in_array_constructor);
  368. end;
  369. end;
  370. if (pd=nil) then
  371. pd:=hp^.left^.resulttype
  372. else
  373. if (not varia) and (not is_equal(pd,hp^.left^.resulttype)) then
  374. varia:=true;
  375. inc(len);
  376. hp:=hp^.right;
  377. end;
  378. { swap the tree for cargs }
  379. if p^.cargs and (not p^.cargswap) then
  380. begin
  381. chp:=nil;
  382. hp:=p;
  383. while assigned(hp) do
  384. begin
  385. thp:=hp^.right;
  386. hp^.right:=chp;
  387. chp:=hp;
  388. hp:=thp;
  389. end;
  390. p:=chp;
  391. p^.cargs:=true;
  392. p^.cargswap:=true;
  393. end;
  394. end;
  395. calcregisters(p,0,0,0);
  396. p^.resulttype:=new(parraydef,init(0,len-1,s32bitdef));
  397. parraydef(p^.resulttype)^.definition:=pd;
  398. parraydef(p^.resulttype)^.IsConstructor:=true;
  399. parraydef(p^.resulttype)^.IsVariant:=varia;
  400. p^.location.loc:=LOC_REFERENCE;
  401. end;
  402. {*****************************************************************************
  403. Type
  404. *****************************************************************************}
  405. procedure firsttype(var p : ptree);
  406. begin
  407. { do nothing, p^.resulttype is already set }
  408. end;
  409. end.
  410. {
  411. $Log$
  412. Revision 1.26 1999-05-06 09:05:36 peter
  413. * generic write_float and str_float
  414. * fixed constant float conversions
  415. Revision 1.25 1999/05/01 13:24:54 peter
  416. * merged nasm compiler
  417. * old asm moved to oldasm/
  418. Revision 1.24 1999/04/28 06:02:17 florian
  419. * changes of Bruessel:
  420. + message handler can now take an explicit self
  421. * typinfo fixed: sometimes the type names weren't written
  422. * the type checking for pointer comparisations and subtraction
  423. and are now more strict (was also buggy)
  424. * small bug fix to link.pas to support compiling on another
  425. drive
  426. * probable bug in popt386 fixed: call/jmp => push/jmp
  427. transformation didn't count correctly the jmp references
  428. + threadvar support
  429. * warning if ln/sqrt gets an invalid constant argument
  430. Revision 1.23 1999/04/21 21:57:33 pierre
  431. * previous log corrected
  432. Revision 1.22 1999/04/21 16:31:47 pierre
  433. * some wrong code in firstfuncret corrected
  434. Revision 1.21 1999/04/01 21:59:57 peter
  435. * type error for array constructor with array,record as argument
  436. Revision 1.20 1999/03/24 23:17:39 peter
  437. * fixed bugs 212,222,225,227,229,231,233
  438. Revision 1.19 1999/03/18 11:21:52 peter
  439. * convert only to s32bit if integer or enum
  440. Revision 1.18 1999/03/16 21:02:10 peter
  441. * all array of const enum/ord are converted to s32bit
  442. Revision 1.17 1999/03/10 13:24:23 pierre
  443. * array of const type to definition field
  444. Revision 1.16 1999/02/22 02:15:52 peter
  445. * updates for ag386bin
  446. Revision 1.15 1999/02/15 13:13:19 pierre
  447. * fix for bug0216
  448. Revision 1.14 1999/01/27 00:13:58 florian
  449. * "procedure of object"-stuff fixed
  450. Revision 1.13 1999/01/21 16:41:07 pierre
  451. * fix for constructor inside with statements
  452. Revision 1.12 1998/12/30 13:41:19 peter
  453. * released valuepara
  454. Revision 1.11 1998/11/18 17:45:28 peter
  455. * fixes for VALUEPARA
  456. Revision 1.10 1998/11/18 15:44:23 peter
  457. * VALUEPARA for tp7 compatible value parameters
  458. Revision 1.9 1998/11/17 00:36:49 peter
  459. * more ansistring fixes
  460. Revision 1.8 1998/11/10 10:09:18 peter
  461. * va_list -> array of const
  462. Revision 1.7 1998/11/05 14:26:48 peter
  463. * fixed variant warning with was sometimes said with sets
  464. Revision 1.6 1998/10/19 08:55:12 pierre
  465. * wrong stabs info corrected once again !!
  466. + variable vmt offset with vmt field only if required
  467. implemented now !!!
  468. Revision 1.5 1998/10/06 20:49:12 peter
  469. * m68k compiler compiles again
  470. Revision 1.4 1998/09/28 11:07:40 peter
  471. + floatdef support for array of const
  472. Revision 1.3 1998/09/27 10:16:27 florian
  473. * type casts pchar<->ansistring fixed
  474. * ansistring[..] calls does now an unique call
  475. Revision 1.2 1998/09/24 15:13:48 peter
  476. * fixed type node which was always set to void :(
  477. Revision 1.1 1998/09/23 20:42:24 peter
  478. * splitted pass_1
  479. }