Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] View of /sml/trunk/src/MLRISC/hppa/instructions/hppaProps.sml
ViewVC logotype

View of /sml/trunk/src/MLRISC/hppa/instructions/hppaProps.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1003 - (download) (annotate)
Fri Dec 7 02:45:32 2001 UTC (18 years, 1 month ago) by george
File size: 11629 byte(s)
Changed the representation of instructions from being fully abstract
to being partially concrete. That is to say:

  from
	type instruction

  to
	type instr				(* machine instruction *)

	datatype instruction =
	    LIVE of {regs: C.cellset, spilled: C.cellset}
          | KILL of {regs: C.cellset, spilled: C.cellset}
          | COPYXXX of {k: CB.cellkind, dst: CB.cell list, src: CB.cell list}
          | ANNOTATION of {i: instruction, a: Annotations.annotation}
          | INSTR of instr

This makes the handling of certain special instructions that appear on
all architectures easier and uniform.

LIVE and KILL say that a list of registers are live or killed at the
program point where they appear. No spill code is generated when an
element of the 'regs' field is spilled, but the register is moved to
the 'spilled' (which is present, more for debugging than anything else).

LIVE replaces the (now deprecated) DEFFREG instruction on the alpha.
We used to generate:

	DEFFREG f1
	f1 := f2 + f3
        trapb

but now generate:

	f1 := f2 + f3
	trapb
	LIVE {regs=[f1,f2,f3], spilled=[]}

Furthermore, the DEFFREG (hack) required that all floating point instruction
use all registers mentioned in the instruction. Therefore f1 := f2 + f3,
defines f1 and uses [f1,f2,f3]! This hack is no longer required resulting
in a cleaner alpha implementation. (Hopefully, intel will not get rid of
this architecture).

COPYXXX is intended to replace the parallel COPY and FCOPY  available on
all the architectures. This will result in further simplification of the
register allocator that must be aware of them for coalescing purposes, and
will also simplify certain aspects of the machine description that provides
callbacks related to parallel copies.

ANNOTATION should be obvious, and now INSTR represents the honest to God
machine instruction set!

The <arch>/instructions/<arch>Instr.sml files define certain utility
functions for making porting easier -- essentially converting upper case
to lower case. All machine instructions (of type instr) are in upper case,
and the lower case form generates an MLRISC instruction. For example on
the alpha we have:

  datatype instr =
     LDA of {r:cell, b:cell, d:operand}
   | ...

  val lda : {r:cell, b:cell, d:operand} -> instruction
    ...

where lda is just (INSTR o LDA), etc.
(* hppaProps.sml
 *
 * COPYRIGHT (c) 1996 Bell Laboratories.
 *
 *)

functor HppaProps
   ( structure HppaInstr : HPPAINSTR
     structure MLTreeEval : MLTREE_EVAL where T = HppaInstr.T
     structure MLTreeHash : MLTREE_HASH where T = HppaInstr.T
    ) : INSN_PROPERTIES = 
