authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-11-24 13:37:14-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-11-24 13:37:14-07:00
log925c805d4b2431b1c3140ba9c9dd7fc81de8a7d7
treee443118694e1705665a9f251a031e26dd5e732d6
parentc2e5d50027779468c66608f445f3ab0f77d34c15

add unreachable expression

now creating .o file from hello.zig correctly

8 files changed, 95 insertions(+), 41 deletions(-)

README.md+2-4
......@@ -40,8 +40,6 @@ readable, safe, optimal, and concise code to solve any computing problem.
4040
4141## Roadmap
4242
43 * Hello, world.
44 - Produce .o file.
4543 * Produce executable file instead of .o file.
4644 * Add debugging symbols.
4745 * Debug/Release mode.
......@@ -87,7 +85,7 @@ ParamDeclList : token(LParen) list(ParamDecl, token(Comma)) token(RParen)
8785
8886ParamDecl : token(Symbol) token(Colon) Type
8987
90Type : token(Symbol) | PointerType
88Type : token(Symbol) | PointerType | token(Unreachable)
9189
9290PointerType : token(Star) token(Const) Type | token(Star) token(Mut) Type
9391
......@@ -99,7 +97,7 @@ ExpressionStatement : Expression token(Semicolon)
9997
10098ReturnStatement : token(Return) Expression token(Semicolon)
10199
102Expression : token(Number) | token(String) | FnCall
100Expression : token(Number) | token(String) | token(Unreachable) | FnCall
103101
104102FnCall : token(Symbol) token(LParen) list(Expression, token(Comma)) token(RParen)
105103```
src/codegen.cpp+69-30
......@@ -26,9 +26,14 @@ struct CodeGen {
2626 HashMap<Buf *, LLVMValueRef, buf_hash, buf_eql_buf> str_table;
2727};
2828
29struct TypeNode {
30 LLVMTypeRef type_ref;
31 bool is_unreachable;
32};
33
2934struct CodeGenNode {
3035 union {
31 LLVMTypeRef type_ref; // for NodeTypeType
36 TypeNode type_node; // for NodeTypeType
3237 } data;
3338};
3439
......@@ -54,9 +59,16 @@ static void add_node_error(CodeGen *g, AstNode *node, Buf *msg) {
5459static LLVMTypeRef to_llvm_type(AstNode *type_node) {
5560 assert(type_node->type == NodeTypeType);
5661 assert(type_node->codegen_node);
57 assert(type_node->codegen_node->data.type_ref);
62 assert(type_node->codegen_node->data.type_node.type_ref);
5863
59 return type_node->codegen_node->data.type_ref;
64 return type_node->codegen_node->data.type_node.type_ref;
65}
66
67
68static bool type_is_unreachable(AstNode *type_node) {
69 assert(type_node->type == NodeTypeType);
70 return type_node->data.type.type == AstNodeTypeTypePrimitive &&
71 buf_eql_str(&type_node->data.type.primitive_name, "unreachable");
6072}
6173
6274static void analyze_node(CodeGen *g, AstNode *node) {
......@@ -83,13 +95,18 @@ static void analyze_node(CodeGen *g, AstNode *node) {
8395 AstNode *param_type = param_node->data.param_decl.type;
8496 fn_param_values[param_i] = to_llvm_type(param_type);
8597 }
86 LLVMTypeRef return_type = to_llvm_type(fn_proto->data.fn_proto.return_type);
98 AstNode *return_type_node = fn_proto->data.fn_proto.return_type;
99 LLVMTypeRef return_type = to_llvm_type(return_type_node);
87100
88101 LLVMTypeRef fn_type = LLVMFunctionType(return_type, fn_param_values, params->length, 0);
89102 LLVMValueRef fn_val = LLVMAddFunction(g->mod, buf_ptr(name), fn_type);
90103 LLVMSetLinkage(fn_val, LLVMExternalLinkage);
91104 LLVMSetFunctionCallConv(fn_val, LLVMCCallConv);
92105
106 if (type_is_unreachable(return_type_node)) {
107 LLVMAddFunctionAttr(fn_val, LLVMNoReturnAttribute);
108 }
109
93110 FnTableEntry *fn_table_entry = allocate<FnTableEntry>(1);
94111 fn_table_entry->fn_value = fn_val;
95112 fn_table_entry->proto_node = fn_proto;
......@@ -131,33 +148,43 @@ static void analyze_node(CodeGen *g, AstNode *node) {
131148 analyze_node(g, node->data.param_decl.type);
132149 break;
133150 case NodeTypeType:
134 node->codegen_node = allocate<CodeGenNode>(1);
135 switch (node->data.type.type) {
136 case AstNodeTypeTypePrimitive:
137 {
138 Buf *name = &node->data.type.primitive_name;
139 if (buf_eql_str(name, "u8")) {
140 node->codegen_node->data.type_ref = LLVMInt8Type();
141 } else if (buf_eql_str(name, "i32")) {
142 node->codegen_node->data.type_ref = LLVMInt32Type();
143 } else if (buf_eql_str(name, "void")) {
144 node->codegen_node->data.type_ref = LLVMVoidType();
145 } else {
146 add_node_error(g, node,
147 buf_sprintf("invalid type name: '%s'", buf_ptr(name)));
148 node->codegen_node->data.type_ref = LLVMInt8Type();
151 {
152 node->codegen_node = allocate<CodeGenNode>(1);
153 TypeNode *type_node = &node->codegen_node->data.type_node;
154 switch (node->data.type.type) {
155 case AstNodeTypeTypePrimitive:
156 {
157 Buf *name = &node->data.type.primitive_name;
158 if (buf_eql_str(name, "u8")) {
159 type_node->type_ref = LLVMInt8Type();
160 } else if (buf_eql_str(name, "i32")) {
161 type_node->type_ref = LLVMInt32Type();
162 } else if (buf_eql_str(name, "void")) {
163 type_node->type_ref = LLVMVoidType();
164 } else if (buf_eql_str(name, "unreachable")) {
165 type_node->type_ref = LLVMVoidType();
166 type_node->is_unreachable = true;
167 } else {
168 add_node_error(g, node,
169 buf_sprintf("invalid type name: '%s'", buf_ptr(name)));
170 type_node->type_ref = LLVMVoidType();
171 }
172 break;
149173 }
150 break;
151 }
152 case AstNodeTypeTypePointer:
153 {
154 analyze_node(g, node->data.type.child_type);
155 node->codegen_node->data.type_ref = LLVMPointerType(
156 node->data.type.child_type->codegen_node->data.type_ref, 0);
157 break;
158 }
174 case AstNodeTypeTypePointer:
175 {
176 analyze_node(g, node->data.type.child_type);
177 TypeNode *child_type_node = &node->data.type.child_type->codegen_node->data.type_node;
178 if (child_type_node->is_unreachable) {
179 add_node_error(g, node,
180 buf_create_from_str("pointer to unreachable not allowed"));
181 }
182 type_node->type_ref = LLVMPointerType(child_type_node->type_ref, 0);
183 break;
184 }
185 }
186 break;
159187 }
160 break;
161188 case NodeTypeBlock:
162189 for (int i = 0; i < node->data.block.statements.length; i += 1) {
163190 AstNode *child = node->data.block.statements.at(i);
......@@ -183,6 +210,8 @@ static void analyze_node(CodeGen *g, AstNode *node) {
183210 case AstNodeExpressionTypeFnCall:
184211 analyze_node(g, node->data.expression.data.fn_call);
185212 break;
213 case AstNodeExpressionTypeUnreachable:
214 break;
186215 }
187216 break;
188217 case NodeTypeFnCall:
......@@ -235,7 +264,11 @@ static LLVMValueRef gen_fn_call(CodeGen *g, AstNode *fn_call_node) {
235264 LLVMValueRef result = LLVMBuildCall(g->builder, fn_table_entry->fn_value,
236265 param_values, actual_param_count, "");
237266
238 return result;
267 if (type_is_unreachable(fn_table_entry->proto_node->data.fn_proto.return_type)) {
268 return LLVMBuildUnreachable(g->builder);
269 } else {
270 return result;
271 }
239272}
240273
241274static LLVMValueRef find_or_create_string(CodeGen *g, Buf *str) {
......@@ -280,6 +313,8 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *expr_node) {
280313 }
281314 case AstNodeExpressionTypeFnCall:
282315 return gen_fn_call(g, expr_node->data.expression.data.fn_call);
316 case AstNodeExpressionTypeUnreachable:
317 return LLVMBuildUnreachable(g->builder);
283318 }
284319 zig_unreachable();
285320}
......@@ -333,6 +368,10 @@ void code_gen(CodeGen *g) {
333368 LLVMTypeRef function_type = LLVMFunctionType(ret_type, param_types, fn_proto->params.length, 0);
334369 LLVMValueRef fn = LLVMAddFunction(g->mod, buf_ptr(&fn_proto->name), function_type);
335370
371 if (type_is_unreachable(fn_proto->return_type)) {
372 LLVMAddFunctionAttr(fn, LLVMNoReturnAttribute);
373 }
374
336375 LLVMBasicBlockRef entry_block = LLVMAppendBasicBlock(fn, "entry");
337376 LLVMPositionBuilderAtEnd(g->builder, entry_block);
338377
src/main.cpp+3-1
......@@ -102,7 +102,8 @@ static int build(const char *arg0, const char *in_file, const char *out_file, Zi
102102 } else {
103103 for (int i = 0; i < errors->length; i += 1) {
104104 ErrorMsg *err = &errors->at(i);
105 fprintf(stderr, "Error: Line %d, column %d: %s\n", err->line_start, err->column_start,
105 fprintf(stderr, "Error: Line %d, column %d: %s\n",
106 err->line_start + 1, err->column_start + 1,
106107 buf_ptr(err->msg));
107108 }
108109 return 1;
......@@ -115,6 +116,7 @@ static int build(const char *arg0, const char *in_file, const char *out_file, Zi
115116 fprintf(stderr, "\nLink:\n");
116117 fprintf(stderr, "------------------\n");
117118 code_gen_link(codegen, false, out_file);
119 fprintf(stderr, "OK\n");
118120
119121 return 0;
120122}
src/parser.cpp+14-4
......@@ -236,7 +236,7 @@ static void ast_expect_token(ParseContext *pc, Token *token, TokenId token_id) {
236236}
237237
238238/*
239Type : token(Symbol) | PointerType;
239Type : token(Symbol) | PointerType | token(Unreachable)
240240PointerType : token(Star) token(Const) Type | token(Star) token(Mut) Type;
241241*/
242242static AstNode *ast_parse_type(ParseContext *pc, int token_index, int *new_token_index) {
......@@ -245,7 +245,10 @@ static AstNode *ast_parse_type(ParseContext *pc, int token_index, int *new_token
245245
246246 AstNode *node = ast_create_node(NodeTypeType, token);
247247
248 if (token->id == TokenIdSymbol) {
248 if (token->id == TokenIdKeywordUnreachable) {
249 node->data.type.type = AstNodeTypeTypePrimitive;
250 buf_init_from_str(&node->data.type.primitive_name, "unreachable");
251 } else if (token->id == TokenIdSymbol) {
249252 node->data.type.type = AstNodeTypeTypePrimitive;
250253 ast_buf_from_token(pc, token, &node->data.type.primitive_name);
251254 } else if (token->id == TokenIdStar) {
......@@ -373,10 +376,16 @@ static AstNode *ast_parse_fn_call(ParseContext *pc, int token_index, int *new_to
373376 return node;
374377}
375378
379/*
380Expression : token(Number) | token(String) | token(Unreachable) | FnCall
381*/
376382static AstNode *ast_parse_expression(ParseContext *pc, int token_index, int *new_token_index) {
377383 Token *token = &pc->tokens->at(token_index);
378384 AstNode *node = ast_create_node(NodeTypeExpression, token);
379 if (token->id == TokenIdSymbol) {
385 if (token->id == TokenIdKeywordUnreachable) {
386 node->data.expression.type = AstNodeExpressionTypeUnreachable;
387 token_index += 1;
388 } else if (token->id == TokenIdSymbol) {
380389 node->data.expression.type = AstNodeExpressionTypeFnCall;
381390 node->data.expression.data.fn_call = ast_parse_fn_call(pc, token_index, &token_index);
382391 } else if (token->id == TokenIdNumberLiteral) {
......@@ -402,7 +411,7 @@ ExpressionStatement : Expression token(Semicolon) ;
402411
403412ReturnStatement : token(Return) Expression token(Semicolon) ;
404413
405Expression : token(Number) | token(String) | FnCall ;
414Expression : token(Number) | token(String) | token(Unreachable) | FnCall
406415
407416FnCall : token(Symbol) token(LParen) list(Expression, token(Comma)) token(RParen) ;
408417*/
......@@ -420,6 +429,7 @@ static AstNode *ast_parse_statement(ParseContext *pc, int token_index, int *new_
420429 ast_expect_token(pc, semicolon, TokenIdSemicolon);
421430 } else if (token->id == TokenIdSymbol ||
422431 token->id == TokenIdStringLiteral ||
432 token->id == TokenIdKeywordUnreachable ||
423433 token->id == TokenIdNumberLiteral)
424434 {
425435 node->data.statement.type = AstNodeStatementTypeExpression;
src/parser.hpp+1
......@@ -94,6 +94,7 @@ enum AstNodeExpressionType {
9494 AstNodeExpressionTypeNumber,
9595 AstNodeExpressionTypeString,
9696 AstNodeExpressionTypeFnCall,
97 AstNodeExpressionTypeUnreachable,
9798};
9899
99100struct AstNodeExpression {
src/tokenizer.cpp+3
......@@ -152,6 +152,8 @@ static void end_token(Tokenize *t) {
152152 t->cur_tok->id = TokenIdKeywordConst;
153153 } else if (mem_eql_str(token_mem, token_len, "extern")) {
154154 t->cur_tok->id = TokenIdKeywordExtern;
155 } else if (mem_eql_str(token_mem, token_len, "unreachable")) {
156 t->cur_tok->id = TokenIdKeywordUnreachable;
155157 }
156158
157159 t->cur_tok = nullptr;
......@@ -311,6 +313,7 @@ static const char * token_name(Token *token) {
311313 case TokenIdKeywordMut: return "Mut";
312314 case TokenIdKeywordReturn: return "Return";
313315 case TokenIdKeywordExtern: return "Extern";
316 case TokenIdKeywordUnreachable: return "Unreachable";
314317 case TokenIdLParen: return "LParen";
315318 case TokenIdRParen: return "RParen";
316319 case TokenIdComma: return "Comma";
src/tokenizer.hpp+1
......@@ -18,6 +18,7 @@ enum TokenId {
1818 TokenIdKeywordMut,
1919 TokenIdKeywordConst,
2020 TokenIdKeywordExtern,
21 TokenIdKeywordUnreachable,
2122 TokenIdLParen,
2223 TokenIdRParen,
2324 TokenIdComma,
test/hello.zig+2-2
......@@ -1,9 +1,9 @@
11extern {
22 fn puts(s: *mut u8) -> i32;
3 fn exit(code: i32);
3 fn exit(code: i32) -> unreachable;
44}
55
6fn _start() {
6fn _start() -> unreachable {
77 puts("Hello, world!");
88 exit(0);
99}