tcld.pas 22 KB

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