nld.pas 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. {
  2. $Id$
  3. Copyright (c) 2000 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.41 2002-05-16 19:46:38 carl
  837. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  838. + try to fix temp allocation (still in ifdef)
  839. + generic constructor calls
  840. + start of tassembler / tmodulebase class cleanup
  841. Revision 1.39 2002/05/12 16:53:07 peter
  842. * moved entry and exitcode to ncgutil and cgobj
  843. * foreach gets extra argument for passing local data to the
  844. iterator function
  845. * -CR checks also class typecasts at runtime by changing them
  846. into as
  847. * fixed compiler to cycle with the -CR option
  848. * fixed stabs with elf writer, finally the global variables can
  849. be watched
  850. * removed a lot of routines from cga unit and replaced them by
  851. calls to cgobj
  852. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  853. u32bit then the other is typecasted also to u32bit without giving
  854. a rangecheck warning/error.
  855. * fixed pascal calling method with reversing also the high tree in
  856. the parast, detected by tcalcst3 test
  857. Revision 1.38 2002/04/25 20:16:39 peter
  858. * moved more routines from cga/n386util
  859. Revision 1.37 2002/04/23 19:16:34 peter
  860. * add pinline unit that inserts compiler supported functions using
  861. one or more statements
  862. * moved finalize and setlength from ninl to pinline
  863. Revision 1.36 2002/04/22 16:30:06 peter
  864. * fixed @methodpointer
  865. Revision 1.35 2002/04/21 19:02:04 peter
  866. * removed newn and disposen nodes, the code is now directly
  867. inlined from pexpr
  868. * -an option that will write the secondpass nodes to the .s file, this
  869. requires EXTDEBUG define to actually write the info
  870. * fixed various internal errors and crashes due recent code changes
  871. Revision 1.34 2002/04/02 17:11:29 peter
  872. * tlocation,treference update
  873. * LOC_CONSTANT added for better constant handling
  874. * secondadd splitted in multiple routines
  875. * location_force_reg added for loading a location to a register
  876. of a specified size
  877. * secondassignment parses now first the right and then the left node
  878. (this is compatible with Kylix). This saves a lot of push/pop especially
  879. with string operations
  880. * adapted some routines to use the new cg methods
  881. Revision 1.33 2002/03/31 20:26:34 jonas
  882. + a_loadfpu_* and a_loadmm_* methods in tcg
  883. * register allocation is now handled by a class and is mostly processor
  884. independent (+rgobj.pas and i386/rgcpu.pas)
  885. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  886. * some small improvements and fixes to the optimizer
  887. * some register allocation fixes
  888. * some fpuvaroffset fixes in the unary minus node
  889. * push/popusedregisters is now called rg.save/restoreusedregisters and
  890. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  891. also better optimizable)
  892. * fixed and optimized register saving/restoring for new/dispose nodes
  893. * LOC_FPU locations now also require their "register" field to be set to
  894. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  895. - list field removed of the tnode class because it's not used currently
  896. and can cause hard-to-find bugs
  897. Revision 1.32 2002/01/19 11:52:32 peter
  898. * dynarr:=nil support added
  899. Revision 1.31 2001/12/28 15:02:00 jonas
  900. * fixed web bug 1684 (it already didn't crash anymore, but it also didn't
  901. generate an error) ("merged")
  902. Revision 1.30 2001/11/07 13:52:52 jonas
  903. * only save/restore nf_forcevaria flag when reversing order of
  904. arrayconstructor elements, since the other flags are element specific
  905. Revision 1.29 2001/11/02 22:58:02 peter
  906. * procsym definition rewrite
  907. Revision 1.28 2001/10/31 17:34:20 jonas
  908. * fixed web bug 1651
  909. Revision 1.27 2001/10/28 17:22:25 peter
  910. * allow assignment of overloaded procedures to procvars when we know
  911. which procedure to take
  912. Revision 1.26 2001/10/12 13:51:51 jonas
  913. * fixed internalerror(10) due to previous fpu overflow fixes ("merged")
  914. * fixed bug in n386add (introduced after compilerproc changes for string
  915. operations) where calcregisters wasn't called for shortstring addnodes
  916. * NOTE: from now on, the location of a binary node must now always be set
  917. before you call calcregisters() for it
  918. Revision 1.25 2001/09/02 21:12:07 peter
  919. * move class of definitions into type section for delphi
  920. Revision 1.24 2001/08/30 15:48:34 jonas
  921. * fix from Peter for getting correct symtableentry for funcret loads
  922. Revision 1.23 2001/08/26 13:36:41 florian
  923. * some cg reorganisation
  924. * some PPC updates
  925. Revision 1.22 2001/08/12 22:11:52 peter
  926. * errordef.typesym is not updated anymore
  927. Revision 1.21 2001/08/06 21:40:47 peter
  928. * funcret moved from tprocinfo to tprocdef
  929. Revision 1.20 2001/07/30 20:52:25 peter
  930. * fixed array constructor passing with type conversions
  931. Revision 1.19 2001/06/04 18:07:47 peter
  932. * remove unused typenode for procvar load. Don't know what happened why
  933. this code was not there already with revision 1.17.
  934. Revision 1.18 2001/06/04 11:48:01 peter
  935. * better const to var checking
  936. Revision 1.17 2001/05/19 21:19:57 peter
  937. * remove unused typenode for procvars to prevent error
  938. * typenode.allowed flag to allow a typenode
  939. Revision 1.16 2001/05/09 19:57:51 peter
  940. * typenode doesn't generate code, give error in pass_1 instead of
  941. getting an abstract methode runtime error
  942. Revision 1.15 2001/04/14 14:06:31 peter
  943. * move more code from loadnode.pass_1 to det_resulttype
  944. Revision 1.14 2001/04/13 01:22:10 peter
  945. * symtable change to classes
  946. * range check generation and errors fixed, make cycle DEBUG=1 works
  947. * memory leaks fixed
  948. Revision 1.13 2001/04/05 21:03:08 peter
  949. * array constructor fix
  950. Revision 1.12 2001/04/04 22:42:40 peter
  951. * move constant folding into det_resulttype
  952. Revision 1.11 2001/04/02 21:20:31 peter
  953. * resulttype rewrite
  954. Revision 1.10 2000/12/31 11:14:10 jonas
  955. + implemented/fixed docompare() mathods for all nodes (not tested)
  956. + nopt.pas, nadd.pas, i386/n386opt.pas: optimized nodes for adding strings
  957. and constant strings/chars together
  958. * n386add.pas: don't copy temp strings (of size 256) to another temp string
  959. when adding
  960. Revision 1.9 2000/11/29 00:30:33 florian
  961. * unused units removed from uses clause
  962. * some changes for widestrings
  963. Revision 1.8 2000/11/04 14:25:20 florian
  964. + merged Attila's changes for interfaces, not tested yet
  965. Revision 1.7 2000/10/31 22:02:49 peter
  966. * symtable splitted, no real code changes
  967. Revision 1.6 2000/10/14 10:14:50 peter
  968. * moehrendorf oct 2000 rewrite
  969. Revision 1.5 2000/10/01 19:48:24 peter
  970. * lot of compile updates for cg11
  971. Revision 1.4 2000/09/28 19:49:52 florian
  972. *** empty log message ***
  973. Revision 1.3 2000/09/27 18:14:31 florian
  974. * fixed a lot of syntax errors in the n*.pas stuff
  975. Revision 1.2 2000/09/25 15:37:14 florian
  976. * more fixes
  977. Revision 1.1 2000/09/25 14:55:05 florian
  978. * initial revision
  979. }