tcld.pas 21 KB

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