nld.pas 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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 defines.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
  477. (right,
  478. ccallparanode.create(caddrnode.create
  479. (left),nil));
  480. hp:=ccallparanode.create(right,nil);
  481. result:=ccallnode.createintern('fpc_intf_assign',hp);
  482. left:=nil;
  483. right:=nil;
  484. exit;
  485. end;
  486. { check if local proc/func is assigned to procvar }
  487. if right.resulttype.def.deftype=procvardef then
  488. test_local_to_procvar(tprocvardef(right.resulttype.def),left.resulttype.def);
  489. end;
  490. function tassignmentnode.pass_1 : tnode;
  491. begin
  492. result:=nil;
  493. firstpass(left);
  494. firstpass(right);
  495. if codegenerror then
  496. exit;
  497. registers32:=left.registers32+right.registers32;
  498. registersfpu:=max(left.registersfpu,right.registersfpu);
  499. {$ifdef SUPPORT_MMX}
  500. registersmmx:=max(left.registersmmx,right.registersmmx);
  501. {$endif SUPPORT_MMX}
  502. end;
  503. function tassignmentnode.docompare(p: tnode): boolean;
  504. begin
  505. docompare :=
  506. inherited docompare(p) and
  507. (assigntype = tassignmentnode(p).assigntype);
  508. end;
  509. {*****************************************************************************
  510. TFUNCRETNODE
  511. *****************************************************************************}
  512. constructor tfuncretnode.create(v:tsym);
  513. begin
  514. inherited create(funcretn);
  515. funcretsym:=tfuncretsym(v);
  516. end;
  517. function tfuncretnode.getcopy : tnode;
  518. var
  519. n : tfuncretnode;
  520. begin
  521. n:=tfuncretnode(inherited getcopy);
  522. n.funcretsym:=funcretsym;
  523. getcopy:=n;
  524. end;
  525. function tfuncretnode.det_resulttype:tnode;
  526. begin
  527. result:=nil;
  528. resulttype:=funcretsym.returntype;
  529. end;
  530. function tfuncretnode.pass_1 : tnode;
  531. begin
  532. result:=nil;
  533. location.loc:=LOC_REFERENCE;
  534. if ret_in_param(resulttype.def) or
  535. (lexlevel<>funcretsym.owner.symtablelevel) then
  536. registers32:=1;
  537. end;
  538. function tfuncretnode.docompare(p: tnode): boolean;
  539. begin
  540. docompare :=
  541. inherited docompare(p) and
  542. (funcretsym = tfuncretnode(p).funcretsym);
  543. end;
  544. {*****************************************************************************
  545. TARRAYCONSTRUCTORRANGENODE
  546. *****************************************************************************}
  547. constructor tarrayconstructorrangenode.create(l,r : tnode);
  548. begin
  549. inherited create(arrayconstructorrangen,l,r);
  550. end;
  551. function tarrayconstructorrangenode.det_resulttype:tnode;
  552. begin
  553. result:=nil;
  554. resulttypepass(left);
  555. resulttypepass(right);
  556. set_varstate(left,true);
  557. set_varstate(right,true);
  558. if codegenerror then
  559. exit;
  560. resulttype:=left.resulttype;
  561. end;
  562. function tarrayconstructorrangenode.pass_1 : tnode;
  563. begin
  564. firstpass(left);
  565. firstpass(right);
  566. location.loc := LOC_CREFERENCE;
  567. calcregisters(self,0,0,0);
  568. result:=nil;
  569. end;
  570. {****************************************************************************
  571. TARRAYCONSTRUCTORNODE
  572. *****************************************************************************}
  573. constructor tarrayconstructornode.create(l,r : tnode);
  574. begin
  575. inherited create(arrayconstructorn,l,r);
  576. end;
  577. function tarrayconstructornode.getcopy : tnode;
  578. var
  579. n : tarrayconstructornode;
  580. begin
  581. n:=tarrayconstructornode(inherited getcopy);
  582. result:=n;
  583. end;
  584. function tarrayconstructornode.det_resulttype:tnode;
  585. var
  586. htype : ttype;
  587. hp : tarrayconstructornode;
  588. len : longint;
  589. varia : boolean;
  590. begin
  591. result:=nil;
  592. { are we allowing array constructor? Then convert it to a set }
  593. if not allow_array_constructor then
  594. begin
  595. hp:=tarrayconstructornode(getcopy);
  596. arrayconstructor_to_set(hp);
  597. result:=hp;
  598. exit;
  599. end;
  600. { only pass left tree, right tree contains next construct if any }
  601. htype.reset;
  602. len:=0;
  603. varia:=false;
  604. if assigned(left) then
  605. begin
  606. hp:=self;
  607. while assigned(hp) do
  608. begin
  609. resulttypepass(hp.left);
  610. set_varstate(hp.left,true);
  611. if (htype.def=nil) then
  612. htype:=hp.left.resulttype
  613. else
  614. begin
  615. if ((nf_novariaallowed in flags) or (not varia)) and
  616. (not is_equal(htype.def,hp.left.resulttype.def)) then
  617. begin
  618. varia:=true;
  619. end;
  620. end;
  621. inc(len);
  622. hp:=tarrayconstructornode(hp.right);
  623. end;
  624. end;
  625. if not assigned(htype.def) then
  626. htype:=voidtype;
  627. resulttype.setdef(tarraydef.create(0,len-1,s32bittype));
  628. tarraydef(resulttype.def).elementtype:=htype;
  629. tarraydef(resulttype.def).IsConstructor:=true;
  630. tarraydef(resulttype.def).IsVariant:=varia;
  631. end;
  632. procedure tarrayconstructornode.force_type(tt:ttype);
  633. var
  634. hp : tarrayconstructornode;
  635. begin
  636. tarraydef(resulttype.def).elementtype:=tt;
  637. tarraydef(resulttype.def).IsConstructor:=true;
  638. tarraydef(resulttype.def).IsVariant:=false;
  639. if assigned(left) then
  640. begin
  641. hp:=self;
  642. while assigned(hp) do
  643. begin
  644. inserttypeconv(hp.left,tt);
  645. hp:=tarrayconstructornode(hp.right);
  646. end;
  647. end;
  648. end;
  649. function tarrayconstructornode.pass_1 : tnode;
  650. var
  651. thp,
  652. chp,
  653. hp : tarrayconstructornode;
  654. dovariant : boolean;
  655. htype : ttype;
  656. orgflags : tnodeflagset;
  657. begin
  658. dovariant:=(nf_forcevaria in flags) or tarraydef(resulttype.def).isvariant;
  659. result:=nil;
  660. { only pass left tree, right tree contains next construct if any }
  661. if assigned(left) then
  662. begin
  663. hp:=self;
  664. while assigned(hp) do
  665. begin
  666. firstpass(hp.left);
  667. { Insert typeconvs for array of const }
  668. if dovariant then
  669. begin
  670. case hp.left.resulttype.def.deftype of
  671. enumdef :
  672. begin
  673. hp.left:=ctypeconvnode.create(hp.left,s32bittype);
  674. firstpass(hp.left);
  675. end;
  676. orddef :
  677. begin
  678. if is_integer(hp.left.resulttype.def) and
  679. not(is_64bitint(hp.left.resulttype.def)) then
  680. begin
  681. hp.left:=ctypeconvnode.create(hp.left,s32bittype);
  682. firstpass(hp.left);
  683. end;
  684. end;
  685. floatdef :
  686. begin
  687. hp.left:=ctypeconvnode.create(hp.left,pbestrealtype^);
  688. firstpass(hp.left);
  689. end;
  690. stringdef :
  691. begin
  692. if nf_cargs in flags then
  693. begin
  694. hp.left:=ctypeconvnode.create(hp.left,charpointertype);
  695. firstpass(hp.left);
  696. end;
  697. end;
  698. procvardef :
  699. begin
  700. hp.left:=ctypeconvnode.create(hp.left,voidpointertype);
  701. firstpass(hp.left);
  702. end;
  703. pointerdef,
  704. classrefdef,
  705. objectdef : ;
  706. else
  707. CGMessagePos1(hp.left.fileinfo,type_e_wrong_type_in_array_constructor,hp.left.resulttype.def.typename);
  708. end;
  709. end;
  710. hp:=tarrayconstructornode(hp.right);
  711. end;
  712. { swap the tree for cargs }
  713. if (nf_cargs in flags) and (not(nf_cargswap in flags)) then
  714. begin
  715. chp:=nil;
  716. { save resulttype }
  717. htype:=resulttype;
  718. { we need a copy here, because self is destroyed }
  719. { by firstpass later }
  720. hp:=tarrayconstructornode(getcopy);
  721. { we also need a copy of the nf_ forcevaria flag to restore }
  722. { later) (JM) }
  723. orgflags := flags * [nf_forcevaria];
  724. while assigned(hp) do
  725. begin
  726. thp:=tarrayconstructornode(hp.right);
  727. hp.right:=chp;
  728. chp:=hp;
  729. hp:=thp;
  730. end;
  731. chp.flags := chp.flags+orgflags;
  732. include(chp.flags,nf_cargswap);
  733. chp.location.loc:=LOC_CREFERENCE;
  734. calcregisters(chp,0,0,0);
  735. chp.resulttype:=htype;
  736. result:=chp;
  737. exit;
  738. end;
  739. end;
  740. { C Arguments are pushed on the stack and
  741. are not accesible after the push }
  742. if not(nf_cargs in flags) then
  743. location.loc:=LOC_CREFERENCE
  744. else
  745. location.loc:=LOC_INVALID;
  746. calcregisters(self,0,0,0);
  747. end;
  748. function tarrayconstructornode.docompare(p: tnode): boolean;
  749. begin
  750. docompare :=
  751. inherited docompare(p);
  752. end;
  753. {*****************************************************************************
  754. TTYPENODE
  755. *****************************************************************************}
  756. constructor ttypenode.create(t : ttype);
  757. begin
  758. inherited create(typen);
  759. restype:=t;
  760. allowed:=false;
  761. end;
  762. function ttypenode.det_resulttype:tnode;
  763. begin
  764. result:=nil;
  765. resulttype:=restype;
  766. { check if it's valid }
  767. if restype.def.deftype = errordef then
  768. CGMessage(cg_e_illegal_expression);
  769. end;
  770. function ttypenode.pass_1 : tnode;
  771. begin
  772. result:=nil;
  773. { a typenode can't generate code, so we give here
  774. an error. Else it'll be an abstract error in pass_2.
  775. Only when the allowed flag is set we don't generate
  776. an error }
  777. if not allowed then
  778. Message(parser_e_no_type_not_allowed_here);
  779. end;
  780. function ttypenode.docompare(p: tnode): boolean;
  781. begin
  782. docompare :=
  783. inherited docompare(p);
  784. end;
  785. {*****************************************************************************
  786. TRTTINODE
  787. *****************************************************************************}
  788. constructor trttinode.create(def:tstoreddef;rt:trttitype);
  789. begin
  790. inherited create(rttin);
  791. rttidef:=def;
  792. rttitype:=rt;
  793. end;
  794. function trttinode.getcopy : tnode;
  795. var
  796. n : trttinode;
  797. begin
  798. n:=trttinode(inherited getcopy);
  799. n.rttidef:=rttidef;
  800. n.rttitype:=rttitype;
  801. result:=n;
  802. end;
  803. function trttinode.det_resulttype:tnode;
  804. begin
  805. { rtti information will be returned as a void pointer }
  806. result:=nil;
  807. resulttype:=voidpointertype;
  808. end;
  809. function trttinode.pass_1 : tnode;
  810. begin
  811. result:=nil;
  812. location.loc:=LOC_CREFERENCE;
  813. end;
  814. function trttinode.docompare(p: tnode): boolean;
  815. begin
  816. docompare :=
  817. inherited docompare(p) and
  818. (rttidef = trttinode(p).rttidef) and
  819. (rttitype = trttinode(p).rttitype);
  820. end;
  821. procedure trttinode.pass_2;
  822. begin
  823. location_reset(location,LOC_CREFERENCE,OS_NO);
  824. location.reference.symbol:=rttidef.get_rtti_label(rttitype);
  825. end;
  826. begin
  827. cloadnode:=tloadnode;
  828. cassignmentnode:=tassignmentnode;
  829. cfuncretnode:=tfuncretnode;
  830. carrayconstructorrangenode:=tarrayconstructorrangenode;
  831. carrayconstructornode:=tarrayconstructornode;
  832. ctypenode:=ttypenode;
  833. crttinode:=trttinode;
  834. end.
  835. {
  836. $Log$
  837. Revision 1.38 2002-04-25 20:16:39 peter
  838. * moved more routines from cga/n386util
  839. Revision 1.37 2002/04/23 19:16:34 peter
  840. * add pinline unit that inserts compiler supported functions using
  841. one or more statements
  842. * moved finalize and setlength from ninl to pinline
  843. Revision 1.36 2002/04/22 16:30:06 peter
  844. * fixed @methodpointer
  845. Revision 1.35 2002/04/21 19:02:04 peter
  846. * removed newn and disposen nodes, the code is now directly
  847. inlined from pexpr
  848. * -an option that will write the secondpass nodes to the .s file, this
  849. requires EXTDEBUG define to actually write the info
  850. * fixed various internal errors and crashes due recent code changes
  851. Revision 1.34 2002/04/02 17:11:29 peter
  852. * tlocation,treference update
  853. * LOC_CONSTANT added for better constant handling
  854. * secondadd splitted in multiple routines
  855. * location_force_reg added for loading a location to a register
  856. of a specified size
  857. * secondassignment parses now first the right and then the left node
  858. (this is compatible with Kylix). This saves a lot of push/pop especially
  859. with string operations
  860. * adapted some routines to use the new cg methods
  861. Revision 1.33 2002/03/31 20:26:34 jonas
  862. + a_loadfpu_* and a_loadmm_* methods in tcg
  863. * register allocation is now handled by a class and is mostly processor
  864. independent (+rgobj.pas and i386/rgcpu.pas)
  865. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  866. * some small improvements and fixes to the optimizer
  867. * some register allocation fixes
  868. * some fpuvaroffset fixes in the unary minus node
  869. * push/popusedregisters is now called rg.save/restoreusedregisters and
  870. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  871. also better optimizable)
  872. * fixed and optimized register saving/restoring for new/dispose nodes
  873. * LOC_FPU locations now also require their "register" field to be set to
  874. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  875. - list field removed of the tnode class because it's not used currently
  876. and can cause hard-to-find bugs
  877. Revision 1.32 2002/01/19 11:52:32 peter
  878. * dynarr:=nil support added
  879. Revision 1.31 2001/12/28 15:02:00 jonas
  880. * fixed web bug 1684 (it already didn't crash anymore, but it also didn't
  881. generate an error) ("merged")
  882. Revision 1.30 2001/11/07 13:52:52 jonas
  883. * only save/restore nf_forcevaria flag when reversing order of
  884. arrayconstructor elements, since the other flags are element specific
  885. Revision 1.29 2001/11/02 22:58:02 peter
  886. * procsym definition rewrite
  887. Revision 1.28 2001/10/31 17:34:20 jonas
  888. * fixed web bug 1651
  889. Revision 1.27 2001/10/28 17:22:25 peter
  890. * allow assignment of overloaded procedures to procvars when we know
  891. which procedure to take
  892. Revision 1.26 2001/10/12 13:51:51 jonas
  893. * fixed internalerror(10) due to previous fpu overflow fixes ("merged")
  894. * fixed bug in n386add (introduced after compilerproc changes for string
  895. operations) where calcregisters wasn't called for shortstring addnodes
  896. * NOTE: from now on, the location of a binary node must now always be set
  897. before you call calcregisters() for it
  898. Revision 1.25 2001/09/02 21:12:07 peter
  899. * move class of definitions into type section for delphi
  900. Revision 1.24 2001/08/30 15:48:34 jonas
  901. * fix from Peter for getting correct symtableentry for funcret loads
  902. Revision 1.23 2001/08/26 13:36:41 florian
  903. * some cg reorganisation
  904. * some PPC updates
  905. Revision 1.22 2001/08/12 22:11:52 peter
  906. * errordef.typesym is not updated anymore
  907. Revision 1.21 2001/08/06 21:40:47 peter
  908. * funcret moved from tprocinfo to tprocdef
  909. Revision 1.20 2001/07/30 20:52:25 peter
  910. * fixed array constructor passing with type conversions
  911. Revision 1.19 2001/06/04 18:07:47 peter
  912. * remove unused typenode for procvar load. Don't know what happened why
  913. this code was not there already with revision 1.17.
  914. Revision 1.18 2001/06/04 11:48:01 peter
  915. * better const to var checking
  916. Revision 1.17 2001/05/19 21:19:57 peter
  917. * remove unused typenode for procvars to prevent error
  918. * typenode.allowed flag to allow a typenode
  919. Revision 1.16 2001/05/09 19:57:51 peter
  920. * typenode doesn't generate code, give error in pass_1 instead of
  921. getting an abstract methode runtime error
  922. Revision 1.15 2001/04/14 14:06:31 peter
  923. * move more code from loadnode.pass_1 to det_resulttype
  924. Revision 1.14 2001/04/13 01:22:10 peter
  925. * symtable change to classes
  926. * range check generation and errors fixed, make cycle DEBUG=1 works
  927. * memory leaks fixed
  928. Revision 1.13 2001/04/05 21:03:08 peter
  929. * array constructor fix
  930. Revision 1.12 2001/04/04 22:42:40 peter
  931. * move constant folding into det_resulttype
  932. Revision 1.11 2001/04/02 21:20:31 peter
  933. * resulttype rewrite
  934. Revision 1.10 2000/12/31 11:14:10 jonas
  935. + implemented/fixed docompare() mathods for all nodes (not tested)
  936. + nopt.pas, nadd.pas, i386/n386opt.pas: optimized nodes for adding strings
  937. and constant strings/chars together
  938. * n386add.pas: don't copy temp strings (of size 256) to another temp string
  939. when adding
  940. Revision 1.9 2000/11/29 00:30:33 florian
  941. * unused units removed from uses clause
  942. * some changes for widestrings
  943. Revision 1.8 2000/11/04 14:25:20 florian
  944. + merged Attila's changes for interfaces, not tested yet
  945. Revision 1.7 2000/10/31 22:02:49 peter
  946. * symtable splitted, no real code changes
  947. Revision 1.6 2000/10/14 10:14:50 peter
  948. * moehrendorf oct 2000 rewrite
  949. Revision 1.5 2000/10/01 19:48:24 peter
  950. * lot of compile updates for cg11
  951. Revision 1.4 2000/09/28 19:49:52 florian
  952. *** empty log message ***
  953. Revision 1.3 2000/09/27 18:14:31 florian
  954. * fixed a lot of syntax errors in the n*.pas stuff
  955. Revision 1.2 2000/09/25 15:37:14 florian
  956. * more fixes
  957. Revision 1.1 2000/09/25 14:55:05 florian
  958. * initial revision
  959. }