struct
  structure I = HppaInstr
  structure C = HppaInstr.C
  structure CB = CellsBasis

  exception NegateConditional

  fun error msg = MLRiscErrorMsg.error("HppaProps",msg)

  datatype kind = IK_JUMP | IK_NOP | IK_INSTR | IK_COPY | IK_CALL 
                | IK_CALL_WITH_CUTS | IK_PHI | IK_SOURCE | IK_SINK
  datatype target = LABELLED of Label.label | FALLTHROUGH | ESCAPES

  val zeroR = Option.valOf(C.zeroReg CB.GP)
  val r31   = C.Reg CB.GP 31

   (*========================================================================
    *  Instruction Kinds
    *========================================================================*)
  (* Note: BLE and BL used to implement calls are not view as branches *)
  fun instrKind(I.ANNOTATION{i, ...}) = instrKind i
    | instrKind(I.INSTR instr) = 
      (case instr
	of (I.BCOND _) => IK_JUMP
	 | (I.BCONDI _) => IK_JUMP
	 | (I.BB _)     => IK_JUMP
	 | (I.B _)      => IK_JUMP
	 | (I.BE _)     => IK_JUMP
	 | (I.FBRANCH _)=> IK_JUMP
	 | (I.BV _)     => IK_JUMP
	 | (I.BLR _)    => IK_JUMP
	 | (I.NOP)      => IK_NOP
	 | (I.COPY _)   => IK_COPY
	 | (I.FCOPY _)  => IK_COPY
	 | (I.BL{cutsTo=_::_,...}) => IK_CALL_WITH_CUTS
	 | (I.BL  _)    => IK_CALL
	 | (I.BLE{cutsTo=_::_,...}) => IK_CALL_WITH_CUTS
	 | (I.BLE _)    => IK_CALL
	 | (I.PHI _)    => IK_PHI
	 | (I.SOURCE _) => IK_SOURCE
	 | (I.SINK _)   => IK_SINK
	 |  _	       => IK_INSTR)
    | instrKind _ = error "instrKind"

  fun moveInstr(I.INSTR(I.COPY _))   = true
    | moveInstr(I.INSTR(I.FCOPY _))  = true
    | moveInstr(I.LIVE _)	     = false
    | moveInstr(I.KILL _)	     = false
    | moveInstr(I.ANNOTATION{i,...}) = moveInstr i
    | moveInstr _ = false

  fun nop() = I.nop

   (*========================================================================
    *  Parallel Move
    *========================================================================*)
  fun moveTmpR(I.INSTR(I.COPY{tmp=SOME(I.Direct r), ...})) = SOME r
    | moveTmpR(I.INSTR(I.FCOPY{tmp=SOME(I.FDirect f), ...})) = SOME f
    | moveTmpR(I.ANNOTATION{i,...}) = moveTmpR i
    | moveTmpR _ = NONE

  fun moveDstSrc(I.INSTR(I.COPY{dst, src, ...})) = (dst, src)
    | moveDstSrc(I.INSTR(I.FCOPY{dst, src, ...})) = (dst, src)
    | moveDstSrc(I.ANNOTATION{i,...}) = moveDstSrc i
    | moveDstSrc _ = error "moveDstSrc"

   (*========================================================================
    *  Branches and Calls/Returns
    *========================================================================*)
  fun branchTargets(I.ANNOTATION{i,...}) = branchTargets i
    | branchTargets(I.INSTR instr) = 
      (case instr
       of (I.BCOND{t, ...})    => [LABELLED t, FALLTHROUGH]
	| (I.BCONDI{t, ...})   => [LABELLED t, FALLTHROUGH]
	| (I.BB{t, ...})       => [LABELLED t, FALLTHROUGH]
	| (I.B{lab, ...})      => [LABELLED lab]
	| (I.FBRANCH{t,...})   => [LABELLED t, FALLTHROUGH]
	| (I.BE{labs=[],...})  => [ESCAPES]
	| (I.BE{labs,...})     => map LABELLED labs
	| (I.BV{labs=[],...})  => [ESCAPES]
	| (I.BV{labs,...})     => map LABELLED labs
	| (I.BLR{labs,...})    => map LABELLED labs
	| (I.BL{cutsTo,...})   => FALLTHROUGH::map LABELLED cutsTo
	| (I.BLE{cutsTo,...})  => FALLTHROUGH::map LABELLED cutsTo
	| _ => error "branchTargets"
      (*easc*))
    | branchTargets  _ = error "branchTargets"

  fun jump label = I.b{lab=label,n=true}

  val immedRange = {lo= ~8192, hi=8191}
  fun loadImmed{immed,t} = 
      I.ldo{i=if #lo immedRange <= immed andalso immed <= #hi immedRange 
              then I.IMMED immed
              else I.LabExp(I.T.LI(I.T.I.fromInt(32,immed)),I.F),b=zeroR,t=t}
  fun loadOperand{opn,t} = I.ldo{i=opn,b=zeroR,t=t}

  fun setTargets(I.INSTR(I.BCOND{cmp,bc,r1,r2,t,f,n,nop}), [F,T]) =
          I.bcond{cmp=cmp,bc=bc,r1=r1,r2=r2,t=T,f=F,n=n,nop=nop}
    | setTargets(I.INSTR(I.BCONDI{cmpi,bc,i,r2,t,f,n,nop=nop}), [F,T]) =
          I.bcondi{cmpi=cmpi,bc=bc,i=i,r2=r2,t=T,f=F,n=n,nop=nop}
    | setTargets(I.INSTR(I.BB{bc,r,p,t,f,n,nop}), [F,T]) =
          I.bb{bc=bc,r=r,p=p,t=T,f=F,n=n,nop=nop}
    | setTargets(I.INSTR(I.B{n,...}), [L]) = I.b{lab=L,n=n}
    | setTargets(I.INSTR(I.FBRANCH{cc,fmt,n,long,f1,f2,...}), [F,T]) =
          I.fbranch{cc=cc,fmt=fmt,t=T,f=F,n=n,long=long,f1=f1,f2=f2} 
    | setTargets(I.INSTR(I.BV{x,b,n,...}),labels) = I.bv{x=x,b=b,labs=labels,n=n}
    | setTargets(I.INSTR(I.BE{b,d,n,sr,...}),labs) = I.be{b=b,d=d,n=n,sr=sr,labs=labs}
    | setTargets(I.INSTR(I.BLR{x,t,n,...}),labels) = I.blr{x=x,t=t,labs=labels,n=n}
    | setTargets(I.ANNOTATION{i,a},labels) =
          I.ANNOTATION{i=setTargets(i,labels),a=a}
    | setTargets(i,_) = i

  fun negateConditional br = let
    fun revFcond I.?    = I.!?
      | revFcond I.!<=> = I.<=>
      | revFcond I.==   = I.!=
      | revFcond I.?=   = I.!?=
      | revFcond I.!<>  = I.<>
      | revFcond I.!?>= = I.?>=
      | revFcond I.<    = I.!<
      | revFcond I.?<   = I.!?<
      | revFcond I.!>=  = I.>=
      | revFcond I.!?>  = I.?>
      | revFcond I.<=   = I.!<=
      | revFcond I.?<=  = I.!?<=
      | revFcond I.!>   = I.>
      | revFcond I.!?<= = I.?<=
      | revFcond I.>    = I.!>
      | revFcond I.?>   = I.!?>
      | revFcond I.!<=  = I.<=
      | revFcond I.!?<  = I.?<
      | revFcond I.>=   = I.!>=
      | revFcond I.?>=  = I.!?>=
      | revFcond I.!<   = I.<
      | revFcond I.!?=  = I.?=
      | revFcond I.<>   = I.!<>
      | revFcond I.!=   = I.==
      | revFcond I.!?   = I.?
      | revFcond I.<=>  = I.!<=>
      | revFcond _      = error "revFcond"
  in
    case br of 
      I.INSTR(I.BCOND{cmp,bc,r1,r2,t,f,n,nop}) => 
         I.bcond{bc=bc, r1=r1, r2=r2, t=t, f=f, n=n, nop=nop,
		 cmp=case cmp of I.COMBT => I.COMBF | I.COMBF => I.COMBT}
    | I.INSTR(I.BCONDI{cmpi,bc,i,r2,t,f,n,nop}) =>
        I.bcondi{bc=bc, i=i, r2=r2, t=t, f=f, n=n, nop=nop,
		 cmpi=case cmpi of I.COMIBT => I.COMIBF | I.COMIBF => I.COMIBT}
    | I.INSTR(I.BB{bc,r,p,t,f,n,nop}) => 
         I.bb{bc=case bc of I.BSET => I.BCLR | I.BCLR => I.BSET, 
              r=r,p=p,t=t,f=f,n=n,nop=nop}
    | I.INSTR(I.FBRANCH{cc,fmt,f1,f2,t,f,n,long}) =>
        I.fbranch{cc=revFcond cc,fmt=fmt,f1=f1,f2=f2,t=t,f=f,n=n,long=long} 
    | I.ANNOTATION{i,a} => I.ANNOTATION{i=negateConditional i,a=a}
    | _ => raise NegateConditional
  end

  (*========================================================================
   *  Equality and hashing for operands
   *========================================================================*)
   fun hashFieldSel I.F = 0w0
     | hashFieldSel I.S = 0w1
     | hashFieldSel I.D = 0w2
     | hashFieldSel I.R = 0w3
     | hashFieldSel I.T = 0w4
     | hashFieldSel I.P = 0w5
   fun hashOpn(I.IMMED i) = Word.fromInt i
     | hashOpn(I.LabExp(l,f)) = MLTreeHash.hash l + hashFieldSel f
     | hashOpn(I.HILabExp(l,f)) = MLTreeHash.hash l + hashFieldSel f + 0w10000
     | hashOpn(I.LOLabExp(l,f)) = MLTreeHash.hash l + hashFieldSel f + 0w20000
     | hashOpn(I.REG r) = CB.hashCell r
   fun eqOpn(I.IMMED i,I.IMMED j) = i = j
     | eqOpn(I.REG x,I.REG y) = CB.sameColor(x,y)
     | eqOpn(I.LabExp(a,b),I.LabExp(c,d)) = 
          b = d andalso MLTreeEval.==(a,c)
     | eqOpn(I.HILabExp(a,b),I.HILabExp(c,d)) = 
          b = d andalso MLTreeEval.==(a,c)
     | eqOpn(I.LOLabExp(a,b),I.LOLabExp(c,d)) = 
          b = d andalso MLTreeEval.==(a,c)
     | eqOpn _ = false
   

  (*========================================================================
   *  Definition and use (for register allocation mainly)
   *========================================================================*)
  fun defUseR instr = let
    fun hppaDU instr = let
      fun trap((I.ADDO | I.SUBO | I.SH1ADDO), d, u) = (d, u)
	| trap(_, d, u) = (d, u)
      fun trapi((I.ADDIO | I.SUBIO), d, u) = (d, u)
	| trapi(_, d, u) = (d, u)
    in
      case instr
       of I.STORE {b, r,...}          => ([],  [b,r])
	| I.LOAD {l, r1, r2, t, ...}  => ([t], [r1,r2])
	| I.LOADI {li, r, t, ...}     => ([t], [r])
	| I.ARITH {a, r1, r2, t, ...} => trap(a, [t], [r1,r2])
	| I.ARITHI {ai, r, t, ...}    => trapi(ai, [t], [r])
	| I.COMCLR_LDO{r1, r2, b, t1, t2, ...}=> 
	    if CB.sameColor(t1,t2) then ([t1], [b, r1, r2])
	    else ([t1, t2], [b, r1, r2, t2])
	| I.COMICLR_LDO{i1, r2, b, t1, t2, ...}=> 
	    if CB.sameColor(t1,t2) then ([t1], [b, r2])
	    else ([t1, t2], [b, r2, t2])
	| I.SHIFTV {r, t, ...}        => ([t], [r])
	| I.SHIFT {r, t, ...}         => ([t], [r])
	| I.BCOND {r1, r2, ...}       => ([],  [r1,r2])
	| I.BCONDI {r2, ...} 	    => ([],  [r2])
	| I.BB {r, ...} 	            => ([],  [r])
	| I.BV {x, b, ...}	    => ([],  [x,b])
	| I.BE {b, ...}	            => ([],  [b])
	| I.BLR{x, t, ...}            => ([t], [x])
	| I.BL{defs, uses, ...}       => (C.getReg defs, C.getReg uses)
	| I.BLE{t, b, defs, uses, ...}=>
	      (r31 :: t :: C.getReg defs, b :: C.getReg uses)
	| I.LDIL{i, t}		    => ([t], [])
	| I.LDO{b, t, ...}	    => ([t], [b])
	| I.COPY{dst, src, tmp=SOME(I.Direct r), ...} => (r::dst, src)
	| I.COPY{dst, src, ...}       => (dst, src)
	| I.MTCTL{r, t}		    => ([],  [r])
	| I.FSTORE {b, ...}	    => ([],  [b])
	| I.FSTOREX {b, x, ...}  	    => ([],  [b,x])
	| I.FLOAD {b, ...}	    => ([],  [b])
	| I.FLOADX{b, x, ...} 	    => ([],  [b,x])
	| _   => ([],[])
    end
  in
      case instr
       of I.ANNOTATION{i, ...} => defUseR i
	| I.LIVE{regs, ...} => ([], C.getReg regs)
	| I.KILL{regs, ...} => (C.getReg regs, [])
	| I.INSTR(i) => hppaDU(i)
	| _ => error "defUseR"
  end

  fun defUseF instr = let
    fun hppaDU instr = 
      case instr
	of I.FSTORE {r, ...}  	   => ([],  [r])
	 | I.FSTOREX{r, ...}	   => ([],  [r])
	 | I.FLOAD{t, ...}	   => ([t], [])
	 | I.FLOADX{t, ...}	   => ([t], [])
	 | I.FARITH {r1, r2, t, ...} => ([t], [r1,r2])
	 | I.FUNARY {f, t, ...}      => ([t], [f])
	 | I.FCNV {f, t, ...}        => ([t], [f])
	 | I.FBRANCH{f1, f2,...}	   => ([],  [f1, f2])
	 | I.BL{defs, uses, ...}     => (C.getFreg defs, C.getFreg uses)
	 | I.BLE{defs, uses, ...}    => (C.getFreg defs, C.getFreg uses)
	 | I.FCOPY{dst, src, tmp=SOME(I.FDirect f), ...} => (f::dst, src)
	 | I.FCOPY{dst, src, ...}    => (dst, src)
	 | _ => ([],[])
  in 
      case instr
       of I.ANNOTATION{i, ...} => defUseF i
	| I.INSTR(i) => hppaDU(i)
	| I.LIVE{regs, ...} => ([], C.getFreg regs)
	| I.KILL{regs, ...} => (C.getFreg regs, [])
	| _ => error "defUseR"
  end

  fun defUse CB.GP = defUseR
    | defUse CB.FP = defUseF
    | defUse _ = error "defUse"

  (*========================================================================
   *  Annotations 
   *========================================================================*)
  fun getAnnotations(I.ANNOTATION{i,a}) = 
       let val (i,an) = getAnnotations i in (i,a::an) end
    | getAnnotations i = (i,[])
  fun annotate(i,a) = I.ANNOTATION{i=i,a=a}

  (*========================================================================
   *  Replicate an instruction
   *========================================================================*)
  fun replicate(I.ANNOTATION{i,a}) = I.ANNOTATION{i=replicate i,a=a}
    | replicate(I.INSTR(I.COPY{tmp=SOME _, dst, src, impl})) = 
        I.copy{tmp=SOME(I.Direct(C.newReg())), dst=dst, src=src, impl=ref NONE}
    | replicate(I.INSTR(I.FCOPY{tmp=SOME _, dst, src, impl})) = 
        I.fcopy{tmp=SOME(I.FDirect(C.newFreg())), 
                dst=dst, src=src, impl=ref NONE}
    | replicate i = i
end




root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0