Skip to content

Commit

Permalink
add signext/zeroext attributes to small integer ccall arguments.
Browse files Browse the repository at this point in the history
fixes #978
  • Loading branch information
JeffBezanson committed Jan 6, 2013
1 parent a86d6d0 commit c8041e7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
24 changes: 23 additions & 1 deletion src/ccall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ static Value *julia_to_native(Type *ty, jl_value_t *jt, Value *jv,
false);
}

static jl_value_t *jl_signed_type=NULL;

// --- code generator for ccall itself ---

// ccall(pointer, rettype, (argtypes...), args...)
Expand Down Expand Up @@ -368,12 +370,31 @@ static Value *emit_ccall(jl_value_t **args, size_t nargs, jl_codectx_t *ctx)
size_t i;
bool haspointers = false;
bool isVa = false;
for(i=0; i < jl_tuple_len(tt); i++) {
size_t nargt = jl_tuple_len(tt);
std::vector<AttributeWithIndex> attrs;
for(i=0; i < nargt; i++) {
jl_value_t *tti = jl_tupleref(tt,i);
if (jl_is_seq_type(tti)) {
isVa = true;
tti = jl_tparam0(tti);
}
if (jl_is_bits_type(tti)) {
// see pull req #978. need to annotate signext/zeroext for
// small integer arguments.
jl_bits_type_t *bt = (jl_bits_type_t*)tti;
if (bt->nbits < 32) {
if (jl_signed_type == NULL) {
jl_signed_type = jl_get_global(jl_core_module,jl_symbol("Signed"));
}
Attributes::AttrVal av;
if (jl_signed_type && jl_subtype(tti, jl_signed_type, 0))
av = Attributes::SExt;
else
av = Attributes::ZExt;
attrs.push_back(AttributeWithIndex::get(getGlobalContext(), i+1,
ArrayRef<Attributes::AttrVal>(&av, 1)));
}
}
Type *t = julia_type_to_llvm(tti);
if (t == NULL) {
JL_GC_POP();
Expand Down Expand Up @@ -524,6 +545,7 @@ static Value *emit_ccall(jl_value_t **args, size_t nargs, jl_codectx_t *ctx)
ArrayRef<Value*>(&argvals[0],(nargs-3)/2));
if (cc != CallingConv::C)
((CallInst*)result)->setCallingConv(cc);
((CallInst*)result)->setAttributes(AttrListPtr::get(getGlobalContext(), ArrayRef<AttributeWithIndex>(attrs)));

// restore temp argument area stack pointer
if (haspointers) {
Expand Down
1 change: 1 addition & 0 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "llvm/Intrinsics.h"
#include "llvm/PassManager.h"
#include "llvm/Analysis/Verifier.h"
#include "llvm/Attributes.h"
#if defined(LLVM_VERSION_MAJOR) && LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 2
#include "llvm/DebugInfo.h"
#include "llvm/DIBuilder.h"
Expand Down
2 changes: 1 addition & 1 deletion test/ccall.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
ccall_test_func(x) = ccall((:testUcharX, "./libccalltest"), Int32, (Int8,), x)
ccall_test_func(x) = ccall((:testUcharX, "./libccalltest"), Int32, (Uint8,), x)
@assert ccall_test_func(3) == 1
@assert ccall_test_func(259) == 1

0 comments on commit c8041e7

Please sign in to comment.