nld.pas 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  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. {$ifdef state_tracking}
  24. nstate,
  25. {$endif}
  26. symconst,symbase,symtype,symsym,symdef;
  27. type
  28. tloadnode = class(tunarynode)
  29. symtableentry : tsym;
  30. symtableentryderef : tderef;
  31. symtable : tsymtable;
  32. procdef : tprocdef;
  33. procdefderef : tderef;
  34. constructor create(v : tsym;st : tsymtable);virtual;
  35. constructor create_procvar(v : tsym;d:tprocdef;st : tsymtable);virtual;
  36. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  37. procedure ppuwrite(ppufile:tcompilerppufile);override;
  38. procedure buildderefimpl;override;
  39. procedure derefimpl;override;
  40. procedure set_mp(p:tnode);
  41. function is_addr_param_load:boolean;
  42. function getcopy : tnode;override;
  43. function pass_1 : tnode;override;
  44. function det_resulttype:tnode;override;
  45. procedure mark_write;override;
  46. function docompare(p: tnode): boolean; override;
  47. procedure printnodedata(var t:text);override;
  48. end;
  49. tloadnodeclass = class of tloadnode;
  50. { different assignment types }
  51. tassigntype = (at_normal,at_plus,at_minus,at_star,at_slash);
  52. tassignmentnode = class(tbinarynode)
  53. assigntype : tassigntype;
  54. constructor create(l,r : tnode);virtual;
  55. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  56. procedure ppuwrite(ppufile:tcompilerppufile);override;
  57. function getcopy : tnode;override;
  58. function pass_1 : tnode;override;
  59. function det_resulttype:tnode;override;
  60. {$ifdef state_tracking}
  61. function track_state_pass(exec_known:boolean):boolean;override;
  62. {$endif state_tracking}
  63. function docompare(p: tnode): boolean; override;
  64. end;
  65. tassignmentnodeclass = class of tassignmentnode;
  66. tarrayconstructorrangenode = class(tbinarynode)
  67. constructor create(l,r : tnode);virtual;
  68. function pass_1 : tnode;override;
  69. function det_resulttype:tnode;override;
  70. end;
  71. tarrayconstructorrangenodeclass = class of tarrayconstructorrangenode;
  72. tarrayconstructornode = class(tbinarynode)
  73. constructor create(l,r : tnode);virtual;
  74. function getcopy : tnode;override;
  75. function pass_1 : tnode;override;
  76. function det_resulttype:tnode;override;
  77. function docompare(p: tnode): boolean; override;
  78. procedure force_type(tt:ttype);
  79. procedure insert_typeconvs;
  80. end;
  81. tarrayconstructornodeclass = class of tarrayconstructornode;
  82. ttypenode = class(tnode)
  83. allowed : boolean;
  84. restype : ttype;
  85. constructor create(t : ttype);virtual;
  86. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  87. procedure ppuwrite(ppufile:tcompilerppufile);override;
  88. procedure buildderefimpl;override;
  89. procedure derefimpl;override;
  90. function pass_1 : tnode;override;
  91. function det_resulttype:tnode;override;
  92. function docompare(p: tnode): boolean; override;
  93. end;
  94. ttypenodeclass = class of ttypenode;
  95. trttinode = class(tnode)
  96. l1,l2 : longint;
  97. rttitype : trttitype;
  98. rttidef : tstoreddef;
  99. rttidefderef : tderef;
  100. constructor create(def:tstoreddef;rt:trttitype);virtual;
  101. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  102. procedure ppuwrite(ppufile:tcompilerppufile);override;
  103. procedure buildderefimpl;override;
  104. procedure derefimpl;override;
  105. function getcopy : tnode;override;
  106. function pass_1 : tnode;override;
  107. function det_resulttype:tnode;override;
  108. function docompare(p: tnode): boolean; override;
  109. end;
  110. trttinodeclass = class of trttinode;
  111. var
  112. cloadnode : tloadnodeclass;
  113. cassignmentnode : tassignmentnodeclass;
  114. carrayconstructorrangenode : tarrayconstructorrangenodeclass;
  115. carrayconstructornode : tarrayconstructornodeclass;
  116. ctypenode : ttypenodeclass;
  117. crttinode : trttinodeclass;
  118. implementation
  119. uses
  120. cutils,verbose,globtype,globals,systems,
  121. symnot,
  122. defutil,defcmp,
  123. htypechk,pass_1,procinfo,paramgr,
  124. ncon,ninl,ncnv,nmem,ncal,nutils,
  125. cgobj,cgbase
  126. ;
  127. {*****************************************************************************
  128. TLOADNODE
  129. *****************************************************************************}
  130. constructor tloadnode.create(v : tsym;st : tsymtable);
  131. begin
  132. inherited create(loadn,nil);
  133. if not assigned(v) then
  134. internalerror(200108121);
  135. symtableentry:=v;
  136. symtable:=st;
  137. procdef:=nil;
  138. end;
  139. constructor tloadnode.create_procvar(v : tsym;d:tprocdef;st : tsymtable);
  140. begin
  141. inherited create(loadn,nil);
  142. if not assigned(v) then
  143. internalerror(200108121);
  144. symtableentry:=v;
  145. symtable:=st;
  146. procdef:=d;
  147. end;
  148. constructor tloadnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  149. begin
  150. inherited ppuload(t,ppufile);
  151. ppufile.getderef(symtableentryderef);
  152. symtable:=nil;
  153. ppufile.getderef(procdefderef);
  154. end;
  155. procedure tloadnode.ppuwrite(ppufile:tcompilerppufile);
  156. begin
  157. inherited ppuwrite(ppufile);
  158. ppufile.putderef(symtableentryderef);
  159. ppufile.putderef(procdefderef);
  160. end;
  161. procedure tloadnode.buildderefimpl;
  162. begin
  163. inherited buildderefimpl;
  164. symtableentryderef.build(symtableentry);
  165. procdefderef.build(procdef);
  166. end;
  167. procedure tloadnode.derefimpl;
  168. begin
  169. inherited derefimpl;
  170. symtableentry:=tsym(symtableentryderef.resolve);
  171. symtable:=symtableentry.owner;
  172. procdef:=tprocdef(procdefderef.resolve);
  173. end;
  174. procedure tloadnode.set_mp(p:tnode);
  175. begin
  176. { typen nodes should not be set }
  177. if p.nodetype=typen then
  178. internalerror(200301042);
  179. left:=p;
  180. end;
  181. function tloadnode.getcopy : tnode;
  182. var
  183. n : tloadnode;
  184. begin
  185. n:=tloadnode(inherited getcopy);
  186. n.symtable:=symtable;
  187. n.symtableentry:=symtableentry;
  188. n.procdef:=procdef;
  189. result:=n;
  190. end;
  191. function tloadnode.is_addr_param_load:boolean;
  192. begin
  193. result:=(symtable.symtabletype=parasymtable) and
  194. (symtableentry.typ=paravarsym) and
  195. not(vo_has_local_copy in tparavarsym(symtableentry).varoptions) and
  196. not(nf_load_self_pointer in flags) and
  197. paramanager.push_addr_param(tparavarsym(symtableentry).varspez,tparavarsym(symtableentry).vartype.def,tprocdef(symtable.defowner).proccalloption);
  198. end;
  199. function tloadnode.det_resulttype:tnode;
  200. begin
  201. result:=nil;
  202. case symtableentry.typ of
  203. absolutevarsym :
  204. resulttype:=tabsolutevarsym(symtableentry).vartype;
  205. constsym:
  206. begin
  207. if tconstsym(symtableentry).consttyp=constresourcestring then
  208. begin
  209. {$ifdef ansistring_bits}
  210. case aktansistring_bits of
  211. sb_16:
  212. resulttype:=cansistringtype16;
  213. sb_32:
  214. resulttype:=cansistringtype32;
  215. sb_64:
  216. resulttype:=cansistringtype64;
  217. end;
  218. {$else}
  219. resulttype:=cansistringtype
  220. {$endif}
  221. end
  222. else
  223. internalerror(22799);
  224. end;
  225. globalvarsym,
  226. paravarsym,
  227. localvarsym :
  228. begin
  229. inc(tabstractvarsym(symtableentry).refs);
  230. { Nested variable? The we need to load the framepointer of
  231. the parent procedure }
  232. if assigned(current_procinfo) then
  233. begin
  234. if (symtable.symtabletype in [localsymtable,parasymtable]) and
  235. (symtable.symtablelevel<>current_procinfo.procdef.parast.symtablelevel) then
  236. begin
  237. if assigned(left) then
  238. internalerror(200309289);
  239. left:=cloadparentfpnode.create(tprocdef(symtable.defowner));
  240. { reference in nested procedures, variable needs to be in memory }
  241. make_not_regable(self);
  242. end;
  243. { static variables referenced in procedures or from finalization,
  244. variable needs to be in memory.
  245. It is too hard and the benefit is too small to detect whether a
  246. variable is only used in the finalization to add support for it (PFV) }
  247. if (symtable.symtabletype=staticsymtable) and
  248. (
  249. (symtable.symtablelevel<>current_procinfo.procdef.localst.symtablelevel) or
  250. (current_procinfo.procdef.proctypeoption=potype_unitfinalize)
  251. ) then
  252. make_not_regable(self);
  253. end;
  254. { fix self type which is declared as voidpointer in the
  255. definition }
  256. if vo_is_self in tabstractvarsym(symtableentry).varoptions then
  257. begin
  258. resulttype.setdef(tprocdef(symtableentry.owner.defowner)._class);
  259. if (po_classmethod in tprocdef(symtableentry.owner.defowner).procoptions) or
  260. (po_staticmethod in tprocdef(symtableentry.owner.defowner).procoptions) then
  261. resulttype.setdef(tclassrefdef.create(resulttype))
  262. else if is_object(resulttype.def) and
  263. (nf_load_self_pointer in flags) then
  264. resulttype.setdef(tpointerdef.create(resulttype));
  265. end
  266. else if vo_is_vmt in tabstractvarsym(symtableentry).varoptions then
  267. begin
  268. resulttype.setdef(tprocdef(symtableentry.owner.defowner)._class);
  269. resulttype.setdef(tclassrefdef.create(resulttype));
  270. end
  271. else
  272. resulttype:=tabstractvarsym(symtableentry).vartype;
  273. end;
  274. typedconstsym :
  275. resulttype:=ttypedconstsym(symtableentry).typedconsttype;
  276. procsym :
  277. begin
  278. if not assigned(procdef) then
  279. begin
  280. if Tprocsym(symtableentry).procdef_count>1 then
  281. CGMessage(parser_e_no_overloaded_procvars);
  282. procdef:=tprocsym(symtableentry).first_procdef;
  283. end;
  284. { the result is a procdef, addrn and proc_to_procvar
  285. typeconvn need this as resulttype so they know
  286. that the address needs to be returned }
  287. resulttype.setdef(procdef);
  288. { process methodpointer }
  289. if assigned(left) then
  290. resulttypepass(left);
  291. end;
  292. labelsym:
  293. resulttype:=voidtype;
  294. else
  295. internalerror(200104141);
  296. end;
  297. end;
  298. procedure Tloadnode.mark_write;
  299. begin
  300. include(flags,nf_write);
  301. end;
  302. function tloadnode.pass_1 : tnode;
  303. begin
  304. result:=nil;
  305. expectloc:=LOC_REFERENCE;
  306. registersint:=0;
  307. registersfpu:=0;
  308. {$ifdef SUPPORT_MMX}
  309. registersmmx:=0;
  310. {$endif SUPPORT_MMX}
  311. if (cs_create_pic in aktmoduleswitches) and
  312. not(symtableentry.typ in [paravarsym,localvarsym]) then
  313. include(current_procinfo.flags,pi_needs_got);
  314. case symtableentry.typ of
  315. absolutevarsym :
  316. ;
  317. constsym:
  318. begin
  319. if tconstsym(symtableentry).consttyp=constresourcestring then
  320. expectloc:=LOC_CREFERENCE;
  321. end;
  322. globalvarsym,
  323. localvarsym,
  324. paravarsym :
  325. begin
  326. if assigned(left) then
  327. firstpass(left);
  328. if not is_addr_param_load and
  329. tabstractvarsym(symtableentry).is_regvar then
  330. begin
  331. case tabstractvarsym(symtableentry).varregable of
  332. vr_intreg :
  333. expectloc:=LOC_CREGISTER;
  334. vr_fpureg :
  335. expectloc:=LOC_CFPUREGISTER;
  336. vr_mmreg :
  337. expectloc:=LOC_CMMREGISTER;
  338. end
  339. end
  340. else
  341. if (tabstractvarsym(symtableentry).varspez=vs_const) then
  342. expectloc:=LOC_CREFERENCE;
  343. { we need a register for call by reference parameters }
  344. if paramanager.push_addr_param(tabstractvarsym(symtableentry).varspez,tabstractvarsym(symtableentry).vartype.def,pocall_default) then
  345. registersint:=1;
  346. if ([vo_is_thread_var,vo_is_dll_var]*tabstractvarsym(symtableentry).varoptions)<>[] then
  347. registersint:=1;
  348. if (target_info.system=system_powerpc_darwin) and (vo_is_dll_var in tabstractvarsym(symtableentry).varoptions) then
  349. include(current_procinfo.flags,pi_needs_got);
  350. { call to get address of threadvar }
  351. if (vo_is_thread_var in tabstractvarsym(symtableentry).varoptions) then
  352. include(current_procinfo.flags,pi_do_call);
  353. if nf_write in flags then
  354. Tabstractvarsym(symtableentry).trigger_notifications(vn_onwrite)
  355. else
  356. Tabstractvarsym(symtableentry).trigger_notifications(vn_onread);
  357. { count variable references }
  358. if cg.t_times>1 then
  359. inc(tabstractvarsym(symtableentry).refs,cg.t_times-1);
  360. end;
  361. typedconstsym :
  362. ;
  363. procsym :
  364. begin
  365. { method pointer ? }
  366. if assigned(left) then
  367. begin
  368. expectloc:=LOC_CREFERENCE;
  369. firstpass(left);
  370. registersint:=max(registersint,left.registersint);
  371. registersfpu:=max(registersfpu,left.registersfpu);
  372. {$ifdef SUPPORT_MMX}
  373. registersmmx:=max(registersmmx,left.registersmmx);
  374. {$endif SUPPORT_MMX}
  375. end;
  376. end;
  377. labelsym :
  378. ;
  379. else
  380. internalerror(200104143);
  381. end;
  382. end;
  383. function tloadnode.docompare(p: tnode): boolean;
  384. begin
  385. docompare :=
  386. inherited docompare(p) and
  387. (symtableentry = tloadnode(p).symtableentry) and
  388. (procdef = tloadnode(p).procdef) and
  389. (symtable = tloadnode(p).symtable);
  390. end;
  391. procedure Tloadnode.printnodedata(var t:text);
  392. begin
  393. inherited printnodedata(t);
  394. write(t,printnodeindention,'symbol = ',symtableentry.name);
  395. if symtableentry.typ=procsym then
  396. write(t,printnodeindention,'procdef = ',procdef.mangledname);
  397. writeln(t,'');
  398. end;
  399. {*****************************************************************************
  400. TASSIGNMENTNODE
  401. *****************************************************************************}
  402. constructor tassignmentnode.create(l,r : tnode);
  403. begin
  404. inherited create(assignn,l,r);
  405. l.mark_write;
  406. assigntype:=at_normal;
  407. end;
  408. constructor tassignmentnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  409. begin
  410. inherited ppuload(t,ppufile);
  411. assigntype:=tassigntype(ppufile.getbyte);
  412. end;
  413. procedure tassignmentnode.ppuwrite(ppufile:tcompilerppufile);
  414. begin
  415. inherited ppuwrite(ppufile);
  416. ppufile.putbyte(byte(assigntype));
  417. end;
  418. function tassignmentnode.getcopy : tnode;
  419. var
  420. n : tassignmentnode;
  421. begin
  422. n:=tassignmentnode(inherited getcopy);
  423. n.assigntype:=assigntype;
  424. getcopy:=n;
  425. end;
  426. function tassignmentnode.det_resulttype:tnode;
  427. var
  428. hp : tnode;
  429. useshelper : boolean;
  430. original_size : longint;
  431. begin
  432. result:=nil;
  433. resulttype:=voidtype;
  434. original_size := 0;
  435. { must be made unique }
  436. set_unique(left);
  437. resulttypepass(left);
  438. if is_ansistring(left.resulttype.def) then
  439. begin
  440. { fold <ansistring>:=<ansistring>+<char|shortstring|ansistring> }
  441. if (right.nodetype=addn) and
  442. left.isequal(tbinarynode(right).left) and
  443. { don't fold multiple concatenations else we could get trouble
  444. with multiple uses of s
  445. }
  446. (tbinarynode(right).left.nodetype<>addn) and
  447. (tbinarynode(right).right.nodetype<>addn) then
  448. begin
  449. { don't do a resulttypepass(right), since then the addnode }
  450. { may insert typeconversions that make this optimization }
  451. { opportunity quite difficult to detect (JM) }
  452. resulttypepass(tbinarynode(right).left);
  453. resulttypepass(tbinarynode(right).right);
  454. if (is_char(tbinarynode(right).right.resulttype.def) or
  455. is_shortstring(tbinarynode(right).right.resulttype.def) or
  456. is_ansistring(tbinarynode(right).right.resulttype.def)) then
  457. begin
  458. { remove property flag so it'll not trigger an error }
  459. exclude(left.flags,nf_isproperty);
  460. { generate call to helper }
  461. hp:=ccallparanode.create(tbinarynode(right).right,
  462. ccallparanode.create(left,nil));
  463. if is_char(tbinarynode(right).right.resulttype.def) then
  464. result:=ccallnode.createintern('fpc_'+Tstringdef(left.resulttype.def).stringtypname+'_append_char',hp)
  465. else if is_shortstring(tbinarynode(right).right.resulttype.def) then
  466. result:=ccallnode.createintern('fpc_'+Tstringdef(left.resulttype.def).stringtypname+'_append_shortstring',hp)
  467. else
  468. result:=ccallnode.createintern('fpc_'+Tstringdef(left.resulttype.def).stringtypname+'_append_ansistring',hp);
  469. tbinarynode(right).right:=nil;
  470. left:=nil;
  471. exit;
  472. end;
  473. end;
  474. end
  475. else
  476. if is_shortstring(left.resulttype.def) then
  477. begin
  478. { fold <shortstring>:=<shortstring>+<shortstring>,
  479. <shortstring>+<char> is handled by an optimized node }
  480. if (right.nodetype=addn) and
  481. left.isequal(tbinarynode(right).left) and
  482. { don't fold multiple concatenations else we could get trouble
  483. with multiple uses of s }
  484. (tbinarynode(right).left.nodetype<>addn) and
  485. (tbinarynode(right).right.nodetype<>addn) then
  486. begin
  487. { don't do a resulttypepass(right), since then the addnode }
  488. { may insert typeconversions that make this optimization }
  489. { opportunity quite difficult to detect (JM) }
  490. resulttypepass(tbinarynode(right).left);
  491. resulttypepass(tbinarynode(right).right);
  492. if is_shortstring(tbinarynode(right).right.resulttype.def) then
  493. begin
  494. { remove property flag so it'll not trigger an error }
  495. exclude(left.flags,nf_isproperty);
  496. { generate call to helper }
  497. hp:=ccallparanode.create(tbinarynode(right).right,
  498. ccallparanode.create(left,nil));
  499. if is_shortstring(tbinarynode(right).right.resulttype.def) then
  500. result:=ccallnode.createintern('fpc_shortstr_append_shortstr',hp);
  501. tbinarynode(right).right:=nil;
  502. left:=nil;
  503. exit;
  504. end;
  505. end;
  506. end;
  507. resulttypepass(right);
  508. set_varstate(left,vs_assigned,false);
  509. set_varstate(right,vs_used,true);
  510. if codegenerror then
  511. exit;
  512. { tp procvar support, when we don't expect a procvar
  513. then we need to call the procvar }
  514. if (left.resulttype.def.deftype<>procvardef) then
  515. maybe_call_procvar(right,true);
  516. { assignments to formaldefs and open arrays aren't allowed }
  517. if (left.resulttype.def.deftype=formaldef) or
  518. is_open_array(left.resulttype.def) then
  519. CGMessage(type_e_operator_not_allowed);
  520. { test if node can be assigned, properties are allowed }
  521. valid_for_assignment(left);
  522. { assigning nil to a dynamic array clears the array }
  523. if is_dynamic_array(left.resulttype.def) and
  524. (right.nodetype=niln) then
  525. begin
  526. hp:=ccallparanode.create(caddrnode.create_internal
  527. (crttinode.create(tstoreddef(left.resulttype.def),initrtti)),
  528. ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil));
  529. result := ccallnode.createintern('fpc_dynarray_clear',hp);
  530. left:=nil;
  531. exit;
  532. end;
  533. { shortstring helpers can do the conversion directly,
  534. so treat them separatly }
  535. if (is_shortstring(left.resulttype.def)) then
  536. begin
  537. { insert typeconv, except for chars that are handled in
  538. secondpass and except for ansi/wide string that can
  539. be converted immediatly }
  540. if not(is_char(right.resulttype.def) or
  541. (right.resulttype.def.deftype=stringdef)) then
  542. inserttypeconv(right,left.resulttype);
  543. if right.resulttype.def.deftype=stringdef then
  544. begin
  545. useshelper:=true;
  546. { convert constant strings to shortstrings. But
  547. skip empty constant strings, that will be handled
  548. in secondpass }
  549. if (right.nodetype=stringconstn) then
  550. begin
  551. { verify if range fits within shortstring }
  552. { just emit a warning, delphi gives an }
  553. { error, only if the type definition of }
  554. { of the string is less < 255 characters }
  555. if not is_open_string(left.resulttype.def) and
  556. (tstringconstnode(right).len > tstringdef(left.resulttype.def).len) then
  557. cgmessage(type_w_string_too_long);
  558. inserttypeconv(right,left.resulttype);
  559. if (tstringconstnode(right).len=0) then
  560. useshelper:=false;
  561. end;
  562. { rest is done in pass 1 (JM) }
  563. if useshelper then
  564. exit;
  565. end
  566. end
  567. else
  568. begin
  569. { get the size before the type conversion - check for all nodes }
  570. if assigned(right.resulttype.def) and
  571. (right.resulttype.def.deftype in [enumdef,orddef,floatdef]) and
  572. (right.nodetype in [loadn,vecn,calln]) then
  573. original_size := right.resulttype.def.size;
  574. inserttypeconv(right,left.resulttype);
  575. end;
  576. { check if the assignment may cause a range check error }
  577. { if its not explicit, and only if the values are }
  578. { ordinals, enumdef and floatdef }
  579. if (right.nodetype = typeconvn) and
  580. not (nf_explicit in ttypeconvnode(right).flags) then
  581. begin
  582. if assigned(left.resulttype.def) and
  583. (left.resulttype.def.deftype in [enumdef,orddef,floatdef]) and
  584. not is_boolean(left.resulttype.def) then
  585. begin
  586. if (original_size <> 0) and
  587. (left.resulttype.def.size < original_size) then
  588. begin
  589. if (cs_check_range in aktlocalswitches) then
  590. Message(type_w_smaller_possible_range_check)
  591. else
  592. Message(type_h_smaller_possible_range_check);
  593. end;
  594. end;
  595. end;
  596. { call helpers for interface }
  597. if is_interfacecom(left.resulttype.def) then
  598. begin
  599. hp:=ccallparanode.create(ctypeconvnode.create_internal
  600. (right,voidpointertype),
  601. ccallparanode.create(ctypeconvnode.create_internal
  602. (left,voidpointertype),nil));
  603. result:=ccallnode.createintern('fpc_intf_assign',hp);
  604. left:=nil;
  605. right:=nil;
  606. exit;
  607. end;
  608. { check if local proc/func is assigned to procvar }
  609. if right.resulttype.def.deftype=procvardef then
  610. test_local_to_procvar(tprocvardef(right.resulttype.def),left.resulttype.def);
  611. end;
  612. function tassignmentnode.pass_1 : tnode;
  613. var
  614. hp: tnode;
  615. begin
  616. result:=nil;
  617. expectloc:=LOC_VOID;
  618. firstpass(left);
  619. firstpass(right);
  620. { assignment to refcounted variable -> inc/decref }
  621. if (not is_class(left.resulttype.def) and
  622. left.resulttype.def.needs_inittable) then
  623. include(current_procinfo.flags,pi_do_call);
  624. if codegenerror then
  625. exit;
  626. if (is_shortstring(left.resulttype.def)) then
  627. begin
  628. if right.resulttype.def.deftype=stringdef then
  629. begin
  630. if (right.nodetype<>stringconstn) or
  631. (tstringconstnode(right).len<>0) then
  632. begin
  633. if (cs_optimize in aktglobalswitches) and
  634. (right.nodetype in [calln,blockn]) and
  635. (left.nodetype = temprefn) and
  636. is_shortstring(right.resulttype.def) and
  637. not is_open_string(left.resulttype.def) and
  638. (tstringdef(left.resulttype.def).len = 255) then
  639. begin
  640. { the blocknode case is handled in pass_2 at the temp }
  641. { reference level (mainly for callparatemp) (JM) }
  642. if (right.nodetype = calln) then
  643. begin
  644. tcallnode(right).funcretnode := left;
  645. result := right;
  646. end
  647. else
  648. exit;
  649. end
  650. else
  651. begin
  652. hp:=ccallparanode.create
  653. (right,
  654. ccallparanode.create(cinlinenode.create
  655. (in_high_x,false,left.getcopy),nil));
  656. result:=ccallnode.createinternreturn('fpc_'+tstringdef(right.resulttype.def).stringtypname+'_to_shortstr',hp,left);
  657. firstpass(result);
  658. end;
  659. left:=nil;
  660. right:=nil;
  661. exit;
  662. end;
  663. end;
  664. end;
  665. if (cs_optimize in aktglobalswitches) and
  666. (right.nodetype = calln) and
  667. { left must be a temp, since otherwise as soon as you modify the }
  668. { result, the current left node is modified and that one may }
  669. { still be an argument to the function or even accessed in the }
  670. { function }
  671. (((left.nodetype = temprefn) and
  672. paramanager.ret_in_param(right.resulttype.def,
  673. tcallnode(right).procdefinition.proccalloption)) or
  674. { there's special support for ansi/widestrings in the callnode }
  675. is_ansistring(right.resulttype.def) or
  676. is_widestring(right.resulttype.def)) then
  677. begin
  678. tcallnode(right).funcretnode := left;
  679. result := right;
  680. left := nil;
  681. right := nil;
  682. exit;
  683. end;
  684. registersint:=left.registersint+right.registersint;
  685. registersfpu:=max(left.registersfpu,right.registersfpu);
  686. {$ifdef SUPPORT_MMX}
  687. registersmmx:=max(left.registersmmx,right.registersmmx);
  688. {$endif SUPPORT_MMX}
  689. end;
  690. function tassignmentnode.docompare(p: tnode): boolean;
  691. begin
  692. docompare :=
  693. inherited docompare(p) and
  694. (assigntype = tassignmentnode(p).assigntype);
  695. end;
  696. {$ifdef state_tracking}
  697. function Tassignmentnode.track_state_pass(exec_known:boolean):boolean;
  698. var se:Tstate_entry;
  699. begin
  700. track_state_pass:=false;
  701. if exec_known then
  702. begin
  703. track_state_pass:=right.track_state_pass(exec_known);
  704. {Force a new resulttype pass.}
  705. right.resulttype.def:=nil;
  706. do_resulttypepass(right);
  707. resulttypepass(right);
  708. aktstate.store_fact(left.getcopy,right.getcopy);
  709. end
  710. else
  711. aktstate.delete_fact(left);
  712. end;
  713. {$endif}
  714. {*****************************************************************************
  715. TARRAYCONSTRUCTORRANGENODE
  716. *****************************************************************************}
  717. constructor tarrayconstructorrangenode.create(l,r : tnode);
  718. begin
  719. inherited create(arrayconstructorrangen,l,r);
  720. end;
  721. function tarrayconstructorrangenode.det_resulttype:tnode;
  722. begin
  723. result:=nil;
  724. resulttypepass(left);
  725. resulttypepass(right);
  726. set_varstate(left,vs_used,true);
  727. set_varstate(right,vs_used,true);
  728. if codegenerror then
  729. exit;
  730. resulttype:=left.resulttype;
  731. end;
  732. function tarrayconstructorrangenode.pass_1 : tnode;
  733. begin
  734. firstpass(left);
  735. firstpass(right);
  736. expectloc:=LOC_CREFERENCE;
  737. calcregisters(self,0,0,0);
  738. result:=nil;
  739. end;
  740. {****************************************************************************
  741. TARRAYCONSTRUCTORNODE
  742. *****************************************************************************}
  743. constructor tarrayconstructornode.create(l,r : tnode);
  744. begin
  745. inherited create(arrayconstructorn,l,r);
  746. end;
  747. function tarrayconstructornode.getcopy : tnode;
  748. var
  749. n : tarrayconstructornode;
  750. begin
  751. n:=tarrayconstructornode(inherited getcopy);
  752. result:=n;
  753. end;
  754. function tarrayconstructornode.det_resulttype:tnode;
  755. var
  756. htype : ttype;
  757. hp : tarrayconstructornode;
  758. len : longint;
  759. varia : boolean;
  760. begin
  761. result:=nil;
  762. { are we allowing array constructor? Then convert it to a set }
  763. if not allow_array_constructor then
  764. begin
  765. hp:=tarrayconstructornode(getcopy);
  766. arrayconstructor_to_set(tnode(hp));
  767. result:=hp;
  768. exit;
  769. end;
  770. { only pass left tree, right tree contains next construct if any }
  771. htype.reset;
  772. len:=0;
  773. varia:=false;
  774. if assigned(left) then
  775. begin
  776. hp:=self;
  777. while assigned(hp) do
  778. begin
  779. resulttypepass(hp.left);
  780. set_varstate(hp.left,vs_used,true);
  781. if (htype.def=nil) then
  782. htype:=hp.left.resulttype
  783. else
  784. begin
  785. if ((nf_novariaallowed in flags) or (not varia)) and
  786. (not equal_defs(htype.def,hp.left.resulttype.def)) then
  787. begin
  788. varia:=true;
  789. end;
  790. end;
  791. inc(len);
  792. hp:=tarrayconstructornode(hp.right);
  793. end;
  794. end;
  795. { Set the type of empty or varia arrays to void. Also
  796. do this if the type is array of const/open array
  797. because those can't be used with setelementtype }
  798. if not assigned(htype.def) or
  799. varia or
  800. is_array_of_const(htype.def) or
  801. is_open_array(htype.def) then
  802. htype:=voidtype;
  803. resulttype.setdef(tarraydef.create(0,len-1,s32inttype));
  804. tarraydef(resulttype.def).setelementtype(htype);
  805. tarraydef(resulttype.def).IsConstructor:=true;
  806. tarraydef(resulttype.def).IsVariant:=varia;
  807. end;
  808. procedure tarrayconstructornode.force_type(tt:ttype);
  809. var
  810. hp : tarrayconstructornode;
  811. begin
  812. tarraydef(resulttype.def).setelementtype(tt);
  813. tarraydef(resulttype.def).IsConstructor:=true;
  814. tarraydef(resulttype.def).IsVariant:=false;
  815. if assigned(left) then
  816. begin
  817. hp:=self;
  818. while assigned(hp) do
  819. begin
  820. inserttypeconv(hp.left,tt);
  821. hp:=tarrayconstructornode(hp.right);
  822. end;
  823. end;
  824. end;
  825. procedure tarrayconstructornode.insert_typeconvs;
  826. var
  827. hp : tarrayconstructornode;
  828. dovariant : boolean;
  829. begin
  830. dovariant:=(nf_forcevaria in flags) or tarraydef(resulttype.def).isvariant;
  831. { only pass left tree, right tree contains next construct if any }
  832. if assigned(left) then
  833. begin
  834. hp:=self;
  835. while assigned(hp) do
  836. begin
  837. resulttypepass(hp.left);
  838. { Insert typeconvs for array of const }
  839. if dovariant then
  840. begin
  841. case hp.left.resulttype.def.deftype of
  842. enumdef :
  843. hp.left:=ctypeconvnode.create_internal(hp.left,s32inttype);
  844. arraydef :
  845. begin
  846. if is_chararray(hp.left.resulttype.def) then
  847. hp.left:=ctypeconvnode.create_internal(hp.left,charpointertype)
  848. else
  849. if is_widechararray(hp.left.resulttype.def) then
  850. hp.left:=ctypeconvnode.create_internal(hp.left,widecharpointertype)
  851. else
  852. CGMessagePos1(hp.left.fileinfo,type_e_wrong_type_in_array_constructor,hp.left.resulttype.def.typename);
  853. end;
  854. orddef :
  855. begin
  856. if is_integer(hp.left.resulttype.def) and
  857. not(is_64bitint(hp.left.resulttype.def)) then
  858. hp.left:=ctypeconvnode.create(hp.left,s32inttype);
  859. end;
  860. floatdef :
  861. hp.left:=ctypeconvnode.create(hp.left,pbestrealtype^);
  862. procvardef :
  863. hp.left:=ctypeconvnode.create(hp.left,voidpointertype);
  864. stringdef,
  865. variantdef,
  866. pointerdef,
  867. classrefdef,
  868. objectdef : ;
  869. else
  870. CGMessagePos1(hp.left.fileinfo,type_e_wrong_type_in_array_constructor,hp.left.resulttype.def.typename);
  871. end;
  872. end;
  873. resulttypepass(hp.left);
  874. hp:=tarrayconstructornode(hp.right);
  875. end;
  876. end;
  877. end;
  878. function tarrayconstructornode.pass_1 : tnode;
  879. var
  880. hp : tarrayconstructornode;
  881. do_variant:boolean;
  882. begin
  883. do_variant:=(nf_forcevaria in flags) or tarraydef(resulttype.def).isvariant;
  884. result:=nil;
  885. { Insert required type convs, this must be
  886. done in pass 1, because the call must be
  887. resulttypepassed already }
  888. if assigned(left) then
  889. begin
  890. insert_typeconvs;
  891. { call firstpass for all nodes }
  892. hp:=self;
  893. while assigned(hp) do
  894. begin
  895. if hp.left<>nil then
  896. begin
  897. {This check is pessimistic; a call will happen depending
  898. on the location in which the elements will be found in
  899. pass 2.}
  900. if not do_variant then
  901. include(current_procinfo.flags,pi_do_call);
  902. firstpass(hp.left);
  903. end;
  904. hp:=tarrayconstructornode(hp.right);
  905. end;
  906. end;
  907. expectloc:=LOC_CREFERENCE;
  908. calcregisters(self,0,0,0);
  909. end;
  910. function tarrayconstructornode.docompare(p: tnode): boolean;
  911. begin
  912. docompare:=inherited docompare(p);
  913. end;
  914. {*****************************************************************************
  915. TTYPENODE
  916. *****************************************************************************}
  917. constructor ttypenode.create(t : ttype);
  918. begin
  919. inherited create(typen);
  920. restype:=t;
  921. allowed:=false;
  922. end;
  923. constructor ttypenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  924. begin
  925. inherited ppuload(t,ppufile);
  926. ppufile.gettype(restype);
  927. allowed:=boolean(ppufile.getbyte);
  928. end;
  929. procedure ttypenode.ppuwrite(ppufile:tcompilerppufile);
  930. begin
  931. inherited ppuwrite(ppufile);
  932. ppufile.puttype(restype);
  933. ppufile.putbyte(byte(allowed));
  934. end;
  935. procedure ttypenode.buildderefimpl;
  936. begin
  937. inherited buildderefimpl;
  938. restype.buildderef;
  939. end;
  940. procedure ttypenode.derefimpl;
  941. begin
  942. inherited derefimpl;
  943. restype.resolve;
  944. end;
  945. function ttypenode.det_resulttype:tnode;
  946. begin
  947. result:=nil;
  948. resulttype:=restype;
  949. { check if it's valid }
  950. if restype.def.deftype = errordef then
  951. CGMessage(parser_e_illegal_expression);
  952. end;
  953. function ttypenode.pass_1 : tnode;
  954. begin
  955. result:=nil;
  956. expectloc:=LOC_VOID;
  957. { a typenode can't generate code, so we give here
  958. an error. Else it'll be an abstract error in pass_2.
  959. Only when the allowed flag is set we don't generate
  960. an error }
  961. if not allowed then
  962. Message(parser_e_no_type_not_allowed_here);
  963. end;
  964. function ttypenode.docompare(p: tnode): boolean;
  965. begin
  966. docompare :=
  967. inherited docompare(p);
  968. end;
  969. {*****************************************************************************
  970. TRTTINODE
  971. *****************************************************************************}
  972. constructor trttinode.create(def:tstoreddef;rt:trttitype);
  973. begin
  974. inherited create(rttin);
  975. rttidef:=def;
  976. rttitype:=rt;
  977. end;
  978. constructor trttinode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  979. begin
  980. inherited ppuload(t,ppufile);
  981. ppufile.getderef(rttidefderef);
  982. rttitype:=trttitype(ppufile.getbyte);
  983. end;
  984. procedure trttinode.ppuwrite(ppufile:tcompilerppufile);
  985. begin
  986. inherited ppuwrite(ppufile);
  987. ppufile.putderef(rttidefderef);
  988. ppufile.putbyte(byte(rttitype));
  989. end;
  990. procedure trttinode.buildderefimpl;
  991. begin
  992. inherited buildderefimpl;
  993. rttidefderef.build(rttidef);
  994. end;
  995. procedure trttinode.derefimpl;
  996. begin
  997. inherited derefimpl;
  998. rttidef:=tstoreddef(rttidefderef.resolve);
  999. end;
  1000. function trttinode.getcopy : tnode;
  1001. var
  1002. n : trttinode;
  1003. begin
  1004. n:=trttinode(inherited getcopy);
  1005. n.rttidef:=rttidef;
  1006. n.rttitype:=rttitype;
  1007. result:=n;
  1008. end;
  1009. function trttinode.det_resulttype:tnode;
  1010. begin
  1011. { rtti information will be returned as a void pointer }
  1012. result:=nil;
  1013. resulttype:=voidpointertype;
  1014. end;
  1015. function trttinode.pass_1 : tnode;
  1016. begin
  1017. result:=nil;
  1018. expectloc:=LOC_CREFERENCE;
  1019. end;
  1020. function trttinode.docompare(p: tnode): boolean;
  1021. begin
  1022. docompare :=
  1023. inherited docompare(p) and
  1024. (rttidef = trttinode(p).rttidef) and
  1025. (rttitype = trttinode(p).rttitype);
  1026. end;
  1027. begin
  1028. cloadnode:=tloadnode;
  1029. cassignmentnode:=tassignmentnode;
  1030. carrayconstructorrangenode:=tarrayconstructorrangenode;
  1031. carrayconstructornode:=tarrayconstructornode;
  1032. ctypenode:=ttypenode;
  1033. crttinode:=trttinode;
  1034. end.
  1035. {
  1036. $Log$
  1037. Revision 1.146 2005-03-18 16:41:27 peter
  1038. don't check size differences for booleans
  1039. Revision 1.145 2005/02/14 17:13:06 peter
  1040. * truncate log
  1041. Revision 1.144 2005/02/11 16:25:26 peter
  1042. * fix IE with array of const in array of conts
  1043. Revision 1.143 2005/01/23 17:14:21 florian
  1044. + optimized code generation on sparc
  1045. + some stuff for pic code on sparc added
  1046. }