| author | |
| committer | |
| log | 9ca9a2c5540683a54bae597c59152d06d095beef |
| tree | 6a0b9c01c5bfaeaeaa230ea184baf9561f3ed03c |
| parent | 821907317eb77a96dc53adf20ac705b4501e2ab8 |
4 files changed, 107 insertions(+), 23 deletions(-)
README.md+4-2| ... | ... | @@ -31,7 +31,9 @@ readable, safe, optimal, and concise code to solve any computing problem. |
| 31 | 31 | |
| 32 | 32 | ## Roadmap |
| 33 | 33 | |
| 34 | * empty function and return with no expression | |
| 34 | * pub/private/export functions | |
| 35 | * make sure that release mode optimizes out empty private functions | |
| 36 | * test framework to test for compile errors | |
| 35 | 37 | * Simple .so library |
| 36 | 38 | * Multiple files |
| 37 | 39 | * figure out integers |
| ... | ... | @@ -87,7 +89,7 @@ Statement : ExpressionStatement | ReturnStatement |
| 87 | 89 | |
| 88 | 90 | ExpressionStatement : Expression token(Semicolon) |
| 89 | 91 | |
| 90 | ReturnStatement : token(Return) Expression token(Semicolon) | |
| 92 | ReturnStatement : token(Return) option(Expression) token(Semicolon) | |
| 91 | 93 | |
| 92 | 94 | Expression : token(Number) | token(String) | token(Unreachable) | FnCall |
| 93 | 95 |
src/codegen.cpp+81-6| ... | ... | @@ -80,9 +80,14 @@ struct TypeNode { |
| 80 | 80 | TypeTableEntry *entry; |
| 81 | 81 | }; |
| 82 | 82 | |
| 83 | struct FnDefNode { | |
| 84 | bool add_implicit_return; | |
| 85 | }; | |
| 86 | ||
| 83 | 87 | struct CodeGenNode { |
| 84 | 88 | union { |
| 85 | 89 | TypeNode type_node; // for NodeTypeType |
| 90 | FnDefNode fn_def_node; // for NodeTypeFnDef | |
| 86 | 91 | } data; |
| 87 | 92 | }; |
| 88 | 93 | |
| ... | ... | @@ -275,6 +280,60 @@ static void find_declarations(CodeGen *g, AstNode *node) { |
| 275 | 280 | } |
| 276 | 281 | } |
| 277 | 282 | |
| 283 | static void check_fn_def_control_flow(CodeGen *g, AstNode *node) { | |
| 284 | // Follow the execution flow and make sure the code returns appropriately. | |
| 285 | // * A `return` statement in an unreachable type function should be an error. | |
| 286 | // * Control flow should not be able to reach the end of an unreachable type function. | |
| 287 | // * Functions that have a type other than void should not return without a value. | |
| 288 | // * void functions without explicit return statements at the end need the | |
| 289 | // add_implicit_return flag set on the codegen node. | |
| 290 | assert(node->type == NodeTypeFnDef); | |
| 291 | AstNode *proto_node = node->data.fn_def.fn_proto; | |
| 292 | assert(proto_node->type == NodeTypeFnProto); | |
| 293 | AstNode *return_type_node = proto_node->data.fn_proto.return_type; | |
| 294 | assert(return_type_node->type == NodeTypeType); | |
| 295 | ||
| 296 | node->codegen_node = allocate<CodeGenNode>(1); | |
| 297 | FnDefNode *codegen_fn_def = &node->codegen_node->data.fn_def_node; | |
| 298 | ||
| 299 | assert(return_type_node->codegen_node); | |
| 300 | TypeTableEntry *type_entry = return_type_node->codegen_node->data.type_node.entry; | |
| 301 | assert(type_entry); | |
| 302 | TypeId type_id = type_entry->id; | |
| 303 | ||
| 304 | AstNode *body_node = node->data.fn_def.body; | |
| 305 | assert(body_node->type == NodeTypeBlock); | |
| 306 | ||
| 307 | // TODO once we understand types, do this pass after type checking, and | |
| 308 | // if an expression has an unreachable value then stop looking at statements after | |
| 309 | // it. then we can remove the check to `unreachable` in the end of this function. | |
| 310 | bool prev_statement_return = false; | |
| 311 | for (int i = 0; i < body_node->data.block.statements.length; i += 1) { | |
| 312 | AstNode *statement_node = body_node->data.block.statements.at(i); | |
| 313 | if (statement_node->type == NodeTypeStatementReturn) { | |
| 314 | if (type_id == TypeIdUnreachable) { | |
| 315 | add_node_error(g, statement_node, | |
| 316 | buf_sprintf("return statement in function with unreachable return type")); | |
| 317 | return; | |
| 318 | } else { | |
| 319 | prev_statement_return = true; | |
| 320 | } | |
| 321 | } else if (prev_statement_return) { | |
| 322 | add_node_error(g, statement_node, | |
| 323 | buf_sprintf("unreachable code")); | |
| 324 | } | |
| 325 | } | |
| 326 | ||
| 327 | if (!prev_statement_return) { | |
| 328 | if (type_id == TypeIdVoid) { | |
| 329 | codegen_fn_def->add_implicit_return = true; | |
| 330 | } else if (type_id != TypeIdUnreachable) { | |
| 331 | add_node_error(g, node, | |
| 332 | buf_sprintf("control reaches end of non-void function")); | |
| 333 | } | |
| 334 | } | |
| 335 | } | |
| 336 | ||
| 278 | 337 | static void analyze_node(CodeGen *g, AstNode *node) { |
| 279 | 338 | switch (node->type) { |
| 280 | 339 | case NodeTypeRoot: |
| ... | ... | @@ -299,6 +358,8 @@ static void analyze_node(CodeGen *g, AstNode *node) { |
| 299 | 358 | AstNode *proto_node = node->data.fn_def.fn_proto; |
| 300 | 359 | assert(proto_node->type == NodeTypeFnProto); |
| 301 | 360 | analyze_node(g, proto_node); |
| 361 | ||
| 362 | check_fn_def_control_flow(g, node); | |
| 302 | 363 | break; |
| 303 | 364 | } |
| 304 | 365 | case NodeTypeFnDecl: |
| ... | ... | @@ -331,7 +392,9 @@ static void analyze_node(CodeGen *g, AstNode *node) { |
| 331 | 392 | } |
| 332 | 393 | break; |
| 333 | 394 | case NodeTypeStatementReturn: |
| 334 | analyze_node(g, node->data.statement_return.expression); | |
| 395 | if (node->data.statement_return.expression) { | |
| 396 | analyze_node(g, node->data.statement_return.expression); | |
| 397 | } | |
| 335 | 398 | break; |
| 336 | 399 | case NodeTypeExpression: |
| 337 | 400 | switch (node->data.expression.type) { |
| ... | ... | @@ -545,7 +608,7 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *expr_node) { |
| 545 | 608 | zig_unreachable(); |
| 546 | 609 | } |
| 547 | 610 | |
| 548 | static void gen_block(CodeGen *g, AstNode *block_node) { | |
| 611 | static void gen_block(CodeGen *g, AstNode *block_node, bool add_implicit_return) { | |
| 549 | 612 | assert(block_node->type == NodeTypeBlock); |
| 550 | 613 | |
| 551 | 614 | llvm::DILexicalBlock *di_block = g->dbuilder->createLexicalBlock(g->block_scopes.last(), |
| ... | ... | @@ -558,10 +621,15 @@ static void gen_block(CodeGen *g, AstNode *block_node) { |
| 558 | 621 | case NodeTypeStatementReturn: |
| 559 | 622 | { |
| 560 | 623 | AstNode *expr_node = statement_node->data.statement_return.expression; |
| 561 | LLVMValueRef value = gen_expr(g, expr_node); | |
| 624 | if (expr_node) { | |
| 625 | LLVMValueRef value = gen_expr(g, expr_node); | |
| 562 | 626 | |
| 563 | add_debug_source_node(g, statement_node); | |
| 564 | LLVMBuildRet(g->builder, value); | |
| 627 | add_debug_source_node(g, statement_node); | |
| 628 | LLVMBuildRet(g->builder, value); | |
| 629 | } else { | |
| 630 | add_debug_source_node(g, statement_node); | |
| 631 | LLVMBuildRetVoid(g->builder); | |
| 632 | } | |
| 565 | 633 | break; |
| 566 | 634 | } |
| 567 | 635 | case NodeTypeExpression: |
| ... | ... | @@ -583,6 +651,10 @@ static void gen_block(CodeGen *g, AstNode *block_node) { |
| 583 | 651 | } |
| 584 | 652 | } |
| 585 | 653 | |
| 654 | if (add_implicit_return) { | |
| 655 | LLVMBuildRetVoid(g->builder); | |
| 656 | } | |
| 657 | ||
| 586 | 658 | g->block_scopes.pop(); |
| 587 | 659 | } |
| 588 | 660 | |
| ... | ... | @@ -685,7 +757,10 @@ void code_gen(CodeGen *g) { |
| 685 | 757 | LLVMBasicBlockRef entry_block = LLVMAppendBasicBlock(fn, "entry"); |
| 686 | 758 | LLVMPositionBuilderAtEnd(g->builder, entry_block); |
| 687 | 759 | |
| 688 | gen_block(g, fn_def_node->data.fn_def.body); | |
| 760 | CodeGenNode *codegen_node = fn_def_node->codegen_node; | |
| 761 | assert(codegen_node); | |
| 762 | bool add_implicit_return = codegen_node->data.fn_def_node.add_implicit_return; | |
| 763 | gen_block(g, fn_def_node->data.fn_def.body, add_implicit_return); | |
| 689 | 764 | |
| 690 | 765 | g->block_scopes.pop(); |
| 691 | 766 | } |
src/parser.cpp+16-14| ... | ... | @@ -128,7 +128,8 @@ void ast_print(AstNode *node, int indent) { |
| 128 | 128 | break; |
| 129 | 129 | case NodeTypeStatementReturn: |
| 130 | 130 | fprintf(stderr, "ReturnStatement\n"); |
| 131 | ast_print(node->data.statement_return.expression, indent + 2); | |
| 131 | if (node->data.statement_return.expression) | |
| 132 | ast_print(node->data.statement_return.expression, indent + 2); | |
| 132 | 133 | break; |
| 133 | 134 | case NodeTypeExternBlock: |
| 134 | 135 | { |
| ... | ... | @@ -258,7 +259,7 @@ void ast_invalid_token_error(ParseContext *pc, Token *token) { |
| 258 | 259 | ast_error(token, "invalid token: '%s'", buf_ptr(&token_value)); |
| 259 | 260 | } |
| 260 | 261 | |
| 261 | static AstNode *ast_parse_expression(ParseContext *pc, int token_index, int *new_token_index); | |
| 262 | static AstNode *ast_parse_expression(ParseContext *pc, int *token_index, bool mandatory); | |
| 262 | 263 | |
| 263 | 264 | |
| 264 | 265 | static void ast_expect_token(ParseContext *pc, Token *token, TokenId token_id) { |
| ... | ... | @@ -374,7 +375,7 @@ static void ast_parse_fn_call_param_list(ParseContext *pc, int token_index, int |
| 374 | 375 | } |
| 375 | 376 | |
| 376 | 377 | for (;;) { |
| 377 | AstNode *expr = ast_parse_expression(pc, token_index, &token_index); | |
| 378 | AstNode *expr = ast_parse_expression(pc, &token_index, true); | |
| 378 | 379 | params->append(expr); |
| 379 | 380 | |
| 380 | 381 | Token *token = &pc->tokens->at(token_index); |
| ... | ... | @@ -411,28 +412,29 @@ static AstNode *ast_parse_fn_call(ParseContext *pc, int token_index, int *new_to |
| 411 | 412 | /* |
| 412 | 413 | Expression : token(Number) | token(String) | token(Unreachable) | FnCall |
| 413 | 414 | */ |
| 414 | static AstNode *ast_parse_expression(ParseContext *pc, int token_index, int *new_token_index) { | |
| 415 | Token *token = &pc->tokens->at(token_index); | |
| 415 | static AstNode *ast_parse_expression(ParseContext *pc, int *token_index, bool mandatory) { | |
| 416 | Token *token = &pc->tokens->at(*token_index); | |
| 416 | 417 | AstNode *node = ast_create_node(NodeTypeExpression, token); |
| 417 | 418 | if (token->id == TokenIdKeywordUnreachable) { |
| 418 | 419 | node->data.expression.type = AstNodeExpressionTypeUnreachable; |
| 419 | token_index += 1; | |
| 420 | *token_index += 1; | |
| 420 | 421 | } else if (token->id == TokenIdSymbol) { |
| 421 | 422 | node->data.expression.type = AstNodeExpressionTypeFnCall; |
| 422 | node->data.expression.data.fn_call = ast_parse_fn_call(pc, token_index, &token_index); | |
| 423 | node->data.expression.data.fn_call = ast_parse_fn_call(pc, *token_index, token_index); | |
| 423 | 424 | } else if (token->id == TokenIdNumberLiteral) { |
| 424 | 425 | node->data.expression.type = AstNodeExpressionTypeNumber; |
| 425 | 426 | ast_buf_from_token(pc, token, &node->data.expression.data.number); |
| 426 | token_index += 1; | |
| 427 | *token_index += 1; | |
| 427 | 428 | } else if (token->id == TokenIdStringLiteral) { |
| 428 | 429 | node->data.expression.type = AstNodeExpressionTypeString; |
| 429 | 430 | parse_string_literal(pc, token, &node->data.expression.data.string); |
| 430 | token_index += 1; | |
| 431 | } else { | |
| 431 | *token_index += 1; | |
| 432 | } else if (mandatory) { | |
| 432 | 433 | ast_invalid_token_error(pc, token); |
| 434 | } else { | |
| 435 | return nullptr; | |
| 433 | 436 | } |
| 434 | 437 | |
| 435 | *new_token_index = token_index; | |
| 436 | 438 | return node; |
| 437 | 439 | } |
| 438 | 440 | |
| ... | ... | @@ -441,14 +443,14 @@ Statement : ExpressionStatement | ReturnStatement ; |
| 441 | 443 | |
| 442 | 444 | ExpressionStatement : Expression token(Semicolon) ; |
| 443 | 445 | |
| 444 | ReturnStatement : token(Return) Expression token(Semicolon) ; | |
| 446 | ReturnStatement : token(Return) option(Expression) token(Semicolon) ; | |
| 445 | 447 | */ |
| 446 | 448 | static AstNode *ast_parse_statement(ParseContext *pc, int token_index, int *new_token_index) { |
| 447 | 449 | Token *token = &pc->tokens->at(token_index); |
| 448 | 450 | if (token->id == TokenIdKeywordReturn) { |
| 449 | 451 | AstNode *node = ast_create_node(NodeTypeStatementReturn, token); |
| 450 | 452 | token_index += 1; |
| 451 | node->data.statement_return.expression = ast_parse_expression(pc, token_index, &token_index); | |
| 453 | node->data.statement_return.expression = ast_parse_expression(pc, &token_index, false); | |
| 452 | 454 | |
| 453 | 455 | Token *semicolon = &pc->tokens->at(token_index); |
| 454 | 456 | token_index += 1; |
| ... | ... | @@ -460,7 +462,7 @@ static AstNode *ast_parse_statement(ParseContext *pc, int token_index, int *new_ |
| 460 | 462 | token->id == TokenIdKeywordUnreachable || |
| 461 | 463 | token->id == TokenIdNumberLiteral) |
| 462 | 464 | { |
| 463 | AstNode *node = ast_parse_expression(pc, token_index, &token_index); | |
| 465 | AstNode *node = ast_parse_expression(pc, &token_index, true); | |
| 464 | 466 | |
| 465 | 467 | Token *semicolon = &pc->tokens->at(token_index); |
| 466 | 468 | token_index += 1; |
test/standalone.cpp+6-1| ... | ... | @@ -66,7 +66,12 @@ static void add_all_test_cases(void) { |
| 66 | 66 | fn exit(code: i32) -> unreachable; |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | fn empty_function_1() {} | |
| 70 | fn empty_function_2() { return; } | |
| 71 | ||
| 69 | 72 | fn _start() -> unreachable { |
| 73 | empty_function_1(); | |
| 74 | empty_function_2(); | |
| 70 | 75 | this_is_a_function(); |
| 71 | 76 | } |
| 72 | 77 | |
| ... | ... | @@ -86,7 +91,7 @@ static void add_all_test_cases(void) { |
| 86 | 91 | /** |
| 87 | 92 | * multi line doc comment |
| 88 | 93 | */ |
| 89 | fn another_function() -> i32 { return 0; } | |
| 94 | fn another_function() {} | |
| 90 | 95 | |
| 91 | 96 | /// this is a documentation comment |
| 92 | 97 | /// doc comment line 2 |