Added While loop to RISC

This commit is contained in:
elvis
2024-12-02 14:26:05 +01:00
parent 15356aa5a9
commit efa6ed21c9

View File

@ -192,7 +192,15 @@ let nextCommonSuccessor (prg: CfgRISC.RISCCfg.t) (node1: Cfg.Node.t) (node2: Cfg
| a::_ -> Some a
let rec helper (prg: CfgRISC.RISCCfg.t) (currentnode: Cfg.Node.t) (alreadyVisited: Cfg.Node.t list) : RISCAssembly.t * Cfg.Node.t list =
let rec helper
(prg: CfgRISC.RISCCfg.t)
(currentnode: Cfg.Node.t)
(alreadyVisited: Cfg.Node.t list)
: RISCAssembly.t * 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
node has already been visited or if no nodes are next *)
if List.mem currentnode alreadyVisited then
([], alreadyVisited)
else (
@ -214,9 +222,28 @@ let rec helper (prg: CfgRISC.RISCCfg.t) (currentnode: Cfg.Node.t) (alreadyVisite
failwith "Topology got a little mixed up"
| Some ncs -> (
if (ncs.id = nextnode2.id)
then (* while or for loop *)
failwith "Not implemented"
else (* if branches *)
then (* while or for loop, three labels are necessary *)
let label1 = nextLabel () in
let label2 = nextLabel () in
let label3 = nextLabel () in
let res1, _ = (helper prg nextnode1 (currentnode :: nextnode2 :: alreadyVisited)) in
let res2, vis2 = (helper prg nextnode2 (currentnode :: nextnode1 :: alreadyVisited)) in
match List.nth currentcode ((List.length currentcode) - 1) with
| BRegOp (_, _, _, r)
| BImmOp (_, _, _, r)
| URegOp (_, _, r)
| Load (_, r)
| LoadI (r, _) -> (([Label label1] : RISCAssembly.t) @
currentcode @
([CJump (r, label2, label3); Label label2] : RISCAssembly.t) @
res1 @
([Jump label1; Label label3] : RISCAssembly.t) @
res2
, vis2)
| _ -> failwith "Missing instruction"
else (* if branches, three labels are necessary *)
let label1 = nextLabel () in
let label2 = nextLabel () in
let label3 = nextLabel () in