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/sparc/instructions/sparcProps.sml
ViewVC logotype

View of /sml/trunk/src/MLRISC/sparc/instructions/sparcProps.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: 10254 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.
functor SparcProps
  (structure SparcInstr : SPARCINSTR
   structure MLTreeEval : MLTREE_EVAL where T = SparcInstr.T
   structure MLTreeHash : MLTREE_HASH where T = SparcInstr.T
   ) : INSN_PROPERTIES =
struct
  structure I = SparcInstr
  structure C = I.C
  structure T = I.T 
  structure CB = CellsBasis

  exception NegateConditional

  fun error msg = MLRiscErrorMsg.error("SparcProps",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 r15   = C.Reg CB.GP 15
  val r31   = C.Reg CB.GP 31

  (*========================================================================
   *  Instruction Kinds
   *========================================================================*)
  fun instrKind(I.ANNOTATION{i, ...}) = instrKind i
    | instrKind(I.INSTR instr) = 
      (case instr
       of (I.Bicc _)  => IK_JUMP
	| (I.FBfcc _) => IK_JUMP
	| (I.JMP _)   => IK_JUMP
	| (I.RET _)   => IK_JUMP
	| (I.BR _)    => IK_JUMP
	| (I.BP _)    => IK_JUMP
	| (I.COPY _)  => IK_COPY
	| (I.FCOPY _) => IK_COPY
	| (I.CALL{cutsTo=_::_,...})  => IK_CALL_WITH_CUTS
	| (I.CALL _)  => IK_CALL
	| (I.JMPL{cutsTo=_::_,...})  => IK_CALL_WITH_CUTS
	| (I.JMPL _)  => IK_CALL
	| (I.PHI _)    => IK_PHI
	| (I.SOURCE _) => IK_SOURCE
	| (I.SINK _)   => IK_SINK
	|  _          => IK_INSTR
      (*esac*))
    | instrKind _ = error "instrKind"

  fun branchTargets(I.ANNOTATION{i,...}) = branchTargets i
    | branchTargets(I.INSTR(instr)) = 
      (case instr 
	of (I.Bicc{b=I.BA,label,...}) => [LABELLED label]
	 | (I.Bicc{label,...}) => [LABELLED label, FALLTHROUGH] 
	 | (I.FBfcc{b=I.FBA,label,...}) => [LABELLED label]
	 | (I.FBfcc{label,...}) => [LABELLED label, FALLTHROUGH]
	 | (I.BR{label,...}) => [LABELLED label, FALLTHROUGH]
	 | (I.BP{label,...}) => [LABELLED label, FALLTHROUGH]
	 | (I.JMP{labs=[],...}) => [ESCAPES] 
	 | (I.RET _)   => [ESCAPES]
	 | (I.JMP{labs,...})    => map LABELLED labs
	 | (I.CALL{cutsTo,...}) => FALLTHROUGH::map LABELLED cutsTo
	 | (I.JMPL{cutsTo,...}) => FALLTHROUGH::map LABELLED cutsTo
	 |  _ => error "branchTargets"
      (*esac*))
    | branchTargets _  = error "branchTargets"

  fun setTargets(I.INSTR(I.Bicc{b=I.BA,a,nop,...}),[L]) = 
          I.bicc{b=I.BA,a=a,label=L,nop=nop}
    | setTargets(I.INSTR(I.Bicc{b,a,nop,...}),[F,T]) = 
          I.bicc{b=b,a=a,label=T,nop=nop}
    | setTargets(I.INSTR(I.FBfcc{b,a,nop,...}),[F,T]) = 
          I.fbfcc{b=b,a=a,label=T,nop=nop}
    | setTargets(I.INSTR(I.BR{rcond,p,r,a,nop,...}),[F,T]) = 
          I.br{rcond=rcond,p=p,r=r,a=a,label=T,nop=nop}
    | setTargets(I.INSTR(I.BP{b,cc,p,a,nop,...}),[F,T]) = 
          I.bp{b=b,cc=cc,p=p,a=a,label=T,nop=nop}
    | setTargets(I.INSTR(I.JMP{r,i,nop,...}),labels) = 
          I.jmp{r=r,i=i,labs=labels,nop=nop}
    | setTargets(I.ANNOTATION{i,a},labs) = 
          I.ANNOTATION{i=setTargets(i,labs),a=a}
    | setTargets(i,_) = i

   fun revCond I.BA = I.BN
     | revCond I.BN = I.BA
     | revCond I.BNE = I.BE
     | revCond I.BE  = I.BNE
     | revCond I.BG  = I.BLE
     | revCond I.BLE = I.BG
     | revCond I.BGE = I.BL
     | revCond I.BL  = I.BGE
     | revCond I.BGU = I.BLEU
     | revCond I.BLEU = I.BGU
     | revCond I.BCC  = I.BCS
     | revCond I.BCS  = I.BCC
     | revCond I.BPOS = I.BNEG
     | revCond I.BNEG = I.BPOS
     | revCond I.BVC  = I.BVS
     | revCond I.BVS  = I.BVC

   fun revFcond I.FBA   = I.FBN
     | revFcond I.FBN   = I.FBA
     | revFcond I.FBU   = I.FBO
     | revFcond I.FBG   = I.FBULE
     | revFcond I.FBUG  = I.FBLE
     | revFcond I.FBL   = I.FBUGE
     | revFcond I.FBUL  = I.FBGE
     | revFcond I.FBLG  = I.FBUE
     | revFcond I.FBNE  = I.FBE
     | revFcond I.FBE   = I.FBNE
     | revFcond I.FBUE  = I.FBLG
     | revFcond I.FBGE  = I.FBUL
     | revFcond I.FBUGE = I.FBL
     | revFcond I.FBLE  = I.FBUG
     | revFcond I.FBULE = I.FBG
     | revFcond I.FBO   = I.FBU

  fun revRcond I.RZ   = I.RNZ
    | revRcond I.RLEZ = I.RGZ
    | revRcond I.RLZ  = I.RGEZ
    | revRcond I.RNZ  = I.RZ
    | revRcond I.RGZ  = I.RLEZ
    | revRcond I.RGEZ = I.RLZ

  fun revP I.PT = I.PN
    | revP I.PN = I.PT

  fun negateConditional(I.INSTR(I.Bicc{b,a,label,nop})) =
         I.bicc{b=revCond b,a=a,label=label,nop=nop}
    | negateConditional(I.INSTR(I.FBfcc{b,a,label,nop})) =
         I.fbfcc{b=revFcond b,a=a,label=label,nop=nop} 
    | negateConditional(I.INSTR(I.BR{p,r,rcond,a,label,nop})) =
         I.br{p=revP p,a=a,r=r,rcond=revRcond rcond,label=label,nop=nop} 
    | negateConditional(I.INSTR(I.BP{b,cc,p,a,label,nop})) =
         I.bp{p=revP p,a=a,b=revCond b,cc=cc,label=label,nop=nop} 
    | negateConditional(I.ANNOTATION{i,a}) = 
         I.ANNOTATION{i=negateConditional i,a=a}
    | negateConditional _ = raise NegateConditional

  fun jump label = I.bicc{b=I.BA,a=true,label=label,nop=true}

  val immedRange = {lo= ~4096, hi = 4095}

  fun loadImmed{immed,t} = 
      I.arith{a=I.OR,r=zeroR,i=
              if #lo immedRange <= immed andalso immed <= #hi immedRange 
              then I.IMMED immed else I.LAB(T.LI(IntInf.fromInt immed)),d=t}
  fun loadOperand{opn, t} = I.arith{a=I.OR,r=zeroR,i=opn, d=t}

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

  fun nop() = I.sethi{d=zeroR, i=0}

  (*========================================================================
   *  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"

  (*========================================================================
   *  Equality and hashing
   *========================================================================*)
   fun hashOpn(I.REG r) = CB.hashCell r
     | hashOpn(I.IMMED i) = Word.fromInt i
     | hashOpn(I.LAB l) = MLTreeHash.hash l
     | hashOpn(I.LO l) = MLTreeHash.hash l
     | hashOpn(I.HI l) = MLTreeHash.hash l
   fun eqOpn(I.REG a,I.REG b) = CB.sameColor(a,b)
     | eqOpn(I.IMMED a,I.IMMED b) = a = b
     | eqOpn(I.LAB a,I.LAB b) = MLTreeEval.==(a,b)
     | eqOpn(I.LO a,I.LO b) = MLTreeEval.==(a,b)
     | eqOpn(I.HI a,I.HI b) = MLTreeEval.==(a,b)
     | eqOpn _ = false

  fun defUseR instr = let
    fun sparcDU instr =
      let
	 fun oper (I.REG r,def,use) = (def,r::use)
	   | oper (_,def,use)       = (def,use)
      in
	  case instr of
	    (* load/store instructions *)
	    I.LOAD {r,d,i,...} => oper(i,[d],[r])
	  | I.STORE {r,d,i,...} => oper(i,[],[r,d])
	  | I.FLOAD {r,d,i,...} => oper(i,[],[r])
	  | I.FSTORE {r,d,i,...} => oper(i,[],[r])
	  | I.SETHI {d,...} => ([d],[])
	  | I.ARITH {r,i,d,...} => oper(i,[d],[r])
	  | I.SHIFT {r,i,d,...} => oper(i,[d],[r])
	  | I.JMPL{defs,uses,d,r,i,...} => 
	       oper(i,d:: C.getReg defs,r:: C.getReg uses)
	  | I.BR{r,...} => ([],[r])
	  | I.MOVicc{i,d,...} => oper(i,[d],[d])
	  | I.MOVfcc{i,d,...} => oper(i,[d],[d])
	  | I.MOVR{r,i,d,...} => oper(i,[d],[r,d])
	  | I.CALL{defs,uses,...} => (r15 :: C.getReg defs, C.getReg uses)
	  | I.JMP{r,i,...} => oper(i,[],[r])
	  | I.RET{leaf=false,...} => ([],[r31])
	  | I.RET{leaf=true,...} => ([],[r15])
	  | I.COPY{src,dst,tmp=SOME(I.Direct r),...} => (r::dst,src)
	  | I.COPY{src,dst,...} => (dst,src)
	  | I.SAVE{r,i,d} => oper(i,[d],[r])
	  | I.RESTORE{r,i,d} => oper(i,[d],[r])
	  | I.Ticc{r,i,...} => oper(i,[],[r]) 
	  | I.RDY{d,...} => ([d],[]) 
	  | I.WRY{r,i,...} => oper(i,[],[r]) 
	  | _ => ([],[])  
      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) => sparcDU(i)
	| _ => error "defUseR"
  end

  (* Use of FP registers *)
  fun defUseF instr = let
    fun sparcDU instr =
     (case instr of
        I.FLOAD{r,d,i,...} => ([d],[])
      | I.FSTORE{r,d,i,...} => ([],[d])
      | I.FPop1{r,d,...} => ([d],[r])
      | I.FPop2{r1,r2,d,...} => ([d],[r1,r2])
      | I.FCMP{r1,r2,...} => ([],[r1,r2])
      | I.JMPL{defs,uses,...} => (C.getFreg defs,C.getFreg uses)
      | I.CALL{defs,uses,...} => (C.getFreg defs,C.getFreg uses)
      | I.FMOVicc{r,d,...} => ([d],[r,d])
      | I.FMOVfcc{r,d,...} => ([d],[r,d])
      | I.FCOPY{src,dst,tmp=SOME(I.FDirect r),...} => (r::dst,src)
      | I.FCOPY{src,dst,...} => (dst,src)
      | _ => ([],[])
     (*esac*))
  in
     case instr
      of I.ANNOTATION{i, ...} => defUseF i
       | I.LIVE{regs, ...} => ([], C.getFreg regs)
       | I.KILL{regs, ...} => (C.getFreg regs, [])
       | I.INSTR(i) => sparcDU(i)
       | _ => error "defUseF"
  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