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...@@ -987,6 +987,21 @@ static AstNode *trans_create_assign(Context *c, bool result_used, AstNode *block
987 }987 }
988}988}
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
990static AstNode *trans_binary_operator(Context *c, bool result_used, AstNode *block, BinaryOperator *stmt) {1005static AstNode *trans_binary_operator(Context *c, bool result_used, AstNode *block, BinaryOperator *stmt) {
991 switch (stmt->getOpcode()) {1006 switch (stmt->getOpcode()) {
992 case BO_PtrMemD:1007 case BO_PtrMemD:
...@@ -1038,11 +1053,9 @@ static AstNode *trans_binary_operator(Context *c, bool result_used, AstNode *blo...@@ -1038,11 +1053,9 @@ static AstNode *trans_binary_operator(Context *c, bool result_used, AstNode *blo
1038 qual_type_has_wrapping_overflow(c, stmt->getType()) ? BinOpTypeSubWrap : BinOpTypeSub,1053 qual_type_has_wrapping_overflow(c, stmt->getType()) ? BinOpTypeSubWrap : BinOpTypeSub,
1039 stmt->getRHS());1054 stmt->getRHS());
1040 case BO_Shl:1055 case BO_Shl:
1041 emit_warning(c, stmt->getLocStart(), "TODO handle more C binary operators: BO_Shl");1056 return trans_create_shift_op(c, block, stmt->getType(), stmt->getLHS(), BinOpTypeBitShiftLeft, stmt->getRHS());
1042 return nullptr;
1043 case BO_Shr:1057 case BO_Shr:
1044 emit_warning(c, stmt->getLocStart(), "TODO handle more C binary operators: BO_Shr");1058 return trans_create_shift_op(c, block, stmt->getType(), stmt->getLHS(), BinOpTypeBitShiftRight, stmt->getRHS());
1045 return nullptr;
1046 case BO_LT:1059 case BO_LT:
1047 return trans_create_bin_op(c, block, stmt->getLHS(), BinOpTypeCmpLessThan, stmt->getRHS());1060 return trans_create_bin_op(c, block, stmt->getLHS(), BinOpTypeCmpLessThan, stmt->getRHS());
1048 case BO_GT:1061 case BO_GT:
test/parsec.zig+10
...@@ -620,6 +620,16 @@ pub fn addCases(cases: &tests.ParseCContext) {...@@ -620,6 +620,16 @@ pub fn addCases(cases: &tests.ParseCContext) {
620 \\}620 \\}
621 );621 );
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
623 cases.addC("duplicate typedef",633 cases.addC("duplicate typedef",
624 \\typedef long foo;634 \\typedef long foo;
625 \\typedef int bar;635 \\typedef int bar;