2 * blargCPU instruction/operand internal representation and manipulation
3 * Author: Joshua Wise <jwise@andrew.cmu.edu>
4 * Author: Chris Lu <czl@andrew.cmu.edu>
11 R0 | R1 | R2 | R3 | R4 | R5 | R6 | R7 | R8 | R9 | R10 | R11 | R12 | FR | SP | PC
12 (* operands to instructions *)
13 datatype oper = REG of reg |
16 datatype pred = NV | NE | EQ | LT | GT | LE | GE | AL
19 MOVLIT of oper * word |
20 MOVSYM of oper * Symbol.symbol |
21 MOVLBL of oper * Label.label |
22 MOVSTR of oper * Stringref.stringref |
37 CALL of oper * oper * int |
44 LABEL of Label.label |
48 structure OperSet : ORD_SET
49 where type Key.ord_key = oper;
50 structure LiveMap : ORD_MAP
51 where type Key.ord_key = int;
53 val regcmp : reg * reg -> order
54 val cmpoper : oper * oper -> order
55 val opereq : oper * oper -> bool
56 val regname : reg -> string
57 val regtonum : reg -> int
58 val numtoreg : int -> reg
59 val predname : pred -> string
60 val pp_oper : oper -> string
61 val print : insn -> string
64 structure Blarg :> BLARG =
69 R0 | R1 | R2 | R3 | R4 | R5 | R6 | R7 | R8 | R9 | R10 | R11 | R12 | FR | SP | PC
70 (* operands to instructions *)
71 datatype oper = REG of reg |
74 datatype pred = NV | NE | EQ | LT | GT | LE | GE | AL
77 MOVLIT of oper * word |
78 MOVSYM of oper * Symbol.symbol |
79 MOVLBL of oper * Label.label |
80 MOVSTR of oper * Stringref.stringref |
95 CALL of oper * oper * int |
100 DIRECTIVE of string |
102 LABEL of Label.label |
106 type func = Ast.ident * insn list
108 (* gives name of reg *)
129 val (r, n) = valOf (List.find (fn (r, _) => r = reg) regnames)
134 fun predname NV = "nv"
143 (* gives number (color) associated with reg *)
161 (* gives reg associated with number (color) *)
175 | numtoreg n = raise ErrorMsg.InternalError ("numtoreg: Invalid register "^(Int.toString n))
177 (* register compare *)
178 fun regcmp (r1, r2) = Int.compare (regtonum r1, regtonum r2)
180 (* operand compare; arbitrary order imposed to make
181 * various things easier (e.g. liveness, for sorting)
183 fun cmpoper (REG reg1, REG reg2) = regcmp (reg1, reg2)
184 | cmpoper (TEMP temp1, TEMP temp2) = Temp.compare (temp1,temp2)
185 | cmpoper (REG _, _) = LESS
186 | cmpoper (_, _) = GREATER
188 fun opereq (REG a, REG b) = a = b
189 | opereq (TEMP a, TEMP b) = Temp.eq (a, b)
190 | opereq (_, _) = false
192 structure OperSet = ListSetFn (
195 val compare = cmpoper
198 structure LiveMap = SplayMapFn(struct
200 val compare = Int.compare
203 fun pp_oper (REG r) = (regname r)
204 | pp_oper (TEMP t) = (Temp.name t)
205 | pp_oper (STACKARG i) = "arg#"^Int.toString i
207 fun pp_insn pr (MOVLIT (d, w)) = "\tmov"^pr^" "^(pp_oper d)^", #0x"^(Word.toString w)^"\n"
208 | pp_insn pr (MOVSYM (d, s)) = "\tmov"^pr^" "^(pp_oper d)^", #"^(Symbol.name s)^"\n"
209 | pp_insn pr (MOVLBL (d, l)) = "\tmov"^pr^" "^(pp_oper d)^", #"^(Label.name l)^"\n"
210 | pp_insn pr (MOVSTR (d, s)) = "\tmov"^pr^" "^(pp_oper d)^", #"^(Stringref.name s)^"\n"
211 | pp_insn pr (LDR (d, s)) = "\tldr"^pr^" "^(pp_oper d)^", ["^(pp_oper s)^"]\n"
212 | pp_insn pr (STO (d, s)) = "\tsto"^pr^" ["^(pp_oper d)^"], "^(pp_oper s)^"\n"
213 | pp_insn pr (MOV (d, s)) = "\tmov"^pr^" "^(pp_oper d)^", "^(pp_oper s)^"\n"
214 | pp_insn pr (MOVS (d, s)) = "\tmovs"^pr^" "^(pp_oper d)^", "^(pp_oper s)^"\n"
215 | pp_insn pr (ADD (d, s)) = "\tadd"^pr^" "^(pp_oper d)^", "^(pp_oper s)^"\n"
216 | pp_insn pr (ADDS (d, s)) = "\tadds"^pr^" "^(pp_oper d)^", "^(pp_oper s)^"\n"
217 | pp_insn pr (SUB (d, s)) = "\tsub"^pr^" "^(pp_oper d)^", "^(pp_oper s)^"\n"
218 | pp_insn pr (SUBS (d, s)) = "\tsubs"^pr^" "^(pp_oper d)^", "^(pp_oper s)^"\n"
219 | pp_insn pr (AND (d, s)) = "\tand"^pr^" "^(pp_oper d)^", "^(pp_oper s)^"\n"
220 | pp_insn pr (ANDS (d, s)) = "\tands"^pr^" "^(pp_oper d)^", "^(pp_oper s)^"\n"
221 | pp_insn pr (NOT (d, s)) = "\tnot"^pr^" "^(pp_oper d)^", "^(pp_oper s)^"\n"
222 | pp_insn pr (NOTS (d, s)) = "\tnots"^pr^" "^(pp_oper d)^", "^(pp_oper s)^"\n"
223 | pp_insn pr (PUSH (d, s)) = "\tpush"^pr^" "^(pp_oper d)^", "^(pp_oper s)^"\n"
224 | pp_insn pr (POP (d, s)) = "\tpop"^pr^" "^(pp_oper d)^", "^(pp_oper s)^"\n"
225 | pp_insn pr (CALL (d, s, n)) = "\tcall"^pr^" "^(pp_oper d)^", "^(pp_oper s)^" @ ("^(Int.toString n)^" args)\n"
226 | pp_insn pr (SHR (d, s)) = "\tshr"^pr^" "^(pp_oper d)^", "^(pp_oper s)^"\n"
227 | pp_insn pr (SHL (d, s)) = "\tshl"^pr^" "^(pp_oper d)^", "^(pp_oper s)^"\n"
229 (* pretty prints the asm *)
230 fun print (DIRECTIVE(str)) = str ^ "\n"
231 | print (COMMENT(str)) = "@ " ^ str ^ "\n"
232 | print (LABEL(l)) = Label.name l ^ ":\n"
233 | print (INSN (pred, insn)) = pp_insn (predname pred) insn
234 | print (LIVEIGN i) = print i