|
|
1.1 root 1: (* Copyright 1989 by AT&T Bell Laboratories *)
2: structure Emitters : EMITTERS = struct
3: type emitter_triple = (int * int -> unit) * (int -> string -> unit)
4: * (string -> unit)
5: local
6: val so_far = ref nil : string list ref
7: fun squirrel s = so_far := s :: !so_far
8: fun emit_byte n = squirrel (chr n)
9: in
10: fun emit_string n s = squirrel (substring(s,0,n))
11: handle e =>
12: (print "?exception "; print (System.exn_name e);
13: print (" in emitters.emit_string "^
14: (Integer.makestring n) ^ " \""^s^"\"\n");
15: raise e)
16:
17: fun emit_pair_little(hi,lo) =
18: let open Bits
19: fun emit_word(n) =
20: (emit_byte(andb(n,255));emit_byte(andb(rshift(n,8),255)))
21: in (emit_word(lo);emit_word(hi))
22: end
23:
24: fun emit_pair_big(hi,lo) =
25: let open Bits
26: fun emit_word(n) =
27: (emit_byte(andb(rshift(n,8),255));emit_byte(andb(n,255)))
28: in (emit_word(hi);emit_word(lo))
29: end
30:
31:
32: fun emitted_string () =
33: let val s = implode (rev (!so_far))
34: in so_far := nil; s
35: end
36: end
37: structure BigReal = MipsReal(struct val emit_word = emit_pair_big end)
38: structure LittleReal =MipsReal(struct val emit_word = emit_pair_little end)
39: val LittleEndian = (emit_pair_little,emit_string,LittleReal.realconst)
40: val BigEndian = (emit_pair_big,emit_string,BigReal.realconst)
41: val address = ref 0 (* address of next instruction in words *)
42: val decode_real_ptr = ref NONE : ((int * int) * string -> unit) option ref
43: (* used to emit asm code for a real word *)
44:
45: val real_least = ref NONE : (int * int) option ref
46: (* least significant word of real *)
47: val real_string = ref ""
48: fun emit_real_word w =
49: let val decode_real = case !decode_real_ptr of
50: SOME f => f
51: | NONE => ErrorMsg.impossible "missed real decoder in mips asm"
52: in
53: case !real_least of
54: NONE => real_least := SOME w
55: | SOME least =>
56: (decode_real(least,"[low word of "^(!real_string)^"]");
57: decode_real(w,"[high word of "^(!real_string)^"]"))
58: end
59:
60: structure AsmReal = MipsReal(struct val emit_word = emit_real_word end)
61:
62: fun MipsAsm stream =
63: let fun say s = (output stream s; flush_out stream)
64: fun printaddr addrref =
65: let val n = !addrref
66: in (if n<10 then " " else if n < 100 then " " else "")
67: ^ (Integer.makestring n)
68: end
69: local
70: open Bits
71: fun hexdigit n =
72: let val d = andb(n,15)
73: in if d <= 9 then chr(d+ord("0"))
74: else chr(d-10+ord("a"))
75: end
76: fun hex1 n = hexdigit(rshift(n,4))^hexdigit(n)
77: fun hex2 n = hex1(rshift(n,8))^hex1(n)
78: fun hex4 n = hex2(rshift(n,16))^hex2(n)
79: in
80: fun hex(hi,lo) = hex2(hi) ^ hex2(lo)
81: fun printaddr addrref =
82: let val n = 4 * (!addrref) (* address in bytes *)
83: in "0x" ^ (hex4 n)
84: end
85: end
86: fun decode x = (
87: say ((printaddr address) ^ ": (" ^ (hex x) ^") "
88: ^ (MipsDecode.decode x));
89: address := !address + 1; ()
90: )
91: fun do_decode_real(w,s) = (
92: say ((printaddr address) ^ ": (" ^ (hex w) ^") "
93: ^ s ^ "\n");
94: address := !address + 1; ()
95: )
96: fun decode_real s = (real_string := s; AsmReal.realconst s)
97: fun decode_string n s =
98: if n > 0 then
99: (say ((printaddr address)
100: ^ ": \"" ^substring(s,0,4) ^"\"\n");
101: address := !address + 1;
102: decode_string (n-4) (substring(s,4,String.length(s)-4))
103: )
104: else ()
105: in
106: decode_real_ptr := SOME do_decode_real;
107: (decode,decode_string,decode_real) : emitter_triple
108: end
109: end
110:
111:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.