tcld.pas 23 KB

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