|
|
1.1 root 1: BEGIN {
2: print "structure Opcodes = struct"
3: print "val andb = Bits.andb"
4: print "fun lshift(op1,amt) = "
5: print " if amt<0 then Bits.rshift(op1,0-amt)"
6: print " else Bits.lshift(op1,amt)"
7: print "nonfix sub" # bug fixes; want [[sub]] to be a MIPS opcode
8: print "nonfix div" # bug fixes; want [[div]] to be a MIPS opcode
9:
10: decode = "mipsdecode.sml";
11: print "structure MipsDecode = struct" > decode
12: print "val andb = Bits.andb" > decode
13: print "fun rshift(op1,amt) = " > decode
14: print " if amt<0 then Bits.lshift(op1,0-amt)" > decode
15: print " else Bits.rshift(op1,amt)" > decode
16: OPCODE = 1 ; SPECIAL = 2 ; BCOND = 3 ; COP1 = 4
17: stderr="/dev/tty"
18: }
19: function mlnumber(n, s) {
20: if (n<0) s = sprintf("~%d", -n)
21: else s = sprintf("%d", n)
22: return s
23: }
24: function min(x,y){
25: if (x<y) return x
26: else return y
27: }
28: function function_definition(name, argc, i, temp) {
29: if (argc==0) {
30: temp = sprintf("val %s = ", name)
31: } else {
32: temp = sprintf( "fun %s(", name)
33: for (i=1; i< argc; i++) temp = sprintf("%sA%d,", temp,i)
34: temp = sprintf( "%sA%d) = ", temp, argc)
35: }
36: return temp
37: }
38: function insist_fields(n) {
39: if (NF != n) {
40: print "Must have", n, "fields on line",NR ":", $0 > stderr
41: return 0
42: } else {
43: return 1
44: }
45: }
46: NF == 1 && $1 == "opcode" {
47: startline = NR
48: opcodes = 1
49: next
50: }
51: NF == 1 && $1 == "special" {
52: startline = NR
53: specials = 1
54: next
55: }
56: NF == 1 && $1 == "bcond" {
57: startline = NR
58: bconds = 1
59: next
60: }
61: NF == 1 && $1 == "cop1" {
62: startline = NR
63: cop1s = 1
64: next
65: }
66: NF == 0 {opcodes = 0; specials = 0; bconds = 0; cop1s = 0
67: fields = 0
68: instructions= 0
69: }
70: opcodes || specials || bconds || cop1s {
71: if (!insist_fields(8)) next
72: type = OPCODE * opcodes + SPECIAL * specials + BCOND * bconds + COP1 * cop1s
73: major = NR - startline - 1 # major octal digit from row
74: for (i=1; i<= NF; i++) {
75: minor = i-1 # minor octal digit from column
76: code = minor + 8 * major
77: if ($i != "*") {
78: numberof[$i] = code
79: typeof[$i] = type
80: opcode[type,code] = $i
81: } else {
82: opcode[type,code] = "reserved"
83: }
84: }
85: }
86: NF == 1 && $1 == "fields" {
87: startline = NR
88: fields = 1
89: print "val S_fmt = 16+0"
90: print "val D_fmt = 16+1"
91: print "val W_fmt = 16+4"
92:
93: next
94: }
95: fields {
96: if (!insist_fields(3)) next
97: fieldname = $1; low = $2; high = $3
98: if (substr(fieldname,1,1)=="+") {
99: signed = 1
100: fieldname = substr(fieldname,2)
101: } else {
102: signed = 0
103: }
104: fieldnames[fieldname]= 1 # rememeber all the field names
105:
106: if (low >= 16) {
107: printf "%s", function_definition(fieldname "LO",1); print "0"
108: } else {
109: printf "%s", function_definition(fieldname "LO",1)
110: printf "andb(lshift(A1,%d),65535)\n", low
111: }
112: if (high < 16) {
113: printf "%s", function_definition(fieldname "HI",1); print "0"
114: } else {
115: printf "%s", function_definition(fieldname "HI",1)
116: printf "lshift(A1,%s)\n", mlnumber(low - 16)
117: }
118: printf "%s", function_definition("THE" fieldname,2) > decode
119: if (signed) printf "let val n = " > decode
120: if (low >= 16) {
121: printf "0" > decode
122: } else {
123: printf "andb(rshift(A2,%d),%d)", low,
124: (2**(min(15,high)-low+1)-1) > decode
125: }
126: printf " + " > decode
127: if (high < 16) {
128: printf "0\n" > decode
129: } else {
130: printf "rshift(andb(A1,%d),%s)\n", (2**(high-16+1)-1),
131: mlnumber(low - 16) > decode
132: }
133: if (signed) {
134: printf "in if n < %d then n else n - %d\nend\n",
135: 2**(high-low), 2**(high-low+1) > decode
136: }
137:
138:
139: }
140: NF == 1 && $1 == "instructions" {
141: startline = NR
142: instructions = 1
143: next
144: }
145: instructions && $0 !~ /^#/ {
146: opname = $1
147:
148: temp = "\"" opname " \""
149: for (i=2; i<=NF; i++) {
150: temp = sprintf( "%s ^ \"%s = \" ^ S%s", temp, $i, $i)
151: if (i<NF) temp = sprintf("%s ^ \",\" ", temp)
152: }
153: displayof[opname]=temp " ^ \"\\n\""
154:
155: ######## gsub("[^a-z']+"," ") ### ill-advised
156:
157: printf "%s", function_definition(opname, NF-1)
158: printf "(" # open parenthesis for pair
159: for (i=2; i<= NF; i++) {
160: if (!($i in fieldnames)) {
161: print "unknown field", $i, "on line", NR > stderr
162: next
163: }
164: printf "%sHI(A%d)+", $i, i-1
165: }
166: if (typeof[opname]==OPCODE) {
167: printf "op'HI(%d)", numberof[opname]
168: } else if (typeof[opname]==SPECIAL) {
169: printf "op'HI(%d)+", numberof["special"]
170: printf "functHI(%d)", numberof[opname]
171: } else if (typeof[opname]==BCOND) {
172: printf "op'HI(%d)+", numberof["bcond"]
173: printf "condHI(%d)", numberof[opname]
174: } else if (typeof[opname]==COP1) {
175: printf "op'HI(%d)+", numberof["cop1"]
176: printf "functHI(%d)", numberof[opname]
177: } else {
178: print "unknown opcode", opname, "on line", NR > stderr
179: next
180: }
181: printf ", "
182: for (i=2; i<= NF; i++) {
183: if (!($i in fieldnames)) {
184: print "unknown field", $i, "on line", NR > stderr
185: next
186: }
187: printf "%sLO(A%d)+", $i, i-1
188: }
189: if (typeof[opname]==OPCODE) {
190: printf "op'LO(%d)", numberof[opname]
191: } else if (typeof[opname]==SPECIAL) {
192: printf "op'LO(%d)+", numberof["special"]
193: printf "functLO(%d)", numberof[opname]
194: } else if (typeof[opname]==BCOND) {
195: printf "op'LO(%d)+", numberof["bcond"]
196: printf "condLO(%d)", numberof[opname]
197: } else if (typeof[opname]==COP1) {
198: printf "op'LO(%d)+", numberof["cop1"]
199: printf "functLO(%d)", numberof[opname]
200: } else {
201: print "unknown opcode", opname, "on line", NR > stderr
202: next
203: }
204: printf ")\n"
205: }
206:
207: END {
208: printf "%s", function_definition("decode",2) > decode
209: print "let" > decode
210: for (f in fieldnames) {
211: printf "val %s = THE%s(A1,A2)\n", f, f > decode
212: printf "val S%s = Integer.makestring %s\n", f, f > decode
213: }
214: print "val do_special =" > decode
215: print "(case funct of" > decode
216: for (code=0; code<256; code++) {
217: name = opcode[SPECIAL,code]
218: if (name != "" && name != "reserved") {
219: if (code!=0) printf " | " > decode # hack but it works
220: else printf " " > decode
221: disp = displayof[name]
222: if (disp=="") disp="\"" name "(??? unknown format???)\\n\""
223: printf "%d => %s\n", code, disp > decode
224: }
225: }
226: printf " | _ => \"unknown special\\n\"\n" > decode
227: print " ) " > decode
228: print "val do_bcond =" > decode
229: print "(case cond of" > decode
230: for (code=0; code<256; code++) {
231: name = opcode[BCOND,code]
232: if (name != "" && name != "reserved") {
233: if (code!=0) printf " | " > decode # hack but it works
234: else printf " " > decode
235: disp = displayof[name]
236: if (disp=="") disp="\"" name "(??? unknown format???)\\n\""
237: printf "%d => %s\n", code, disp > decode
238: }
239: }
240: printf " | _ => \"unknown bcond\\n\"\n" > decode
241: print " ) " > decode
242: print "val do_cop1 =" > decode
243: print "(case funct of" > decode
244: for (code=0; code<256; code++) {
245: name = opcode[COP1,code]
246: if (name != "" && name != "reserved") {
247: if (code!=0) printf " | " > decode # hack but it works
248: else printf " " > decode
249: disp = displayof[name]
250: if (disp=="") disp="\"" name "(??? unknown format???)\\n\""
251: printf "%d => %s\n", code, disp > decode
252: }
253: }
254: printf " | _ => \"unknown cop1\\n\"\n" > decode
255: print " ) " > decode
256: print "in" > decode
257: print "(case op' of" > decode
258: for (code=0; code<256; code++) {
259: name = opcode[OPCODE,code]
260: if (name=="special") {
261: if (code!=0) printf " | " > decode # hack but it works
262: else printf " " > decode
263: printf "%d => %s\n", code, "do_special" > decode
264: } else if (name=="bcond") {
265: if (code!=0) printf " | " > decode # hack but it works
266: else printf " " > decode
267: printf "%d => %s\n", code, "do_bcond" > decode
268: } else if (name=="cop1") {
269: if (code!=0) printf " | " > decode # hack but it works
270: else printf " " > decode
271: printf "%d => %s\n", code, "do_cop1" > decode
272: } else if (name != "" && name != "reserved") {
273: if (code!=0) printf " | " > decode # hack but it works
274: else printf " " > decode
275: disp = displayof[name]
276: if (disp=="") disp="\"" name "(??? unknown format???)\\n\""
277: printf "%d => %s\n", code, disp > decode
278: }
279: }
280: printf " | _ => \"unknown opcode\\n\"\n" > decode
281: print " ) " > decode
282: print "end" > decode
283: print "end (* Opcodes *)"
284: print "end (* Decode *)" > decode
285: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.