Translation of basic store instruction.

master
Rohan Jacob-Rao 9 years ago
parent 51ebf2493e
commit a20d178e7c

@ -0,0 +1,5 @@
; Function with store instruction
define i32 @main() {
store i32 0, i32* %i
ret i32 0
}

@ -34,6 +34,7 @@ type instr =
| Ret of (typ * operand) option | Ret of (typ * operand) option
| UncondBranch of variable | UncondBranch of variable
| CondBranch of operand * variable * variable | CondBranch of operand * variable * variable
| Store of operand * typ * variable
type func_def = FuncDef of variable * typ option * (typ * string) list * instr list type func_def = FuncDef of variable * typ option * (typ * string) list * instr list

@ -121,7 +121,7 @@ rule token = parse
(* memory access and addressing operations *) (* memory access and addressing operations *)
(*| "alloca" { ALLOCA }*) (*| "alloca" { ALLOCA }*)
(*| "load" { LOAD }*) (*| "load" { LOAD }*)
(*| "store" { STORE }*) | "store" { STORE }
(*| "fence" { FENCE }*) (*| "fence" { FENCE }*)
(*| "cmpxchg" { CMPXCHG }*) (*| "cmpxchg" { CMPXCHG }*)
(*| "atomicrmw" { ATOMICRMW }*) (*| "atomicrmw" { ATOMICRMW }*)

@ -96,7 +96,7 @@
(* memory access and addressing operations *) (* memory access and addressing operations *)
(*%token ALLOCA*) (*%token ALLOCA*)
(*%token LOAD*) (*%token LOAD*)
(*%token STORE*) %token STORE
(*%token FENCE*) (*%token FENCE*)
(*%token CMPXCHG*) (*%token CMPXCHG*)
(*%token ATOMICRMW*) (*%token ATOMICRMW*)
@ -165,7 +165,7 @@ vector_typ:
element_typ: element_typ:
| width=INT { Tint width } | width=INT { Tint width }
| floating_typ { Tfloat } | floating_typ { Tfloat }
| tp=ptr_typ { tp } | tp=ptr_typ { Tptr tp }
floating_typ: floating_typ:
| HALF { () } | HALF { () }
@ -176,7 +176,7 @@ floating_typ:
| PPC_FP128 { () } | PPC_FP128 { () }
ptr_typ: ptr_typ:
| tp=typ STAR { Tptr tp } | tp=typ STAR { tp }
block: block:
| LBRACE instrs=list(instr) RBRACE { instrs } | LBRACE instrs=list(instr) RBRACE { instrs }
@ -184,6 +184,8 @@ block:
instr: instr:
| term=terminator { term } | term=terminator { term }
| variable EQUALS binop { Ret None } (* TODO *) | variable EQUALS binop { Ret None } (* TODO *)
| STORE val_tp=typ value=operand COMMA ptr_tp=ptr_typ var=variable { Store (value, val_tp, var) }
(* don't yet know why val_tp and ptr_tp would be different *)
terminator: terminator:
| RET tp=typ op=operand { Ret (Some (tp, op)) } | RET tp=typ op=operand { Ret (Some (tp, op)) }

@ -37,6 +37,8 @@ let trans_instr (cfg : Cfg.cfg) (pdesc : Cfg.Procdesc.t) : LAst.instr -> Sil.ins
| Ret (Some (tp, exp)) -> | Ret (Some (tp, exp)) ->
let ret_var = Sil.get_ret_pvar (Cfg.Procdesc.get_proc_name pdesc) in let ret_var = Sil.get_ret_pvar (Cfg.Procdesc.get_proc_name pdesc) in
[Sil.Set (Sil.Lvar ret_var, trans_typ tp, trans_operand exp, Sil.dummy_location)] [Sil.Set (Sil.Lvar ret_var, trans_typ tp, trans_operand exp, Sil.dummy_location)]
| Store (op, tp, var) ->
[Sil.Set (trans_variable var, trans_typ tp, trans_operand op, Sil.dummy_location)]
| _ -> raise (Unimplemented "Need to translate instruction to SIL.") | _ -> raise (Unimplemented "Need to translate instruction to SIL.")
(* Update CFG and call graph with new function definition *) (* Update CFG and call graph with new function definition *)
@ -76,7 +78,7 @@ let trans_func_def (cfg : Cfg.cfg) (cg: Cg.t) : LAst.func_def -> unit = function
let exit_kind = Cfg.Node.Exit_node procdesc in let exit_kind = Cfg.Node.Exit_node procdesc in
let exit_node = Cfg.Node.create cfg Sil.dummy_location exit_kind [] procdesc [] in let exit_node = Cfg.Node.create cfg Sil.dummy_location exit_kind [] procdesc [] in
let nodekind_of_instr : LAst.instr -> Cfg.Node.nodekind = function let nodekind_of_instr : LAst.instr -> Cfg.Node.nodekind = function
| Ret _ -> Cfg.Node.Stmt_node "method_body" | Ret _ | Store _ -> Cfg.Node.Stmt_node "method_body"
| _ -> raise (Unimplemented "Need to get node type for instruction.") in | _ -> raise (Unimplemented "Need to get node type for instruction.") in
let node_of_instr (cfg : Cfg.cfg) (procdesc : Cfg.Procdesc.t) (instr : LAst.instr) : Cfg.Node.t = let node_of_instr (cfg : Cfg.cfg) (procdesc : Cfg.Procdesc.t) (instr : LAst.instr) : Cfg.Node.t =
Cfg.Node.create cfg Sil.dummy_location (nodekind_of_instr instr) (trans_instr cfg procdesc instr) procdesc [] in Cfg.Node.create cfg Sil.dummy_location (nodekind_of_instr instr) (trans_instr cfg procdesc instr) procdesc [] in

Loading…
Cancel
Save