Skip to content

Commit

Permalink
Implement bitwise shift operation
Browse files Browse the repository at this point in the history
  • Loading branch information
leewei05 authored and Lai-YT committed May 13, 2024
1 parent bd18bad commit bb8f496
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 2 deletions.
2 changes: 2 additions & 0 deletions include/operator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ enum class BinaryOperator : std::uint8_t {
kAnd,
kXor,
kOr,
kShl,
kShr,
};

enum class UnaryOperator : std::uint8_t {
Expand Down
4 changes: 2 additions & 2 deletions parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,8 @@ add_expr: mul_expr { $$ = $1; }

/* 6.5.7 Bitwise shift operators */
shift_expr: add_expr { $$ = $1; }
| shift_expr SHIFT_LEFT add_expr
| shift_expr SHIFT_RIGHT add_expr
| shift_expr SHIFT_LEFT add_expr { $$ = std::make_unique<BinaryExprNode>(Loc(@2), BinaryOperator::kShl, $1, $3); }
| shift_expr SHIFT_RIGHT add_expr { $$ = std::make_unique<BinaryExprNode>(Loc(@2), BinaryOperator::kShr, $1, $3); }
;

/* 6.5.8 Relational operators */
Expand Down
4 changes: 4 additions & 0 deletions src/ast_dumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ std::string GetBinaryOperator(BinaryOperator op) {
return "^";
case BinaryOperator::kOr:
return "|";
case BinaryOperator::kShl:
return "<<";
case BinaryOperator::kShr:
return ">>";
default:
return "Unknown";
}
Expand Down
7 changes: 7 additions & 0 deletions src/qbe_ir_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ std::string GetBinaryOperator(BinaryOperator op) {
return "xor";
case BinaryOperator::kOr:
return "or";
case BinaryOperator::kShl:
return "shl";
// NOTE: Arithmetic shift right (sar) is akin to dividing by a power of two
// for non-negative numbers. For negatives, it's implementation-defined, so
// we opt for arithmetic shifting.
case BinaryOperator::kShr:
return "sar";
default:
return "Unknown";
}
Expand Down
4 changes: 4 additions & 0 deletions test/codegen/bin_expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@ int main() {
__builtin_print(5 | 4);
__builtin_print(5 ^ 4);
__builtin_print(5 & 4);
__builtin_print(1 << 16);
__builtin_print(-1 << 31);
__builtin_print(65536 >> 16);
__builtin_print(-1 >> 16);
return 0;
}
4 changes: 4 additions & 0 deletions test/codegen/bin_expr.exp
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
5
1
4
65536
-2147483648
1
-1
2 changes: 2 additions & 0 deletions test/typecheck/bin_expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ int main() {
1 & 4;
1 | 2;
1 ^ 3;
1 << 16;
65536 >> 16;
}
8 changes: 8 additions & 0 deletions test/typecheck/bin_expr.exp
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,11 @@ ProgramNode <1:1>
BinaryExprNode <10:5> int ^
IntConstExprNode <10:3> 1: int
IntConstExprNode <10:7> 3: int
ExprStmtNode <11:3>
BinaryExprNode <11:5> int <<
IntConstExprNode <11:3> 1: int
IntConstExprNode <11:8> 16: int
ExprStmtNode <12:3>
BinaryExprNode <12:9> int >>
IntConstExprNode <12:3> 65536: int
IntConstExprNode <12:12> 16: int

0 comments on commit bb8f496

Please sign in to comment.