mirror of https://github.com/rui314/chibicc
Add ~ operator
This commit is contained in:
parent
6b88bcb306
commit
46a96d6862
|
@ -106,6 +106,7 @@ typedef enum {
|
|||
ND_ADDR, // unary &
|
||||
ND_DEREF, // unary *
|
||||
ND_NOT, // !
|
||||
ND_BITNOT, // ~
|
||||
ND_RETURN, // "return"
|
||||
ND_IF, // "if"
|
||||
ND_FOR, // "for" or "while"
|
||||
|
|
|
@ -216,6 +216,10 @@ static void gen_expr(Node *node) {
|
|||
println(" sete %%al");
|
||||
println(" movzx %%al, %%rax");
|
||||
return;
|
||||
case ND_BITNOT:
|
||||
gen_expr(node->lhs);
|
||||
println(" not %%rax");
|
||||
return;
|
||||
case ND_FUNCALL: {
|
||||
int nargs = 0;
|
||||
for (Node *arg = node->args; arg; arg = arg->next) {
|
||||
|
|
5
parse.c
5
parse.c
|
@ -888,7 +888,7 @@ static Node *cast(Token **rest, Token *tok) {
|
|||
return unary(rest, tok);
|
||||
}
|
||||
|
||||
// unary = ("+" | "-" | "*" | "&" | "!") cast
|
||||
// unary = ("+" | "-" | "*" | "&" | "!" | "~") cast
|
||||
// | ("++" | "--") unary
|
||||
// | postfix
|
||||
static Node *unary(Token **rest, Token *tok) {
|
||||
|
@ -907,6 +907,9 @@ static Node *unary(Token **rest, Token *tok) {
|
|||
if (equal(tok, "!"))
|
||||
return new_unary(ND_NOT, cast(rest, tok->next), tok);
|
||||
|
||||
if (equal(tok, "~"))
|
||||
return new_unary(ND_BITNOT, cast(rest, tok->next), tok);
|
||||
|
||||
// Read ++i as i+=1
|
||||
if (equal(tok, "++"))
|
||||
return to_assign(new_add(unary(rest, tok->next), new_num(1, tok), tok));
|
||||
|
|
|
@ -71,6 +71,9 @@ int main() {
|
|||
ASSERT(4, sizeof(!(char)0));
|
||||
ASSERT(4, sizeof(!(long)0));
|
||||
|
||||
ASSERT(-1, ~0);
|
||||
ASSERT(0, ~-1);
|
||||
|
||||
printf("OK\n");
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue