123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128 |
- {
- $Id$
- Copyright (c) 1998-2002 by Florian Klaempfl
- Generate i386 assembler for in set/case 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 n386set;
- {$i fpcdefs.inc}
- interface
- uses
- node,nset,pass_1;
- type
- ti386innode = class(tinnode)
- procedure pass_2;override;
- function pass_1 : tnode;override;
- end;
- ti386casenode = class(tcasenode)
- procedure pass_2;override;
- end;
- implementation
- uses
- globtype,systems,
- verbose,globals,
- symconst,symdef,defbase,
- aasmbase,aasmtai,aasmcpu,
- cginfo,cgbase,pass_2,
- ncon,
- cpubase,
- cga,cgobj,tgobj,ncgutil,regvars,rgobj;
- const
- bytes2Sxx:array[1..8] of Topsize=(S_B,S_W,S_NO,S_L,S_NO,S_NO,S_NO,S_Q);
- {*****************************************************************************
- TI386INNODE
- *****************************************************************************}
- function ti386innode.pass_1 : tnode;
- begin
- result:=nil;
- { this is the only difference from the generic version }
- location.loc:=LOC_FLAGS;
- firstpass(right);
- firstpass(left);
- if codegenerror then
- exit;
- left_right_max;
- { this is not allways true due to optimization }
- { but if we don't set this we get problems with optimizing self code }
- if tsetdef(right.resulttype.def).settype<>smallset then
- procinfo^.flags:=procinfo^.flags or pi_do_call
- else
- begin
- { a smallset needs maybe an misc. register }
- if (left.nodetype<>ordconstn) and
- not(right.location.loc in [LOC_CREGISTER,LOC_REGISTER]) and
- (right.registers32<1) then
- inc(registers32);
- end;
- end;
- procedure ti386innode.pass_2;
- type
- Tsetpart=record
- range : boolean; {Part is a range.}
- start,stop : byte; {Start/stop when range; Stop=element when an element.}
- end;
- var
- genjumps,
- use_small,
- ranges : boolean;
- hr,hr2,
- pleftreg : tregister;
- href : treference;
- opsize : topsize;
- setparts : array[1..8] of Tsetpart;
- i,numparts : byte;
- adjustment : longint;
- pushedregs : tmaybesave;
- l,l2 : tasmlabel;
- {$ifdef CORRECT_SET_IN_FPC}
- AM : tasmop;
- {$endif CORRECT_SET_IN_FPC}
- function analizeset(Aset:pconstset;is_small:boolean):boolean;
- type
- byteset=set of byte;
- var
- compares,maxcompares:word;
- i:byte;
- begin
- if byteset(Aset^)=[] then
- {The expression...
- if expr in []
- ...is allways false. It should be optimized away in the
- resulttype pass, and thus never occur here. Since we
- do generate wrong code for it, do internalerror.}
- internalerror(2002072301);
- analizeset:=false;
- ranges:=false;
- numparts:=0;
- compares:=0;
- { Lots of comparisions take a lot of time, so do not allow
- too much comparisions. 8 comparisions are, however, still
- smalller than emitting the set }
- if cs_littlesize in aktglobalswitches then
- maxcompares:=8
- else
- maxcompares:=5;
- { when smallset is possible allow only 3 compares the smallset
- code is for littlesize also smaller when more compares are used }
- if is_small then
- maxcompares:=3;
- for i:=0 to 255 do
- if i in byteset(Aset^) then
- begin
- if (numparts=0) or (i<>setparts[numparts].stop+1) then
- begin
- {Set element is a separate element.}
- inc(compares);
- if compares>maxcompares then
- exit;
- inc(numparts);
- setparts[numparts].range:=false;
- setparts[numparts].stop:=i;
- end
- else
- {Set element is part of a range.}
- if not setparts[numparts].range then
- begin
- {Transform an element into a range.}
- setparts[numparts].range:=true;
- setparts[numparts].start:=setparts[numparts].stop;
- setparts[numparts].stop:=i;
- ranges := true;
- { there's only one compare per range anymore. Only a }
- { sub is added, but that's much faster than a }
- { cmp/jcc combo so neglect its effect }
- { inc(compares);
- if compares>maxcompares then
- exit; }
- end
- else
- begin
- {Extend a range.}
- setparts[numparts].stop:=i;
- end;
- end;
- analizeset:=true;
- end;
- begin
- { We check first if we can generate jumps, this can be done
- because the resulttype.def is already set in firstpass }
- { check if we can use smallset operation using btl which is limited
- to 32 bits, the left side may also not contain higher values !! }
- use_small:=(tsetdef(right.resulttype.def).settype=smallset) and
- ((left.resulttype.def.deftype=orddef) and (torddef(left.resulttype.def).high<=32) or
- (left.resulttype.def.deftype=enumdef) and (tenumdef(left.resulttype.def).max<=32));
- { Can we generate jumps? Possible for all types of sets }
- genjumps:=(right.nodetype=setconstn) and
- analizeset(tsetconstnode(right).value_set,use_small);
- { calculate both operators }
- { the complex one first }
- firstcomplex(self);
- secondpass(left);
- { Only process the right if we are not generating jumps }
- if not genjumps then
- begin
- maybe_save(exprasmlist,right.registers32,left.location,pushedregs);
- secondpass(right);
- maybe_restore(exprasmlist,left.location,pushedregs);
- end;
- if codegenerror then
- exit;
- { ofcourse not commutative }
- if nf_swaped in flags then
- swapleftright;
- if genjumps then
- begin
- { It gives us advantage to check for the set elements
- separately instead of using the SET_IN_BYTE procedure.
- To do: Build in support for LOC_JUMP }
- opsize := def_opsize(left.resulttype.def);
- { If register is used, use only lower 8 bits }
- if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
- begin
- { for ranges we always need a 32bit register, because then we }
- { use the register as base in a reference (JM) }
- if ranges then
- begin
- pleftreg:=rg.makeregsize(left.location.register,OS_INT);
- cg.a_load_reg_reg(exprasmlist,left.location.size,left.location.register,pleftreg);
- if opsize <> S_L then
- emit_const_reg(A_AND,S_L,255,pleftreg);
- opsize := S_L;
- end
- else
- { otherwise simply use the lower 8 bits (no "and" }
- { necessary this way) (JM) }
- begin
- pleftreg:=rg.makeregsize(left.location.register,OS_8);
- opsize := S_B;
- end;
- end
- else
- begin
- { load the value in a register }
- pleftreg := rg.getexplicitregisterint(exprasmlist,R_EDI);
- opsize := S_L;
- emit_ref_reg(A_MOVZX,S_BL,left.location.reference,pleftreg);
- end;
- { Get a label to jump to the end }
- location_reset(location,LOC_FLAGS,OS_NO);
- { It's better to use the zero flag when there are
- no ranges }
- if ranges then
- location.resflags:=F_C
- else
- location.resflags:=F_E;
- getlabel(l);
- { how much have we already substracted from the x in the }
- { "x in [y..z]" expression }
- adjustment := 0;
- for i:=1 to numparts do
- if setparts[i].range then
- { use fact that a <= x <= b <=> cardinal(x-a) <= cardinal(b-a) }
- begin
- { is the range different from all legal values? }
- if (setparts[i].stop-setparts[i].start <> 255) then
- begin
- { yes, is the lower bound <> 0? }
- if (setparts[i].start <> 0) then
- { we're going to substract from the left register, }
- { so in case of a LOC_CREGISTER first move the value }
- { to edi (not done before because now we can do the }
- { move and substract in one instruction with LEA) }
- if (pleftreg <> R_EDI) and
- (left.location.loc = LOC_CREGISTER) then
- begin
- rg.ungetregister(exprasmlist,pleftreg);
- rg.getexplicitregisterint(exprasmlist,R_EDI);
- reference_reset_base(href,pleftreg,-setparts[i].start);
- emit_ref_reg(A_LEA,S_L,href,R_EDI);
- { only now change pleftreg since previous value is }
- { still used in previous instruction }
- pleftreg := R_EDI;
- opsize := S_L;
- end
- else
- begin
- { otherwise, the value is already in a register }
- { that can be modified }
- if setparts[i].start-adjustment <> 1 then
- emit_const_reg(A_SUB,opsize,
- setparts[i].start-adjustment,pleftreg)
- else emit_reg(A_DEC,opsize,pleftreg);
- end;
- { new total value substracted from x: }
- { adjustment + (setparts[i].start - adjustment) }
- adjustment := setparts[i].start;
- { check if result < b-a+1 (not "result <= b-a", since }
- { we need a carry in case the element is in the range }
- { (this will never overflow since we check at the }
- { beginning whether stop-start <> 255) }
- emit_const_reg(A_CMP,opsize,
- setparts[i].stop-setparts[i].start+1,pleftreg);
- { use C_C instead of C_B: the meaning is the same, but }
- { then the optimizer can easier trace the jump to its }
- { final destination since the resultflag of this node }
- { is set to the carryflag }
- emitjmp(C_C,l);
- end
- else
- { if setparts[i].start = 0 and setparts[i].stop = 255, }
- { it's always true since "in" is only allowed for bytes }
- begin
- emit_none(A_STC,S_NO);
- cg.a_jmp_always(exprasmlist,l);
- end;
- end
- else
- begin
- { Emit code to check if left is an element }
- emit_const_reg(A_CMP,opsize,setparts[i].stop-adjustment,
- pleftreg);
- { Result should be in carry flag when ranges are used }
- if ranges then
- emit_none(A_STC,S_NO);
- { If found, jump to end }
- emitjmp(C_E,l);
- end;
- if ranges and
- { if the last one was a range, the carry flag is already }
- { set appropriately }
- not(setparts[numparts].range) then
- emit_none(A_CLC,S_NO);
- { To compensate for not doing a second pass }
- right.location.reference.symbol:=nil;
- { Now place the end label }
- cg.a_label(exprasmlist,l);
- case left.location.loc of
- LOC_REGISTER,
- LOC_CREGISTER :
- rg.ungetregister(exprasmlist,pleftreg);
- else
- begin
- reference_release(exprasmlist,left.location.reference);
- rg.ungetregister(exprasmlist,R_EDI);
- end;
- end;
- end
- else
- begin
- location_reset(location,LOC_FLAGS,OS_NO);
- { We will now generated code to check the set itself, no jmps,
- handle smallsets separate, because it allows faster checks }
- if use_small then
- begin
- if left.nodetype=ordconstn then
- begin
- location.resflags:=F_NE;
- case right.location.loc of
- LOC_REGISTER,
- LOC_CREGISTER:
- begin
- emit_const_reg(A_TEST,S_L,
- 1 shl (tordconstnode(left).value and 31),right.location.register);
- end;
- LOC_REFERENCE,
- LOC_CREFERENCE :
- begin
- emit_const_ref(A_TEST,S_L,1 shl (tordconstnode(left).value and 31),
- right.location.reference);
- end;
- else
- internalerror(200203312);
- end;
- location_release(exprasmlist,right.location);
- end
- else
- begin
- case left.location.loc of
- LOC_REGISTER,
- LOC_CREGISTER:
- begin
- hr:=rg.makeregsize(left.location.register,OS_INT);
- cg.a_load_reg_reg(exprasmlist,left.location.size,left.location.register,hr);
- end;
- else
- begin
- { the set element isn't never samller than a byte }
- { and because it's a small set we need only 5 bits }
- { but 8 bits are easier to load }
- rg.getexplicitregisterint(exprasmlist,R_EDI);
- emit_ref_reg(A_MOVZX,S_BL,left.location.reference,R_EDI);
- hr:=R_EDI;
- location_release(exprasmlist,left.location);
- end;
- end;
- case right.location.loc of
- LOC_REGISTER,
- LOC_CREGISTER :
- begin
- emit_reg_reg(A_BT,S_L,hr,
- right.location.register);
- rg.ungetregisterint(exprasmlist,right.location.register);
- end;
- LOC_CONSTANT :
- begin
- { We have to load the value into a register because
- btl does not accept values only refs or regs (PFV) }
- hr2:=rg.getregisterint(exprasmlist);
- emit_const_reg(A_MOV,S_L,
- right.location.value,hr2);
- emit_reg_reg(A_BT,S_L,hr,hr2);
- rg.ungetregisterint(exprasmlist,hr2);
- end;
- LOC_CREFERENCE,
- LOC_REFERENCE :
- begin
- location_release(exprasmlist,right.location);
- emit_reg_ref(A_BT,S_L,hr,right.location.reference);
- end;
- else
- internalerror(2002032210);
- end;
- { simply to indicate EDI is deallocated here too (JM) }
- rg.ungetregisterint(exprasmlist,hr);
- location.loc:=LOC_FLAGS;
- location.resflags:=F_C;
- end;
- end
- else
- begin
- if right.location.loc=LOC_CONSTANT then
- begin
- location.resflags:=F_C;
- getlabel(l);
- getlabel(l2);
- { Is this treated in firstpass ?? }
- if left.nodetype=ordconstn then
- begin
- hr:=rg.getregisterint(exprasmlist);
- left.location.loc:=LOC_REGISTER;
- left.location.register:=hr;
- emit_const_reg(A_MOV,S_L,
- tordconstnode(left).value,hr);
- end;
- case left.location.loc of
- LOC_REGISTER,
- LOC_CREGISTER:
- begin
- hr:=rg.makeregsize(left.location.register,OS_INT);
- cg.a_load_reg_reg(exprasmlist,left.location.size,left.location.register,hr);
- emit_const_reg(A_CMP,S_L,31,hr);
- emitjmp(C_NA,l);
- { reset carry flag }
- emit_none(A_CLC,S_NO);
- cg.a_jmp_always(exprasmlist,l2);
- cg.a_label(exprasmlist,l);
- { We have to load the value into a register because
- btl does not accept values only refs or regs (PFV) }
- hr2:=rg.getregisterint(exprasmlist);
- emit_const_reg(A_MOV,S_L,right.location.value,hr2);
- emit_reg_reg(A_BT,S_L,hr,hr2);
- rg.ungetregisterint(exprasmlist,hr2);
- end;
- else
- begin
- {$ifdef CORRECT_SET_IN_FPC}
- if m_tp in aktmodeswitches then
- begin
- {***WARNING only correct if
- reference is 32 bits (PM) *****}
- emit_const_ref(A_CMP,S_L,31,reference_copy(left.location.reference));
- end
- else
- {$endif CORRECT_SET_IN_FPC}
- begin
- emit_const_ref(A_CMP,S_B,31,left.location.reference);
- end;
- emitjmp(C_NA,l);
- { reset carry flag }
- emit_none(A_CLC,S_NO);
- cg.a_jmp_always(exprasmlist,l2);
- cg.a_label(exprasmlist,l);
- location_release(exprasmlist,left.location);
- hr:=rg.getregisterint(exprasmlist);
- emit_ref_reg(A_MOV,S_L,left.location.reference,hr);
- { We have to load the value into a register because
- btl does not accept values only refs or regs (PFV) }
- hr2:=rg.getregisterint(exprasmlist);
- emit_const_reg(A_MOV,S_L,
- right.location.value,hr2);
- emit_reg_reg(A_BT,S_L,hr,hr2);
- rg.ungetregisterint(exprasmlist,hr2);
- end;
- end;
- cg.a_label(exprasmlist,l2);
- end { of right.location.loc=LOC_CONSTANT }
- { do search in a normal set which could have >32 elementsm
- but also used if the left side contains higher values > 32 }
- else if left.nodetype=ordconstn then
- begin
- location.resflags:=F_NE;
- inc(right.location.reference.offset,tordconstnode(left).value shr 3);
- emit_const_ref(A_TEST,S_B,1 shl (tordconstnode(left).value and 7),right.location.reference);
- location_release(exprasmlist,right.location);
- end
- else
- begin
- if (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
- pleftreg:=rg.makeregsize(left.location.register,OS_INT)
- else
- pleftreg:=rg.getexplicitregisterint(exprasmlist,R_EDI);
- cg.a_load_loc_reg(exprasmlist,left.location,pleftreg);
- location_freetemp(exprasmlist,left.location);
- location_release(exprasmlist,left.location);
- emit_reg_ref(A_BT,S_L,pleftreg,right.location.reference);
- rg.ungetregister(exprasmlist,pleftreg);
- location_release(exprasmlist,right.location);
- { tg.ungetiftemp(exprasmlist,right.location.reference) happens below }
- location.resflags:=F_C;
- end;
- end;
- end;
- location_freetemp(exprasmlist,right.location);
- end;
- {*****************************************************************************
- TI386CASENODE
- *****************************************************************************}
- procedure ti386casenode.pass_2;
- var
- with_sign : boolean;
- opsize : topsize;
- jmp_gt,jmp_le,jmp_lee : tasmcond;
- hp : tnode;
- { register with case expression }
- hregister,hregister2 : tregister;
- endlabel,elselabel : tasmlabel;
- { true, if we can omit the range check of the jump table }
- jumptable_no_range : boolean;
- { where to put the jump table }
- jumpsegment : TAAsmoutput;
- min_label : TConstExprInt;
- procedure gentreejmp(p : pcaserecord);
- var
- lesslabel,greaterlabel : tasmlabel;
- begin
- cg.a_label(exprasmlist,p^._at);
- { calculate labels for left and right }
- if (p^.less=nil) then
- lesslabel:=elselabel
- else
- lesslabel:=p^.less^._at;
- if (p^.greater=nil) then
- greaterlabel:=elselabel
- else
- greaterlabel:=p^.greater^._at;
- { calculate labels for left and right }
- { no range label: }
- if p^._low=p^._high then
- begin
- emit_const_reg(A_CMP,opsize,p^._low,hregister);
- if greaterlabel=lesslabel then
- emitjmp(C_NE,lesslabel)
- else
- begin
- emitjmp(jmp_le,lesslabel);
- emitjmp(jmp_gt,greaterlabel);
- end;
- cg.a_jmp_always(exprasmlist,p^.statement);
- end
- else
- begin
- emit_const_reg(A_CMP,opsize,p^._low,hregister);
- emitjmp(jmp_le,lesslabel);
- emit_const_reg(A_CMP,opsize,p^._high,hregister);
- emitjmp(jmp_gt,greaterlabel);
- cg.a_jmp_always(exprasmlist,p^.statement);
- end;
- if assigned(p^.less) then
- gentreejmp(p^.less);
- if assigned(p^.greater) then
- gentreejmp(p^.greater);
- end;
- procedure genlinearcmplist(hp : pcaserecord);
- var
- first : boolean;
- last : TConstExprInt;
- procedure genitem(t : pcaserecord);
- var
- l1 : tasmlabel;
- begin
- if assigned(t^.less) then
- genitem(t^.less);
- if t^._low=t^._high then
- begin
- if opsize=S_Q then
- begin
- getlabel(l1);
- emit_const_reg(A_CMP,S_L,longint(hi(int64(t^._low))),hregister2);
- emitjmp(C_NZ,l1);
- emit_const_reg(A_CMP,S_L,longint(lo(int64(t^._low))),hregister);
- emitjmp(C_Z,t^.statement);
- cg.a_label(exprasmlist,l1);
- end
- else
- begin
- emit_const_reg(A_CMP,opsize,longint(t^._low),hregister);
- emitjmp(C_Z,t^.statement);
- last:=t^._low;
- end;
- end
- else
- begin
- { if there is no unused label between the last and the }
- { present label then the lower limit can be checked }
- { immediately. else check the range in between: }
- if first or (t^._low-last>1) then
- begin
- if opsize=S_Q then
- begin
- getlabel(l1);
- emit_const_reg(A_CMP,S_L,longint(hi(int64(t^._low))),hregister2);
- emitjmp(jmp_le,elselabel);
- emitjmp(jmp_gt,l1);
- emit_const_reg(A_CMP,S_L,longint(lo(int64(t^._low))),hregister);
- { the comparisation of the low dword must be always unsigned! }
- emitjmp(C_B,elselabel);
- cg.a_label(exprasmlist,l1);
- end
- else
- begin
- emit_const_reg(A_CMP,opsize,longint(t^._low),hregister);
- emitjmp(jmp_le,elselabel);
- end;
- end;
- if opsize=S_Q then
- begin
- getlabel(l1);
- emit_const_reg(A_CMP,S_L,longint(hi(int64(t^._high))),hregister2);
- emitjmp(jmp_le,t^.statement);
- emitjmp(jmp_gt,l1);
- emit_const_reg(A_CMP,S_L,longint(lo(int64(t^._high))),hregister);
- { the comparisation of the low dword must be always unsigned! }
- emitjmp(C_BE,t^.statement);
- cg.a_label(exprasmlist,l1);
- end
- else
- begin
- emit_const_reg(A_CMP,opsize,longint(t^._high),hregister);
- emitjmp(jmp_lee,t^.statement);
- end;
- last:=t^._high;
- end;
- first:=false;
- if assigned(t^.greater) then
- genitem(t^.greater);
- end;
- begin
- last:=0;
- first:=true;
- genitem(hp);
- cg.a_jmp_always(exprasmlist,elselabel);
- end;
- procedure genlinearlist(hp : pcaserecord);
- var
- first : boolean;
- last : TConstExprInt;
- {helplabel : longint;}
- procedure genitem(t : pcaserecord);
- procedure gensub(value:longint);
- begin
- if value=1 then
- emit_reg(A_DEC,opsize,hregister)
- else
- emit_const_reg(A_SUB,opsize,value,hregister);
- end;
- begin
- if assigned(t^.less) then
- genitem(t^.less);
- { need we to test the first value }
- if first and (t^._low>get_min_value(left.resulttype.def)) then
- begin
- emit_const_reg(A_CMP,opsize,longint(t^._low),hregister);
- emitjmp(jmp_le,elselabel);
- end;
- if t^._low=t^._high then
- begin
- if t^._low-last=0 then
- emit_reg_reg(A_OR,opsize,hregister,hregister)
- else
- gensub(longint(t^._low-last));
- last:=t^._low;
- emitjmp(C_Z,t^.statement);
- end
- else
- begin
- { it begins with the smallest label, if the value }
- { is even smaller then jump immediately to the }
- { ELSE-label }
- if first then
- begin
- { have we to ajust the first value ? }
- if (t^._low>get_min_value(left.resulttype.def)) then
- gensub(longint(t^._low));
- end
- else
- begin
- { if there is no unused label between the last and the }
- { present label then the lower limit can be checked }
- { immediately. else check the range in between: }
- { note: you can't use gensub() here because dec doesn't }
- { change the carry flag (needed for jmp_lxx) (JM) }
- emit_const_reg(A_SUB,opsize,longint(t^._low-last),hregister);
- emitjmp(jmp_le,elselabel);
- end;
- emit_const_reg(A_SUB,opsize,longint(t^._high-t^._low),hregister);
- emitjmp(jmp_lee,t^.statement);
- last:=t^._high;
- end;
- first:=false;
- if assigned(t^.greater) then
- genitem(t^.greater);
- end;
- begin
- { do we need to generate cmps? }
- if (with_sign and (min_label<0)) then
- genlinearcmplist(hp)
- else
- begin
- last:=0;
- first:=true;
- genitem(hp);
- cg.a_jmp_always(exprasmlist,elselabel);
- end;
- end;
- procedure genjumptable(hp : pcaserecord;min_,max_ : longint);
- var
- table : tasmlabel;
- last : TConstExprInt;
- href : treference;
- procedure genitem(t : pcaserecord);
- var
- i : longint;
- begin
- if assigned(t^.less) then
- genitem(t^.less);
- { fill possible hole }
- for i:=last+1 to t^._low-1 do
- jumpSegment.concat(Tai_const_symbol.Create(elselabel));
- for i:=t^._low to t^._high do
- jumpSegment.concat(Tai_const_symbol.Create(t^.statement));
- last:=t^._high;
- if assigned(t^.greater) then
- genitem(t^.greater);
- end;
- begin
- if not(jumptable_no_range) then
- begin
- emit_const_reg(A_CMP,opsize,longint(min_),hregister);
- { case expr less than min_ => goto elselabel }
- emitjmp(jmp_le,elselabel);
- emit_const_reg(A_CMP,opsize,longint(max_),hregister);
- emitjmp(jmp_gt,elselabel);
- end;
- getlabel(table);
- { extend with sign }
- if opsize=S_W then
- begin
- if with_sign then
- emit_reg_reg(A_MOVSX,S_WL,hregister,
- rg.makeregsize(hregister,OS_INT))
- else
- emit_reg_reg(A_MOVZX,S_WL,hregister,
- rg.makeregsize(hregister,OS_INT));
- hregister:=rg.makeregsize(hregister,OS_INT);
- end
- else if opsize=S_B then
- begin
- if with_sign then
- emit_reg_reg(A_MOVSX,S_BL,hregister,
- rg.makeregsize(hregister,OS_INT))
- else
- emit_reg_reg(A_MOVZX,S_BL,hregister,
- rg.makeregsize(hregister,OS_INT));
- hregister:=rg.makeregsize(hregister,OS_INT);
- end;
- reference_reset_symbol(href,table,0);
- href.offset:=(-longint(min_))*4;
- href.index:=hregister;
- href.scalefactor:=4;
- emit_ref(A_JMP,S_NO,href);
- { !!!!! generate tables
- if not(cs_littlesize in aktlocalswitches) then
- jumpSegment.concat(Taicpu.Op_const(A_ALIGN,S_NO,4));
- }
- jumpSegment.concat(Tai_label.Create(table));
- last:=min_;
- genitem(hp);
- { !!!!!!!
- if not(cs_littlesize in aktlocalswitches) then
- emit_const(A_ALIGN,S_NO,4);
- }
- end;
- var
- lv,hv,
- max_label: tconstexprint;
- labels : longint;
- max_linear_list : longint;
- otl, ofl: tasmlabel;
- isjump : boolean;
- {$ifdef Delphi}
- dist : cardinal;
- {$else Delphi}
- dist : dword;
- {$endif Delphi}
- begin
- getlabel(endlabel);
- getlabel(elselabel);
- if (cs_create_smart in aktmoduleswitches) then
- jumpsegment:=procinfo^.aktlocaldata
- else
- jumpsegment:=datasegment;
- with_sign:=is_signed(left.resulttype.def);
- if with_sign then
- begin
- jmp_gt:=C_G;
- jmp_le:=C_L;
- jmp_lee:=C_LE;
- end
- else
- begin
- jmp_gt:=C_A;
- jmp_le:=C_B;
- jmp_lee:=C_BE;
- end;
- rg.cleartempgen;
- { save current truelabel and falselabel }
- isjump:=false;
- if left.location.loc=LOC_JUMP then
- begin
- otl:=truelabel;
- getlabel(truelabel);
- ofl:=falselabel;
- getlabel(falselabel);
- isjump:=true;
- end;
- secondpass(left);
- { determines the size of the operand }
- opsize:=bytes2Sxx[left.resulttype.def.size];
- { copy the case expression to a register }
- location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),false);
- if opsize=S_Q then
- begin
- hregister:=left.location.registerlow;
- hregister2:=left.location.registerhigh;
- end
- else
- hregister:=left.location.register;
- if isjump then
- begin
- truelabel:=otl;
- falselabel:=ofl;
- end;
- { we need the min_label always to choose between }
- { cmps and subs/decs }
- min_label:=case_get_min(nodes);
- load_all_regvars(exprasmlist);
- { now generate the jumps }
- if opsize=S_Q then
- genlinearcmplist(nodes)
- else
- begin
- if cs_optimize in aktglobalswitches then
- begin
- { procedures are empirically passed on }
- { consumption can also be calculated }
- { but does it pay on the different }
- { processors? }
- { moreover can the size only be appro- }
- { ximated as it is not known if rel8, }
- { rel16 or rel32 jumps are used }
- max_label:=case_get_max(nodes);
- labels:=case_count_labels(nodes);
- { can we omit the range check of the jump table ? }
- getrange(left.resulttype.def,lv,hv);
- jumptable_no_range:=(lv=min_label) and (hv=max_label);
- { hack a little bit, because the range can be greater }
- { than the positive range of a longint }
- if (min_label<0) and (max_label>0) then
- begin
- {$ifdef Delphi}
- if min_label=longint($80000000) then
- dist:=Cardinal(max_label)+Cardinal($80000000)
- else
- dist:=Cardinal(max_label)+Cardinal(-min_label)
- {$else Delphi}
- if min_label=$80000000 then
- dist:=dword(max_label)+dword($80000000)
- else
- dist:=dword(max_label)+dword(-min_label)
- {$endif Delphi}
- end
- else
- dist:=max_label-min_label;
- { optimize for size ? }
- if cs_littlesize in aktglobalswitches then
- begin
- if (labels<=2) or
- ((max_label-min_label)<0) or
- ((max_label-min_label)>3*labels) then
- { a linear list is always smaller than a jump tree }
- genlinearlist(nodes)
- else
- { if the labels less or more a continuum then }
- genjumptable(nodes,min_label,max_label);
- end
- else
- begin
- if jumptable_no_range then
- max_linear_list:=4
- else
- max_linear_list:=2;
- { a jump table crashes the pipeline! }
- if aktoptprocessor=Class386 then
- inc(max_linear_list,3);
- if aktoptprocessor=ClassP5 then
- inc(max_linear_list,6);
- if aktoptprocessor>=ClassP6 then
- inc(max_linear_list,9);
- if (labels<=max_linear_list) then
- genlinearlist(nodes)
- else
- begin
- if (dist>4*cardinal(labels)) then
- begin
- if labels>16 then
- gentreejmp(nodes)
- else
- genlinearlist(nodes);
- end
- else
- genjumptable(nodes,min_label,max_label);
- end;
- end;
- end
- else
- { it's always not bad }
- genlinearlist(nodes);
- end;
- rg.ungetregister(exprasmlist,hregister);
- { now generate the instructions }
- hp:=right;
- while assigned(hp) do
- begin
- rg.cleartempgen;
- secondpass(tbinarynode(hp).right);
- { don't come back to case line }
- aktfilepos:=exprasmList.getlasttaifilepos^;
- load_all_regvars(exprasmlist);
- cg.a_jmp_always(exprasmlist,endlabel);
- hp:=tbinarynode(hp).left;
- end;
- cg.a_label(exprasmlist,elselabel);
- { ...and the else block }
- if assigned(elseblock) then
- begin
- rg.cleartempgen;
- secondpass(elseblock);
- load_all_regvars(exprasmlist);
- end;
- cg.a_label(exprasmlist,endlabel);
- end;
- begin
- {$ifndef TEST_GENERIC}
- cinnode:=ti386innode;
- {$endif}
- ccasenode:=ti386casenode;
- end.
- {
- $Log$
- Revision 1.36 2002-07-23 14:31:00 daniel
- * Added internal error when asked to generate code for 'if expr in []'
- Revision 1.35 2002/07/20 11:58:04 florian
- * types.pas renamed to defbase.pas because D6 contains a types
- unit so this would conflicts if D6 programms are compiled
- + Willamette/SSE2 instructions to assembler added
- Revision 1.34 2002/07/11 14:41:34 florian
- * start of the new generic parameter handling
- Revision 1.33 2002/07/06 20:27:26 carl
- + generic set handling
- Revision 1.32 2002/07/01 18:46:33 peter
- * internal linker
- * reorganized aasm layer
- Revision 1.31 2002/05/18 13:34:25 peter
- * readded missing revisions
- Revision 1.30 2002/05/16 19:46:52 carl
- + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
- + try to fix temp allocation (still in ifdef)
- + generic constructor calls
- + start of tassembler / tmodulebase class cleanup
- Revision 1.28 2002/05/13 19:54:38 peter
- * removed n386ld and n386util units
- * maybe_save/maybe_restore added instead of the old maybe_push
- Revision 1.27 2002/05/12 16:53:17 peter
- * moved entry and exitcode to ncgutil and cgobj
- * foreach gets extra argument for passing local data to the
- iterator function
- * -CR checks also class typecasts at runtime by changing them
- into as
- * fixed compiler to cycle with the -CR option
- * fixed stabs with elf writer, finally the global variables can
- be watched
- * removed a lot of routines from cga unit and replaced them by
- calls to cgobj
- * u32bit-s32bit updates for and,or,xor nodes. When one element is
- u32bit then the other is typecasted also to u32bit without giving
- a rangecheck warning/error.
- * fixed pascal calling method with reversing also the high tree in
- the parast, detected by tcalcst3 test
- Revision 1.26 2002/04/25 20:16:40 peter
- * moved more routines from cga/n386util
- Revision 1.25 2002/04/21 19:02:07 peter
- * removed newn and disposen nodes, the code is now directly
- inlined from pexpr
- * -an option that will write the secondpass nodes to the .s file, this
- requires EXTDEBUG define to actually write the info
- * fixed various internal errors and crashes due recent code changes
- Revision 1.24 2002/04/21 15:37:26 carl
- * changeregsize -> rg.makeregsize
- Revision 1.23 2002/04/19 15:39:35 peter
- * removed some more routines from cga
- * moved location_force_reg/mem to ncgutil
- * moved arrayconstructnode secondpass to ncgld
- Revision 1.22 2002/04/15 19:44:21 peter
- * fixed stackcheck that would be called recursively when a stack
- error was found
- * generic changeregsize(reg,size) for i386 register resizing
- * removed some more routines from cga unit
- * fixed returnvalue handling
- * fixed default stacksize of linux and go32v2, 8kb was a bit small :-)
- Revision 1.21 2002/04/02 17:11:36 peter
- * tlocation,treference update
- * LOC_CONSTANT added for better constant handling
- * secondadd splitted in multiple routines
- * location_force_reg added for loading a location to a register
- of a specified size
- * secondassignment parses now first the right and then the left node
- (this is compatible with Kylix). This saves a lot of push/pop especially
- with string operations
- * adapted some routines to use the new cg methods
- Revision 1.20 2002/03/31 20:26:39 jonas
- + a_loadfpu_* and a_loadmm_* methods in tcg
- * register allocation is now handled by a class and is mostly processor
- independent (+rgobj.pas and i386/rgcpu.pas)
- * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
- * some small improvements and fixes to the optimizer
- * some register allocation fixes
- * some fpuvaroffset fixes in the unary minus node
- * push/popusedregisters is now called rg.save/restoreusedregisters and
- (for i386) uses temps instead of push/pop's when using -Op3 (that code is
- also better optimizable)
- * fixed and optimized register saving/restoring for new/dispose nodes
- * LOC_FPU locations now also require their "register" field to be set to
- R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
- - list field removed of the tnode class because it's not used currently
- and can cause hard-to-find bugs
- }
|