From 4647569fe7706e76135a08ca0e5f90a447ccc5b4 Mon Sep 17 00:00:00 2001 From: Gordon Henriksen Date: Sat, 6 Oct 2007 14:29:36 +0000 Subject: [PATCH] Adding C and Ocaml bindings for ConstantExpr. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@42696 91177308-0d34-0410-b5e6-96231b3b80d8 --- bindings/ocaml/llvm/llvm.ml | 45 +++++++ bindings/ocaml/llvm/llvm.mli | 45 +++++++ bindings/ocaml/llvm/llvm_ocaml.c | 40 ++++-- include/llvm-c/Core.h | 49 ++++++++ include/llvm/Constants.h | 6 +- lib/VMCore/Constants.cpp | 4 +- lib/VMCore/Core.cpp | 201 +++++++++++++++++++++++++++++++ test/Bindings/Ocaml/vmcore.ml | 103 +++++++++++++++- 8 files changed, 478 insertions(+), 15 deletions(-) diff --git a/bindings/ocaml/llvm/llvm.ml b/bindings/ocaml/llvm/llvm.ml index 5121291cea5..79de1c760bf 100644 --- a/bindings/ocaml/llvm/llvm.ml +++ b/bindings/ocaml/llvm/llvm.ml @@ -209,6 +209,51 @@ external make_struct_constant : llvalue array -> bool -> llvalue external make_vector_constant : llvalue array -> llvalue = "llvm_make_vector_constant" +(*--... Constant expressions ...............................................--*) +external sizeof : lltype -> llvalue = "LLVMSizeOf" +external const_neg : llvalue -> llvalue = "LLVMConstNeg" +external const_not : llvalue -> llvalue = "LLVMConstNot" +external const_add : llvalue -> llvalue -> llvalue = "LLVMConstAdd" +external const_sub : llvalue -> llvalue -> llvalue = "LLVMConstSub" +external const_mul : llvalue -> llvalue -> llvalue = "LLVMConstMul" +external const_udiv : llvalue -> llvalue -> llvalue = "LLVMConstUDiv" +external const_sdiv : llvalue -> llvalue -> llvalue = "LLVMConstSDiv" +external const_fdiv : llvalue -> llvalue -> llvalue = "LLVMConstFDiv" +external const_urem : llvalue -> llvalue -> llvalue = "LLVMConstURem" +external const_srem : llvalue -> llvalue -> llvalue = "LLVMConstSRem" +external const_frem : llvalue -> llvalue -> llvalue = "LLVMConstFRem" +external const_and : llvalue -> llvalue -> llvalue = "LLVMConstAnd" +external const_or : llvalue -> llvalue -> llvalue = "LLVMConstOr" +external const_xor : llvalue -> llvalue -> llvalue = "LLVMConstXor" +external const_icmp : int_predicate -> llvalue -> llvalue -> llvalue + = "llvm_const_icmp" +external const_fcmp : real_predicate -> llvalue -> llvalue -> llvalue + = "llvm_const_fcmp" +external const_shl : llvalue -> llvalue -> llvalue = "LLVMConstShl" +external const_lshr : llvalue -> llvalue -> llvalue = "LLVMConstLShr" +external const_ashr : llvalue -> llvalue -> llvalue = "LLVMConstAShr" +external const_gep : llvalue -> llvalue array -> llvalue = "llvm_const_gep" +external const_trunc : llvalue -> lltype -> llvalue = "LLVMConstTrunc" +external const_sext : llvalue -> lltype -> llvalue = "LLVMConstSExt" +external const_zext : llvalue -> lltype -> llvalue = "LLVMConstZExt" +external const_fptrunc : llvalue -> lltype -> llvalue = "LLVMConstFPTrunc" +external const_fpext : llvalue -> lltype -> llvalue = "LLVMConstFPExt" +external const_uitofp : llvalue -> lltype -> llvalue = "LLVMConstUIToFP" +external const_sitofp : llvalue -> lltype -> llvalue = "LLVMConstSIToFP" +external const_fptoui : llvalue -> lltype -> llvalue = "LLVMConstFPToUI" +external const_fptosi : llvalue -> lltype -> llvalue = "LLVMConstFPToSI" +external const_ptrtoint : llvalue -> lltype -> llvalue = "LLVMConstPtrToInt" +external const_inttoptr : llvalue -> lltype -> llvalue = "LLVMConstIntToPtr" +external const_bitcast : llvalue -> lltype -> llvalue = "LLVMConstBitCast" +external const_select : llvalue -> llvalue -> llvalue -> llvalue + = "LLVMConstSelect" +external const_extractelement : llvalue -> llvalue -> llvalue + = "LLVMConstExtractElement" +external const_insertelement : llvalue -> llvalue -> llvalue -> llvalue + = "LLVMConstInsertElement" +external const_shufflevector : llvalue -> llvalue -> llvalue -> llvalue + = "LLVMConstShuffleVector" + (*--... Operations on global variables, functions, and aliases (globals) ...--*) external is_declaration : llvalue -> bool = "llvm_is_declaration" external linkage : llvalue -> linkage = "llvm_linkage" diff --git a/bindings/ocaml/llvm/llvm.mli b/bindings/ocaml/llvm/llvm.mli index 85b93ae19de..943563acb2b 100644 --- a/bindings/ocaml/llvm/llvm.mli +++ b/bindings/ocaml/llvm/llvm.mli @@ -192,6 +192,51 @@ external make_struct_constant : llvalue array -> bool -> llvalue external make_vector_constant : llvalue array -> llvalue = "llvm_make_vector_constant" +(*--... Constant expressions ...............................................--*) +external sizeof : lltype -> llvalue = "LLVMSizeOf" +external const_neg : llvalue -> llvalue = "LLVMConstNeg" +external const_not : llvalue -> llvalue = "LLVMConstNot" +external const_add : llvalue -> llvalue -> llvalue = "LLVMConstAdd" +external const_sub : llvalue -> llvalue -> llvalue = "LLVMConstSub" +external const_mul : llvalue -> llvalue -> llvalue = "LLVMConstMul" +external const_udiv : llvalue -> llvalue -> llvalue = "LLVMConstUDiv" +external const_sdiv : llvalue -> llvalue -> llvalue = "LLVMConstSDiv" +external const_fdiv : llvalue -> llvalue -> llvalue = "LLVMConstFDiv" +external const_urem : llvalue -> llvalue -> llvalue = "LLVMConstURem" +external const_srem : llvalue -> llvalue -> llvalue = "LLVMConstSRem" +external const_frem : llvalue -> llvalue -> llvalue = "LLVMConstFRem" +external const_and : llvalue -> llvalue -> llvalue = "LLVMConstAnd" +external const_or : llvalue -> llvalue -> llvalue = "LLVMConstOr" +external const_xor : llvalue -> llvalue -> llvalue = "LLVMConstXor" +external const_icmp : int_predicate -> llvalue -> llvalue -> llvalue + = "llvm_const_icmp" +external const_fcmp : real_predicate -> llvalue -> llvalue -> llvalue + = "llvm_const_fcmp" +external const_shl : llvalue -> llvalue -> llvalue = "LLVMConstShl" +external const_lshr : llvalue -> llvalue -> llvalue = "LLVMConstLShr" +external const_ashr : llvalue -> llvalue -> llvalue = "LLVMConstAShr" +external const_gep : llvalue -> llvalue array -> llvalue = "llvm_const_gep" +external const_trunc : llvalue -> lltype -> llvalue = "LLVMConstTrunc" +external const_sext : llvalue -> lltype -> llvalue = "LLVMConstSExt" +external const_zext : llvalue -> lltype -> llvalue = "LLVMConstZExt" +external const_fptrunc : llvalue -> lltype -> llvalue = "LLVMConstFPTrunc" +external const_fpext : llvalue -> lltype -> llvalue = "LLVMConstFPExt" +external const_uitofp : llvalue -> lltype -> llvalue = "LLVMConstUIToFP" +external const_sitofp : llvalue -> lltype -> llvalue = "LLVMConstSIToFP" +external const_fptoui : llvalue -> lltype -> llvalue = "LLVMConstFPToUI" +external const_fptosi : llvalue -> lltype -> llvalue = "LLVMConstFPToSI" +external const_ptrtoint : llvalue -> lltype -> llvalue = "LLVMConstPtrToInt" +external const_inttoptr : llvalue -> lltype -> llvalue = "LLVMConstIntToPtr" +external const_bitcast : llvalue -> lltype -> llvalue = "LLVMConstBitCast" +external const_select : llvalue -> llvalue -> llvalue -> llvalue + = "LLVMConstSelect" +external const_extractelement : llvalue -> llvalue -> llvalue + = "LLVMConstExtractElement" +external const_insertelement : llvalue -> llvalue -> llvalue -> llvalue + = "LLVMConstInsertElement" +external const_shufflevector : llvalue -> llvalue -> llvalue -> llvalue + = "LLVMConstShuffleVector" + (*--... Operations on global variables, functions, and aliases (globals) ...--*) external is_declaration : llvalue -> bool = "llvm_is_declaration" external linkage : llvalue -> linkage = "llvm_linkage" diff --git a/bindings/ocaml/llvm/llvm_ocaml.c b/bindings/ocaml/llvm/llvm_ocaml.c index bc6e0b70f09..47f19368921 100644 --- a/bindings/ocaml/llvm/llvm_ocaml.c +++ b/bindings/ocaml/llvm/llvm_ocaml.c @@ -139,10 +139,10 @@ CAMLprim value llvm_param_types(LLVMTypeRef FunTy) { /*--... Operations on struct types .........................................--*/ /* lltype array -> bool -> lltype */ -CAMLprim value llvm_make_struct_type(value ElementTypes, value Packed) { - return (value) LLVMCreateStructType((LLVMTypeRef *) ElementTypes, - Wosize_val(ElementTypes), - Bool_val(Packed)); +CAMLprim LLVMTypeRef llvm_make_struct_type(value ElementTypes, value Packed) { + return LLVMCreateStructType((LLVMTypeRef *) ElementTypes, + Wosize_val(ElementTypes), + Bool_val(Packed)); } /* lltype -> lltype array */ @@ -160,8 +160,8 @@ CAMLprim value llvm_is_packed(LLVMTypeRef StructTy) { /*--... Operations on array, pointer, and vector types .....................--*/ /* lltype -> int -> lltype */ -CAMLprim value llvm_make_array_type(LLVMTypeRef ElementTy, value Count) { - return (value) LLVMCreateArrayType(ElementTy, Int_val(Count)); +CAMLprim LLVMTypeRef llvm_make_array_type(LLVMTypeRef ElementTy, value Count) { + return LLVMCreateArrayType(ElementTy, Int_val(Count)); } /* lltype -> lltype */ @@ -291,9 +291,31 @@ CAMLprim LLVMValueRef llvm_make_struct_constant(value ElementVals, } /* llvalue array -> llvalue */ -CAMLprim value llvm_make_vector_constant(value ElementVals) { - return (value) LLVMGetVectorConstant((LLVMValueRef*) Op_val(ElementVals), - Wosize_val(ElementVals)); +CAMLprim LLVMValueRef llvm_make_vector_constant(value ElementVals) { + return LLVMGetVectorConstant((LLVMValueRef*) Op_val(ElementVals), + Wosize_val(ElementVals)); +} + +/*--... Constant expressions ...............................................--*/ + +/* int_predicate -> llvalue -> llvalue -> llvalue */ +CAMLprim LLVMValueRef llvm_const_icmp(value Pred, + LLVMValueRef LHSConstant, + LLVMValueRef RHSConstant) { + return LLVMConstICmp(Int_val(Pred) + LLVMIntEQ, LHSConstant, RHSConstant); +} + +/* real_predicate -> llvalue -> llvalue -> llvalue */ +CAMLprim LLVMValueRef llvm_const_fcmp(value Pred, + LLVMValueRef LHSConstant, + LLVMValueRef RHSConstant) { + return LLVMConstFCmp(Int_val(Pred), LHSConstant, RHSConstant); +} + +/* llvalue -> llvalue array -> llvalue */ +CAMLprim LLVMValueRef llvm_const_gep(LLVMValueRef ConstantVal, value Indices) { + return LLVMConstGEP(ConstantVal, (LLVMValueRef*) Op_val(Indices), + Wosize_val(Indices)); } /*--... Operations on global variables, functions, and aliases (globals) ...--*/ diff --git a/include/llvm-c/Core.h b/include/llvm-c/Core.h index a9ed27b4201..50f0727ac6c 100644 --- a/include/llvm-c/Core.h +++ b/include/llvm-c/Core.h @@ -250,6 +250,55 @@ LLVMValueRef LLVMGetStructConstant(LLVMValueRef *ConstantVals, unsigned Count, LLVMValueRef LLVMGetVectorConstant(LLVMValueRef *ScalarConstantVals, unsigned Size); +/* Constant expressions */ +LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty); +LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal); +LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal); +LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant); +LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant); +LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant); +LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant); +LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant); +LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant); +LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant); +LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant); +LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant); +LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant); +LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant); +LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant); +LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate, + LLVMValueRef LHSConstant, LLVMValueRef RHSConstant); +LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate, + LLVMValueRef LHSConstant, LLVMValueRef RHSConstant); +LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant); +LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant); +LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant); +LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal, + LLVMValueRef *ConstantIndices, unsigned NumIndices); +LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType); +LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType); +LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType); +LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType); +LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType); +LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType); +LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType); +LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType); +LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType); +LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType); +LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType); +LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType); +LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition, + LLVMValueRef ConstantIfTrue, + LLVMValueRef ConstantIfFalse); +LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant, + LLVMValueRef IndexConstant); +LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant, + LLVMValueRef ElementValueConstant, + LLVMValueRef IndexConstant); +LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant, + LLVMValueRef VectorBConstant, + LLVMValueRef MaskConstant); + /* Operations on global variables, functions, and aliases (globals) */ int LLVMIsDeclaration(LLVMValueRef Global); LLVMLinkage LLVMGetLinkage(LLVMValueRef Global); diff --git a/include/llvm/Constants.h b/include/llvm/Constants.h index 2f3ad9d6ebc..4c497b6f58b 100644 --- a/include/llvm/Constants.h +++ b/include/llvm/Constants.h @@ -599,7 +599,7 @@ public: } /// getSizeOf constant expr - computes the size of a type in a target - /// independent way (Note: the return type is a ULong). + /// independent way (Note: the return type is an i64). /// static Constant *getSizeOf(const Type *Ty); @@ -628,8 +628,8 @@ public: static Constant *getAnd(Constant *C1, Constant *C2); static Constant *getOr(Constant *C1, Constant *C2); static Constant *getXor(Constant *C1, Constant *C2); - static Constant* getICmp(unsigned short pred, Constant* LHS, Constant* RHS); - static Constant* getFCmp(unsigned short pred, Constant* LHS, Constant* RHS); + static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS); + static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS); static Constant *getShl(Constant *C1, Constant *C2); static Constant *getLShr(Constant *C1, Constant *C2); static Constant *getAShr(Constant *C1, Constant *C2); diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp index 28b7e45bf55..7de823b1ba5 100644 --- a/lib/VMCore/Constants.cpp +++ b/lib/VMCore/Constants.cpp @@ -533,7 +533,7 @@ Constant *ConstantExpr::getNeg(Constant *C) { C); } Constant *ConstantExpr::getNot(Constant *C) { - assert(isa(C) && "Cannot NOT a nonintegral type!"); + assert(isa(C->getType()) && "Cannot NOT a nonintegral value!"); return get(Instruction::Xor, C, ConstantInt::getAllOnesValue(C->getType())); } @@ -1675,7 +1675,7 @@ Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) { } Constant *ConstantExpr::getSizeOf(const Type *Ty) { - // sizeof is implemented as: (ulong) gep (Ty*)null, 1 + // sizeof is implemented as: (i64) gep (Ty*)null, 1 Constant *GEPIdx = ConstantInt::get(Type::Int32Ty, 1); Constant *GEP = getGetElementPtr(getNullValue(PointerType::get(Ty)), &GEPIdx, 1); diff --git a/lib/VMCore/Core.cpp b/lib/VMCore/Core.cpp index c63534df6c6..71a3b6db64c 100644 --- a/lib/VMCore/Core.cpp +++ b/lib/VMCore/Core.cpp @@ -266,6 +266,207 @@ LLVMValueRef LLVMGetVectorConstant(LLVMValueRef *ScalarConstantVals, Size)); } +/*--.. Constant expressions ................................................--*/ + +LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) { + return wrap(ConstantExpr::getSizeOf(unwrap(Ty))); +} + +LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) { + return wrap(ConstantExpr::getNeg(unwrap(ConstantVal))); +} + +LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) { + return wrap(ConstantExpr::getNot(unwrap(ConstantVal))); +} + +LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { + return wrap(ConstantExpr::getAdd(unwrap(LHSConstant), + unwrap(RHSConstant))); +} + +LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { + return wrap(ConstantExpr::getSub(unwrap(LHSConstant), + unwrap(RHSConstant))); +} + +LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { + return wrap(ConstantExpr::getMul(unwrap(LHSConstant), + unwrap(RHSConstant))); +} + +LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { + return wrap(ConstantExpr::getUDiv(unwrap(LHSConstant), + unwrap(RHSConstant))); +} + +LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { + return wrap(ConstantExpr::getSDiv(unwrap(LHSConstant), + unwrap(RHSConstant))); +} + +LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { + return wrap(ConstantExpr::getFDiv(unwrap(LHSConstant), + unwrap(RHSConstant))); +} + +LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { + return wrap(ConstantExpr::getURem(unwrap(LHSConstant), + unwrap(RHSConstant))); +} + +LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { + return wrap(ConstantExpr::getSRem(unwrap(LHSConstant), + unwrap(RHSConstant))); +} + +LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { + return wrap(ConstantExpr::getFRem(unwrap(LHSConstant), + unwrap(RHSConstant))); +} + +LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { + return wrap(ConstantExpr::getAnd(unwrap(LHSConstant), + unwrap(RHSConstant))); +} + +LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { + return wrap(ConstantExpr::getOr(unwrap(LHSConstant), + unwrap(RHSConstant))); +} + +LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { + return wrap(ConstantExpr::getXor(unwrap(LHSConstant), + unwrap(RHSConstant))); +} + +LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate, + LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { + return wrap(ConstantExpr::getICmp(Predicate, + unwrap(LHSConstant), + unwrap(RHSConstant))); +} + +LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate, + LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { + return wrap(ConstantExpr::getFCmp(Predicate, + unwrap(LHSConstant), + unwrap(RHSConstant))); +} + +LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { + return wrap(ConstantExpr::getShl(unwrap(LHSConstant), + unwrap(RHSConstant))); +} + +LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { + return wrap(ConstantExpr::getLShr(unwrap(LHSConstant), + unwrap(RHSConstant))); +} + +LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { + return wrap(ConstantExpr::getAShr(unwrap(LHSConstant), + unwrap(RHSConstant))); +} + +LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal, + LLVMValueRef *ConstantIndices, unsigned NumIndices) { + return wrap(ConstantExpr::getGetElementPtr(unwrap(ConstantVal), + unwrap(ConstantIndices, + NumIndices), + NumIndices)); +} + +LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { + return wrap(ConstantExpr::getTrunc(unwrap(ConstantVal), + unwrap(ToType))); +} + +LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { + return wrap(ConstantExpr::getSExt(unwrap(ConstantVal), + unwrap(ToType))); +} + +LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { + return wrap(ConstantExpr::getZExt(unwrap(ConstantVal), + unwrap(ToType))); +} + +LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { + return wrap(ConstantExpr::getFPTrunc(unwrap(ConstantVal), + unwrap(ToType))); +} + +LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { + return wrap(ConstantExpr::getFPExtend(unwrap(ConstantVal), + unwrap(ToType))); +} + +LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { + return wrap(ConstantExpr::getUIToFP(unwrap(ConstantVal), + unwrap(ToType))); +} + +LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { + return wrap(ConstantExpr::getSIToFP(unwrap(ConstantVal), + unwrap(ToType))); +} + +LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { + return wrap(ConstantExpr::getFPToUI(unwrap(ConstantVal), + unwrap(ToType))); +} + +LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { + return wrap(ConstantExpr::getFPToSI(unwrap(ConstantVal), + unwrap(ToType))); +} + +LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { + return wrap(ConstantExpr::getPtrToInt(unwrap(ConstantVal), + unwrap(ToType))); +} + +LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { + return wrap(ConstantExpr::getIntToPtr(unwrap(ConstantVal), + unwrap(ToType))); +} + +LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { + return wrap(ConstantExpr::getBitCast(unwrap(ConstantVal), + unwrap(ToType))); +} + +LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition, + LLVMValueRef ConstantIfTrue, + LLVMValueRef ConstantIfFalse) { + return wrap(ConstantExpr::getSelect(unwrap(ConstantCondition), + unwrap(ConstantIfTrue), + unwrap(ConstantIfFalse))); +} + +LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant, + LLVMValueRef IndexConstant) { + return wrap(ConstantExpr::getExtractElement(unwrap(VectorConstant), + unwrap(IndexConstant))); +} + +LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant, + LLVMValueRef ElementValueConstant, + LLVMValueRef IndexConstant) { + return wrap(ConstantExpr::getInsertElement(unwrap(VectorConstant), + unwrap(ElementValueConstant), + unwrap(IndexConstant))); +} + +LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant, + LLVMValueRef VectorBConstant, + LLVMValueRef MaskConstant) { + return wrap(ConstantExpr::getShuffleVector(unwrap(VectorAConstant), + unwrap(VectorBConstant), + unwrap(MaskConstant))); +} + /*--.. Operations on global variables, functions, and aliases (globals) ....--*/ int LLVMIsDeclaration(LLVMValueRef Global) { diff --git a/test/Bindings/Ocaml/vmcore.ml b/test/Bindings/Ocaml/vmcore.ml index b1d2736f567..907334f5719 100644 --- a/test/Bindings/Ocaml/vmcore.ml +++ b/test/Bindings/Ocaml/vmcore.ml @@ -227,7 +227,108 @@ let test_constants () = let c = make_undef i1_type in ignore (define_global "Const12" c m); insist (i1_type = type_of c); - insist (is_undef c) + insist (is_undef c); + + group "constant arithmetic"; + (* RUN: grep {ConstNeg.*sub} < %t.ll + * RUN: grep {ConstNot.*xor} < %t.ll + * RUN: grep {ConstAdd.*add} < %t.ll + * RUN: grep {ConstSub.*sub} < %t.ll + * RUN: grep {ConstMul.*mul} < %t.ll + * RUN: grep {ConstUDiv.*udiv} < %t.ll + * RUN: grep {ConstSDiv.*sdiv} < %t.ll + * RUN: grep {ConstFDiv.*fdiv} < %t.ll + * RUN: grep {ConstURem.*urem} < %t.ll + * RUN: grep {ConstSRem.*srem} < %t.ll + * RUN: grep {ConstFRem.*frem} < %t.ll + * RUN: grep {ConstAnd.*and} < %t.ll + * RUN: grep {ConstOr.*or} < %t.ll + * RUN: grep {ConstXor.*xor} < %t.ll + * RUN: grep {ConstICmp.*icmp} < %t.ll + * RUN: grep {ConstFCmp.*fcmp} < %t.ll + *) + let void_ptr = make_pointer_type i8_type in + let five = make_int_constant i64_type 5 false in + let ffive = const_uitofp five double_type in + let foldbomb_gv = define_global "FoldBomb" (make_null i8_type) m in + let foldbomb = const_ptrtoint foldbomb_gv i64_type in + let ffoldbomb = const_uitofp foldbomb double_type in + ignore (define_global "ConstNeg" (const_neg foldbomb) m); + ignore (define_global "ConstNot" (const_not foldbomb) m); + ignore (define_global "ConstAdd" (const_add foldbomb five) m); + ignore (define_global "ConstSub" (const_sub foldbomb five) m); + ignore (define_global "ConstMul" (const_mul foldbomb five) m); + ignore (define_global "ConstUDiv" (const_udiv foldbomb five) m); + ignore (define_global "ConstSDiv" (const_sdiv foldbomb five) m); + ignore (define_global "ConstFDiv" (const_fdiv ffoldbomb ffive) m); + ignore (define_global "ConstURem" (const_urem foldbomb five) m); + ignore (define_global "ConstSRem" (const_srem foldbomb five) m); + ignore (define_global "ConstFRem" (const_frem ffoldbomb ffive) m); + ignore (define_global "ConstAnd" (const_and foldbomb five) m); + ignore (define_global "ConstOr" (const_or foldbomb five) m); + ignore (define_global "ConstXor" (const_xor foldbomb five) m); + ignore (define_global "ConstICmp" (const_icmp Icmp_sle foldbomb five) m); + ignore (define_global "ConstFCmp" (const_fcmp Fcmp_ole ffoldbomb ffive) m); + + group "constant casts"; + (* RUN: grep {ConstTrunc.*trunc} < %t.ll + * RUN: grep {ConstSExt.*sext} < %t.ll + * RUN: grep {ConstZExt.*zext} < %t.ll + * RUN: grep {ConstFPTrunc.*fptrunc} < %t.ll + * RUN: grep {ConstFPExt.*fpext} < %t.ll + * RUN: grep {ConstUIToFP.*uitofp} < %t.ll + * RUN: grep {ConstSIToFP.*sitofp} < %t.ll + * RUN: grep {ConstFPToUI.*fptoui} < %t.ll + * RUN: grep {ConstFPToSI.*fptosi} < %t.ll + * RUN: grep {ConstPtrToInt.*ptrtoint} < %t.ll + * RUN: grep {ConstIntToPtr.*inttoptr} < %t.ll + * RUN: grep {ConstBitCast.*bitcast} < %t.ll + *) + let i128_type = make_integer_type 128 in + ignore (define_global "ConstTrunc" (const_trunc (const_add foldbomb five) + i8_type) m); + ignore (define_global "ConstSExt" (const_sext foldbomb i128_type) m); + ignore (define_global "ConstZExt" (const_zext foldbomb i128_type) m); + ignore (define_global "ConstFPTrunc" (const_fptrunc ffoldbomb float_type) m); + ignore (define_global "ConstFPExt" (const_fpext ffoldbomb fp128_type) m); + ignore (define_global "ConstUIToFP" (const_uitofp foldbomb double_type) m); + ignore (define_global "ConstSIToFP" (const_sitofp foldbomb double_type) m); + ignore (define_global "ConstFPToUI" (const_fptoui ffoldbomb i32_type) m); + ignore (define_global "ConstFPToSI" (const_fptosi ffoldbomb i32_type) m); + ignore (define_global "ConstPtrToInt" (const_ptrtoint + (const_gep (make_null (make_pointer_type i8_type)) + [| make_int_constant i32_type 1 false |]) + i32_type) m); + ignore (define_global "ConstIntToPtr" (const_inttoptr (const_add foldbomb five) + void_ptr) m); + ignore (define_global "ConstBitCast" (const_bitcast ffoldbomb i64_type) m); + + group "misc constants"; + (* RUN: grep {ConstSizeOf.*getelementptr.*null} < %t.ll + * RUN: grep {ConstGEP.*getelementptr} < %t.ll + * RUN: grep {ConstSelect.*select} < %t.ll + * RUN: grep {ConstExtractElement.*extractelement} < %t.ll + * RUN: grep {ConstInsertElement.*insertelement} < %t.ll + * RUN: grep {ConstShuffleVector.*shufflevector} < %t.ll + *) + ignore (define_global "ConstSizeOf" (sizeof (make_pointer_type i8_type)) m); + ignore (define_global "ConstGEP" (const_gep foldbomb_gv [| five |]) m); + ignore (define_global "ConstSelect" (const_select + (const_icmp Icmp_sle foldbomb five) + (make_int_constant i8_type (-1) true) + (make_int_constant i8_type 0 true)) m); + let zero = make_int_constant i32_type 0 false in + let one = make_int_constant i32_type 1 false in + ignore (define_global "ConstExtractElement" (const_extractelement + (make_vector_constant [| zero; one; zero; one |]) + (const_trunc foldbomb i32_type)) m); + ignore (define_global "ConstInsertElement" (const_insertelement + (make_vector_constant [| zero; one; zero; one |]) + zero (const_trunc foldbomb i32_type)) m); + ignore (define_global "ConstShuffleVector" (const_shufflevector + (make_vector_constant [| zero; one |]) + (make_vector_constant [| one; zero |]) + (const_bitcast foldbomb (make_vector_type i32_type 2))) m) (*===-- Global Values -----------------------------------------------------===*) -- 2.34.1