Linux SPARSE checker discussions
 help / color / mirror / Atom feed
* sparse-llvm the result of expression add assign when target is a pointer should be a pointer
@ 2017-03-05  7:29 Dibyendu Majumdar
  2017-03-05 10:53 ` Luc Van Oostenryck
  0 siblings, 1 reply; 2+ messages in thread
From: Dibyendu Majumdar @ 2017-03-05  7:29 UTC (permalink / raw)
  To: Linux-Sparse

Hi,

I am starting a new thread on this issue. When processing following
simple example:

char *incr(char *p) {
 char *tmp = p;
 tmp += 5;
 return tmp;
}

The linearizer is taking the type of the expression 'tmp += 5' from
the src operand which is unsigned long, instead of taking the type
from the target operand which is char pointer. This results in
following sequence:

incr:
.L000001CB177AD5C8:
        <entry-point>
        ptrcast.64  %r3 <- (64) %arg1
        add.64      %r4 <- %r3, $5
        cast.64     %r5 <- (64) %r4
        ret.64      %r5

However if we make the change shown below then this output is produced
which is correct I believe as in add or sub operations, if either left
or right operand is a pointer and the other operand is an int then the
result should be a pointer.

incr:
.L000001633F0BEE58:
        <entry-point>
        ptrcast.64  %r3 <- (64) %arg1
        add.64      %r4 <- %r3, $5
        ptrcast.64  %r5 <- (64) %r4
        ret.64      %r5

The change required is in linearize_assignment() function. The
suggested version is shown below.


static pseudo_t linearize_assignment(struct entrypoint *ep, struct
expression *expr)
{
  ...
  int opcode;
  struct symbol *ctype = src->ctype;
  if (!src)
   return VOID_PSEUDO;
  oldvalue = cast_pseudo(ep, oldvalue, src->ctype, expr->ctype);
  opcode = opcode_sign(op_trans[expr->op - SPECIAL_BASE], src->ctype);
  if (opcode == OP_ADD || opcode == OP_SUB) {
   if (is_ptr_type(target->ctype)) {
    ctype = target->ctype;
   }
  dst = add_binary_op(ep, ctype, opcode, oldvalue, value);
  value = cast_pseudo(ep, dst, expr->ctype, ctype);
  ...
}

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-03-05 10:53 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-03-05  7:29 sparse-llvm the result of expression add assign when target is a pointer should be a pointer Dibyendu Majumdar
2017-03-05 10:53 ` Luc Van Oostenryck

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox