nld.pas 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. {
  2. $Id$
  3. Copyright (c) 2000-2002 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 nld;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,
  23. symconst,symbase,symtype,symsym,symdef;
  24. type
  25. tloadnode = class(tunarynode)
  26. symtableentry : tsym;
  27. symtable : tsymtable;
  28. procdeflist : tprocdef;
  29. constructor create(v : tsym;st : tsymtable);virtual;
  30. constructor create_procvar(v : tsym;d:tprocdef;st : tsymtable);virtual;
  31. procedure set_mp(p:tnode);
  32. function getcopy : tnode;override;
  33. function pass_1 : tnode;override;
  34. function det_resulttype:tnode;override;
  35. function docompare(p: tnode): boolean; override;
  36. end;
  37. tloadnodeclass = class of tloadnode;
  38. { different assignment types }
  39. tassigntype = (at_normal,at_plus,at_minus,at_star,at_slash);
  40. tassignmentnode = class(tbinarynode)
  41. assigntype : tassigntype;
  42. constructor create(l,r : tnode);virtual;
  43. function getcopy : tnode;override;
  44. function pass_1 : tnode;override;
  45. function det_resulttype:tnode;override;
  46. function docompare(p: tnode): boolean; override;
  47. end;
  48. tassignmentnodeclass = class of tassignmentnode;
  49. tfuncretnode = class(tnode)
  50. funcretsym : tfuncretsym;
  51. constructor create(v:tsym);virtual;
  52. function getcopy : tnode;override;
  53. function pass_1 : tnode;override;
  54. function det_resulttype:tnode;override;
  55. function docompare(p: tnode): boolean; override;
  56. end;
  57. tfuncretnodeclass = class of tfuncretnode;
  58. tarrayconstructorrangenode = class(tbinarynode)
  59. constructor create(l,r : tnode);virtual;
  60. function pass_1 : tnode;override;
  61. function det_resulttype:tnode;override;
  62. end;
  63. tarrayconstructorrangenodeclass = class of tarrayconstructorrangenode;
  64. tarrayconstructornode = class(tbinarynode)
  65. constructor create(l,r : tnode);virtual;
  66. function getcopy : tnode;override;
  67. function pass_1 : tnode;override;
  68. function det_resulttype:tnode;override;
  69. function docompare(p: tnode): boolean; override;
  70. procedure force_type(tt:ttype);
  71. end;
  72. tarrayconstructornodeclass = class of tarrayconstructornode;
  73. ttypenode = class(tnode)
  74. allowed : boolean;
  75. restype : ttype;
  76. constructor create(t : ttype);virtual;
  77. function pass_1 : tnode;override;
  78. function det_resulttype:tnode;override;
  79. function docompare(p: tnode): boolean; override;
  80. end;
  81. ttypenodeclass = class of ttypenode;
  82. trttinode = class(tnode)
  83. l1,l2 : longint;
  84. rttitype : trttitype;
  85. rttidef : tstoreddef;
  86. constructor create(def:tstoreddef;rt:trttitype);virtual;
  87. function getcopy : tnode;override;
  88. function pass_1 : tnode;override;
  89. procedure pass_2;override;
  90. function det_resulttype:tnode;override;
  91. function docompare(p: tnode): boolean; override;
  92. end;
  93. trttinodeclass = class of trttinode;
  94. var
  95. cloadnode : tloadnodeclass;
  96. cassignmentnode : tassignmentnodeclass;
  97. cfuncretnode : tfuncretnodeclass;
  98. carrayconstructorrangenode : tarrayconstructorrangenodeclass;
  99. carrayconstructornode : tarrayconstructornodeclass;
  100. ctypenode : ttypenodeclass;
  101. crttinode : trttinodeclass;
  102. implementation
  103. uses
  104. cutils,verbose,globtype,globals,systems,
  105. symtable,types,
  106. htypechk,pass_1,
  107. ncon,ninl,ncnv,nmem,ncal,cpubase,rgobj,cginfo,cgbase
  108. ;
  109. {*****************************************************************************
  110. TLOADNODE
  111. *****************************************************************************}
  112. constructor tloadnode.create(v : tsym;st : tsymtable);
  113. begin
  114. inherited create(loadn,nil);
  115. if not assigned(v) then
  116. internalerror(200108121);
  117. symtableentry:=v;
  118. symtable:=st;
  119. procdeflist:=nil;
  120. end;
  121. constructor tloadnode.create_procvar(v : tsym;d:tprocdef;st : tsymtable);
  122. begin
  123. inherited create(loadn,nil);
  124. if not assigned(v) then
  125. internalerror(200108121);
  126. symtableentry:=v;
  127. symtable:=st;
  128. procdeflist:=d;
  129. end;
  130. procedure tloadnode.set_mp(p:tnode);
  131. begin
  132. left:=p;
  133. end;
  134. function tloadnode.getcopy : tnode;
  135. var
  136. n : tloadnode;
  137. begin
  138. n:=tloadnode(inherited getcopy);
  139. n.symtable:=symtable;
  140. n.symtableentry:=symtableentry;
  141. result:=n;
  142. end;
  143. function tloadnode.det_resulttype:tnode;
  144. var
  145. p1 : tnode;
  146. p : pprocinfo;
  147. begin
  148. result:=nil;
  149. { optimize simple with loadings }
  150. if (symtable.symtabletype=withsymtable) and
  151. (twithsymtable(symtable).direct_with) and
  152. (symtableentry.typ=varsym) then
  153. begin
  154. p1:=tnode(twithsymtable(symtable).withrefnode).getcopy;
  155. p1:=csubscriptnode.create(tvarsym(symtableentry),p1);
  156. left:=nil;
  157. result:=p1;
  158. exit;
  159. end;
  160. { handle first absolute as it will replace the symtableentry }
  161. if symtableentry.typ=absolutesym then
  162. begin
  163. { force the resulttype to the type of the absolute }
  164. resulttype:=tabsolutesym(symtableentry).vartype;
  165. { replace the symtableentry when it points to a var, else
  166. we are finished }
  167. if tabsolutesym(symtableentry).abstyp=tovar then
  168. begin
  169. symtableentry:=tabsolutesym(symtableentry).ref;
  170. symtable:=symtableentry.owner;
  171. include(flags,nf_absolute);
  172. end
  173. else
  174. exit;
  175. end;
  176. case symtableentry.typ of
  177. funcretsym :
  178. begin
  179. { find the main funcret for the function }
  180. p:=procinfo;
  181. while assigned(p) do
  182. begin
  183. if assigned(p^.procdef.funcretsym) and
  184. ((tfuncretsym(symtableentry)=p^.procdef.resultfuncretsym) or
  185. (tfuncretsym(symtableentry)=p^.procdef.funcretsym)) then
  186. begin
  187. symtableentry:=p^.procdef.funcretsym;
  188. break;
  189. end;
  190. p:=p^.parent;
  191. end;
  192. { generate funcretnode }
  193. p1:=cfuncretnode.create(symtableentry);
  194. resulttypepass(p1);
  195. { if it's refered as absolute then we need to have the
  196. type of the absolute instead of the function return,
  197. the function return is then also assigned }
  198. if nf_absolute in flags then
  199. begin
  200. tfuncretsym(symtableentry).funcretstate:=vs_assigned;
  201. p1.resulttype:=resulttype;
  202. end;
  203. left:=nil;
  204. result:=p1;
  205. end;
  206. constsym:
  207. begin
  208. if tconstsym(symtableentry).consttyp=constresourcestring then
  209. resulttype:=cansistringtype
  210. else
  211. internalerror(22799);
  212. end;
  213. varsym :
  214. begin
  215. { if it's refered by absolute then it's used }
  216. if nf_absolute in flags then
  217. tvarsym(symtableentry).varstate:=vs_used
  218. else
  219. resulttype:=tvarsym(symtableentry).vartype;
  220. end;
  221. typedconstsym :
  222. if not(nf_absolute in flags) then
  223. resulttype:=ttypedconstsym(symtableentry).typedconsttype;
  224. procsym :
  225. begin
  226. if not assigned(procdeflist) then
  227. begin
  228. if assigned(tprocsym(symtableentry).defs^.next) then
  229. CGMessage(parser_e_no_overloaded_procvars);
  230. resulttype.setdef(tprocsym(symtableentry).defs^.def);
  231. end
  232. else
  233. resulttype.setdef(procdeflist);
  234. if (m_tp_procvar in aktmodeswitches) then
  235. begin
  236. if assigned(left) then
  237. begin
  238. if left.nodetype=typen then
  239. begin
  240. { we need to return only a voidpointer,
  241. so no need to keep the typen }
  242. left.free;
  243. left:=nil;
  244. end;
  245. end
  246. else
  247. begin
  248. { if the owner of the procsym is a object, }
  249. { left must be set, if left isn't set }
  250. { it can be only self }
  251. if (tprocsym(symtableentry).owner.symtabletype=objectsymtable) then
  252. left:=cselfnode.create(tobjectdef(symtableentry.owner.defowner));
  253. end;
  254. end;
  255. { process methodpointer }
  256. if assigned(left) then
  257. begin
  258. resulttypepass(left);
  259. { turn on the allowed flag, the secondpass
  260. will handle the typen itself }
  261. if left.nodetype=typen then
  262. ttypenode(left).allowed:=true;
  263. end;
  264. end;
  265. else
  266. internalerror(200104141);
  267. end;
  268. end;
  269. function tloadnode.pass_1 : tnode;
  270. begin
  271. result:=nil;
  272. location.loc:=LOC_REFERENCE;
  273. registers32:=0;
  274. registersfpu:=0;
  275. {$ifdef SUPPORT_MMX}
  276. registersmmx:=0;
  277. {$endif SUPPORT_MMX}
  278. case symtableentry.typ of
  279. absolutesym :
  280. ;
  281. funcretsym :
  282. internalerror(200104142);
  283. constsym:
  284. begin
  285. if tconstsym(symtableentry).consttyp=constresourcestring then
  286. begin
  287. { we use ansistrings so no fast exit here }
  288. if assigned(procinfo) then
  289. procinfo^.no_fast_exit:=true;
  290. location.loc:=LOC_CREFERENCE;
  291. end;
  292. end;
  293. varsym :
  294. begin
  295. if (symtable.symtabletype in [parasymtable,localsymtable]) and
  296. (lexlevel>symtable.symtablelevel) then
  297. begin
  298. { if the variable is in an other stackframe then we need
  299. a register to dereference }
  300. if (symtable.symtablelevel)>0 then
  301. begin
  302. registers32:=1;
  303. { further, the variable can't be put into a register }
  304. tvarsym(symtableentry).varoptions:=
  305. tvarsym(symtableentry).varoptions-[vo_fpuregable,vo_regable];
  306. end;
  307. end;
  308. if (tvarsym(symtableentry).varspez=vs_const) then
  309. location.loc:=LOC_CREFERENCE;
  310. { we need a register for call by reference parameters }
  311. if (tvarsym(symtableentry).varspez in [vs_var,vs_out]) or
  312. ((tvarsym(symtableentry).varspez=vs_const) and
  313. push_addr_param(tvarsym(symtableentry).vartype.def)) or
  314. { call by value open arrays are also indirect addressed }
  315. is_open_array(tvarsym(symtableentry).vartype.def) then
  316. registers32:=1;
  317. if symtable.symtabletype=withsymtable then
  318. inc(registers32);
  319. if ([vo_is_thread_var,vo_is_dll_var]*tvarsym(symtableentry).varoptions)<>[] then
  320. registers32:=1;
  321. { count variable references }
  322. { this will create problem with local var set by
  323. under_procedures
  324. if (assigned(tvarsym(symtableentry).owner) and assigned(aktprocsym)
  325. and ((tvarsym(symtableentry).owner = aktprocdef.localst)
  326. or (tvarsym(symtableentry).owner = aktprocdef.localst))) then }
  327. if rg.t_times<1 then
  328. inc(tvarsym(symtableentry).refs)
  329. else
  330. inc(tvarsym(symtableentry).refs,rg.t_times);
  331. end;
  332. typedconstsym :
  333. ;
  334. procsym :
  335. begin
  336. { method pointer ? }
  337. if assigned(left) then
  338. begin
  339. firstpass(left);
  340. registers32:=max(registers32,left.registers32);
  341. registersfpu:=max(registersfpu,left.registersfpu);
  342. {$ifdef SUPPORT_MMX}
  343. registersmmx:=max(registersmmx,left.registersmmx);
  344. {$endif SUPPORT_MMX}
  345. end;
  346. end;
  347. else
  348. internalerror(200104143);
  349. end;
  350. end;
  351. function tloadnode.docompare(p: tnode): boolean;
  352. begin
  353. docompare :=
  354. inherited docompare(p) and
  355. (symtableentry = tloadnode(p).symtableentry) and
  356. (symtable = tloadnode(p).symtable);
  357. end;
  358. {*****************************************************************************
  359. TASSIGNMENTNODE
  360. *****************************************************************************}
  361. constructor tassignmentnode.create(l,r : tnode);
  362. begin
  363. inherited create(assignn,l,r);
  364. assigntype:=at_normal;
  365. end;
  366. function tassignmentnode.getcopy : tnode;
  367. var
  368. n : tassignmentnode;
  369. begin
  370. n:=tassignmentnode(inherited getcopy);
  371. n.assigntype:=assigntype;
  372. getcopy:=n;
  373. end;
  374. function tassignmentnode.det_resulttype:tnode;
  375. var
  376. hp : tnode;
  377. useshelper : boolean;
  378. begin
  379. result:=nil;
  380. resulttype:=voidtype;
  381. { must be made unique }
  382. if assigned(left) then
  383. begin
  384. set_unique(left);
  385. { set we the function result? }
  386. set_funcret_is_valid(left);
  387. end;
  388. resulttypepass(left);
  389. resulttypepass(right);
  390. set_varstate(left,false);
  391. set_varstate(right,true);
  392. if codegenerror then
  393. exit;
  394. { assignments to open arrays aren't allowed }
  395. if is_open_array(left.resulttype.def) then
  396. CGMessage(type_e_mismatch);
  397. { test if node can be assigned, properties are allowed }
  398. valid_for_assignment(left);
  399. { assigning nil to a dynamic array clears the array }
  400. if is_dynamic_array(left.resulttype.def) and
  401. (right.nodetype=niln) then
  402. begin
  403. hp:=ccallparanode.create(caddrnode.create
  404. (crttinode.create(tstoreddef(left.resulttype.def),initrtti)),
  405. ccallparanode.create(ctypeconvnode.create_explicit(left,voidpointertype),nil));
  406. result := ccallnode.createintern('fpc_dynarray_clear',hp);
  407. left:=nil;
  408. exit;
  409. end;
  410. { shortstring helpers can do the conversion directly,
  411. so treat them separatly }
  412. if (is_shortstring(left.resulttype.def)) then
  413. begin
  414. { test for s:=s+anything ... }
  415. { the problem is for
  416. s:=s+s+s;
  417. this is broken here !! }
  418. {$ifdef newoptimizations2}
  419. { the above is fixed now, but still problem with s := s + f(); if }
  420. { f modifies s (bad programming, so only enable if uncertain }
  421. { optimizations are on) (JM) }
  422. if (cs_UncertainOpts in aktglobalswitches) then
  423. begin
  424. hp := right;
  425. while hp.treetype=addn do
  426. hp:=hp.left;
  427. if equal_trees(left,hp) and
  428. not multiple_uses(left,right) then
  429. begin
  430. concat_string:=true;
  431. hp:=right;
  432. while hp.treetype=addn do
  433. begin
  434. hp.use_strconcat:=true;
  435. hp:=hp.left;
  436. end;
  437. end;
  438. end;
  439. {$endif newoptimizations2}
  440. { insert typeconv, except for chars that are handled in
  441. secondpass and except for ansi/wide string that can
  442. be converted immediatly }
  443. if not(is_char(right.resulttype.def) or
  444. (right.resulttype.def.deftype=stringdef)) then
  445. inserttypeconv(right,left.resulttype);
  446. if right.resulttype.def.deftype=stringdef then
  447. begin
  448. useshelper:=true;
  449. { convert constant strings to shortstrings. But
  450. skip empty constant strings, that will be handled
  451. in secondpass }
  452. if (right.nodetype=stringconstn) then
  453. begin
  454. inserttypeconv(right,left.resulttype);
  455. if (tstringconstnode(right).len=0) then
  456. useshelper:=false;
  457. end;
  458. if useshelper then
  459. begin
  460. hp:=ccallparanode.create
  461. (right,
  462. ccallparanode.create(cinlinenode.create
  463. (in_high_x,false,left.getcopy),nil));
  464. result:=ccallnode.createinternreturn('fpc_'+tstringdef(right.resulttype.def).stringtypname+'_to_shortstr',hp,left);
  465. left:=nil;
  466. right:=nil;
  467. exit;
  468. end;
  469. end;
  470. end
  471. else
  472. inserttypeconv(right,left.resulttype);
  473. { call helpers for interface }
  474. if is_interfacecom(left.resulttype.def) then
  475. begin
  476. hp:=ccallparanode.create(ctypeconvnode.create_explicit
  477. (right,voidpointertype),
  478. ccallparanode.create(ctypeconvnode.create_explicit
  479. (left,voidpointertype),nil));
  480. result:=ccallnode.createintern('fpc_intf_assign',hp);
  481. left:=nil;
  482. right:=nil;
  483. exit;
  484. end;
  485. { check if local proc/func is assigned to procvar }
  486. if right.resulttype.def.deftype=procvardef then
  487. test_local_to_procvar(tprocvardef(right.resulttype.def),left.resulttype.def);
  488. end;
  489. function tassignmentnode.pass_1 : tnode;
  490. begin
  491. result:=nil;
  492. firstpass(left);
  493. firstpass(right);
  494. if codegenerror then
  495. exit;
  496. registers32:=left.registers32+right.registers32;
  497. registersfpu:=max(left.registersfpu,right.registersfpu);
  498. {$ifdef SUPPORT_MMX}
  499. registersmmx:=max(left.registersmmx,right.registersmmx);
  500. {$endif SUPPORT_MMX}
  501. end;
  502. function tassignmentnode.docompare(p: tnode): boolean;
  503. begin
  504. docompare :=
  505. inherited docompare(p) and
  506. (assigntype = tassignmentnode(p).assigntype);
  507. end;
  508. {*****************************************************************************
  509. TFUNCRETNODE
  510. *****************************************************************************}
  511. constructor tfuncretnode.create(v:tsym);
  512. begin
  513. inherited create(funcretn);
  514. funcretsym:=tfuncretsym(v);
  515. end;
  516. function tfuncretnode.getcopy : tnode;
  517. var
  518. n : tfuncretnode;
  519. begin
  520. n:=tfuncretnode(inherited getcopy);
  521. n.funcretsym:=funcretsym;
  522. getcopy:=n;
  523. end;
  524. function tfuncretnode.det_resulttype:tnode;
  525. begin
  526. result:=nil;
  527. resulttype:=funcretsym.returntype;
  528. end;
  529. function tfuncretnode.pass_1 : tnode;
  530. begin
  531. result:=nil;
  532. location.loc:=LOC_REFERENCE;
  533. if ret_in_param(resulttype.def) or
  534. (lexlevel<>funcretsym.owner.symtablelevel) then
  535. registers32:=1;
  536. end;
  537. function tfuncretnode.docompare(p: tnode): boolean;
  538. begin
  539. docompare :=
  540. inherited docompare(p) and
  541. (funcretsym = tfuncretnode(p).funcretsym);
  542. end;
  543. {*****************************************************************************
  544. TARRAYCONSTRUCTORRANGENODE
  545. *****************************************************************************}
  546. constructor tarrayconstructorrangenode.create(l,r : tnode);
  547. begin
  548. inherited create(arrayconstructorrangen,l,r);
  549. end;
  550. function tarrayconstructorrangenode.det_resulttype:tnode;
  551. begin
  552. result:=nil;
  553. resulttypepass(left);
  554. resulttypepass(right);
  555. set_varstate(left,true);
  556. set_varstate(right,true);
  557. if codegenerror then
  558. exit;
  559. resulttype:=left.resulttype;
  560. end;
  561. function tarrayconstructorrangenode.pass_1 : tnode;
  562. begin
  563. firstpass(left);
  564. firstpass(right);
  565. location.loc := LOC_CREFERENCE;
  566. calcregisters(self,0,0,0);
  567. result:=nil;
  568. end;
  569. {****************************************************************************
  570. TARRAYCONSTRUCTORNODE
  571. *****************************************************************************}
  572. constructor tarrayconstructornode.create(l,r : tnode);
  573. begin
  574. inherited create(arrayconstructorn,l,r);
  575. end;
  576. function tarrayconstructornode.getcopy : tnode;
  577. var
  578. n : tarrayconstructornode;
  579. begin
  580. n:=tarrayconstructornode(inherited getcopy);
  581. result:=n;
  582. end;
  583. function tarrayconstructornode.det_resulttype:tnode;
  584. var
  585. htype : ttype;
  586. hp : tarrayconstructornode;
  587. len : longint;
  588. varia : boolean;
  589. begin
  590. result:=nil;
  591. { are we allowing array constructor? Then convert it to a set }
  592. if not allow_array_constructor then
  593. begin
  594. hp:=tarrayconstructornode(getcopy);
  595. arrayconstructor_to_set(hp);
  596. result:=hp;
  597. exit;
  598. end;
  599. { only pass left tree, right tree contains next construct if any }
  600. htype.reset;
  601. len:=0;
  602. varia:=false;
  603. if assigned(left) then
  604. begin
  605. hp:=self;
  606. while assigned(hp) do
  607. begin
  608. resulttypepass(hp.left);
  609. set_varstate(hp.left,true);
  610. if (htype.def=nil) then
  611. htype:=hp.left.resulttype
  612. else
  613. begin
  614. if ((nf_novariaallowed in flags) or (not varia)) and
  615. (not is_equal(htype.def,hp.left.resulttype.def)) then
  616. begin
  617. varia:=true;
  618. end;
  619. end;
  620. inc(len);
  621. hp:=tarrayconstructornode(hp.right);
  622. end;
  623. end;
  624. if not assigned(htype.def) then
  625. htype:=voidtype;
  626. resulttype.setdef(tarraydef.create(0,len-1,s32bittype));
  627. tarraydef(resulttype.def).elementtype:=htype;
  628. tarraydef(resulttype.def).IsConstructor:=true;
  629. tarraydef(resulttype.def).IsVariant:=varia;
  630. end;
  631. procedure tarrayconstructornode.force_type(tt:ttype);
  632. var
  633. hp : tarrayconstructornode;
  634. begin
  635. tarraydef(resulttype.def).elementtype:=tt;
  636. tarraydef(resulttype.def).IsConstructor:=true;
  637. tarraydef(resulttype.def).IsVariant:=false;
  638. if assigned(left) then
  639. begin
  640. hp:=self;
  641. while assigned(hp) do
  642. begin
  643. inserttypeconv(hp.left,tt);
  644. hp:=tarrayconstructornode(hp.right);
  645. end;
  646. end;
  647. end;
  648. function tarrayconstructornode.pass_1 : tnode;
  649. var
  650. thp,
  651. chp,
  652. hp : tarrayconstructornode;
  653. dovariant : boolean;
  654. htype : ttype;
  655. orgflags : tnodeflagset;
  656. begin
  657. dovariant:=(nf_forcevaria in flags) or tarraydef(resulttype.def).isvariant;
  658. result:=nil;
  659. { only pass left tree, right tree contains next construct if any }
  660. if assigned(left) then
  661. begin
  662. hp:=self;
  663. while assigned(hp) do
  664. begin
  665. firstpass(hp.left);
  666. { Insert typeconvs for array of const }
  667. if dovariant then
  668. begin
  669. case hp.left.resulttype.def.deftype of
  670. enumdef :
  671. begin
  672. hp.left:=ctypeconvnode.create(hp.left,s32bittype);
  673. firstpass(hp.left);
  674. end;
  675. orddef :
  676. begin
  677. if is_integer(hp.left.resulttype.def) and
  678. not(is_64bitint(hp.left.resulttype.def)) then
  679. begin
  680. hp.left:=ctypeconvnode.create(hp.left,s32bittype);
  681. firstpass(hp.left);
  682. end;
  683. end;
  684. floatdef :
  685. begin
  686. hp.left:=ctypeconvnode.create(hp.left,pbestrealtype^);
  687. firstpass(hp.left);
  688. end;
  689. stringdef :
  690. begin
  691. if nf_cargs in flags then
  692. begin
  693. hp.left:=ctypeconvnode.create(hp.left,charpointertype);
  694. firstpass(hp.left);
  695. end;
  696. end;
  697. procvardef :
  698. begin
  699. hp.left:=ctypeconvnode.create(hp.left,voidpointertype);
  700. firstpass(hp.left);
  701. end;
  702. pointerdef,
  703. classrefdef,
  704. objectdef : ;
  705. else
  706. CGMessagePos1(hp.left.fileinfo,type_e_wrong_type_in_array_constructor,hp.left.resulttype.def.typename);
  707. end;
  708. end;
  709. hp:=tarrayconstructornode(hp.right);
  710. end;
  711. { swap the tree for cargs }
  712. if (nf_cargs in flags) and (not(nf_cargswap in flags)) then
  713. begin
  714. chp:=nil;
  715. { save resulttype }
  716. htype:=resulttype;
  717. { we need a copy here, because self is destroyed }
  718. { by firstpass later }
  719. hp:=tarrayconstructornode(getcopy);
  720. { we also need a copy of the nf_ forcevaria flag to restore }
  721. { later) (JM) }
  722. orgflags := flags * [nf_forcevaria];
  723. while assigned(hp) do
  724. begin
  725. thp:=tarrayconstructornode(hp.right);
  726. hp.right:=chp;
  727. chp:=hp;
  728. hp:=thp;
  729. end;
  730. chp.flags := chp.flags+orgflags;
  731. include(chp.flags,nf_cargswap);
  732. chp.location.loc:=LOC_CREFERENCE;
  733. calcregisters(chp,0,0,0);
  734. chp.resulttype:=htype;
  735. result:=chp;
  736. exit;
  737. end;
  738. end;
  739. { C Arguments are pushed on the stack and
  740. are not accesible after the push }
  741. if not(nf_cargs in flags) then
  742. location.loc:=LOC_CREFERENCE
  743. else
  744. location.loc:=LOC_INVALID;
  745. calcregisters(self,0,0,0);
  746. end;
  747. function tarrayconstructornode.docompare(p: tnode): boolean;
  748. begin
  749. docompare :=
  750. inherited docompare(p);
  751. end;
  752. {*****************************************************************************
  753. TTYPENODE
  754. *****************************************************************************}
  755. constructor ttypenode.create(t : ttype);
  756. begin
  757. inherited create(typen);
  758. restype:=t;
  759. allowed:=false;
  760. end;
  761. function ttypenode.det_resulttype:tnode;
  762. begin
  763. result:=nil;
  764. resulttype:=restype;
  765. { check if it's valid }
  766. if restype.def.deftype = errordef then
  767. CGMessage(cg_e_illegal_expression);
  768. end;
  769. function ttypenode.pass_1 : tnode;
  770. begin
  771. result:=nil;
  772. { a typenode can't generate code, so we give here
  773. an error. Else it'll be an abstract error in pass_2.
  774. Only when the allowed flag is set we don't generate
  775. an error }
  776. if not allowed then
  777. Message(parser_e_no_type_not_allowed_here);
  778. end;
  779. function ttypenode.docompare(p: tnode): boolean;
  780. begin
  781. docompare :=
  782. inherited docompare(p);
  783. end;
  784. {*****************************************************************************
  785. TRTTINODE
  786. *****************************************************************************}
  787. constructor trttinode.create(def:tstoreddef;rt:trttitype);
  788. begin
  789. inherited create(rttin);
  790. rttidef:=def;
  791. rttitype:=rt;
  792. end;
  793. function trttinode.getcopy : tnode;
  794. var
  795. n : trttinode;
  796. begin
  797. n:=trttinode(inherited getcopy);
  798. n.rttidef:=rttidef;
  799. n.rttitype:=rttitype;
  800. result:=n;
  801. end;
  802. function trttinode.det_resulttype:tnode;
  803. begin
  804. { rtti information will be returned as a void pointer }
  805. result:=nil;
  806. resulttype:=voidpointertype;
  807. end;
  808. function trttinode.pass_1 : tnode;
  809. begin
  810. result:=nil;
  811. location.loc:=LOC_CREFERENCE;
  812. end;
  813. function trttinode.docompare(p: tnode): boolean;
  814. begin
  815. docompare :=
  816. inherited docompare(p) and
  817. (rttidef = trttinode(p).rttidef) and
  818. (rttitype = trttinode(p).rttitype);
  819. end;
  820. procedure trttinode.pass_2;
  821. begin
  822. location_reset(location,LOC_CREFERENCE,OS_NO);
  823. location.reference.symbol:=rttidef.get_rtti_label(rttitype);
  824. end;
  825. begin
  826. cloadnode:=tloadnode;
  827. cassignmentnode:=tassignmentnode;
  828. cfuncretnode:=tfuncretnode;
  829. carrayconstructorrangenode:=tarrayconstructorrangenode;
  830. carrayconstructornode:=tarrayconstructornode;
  831. ctypenode:=ttypenode;
  832. crttinode:=trttinode;
  833. end.
  834. {
  835. $Log$
  836. Revision 1.42 2002-05-18 13:34:10 peter
  837. * readded missing revisions
  838. Revision 1.41 2002/05/16 19:46:38 carl
  839. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  840. + try to fix temp allocation (still in ifdef)
  841. + generic constructor calls
  842. + start of tassembler / tmodulebase class cleanup
  843. Revision 1.39 2002/05/12 16:53:07 peter
  844. * moved entry and exitcode to ncgutil and cgobj
  845. * foreach gets extra argument for passing local data to the
  846. iterator function
  847. * -CR checks also class typecasts at runtime by changing them
  848. into as
  849. * fixed compiler to cycle with the -CR option
  850. * fixed stabs with elf writer, finally the global variables can
  851. be watched
  852. * removed a lot of routines from cga unit and replaced them by
  853. calls to cgobj
  854. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  855. u32bit then the other is typecasted also to u32bit without giving
  856. a rangecheck warning/error.
  857. * fixed pascal calling method with reversing also the high tree in
  858. the parast, detected by tcalcst3 test
  859. Revision 1.38 2002/04/25 20:16:39 peter
  860. * moved more routines from cga/n386util
  861. Revision 1.37 2002/04/23 19:16:34 peter
  862. * add pinline unit that inserts compiler supported functions using
  863. one or more statements
  864. * moved finalize and setlength from ninl to pinline
  865. Revision 1.36 2002/04/22 16:30:06 peter
  866. * fixed @methodpointer
  867. Revision 1.35 2002/04/21 19:02:04 peter
  868. * removed newn and disposen nodes, the code is now directly
  869. inlined from pexpr
  870. * -an option that will write the secondpass nodes to the .s file, this
  871. requires EXTDEBUG define to actually write the info
  872. * fixed various internal errors and crashes due recent code changes
  873. Revision 1.34 2002/04/02 17:11:29 peter
  874. * tlocation,treference update
  875. * LOC_CONSTANT added for better constant handling
  876. * secondadd splitted in multiple routines
  877. * location_force_reg added for loading a location to a register
  878. of a specified size
  879. * secondassignment parses now first the right and then the left node
  880. (this is compatible with Kylix). This saves a lot of push/pop especially
  881. with string operations
  882. * adapted some routines to use the new cg methods
  883. Revision 1.33 2002/03/31 20:26:34 jonas
  884. + a_loadfpu_* and a_loadmm_* methods in tcg
  885. * register allocation is now handled by a class and is mostly processor
  886. independent (+rgobj.pas and i386/rgcpu.pas)
  887. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  888. * some small improvements and fixes to the optimizer
  889. * some register allocation fixes
  890. * some fpuvaroffset fixes in the unary minus node
  891. * push/popusedregisters is now called rg.save/restoreusedregisters and
  892. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  893. also better optimizable)
  894. * fixed and optimized register saving/restoring for new/dispose nodes
  895. * LOC_FPU locations now also require their "register" field to be set to
  896. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  897. - list field removed of the tnode class because it's not used currently
  898. and can cause hard-to-find bugs
  899. Revision 1.32 2002/01/19 11:52:32 peter
  900. * dynarr:=nil support added
  901. }