123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- {
- $Id$
- Copyright (c) 1993-98 by Florian Klaempfl
- Type checking and register allocation for 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 tcset;
- interface
- uses
- tree;
- procedure firstsetelement(var p : ptree);
- procedure firstin(var p : ptree);
- procedure firstrange(var p : ptree);
- procedure firstcase(var p : ptree);
- implementation
- uses
- globtype,systems,
- cobjects,verbose,globals,
- symtable,aasm,types,
- hcodegen,htypechk,pass_1,
- tccnv
- {$ifdef i386}
- ,i386base
- ,tgeni386
- {$endif}
- {$ifdef m68k}
- ,m68k,tgen68k
- {$endif}
- ;
- {*****************************************************************************
- FirstSetElement
- *****************************************************************************}
- procedure firstsetelement(var p : ptree);
- begin
- firstpass(p^.left);
- if codegenerror then
- exit;
- if assigned(p^.right) then
- begin
- firstpass(p^.right);
- if codegenerror then
- exit;
- end;
- calcregisters(p,0,0,0);
- p^.resulttype:=p^.left^.resulttype;
- set_location(p^.location,p^.left^.location);
- end;
- {*****************************************************************************
- FirstIn
- *****************************************************************************}
- procedure firstin(var p : ptree);
- type
- byteset = set of byte;
- var
- t : ptree;
- begin
- p^.location.loc:=LOC_FLAGS;
- p^.resulttype:=booldef;
- firstpass(p^.right);
- if codegenerror then
- exit;
- { Convert array constructor first to set }
- if is_array_constructor(p^.right^.resulttype) then
- begin
- arrayconstructor_to_set(p^.right);
- firstpass(p^.right);
- if codegenerror then
- exit;
- end;
- if p^.right^.resulttype^.deftype<>setdef then
- CGMessage(sym_e_set_expected);
- firstpass(p^.left);
- if codegenerror then
- exit;
- { empty set then return false }
- if not assigned(psetdef(p^.right^.resulttype)^.setof) then
- begin
- t:=genordinalconstnode(0,booldef);
- disposetree(p);
- firstpass(t);
- p:=t;
- exit;
- end;
- { type conversion/check }
- p^.left:=gentypeconvnode(p^.left,psetdef(p^.right^.resulttype)^.setof);
- firstpass(p^.left);
- if codegenerror then
- exit;
- { constant evaulation }
- if (p^.left^.treetype=ordconstn) and (p^.right^.treetype=setconstn) then
- begin
- t:=genordinalconstnode(byte(p^.left^.value in byteset(p^.right^.value_set^)),booldef);
- disposetree(p);
- firstpass(t);
- p:=t;
- exit;
- end;
- left_right_max(p);
- { this is not allways true due to optimization }
- { but if we don't set this we get problems with optimizing self code }
- if psetdef(p^.right^.resulttype)^.settype<>smallset then
- procinfo.flags:=procinfo.flags or pi_do_call
- else
- begin
- { a smallset needs maybe an misc. register }
- if (p^.left^.treetype<>ordconstn) and
- not(p^.right^.location.loc in [LOC_CREGISTER,LOC_REGISTER]) and
- (p^.right^.registers32<1) then
- inc(p^.registers32);
- end;
- end;
- {*****************************************************************************
- FirstRange
- *****************************************************************************}
- procedure firstrange(var p : ptree);
- var
- ct : tconverttype;
- begin
- firstpass(p^.left);
- firstpass(p^.right);
- if codegenerror then
- exit;
- { both types must be compatible }
- if not(is_equal(p^.left^.resulttype,p^.right^.resulttype)) and
- (isconvertable(p^.left^.resulttype,p^.right^.resulttype,ct,ordconstn,false)=0) then
- CGMessage(type_e_mismatch);
- { Check if only when its a constant set }
- if (p^.left^.treetype=ordconstn) and (p^.right^.treetype=ordconstn) then
- begin
- { upper limit must be greater or equal than lower limit }
- { not if u32bit }
- if (p^.left^.value>p^.right^.value) and
- (( p^.left^.value<0) or (p^.right^.value>=0)) then
- CGMessage(cg_e_upper_lower_than_lower);
- end;
- left_right_max(p);
- p^.resulttype:=p^.left^.resulttype;
- set_location(p^.location,p^.left^.location);
- end;
- {*****************************************************************************
- FirstCase
- *****************************************************************************}
- procedure firstcase(var p : ptree);
- var
- old_t_times : longint;
- hp : ptree;
- begin
- { evalutes the case expression }
- cleartempgen;
- must_be_valid:=true;
- firstpass(p^.left);
- if codegenerror then
- exit;
- p^.registers32:=p^.left^.registers32;
- p^.registersfpu:=p^.left^.registersfpu;
- {$ifdef SUPPORT_MMX}
- p^.registersmmx:=p^.left^.registersmmx;
- {$endif SUPPORT_MMX}
- { walk through all instructions }
- { estimates the repeat of each instruction }
- old_t_times:=t_times;
- if not(cs_littlesize in aktglobalswitches) then
- begin
- t_times:=t_times div case_count_labels(p^.nodes);
- if t_times<1 then
- t_times:=1;
- end;
- { first case }
- hp:=p^.right;
- while assigned(hp) do
- begin
- cleartempgen;
- firstpass(hp^.right);
- { searchs max registers }
- if hp^.right^.registers32>p^.registers32 then
- p^.registers32:=hp^.right^.registers32;
- if hp^.right^.registersfpu>p^.registersfpu then
- p^.registersfpu:=hp^.right^.registersfpu;
- {$ifdef SUPPORT_MMX}
- if hp^.right^.registersmmx>p^.registersmmx then
- p^.registersmmx:=hp^.right^.registersmmx;
- {$endif SUPPORT_MMX}
- hp:=hp^.left;
- end;
- { may be handle else tree }
- if assigned(p^.elseblock) then
- begin
- cleartempgen;
- firstpass(p^.elseblock);
- if codegenerror then
- exit;
- if p^.registers32<p^.elseblock^.registers32 then
- p^.registers32:=p^.elseblock^.registers32;
- if p^.registersfpu<p^.elseblock^.registersfpu then
- p^.registersfpu:=p^.elseblock^.registersfpu;
- {$ifdef SUPPORT_MMX}
- if p^.registersmmx<p^.elseblock^.registersmmx then
- p^.registersmmx:=p^.elseblock^.registersmmx;
- {$endif SUPPORT_MMX}
- end;
- t_times:=old_t_times;
- { there is one register required for the case expression }
- if p^.registers32<1 then p^.registers32:=1;
- end;
- end.
- {
- $Log$
- Revision 1.10 1999-05-27 19:45:25 peter
- * removed oldasm
- * plabel -> pasmlabel
- * -a switches to source writing automaticly
- * assembler readers OOPed
- * asmsymbol automaticly external
- * jumptables and other label fixes for asm readers
- Revision 1.9 1999/05/01 13:24:58 peter
- * merged nasm compiler
- * old asm moved to oldasm/
- Revision 1.8 1999/04/14 15:00:13 peter
- * forgot firstpass after array->set conversion
- Revision 1.7 1999/03/02 18:22:36 peter
- * arrayconstructor convert for in
- Revision 1.6 1999/02/22 02:15:55 peter
- * updates for ag386bin
- Revision 1.5 1998/12/18 17:15:40 peter
- * added 'in []' support
- Revision 1.4 1998/12/11 00:03:58 peter
- + globtype,tokens,version unit splitted from globals
- Revision 1.3 1998/11/13 10:17:06 peter
- + constant eval for in
- Revision 1.2 1998/10/06 20:49:13 peter
- * m68k compiler compiles again
- Revision 1.1 1998/09/23 20:42:24 peter
- * splitted pass_1
- }
|