Skip to content

Commit

Permalink
Expose LLVMSetOperand and LLVMGetNumOperands to llvm-c and ocaml.
Browse files Browse the repository at this point in the history
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@111625 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
Erick Tryzelaar committed Aug 20, 2010
1 parent e127410 commit f7af931
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions bindings/ocaml/llvm/llvm.ml
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ let fold_right_uses f v init =

(*--... Operations on users ................................................--*)
external operand : llvalue -> int -> llvalue = "llvm_operand"
external set_operand : llvalue -> int -> llvalue -> unit = "llvm_set_operand"
external num_operands : llvalue -> int = "llvm_num_operands"

(*--... Operations on constants of (mostly) any type .......................--*)
external is_constant : llvalue -> bool = "llvm_is_constant"
Expand Down
8 changes: 8 additions & 0 deletions bindings/ocaml/llvm/llvm.mli
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,14 @@ val fold_right_uses : (lluse -> 'a -> 'a) -> llvalue -> 'a -> 'a
method [llvm::User::getOperand]. *)
external operand : llvalue -> int -> llvalue = "llvm_operand"

(** [set_operand v i o] sets the operand of the value [v] at the index [i] to
the value [o].
See the method [llvm::User::setOperand]. *)
external set_operand : llvalue -> int -> llvalue -> unit = "llvm_set_operand"

(** [num_operands v] returns the number of operands for the value [v].
See the method [llvm::User::getNumOperands]. *)
external num_operands : llvalue -> int = "llvm_num_operands"

(** {7 Operations on constants of (mostly) any type} *)

Expand Down
11 changes: 11 additions & 0 deletions bindings/ocaml/llvm/llvm_ocaml.c
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,17 @@ CAMLprim LLVMValueRef llvm_operand(LLVMValueRef V, value I) {
return LLVMGetOperand(V, Int_val(I));
}

/* llvalue -> int -> llvalue -> unit */
CAMLprim value llvm_set_operand(LLVMValueRef U, value I, LLVMValueRef V) {
LLVMSetOperand(U, Int_val(I), V);
return Val_unit;
}

/* llvalue -> int */
CAMLprim value llvm_num_operands(LLVMValueRef V) {
return Val_int(LLVMGetNumOperands(V));
}

/*--... Operations on constants of (mostly) any type .......................--*/

/* llvalue -> bool */
Expand Down
2 changes: 2 additions & 0 deletions include/llvm-c/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,8 @@ LLVMValueRef LLVMGetUsedValue(LLVMUseRef U);

/* Operations on Users */
LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index);
void LLVMSetOperand(LLVMValueRef User, unsigned Index, LLVMValueRef Val);
int LLVMGetNumOperands(LLVMValueRef Val);

/* Operations on constants of any type */
LLVMValueRef LLVMConstNull(LLVMTypeRef Ty); /* all zeroes */
Expand Down
8 changes: 8 additions & 0 deletions lib/VMCore/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,14 @@ LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
return wrap(unwrap<User>(Val)->getOperand(Index));
}

void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
unwrap<User>(Val)->setOperand(Index, unwrap(Op));
}

int LLVMGetNumOperands(LLVMValueRef Val) {
return unwrap<User>(Val)->getNumOperands();
}

/*--.. Operations on constants of any type .................................--*/

LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
Expand Down
7 changes: 7 additions & 0 deletions test/Bindings/Ocaml/vmcore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -642,11 +642,18 @@ let test_users () =

let p1 = param fn 0 in
let p2 = param fn 1 in
let a3 = build_alloca i32_type "user_alloca" b in
let p3 = build_load a3 "user_load" b in
let i = build_add p1 p2 "sum" b in

insist ((num_operands i) = 2);
insist ((operand i 0) = p1);
insist ((operand i 1) = p2);

set_operand i 1 p3;
insist ((operand i 1) != p2);
insist ((operand i 1) = p3);

ignore (build_unreachable b)


Expand Down

0 comments on commit f7af931

Please sign in to comment.