tcld.pas 19 KB

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