Skip to content

unary operators + and ~ don't promote their operand #169

@Bricktech2000

Description

@Bricktech2000
// prog.c
#include <stdio.h>

int main(void) {
  char c;
  printf("%zd", sizeof c);
  printf("%zd", sizeof +c);
  printf("%zd", sizeof ~c);
  printf("%zd", sizeof -c);
}

from my reading of the standard the output should be '1444' and GCC agrees but chibicc outputs '1114'.

$ gcc prog.c && ./a.out
1444
$ ./chibicc prog.c && ./a.out
1114

ISO/IEC 9899:TC3 $6.5.3.3 'Unary arithmetic operators':

  1. The result of the unary + operator is the value of its (promoted) operand. The integer promotions are performed on the operand, and the result has the promoted type.
  2. The result of the unary - operator is the negative of its (promoted) operand. The integer promotions are performed on the operand, and the result has the promoted type.
  3. The result of the ~ operator is the bitwise complement of its (promoted) operand (that is, each bit in the result is set if and only if the corresponding bit in the converted operand is not set). The integer promotions are performed on the operand, and the result has the promoted type. [...]

after a quick look at some disassemblies it looks like chibicc always zero- or sign- extends small integer types into eax anyway, so I couldn't get codegen to misbehave and the issue remains isolated to the typechecker.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions