Semantics for RISC code
This commit is contained in:
@ -6,11 +6,10 @@ let nextLabel () : string =
|
||||
|
||||
module RISCAssembly = struct
|
||||
type register = {
|
||||
index : int
|
||||
index : string
|
||||
}
|
||||
|
||||
type label =
|
||||
string
|
||||
type label = string
|
||||
|
||||
type risci =
|
||||
| Nop
|
||||
@ -56,20 +55,23 @@ module RISCAssembly = struct
|
||||
| Copy
|
||||
| Rand
|
||||
|
||||
type t = risci list
|
||||
type t = {
|
||||
code : risci list;
|
||||
inputval: int option
|
||||
}
|
||||
|
||||
let pp (ppf: out_channel) (t: t) : unit =
|
||||
let pp_risci (ppf: out_channel) (v: risci) : unit =
|
||||
let rec pp_risci (ppf: out_channel) (v: risci) : unit =
|
||||
match v with
|
||||
Nop -> Printf.fprintf ppf "\tNop\n"
|
||||
| BRegOp (b, r1, r2, r3) -> Printf.fprintf ppf "\t%a r%d r%d => r%d\n" pp_brop b r1.index r2.index r3.index
|
||||
| BImmOp (b, r1, i, r3) -> Printf.fprintf ppf "\t%a r%d %d => r%d\n" pp_biop b r1.index i r3.index
|
||||
| URegOp (u, r1, r2) -> Printf.fprintf ppf "\t%a r%d => r%d\n" pp_urop u r1.index r2.index
|
||||
| Load (r1, r2) -> Printf.fprintf ppf "\tLoad r%d => r%d\n" r1.index r2.index
|
||||
| LoadI (r2, i) -> Printf.fprintf ppf "\tLoadI %d => r%d\n" i r2.index
|
||||
| Store (r1, r2) -> Printf.fprintf ppf "\tStore r%d => r%d\n" r1.index r2.index
|
||||
| BRegOp (b, r1, r2, r3) -> Printf.fprintf ppf "\t%a r%s r%s => r%s\n" pp_brop b r1.index r2.index r3.index
|
||||
| BImmOp (b, r1, i, r3) -> Printf.fprintf ppf "\t%a r%s %d => r%s\n" pp_biop b r1.index i r3.index
|
||||
| URegOp (u, r1, r2) -> Printf.fprintf ppf "\t%a r%s => r%s\n" pp_urop u r1.index r2.index
|
||||
| Load (r1, r2) -> Printf.fprintf ppf "\tLoad r%s => r%s\n" r1.index r2.index
|
||||
| LoadI (r2, i) -> Printf.fprintf ppf "\tLoadI %d => r%s\n" i r2.index
|
||||
| Store (r1, r2) -> Printf.fprintf ppf "\tStore r%s => r%s\n" r1.index r2.index
|
||||
| Jump (label) -> Printf.fprintf ppf "\tJump %s\n" label
|
||||
| CJump (r, l1, l2) -> Printf.fprintf ppf "\tCJump r%d => %s, %s\n" r.index l1 l2
|
||||
| CJump (r, l1, l2) -> Printf.fprintf ppf "\tCJump r%s => %s, %s\n" r.index l1 l2
|
||||
| Label (label) -> Printf.fprintf ppf "%s:" label
|
||||
and pp_brop (ppf: out_channel) (v: brop) : unit =
|
||||
match v with
|
||||
@ -107,10 +109,18 @@ module RISCAssembly = struct
|
||||
| Copy -> Printf.fprintf ppf "Copy"
|
||||
| Rand -> Printf.fprintf ppf "Rand"
|
||||
in
|
||||
List.iter (pp_risci ppf) t
|
||||
pp_risci ppf v
|
||||
|
||||
let pp (ppf: out_channel) (t: t) : unit =
|
||||
Printf.fprintf ppf "Input Val: ";
|
||||
match t.inputval with
|
||||
None -> Printf.fprintf ppf "None\n"
|
||||
| Some i -> Printf.fprintf ppf "Some %d\n" i;
|
||||
Printf.fprintf ppf "Code:\n";
|
||||
List.iter (pp_risci ppf) t.code
|
||||
end
|
||||
|
||||
let convert_cfgrisc_risci (i: CfgRISC.RISCSimpleStatements.t list) : (RISCAssembly.t) =
|
||||
let convert_cfgrisc_risci (i: CfgRISC.RISCSimpleStatements.t list) : (RISCAssembly.risci list) =
|
||||
let rec helper (i: CfgRISC.RISCSimpleStatements.t) : RISCAssembly.risci =
|
||||
match i with
|
||||
| Nop -> Nop
|
||||
@ -196,7 +206,7 @@ let rec helper
|
||||
(prg: CfgRISC.RISCCfg.t)
|
||||
(currentnode: Cfg.Node.t)
|
||||
(alreadyVisited: Cfg.Node.t list)
|
||||
: RISCAssembly.t * Cfg.Node.t list =
|
||||
: (RISCAssembly.risci list) * (Cfg.Node.t list) =
|
||||
(* takes the program, the current node and a list of already visited nodes to
|
||||
compute the linearized three address instructions and the list of
|
||||
previoulsy visited nodes plus the newly visited nodes. Stops as soon if
|
||||
@ -235,14 +245,14 @@ let rec helper
|
||||
| BImmOp (_, _, _, r)
|
||||
| URegOp (_, _, r)
|
||||
| Load (_, r)
|
||||
| LoadI (r, _) -> (([Label label1] : RISCAssembly.t) @
|
||||
| LoadI (r, _) -> (([Label label1] : RISCAssembly.risci list) @
|
||||
currentcode @
|
||||
([CJump (r, label2, label3); Label label2] : RISCAssembly.t) @
|
||||
([CJump (r, label2, label3); Label label2] : RISCAssembly.risci list) @
|
||||
res1 @
|
||||
([Jump label1; Label label3] : RISCAssembly.t) @
|
||||
([Jump label1; Label label3] : RISCAssembly.risci list) @
|
||||
res2
|
||||
, vis2)
|
||||
| _ -> failwith "Missing instruction"
|
||||
| _ -> failwith "Missing instruction at branch"
|
||||
else (* if branches, three labels are necessary *)
|
||||
let label1 = nextLabel () in
|
||||
let label2 = nextLabel () in
|
||||
@ -257,19 +267,20 @@ let rec helper
|
||||
| URegOp (_, _, r)
|
||||
| Load (_, r)
|
||||
| LoadI (r, _) -> (currentcode @
|
||||
([CJump (r, label1, label2); Label label1] : RISCAssembly.t) @
|
||||
([CJump (r, label1, label2); Label label1] : RISCAssembly.risci list) @
|
||||
res1 @
|
||||
([Jump label3; Label label2] : RISCAssembly.t) @
|
||||
([Jump label3; Label label2] : RISCAssembly.risci list) @
|
||||
res2 @
|
||||
([Label label3] : RISCAssembly.t) @
|
||||
([Label label3] : RISCAssembly.risci list) @
|
||||
res3
|
||||
, vis3)
|
||||
| _ -> failwith "Missing instruction"
|
||||
| _ -> failwith "Missing instruction at branch"
|
||||
)
|
||||
)
|
||||
| None -> (currentcode, currentnode :: alreadyVisited)
|
||||
)
|
||||
|
||||
let convert (prg: CfgRISC.RISCCfg.t) : RISCAssembly.t =
|
||||
let res, _ = helper prg (Option.get prg.initial) [] in
|
||||
res
|
||||
{code = (helper prg (Option.get prg.initial) [] |> fst |>
|
||||
List.append ([Label "main"] : RISCAssembly.risci list));
|
||||
inputval = prg.inputVal}
|
||||
|
||||
Reference in New Issue
Block a user