Annotation of researchv10no/cmd/sml/src/ns32/ns32ascode.sml, revision 1.1.1.1

1.1       root        1: (* Copyright 1989 by AT&T Bell Laboratories *)
                      2: structure NS32Assem = struct val outfile = ref std_out end
                      3: 
                      4: structure NS32AsCode  : NS32CODER = struct
                      5: 
                      6: open System.Tags NS32Assem
                      7: 
                      8: val offset = ref 0
                      9: 
                     10: type Label = string
                     11: 
                     12: local val i = ref 0 in
                     13: fun newlabel () = (i := !i + 1; "L" ^ makestring (!i))
                     14: end
                     15: 
                     16: fun itoa (i:int) = if i < 0 then "-" ^ makestring (~i)
                     17:                   else makestring i
                     18: 
                     19: datatype Register = Genreg of int
                     20:                  | FloatReg of int
                     21:                  | FP
                     22:                  | SP
                     23:                  | SB
                     24:                  | PC
                     25: 
                     26: val r0 = Genreg 0
                     27: val r1 = Genreg 1
                     28: val r2 = Genreg 2
                     29: val r3 = Genreg 3
                     30: val r4 = Genreg 4
                     31: val r5 = Genreg 5
                     32: val r6 = Genreg 6
                     33: val r7 = Genreg 7
                     34: val fp = FP
                     35: val sp = SP
                     36: val sb = SB
                     37: val pc = PC
                     38: val fp0 = FloatReg 0
                     39: 
                     40: datatype Size = Byte | Word | Long
                     41: 
                     42: datatype EA = Direct of Register
                     43:            | Topofstack
                     44:            | Displace of int * Register
                     45:            | Immed of int
                     46:            | Immedlab of Label
                     47:            | Abs of int
                     48:            | OffAddress of Label * int
                     49:            | Index of EA * Register * Size
                     50: 
                     51: fun address lab = OffAddress(lab,0)    (* old Address style *)
                     52: 
                     53: fun emit s = output (!outfile) s
                     54: (* fun emit s = (output std_out s; output (!outfile) s)        DEBUG *)
                     55: 
                     56: fun newline () = (emit "\n"   (*  ; emit(makestring(!offset)); emit "\t" *) )
                     57: 
                     58: fun emitopc opc = (emit "\t"; emit opc; emit "\t")
                     59: 
                     60: fun emitreg (Genreg r) = emit ("r" ^ itoa r)
                     61:   | emitreg (FloatReg r) = emit ("f" ^ itoa r)
                     62:   | emitreg (FP) = emit "fp"
                     63:   | emitreg (SP) = emit "sp"
                     64:   | emitreg (SB) = emit "sb"
                     65:   | emitreg (PC) = emit "pc"
                     66: 
                     67: fun emitsize (Byte) = emit "B"
                     68:   | emitsize (Word) = emit "W"
                     69:   | emitsize (Long) = emit "L"
                     70: 
                     71: fun emitarg (Direct r) = emitreg r
                     72:   | emitarg (Immed i) = emit ("$" ^ itoa i)
                     73:   | emitarg (Displace(0,r)) = (emit "("; emitreg r; emit ")")
                     74:   | emitarg (Displace(i,r)) = (emit (itoa i); emit "("; emitreg r; emit ")")
                     75:   | emitarg (OffAddress(lab,i)) = (emit lab;
                     76:                                   if i>0 then emit ("+" ^ makestring i)
                     77:                                   else if i<0 then emit (itoa i)
                     78:                                   else ())
                     79:   | emitarg (Index(ea,r,sz)) = (emitarg ea; emit "["; emitreg r; emit ":";
                     80:                                emitsize sz; emit "]") 
                     81:   | emitarg (Topofstack) = (emit "TOS")
                     82:   | emitarg (Immedlab lab) = (emit "BOGUS:"; emit lab)
                     83:   | emitarg (Abs n) = emit ("BOGUSABS:" ^ itoa(n))
                     84: 
                     85: fun emit1arg (a) = (emitarg a; newline())
                     86: 
                     87: fun emit2arg (a,b) = (emitarg a; emit ","; emitarg b; newline())
                     88: 
                     89: fun align () = emit "\t.align\t2\n"
                     90: 
                     91: local val p = makestring power_tags
                     92:       val t = makestring tag_backptr
                     93: in
                     94: fun mark () = let val lab = newlabel()
                     95:              in  emit lab;
                     96:                  emit ":\t.long\t((";
                     97:                  emit lab;
                     98:                  emit "-base)/4+1)*";   (* STRING dependency *)
                     99:                  emit p;
                    100:                  emit "+";
                    101:                  emit t;
                    102:                  emit "\n"
                    103:              end
                    104: end
                    105: 
                    106: fun define lab = (emit lab; emit ":\n")
                    107: fun oct i = let val m = Integer.makestring
                    108:            in  m(i div 64)^m((i div 8)mod 8)^m(i mod 8) end
                    109: fun c_char "\n" = "\\n"
                    110:   | c_char "\t" = "\\t"
                    111:   | c_char "\\" = "\\\\"
                    112:   | c_char "\"" = "\\\""
                    113:   | c_char c = if ord c < 32 then "\\"^oct(ord c) else c
                    114: fun a_str s = implode(map c_char (explode s))
                    115: fun emitstring s = (emit "\t.ascii\t\""; emit(a_str s); emit "\"\n")
                    116: fun realconst s = (emit "\t.gfloat\t"; emit s; emit "\n")
                    117: fun emitlong (i : int) = (emit "\t.long\t"; emit(makestring i); emit "\n")
                    118: 
                    119: fun emitlab (offset,l2) = 
                    120:        (emit "5:\t.long\t"; emit l2; emit "-5b";
                    121:         if offset < 0 then (emit "-"; emit (makestring (~offset)))
                    122:                       else (emit "+"; emit (makestring offset));
                    123:         emit "\n")
                    124: 
                    125: fun isquick k = (k<=7 andalso k>= ~8)
                    126: 
                    127: fun lprl (src,preg) = (emitopc "lprl"; emit2arg (preg,src))
                    128: fun sprl args = (emitopc "sprl"; emit2arg args)
                    129: fun tbit (arg1 as Immed k, arg2) = (emitopc "tbitl"; emit2arg(arg1,arg2))
                    130:   | tbit args = (emitopc "tbitl"; emit2arg args)
                    131: fun bfs arg = (emitopc "bfs"; emit1arg arg)
                    132: 
                    133: fun movql args = (emitopc "movql"; emit2arg args)
                    134: 
                    135: fun movb args = (emitopc "movb"; emit2arg args)
                    136: fun movzbl args = (emitopc "movzbl"; emit2arg args)
                    137: 
                    138: fun lea args = (emitopc "addr"; emit2arg args)
                    139: 
                    140: fun movl (Immedlab l, arg) = lea(address l, arg)
                    141:   | movl (args as (Immed k, dest)) = 
                    142:        if isquick(k)
                    143:            then movql args
                    144:            else (emitopc "movl"; emit2arg args)
                    145:   | movl args = (emitopc "movl"; emit2arg args)
                    146: 
                    147: fun addl (args as (Immed k, dest)) =
                    148:        if isquick(k)
                    149:            then (emitopc "addql"; emit2arg args)
                    150:            else (emitopc "addl"; emit2arg args)
                    151:   | addl args = (emitopc "addl"; emit2arg args)
                    152: fun subl args = (emitopc "subl"; emit2arg args)
                    153: fun negl args = (emitopc "negl"; emit2arg args)
                    154: fun ashl args = (emitopc "ashl"; emit2arg args)
                    155: fun andl args = (emitopc "andl"; emit2arg args)
                    156: fun orl args = (emitopc "orl"; emit2arg args)
                    157: fun xorl args = (emitopc "xorl"; emit2arg args)
                    158: fun coml args = (emitopc "coml"; emit2arg args)
                    159: fun mull args = (emitopc "mull"; emit2arg args)
                    160: fun divl args = (emitopc "divl"; emit2arg args)
                    161: 
                    162: fun br arg = (emitopc "br"; emit1arg arg)
                    163: fun jump (arg as OffAddress _) = br arg
                    164:   | jump arg = (emitopc "jump"; emit1arg arg)
                    165: fun beq arg = (emitopc "beq"; emit1arg arg)
                    166: fun bne arg = (emitopc "bne"; emit1arg arg)
                    167: fun bge arg = (emitopc "bge"; emit1arg arg)
                    168: fun bgt arg = (emitopc "bgt"; emit1arg arg)
                    169: fun blt arg = (emitopc "blt"; emit1arg arg)
                    170: fun ble arg = (emitopc "ble"; emit1arg arg)
                    171: fun cmpl (args as (Immed k,arg2)) =
                    172:        if isquick(k)
                    173:            then (emitopc "cmpql"; emit2arg args)
                    174:            else (emitopc "cmpl"; emit2arg args)
                    175:   | cmpl args = (emitopc "cmpl"; emit2arg args)
                    176: 
                    177: 
                    178: fun movg args = (emitopc "movg"; emit2arg args)
                    179: fun negg args = (emitopc "negg"; emit2arg args)
                    180: fun addg args = (emitopc "addg"; emit2arg args)
                    181: fun subg args = (emitopc "subg"; emit2arg args)
                    182: fun mulg args = (emitopc "mulg"; emit2arg args)
                    183: fun divg args = (emitopc "divg"; emit2arg args)
                    184: fun cmpg args = (emitopc "cmpg"; emit2arg args)
                    185: 
                    186: 
                    187: val comment = emit
                    188: 
                    189: end (* structure NS32AsCode *)

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.