tcld.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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. ,i386base
  36. ,tgeni386
  37. {$endif}
  38. {$ifdef m68k}
  39. ,m68k,tgen68k
  40. {$endif}
  41. ;
  42. {*****************************************************************************
  43. FirstLoad
  44. *****************************************************************************}
  45. procedure firstload(var p : ptree);
  46. var
  47. p1 : ptree;
  48. begin
  49. if (p^.symtable^.symtabletype=withsymtable) and
  50. (pwithsymtable(p^.symtable)^.direct_with) then
  51. begin
  52. p1:=getcopy(ptree(pwithsymtable(p^.symtable)^.withrefnode));
  53. p1:=gensubscriptnode(pvarsym(p^.symtableentry),p1);
  54. putnode(p);
  55. p:=p1;
  56. firstpass(p);
  57. exit;
  58. end;
  59. p^.location.loc:=LOC_REFERENCE;
  60. p^.registers32:=0;
  61. p^.registersfpu:=0;
  62. {$ifdef SUPPORT_MMX}
  63. p^.registersmmx:=0;
  64. {$endif SUPPORT_MMX}
  65. if p^.symtableentry^.typ=funcretsym then
  66. begin
  67. p1:=genzeronode(funcretn);
  68. p1^.funcretprocinfo:=pprocinfo(pfuncretsym(p^.symtableentry)^.funcretprocinfo);
  69. p1^.retdef:=pfuncretsym(p^.symtableentry)^.funcretdef;
  70. firstpass(p1);
  71. putnode(p);
  72. p:=p1;
  73. exit;
  74. end;
  75. if p^.symtableentry^.typ=absolutesym then
  76. begin
  77. p^.resulttype:=pabsolutesym(p^.symtableentry)^.definition;
  78. if pabsolutesym(p^.symtableentry)^.abstyp=tovar then
  79. p^.symtableentry:=pabsolutesym(p^.symtableentry)^.ref;
  80. p^.symtable:=p^.symtableentry^.owner;
  81. p^.is_absolute:=true;
  82. end;
  83. case p^.symtableentry^.typ of
  84. absolutesym :;
  85. varsym :
  86. begin
  87. if not(p^.is_absolute) and (p^.resulttype=nil) then
  88. p^.resulttype:=pvarsym(p^.symtableentry)^.definition;
  89. if (p^.symtable^.symtabletype in [parasymtable,localsymtable]) and
  90. (lexlevel>p^.symtable^.symtablelevel) then
  91. begin
  92. { if the variable is in an other stackframe then we need
  93. a register to dereference }
  94. if (p^.symtable^.symtablelevel)>0 then
  95. begin
  96. p^.registers32:=1;
  97. { further, the variable can't be put into a register }
  98. pvarsym(p^.symtableentry)^.var_options:=
  99. pvarsym(p^.symtableentry)^.var_options and not vo_regable;
  100. end;
  101. end;
  102. if (pvarsym(p^.symtableentry)^.varspez=vs_const) then
  103. p^.location.loc:=LOC_MEM;
  104. { we need a register for call by reference parameters }
  105. if (pvarsym(p^.symtableentry)^.varspez=vs_var) or
  106. ((pvarsym(p^.symtableentry)^.varspez=vs_const) and
  107. push_addr_param(pvarsym(p^.symtableentry)^.definition)) or
  108. { call by value open arrays are also indirect addressed }
  109. is_open_array(pvarsym(p^.symtableentry)^.definition) then
  110. p^.registers32:=1;
  111. if p^.symtable^.symtabletype=withsymtable then
  112. inc(p^.registers32);
  113. if (pvarsym(p^.symtableentry)^.var_options and (vo_is_thread_var or vo_is_dll_var))<>0 then
  114. p^.registers32:=1;
  115. { a class variable is a pointer !!!
  116. yes, but we have to resolve the reference in an
  117. appropriate tree node (FK)
  118. if (pvarsym(p^.symtableentry)^.definition^.deftype=objectdef) and
  119. ((pobjectdef(pvarsym(p^.symtableentry)^.definition)^.options and oo_is_class)<>0) then
  120. p^.registers32:=1;
  121. }
  122. { count variable references }
  123. if must_be_valid and p^.is_first then
  124. begin
  125. if pvarsym(p^.symtableentry)^.is_valid=2 then
  126. if (assigned(pvarsym(p^.symtableentry)^.owner) and assigned(aktprocsym)
  127. and (pvarsym(p^.symtableentry)^.owner = aktprocsym^.definition^.localst)) then
  128. begin
  129. if p^.symtable^.symtabletype=localsymtable then
  130. CGMessage1(sym_n_uninitialized_local_variable,pvarsym(p^.symtableentry)^.name)
  131. else
  132. CGMessage1(sym_n_uninitialized_variable,pvarsym(p^.symtableentry)^.name);
  133. end;
  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. p^.right:=gentypeconvnode(p^.right,p^.left^.resulttype);
  262. firstpass(p^.right);
  263. if codegenerror then
  264. exit;
  265. end;
  266. p^.resulttype:=voiddef;
  267. {
  268. p^.registers32:=max(p^.left^.registers32,p^.right^.registers32);
  269. p^.registersfpu:=max(p^.left^.registersfpu,p^.right^.registersfpu);
  270. }
  271. p^.registers32:=p^.left^.registers32+p^.right^.registers32;
  272. p^.registersfpu:=max(p^.left^.registersfpu,p^.right^.registersfpu);
  273. {$ifdef SUPPORT_MMX}
  274. p^.registersmmx:=max(p^.left^.registersmmx,p^.right^.registersmmx);
  275. {$endif SUPPORT_MMX}
  276. end;
  277. {*****************************************************************************
  278. FirstFuncRet
  279. *****************************************************************************}
  280. procedure firstfuncret(var p : ptree);
  281. begin
  282. p^.resulttype:=p^.retdef;
  283. p^.location.loc:=LOC_REFERENCE;
  284. if ret_in_param(p^.retdef) or
  285. (@procinfo<>pprocinfo(p^.funcretprocinfo)) then
  286. p^.registers32:=1;
  287. { no claim if setting higher return value_str }
  288. if must_be_valid and
  289. (@procinfo=pprocinfo(p^.funcretprocinfo)) and
  290. not procinfo.funcret_is_valid then
  291. CGMessage(sym_w_function_result_not_set);
  292. if count_ref then
  293. pprocinfo(p^.funcretprocinfo)^.funcret_is_valid:=true;
  294. end;
  295. {*****************************************************************************
  296. FirstArrayConstructRange
  297. *****************************************************************************}
  298. procedure firstarrayconstructrange(var p:ptree);
  299. begin
  300. firstpass(p^.left);
  301. firstpass(p^.right);
  302. calcregisters(p,0,0,0);
  303. p^.resulttype:=p^.left^.resulttype;
  304. end;
  305. {*****************************************************************************
  306. FirstArrayConstruct
  307. *****************************************************************************}
  308. procedure firstarrayconstruct(var p : ptree);
  309. var
  310. pd : pdef;
  311. thp,
  312. chp,
  313. hp : ptree;
  314. len : longint;
  315. varia : boolean;
  316. begin
  317. { are we allowing array constructor? Then convert it to a set }
  318. if not allow_array_constructor then
  319. begin
  320. arrayconstructor_to_set(p);
  321. firstpass(p);
  322. exit;
  323. end;
  324. { only pass left tree, right tree contains next construct if any }
  325. pd:=nil;
  326. len:=0;
  327. varia:=false;
  328. if assigned(p^.left) then
  329. begin
  330. hp:=p;
  331. while assigned(hp) do
  332. begin
  333. firstpass(hp^.left);
  334. if not get_para_resulttype then
  335. begin
  336. case hp^.left^.resulttype^.deftype of
  337. enumdef :
  338. begin
  339. hp^.left:=gentypeconvnode(hp^.left,s32bitdef);
  340. firstpass(hp^.left);
  341. end;
  342. orddef :
  343. begin
  344. if is_integer(hp^.left^.resulttype) then
  345. begin
  346. hp^.left:=gentypeconvnode(hp^.left,s32bitdef);
  347. firstpass(hp^.left);
  348. end;
  349. end;
  350. floatdef :
  351. begin
  352. hp^.left:=gentypeconvnode(hp^.left,bestrealdef^);
  353. firstpass(hp^.left);
  354. end;
  355. stringdef :
  356. begin
  357. if p^.cargs then
  358. begin
  359. hp^.left:=gentypeconvnode(hp^.left,charpointerdef);
  360. firstpass(hp^.left);
  361. end;
  362. end;
  363. recorddef,
  364. arraydef :
  365. CGMessage(type_e_wrong_type_in_array_constructor);
  366. end;
  367. end;
  368. if (pd=nil) then
  369. pd:=hp^.left^.resulttype
  370. else
  371. if (not varia) and (not is_equal(pd,hp^.left^.resulttype)) then
  372. varia:=true;
  373. inc(len);
  374. hp:=hp^.right;
  375. end;
  376. { swap the tree for cargs }
  377. if p^.cargs and (not p^.cargswap) then
  378. begin
  379. chp:=nil;
  380. hp:=p;
  381. while assigned(hp) do
  382. begin
  383. thp:=hp^.right;
  384. hp^.right:=chp;
  385. chp:=hp;
  386. hp:=thp;
  387. end;
  388. p:=chp;
  389. p^.cargs:=true;
  390. p^.cargswap:=true;
  391. end;
  392. end;
  393. calcregisters(p,0,0,0);
  394. { looks a little bit dangerous to me }
  395. { len-1 gives problems with is_open_array if len=0, }
  396. { is_open_array checks now for isconstructor (FK) }
  397. p^.resulttype:=new(parraydef,init(0,len-1,s32bitdef));
  398. parraydef(p^.resulttype)^.definition:=pd;
  399. parraydef(p^.resulttype)^.IsConstructor:=true;
  400. parraydef(p^.resulttype)^.IsVariant:=varia;
  401. p^.location.loc:=LOC_MEM;
  402. end;
  403. {*****************************************************************************
  404. Type
  405. *****************************************************************************}
  406. procedure firsttype(var p : ptree);
  407. begin
  408. { do nothing, p^.resulttype is already set }
  409. end;
  410. end.
  411. {
  412. $Log$
  413. Revision 1.33 1999-05-27 19:45:21 peter
  414. * removed oldasm
  415. * plabel -> pasmlabel
  416. * -a switches to source writing automaticly
  417. * assembler readers OOPed
  418. * asmsymbol automaticly external
  419. * jumptables and other label fixes for asm readers
  420. Revision 1.32 1999/05/23 18:42:22 florian
  421. * better error recovering in typed constants
  422. * some problems with arrays of const fixed, some problems
  423. due my previous
  424. - the location type of array constructor is now LOC_MEM
  425. - the pushing of high fixed
  426. - parameter copying fixed
  427. - zero temp. allocation removed
  428. * small problem in the assembler writers fixed:
  429. ref to nil wasn't written correctly
  430. Revision 1.31 1999/05/19 15:26:41 florian
  431. * if a non local variables isn't initialized the compiler doesn't write
  432. any longer "local var. seems not to be ..."
  433. Revision 1.30 1999/05/19 10:31:55 florian
  434. * two bugs reported by Romio (bugs 13) are fixed:
  435. - empty array constructors are now handled correctly (e.g. for sysutils.format)
  436. - comparsion of ansistrings was sometimes coded wrong
  437. Revision 1.29 1999/05/17 23:51:45 peter
  438. * with temp vars now use a reference with a persistant temp instead
  439. of setting datasize
  440. Revision 1.27 1999/05/12 00:20:02 peter
  441. * removed R_DEFAULT_SEG
  442. * uniform float names
  443. Revision 1.26 1999/05/06 09:05:36 peter
  444. * generic write_float and str_float
  445. * fixed constant float conversions
  446. Revision 1.25 1999/05/01 13:24:54 peter
  447. * merged nasm compiler
  448. * old asm moved to oldasm/
  449. Revision 1.24 1999/04/28 06:02:17 florian
  450. * changes of Bruessel:
  451. + message handler can now take an explicit self
  452. * typinfo fixed: sometimes the type names weren't written
  453. * the type checking for pointer comparisations and subtraction
  454. and are now more strict (was also buggy)
  455. * small bug fix to link.pas to support compiling on another
  456. drive
  457. * probable bug in popt386 fixed: call/jmp => push/jmp
  458. transformation didn't count correctly the jmp references
  459. + threadvar support
  460. * warning if ln/sqrt gets an invalid constant argument
  461. Revision 1.23 1999/04/21 21:57:33 pierre
  462. * previous log corrected
  463. Revision 1.22 1999/04/21 16:31:47 pierre
  464. * some wrong code in firstfuncret corrected
  465. Revision 1.21 1999/04/01 21:59:57 peter
  466. * type error for array constructor with array,record as argument
  467. Revision 1.20 1999/03/24 23:17:39 peter
  468. * fixed bugs 212,222,225,227,229,231,233
  469. Revision 1.19 1999/03/18 11:21:52 peter
  470. * convert only to s32bit if integer or enum
  471. Revision 1.18 1999/03/16 21:02:10 peter
  472. * all array of const enum/ord are converted to s32bit
  473. Revision 1.17 1999/03/10 13:24:23 pierre
  474. * array of const type to definition field
  475. Revision 1.16 1999/02/22 02:15:52 peter
  476. * updates for ag386bin
  477. Revision 1.15 1999/02/15 13:13:19 pierre
  478. * fix for bug0216
  479. Revision 1.14 1999/01/27 00:13:58 florian
  480. * "procedure of object"-stuff fixed
  481. Revision 1.13 1999/01/21 16:41:07 pierre
  482. * fix for constructor inside with statements
  483. Revision 1.12 1998/12/30 13:41:19 peter
  484. * released valuepara
  485. Revision 1.11 1998/11/18 17:45:28 peter
  486. * fixes for VALUEPARA
  487. Revision 1.10 1998/11/18 15:44:23 peter
  488. * VALUEPARA for tp7 compatible value parameters
  489. Revision 1.9 1998/11/17 00:36:49 peter
  490. * more ansistring fixes
  491. Revision 1.8 1998/11/10 10:09:18 peter
  492. * va_list -> array of const
  493. Revision 1.7 1998/11/05 14:26:48 peter
  494. * fixed variant warning with was sometimes said with sets
  495. Revision 1.6 1998/10/19 08:55:12 pierre
  496. * wrong stabs info corrected once again !!
  497. + variable vmt offset with vmt field only if required
  498. implemented now !!!
  499. Revision 1.5 1998/10/06 20:49:12 peter
  500. * m68k compiler compiles again
  501. Revision 1.4 1998/09/28 11:07:40 peter
  502. + floatdef support for array of const
  503. Revision 1.3 1998/09/27 10:16:27 florian
  504. * type casts pchar<->ansistring fixed
  505. * ansistring[..] calls does now an unique call
  506. Revision 1.2 1998/09/24 15:13:48 peter
  507. * fixed type node which was always set to void :(
  508. Revision 1.1 1998/09/23 20:42:24 peter
  509. * splitted pass_1
  510. }