authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-09-20 21:16:26-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-09-20 21:16:26-04:00
log0228f8c9fd848ec86aa188229ee82dfb0698ae0d
tree1418bb44201845c36d00f4b6a1de0eab9afb8e12
parentb1e04865cc2313886238e4361dcf2c849b9ecd7b

all parsec tests passing


2 files changed, 72 insertions(+), 33 deletions(-)

src/parsec.cpp+49-33
......@@ -49,6 +49,8 @@ struct Context {
4949
5050 CodeGen *codegen;
5151 ASTContext *ctx;
52
53 HashMap<Buf *, bool, buf_hash, buf_eql_buf> ptr_params;
5254};
5355
5456static AstNode *resolve_record_decl(Context *c, const RecordDecl *record_decl);
......@@ -441,11 +443,16 @@ static bool c_is_float(Context *c, QualType qt) {
441443 }
442444}
443445
444static AstNode *trans_stmt(Context *c, bool result_used, AstNode *block, Stmt *stmt);
446enum TransLRValue {
447 TransLValue,
448 TransRValue,
449};
450
451static AstNode *trans_stmt(Context *c, bool result_used, AstNode *block, Stmt *stmt, TransLRValue lrval);
445452static AstNode *const skip_add_to_block_node = (AstNode *) 0x2;
446453
447static AstNode *trans_expr(Context *c, bool result_used, AstNode *block, Expr *expr) {
448 return trans_stmt(c, result_used, block, expr);
454static AstNode *trans_expr(Context *c, bool result_used, AstNode *block, Expr *expr, TransLRValue lrval) {
455 return trans_stmt(c, result_used, block, expr, lrval);
449456}
450457
451458static AstNode *trans_type(Context *c, const Type *ty, const SourceLocation &source_loc) {
......@@ -789,7 +796,7 @@ static AstNode *trans_qual_type(Context *c, QualType qt, const SourceLocation &s
789796static AstNode *trans_compound_stmt(Context *c, AstNode *parent, CompoundStmt *stmt) {
790797 AstNode *child_block = trans_create_node(c, NodeTypeBlock);
791798 for (CompoundStmt::body_iterator it = stmt->body_begin(), end_it = stmt->body_end(); it != end_it; ++it) {
792 AstNode *child_node = trans_stmt(c, false, child_block, *it);
799 AstNode *child_node = trans_stmt(c, false, child_block, *it, TransRValue);
793800 if (child_node == nullptr)
794801 return nullptr;
795802 if (child_node != skip_add_to_block_node)
......@@ -805,7 +812,7 @@ static AstNode *trans_return_stmt(Context *c, AstNode *block, ReturnStmt *stmt)
805812 return nullptr;
806813 } else {
807814 AstNode *return_node = trans_create_node(c, NodeTypeReturnExpr);
808 return_node->data.return_expr.expr = trans_expr(c, true, block, value_expr);
815 return_node->data.return_expr.expr = trans_expr(c, true, block, value_expr, TransRValue);
809816 if (return_node->data.return_expr.expr == nullptr)
810817 return nullptr;
811818 return return_node;
......@@ -828,15 +835,15 @@ static AstNode *trans_conditional_operator(Context *c, bool result_used, AstNode
828835 Expr *true_expr = stmt->getTrueExpr();
829836 Expr *false_expr = stmt->getFalseExpr();
830837
831 node->data.if_bool_expr.condition = trans_expr(c, true, block, cond_expr);
838 node->data.if_bool_expr.condition = trans_expr(c, true, block, cond_expr, TransRValue);
832839 if (node->data.if_bool_expr.condition == nullptr)
833840 return nullptr;
834841
835 node->data.if_bool_expr.then_block = trans_expr(c, result_used, block, true_expr);
842 node->data.if_bool_expr.then_block = trans_expr(c, result_used, block, true_expr, TransRValue);
836843 if (node->data.if_bool_expr.then_block == nullptr)
837844 return nullptr;
838845
839 node->data.if_bool_expr.else_node = trans_expr(c, result_used, block, false_expr);
846 node->data.if_bool_expr.else_node = trans_expr(c, result_used, block, false_expr, TransRValue);
840847 if (node->data.if_bool_expr.else_node == nullptr)
841848 return nullptr;
842849
......@@ -847,11 +854,11 @@ static AstNode *trans_create_bin_op(Context *c, AstNode *block, Expr *lhs, BinOp
847854 AstNode *node = trans_create_node(c, NodeTypeBinOpExpr);
848855 node->data.bin_op_expr.bin_op = bin_op;
849856
850 node->data.bin_op_expr.op1 = trans_expr(c, true, block, lhs);
857 node->data.bin_op_expr.op1 = trans_expr(c, true, block, lhs, TransRValue);
851858 if (node->data.bin_op_expr.op1 == nullptr)
852859 return nullptr;
853860
854 node->data.bin_op_expr.op2 = trans_expr(c, true, block, rhs);
861 node->data.bin_op_expr.op2 = trans_expr(c, true, block, rhs, TransRValue);
855862 if (node->data.bin_op_expr.op2 == nullptr)
856863 return nullptr;
857864
......@@ -992,7 +999,7 @@ static AstNode *trans_compound_assign_operator(Context *c, bool result_used, Ast
992999 AstNode *child_block = trans_create_node(c, NodeTypeBlock);
9931000
9941001 // const _ref = &lhs;
995 AstNode *lhs = trans_expr(c, true, child_block, stmt->getLHS());
1002 AstNode *lhs = trans_expr(c, true, child_block, stmt->getLHS(), TransLValue);
9961003 if (lhs == nullptr) return nullptr;
9971004 AstNode *addr_of_lhs = trans_create_node_addr_of(c, false, false, lhs);
9981005 // TODO: avoid name collisions with generated variable names
......@@ -1002,7 +1009,7 @@ static AstNode *trans_compound_assign_operator(Context *c, bool result_used, Ast
10021009
10031010 // *_ref = result_type(operation_type(*_ref) >> u5(rhs));
10041011
1005 AstNode *rhs = trans_expr(c, true, child_block, stmt->getRHS());
1012 AstNode *rhs = trans_expr(c, true, child_block, stmt->getRHS(), TransRValue);
10061013 if (rhs == nullptr) return nullptr;
10071014 const SourceLocation &rhs_location = stmt->getRHS()->getLocStart();
10081015 AstNode *rhs_type = qual_type_to_log2_int_ref(c, stmt->getComputationLHSType(), rhs_location);
......@@ -1074,10 +1081,10 @@ static AstNode *trans_compound_assign_operator(Context *c, bool result_used, Ast
10741081static AstNode *trans_implicit_cast_expr(Context *c, AstNode *block, ImplicitCastExpr *stmt) {
10751082 switch (stmt->getCastKind()) {
10761083 case CK_LValueToRValue:
1077 return trans_expr(c, true, block, stmt->getSubExpr());
1084 return trans_expr(c, true, block, stmt->getSubExpr(), TransRValue);
10781085 case CK_IntegralCast:
10791086 {
1080 AstNode *target_node = trans_expr(c, true, block, stmt->getSubExpr());
1087 AstNode *target_node = trans_expr(c, true, block, stmt->getSubExpr(), TransRValue);
10811088 if (target_node == nullptr)
10821089 return nullptr;
10831090 return trans_c_cast(c, stmt->getExprLoc(), stmt->getType(), target_node);
......@@ -1254,10 +1261,13 @@ static AstNode *trans_implicit_cast_expr(Context *c, AstNode *block, ImplicitCas
12541261 zig_unreachable();
12551262}
12561263
1257static AstNode *trans_decl_ref_expr(Context *c, DeclRefExpr *stmt) {
1264static AstNode *trans_decl_ref_expr(Context *c, DeclRefExpr *stmt, TransLRValue lrval) {
12581265 ValueDecl *value_decl = stmt->getDecl();
1259 const char *name = decl_name(value_decl);
1260 return trans_create_node_symbol_str(c, name);
1266 Buf *symbol_name = buf_create_from_str(decl_name(value_decl));
1267 if (lrval == TransLValue) {
1268 c->ptr_params.put(symbol_name, true);
1269 }
1270 return trans_create_node_symbol(c, symbol_name);
12611271}
12621272
12631273static AstNode *trans_unary_operator(Context *c, AstNode *block, UnaryOperator *stmt) {
......@@ -1290,7 +1300,7 @@ static AstNode *trans_unary_operator(Context *c, AstNode *block, UnaryOperator *
12901300 AstNode *node = trans_create_node(c, NodeTypePrefixOpExpr);
12911301 node->data.prefix_op_expr.prefix_op = PrefixOpNegation;
12921302
1293 node->data.prefix_op_expr.primary_expr = trans_expr(c, true, block, op_expr);
1303 node->data.prefix_op_expr.primary_expr = trans_expr(c, true, block, op_expr, TransRValue);
12941304 if (node->data.prefix_op_expr.primary_expr == nullptr)
12951305 return nullptr;
12961306
......@@ -1300,7 +1310,7 @@ static AstNode *trans_unary_operator(Context *c, AstNode *block, UnaryOperator *
13001310 AstNode *node = trans_create_node(c, NodeTypeBinOpExpr);
13011311 node->data.bin_op_expr.op1 = trans_create_node_unsigned(c, 0);
13021312
1303 node->data.bin_op_expr.op2 = trans_expr(c, true, block, op_expr);
1313 node->data.bin_op_expr.op2 = trans_expr(c, true, block, op_expr, TransRValue);
13041314 if (node->data.bin_op_expr.op2 == nullptr)
13051315 return nullptr;
13061316
......@@ -1342,7 +1352,7 @@ static AstNode *trans_local_declaration(Context *c, AstNode *block, DeclStmt *st
13421352 QualType qual_type = var_decl->getTypeSourceInfo()->getType();
13431353 AstNode *init_node = nullptr;
13441354 if (var_decl->hasInit()) {
1345 init_node = trans_expr(c, true, block, var_decl->getInit());
1355 init_node = trans_expr(c, true, block, var_decl->getInit(), TransRValue);
13461356 if (init_node == nullptr)
13471357 return nullptr;
13481358
......@@ -1351,8 +1361,10 @@ static AstNode *trans_local_declaration(Context *c, AstNode *block, DeclStmt *st
13511361 if (type_node == nullptr)
13521362 return nullptr;
13531363
1364 Buf *symbol_name = buf_create_from_str(decl_name(var_decl));
1365
13541366 AstNode *node = trans_create_node_var_decl_local(c, qual_type.isConstQualified(),
1355 buf_create_from_str(decl_name(var_decl)), type_node, init_node);
1367 symbol_name, type_node, init_node);
13561368 block->data.block.statements.append(node);
13571369 continue;
13581370 }
......@@ -1583,18 +1595,18 @@ static AstNode *trans_local_declaration(Context *c, AstNode *block, DeclStmt *st
15831595static AstNode *trans_while_loop(Context *c, AstNode *block, WhileStmt *stmt) {
15841596 AstNode *while_node = trans_create_node(c, NodeTypeWhileExpr);
15851597
1586 while_node->data.while_expr.condition = trans_expr(c, true, block, stmt->getCond());
1598 while_node->data.while_expr.condition = trans_expr(c, true, block, stmt->getCond(), TransRValue);
15871599 if (while_node->data.while_expr.condition == nullptr)
15881600 return nullptr;
15891601
1590 while_node->data.while_expr.body = trans_stmt(c, false, block, stmt->getBody());
1602 while_node->data.while_expr.body = trans_stmt(c, false, block, stmt->getBody(), TransRValue);
15911603 if (while_node->data.while_expr.body == nullptr)
15921604 return nullptr;
15931605
15941606 return while_node;
15951607}
15961608
1597static AstNode *trans_stmt(Context *c, bool result_used, AstNode *block, Stmt *stmt) {
1609static AstNode *trans_stmt(Context *c, bool result_used, AstNode *block, Stmt *stmt, TransLRValue lrvalue) {
15981610 Stmt::StmtClass sc = stmt->getStmtClass();
15991611 switch (sc) {
16001612 case Stmt::ReturnStmtClass:
......@@ -1612,7 +1624,7 @@ static AstNode *trans_stmt(Context *c, bool result_used, AstNode *block, Stmt *s
16121624 case Stmt::ImplicitCastExprClass:
16131625 return trans_implicit_cast_expr(c, block, (ImplicitCastExpr *)stmt);
16141626 case Stmt::DeclRefExprClass:
1615 return trans_decl_ref_expr(c, (DeclRefExpr *)stmt);
1627 return trans_decl_ref_expr(c, (DeclRefExpr *)stmt, lrvalue);
16161628 case Stmt::UnaryOperatorClass:
16171629 return trans_unary_operator(c, block, (UnaryOperator *)stmt);
16181630 case Stmt::DeclStmtClass:
......@@ -2230,9 +2242,9 @@ static void visit_fn_decl(Context *c, const FunctionDecl *fn_decl) {
22302242 }
22312243
22322244 // actual function definition with body
2233
2245 c->ptr_params.clear();
22342246 Stmt *body = fn_decl->getBody();
2235 AstNode *actual_body_node = trans_stmt(c, false, nullptr, body);
2247 AstNode *actual_body_node = trans_stmt(c, false, nullptr, body, TransRValue);
22362248 assert(actual_body_node != skip_add_to_block_node);
22372249 if (actual_body_node == nullptr) {
22382250 emit_warning(c, fn_decl->getLocation(), "unable to translate function");
......@@ -2247,14 +2259,17 @@ static void visit_fn_decl(Context *c, const FunctionDecl *fn_decl) {
22472259 for (size_t i = 0; i < proto_node->data.fn_proto.params.length; i += 1) {
22482260 AstNode *param_node = proto_node->data.fn_proto.params.at(i);
22492261 Buf *good_name = param_node->data.param_decl.name;
2250 // TODO: avoid name collisions
2251 Buf *mangled_name = buf_sprintf("_arg_%s", buf_ptr(good_name));
2252 param_node->data.param_decl.name = mangled_name;
22532262
2254 // var c_name = _mangled_name;
2255 AstNode *parameter_init = trans_create_node_var_decl_local(c, false, good_name, nullptr, trans_create_node_symbol(c, mangled_name));
2263 if (c->ptr_params.maybe_get(good_name) != nullptr) {
2264 // TODO: avoid name collisions
2265 Buf *mangled_name = buf_sprintf("_arg_%s", buf_ptr(good_name));
2266 param_node->data.param_decl.name = mangled_name;
22562267
2257 body_node_with_param_inits->data.block.statements.append(parameter_init);
2268 // var c_name = _mangled_name;
2269 AstNode *parameter_init = trans_create_node_var_decl_local(c, false, good_name, nullptr, trans_create_node_symbol(c, mangled_name));
2270
2271 body_node_with_param_inits->data.block.statements.append(parameter_init);
2272 }
22582273 }
22592274
22602275 for (size_t i = 0; i < actual_body_node->data.block.statements.length; i += 1) {
......@@ -2875,6 +2890,7 @@ int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const ch
28752890 c->visib_mod = VisibModPub;
28762891 c->decl_table.init(8);
28772892 c->macro_table.init(8);
2893 c->ptr_params.init(8);
28782894 c->codegen = codegen;
28792895 c->source_node = source_node;
28802896
test/parsec.zig+23
......@@ -314,4 +314,27 @@ pub fn addCases(cases: &tests.ParseCContext) {
314314 ,
315315 \\pub const LUA_GLOBALSINDEX = -10002;
316316 );
317
318 cases.add("sift right assign",
319 \\int log2(unsigned a) {
320 \\ int i = 0;
321 \\ while (a > 0) {
322 \\ a >>= 100;
323 \\ //i++;
324 \\ }
325 \\ return i;
326 \\}
327 ,
328 \\export fn log2(_arg_a: c_uint) -> c_int {
329 \\ var a = _arg_a;
330 \\ var i: c_int = 0;
331 \\ while (a > c_uint(0)) {
332 \\ {
333 \\ const _ref = &a;
334 \\ *_ref = c_uint(c_uint(*_ref) >> @import("std").math.Log2Int(c_uint)(100));
335 \\ };
336 \\ };
337 \\ return i;
338 \\}
339 );
317340}