1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195 |
- {
- $Id$
- Copyright (c) 2000-2002 by Florian Klaempfl
- Type checking and register allocation for load/assignment nodes
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- ****************************************************************************
- }
- unit nld;
- {$i fpcdefs.inc}
- interface
- uses
- node,
- {$ifdef state_tracking}
- nstate,
- {$endif}
- symconst,symbase,symtype,symsym,symdef;
- type
- tloadnode = class(tunarynode)
- symtableentry : tsym;
- symtableentryderef : tderef;
- symtable : tsymtable;
- procdef : tprocdef;
- procdefderef : tderef;
- constructor create(v : tsym;st : tsymtable);virtual;
- constructor create_procvar(v : tsym;d:tprocdef;st : tsymtable);virtual;
- constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
- procedure ppuwrite(ppufile:tcompilerppufile);override;
- procedure buildderefimpl;override;
- procedure derefimpl;override;
- procedure set_mp(p:tnode);
- function is_addr_param_load:boolean;
- function getcopy : tnode;override;
- function pass_1 : tnode;override;
- function det_resulttype:tnode;override;
- procedure mark_write;override;
- function docompare(p: tnode): boolean; override;
- procedure printnodedata(var t:text);override;
- end;
- tloadnodeclass = class of tloadnode;
- { different assignment types }
- tassigntype = (at_normal,at_plus,at_minus,at_star,at_slash);
- tassignmentnode = class(tbinarynode)
- assigntype : tassigntype;
- constructor create(l,r : tnode);virtual;
- constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
- procedure ppuwrite(ppufile:tcompilerppufile);override;
- function getcopy : tnode;override;
- function pass_1 : tnode;override;
- function det_resulttype:tnode;override;
- {$ifdef state_tracking}
- function track_state_pass(exec_known:boolean):boolean;override;
- {$endif state_tracking}
- function docompare(p: tnode): boolean; override;
- end;
- tassignmentnodeclass = class of tassignmentnode;
- tarrayconstructorrangenode = class(tbinarynode)
- constructor create(l,r : tnode);virtual;
- function pass_1 : tnode;override;
- function det_resulttype:tnode;override;
- end;
- tarrayconstructorrangenodeclass = class of tarrayconstructorrangenode;
- tarrayconstructornode = class(tbinarynode)
- constructor create(l,r : tnode);virtual;
- function getcopy : tnode;override;
- function pass_1 : tnode;override;
- function det_resulttype:tnode;override;
- function docompare(p: tnode): boolean; override;
- procedure force_type(tt:ttype);
- procedure insert_typeconvs;
- end;
- tarrayconstructornodeclass = class of tarrayconstructornode;
- ttypenode = class(tnode)
- allowed : boolean;
- restype : ttype;
- constructor create(t : ttype);virtual;
- constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
- procedure ppuwrite(ppufile:tcompilerppufile);override;
- procedure buildderefimpl;override;
- procedure derefimpl;override;
- function pass_1 : tnode;override;
- function det_resulttype:tnode;override;
- function docompare(p: tnode): boolean; override;
- end;
- ttypenodeclass = class of ttypenode;
- trttinode = class(tnode)
- l1,l2 : longint;
- rttitype : trttitype;
- rttidef : tstoreddef;
- rttidefderef : tderef;
- constructor create(def:tstoreddef;rt:trttitype);virtual;
- constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
- procedure ppuwrite(ppufile:tcompilerppufile);override;
- procedure buildderefimpl;override;
- procedure derefimpl;override;
- function getcopy : tnode;override;
- function pass_1 : tnode;override;
- procedure pass_2;override;
- function det_resulttype:tnode;override;
- function docompare(p: tnode): boolean; override;
- end;
- trttinodeclass = class of trttinode;
- var
- cloadnode : tloadnodeclass;
- cassignmentnode : tassignmentnodeclass;
- carrayconstructorrangenode : tarrayconstructorrangenodeclass;
- carrayconstructornode : tarrayconstructornodeclass;
- ctypenode : ttypenodeclass;
- crttinode : trttinodeclass;
- implementation
- uses
- cutils,verbose,globtype,globals,systems,
- symnot,
- defutil,defcmp,
- htypechk,pass_1,procinfo,paramgr,
- ncon,ninl,ncnv,nmem,ncal,nutils,
- cgobj,cgbase
- ;
- {*****************************************************************************
- TLOADNODE
- *****************************************************************************}
- constructor tloadnode.create(v : tsym;st : tsymtable);
- begin
- inherited create(loadn,nil);
- if not assigned(v) then
- internalerror(200108121);
- symtableentry:=v;
- symtable:=st;
- procdef:=nil;
- end;
- constructor tloadnode.create_procvar(v : tsym;d:tprocdef;st : tsymtable);
- begin
- inherited create(loadn,nil);
- if not assigned(v) then
- internalerror(200108121);
- symtableentry:=v;
- symtable:=st;
- procdef:=d;
- end;
- constructor tloadnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
- begin
- inherited ppuload(t,ppufile);
- ppufile.getderef(symtableentryderef);
- symtable:=nil;
- ppufile.getderef(procdefderef);
- end;
- procedure tloadnode.ppuwrite(ppufile:tcompilerppufile);
- begin
- inherited ppuwrite(ppufile);
- ppufile.putderef(symtableentryderef);
- ppufile.putderef(procdefderef);
- end;
- procedure tloadnode.buildderefimpl;
- begin
- inherited buildderefimpl;
- symtableentryderef.build(symtableentry);
- procdefderef.build(procdef);
- end;
- procedure tloadnode.derefimpl;
- begin
- inherited derefimpl;
- symtableentry:=tsym(symtableentryderef.resolve);
- symtable:=symtableentry.owner;
- procdef:=tprocdef(procdefderef.resolve);
- end;
- procedure tloadnode.set_mp(p:tnode);
- begin
- { typen nodes should not be set }
- if p.nodetype=typen then
- internalerror(200301042);
- left:=p;
- end;
- function tloadnode.getcopy : tnode;
- var
- n : tloadnode;
- begin
- n:=tloadnode(inherited getcopy);
- n.symtable:=symtable;
- n.symtableentry:=symtableentry;
- n.procdef:=procdef;
- result:=n;
- end;
- function tloadnode.is_addr_param_load:boolean;
- begin
- result:=(symtable.symtabletype=parasymtable) and
- (symtableentry.typ=varsym) and
- not(vo_has_local_copy in tvarsym(symtableentry).varoptions) and
- not(nf_load_self_pointer in flags) and
- paramanager.push_addr_param(tvarsym(symtableentry).varspez,tvarsym(symtableentry).vartype.def,tprocdef(symtable.defowner).proccalloption);
- end;
- function tloadnode.det_resulttype:tnode;
- begin
- result:=nil;
- case symtableentry.typ of
- absolutesym :
- resulttype:=tabsolutesym(symtableentry).vartype;
- constsym:
- begin
- if tconstsym(symtableentry).consttyp=constresourcestring then
- begin
- {$ifdef ansistring_bits}
- case aktansistring_bits of
- sb_16:
- resulttype:=cansistringtype16;
- sb_32:
- resulttype:=cansistringtype32;
- sb_64:
- resulttype:=cansistringtype64;
- end;
- {$else}
- resulttype:=cansistringtype
- {$endif}
- end
- else
- internalerror(22799);
- end;
- varsym :
- begin
- inc(tvarsym(symtableentry).refs);
- { Nested variable? The we need to load the framepointer of
- the parent procedure }
- if (symtable.symtabletype in [localsymtable,parasymtable]) and
- (symtable.symtablelevel<>current_procinfo.procdef.parast.symtablelevel) then
- begin
- if assigned(left) then
- internalerror(200309289);
- left:=cloadparentfpnode.create(tprocdef(symtable.defowner));
- { reference in nested procedures, variable needs to be in memory }
- make_not_regable(self);
- end;
- { static variables referenced in procedures, variable needs to be in memory }
- if (symtable.symtabletype=staticsymtable) and
- (symtable.symtablelevel<>current_procinfo.procdef.parast.symtablelevel) then
- make_not_regable(self);
- { fix self type which is declared as voidpointer in the
- definition }
- if vo_is_self in tvarsym(symtableentry).varoptions then
- begin
- resulttype.setdef(tprocdef(symtableentry.owner.defowner)._class);
- if (po_classmethod in tprocdef(symtableentry.owner.defowner).procoptions) or
- (po_staticmethod in tprocdef(symtableentry.owner.defowner).procoptions) then
- resulttype.setdef(tclassrefdef.create(resulttype))
- else if is_object(resulttype.def) and
- (nf_load_self_pointer in flags) then
- resulttype.setdef(tpointerdef.create(resulttype));
- end
- else if vo_is_vmt in tvarsym(symtableentry).varoptions then
- begin
- resulttype.setdef(tprocdef(symtableentry.owner.defowner)._class);
- resulttype.setdef(tclassrefdef.create(resulttype));
- end
- else
- resulttype:=tvarsym(symtableentry).vartype;
- end;
- typedconstsym :
- resulttype:=ttypedconstsym(symtableentry).typedconsttype;
- procsym :
- begin
- if not assigned(procdef) then
- begin
- if Tprocsym(symtableentry).procdef_count>1 then
- CGMessage(parser_e_no_overloaded_procvars);
- procdef:=tprocsym(symtableentry).first_procdef;
- end;
- { the result is a procdef, addrn and proc_to_procvar
- typeconvn need this as resulttype so they know
- that the address needs to be returned }
- resulttype.setdef(procdef);
- { process methodpointer }
- if assigned(left) then
- resulttypepass(left);
- end;
- else
- internalerror(200104141);
- end;
- end;
- procedure Tloadnode.mark_write;
- begin
- include(flags,nf_write);
- end;
- function tloadnode.pass_1 : tnode;
- begin
- result:=nil;
- expectloc:=LOC_REFERENCE;
- registersint:=0;
- registersfpu:=0;
- {$ifdef SUPPORT_MMX}
- registersmmx:=0;
- {$endif SUPPORT_MMX}
- case symtableentry.typ of
- absolutesym :
- ;
- constsym:
- begin
- if tconstsym(symtableentry).consttyp=constresourcestring then
- begin
- include(current_procinfo.flags,pi_needs_implicit_finally);
- expectloc:=LOC_CREFERENCE;
- end;
- end;
- varsym :
- begin
- if assigned(left) then
- firstpass(left);
- if (cs_regvars in aktglobalswitches) and
- (symtable.symtabletype in [localsymtable]) and
- not(pi_has_assembler_block in current_procinfo.flags) and
- (tvarsym(symtableentry).varregable<>vr_none) then
- expectloc:=LOC_CREGISTER
- else
- if (tvarsym(symtableentry).varspez=vs_const) then
- expectloc:=LOC_CREFERENCE;
- { we need a register for call by reference parameters }
- if paramanager.push_addr_param(tvarsym(symtableentry).varspez,tvarsym(symtableentry).vartype.def,pocall_default) then
- registersint:=1;
- if ([vo_is_thread_var,vo_is_dll_var]*tvarsym(symtableentry).varoptions)<>[] then
- registersint:=1;
- if (target_info.system=system_powerpc_darwin) and (vo_is_dll_var in tvarsym(symtableentry).varoptions) then
- include(current_procinfo.flags,pi_needs_got);
- { call to get address of threadvar }
- if (vo_is_thread_var in tvarsym(symtableentry).varoptions) then
- include(current_procinfo.flags,pi_do_call);
- if nf_write in flags then
- Tvarsym(symtableentry).trigger_notifications(vn_onwrite)
- else
- Tvarsym(symtableentry).trigger_notifications(vn_onread);
- { count variable references }
- if cg.t_times>1 then
- inc(tvarsym(symtableentry).refs,cg.t_times-1);
- end;
- typedconstsym :
- ;
- procsym :
- begin
- { method pointer ? }
- if assigned(left) then
- begin
- expectloc:=LOC_CREFERENCE;
- firstpass(left);
- registersint:=max(registersint,left.registersint);
- registersfpu:=max(registersfpu,left.registersfpu);
- {$ifdef SUPPORT_MMX}
- registersmmx:=max(registersmmx,left.registersmmx);
- {$endif SUPPORT_MMX}
- end;
- end;
- else
- internalerror(200104143);
- end;
- end;
- function tloadnode.docompare(p: tnode): boolean;
- begin
- docompare :=
- inherited docompare(p) and
- (symtableentry = tloadnode(p).symtableentry) and
- (procdef = tloadnode(p).procdef) and
- (symtable = tloadnode(p).symtable);
- end;
- procedure Tloadnode.printnodedata(var t:text);
- begin
- inherited printnodedata(t);
- write(t,printnodeindention,'symbol = ',symtableentry.name);
- if symtableentry.typ=procsym then
- write(t,printnodeindention,'procdef = ',procdef.mangledname);
- writeln(t,'');
- end;
- {*****************************************************************************
- TASSIGNMENTNODE
- *****************************************************************************}
- constructor tassignmentnode.create(l,r : tnode);
- begin
- inherited create(assignn,l,r);
- l.mark_write;
- assigntype:=at_normal;
- end;
- constructor tassignmentnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
- begin
- inherited ppuload(t,ppufile);
- assigntype:=tassigntype(ppufile.getbyte);
- end;
- procedure tassignmentnode.ppuwrite(ppufile:tcompilerppufile);
- begin
- inherited ppuwrite(ppufile);
- ppufile.putbyte(byte(assigntype));
- end;
- function tassignmentnode.getcopy : tnode;
- var
- n : tassignmentnode;
- begin
- n:=tassignmentnode(inherited getcopy);
- n.assigntype:=assigntype;
- getcopy:=n;
- end;
- function tassignmentnode.det_resulttype:tnode;
- var
- hp : tnode;
- useshelper : boolean;
- original_size : longint;
- begin
- result:=nil;
- resulttype:=voidtype;
- original_size := 0;
- { must be made unique }
- set_unique(left);
- resulttypepass(left);
- if is_ansistring(left.resulttype.def) then
- begin
- { fold <ansistring>:=<ansistring>+<char|shortstring|ansistring> }
- if (right.nodetype=addn) and
- left.isequal(tbinarynode(right).left) and
- { don't fold multiple concatenations else we could get trouble
- with multiple uses of s
- }
- (tbinarynode(right).left.nodetype<>addn) and
- (tbinarynode(right).right.nodetype<>addn) then
- begin
- { don't do a resulttypepass(right), since then the addnode }
- { may insert typeconversions that make this optimization }
- { opportunity quite difficult to detect (JM) }
- resulttypepass(tbinarynode(right).left);
- resulttypepass(tbinarynode(right).right);
- if (is_char(tbinarynode(right).right.resulttype.def) or
- is_shortstring(tbinarynode(right).right.resulttype.def) or
- is_ansistring(tbinarynode(right).right.resulttype.def)) then
- begin
- { remove property flag so it'll not trigger an error }
- exclude(left.flags,nf_isproperty);
- { generate call to helper }
- hp:=ccallparanode.create(tbinarynode(right).right,
- ccallparanode.create(left,nil));
- if is_char(tbinarynode(right).right.resulttype.def) then
- result:=ccallnode.createintern('fpc_'+Tstringdef(left.resulttype.def).stringtypname+'_append_char',hp)
- else if is_shortstring(tbinarynode(right).right.resulttype.def) then
- result:=ccallnode.createintern('fpc_'+Tstringdef(left.resulttype.def).stringtypname+'_append_shortstring',hp)
- else
- result:=ccallnode.createintern('fpc_'+Tstringdef(left.resulttype.def).stringtypname+'_append_ansistring',hp);
- tbinarynode(right).right:=nil;
- left:=nil;
- exit;
- end;
- end;
- end
- else
- if is_shortstring(left.resulttype.def) then
- begin
- { fold <shortstring>:=<shortstring>+<shortstring>,
- <shortstring>+<char> is handled by an optimized node }
- if (right.nodetype=addn) and
- left.isequal(tbinarynode(right).left) and
- { don't fold multiple concatenations else we could get trouble
- with multiple uses of s }
- (tbinarynode(right).left.nodetype<>addn) and
- (tbinarynode(right).right.nodetype<>addn) then
- begin
- { don't do a resulttypepass(right), since then the addnode }
- { may insert typeconversions that make this optimization }
- { opportunity quite difficult to detect (JM) }
- resulttypepass(tbinarynode(right).left);
- resulttypepass(tbinarynode(right).right);
- if is_shortstring(tbinarynode(right).right.resulttype.def) then
- begin
- { remove property flag so it'll not trigger an error }
- exclude(left.flags,nf_isproperty);
- { generate call to helper }
- hp:=ccallparanode.create(tbinarynode(right).right,
- ccallparanode.create(left,nil));
- if is_shortstring(tbinarynode(right).right.resulttype.def) then
- result:=ccallnode.createintern('fpc_shortstr_append_shortstr',hp);
- tbinarynode(right).right:=nil;
- left:=nil;
- exit;
- end;
- end;
- end;
- resulttypepass(right);
- set_varstate(left,vs_assigned,false);
- set_varstate(right,vs_used,true);
- if codegenerror then
- exit;
- { tp procvar support, when we don't expect a procvar
- then we need to call the procvar }
- if (left.resulttype.def.deftype<>procvardef) then
- maybe_call_procvar(right,true);
- { assignments to formaldefs and open arrays aren't allowed }
- if (left.resulttype.def.deftype=formaldef) or
- is_open_array(left.resulttype.def) then
- CGMessage(type_e_operator_not_allowed);
- { test if node can be assigned, properties are allowed }
- valid_for_assignment(left);
- { assigning nil to a dynamic array clears the array }
- if is_dynamic_array(left.resulttype.def) and
- (right.nodetype=niln) then
- begin
- hp:=ccallparanode.create(caddrnode.create
- (crttinode.create(tstoreddef(left.resulttype.def),initrtti)),
- ccallparanode.create(ctypeconvnode.create_explicit(left,voidpointertype),nil));
- result := ccallnode.createintern('fpc_dynarray_clear',hp);
- left:=nil;
- exit;
- end;
- { shortstring helpers can do the conversion directly,
- so treat them separatly }
- if (is_shortstring(left.resulttype.def)) then
- begin
- { insert typeconv, except for chars that are handled in
- secondpass and except for ansi/wide string that can
- be converted immediatly }
- if not(is_char(right.resulttype.def) or
- (right.resulttype.def.deftype=stringdef)) then
- inserttypeconv(right,left.resulttype);
- if right.resulttype.def.deftype=stringdef then
- begin
- useshelper:=true;
- { convert constant strings to shortstrings. But
- skip empty constant strings, that will be handled
- in secondpass }
- if (right.nodetype=stringconstn) then
- begin
- { verify if range fits within shortstring }
- { just emit a warning, delphi gives an }
- { error, only if the type definition of }
- { of the string is less < 255 characters }
- if not is_open_string(left.resulttype.def) and
- (tstringconstnode(right).len > tstringdef(left.resulttype.def).len) then
- cgmessage(type_w_string_too_long);
- inserttypeconv(right,left.resulttype);
- if (tstringconstnode(right).len=0) then
- useshelper:=false;
- end;
- { rest is done in pass 1 (JM) }
- if useshelper then
- exit;
- end
- end
- else
- begin
- { get the size before the type conversion - check for all nodes }
- if assigned(right.resulttype.def) and
- (right.resulttype.def.deftype in [enumdef,orddef,floatdef]) and
- (right.nodetype in [loadn,vecn,calln]) then
- original_size := right.resulttype.def.size;
- inserttypeconv(right,left.resulttype);
- end;
- { check if the assignment may cause a range check error }
- { if its not explicit, and only if the values are }
- { ordinals, enumdef and floatdef }
- if (right.nodetype = typeconvn) and
- not (nf_explicit in ttypeconvnode(right).flags) then
- begin
- if assigned(left.resulttype.def) and
- (left.resulttype.def.deftype in [enumdef,orddef,floatdef]) then
- begin
- if (original_size <> 0) and (left.resulttype.def.size < original_size) then
- begin
- if (cs_check_range in aktlocalswitches) then
- Message(type_w_smaller_possible_range_check)
- else
- Message(type_h_smaller_possible_range_check);
- end;
- end;
- end;
- { call helpers for interface }
- if is_interfacecom(left.resulttype.def) then
- begin
- hp:=ccallparanode.create(ctypeconvnode.create_explicit
- (right,voidpointertype),
- ccallparanode.create(ctypeconvnode.create_explicit
- (left,voidpointertype),nil));
- result:=ccallnode.createintern('fpc_intf_assign',hp);
- left:=nil;
- right:=nil;
- exit;
- end;
- { check if local proc/func is assigned to procvar }
- if right.resulttype.def.deftype=procvardef then
- test_local_to_procvar(tprocvardef(right.resulttype.def),left.resulttype.def);
- end;
- function tassignmentnode.pass_1 : tnode;
- var
- hp: tnode;
- begin
- result:=nil;
- expectloc:=LOC_VOID;
- firstpass(left);
- firstpass(right);
- { assignment to refcounted variable -> inc/decref }
- if (not is_class(left.resulttype.def) and
- left.resulttype.def.needs_inittable) then
- include(current_procinfo.flags,pi_do_call);
- if codegenerror then
- exit;
- if (is_shortstring(left.resulttype.def)) then
- begin
- if right.resulttype.def.deftype=stringdef then
- begin
- if (right.nodetype<>stringconstn) or
- (tstringconstnode(right).len<>0) then
- begin
- if (cs_optimize in aktglobalswitches) and
- (right.nodetype in [calln,blockn]) and
- (left.nodetype = temprefn) and
- is_shortstring(right.resulttype.def) and
- not is_open_string(left.resulttype.def) and
- (tstringdef(left.resulttype.def).len = 255) then
- begin
- { the blocknode case is handled in pass_2 at the temp }
- { reference level (mainly for callparatemp) (JM) }
- if (right.nodetype = calln) then
- begin
- tcallnode(right).funcretnode := left;
- result := right;
- end
- else
- exit;
- end
- else
- begin
- hp:=ccallparanode.create
- (right,
- ccallparanode.create(cinlinenode.create
- (in_high_x,false,left.getcopy),nil));
- result:=ccallnode.createinternreturn('fpc_'+tstringdef(right.resulttype.def).stringtypname+'_to_shortstr',hp,left);
- firstpass(result);
- end;
- left:=nil;
- right:=nil;
- exit;
- end;
- end;
- end;
- if (cs_optimize in aktglobalswitches) and
- (right.nodetype = calln) and
- { left must be a temp, since otherwise as soon as you modify the }
- { result, the current left node is modified and that one may }
- { still be an argument to the function or even accessed in the }
- { function }
- (((left.nodetype = temprefn) and
- paramanager.ret_in_param(right.resulttype.def,
- tcallnode(right).procdefinition.proccalloption)) or
- { there's special support for ansi/widestrings in the callnode }
- is_ansistring(right.resulttype.def) or
- is_widestring(right.resulttype.def)) then
- begin
- tcallnode(right).funcretnode := left;
- result := right;
- left := nil;
- right := nil;
- exit;
- end;
- registersint:=left.registersint+right.registersint;
- registersfpu:=max(left.registersfpu,right.registersfpu);
- {$ifdef SUPPORT_MMX}
- registersmmx:=max(left.registersmmx,right.registersmmx);
- {$endif SUPPORT_MMX}
- end;
- function tassignmentnode.docompare(p: tnode): boolean;
- begin
- docompare :=
- inherited docompare(p) and
- (assigntype = tassignmentnode(p).assigntype);
- end;
- {$ifdef state_tracking}
- function Tassignmentnode.track_state_pass(exec_known:boolean):boolean;
- var se:Tstate_entry;
- begin
- track_state_pass:=false;
- if exec_known then
- begin
- track_state_pass:=right.track_state_pass(exec_known);
- {Force a new resulttype pass.}
- right.resulttype.def:=nil;
- do_resulttypepass(right);
- resulttypepass(right);
- aktstate.store_fact(left.getcopy,right.getcopy);
- end
- else
- aktstate.delete_fact(left);
- end;
- {$endif}
- {*****************************************************************************
- TARRAYCONSTRUCTORRANGENODE
- *****************************************************************************}
- constructor tarrayconstructorrangenode.create(l,r : tnode);
- begin
- inherited create(arrayconstructorrangen,l,r);
- end;
- function tarrayconstructorrangenode.det_resulttype:tnode;
- begin
- result:=nil;
- resulttypepass(left);
- resulttypepass(right);
- set_varstate(left,vs_used,true);
- set_varstate(right,vs_used,true);
- if codegenerror then
- exit;
- resulttype:=left.resulttype;
- end;
- function tarrayconstructorrangenode.pass_1 : tnode;
- begin
- firstpass(left);
- firstpass(right);
- expectloc:=LOC_CREFERENCE;
- calcregisters(self,0,0,0);
- result:=nil;
- end;
- {****************************************************************************
- TARRAYCONSTRUCTORNODE
- *****************************************************************************}
- constructor tarrayconstructornode.create(l,r : tnode);
- begin
- inherited create(arrayconstructorn,l,r);
- end;
- function tarrayconstructornode.getcopy : tnode;
- var
- n : tarrayconstructornode;
- begin
- n:=tarrayconstructornode(inherited getcopy);
- result:=n;
- end;
- function tarrayconstructornode.det_resulttype:tnode;
- var
- htype : ttype;
- hp : tarrayconstructornode;
- len : longint;
- varia : boolean;
- begin
- result:=nil;
- { are we allowing array constructor? Then convert it to a set }
- if not allow_array_constructor then
- begin
- hp:=tarrayconstructornode(getcopy);
- arrayconstructor_to_set(tnode(hp));
- result:=hp;
- exit;
- end;
- { only pass left tree, right tree contains next construct if any }
- htype.reset;
- len:=0;
- varia:=false;
- if assigned(left) then
- begin
- hp:=self;
- while assigned(hp) do
- begin
- resulttypepass(hp.left);
- set_varstate(hp.left,vs_used,true);
- if (htype.def=nil) then
- htype:=hp.left.resulttype
- else
- begin
- if ((nf_novariaallowed in flags) or (not varia)) and
- (not equal_defs(htype.def,hp.left.resulttype.def)) then
- begin
- varia:=true;
- end;
- end;
- inc(len);
- hp:=tarrayconstructornode(hp.right);
- end;
- end;
- if not assigned(htype.def) then
- htype:=voidtype;
- resulttype.setdef(tarraydef.create(0,len-1,s32inttype));
- tarraydef(resulttype.def).setelementtype(htype);
- tarraydef(resulttype.def).IsConstructor:=true;
- tarraydef(resulttype.def).IsVariant:=varia;
- end;
- procedure tarrayconstructornode.force_type(tt:ttype);
- var
- hp : tarrayconstructornode;
- begin
- tarraydef(resulttype.def).setelementtype(tt);
- tarraydef(resulttype.def).IsConstructor:=true;
- tarraydef(resulttype.def).IsVariant:=false;
- if assigned(left) then
- begin
- hp:=self;
- while assigned(hp) do
- begin
- inserttypeconv(hp.left,tt);
- hp:=tarrayconstructornode(hp.right);
- end;
- end;
- end;
- procedure tarrayconstructornode.insert_typeconvs;
- var
- hp : tarrayconstructornode;
- dovariant : boolean;
- begin
- dovariant:=(nf_forcevaria in flags) or tarraydef(resulttype.def).isvariant;
- { only pass left tree, right tree contains next construct if any }
- if assigned(left) then
- begin
- hp:=self;
- while assigned(hp) do
- begin
- resulttypepass(hp.left);
- { Insert typeconvs for array of const }
- if dovariant then
- begin
- case hp.left.resulttype.def.deftype of
- enumdef :
- hp.left:=ctypeconvnode.create_explicit(hp.left,s32inttype);
- arraydef :
- hp.left:=ctypeconvnode.create(hp.left,charpointertype);
- orddef :
- begin
- if is_integer(hp.left.resulttype.def) and
- not(is_64bitint(hp.left.resulttype.def)) then
- hp.left:=ctypeconvnode.create(hp.left,s32inttype);
- end;
- floatdef :
- hp.left:=ctypeconvnode.create(hp.left,pbestrealtype^);
- procvardef :
- hp.left:=ctypeconvnode.create(hp.left,voidpointertype);
- stringdef,
- variantdef,
- pointerdef,
- classrefdef,
- objectdef : ;
- else
- CGMessagePos1(hp.left.fileinfo,type_e_wrong_type_in_array_constructor,hp.left.resulttype.def.typename);
- end;
- end;
- resulttypepass(hp.left);
- hp:=tarrayconstructornode(hp.right);
- end;
- end;
- end;
- function tarrayconstructornode.pass_1 : tnode;
- var
- hp : tarrayconstructornode;
- do_variant:boolean;
- begin
- do_variant:=(nf_forcevaria in flags) or tarraydef(resulttype.def).isvariant;
- result:=nil;
- { Insert required type convs, this must be
- done in pass 1, because the call must be
- resulttypepassed already }
- if assigned(left) then
- begin
- insert_typeconvs;
- { call firstpass for all nodes }
- hp:=self;
- while assigned(hp) do
- begin
- if hp.left<>nil then
- begin
- {This check is pessimistic; a call will happen depending
- on the location in which the elements will be found in
- pass 2.}
- if not do_variant then
- include(current_procinfo.flags,pi_do_call);
- firstpass(hp.left);
- end;
- hp:=tarrayconstructornode(hp.right);
- end;
- end;
- expectloc:=LOC_CREFERENCE;
- calcregisters(self,0,0,0);
- end;
- function tarrayconstructornode.docompare(p: tnode): boolean;
- begin
- docompare:=inherited docompare(p);
- end;
- {*****************************************************************************
- TTYPENODE
- *****************************************************************************}
- constructor ttypenode.create(t : ttype);
- begin
- inherited create(typen);
- restype:=t;
- allowed:=false;
- end;
- constructor ttypenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
- begin
- inherited ppuload(t,ppufile);
- ppufile.gettype(restype);
- allowed:=boolean(ppufile.getbyte);
- end;
- procedure ttypenode.ppuwrite(ppufile:tcompilerppufile);
- begin
- inherited ppuwrite(ppufile);
- ppufile.puttype(restype);
- ppufile.putbyte(byte(allowed));
- end;
- procedure ttypenode.buildderefimpl;
- begin
- inherited buildderefimpl;
- restype.buildderef;
- end;
- procedure ttypenode.derefimpl;
- begin
- inherited derefimpl;
- restype.resolve;
- end;
- function ttypenode.det_resulttype:tnode;
- begin
- result:=nil;
- resulttype:=restype;
- { check if it's valid }
- if restype.def.deftype = errordef then
- CGMessage(parser_e_illegal_expression);
- end;
- function ttypenode.pass_1 : tnode;
- begin
- result:=nil;
- expectloc:=LOC_VOID;
- { a typenode can't generate code, so we give here
- an error. Else it'll be an abstract error in pass_2.
- Only when the allowed flag is set we don't generate
- an error }
- if not allowed then
- Message(parser_e_no_type_not_allowed_here);
- end;
- function ttypenode.docompare(p: tnode): boolean;
- begin
- docompare :=
- inherited docompare(p);
- end;
- {*****************************************************************************
- TRTTINODE
- *****************************************************************************}
- constructor trttinode.create(def:tstoreddef;rt:trttitype);
- begin
- inherited create(rttin);
- rttidef:=def;
- rttitype:=rt;
- end;
- constructor trttinode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
- begin
- inherited ppuload(t,ppufile);
- ppufile.getderef(rttidefderef);
- rttitype:=trttitype(ppufile.getbyte);
- end;
- procedure trttinode.ppuwrite(ppufile:tcompilerppufile);
- begin
- inherited ppuwrite(ppufile);
- ppufile.putderef(rttidefderef);
- ppufile.putbyte(byte(rttitype));
- end;
- procedure trttinode.buildderefimpl;
- begin
- inherited buildderefimpl;
- rttidefderef.build(rttidef);
- end;
- procedure trttinode.derefimpl;
- begin
- inherited derefimpl;
- rttidef:=tstoreddef(rttidefderef.resolve);
- end;
- function trttinode.getcopy : tnode;
- var
- n : trttinode;
- begin
- n:=trttinode(inherited getcopy);
- n.rttidef:=rttidef;
- n.rttitype:=rttitype;
- result:=n;
- end;
- function trttinode.det_resulttype:tnode;
- begin
- { rtti information will be returned as a void pointer }
- result:=nil;
- resulttype:=voidpointertype;
- end;
- function trttinode.pass_1 : tnode;
- begin
- result:=nil;
- expectloc:=LOC_CREFERENCE;
- end;
- function trttinode.docompare(p: tnode): boolean;
- begin
- docompare :=
- inherited docompare(p) and
- (rttidef = trttinode(p).rttidef) and
- (rttitype = trttinode(p).rttitype);
- end;
- procedure trttinode.pass_2;
- begin
- location_reset(location,LOC_CREFERENCE,OS_NO);
- location.reference.symbol:=rttidef.get_rtti_label(rttitype);
- end;
- begin
- cloadnode:=tloadnode;
- cassignmentnode:=tassignmentnode;
- carrayconstructorrangenode:=tarrayconstructorrangenode;
- carrayconstructornode:=tarrayconstructornode;
- ctypenode:=ttypenode;
- crttinode:=trttinode;
- end.
- {
- $Log$
- Revision 1.132 2004-10-10 20:22:53 peter
- * symtable allocation rewritten
- * loading of parameters to local temps/regs cleanup
- * regvar support for parameters
- * regvar support for staticsymtable (main body)
- Revision 1.131 2004/10/08 17:09:43 peter
- * tvarsym.varregable added, split vo_regable from varoptions
- Revision 1.130 2004/10/06 19:26:50 jonas
- * regvar fixes from Peter
- Revision 1.129 2004/09/26 17:45:30 peter
- * simple regvar support, not yet finished
- Revision 1.128 2004/06/20 08:55:29 florian
- * logs truncated
- Revision 1.127 2004/06/16 20:07:08 florian
- * dwarf branch merged
- Revision 1.126 2004/04/29 19:56:37 daniel
- * Prepare compiler infrastructure for multiple ansistring types
- Revision 1.125.2.1 2004/04/28 19:55:51 peter
- * new warning for ordinal-pointer when size is different
- * fixed some cg_e_ messages to the correct section type_e_ or parser_e_
- Revision 1.125 2004/03/02 17:32:12 florian
- * make cycle fixed
- + pic support for darwin
- + support of importing vars from shared libs on darwin implemented
- Revision 1.124 2004/02/20 22:15:26 peter
- * fixed compiler err
- }
|