authorgravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2017-11-13 20:49:53-07:00
committergravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2017-11-13 20:49:53-07:00
logc1fde0e8c45e4ba20fbc3306238c5e7627c2dfd9
tree4e408d57e2c9c221c4f38f56252c6494a82c2aed
parent6356724057ffc94fec03c697ae99bed32d33d2f7

parsec supports bitshift operators


2 files changed, 27 insertions(+), 4 deletions(-)

src/parsec.cpp+17-4
......@@ -987,6 +987,21 @@ static AstNode *trans_create_assign(Context *c, bool result_used, AstNode *block
987987 }
988988}
989989
990static AstNode *trans_create_shift_op(Context *c, AstNode *block, QualType result_type, Expr *lhs_expr, BinOpType bin_op, Expr *rhs_expr) {
991 const SourceLocation &rhs_location = rhs_expr->getLocStart();
992 AstNode *rhs_type = qual_type_to_log2_int_ref(c, result_type, rhs_location);
993 // lhs >> u5(rh)
994
995 AstNode *lhs = trans_expr(c, true, block, lhs_expr, TransLValue);
996 if (lhs == nullptr) return nullptr;
997
998 AstNode *rhs = trans_expr(c, true, block, rhs_expr, TransRValue);
999 if (rhs == nullptr) return nullptr;
1000 AstNode *coerced_rhs = trans_create_node_fn_call_1(c, rhs_type, rhs);
1001
1002 return trans_create_node_bin_op(c, lhs, bin_op, coerced_rhs);
1003}
1004
9901005static AstNode *trans_binary_operator(Context *c, bool result_used, AstNode *block, BinaryOperator *stmt) {
9911006 switch (stmt->getOpcode()) {
9921007 case BO_PtrMemD:
......@@ -1038,11 +1053,9 @@ static AstNode *trans_binary_operator(Context *c, bool result_used, AstNode *blo
10381053 qual_type_has_wrapping_overflow(c, stmt->getType()) ? BinOpTypeSubWrap : BinOpTypeSub,
10391054 stmt->getRHS());
10401055 case BO_Shl:
1041 emit_warning(c, stmt->getLocStart(), "TODO handle more C binary operators: BO_Shl");
1042 return nullptr;
1056 return trans_create_shift_op(c, block, stmt->getType(), stmt->getLHS(), BinOpTypeBitShiftLeft, stmt->getRHS());
10431057 case BO_Shr:
1044 emit_warning(c, stmt->getLocStart(), "TODO handle more C binary operators: BO_Shr");
1045 return nullptr;
1058 return trans_create_shift_op(c, block, stmt->getType(), stmt->getLHS(), BinOpTypeBitShiftRight, stmt->getRHS());
10461059 case BO_LT:
10471060 return trans_create_bin_op(c, block, stmt->getLHS(), BinOpTypeCmpLessThan, stmt->getRHS());
10481061 case BO_GT:
test/parsec.zig+10
......@@ -620,6 +620,16 @@ pub fn addCases(cases: &tests.ParseCContext) {
620620 \\}
621621 );
622622
623 cases.addC("bitshift",
624 \\int foo(void) {
625 \\ return (1 << 2) >> 1;
626 \\}
627 ,
628 \\export fn foo() -> c_int {
629 \\ return (1 << @import("std").math.Log2Int(c_int)(2)) >> @import("std").math.Log2Int(c_int)(1);
630 \\}
631 );
632
623633 cases.addC("duplicate typedef",
624634 \\typedef long foo;
625635 \\typedef int bar;