mirror of https://github.com/rui314/chibicc
Add |, & and ^ operators
This commit is contained in:
parent
03acdd2ed9
commit
2586416967
3
chibi.h
3
chibi.h
|
@ -90,6 +90,9 @@ typedef enum {
|
|||
ND_PTR_DIFF, // ptr - ptr
|
||||
ND_MUL, // *
|
||||
ND_DIV, // /
|
||||
ND_BITAND, // &
|
||||
ND_BITOR, // |
|
||||
ND_BITXOR, // ^
|
||||
ND_EQ, // ==
|
||||
ND_NE, // !=
|
||||
ND_LT, // <
|
||||
|
|
|
@ -152,6 +152,15 @@ static void gen_binary(Node *node) {
|
|||
printf(" cqo\n");
|
||||
printf(" idiv rdi\n");
|
||||
break;
|
||||
case ND_BITAND:
|
||||
printf(" and rax, rdi\n");
|
||||
break;
|
||||
case ND_BITOR:
|
||||
printf(" or rax, rdi\n");
|
||||
break;
|
||||
case ND_BITXOR:
|
||||
printf(" xor rax, rdi\n");
|
||||
break;
|
||||
case ND_EQ:
|
||||
printf(" cmp rax, rdi\n");
|
||||
printf(" sete al\n");
|
||||
|
|
34
parse.c
34
parse.c
|
@ -176,6 +176,9 @@ static Node *stmt(void);
|
|||
static Node *stmt2(void);
|
||||
static Node *expr(void);
|
||||
static Node *assign(void);
|
||||
static Node *bitand(void);
|
||||
static Node *bitor(void);
|
||||
static Node *bitxor(void);
|
||||
static Node *equality(void);
|
||||
static Node *relational(void);
|
||||
static Node *add(void);
|
||||
|
@ -742,10 +745,10 @@ static Node *expr(void) {
|
|||
return node;
|
||||
}
|
||||
|
||||
// assign = equality (assign-op assign)?
|
||||
// assign = bitor (assign-op assign)?
|
||||
// assign-op = "=" | "+=" | "-=" | "*=" | "/="
|
||||
static Node *assign(void) {
|
||||
Node *node = equality();
|
||||
Node *node = bitor();
|
||||
Token *tok;
|
||||
|
||||
if (tok = consume("="))
|
||||
|
@ -776,6 +779,33 @@ static Node *assign(void) {
|
|||
return node;
|
||||
}
|
||||
|
||||
// bitor = bitxor ("|" bitxor)*
|
||||
static Node *bitor(void) {
|
||||
Node *node = bitxor();
|
||||
Token *tok;
|
||||
while (tok = consume("|"))
|
||||
node = new_binary(ND_BITOR, node, bitxor(), tok);
|
||||
return node;
|
||||
}
|
||||
|
||||
// bitxor = bitand ("^" bitand)*
|
||||
static Node *bitxor(void) {
|
||||
Node *node = bitand();
|
||||
Token *tok;
|
||||
while (tok = consume("^"))
|
||||
node = new_binary(ND_BITXOR, node, bitxor(), tok);
|
||||
return node;
|
||||
}
|
||||
|
||||
// bitand = equality ("&" equality)*
|
||||
static Node *bitand(void) {
|
||||
Node *node = equality();
|
||||
Token *tok;
|
||||
while (tok = consume("&"))
|
||||
node = new_binary(ND_BITAND, node, equality(), tok);
|
||||
return node;
|
||||
}
|
||||
|
||||
// equality = relational ("==" relational | "!=" relational)*
|
||||
static Node *equality(void) {
|
||||
Node *node = relational();
|
||||
|
|
12
tests
12
tests
|
@ -397,6 +397,18 @@ int main() {
|
|||
assert(-1, ~0, "~0");
|
||||
assert(0, ~-1, "~-1");
|
||||
|
||||
assert(0, 0&1, "0&1");
|
||||
assert(1, 3&1, "3&1");
|
||||
assert(3, 7&3, "7&3");
|
||||
assert(10, -1&10, " -1&10");
|
||||
|
||||
assert(1, 0|1, "0|1");
|
||||
assert(0b10011, 0b10000|0b00011, "01000|0b0011");
|
||||
|
||||
assert(0, 0^0, "0^0");
|
||||
assert(0, 0b1111^0b1111, "0b1111^0b1111");
|
||||
assert(0b110100, 0b111000^0b001100, "0b111000^0b001100");
|
||||
|
||||
printf("OK\n");
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue