All of lore.kernel.org
 help / color / mirror / Atom feed
From: Dibyendu Majumdar <mobile@majumdar.org.uk>
To: Linux-Sparse <linux-sparse@vger.kernel.org>
Subject: sparse-llvm functions must be cast to correct type before calling
Date: Thu, 16 Mar 2017 20:58:25 +0000	[thread overview]
Message-ID: <CACXZuxcmizMoxj=xGAJYbbEbCHQC=4E7spC4MUqT1=0d+-NiAw@mail.gmail.com> (raw)

Hi,

The following test (adapted from snippet posted by Linus) fails in sparse-llvm:

typedef unsigned int (*binop_t)(int, int);
typedef unsigned int (*unop_t)(int);
extern int printf(const char *, ...);
#define BINOP 0
#define UNOP 1
static unsigned int execute(int type, void *fn, int arg1, int arg2)
{
 if (type == BINOP)
  return ((binop_t)fn)(arg1,arg2);
 return ((unop_t)fn)(arg1);
}
static unsigned int unary(int arg1)
{
 return arg1+3;
}
int main(void)
{
 return execute(UNOP, unary, 3, 10) == 6 ? 0 : 1;
}

To resolve this before calling a function we need to cast it to the
expected type. This can be done like this in output_op_call():

 struct symbol *ftype = get_function_basetype(insn->fntype);

...

 LLVMTypeRef function_type = symbol_type(C, fn->module, ftype);
 LLVMTypeRef fptr_type = LLVMPointerType(function_type, 0);
 LLVMTypeRef bytep = LLVMPointerType(LLVMInt8Type(), 0);
 target = LLVMBuildBitCast(fn->builder, func, bytep, name);
 target = LLVMBuildBitCast(fn->builder, target, fptr_type, name);
 target = LLVMBuildCall(fn->builder, target, args, n_arg, name);
 insn->target->priv = target;


Thanks and Regards
Dibyendu

             reply	other threads:[~2017-03-16 21:05 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-16 20:58 Dibyendu Majumdar [this message]
2017-03-19  0:20 ` sparse-llvm functions must be cast to correct type before calling Luc Van Oostenryck

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CACXZuxcmizMoxj=xGAJYbbEbCHQC=4E7spC4MUqT1=0d+-NiAw@mail.gmail.com' \
    --to=mobile@majumdar.org.uk \
    --cc=linux-sparse@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.