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

View of /sml/trunk/src/MLRISC/instructions/labelExp.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 409 - (download) (annotate)
Fri Sep 3 00:21:52 1999 UTC (19 years, 11 months ago) by monnier
File size: 3740 byte(s)
Initial revision
(* labelExp.sml -- expressions involving labels
 *
 * COPYRIGHT (c) 1995 AT&T Bell Laboratories.
 *
 *)
signature LABELEXP = sig
  datatype labexp = 
      LABEL of Label.label
    | CONST of int
    | PLUS of labexp * labexp
    | MINUS of labexp * labexp
    | MULT of labexp * labexp
    | DIV of labexp * labexp
    | LSHIFT of labexp * word
    | RSHIFT of labexp * word
    | AND of labexp * word
    | OR of labexp * word

  val valueOf : labexp -> int
  val toString : labexp -> string
  val hash    : labexp -> word
  val ==      : labexp * labexp -> bool
end

structure LabelExp = struct
  datatype labexp = 
      LABEL of Label.label
    | CONST of int
    | PLUS of labexp * labexp
    | MINUS of labexp * labexp
    | MULT of labexp * labexp
    | DIV of labexp * labexp
    | LSHIFT of labexp * word
    | RSHIFT of labexp * word
    | AND of labexp * word
    | OR of labexp * word

  val itow = Word.fromInt
  val wtoi = Word.toIntX

  fun valueOf(LABEL lab) = Label.addrOf lab
    | valueOf(CONST i) = i
    | valueOf(PLUS(lexp1, lexp2)) = valueOf(lexp1) + valueOf(lexp2)
    | valueOf(MINUS(lexp1, lexp2)) = valueOf(lexp1) - valueOf(lexp2)
    | valueOf(MULT(lexp1, lexp2)) = valueOf(lexp1) * valueOf(lexp2)
    | valueOf(DIV(lexp1, lexp2)) = valueOf(lexp1) div valueOf(lexp2)
    | valueOf(LSHIFT(lexp, cnt)) = wtoi(Word.<<(wValueOf lexp, cnt))
    | valueOf(RSHIFT(lexp, cnt)) = wtoi(Word.>>(wValueOf lexp, cnt))
    | valueOf(AND(lexp, mask)) = wtoi(Word.andb(wValueOf lexp, mask))
    | valueOf(OR(lexp, mask)) = wtoi(Word.orb(wValueOf lexp, mask))

  and wValueOf lexp = itow(valueOf lexp)

  (* This module should be parameterised, in order to generate
   * target label expressions for assembly code purposes.
   *)
  fun parenthesize str = "(" ^ str ^ ")"

  fun pToString(lexp as LABEL _) = toString lexp
    | pToString(lexp as CONST _) = toString lexp
    | pToString lexp = parenthesize(toString lexp)

  and toString(LABEL lab) = Label.nameOf lab 
    | toString(CONST i) = if i < 0 then "-"^Int.toString(~i) else Int.toString i
    | toString(PLUS(lexp1, lexp2)) =  pToString lexp1 ^ "+" ^ pToString lexp2
    | toString(MINUS(lexp1, lexp2)) = pToString lexp1 ^ "-" ^ pToString lexp2
    | toString(MULT(lexp1, lexp2)) = pToString lexp1 ^ "*" ^ pToString lexp2
    | toString(DIV(lexp1, lexp2)) = pToString lexp1 ^ "/" ^ pToString lexp2
    | toString(LSHIFT(lexp, cnt)) = pToString lexp ^ "<<" ^ Word.toString cnt
    | toString(RSHIFT(lexp, cnt)) = pToString lexp ^ ">>" ^ Word.toString cnt
    | toString(AND(lexp, mask)) = pToString lexp ^ "&" ^ Word.toString mask
    | toString(OR(lexp, mask)) = pToString lexp ^ "|" ^ Word.toString mask

  fun hash(LABEL(Label.Label{id,...})) = Word.fromInt id
    | hash(CONST i) = Word.fromInt i
    | hash(PLUS(a,b)) = hash a + hash b + 0w12311
    | hash(MINUS(a,b)) = 0w1232 + hash a + hash b + 0w8834
    | hash(MULT(a,b)) = 0w123123 + hash a + hash b + 0w1714
    | hash(DIV(a,b)) = hash a + hash b + 0w1999
    | hash(LSHIFT(a,c)) = hash a + c + 0w1333
    | hash(RSHIFT(a,c)) = hash a + c + 0w6788
    | hash(AND(a,m)) = hash a + m + 0w444
    | hash(OR(a,m)) = hash a + m + 0w777

  fun ==(LABEL(Label.Label{id=x,...}),LABEL(Label.Label{id=y,...})) = x = y
    | ==(CONST i,CONST j) = i = j
    | ==(PLUS(a,b),PLUS(c,d)) = ==(a,b) andalso ==(c,d)
    | ==(MINUS(a,b),MINUS(c,d)) = ==(a,b) andalso ==(c,d)
    | ==(MULT(a,b),MULT(c,d)) = ==(a,b) andalso ==(c,d)
    | ==(DIV(a,b),DIV(c,d)) = ==(a,b) andalso ==(c,d)
    | ==(LSHIFT(a,b),LSHIFT(c,d)) = b = d andalso ==(a,c)
    | ==(RSHIFT(a,b),RSHIFT(c,d)) = b = d andalso ==(a,c)
    | ==(AND(a,b),AND(c,d)) = b = d andalso ==(a,c)
    | ==(OR(a,b),OR(c,d)) = b = d andalso ==(a,c)
    | == _ = false
end

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