nld.pas 42 KB

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