2015-11-25 04:00:38 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015 Andrew Kelley
|
|
|
|
*
|
|
|
|
* This file is part of zig, which is MIT licensed.
|
|
|
|
* See http://opensource.org/licenses/MIT
|
|
|
|
*/
|
|
|
|
|
2015-11-24 13:47:25 +08:00
|
|
|
#include "codegen.hpp"
|
|
|
|
#include "hash_map.hpp"
|
2015-11-25 04:00:38 +08:00
|
|
|
#include "zig_llvm.hpp"
|
2015-11-25 04:51:36 +08:00
|
|
|
#include "os.hpp"
|
2015-11-25 10:07:33 +08:00
|
|
|
#include "config.h"
|
2015-11-30 17:11:31 +08:00
|
|
|
#include "error.hpp"
|
2015-12-01 10:58:53 +08:00
|
|
|
#include "analyze.hpp"
|
2015-12-01 15:50:11 +08:00
|
|
|
#include "errmsg.hpp"
|
2015-12-01 00:14:58 +08:00
|
|
|
|
2015-11-24 13:47:25 +08:00
|
|
|
#include <stdio.h>
|
2015-12-01 05:43:09 +08:00
|
|
|
#include <errno.h>
|
2015-11-25 10:07:33 +08:00
|
|
|
|
2015-12-01 10:58:53 +08:00
|
|
|
CodeGen *codegen_create(Buf *root_source_dir) {
|
2015-11-24 13:47:25 +08:00
|
|
|
CodeGen *g = allocate<CodeGen>(1);
|
2015-11-24 17:43:45 +08:00
|
|
|
g->str_table.init(32);
|
2015-11-25 14:44:41 +08:00
|
|
|
g->link_table.init(32);
|
2015-12-01 10:58:53 +08:00
|
|
|
g->import_table.init(32);
|
2015-11-25 13:32:26 +08:00
|
|
|
g->build_type = CodeGenBuildTypeDebug;
|
2015-12-01 10:58:53 +08:00
|
|
|
g->root_source_dir = root_source_dir;
|
2015-12-11 08:42:47 +08:00
|
|
|
|
2015-11-24 13:47:25 +08:00
|
|
|
return g;
|
|
|
|
}
|
|
|
|
|
2015-11-25 13:32:26 +08:00
|
|
|
void codegen_set_build_type(CodeGen *g, CodeGenBuildType build_type) {
|
|
|
|
g->build_type = build_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
void codegen_set_is_static(CodeGen *g, bool is_static) {
|
|
|
|
g->is_static = is_static;
|
|
|
|
}
|
|
|
|
|
2015-12-01 10:58:53 +08:00
|
|
|
void codegen_set_verbose(CodeGen *g, bool verbose) {
|
|
|
|
g->verbose = verbose;
|
|
|
|
}
|
|
|
|
|
2015-12-01 16:06:10 +08:00
|
|
|
void codegen_set_errmsg_color(CodeGen *g, ErrColor err_color) {
|
|
|
|
g->err_color = err_color;
|
|
|
|
}
|
|
|
|
|
2015-11-25 13:32:26 +08:00
|
|
|
void codegen_set_strip(CodeGen *g, bool strip) {
|
|
|
|
g->strip_debug_symbols = strip;
|
|
|
|
}
|
|
|
|
|
2015-11-28 12:24:11 +08:00
|
|
|
void codegen_set_out_type(CodeGen *g, OutType out_type) {
|
|
|
|
g->out_type = out_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
void codegen_set_out_name(CodeGen *g, Buf *out_name) {
|
2015-12-01 10:58:53 +08:00
|
|
|
g->root_out_name = out_name;
|
2015-11-28 12:24:11 +08:00
|
|
|
}
|
|
|
|
|
2015-12-16 03:44:42 +08:00
|
|
|
void codegen_set_libc_path(CodeGen *g, Buf *libc_path) {
|
|
|
|
g->libc_path = libc_path;
|
|
|
|
}
|
|
|
|
|
2015-12-01 00:14:58 +08:00
|
|
|
static LLVMValueRef gen_expr(CodeGen *g, AstNode *expr_node);
|
2015-12-23 04:22:40 +08:00
|
|
|
static LLVMValueRef gen_lvalue(CodeGen *g, AstNode *expr_node, AstNode *node, TypeTableEntry **out_type_entry);
|
|
|
|
static LLVMValueRef gen_field_access_expr(CodeGen *g, AstNode *node, bool is_lvalue);
|
2016-01-02 15:06:06 +08:00
|
|
|
static LLVMValueRef gen_var_decl_raw(CodeGen *g, AstNode *source_node, AstNodeVariableDeclaration *var_decl,
|
|
|
|
BlockContext *block_context, bool unwrap_maybe, LLVMValueRef *init_val);
|
|
|
|
static LLVMValueRef gen_assign_raw(CodeGen *g, AstNode *source_node, BinOpType bin_op,
|
|
|
|
LLVMValueRef target_ref, LLVMValueRef value,
|
|
|
|
TypeTableEntry *op1_type, TypeTableEntry *op2_type);
|
2015-12-13 13:55:29 +08:00
|
|
|
|
2015-11-24 13:47:25 +08:00
|
|
|
|
2015-12-13 13:55:29 +08:00
|
|
|
static TypeTableEntry *get_type_for_type_node(CodeGen *g, AstNode *type_node) {
|
2015-11-24 17:43:45 +08:00
|
|
|
assert(type_node->type == NodeTypeType);
|
|
|
|
assert(type_node->codegen_node);
|
2015-11-25 10:07:33 +08:00
|
|
|
assert(type_node->codegen_node->data.type_node.entry);
|
2015-12-13 13:55:29 +08:00
|
|
|
return type_node->codegen_node->data.type_node.entry;
|
2015-11-25 10:07:33 +08:00
|
|
|
}
|
|
|
|
|
2015-12-13 13:55:29 +08:00
|
|
|
static LLVMTypeRef fn_proto_type_from_type_node(CodeGen *g, AstNode *type_node) {
|
|
|
|
TypeTableEntry *type_entry = get_type_for_type_node(g, type_node);
|
2015-11-24 17:43:45 +08:00
|
|
|
|
2015-12-13 13:55:29 +08:00
|
|
|
if (type_entry->id == TypeTableEntryIdStruct || type_entry->id == TypeTableEntryIdArray) {
|
|
|
|
return get_pointer_to_type(g, type_entry, true)->type_ref;
|
|
|
|
} else {
|
|
|
|
return type_entry->type_ref;
|
|
|
|
}
|
2015-11-25 04:37:14 +08:00
|
|
|
}
|
|
|
|
|
2015-12-13 13:55:29 +08:00
|
|
|
static LLVMZigDIType *to_llvm_debug_type(CodeGen *g, AstNode *type_node) {
|
|
|
|
TypeTableEntry *type_entry = get_type_for_type_node(g, type_node);
|
|
|
|
return type_entry->di_type;
|
2015-12-04 05:29:19 +08:00
|
|
|
}
|
|
|
|
|
2015-12-13 13:55:29 +08:00
|
|
|
|
2015-12-04 05:29:19 +08:00
|
|
|
static bool type_is_unreachable(CodeGen *g, AstNode *type_node) {
|
2015-12-14 02:59:34 +08:00
|
|
|
return get_type_for_type_node(g, type_node)->id == TypeTableEntryIdUnreachable;
|
2015-12-04 05:29:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool is_param_decl_type_void(CodeGen *g, AstNode *param_decl_node) {
|
|
|
|
assert(param_decl_node->type == NodeTypeParamDecl);
|
2015-12-14 02:59:34 +08:00
|
|
|
return get_type_for_type_node(g, param_decl_node->data.param_decl.type)->id == TypeTableEntryIdVoid;
|
2015-12-04 05:29:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int count_non_void_params(CodeGen *g, ZigList<AstNode *> *params) {
|
|
|
|
int result = 0;
|
|
|
|
for (int i = 0; i < params->length; i += 1) {
|
|
|
|
if (!is_param_decl_type_void(g, params->at(i)))
|
|
|
|
result += 1;
|
|
|
|
}
|
|
|
|
return result;
|
2015-11-24 17:43:45 +08:00
|
|
|
}
|
|
|
|
|
2015-11-25 10:54:10 +08:00
|
|
|
static void add_debug_source_node(CodeGen *g, AstNode *node) {
|
2015-12-15 14:49:56 +08:00
|
|
|
if (!g->cur_block_context)
|
|
|
|
return;
|
2015-12-08 13:11:04 +08:00
|
|
|
LLVMZigSetCurrentDebugLocation(g->builder, node->line + 1, node->column + 1,
|
|
|
|
g->cur_block_context->di_scope);
|
2015-11-25 10:54:10 +08:00
|
|
|
}
|
|
|
|
|
2015-12-12 15:10:37 +08:00
|
|
|
static LLVMValueRef find_or_create_string(CodeGen *g, Buf *str, bool c) {
|
2015-11-24 17:43:45 +08:00
|
|
|
auto entry = g->str_table.maybe_get(str);
|
|
|
|
if (entry) {
|
|
|
|
return entry->value;
|
|
|
|
}
|
2015-12-12 15:10:37 +08:00
|
|
|
LLVMValueRef text = LLVMConstString(buf_ptr(str), buf_len(str), !c);
|
2015-11-28 06:46:06 +08:00
|
|
|
LLVMValueRef global_value = LLVMAddGlobal(g->module, LLVMTypeOf(text), "");
|
2015-11-24 18:06:10 +08:00
|
|
|
LLVMSetLinkage(global_value, LLVMPrivateLinkage);
|
2015-11-24 17:43:45 +08:00
|
|
|
LLVMSetInitializer(global_value, text);
|
|
|
|
LLVMSetGlobalConstant(global_value, true);
|
2015-11-24 18:06:10 +08:00
|
|
|
LLVMSetUnnamedAddr(global_value, true);
|
2015-11-24 17:43:45 +08:00
|
|
|
g->str_table.put(str, global_value);
|
|
|
|
|
|
|
|
return global_value;
|
2015-11-24 15:35:23 +08:00
|
|
|
}
|
|
|
|
|
2015-12-02 12:19:38 +08:00
|
|
|
static TypeTableEntry *get_expr_type(AstNode *node) {
|
2016-01-02 15:06:06 +08:00
|
|
|
TypeTableEntry *cast_type = node->codegen_node->expr_node.implicit_cast.after_type;
|
2015-12-16 09:21:59 +08:00
|
|
|
return cast_type ? cast_type : node->codegen_node->expr_node.type_entry;
|
2015-12-02 12:19:38 +08:00
|
|
|
}
|
|
|
|
|
2015-11-30 05:39:11 +08:00
|
|
|
static LLVMValueRef gen_fn_call_expr(CodeGen *g, AstNode *node) {
|
|
|
|
assert(node->type == NodeTypeFnCallExpr);
|
|
|
|
|
2016-01-05 07:57:22 +08:00
|
|
|
FnTableEntry *fn_table_entry;
|
|
|
|
AstNode *fn_ref_expr = node->data.fn_call_expr.fn_ref_expr;
|
|
|
|
TypeTableEntry *struct_type;
|
|
|
|
AstNode *first_param_expr;
|
|
|
|
if (fn_ref_expr->type == NodeTypeFieldAccessExpr) {
|
|
|
|
Buf *name = &fn_ref_expr->data.field_access_expr.field_name;
|
|
|
|
first_param_expr = fn_ref_expr->data.field_access_expr.struct_expr;
|
|
|
|
struct_type = get_expr_type(first_param_expr);
|
|
|
|
fn_table_entry = struct_type->data.structure.fn_table.get(name);
|
|
|
|
} else if (fn_ref_expr->type == NodeTypeSymbol) {
|
|
|
|
Buf *name = hack_get_fn_call_name(g, fn_ref_expr);
|
|
|
|
struct_type = nullptr;
|
|
|
|
first_param_expr = nullptr;
|
|
|
|
fn_table_entry = g->cur_fn->import_entry->fn_table.get(name);
|
|
|
|
} else {
|
|
|
|
zig_unreachable();
|
|
|
|
}
|
2015-11-30 05:39:11 +08:00
|
|
|
|
2015-12-01 17:08:58 +08:00
|
|
|
|
2015-11-30 05:39:11 +08:00
|
|
|
assert(fn_table_entry->proto_node->type == NodeTypeFnProto);
|
2015-12-09 16:03:04 +08:00
|
|
|
AstNodeFnProto *fn_proto_data = &fn_table_entry->proto_node->data.fn_proto;
|
|
|
|
|
|
|
|
int expected_param_count = fn_proto_data->params.length;
|
2016-01-05 07:57:22 +08:00
|
|
|
int fn_call_param_count = node->data.fn_call_expr.params.length;
|
|
|
|
int actual_param_count = fn_call_param_count + (struct_type ? 1 : 0);
|
2015-12-09 16:03:04 +08:00
|
|
|
bool is_var_args = fn_proto_data->is_var_args;
|
|
|
|
assert((is_var_args && actual_param_count >= expected_param_count) ||
|
|
|
|
actual_param_count == expected_param_count);
|
2015-11-30 05:39:11 +08:00
|
|
|
|
2015-12-04 05:29:19 +08:00
|
|
|
// don't really include void values
|
2015-12-09 16:03:04 +08:00
|
|
|
int gen_param_count;
|
|
|
|
if (is_var_args) {
|
|
|
|
gen_param_count = actual_param_count;
|
|
|
|
} else {
|
|
|
|
gen_param_count = count_non_void_params(g, &fn_table_entry->proto_node->data.fn_proto.params);
|
|
|
|
}
|
2015-12-04 05:29:19 +08:00
|
|
|
LLVMValueRef *gen_param_values = allocate<LLVMValueRef>(gen_param_count);
|
|
|
|
|
|
|
|
int gen_param_index = 0;
|
2016-01-05 07:57:22 +08:00
|
|
|
if (struct_type) {
|
|
|
|
gen_param_values[gen_param_index] = gen_expr(g, first_param_expr);
|
|
|
|
gen_param_index += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < fn_call_param_count; i += 1) {
|
2015-11-30 05:39:11 +08:00
|
|
|
AstNode *expr_node = node->data.fn_call_expr.params.at(i);
|
2015-12-04 05:29:19 +08:00
|
|
|
LLVMValueRef param_value = gen_expr(g, expr_node);
|
2015-12-09 16:03:04 +08:00
|
|
|
if (is_var_args ||
|
|
|
|
!is_param_decl_type_void(g, fn_table_entry->proto_node->data.fn_proto.params.at(i)))
|
|
|
|
{
|
2015-12-04 05:29:19 +08:00
|
|
|
gen_param_values[gen_param_index] = param_value;
|
|
|
|
gen_param_index += 1;
|
|
|
|
}
|
2015-11-28 15:40:54 +08:00
|
|
|
}
|
|
|
|
|
2015-11-30 05:39:11 +08:00
|
|
|
add_debug_source_node(g, node);
|
|
|
|
LLVMValueRef result = LLVMZigBuildCall(g->builder, fn_table_entry->fn_value,
|
2015-12-04 05:29:19 +08:00
|
|
|
gen_param_values, gen_param_count, fn_table_entry->calling_convention, "");
|
2015-11-30 05:39:11 +08:00
|
|
|
|
2015-12-01 06:36:58 +08:00
|
|
|
if (type_is_unreachable(g, fn_table_entry->proto_node->data.fn_proto.return_type)) {
|
2015-11-30 05:39:11 +08:00
|
|
|
return LLVMBuildUnreachable(g->builder);
|
|
|
|
} else {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-09 06:31:43 +08:00
|
|
|
static LLVMValueRef gen_array_ptr(CodeGen *g, AstNode *node) {
|
2015-12-09 05:15:34 +08:00
|
|
|
assert(node->type == NodeTypeArrayAccessExpr);
|
|
|
|
|
2016-01-03 10:47:36 +08:00
|
|
|
TypeTableEntry *type_entry = get_expr_type(node->data.array_access_expr.array_ref_expr);
|
|
|
|
AstNode *array_expr_node = node->data.array_access_expr.array_ref_expr;
|
|
|
|
|
|
|
|
LLVMValueRef array_ptr = gen_expr(g, array_expr_node);
|
|
|
|
|
2015-12-09 05:47:17 +08:00
|
|
|
LLVMValueRef subscript_value = gen_expr(g, node->data.array_access_expr.subscript);
|
|
|
|
|
2016-01-03 10:47:36 +08:00
|
|
|
assert(array_ptr);
|
2015-12-09 05:47:17 +08:00
|
|
|
assert(subscript_value);
|
|
|
|
|
2016-01-03 10:47:36 +08:00
|
|
|
if (type_entry->id == TypeTableEntryIdArray) {
|
|
|
|
LLVMValueRef indices[] = {
|
|
|
|
LLVMConstNull(g->builtin_types.entry_usize->type_ref),
|
|
|
|
subscript_value
|
|
|
|
};
|
|
|
|
add_debug_source_node(g, node);
|
|
|
|
return LLVMBuildInBoundsGEP(g->builder, array_ptr, indices, 2, "");
|
|
|
|
} else if (type_entry->id == TypeTableEntryIdPointer) {
|
|
|
|
assert(LLVMGetTypeKind(LLVMTypeOf(array_ptr)) == LLVMPointerTypeKind);
|
|
|
|
LLVMValueRef indices[] = {
|
|
|
|
subscript_value
|
|
|
|
};
|
|
|
|
add_debug_source_node(g, node);
|
|
|
|
return LLVMBuildInBoundsGEP(g->builder, array_ptr, indices, 1, "");
|
|
|
|
} else {
|
|
|
|
zig_unreachable();
|
|
|
|
}
|
2015-12-09 06:31:43 +08:00
|
|
|
}
|
|
|
|
|
2015-12-16 10:17:39 +08:00
|
|
|
static LLVMValueRef gen_field_ptr(CodeGen *g, AstNode *node, TypeTableEntry **out_type_entry) {
|
2015-12-12 17:05:08 +08:00
|
|
|
assert(node->type == NodeTypeFieldAccessExpr);
|
|
|
|
|
2015-12-23 04:22:40 +08:00
|
|
|
AstNode *struct_expr_node = node->data.field_access_expr.struct_expr;
|
2015-12-12 17:05:08 +08:00
|
|
|
|
2015-12-23 04:22:40 +08:00
|
|
|
LLVMValueRef struct_ptr;
|
|
|
|
if (struct_expr_node->type == NodeTypeSymbol) {
|
|
|
|
VariableTableEntry *var = find_variable(struct_expr_node->codegen_node->expr_node.block_context,
|
|
|
|
&struct_expr_node->data.symbol);
|
|
|
|
assert(var);
|
|
|
|
|
|
|
|
if (var->is_ptr && var->type->id == TypeTableEntryIdPointer) {
|
|
|
|
add_debug_source_node(g, node);
|
|
|
|
struct_ptr = LLVMBuildLoad(g->builder, var->value_ref, "");
|
|
|
|
} else {
|
|
|
|
struct_ptr = var->value_ref;
|
|
|
|
}
|
|
|
|
} else if (struct_expr_node->type == NodeTypeFieldAccessExpr) {
|
|
|
|
struct_ptr = gen_field_access_expr(g, struct_expr_node, true);
|
|
|
|
TypeTableEntry *field_type = get_expr_type(struct_expr_node);
|
|
|
|
if (field_type->id == TypeTableEntryIdPointer) {
|
|
|
|
// we have a double pointer so we must dereference it once
|
|
|
|
add_debug_source_node(g, node);
|
|
|
|
struct_ptr = LLVMBuildLoad(g->builder, struct_ptr, "");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
struct_ptr = gen_expr(g, struct_expr_node);
|
2015-12-16 12:48:41 +08:00
|
|
|
}
|
2015-12-23 04:22:40 +08:00
|
|
|
|
|
|
|
assert(LLVMGetTypeKind(LLVMTypeOf(struct_ptr)) == LLVMPointerTypeKind);
|
|
|
|
assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(struct_ptr))) == LLVMStructTypeKind);
|
2015-12-16 12:48:41 +08:00
|
|
|
|
2015-12-12 17:05:08 +08:00
|
|
|
FieldAccessNode *codegen_field_access = &node->codegen_node->data.field_access_node;
|
|
|
|
|
|
|
|
assert(codegen_field_access->field_index >= 0);
|
|
|
|
|
2015-12-16 10:17:39 +08:00
|
|
|
*out_type_entry = codegen_field_access->type_struct_field->type_entry;
|
|
|
|
|
2015-12-13 13:55:29 +08:00
|
|
|
add_debug_source_node(g, node);
|
2015-12-12 17:34:09 +08:00
|
|
|
return LLVMBuildStructGEP(g->builder, struct_ptr, codegen_field_access->field_index, "");
|
2015-12-12 17:05:08 +08:00
|
|
|
}
|
|
|
|
|
2015-12-23 04:22:40 +08:00
|
|
|
static LLVMValueRef gen_array_access_expr(CodeGen *g, AstNode *node, bool is_lvalue) {
|
2015-12-09 06:31:43 +08:00
|
|
|
assert(node->type == NodeTypeArrayAccessExpr);
|
|
|
|
|
|
|
|
LLVMValueRef ptr = gen_array_ptr(g, node);
|
2015-12-23 04:22:40 +08:00
|
|
|
|
|
|
|
if (is_lvalue) {
|
|
|
|
return ptr;
|
|
|
|
} else {
|
|
|
|
add_debug_source_node(g, node);
|
|
|
|
return LLVMBuildLoad(g->builder, ptr, "");
|
|
|
|
}
|
2015-12-09 05:15:34 +08:00
|
|
|
}
|
|
|
|
|
2015-12-23 04:22:40 +08:00
|
|
|
static LLVMValueRef gen_field_access_expr(CodeGen *g, AstNode *node, bool is_lvalue) {
|
2015-12-12 15:10:37 +08:00
|
|
|
assert(node->type == NodeTypeFieldAccessExpr);
|
|
|
|
|
|
|
|
TypeTableEntry *struct_type = get_expr_type(node->data.field_access_expr.struct_expr);
|
|
|
|
Buf *name = &node->data.field_access_expr.field_name;
|
|
|
|
|
|
|
|
if (struct_type->id == TypeTableEntryIdArray) {
|
|
|
|
if (buf_eql_str(name, "len")) {
|
|
|
|
return LLVMConstInt(g->builtin_types.entry_usize->type_ref,
|
|
|
|
struct_type->data.array.len, false);
|
2016-01-03 10:47:36 +08:00
|
|
|
} else if (buf_eql_str(name, "ptr")) {
|
|
|
|
LLVMValueRef array_val = gen_expr(g, node->data.field_access_expr.struct_expr);
|
|
|
|
LLVMValueRef indices[] = {
|
|
|
|
LLVMConstNull(g->builtin_types.entry_usize->type_ref),
|
|
|
|
LLVMConstNull(g->builtin_types.entry_usize->type_ref),
|
|
|
|
};
|
|
|
|
add_debug_source_node(g, node);
|
|
|
|
return LLVMBuildInBoundsGEP(g->builder, array_val, indices, 2, "");
|
2015-12-12 15:10:37 +08:00
|
|
|
} else {
|
|
|
|
zig_panic("gen_field_access_expr bad array field");
|
|
|
|
}
|
2015-12-16 12:48:41 +08:00
|
|
|
} else if (struct_type->id == TypeTableEntryIdStruct || (struct_type->id == TypeTableEntryIdPointer &&
|
|
|
|
struct_type->data.pointer.child_type->id == TypeTableEntryIdStruct))
|
|
|
|
{
|
2015-12-16 10:17:39 +08:00
|
|
|
TypeTableEntry *type_entry;
|
|
|
|
LLVMValueRef ptr = gen_field_ptr(g, node, &type_entry);
|
2015-12-23 04:22:40 +08:00
|
|
|
if (is_lvalue) {
|
|
|
|
return ptr;
|
|
|
|
} else {
|
|
|
|
add_debug_source_node(g, node);
|
|
|
|
return LLVMBuildLoad(g->builder, ptr, "");
|
|
|
|
}
|
2015-12-12 15:10:37 +08:00
|
|
|
} else {
|
|
|
|
zig_panic("gen_field_access_expr bad struct type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-23 04:22:40 +08:00
|
|
|
static LLVMValueRef gen_lvalue(CodeGen *g, AstNode *expr_node, AstNode *node,
|
2015-12-16 10:17:39 +08:00
|
|
|
TypeTableEntry **out_type_entry)
|
|
|
|
{
|
|
|
|
LLVMValueRef target_ref;
|
|
|
|
|
|
|
|
if (node->type == NodeTypeSymbol) {
|
2015-12-23 04:22:40 +08:00
|
|
|
VariableTableEntry *var = find_variable(expr_node->codegen_node->expr_node.block_context,
|
2015-12-16 10:17:39 +08:00
|
|
|
&node->data.symbol);
|
2015-12-23 04:22:40 +08:00
|
|
|
assert(var);
|
2015-12-16 10:17:39 +08:00
|
|
|
// semantic checking ensures no variables are constant
|
|
|
|
assert(!var->is_const);
|
|
|
|
|
|
|
|
*out_type_entry = var->type;
|
|
|
|
target_ref = var->value_ref;
|
|
|
|
} else if (node->type == NodeTypeArrayAccessExpr) {
|
|
|
|
TypeTableEntry *array_type = get_expr_type(node->data.array_access_expr.array_ref_expr);
|
2016-01-03 10:47:36 +08:00
|
|
|
if (array_type->id == TypeTableEntryIdArray) {
|
|
|
|
*out_type_entry = array_type->data.array.child_type;
|
|
|
|
target_ref = gen_array_ptr(g, node);
|
|
|
|
} else if (array_type->id == TypeTableEntryIdPointer) {
|
|
|
|
*out_type_entry = array_type->data.pointer.child_type;
|
|
|
|
target_ref = gen_array_ptr(g, node);
|
|
|
|
} else {
|
|
|
|
zig_unreachable();
|
|
|
|
}
|
2015-12-16 10:17:39 +08:00
|
|
|
} else if (node->type == NodeTypeFieldAccessExpr) {
|
|
|
|
target_ref = gen_field_ptr(g, node, out_type_entry);
|
2016-01-05 10:10:52 +08:00
|
|
|
} else if (node->type == NodeTypePrefixOpExpr) {
|
|
|
|
assert(node->data.prefix_op_expr.prefix_op == PrefixOpDereference);
|
|
|
|
AstNode *target_expr = node->data.prefix_op_expr.primary_expr;
|
|
|
|
TypeTableEntry *type_entry = get_expr_type(target_expr);
|
|
|
|
assert(type_entry->id == TypeTableEntryIdPointer);
|
|
|
|
*out_type_entry = type_entry->data.pointer.child_type;
|
|
|
|
return gen_expr(g, target_expr);
|
2015-12-16 10:17:39 +08:00
|
|
|
} else {
|
|
|
|
zig_panic("bad assign target");
|
|
|
|
}
|
|
|
|
|
|
|
|
return target_ref;
|
|
|
|
}
|
|
|
|
|
2015-11-30 05:00:34 +08:00
|
|
|
static LLVMValueRef gen_prefix_op_expr(CodeGen *g, AstNode *node) {
|
|
|
|
assert(node->type == NodeTypePrefixOpExpr);
|
|
|
|
assert(node->data.prefix_op_expr.primary_expr);
|
2015-11-30 04:37:55 +08:00
|
|
|
|
2015-12-16 10:17:39 +08:00
|
|
|
AstNode *expr_node = node->data.prefix_op_expr.primary_expr;
|
2015-11-30 04:37:55 +08:00
|
|
|
|
2015-11-30 05:00:34 +08:00
|
|
|
switch (node->data.prefix_op_expr.prefix_op) {
|
2015-12-16 10:17:39 +08:00
|
|
|
case PrefixOpInvalid:
|
|
|
|
zig_unreachable();
|
2015-11-30 05:00:34 +08:00
|
|
|
case PrefixOpNegation:
|
2015-12-16 10:17:39 +08:00
|
|
|
{
|
|
|
|
LLVMValueRef expr = gen_expr(g, expr_node);
|
|
|
|
add_debug_source_node(g, node);
|
|
|
|
return LLVMBuildNeg(g->builder, expr, "");
|
|
|
|
}
|
2015-11-30 05:00:34 +08:00
|
|
|
case PrefixOpBoolNot:
|
2015-11-30 04:37:55 +08:00
|
|
|
{
|
2015-12-16 10:17:39 +08:00
|
|
|
LLVMValueRef expr = gen_expr(g, expr_node);
|
2015-11-30 04:37:55 +08:00
|
|
|
LLVMValueRef zero = LLVMConstNull(LLVMTypeOf(expr));
|
|
|
|
add_debug_source_node(g, node);
|
|
|
|
return LLVMBuildICmp(g->builder, LLVMIntEQ, expr, zero, "");
|
|
|
|
}
|
2015-11-30 05:00:34 +08:00
|
|
|
case PrefixOpBinNot:
|
2015-12-16 10:17:39 +08:00
|
|
|
{
|
|
|
|
LLVMValueRef expr = gen_expr(g, expr_node);
|
|
|
|
add_debug_source_node(g, node);
|
|
|
|
return LLVMBuildNot(g->builder, expr, "");
|
|
|
|
}
|
|
|
|
case PrefixOpAddressOf:
|
|
|
|
case PrefixOpConstAddressOf:
|
|
|
|
{
|
|
|
|
TypeTableEntry *lvalue_type;
|
|
|
|
return gen_lvalue(g, node, expr_node, &lvalue_type);
|
|
|
|
}
|
|
|
|
|
2016-01-05 10:10:52 +08:00
|
|
|
case PrefixOpDereference:
|
|
|
|
{
|
|
|
|
LLVMValueRef expr = gen_expr(g, expr_node);
|
|
|
|
add_debug_source_node(g, node);
|
|
|
|
return LLVMBuildLoad(g->builder, expr, "");
|
|
|
|
}
|
2015-11-30 04:37:55 +08:00
|
|
|
}
|
2015-11-28 15:40:54 +08:00
|
|
|
zig_unreachable();
|
|
|
|
}
|
|
|
|
|
2015-12-16 08:11:44 +08:00
|
|
|
static LLVMValueRef gen_bare_cast(CodeGen *g, AstNode *node, LLVMValueRef expr_val,
|
|
|
|
TypeTableEntry *actual_type, TypeTableEntry *wanted_type, CastNode *cast_node)
|
|
|
|
{
|
2015-12-12 17:05:08 +08:00
|
|
|
switch (cast_node->op) {
|
2015-12-14 17:46:37 +08:00
|
|
|
case CastOpNothing:
|
|
|
|
return expr_val;
|
2016-01-02 15:06:06 +08:00
|
|
|
case CastOpMaybeWrap:
|
|
|
|
{
|
|
|
|
assert(cast_node->ptr);
|
|
|
|
assert(wanted_type->id == TypeTableEntryIdMaybe);
|
|
|
|
|
|
|
|
add_debug_source_node(g, node);
|
|
|
|
LLVMValueRef val_ptr = LLVMBuildStructGEP(g->builder, cast_node->ptr, 0, "");
|
|
|
|
gen_assign_raw(g, node, BinOpTypeAssign,
|
|
|
|
val_ptr, expr_val, wanted_type->data.maybe.child_type, actual_type);
|
|
|
|
|
|
|
|
add_debug_source_node(g, node);
|
|
|
|
LLVMValueRef maybe_ptr = LLVMBuildStructGEP(g->builder, cast_node->ptr, 1, "");
|
|
|
|
LLVMBuildStore(g->builder, LLVMConstAllOnes(LLVMInt1Type()), maybe_ptr);
|
|
|
|
|
|
|
|
return cast_node->ptr;
|
|
|
|
}
|
2015-12-12 17:05:08 +08:00
|
|
|
case CastOpPtrToInt:
|
2016-01-03 10:47:36 +08:00
|
|
|
add_debug_source_node(g, node);
|
2015-12-12 17:05:08 +08:00
|
|
|
return LLVMBuildPtrToInt(g->builder, expr_val, wanted_type->type_ref, "");
|
2016-01-02 18:38:45 +08:00
|
|
|
case CastOpPointerReinterpret:
|
2016-01-03 10:47:36 +08:00
|
|
|
add_debug_source_node(g, node);
|
2016-01-02 18:38:45 +08:00
|
|
|
return LLVMBuildBitCast(g->builder, expr_val, wanted_type->type_ref, "");
|
2015-12-12 17:05:08 +08:00
|
|
|
case CastOpIntWidenOrShorten:
|
|
|
|
if (actual_type->size_in_bits == wanted_type->size_in_bits) {
|
|
|
|
return expr_val;
|
|
|
|
} else if (actual_type->size_in_bits < wanted_type->size_in_bits) {
|
2016-01-03 10:47:36 +08:00
|
|
|
if (actual_type->data.integral.is_signed) {
|
|
|
|
add_debug_source_node(g, node);
|
2015-12-12 17:05:08 +08:00
|
|
|
return LLVMBuildSExt(g->builder, expr_val, wanted_type->type_ref, "");
|
|
|
|
} else {
|
2016-01-03 10:47:36 +08:00
|
|
|
add_debug_source_node(g, node);
|
|
|
|
return LLVMBuildZExt(g->builder, expr_val, wanted_type->type_ref, "");
|
2015-12-12 17:05:08 +08:00
|
|
|
}
|
2015-12-11 18:55:26 +08:00
|
|
|
} else {
|
2015-12-15 17:58:01 +08:00
|
|
|
assert(actual_type->size_in_bits > wanted_type->size_in_bits);
|
2016-01-03 10:47:36 +08:00
|
|
|
add_debug_source_node(g, node);
|
|
|
|
return LLVMBuildTrunc(g->builder, expr_val, wanted_type->type_ref, "");
|
2015-12-11 18:55:26 +08:00
|
|
|
}
|
2015-12-12 17:05:08 +08:00
|
|
|
case CastOpArrayToString:
|
|
|
|
{
|
2015-12-13 13:55:29 +08:00
|
|
|
assert(cast_node->ptr);
|
|
|
|
|
|
|
|
add_debug_source_node(g, node);
|
|
|
|
|
|
|
|
LLVMValueRef ptr_ptr = LLVMBuildStructGEP(g->builder, cast_node->ptr, 0, "");
|
|
|
|
LLVMBuildStore(g->builder, expr_val, ptr_ptr);
|
|
|
|
|
|
|
|
LLVMValueRef len_ptr = LLVMBuildStructGEP(g->builder, cast_node->ptr, 1, "");
|
|
|
|
LLVMValueRef len_val = LLVMConstInt(g->builtin_types.entry_usize->type_ref,
|
|
|
|
actual_type->data.array.len, false);
|
|
|
|
LLVMBuildStore(g->builder, len_val, len_ptr);
|
|
|
|
|
|
|
|
return cast_node->ptr;
|
2015-12-11 18:55:26 +08:00
|
|
|
}
|
|
|
|
}
|
2015-12-12 17:05:08 +08:00
|
|
|
zig_unreachable();
|
2015-11-28 15:40:54 +08:00
|
|
|
}
|
|
|
|
|
2015-12-16 08:11:44 +08:00
|
|
|
static LLVMValueRef gen_cast_expr(CodeGen *g, AstNode *node) {
|
|
|
|
assert(node->type == NodeTypeCastExpr);
|
|
|
|
|
|
|
|
LLVMValueRef expr_val = gen_expr(g, node->data.cast_expr.expr);
|
|
|
|
|
|
|
|
TypeTableEntry *actual_type = get_expr_type(node->data.cast_expr.expr);
|
|
|
|
TypeTableEntry *wanted_type = get_expr_type(node);
|
|
|
|
|
|
|
|
CastNode *cast_node = &node->codegen_node->data.cast_node;
|
|
|
|
|
|
|
|
return gen_bare_cast(g, node, expr_val, actual_type, wanted_type, cast_node);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-02 15:06:06 +08:00
|
|
|
static LLVMValueRef gen_arithmetic_bin_op(CodeGen *g, AstNode *source_node,
|
2015-12-13 10:47:37 +08:00
|
|
|
LLVMValueRef val1, LLVMValueRef val2,
|
|
|
|
TypeTableEntry *op1_type, TypeTableEntry *op2_type,
|
2016-01-02 15:06:06 +08:00
|
|
|
BinOpType bin_op)
|
2015-12-13 10:47:37 +08:00
|
|
|
{
|
2015-12-09 03:25:30 +08:00
|
|
|
assert(op1_type == op2_type);
|
|
|
|
|
2016-01-02 15:06:06 +08:00
|
|
|
switch (bin_op) {
|
2015-11-30 06:08:25 +08:00
|
|
|
case BinOpTypeBinOr:
|
2015-12-13 10:47:37 +08:00
|
|
|
case BinOpTypeAssignBitOr:
|
2016-01-02 15:06:06 +08:00
|
|
|
add_debug_source_node(g, source_node);
|
2015-11-30 06:08:25 +08:00
|
|
|
return LLVMBuildOr(g->builder, val1, val2, "");
|
|
|
|
case BinOpTypeBinXor:
|
2015-12-13 10:47:37 +08:00
|
|
|
case BinOpTypeAssignBitXor:
|
2016-01-02 15:06:06 +08:00
|
|
|
add_debug_source_node(g, source_node);
|
2015-11-30 06:08:25 +08:00
|
|
|
return LLVMBuildXor(g->builder, val1, val2, "");
|
|
|
|
case BinOpTypeBinAnd:
|
2015-12-13 10:47:37 +08:00
|
|
|
case BinOpTypeAssignBitAnd:
|
2016-01-02 15:06:06 +08:00
|
|
|
add_debug_source_node(g, source_node);
|
2015-11-30 06:08:25 +08:00
|
|
|
return LLVMBuildAnd(g->builder, val1, val2, "");
|
2015-11-30 05:46:05 +08:00
|
|
|
case BinOpTypeBitShiftLeft:
|
2015-12-13 10:47:37 +08:00
|
|
|
case BinOpTypeAssignBitShiftLeft:
|
2016-01-02 15:06:06 +08:00
|
|
|
add_debug_source_node(g, source_node);
|
2015-11-28 15:40:54 +08:00
|
|
|
return LLVMBuildShl(g->builder, val1, val2, "");
|
2015-11-30 05:46:05 +08:00
|
|
|
case BinOpTypeBitShiftRight:
|
2015-12-13 10:47:37 +08:00
|
|
|
case BinOpTypeAssignBitShiftRight:
|
2016-01-02 15:06:06 +08:00
|
|
|
add_debug_source_node(g, source_node);
|
2015-12-09 05:15:34 +08:00
|
|
|
if (op1_type->id == TypeTableEntryIdInt) {
|
2015-12-09 03:25:30 +08:00
|
|
|
return LLVMBuildAShr(g->builder, val1, val2, "");
|
|
|
|
} else {
|
|
|
|
return LLVMBuildLShr(g->builder, val1, val2, "");
|
|
|
|
}
|
2015-11-30 06:08:25 +08:00
|
|
|
case BinOpTypeAdd:
|
2015-12-13 10:47:37 +08:00
|
|
|
case BinOpTypeAssignPlus:
|
2016-01-02 15:06:06 +08:00
|
|
|
add_debug_source_node(g, source_node);
|
2015-12-09 05:15:34 +08:00
|
|
|
if (op1_type->id == TypeTableEntryIdFloat) {
|
2015-12-09 03:25:30 +08:00
|
|
|
return LLVMBuildFAdd(g->builder, val1, val2, "");
|
|
|
|
} else {
|
|
|
|
return LLVMBuildNSWAdd(g->builder, val1, val2, "");
|
|
|
|
}
|
2015-11-30 06:08:25 +08:00
|
|
|
case BinOpTypeSub:
|
2015-12-13 10:47:37 +08:00
|
|
|
case BinOpTypeAssignMinus:
|
2016-01-02 15:06:06 +08:00
|
|
|
add_debug_source_node(g, source_node);
|
2015-12-09 05:15:34 +08:00
|
|
|
if (op1_type->id == TypeTableEntryIdFloat) {
|
2015-12-09 03:25:30 +08:00
|
|
|
return LLVMBuildFSub(g->builder, val1, val2, "");
|
|
|
|
} else {
|
|
|
|
return LLVMBuildNSWSub(g->builder, val1, val2, "");
|
|
|
|
}
|
2015-11-30 06:08:25 +08:00
|
|
|
case BinOpTypeMult:
|
2015-12-13 10:47:37 +08:00
|
|
|
case BinOpTypeAssignTimes:
|
2016-01-02 15:06:06 +08:00
|
|
|
add_debug_source_node(g, source_node);
|
2015-12-09 05:15:34 +08:00
|
|
|
if (op1_type->id == TypeTableEntryIdFloat) {
|
2015-12-09 03:25:30 +08:00
|
|
|
return LLVMBuildFMul(g->builder, val1, val2, "");
|
|
|
|
} else {
|
|
|
|
return LLVMBuildNSWMul(g->builder, val1, val2, "");
|
|
|
|
}
|
2015-11-30 06:08:25 +08:00
|
|
|
case BinOpTypeDiv:
|
2015-12-13 10:47:37 +08:00
|
|
|
case BinOpTypeAssignDiv:
|
2016-01-02 15:06:06 +08:00
|
|
|
add_debug_source_node(g, source_node);
|
2015-12-09 05:15:34 +08:00
|
|
|
if (op1_type->id == TypeTableEntryIdFloat) {
|
2015-12-09 03:25:30 +08:00
|
|
|
return LLVMBuildFDiv(g->builder, val1, val2, "");
|
|
|
|
} else {
|
2015-12-09 05:15:34 +08:00
|
|
|
assert(op1_type->id == TypeTableEntryIdInt);
|
|
|
|
if (op1_type->data.integral.is_signed) {
|
|
|
|
return LLVMBuildSDiv(g->builder, val1, val2, "");
|
|
|
|
} else {
|
|
|
|
return LLVMBuildUDiv(g->builder, val1, val2, "");
|
|
|
|
}
|
2015-12-09 03:25:30 +08:00
|
|
|
}
|
2015-11-30 06:08:25 +08:00
|
|
|
case BinOpTypeMod:
|
2015-12-13 10:47:37 +08:00
|
|
|
case BinOpTypeAssignMod:
|
2016-01-02 15:06:06 +08:00
|
|
|
add_debug_source_node(g, source_node);
|
2015-12-09 05:15:34 +08:00
|
|
|
if (op1_type->id == TypeTableEntryIdFloat) {
|
2015-12-09 03:25:30 +08:00
|
|
|
return LLVMBuildFRem(g->builder, val1, val2, "");
|
|
|
|
} else {
|
2015-12-09 05:15:34 +08:00
|
|
|
assert(op1_type->id == TypeTableEntryIdInt);
|
|
|
|
if (op1_type->data.integral.is_signed) {
|
|
|
|
return LLVMBuildSRem(g->builder, val1, val2, "");
|
|
|
|
} else {
|
|
|
|
return LLVMBuildURem(g->builder, val1, val2, "");
|
|
|
|
}
|
2015-12-09 03:25:30 +08:00
|
|
|
}
|
2015-11-30 06:08:25 +08:00
|
|
|
case BinOpTypeBoolOr:
|
|
|
|
case BinOpTypeBoolAnd:
|
|
|
|
case BinOpTypeCmpEq:
|
|
|
|
case BinOpTypeCmpNotEq:
|
|
|
|
case BinOpTypeCmpLessThan:
|
|
|
|
case BinOpTypeCmpGreaterThan:
|
|
|
|
case BinOpTypeCmpLessOrEq:
|
|
|
|
case BinOpTypeCmpGreaterOrEq:
|
|
|
|
case BinOpTypeInvalid:
|
2015-12-08 11:57:45 +08:00
|
|
|
case BinOpTypeAssign:
|
2015-12-13 10:07:29 +08:00
|
|
|
case BinOpTypeAssignBoolAnd:
|
|
|
|
case BinOpTypeAssignBoolOr:
|
2015-11-28 15:40:54 +08:00
|
|
|
zig_unreachable();
|
|
|
|
}
|
|
|
|
zig_unreachable();
|
|
|
|
}
|
2015-12-13 10:47:37 +08:00
|
|
|
static LLVMValueRef gen_arithmetic_bin_op_expr(CodeGen *g, AstNode *node) {
|
|
|
|
assert(node->type == NodeTypeBinOpExpr);
|
|
|
|
|
|
|
|
LLVMValueRef val1 = gen_expr(g, node->data.bin_op_expr.op1);
|
|
|
|
LLVMValueRef val2 = gen_expr(g, node->data.bin_op_expr.op2);
|
|
|
|
|
|
|
|
TypeTableEntry *op1_type = get_expr_type(node->data.bin_op_expr.op1);
|
|
|
|
TypeTableEntry *op2_type = get_expr_type(node->data.bin_op_expr.op2);
|
2016-01-02 15:06:06 +08:00
|
|
|
return gen_arithmetic_bin_op(g, node, val1, val2, op1_type, op2_type, node->data.bin_op_expr.bin_op);
|
2015-12-13 10:47:37 +08:00
|
|
|
|
|
|
|
}
|
2015-11-28 15:40:54 +08:00
|
|
|
|
2015-11-30 05:46:05 +08:00
|
|
|
static LLVMIntPredicate cmp_op_to_int_predicate(BinOpType cmp_op, bool is_signed) {
|
2015-11-28 15:40:54 +08:00
|
|
|
switch (cmp_op) {
|
2015-11-30 05:46:05 +08:00
|
|
|
case BinOpTypeCmpEq:
|
2015-11-28 15:40:54 +08:00
|
|
|
return LLVMIntEQ;
|
2015-11-30 05:46:05 +08:00
|
|
|
case BinOpTypeCmpNotEq:
|
2015-11-28 15:40:54 +08:00
|
|
|
return LLVMIntNE;
|
2015-11-30 05:46:05 +08:00
|
|
|
case BinOpTypeCmpLessThan:
|
2015-11-28 15:40:54 +08:00
|
|
|
return is_signed ? LLVMIntSLT : LLVMIntULT;
|
2015-11-30 05:46:05 +08:00
|
|
|
case BinOpTypeCmpGreaterThan:
|
2015-11-28 15:40:54 +08:00
|
|
|
return is_signed ? LLVMIntSGT : LLVMIntUGT;
|
2015-11-30 05:46:05 +08:00
|
|
|
case BinOpTypeCmpLessOrEq:
|
2015-11-28 15:40:54 +08:00
|
|
|
return is_signed ? LLVMIntSLE : LLVMIntULE;
|
2015-11-30 05:46:05 +08:00
|
|
|
case BinOpTypeCmpGreaterOrEq:
|
2015-11-28 15:40:54 +08:00
|
|
|
return is_signed ? LLVMIntSGE : LLVMIntUGE;
|
2015-11-30 05:46:05 +08:00
|
|
|
default:
|
|
|
|
zig_unreachable();
|
2015-11-28 15:40:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-09 03:25:30 +08:00
|
|
|
static LLVMRealPredicate cmp_op_to_real_predicate(BinOpType cmp_op) {
|
|
|
|
switch (cmp_op) {
|
|
|
|
case BinOpTypeCmpEq:
|
|
|
|
return LLVMRealOEQ;
|
|
|
|
case BinOpTypeCmpNotEq:
|
|
|
|
return LLVMRealONE;
|
|
|
|
case BinOpTypeCmpLessThan:
|
|
|
|
return LLVMRealOLT;
|
|
|
|
case BinOpTypeCmpGreaterThan:
|
|
|
|
return LLVMRealOGT;
|
|
|
|
case BinOpTypeCmpLessOrEq:
|
|
|
|
return LLVMRealOLE;
|
|
|
|
case BinOpTypeCmpGreaterOrEq:
|
|
|
|
return LLVMRealOGE;
|
|
|
|
default:
|
|
|
|
zig_unreachable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-28 15:40:54 +08:00
|
|
|
static LLVMValueRef gen_cmp_expr(CodeGen *g, AstNode *node) {
|
2015-11-30 05:46:05 +08:00
|
|
|
assert(node->type == NodeTypeBinOpExpr);
|
2015-11-28 15:40:54 +08:00
|
|
|
|
2015-11-30 05:46:05 +08:00
|
|
|
LLVMValueRef val1 = gen_expr(g, node->data.bin_op_expr.op1);
|
|
|
|
LLVMValueRef val2 = gen_expr(g, node->data.bin_op_expr.op2);
|
2015-11-28 15:40:54 +08:00
|
|
|
|
2015-12-09 03:25:30 +08:00
|
|
|
TypeTableEntry *op1_type = get_expr_type(node->data.bin_op_expr.op1);
|
|
|
|
TypeTableEntry *op2_type = get_expr_type(node->data.bin_op_expr.op2);
|
|
|
|
assert(op1_type == op2_type);
|
|
|
|
|
2015-11-28 15:40:54 +08:00
|
|
|
add_debug_source_node(g, node);
|
2015-12-09 05:15:34 +08:00
|
|
|
if (op1_type->id == TypeTableEntryIdFloat) {
|
2015-12-09 03:25:30 +08:00
|
|
|
LLVMRealPredicate pred = cmp_op_to_real_predicate(node->data.bin_op_expr.bin_op);
|
|
|
|
return LLVMBuildFCmp(g->builder, pred, val1, val2, "");
|
|
|
|
} else {
|
2015-12-09 05:15:34 +08:00
|
|
|
assert(op1_type->id == TypeTableEntryIdInt);
|
|
|
|
LLVMIntPredicate pred = cmp_op_to_int_predicate(node->data.bin_op_expr.bin_op,
|
|
|
|
op1_type->data.integral.is_signed);
|
2015-12-09 03:25:30 +08:00
|
|
|
return LLVMBuildICmp(g->builder, pred, val1, val2, "");
|
|
|
|
}
|
2015-11-28 15:40:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static LLVMValueRef gen_bool_and_expr(CodeGen *g, AstNode *node) {
|
2015-11-30 05:46:05 +08:00
|
|
|
assert(node->type == NodeTypeBinOpExpr);
|
2015-11-28 15:40:54 +08:00
|
|
|
|
2015-11-30 05:46:05 +08:00
|
|
|
LLVMValueRef val1 = gen_expr(g, node->data.bin_op_expr.op1);
|
2015-12-13 08:33:45 +08:00
|
|
|
LLVMBasicBlockRef post_val1_block = LLVMGetInsertBlock(g->builder);
|
2015-11-28 15:40:54 +08:00
|
|
|
|
|
|
|
// block for when val1 == true
|
2015-11-30 08:28:28 +08:00
|
|
|
LLVMBasicBlockRef true_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "BoolAndTrue");
|
2015-11-28 15:40:54 +08:00
|
|
|
// block for when val1 == false (don't even evaluate the second part)
|
2015-11-30 08:28:28 +08:00
|
|
|
LLVMBasicBlockRef false_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "BoolAndFalse");
|
2015-11-28 15:40:54 +08:00
|
|
|
|
|
|
|
add_debug_source_node(g, node);
|
2015-12-13 08:03:44 +08:00
|
|
|
LLVMBuildCondBr(g->builder, val1, true_block, false_block);
|
2015-11-28 15:40:54 +08:00
|
|
|
|
|
|
|
LLVMPositionBuilderAtEnd(g->builder, true_block);
|
2015-11-30 05:46:05 +08:00
|
|
|
LLVMValueRef val2 = gen_expr(g, node->data.bin_op_expr.op2);
|
2015-12-13 08:33:45 +08:00
|
|
|
LLVMBasicBlockRef post_val2_block = LLVMGetInsertBlock(g->builder);
|
|
|
|
|
2015-11-28 15:40:54 +08:00
|
|
|
add_debug_source_node(g, node);
|
2015-12-02 12:19:38 +08:00
|
|
|
LLVMBuildBr(g->builder, false_block);
|
2015-11-28 15:40:54 +08:00
|
|
|
|
|
|
|
LLVMPositionBuilderAtEnd(g->builder, false_block);
|
|
|
|
add_debug_source_node(g, node);
|
|
|
|
LLVMValueRef phi = LLVMBuildPhi(g->builder, LLVMInt1Type(), "");
|
2015-12-13 08:03:44 +08:00
|
|
|
LLVMValueRef incoming_values[2] = {val1, val2};
|
2015-12-13 08:33:45 +08:00
|
|
|
LLVMBasicBlockRef incoming_blocks[2] = {post_val1_block, post_val2_block};
|
2015-11-28 15:40:54 +08:00
|
|
|
LLVMAddIncoming(phi, incoming_values, incoming_blocks, 2);
|
|
|
|
|
|
|
|
return phi;
|
|
|
|
}
|
|
|
|
|
|
|
|
static LLVMValueRef gen_bool_or_expr(CodeGen *g, AstNode *expr_node) {
|
2015-11-30 05:46:05 +08:00
|
|
|
assert(expr_node->type == NodeTypeBinOpExpr);
|
2015-11-28 15:40:54 +08:00
|
|
|
|
2015-11-30 05:46:05 +08:00
|
|
|
LLVMValueRef val1 = gen_expr(g, expr_node->data.bin_op_expr.op1);
|
2015-12-13 08:33:45 +08:00
|
|
|
LLVMBasicBlockRef post_val1_block = LLVMGetInsertBlock(g->builder);
|
2015-12-02 12:19:38 +08:00
|
|
|
|
2015-11-28 15:40:54 +08:00
|
|
|
// block for when val1 == false
|
2015-11-30 08:28:28 +08:00
|
|
|
LLVMBasicBlockRef false_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "BoolOrFalse");
|
2015-11-28 15:40:54 +08:00
|
|
|
// block for when val1 == true (don't even evaluate the second part)
|
2015-11-30 08:28:28 +08:00
|
|
|
LLVMBasicBlockRef true_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "BoolOrTrue");
|
2015-11-28 15:40:54 +08:00
|
|
|
|
|
|
|
add_debug_source_node(g, expr_node);
|
2015-12-13 08:03:44 +08:00
|
|
|
LLVMBuildCondBr(g->builder, val1, true_block, false_block);
|
2015-11-28 15:40:54 +08:00
|
|
|
|
|
|
|
LLVMPositionBuilderAtEnd(g->builder, false_block);
|
2015-11-30 05:46:05 +08:00
|
|
|
LLVMValueRef val2 = gen_expr(g, expr_node->data.bin_op_expr.op2);
|
2015-12-13 08:33:45 +08:00
|
|
|
|
|
|
|
LLVMBasicBlockRef post_val2_block = LLVMGetInsertBlock(g->builder);
|
|
|
|
|
2015-11-28 15:40:54 +08:00
|
|
|
add_debug_source_node(g, expr_node);
|
2015-12-02 12:19:38 +08:00
|
|
|
LLVMBuildBr(g->builder, true_block);
|
2015-11-28 15:40:54 +08:00
|
|
|
|
|
|
|
LLVMPositionBuilderAtEnd(g->builder, true_block);
|
|
|
|
add_debug_source_node(g, expr_node);
|
|
|
|
LLVMValueRef phi = LLVMBuildPhi(g->builder, LLVMInt1Type(), "");
|
2015-12-13 08:03:44 +08:00
|
|
|
LLVMValueRef incoming_values[2] = {val1, val2};
|
2015-12-13 08:33:45 +08:00
|
|
|
LLVMBasicBlockRef incoming_blocks[2] = {post_val1_block, post_val2_block};
|
2015-11-28 15:40:54 +08:00
|
|
|
LLVMAddIncoming(phi, incoming_values, incoming_blocks, 2);
|
|
|
|
|
|
|
|
return phi;
|
|
|
|
}
|
|
|
|
|
2015-12-24 15:00:23 +08:00
|
|
|
static LLVMValueRef gen_struct_memcpy(CodeGen *g, AstNode *source_node, LLVMValueRef src, LLVMValueRef dest,
|
|
|
|
TypeTableEntry *type_entry)
|
|
|
|
{
|
2016-01-02 15:06:06 +08:00
|
|
|
assert(type_entry->id == TypeTableEntryIdStruct || type_entry->id == TypeTableEntryIdMaybe);
|
2015-12-24 15:00:23 +08:00
|
|
|
|
|
|
|
LLVMTypeRef ptr_u8 = LLVMPointerType(LLVMInt8Type(), 0);
|
|
|
|
|
|
|
|
add_debug_source_node(g, source_node);
|
|
|
|
LLVMValueRef src_ptr = LLVMBuildBitCast(g->builder, src, ptr_u8, "");
|
|
|
|
LLVMValueRef dest_ptr = LLVMBuildBitCast(g->builder, dest, ptr_u8, "");
|
|
|
|
|
|
|
|
LLVMValueRef params[] = {
|
|
|
|
dest_ptr, // dest pointer
|
|
|
|
src_ptr, // source pointer
|
|
|
|
LLVMConstInt(LLVMIntType(g->pointer_size_bytes * 8), type_entry->size_in_bits / 8, false), // byte count
|
|
|
|
LLVMConstInt(LLVMInt32Type(), type_entry->align_in_bits / 8, false), // align in bytes
|
|
|
|
LLVMConstNull(LLVMInt1Type()), // is volatile
|
|
|
|
};
|
|
|
|
|
|
|
|
return LLVMBuildCall(g->builder, g->memcpy_fn_val, params, 5, "");
|
|
|
|
}
|
|
|
|
|
2016-01-02 15:06:06 +08:00
|
|
|
static LLVMValueRef gen_assign_raw(CodeGen *g, AstNode *source_node, BinOpType bin_op,
|
|
|
|
LLVMValueRef target_ref, LLVMValueRef value,
|
|
|
|
TypeTableEntry *op1_type, TypeTableEntry *op2_type)
|
|
|
|
{
|
|
|
|
if (op1_type->id == TypeTableEntryIdStruct) {
|
|
|
|
assert(op2_type->id == TypeTableEntryIdStruct);
|
|
|
|
assert(op1_type == op2_type);
|
|
|
|
assert(bin_op == BinOpTypeAssign);
|
|
|
|
|
|
|
|
return gen_struct_memcpy(g, source_node, value, target_ref, op1_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bin_op != BinOpTypeAssign) {
|
|
|
|
assert(source_node->type == NodeTypeBinOpExpr);
|
|
|
|
add_debug_source_node(g, source_node->data.bin_op_expr.op1);
|
|
|
|
LLVMValueRef left_value = LLVMBuildLoad(g->builder, target_ref, "");
|
|
|
|
|
|
|
|
value = gen_arithmetic_bin_op(g, source_node, left_value, value, op1_type, op2_type, bin_op);
|
|
|
|
}
|
|
|
|
|
|
|
|
add_debug_source_node(g, source_node);
|
|
|
|
return LLVMBuildStore(g->builder, value, target_ref);
|
|
|
|
}
|
|
|
|
|
2015-12-07 14:09:46 +08:00
|
|
|
static LLVMValueRef gen_assign_expr(CodeGen *g, AstNode *node) {
|
|
|
|
assert(node->type == NodeTypeBinOpExpr);
|
|
|
|
|
2015-12-09 06:31:43 +08:00
|
|
|
AstNode *lhs_node = node->data.bin_op_expr.op1;
|
2015-12-07 14:09:46 +08:00
|
|
|
|
2015-12-13 10:47:37 +08:00
|
|
|
TypeTableEntry *op1_type;
|
2015-12-23 04:22:40 +08:00
|
|
|
|
2015-12-16 10:17:39 +08:00
|
|
|
LLVMValueRef target_ref = gen_lvalue(g, node, lhs_node, &op1_type);
|
2015-12-07 14:09:46 +08:00
|
|
|
|
2015-12-23 18:19:22 +08:00
|
|
|
TypeTableEntry *op2_type = get_expr_type(node->data.bin_op_expr.op2);
|
|
|
|
|
2015-12-13 10:47:37 +08:00
|
|
|
LLVMValueRef value = gen_expr(g, node->data.bin_op_expr.op2);
|
|
|
|
|
2016-01-02 15:06:06 +08:00
|
|
|
return gen_assign_raw(g, node, node->data.bin_op_expr.bin_op, target_ref, value, op1_type, op2_type);
|
2015-12-07 14:09:46 +08:00
|
|
|
}
|
|
|
|
|
2015-11-30 05:46:05 +08:00
|
|
|
static LLVMValueRef gen_bin_op_expr(CodeGen *g, AstNode *node) {
|
|
|
|
switch (node->data.bin_op_expr.bin_op) {
|
|
|
|
case BinOpTypeInvalid:
|
|
|
|
zig_unreachable();
|
2015-12-13 10:07:29 +08:00
|
|
|
case BinOpTypeAssign:
|
|
|
|
case BinOpTypeAssignTimes:
|
|
|
|
case BinOpTypeAssignDiv:
|
|
|
|
case BinOpTypeAssignMod:
|
|
|
|
case BinOpTypeAssignPlus:
|
|
|
|
case BinOpTypeAssignMinus:
|
|
|
|
case BinOpTypeAssignBitShiftLeft:
|
|
|
|
case BinOpTypeAssignBitShiftRight:
|
|
|
|
case BinOpTypeAssignBitAnd:
|
|
|
|
case BinOpTypeAssignBitXor:
|
|
|
|
case BinOpTypeAssignBitOr:
|
|
|
|
case BinOpTypeAssignBoolAnd:
|
|
|
|
case BinOpTypeAssignBoolOr:
|
|
|
|
return gen_assign_expr(g, node);
|
2015-11-30 05:46:05 +08:00
|
|
|
case BinOpTypeBoolOr:
|
|
|
|
return gen_bool_or_expr(g, node);
|
|
|
|
case BinOpTypeBoolAnd:
|
|
|
|
return gen_bool_and_expr(g, node);
|
|
|
|
case BinOpTypeCmpEq:
|
|
|
|
case BinOpTypeCmpNotEq:
|
|
|
|
case BinOpTypeCmpLessThan:
|
|
|
|
case BinOpTypeCmpGreaterThan:
|
|
|
|
case BinOpTypeCmpLessOrEq:
|
|
|
|
case BinOpTypeCmpGreaterOrEq:
|
|
|
|
return gen_cmp_expr(g, node);
|
|
|
|
case BinOpTypeBinOr:
|
|
|
|
case BinOpTypeBinXor:
|
|
|
|
case BinOpTypeBinAnd:
|
|
|
|
case BinOpTypeBitShiftLeft:
|
|
|
|
case BinOpTypeBitShiftRight:
|
|
|
|
case BinOpTypeAdd:
|
|
|
|
case BinOpTypeSub:
|
|
|
|
case BinOpTypeMult:
|
|
|
|
case BinOpTypeDiv:
|
|
|
|
case BinOpTypeMod:
|
2015-11-30 06:08:25 +08:00
|
|
|
return gen_arithmetic_bin_op_expr(g, node);
|
2015-11-30 05:46:05 +08:00
|
|
|
}
|
|
|
|
zig_unreachable();
|
|
|
|
}
|
|
|
|
|
2015-11-28 15:40:54 +08:00
|
|
|
static LLVMValueRef gen_return_expr(CodeGen *g, AstNode *node) {
|
|
|
|
assert(node->type == NodeTypeReturnExpr);
|
|
|
|
AstNode *param_node = node->data.return_expr.expr;
|
|
|
|
if (param_node) {
|
|
|
|
LLVMValueRef value = gen_expr(g, param_node);
|
|
|
|
|
|
|
|
add_debug_source_node(g, node);
|
|
|
|
return LLVMBuildRet(g->builder, value);
|
|
|
|
} else {
|
|
|
|
add_debug_source_node(g, node);
|
|
|
|
return LLVMBuildRetVoid(g->builder);
|
|
|
|
}
|
|
|
|
}
|
2015-12-02 12:19:38 +08:00
|
|
|
|
2016-01-02 15:06:06 +08:00
|
|
|
static LLVMValueRef gen_if_bool_expr_raw(CodeGen *g, AstNode *source_node, LLVMValueRef cond_value,
|
|
|
|
AstNode *then_node, AstNode *else_node)
|
|
|
|
{
|
|
|
|
TypeTableEntry *then_type = get_expr_type(then_node);
|
2015-12-14 02:59:34 +08:00
|
|
|
bool use_expr_value = (then_type->id != TypeTableEntryIdUnreachable &&
|
|
|
|
then_type->id != TypeTableEntryIdVoid);
|
2015-12-02 12:19:38 +08:00
|
|
|
|
2016-01-02 15:06:06 +08:00
|
|
|
if (else_node) {
|
2015-12-02 12:19:38 +08:00
|
|
|
LLVMBasicBlockRef then_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "Then");
|
|
|
|
LLVMBasicBlockRef else_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "Else");
|
|
|
|
LLVMBasicBlockRef endif_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "EndIf");
|
|
|
|
|
|
|
|
LLVMBuildCondBr(g->builder, cond_value, then_block, else_block);
|
|
|
|
|
|
|
|
LLVMPositionBuilderAtEnd(g->builder, then_block);
|
2016-01-02 15:06:06 +08:00
|
|
|
LLVMValueRef then_expr_result = gen_expr(g, then_node);
|
|
|
|
if (get_expr_type(then_node)->id != TypeTableEntryIdUnreachable)
|
2015-12-03 15:47:35 +08:00
|
|
|
LLVMBuildBr(g->builder, endif_block);
|
2015-12-02 12:19:38 +08:00
|
|
|
|
|
|
|
LLVMPositionBuilderAtEnd(g->builder, else_block);
|
2016-01-02 15:06:06 +08:00
|
|
|
LLVMValueRef else_expr_result = gen_expr(g, else_node);
|
|
|
|
if (get_expr_type(else_node)->id != TypeTableEntryIdUnreachable)
|
2015-12-03 15:47:35 +08:00
|
|
|
LLVMBuildBr(g->builder, endif_block);
|
2015-12-02 12:19:38 +08:00
|
|
|
|
|
|
|
LLVMPositionBuilderAtEnd(g->builder, endif_block);
|
|
|
|
if (use_expr_value) {
|
|
|
|
LLVMValueRef phi = LLVMBuildPhi(g->builder, LLVMTypeOf(then_expr_result), "");
|
|
|
|
LLVMValueRef incoming_values[2] = {then_expr_result, else_expr_result};
|
|
|
|
LLVMBasicBlockRef incoming_blocks[2] = {then_block, else_block};
|
|
|
|
LLVMAddIncoming(phi, incoming_values, incoming_blocks, 2);
|
|
|
|
|
|
|
|
return phi;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(!use_expr_value);
|
|
|
|
|
|
|
|
LLVMBasicBlockRef then_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "Then");
|
|
|
|
LLVMBasicBlockRef endif_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "EndIf");
|
|
|
|
|
|
|
|
LLVMBuildCondBr(g->builder, cond_value, then_block, endif_block);
|
|
|
|
|
|
|
|
LLVMPositionBuilderAtEnd(g->builder, then_block);
|
2016-01-02 15:06:06 +08:00
|
|
|
gen_expr(g, then_node);
|
|
|
|
if (get_expr_type(then_node)->id != TypeTableEntryIdUnreachable)
|
2015-12-03 15:47:35 +08:00
|
|
|
LLVMBuildBr(g->builder, endif_block);
|
2015-12-02 12:19:38 +08:00
|
|
|
|
|
|
|
LLVMPositionBuilderAtEnd(g->builder, endif_block);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-01-02 15:06:06 +08:00
|
|
|
static LLVMValueRef gen_if_bool_expr(CodeGen *g, AstNode *node) {
|
|
|
|
assert(node->type == NodeTypeIfBoolExpr);
|
|
|
|
assert(node->data.if_bool_expr.condition);
|
|
|
|
assert(node->data.if_bool_expr.then_block);
|
|
|
|
|
|
|
|
LLVMValueRef cond_value = gen_expr(g, node->data.if_bool_expr.condition);
|
|
|
|
|
|
|
|
return gen_if_bool_expr_raw(g, node, cond_value,
|
|
|
|
node->data.if_bool_expr.then_block,
|
|
|
|
node->data.if_bool_expr.else_node);
|
|
|
|
}
|
|
|
|
|
2015-12-27 06:05:27 +08:00
|
|
|
static LLVMValueRef gen_if_var_expr(CodeGen *g, AstNode *node) {
|
|
|
|
assert(node->type == NodeTypeIfVarExpr);
|
2016-01-02 15:06:06 +08:00
|
|
|
assert(node->data.if_var_expr.var_decl.expr);
|
|
|
|
|
|
|
|
BlockContext *old_block_context = g->cur_block_context;
|
|
|
|
BlockContext *new_block_context = node->codegen_node->data.if_var_node.block_context;
|
|
|
|
|
|
|
|
LLVMValueRef init_val;
|
|
|
|
gen_var_decl_raw(g, node, &node->data.if_var_expr.var_decl, new_block_context, true, &init_val);
|
|
|
|
|
|
|
|
// test if value is the maybe state
|
|
|
|
add_debug_source_node(g, node);
|
|
|
|
LLVMValueRef maybe_field_ptr = LLVMBuildStructGEP(g->builder, init_val, 1, "");
|
|
|
|
LLVMValueRef cond_value = LLVMBuildLoad(g->builder, maybe_field_ptr, "");
|
|
|
|
|
|
|
|
g->cur_block_context = new_block_context;
|
|
|
|
|
|
|
|
LLVMValueRef return_value = gen_if_bool_expr_raw(g, node, cond_value,
|
|
|
|
node->data.if_var_expr.then_block,
|
|
|
|
node->data.if_var_expr.else_node);
|
|
|
|
|
|
|
|
g->cur_block_context = old_block_context;
|
|
|
|
return return_value;
|
2015-12-27 06:05:27 +08:00
|
|
|
}
|
|
|
|
|
2015-12-02 12:19:38 +08:00
|
|
|
static LLVMValueRef gen_block(CodeGen *g, AstNode *block_node, TypeTableEntry *implicit_return_type) {
|
|
|
|
assert(block_node->type == NodeTypeBlock);
|
|
|
|
|
2015-12-13 13:55:29 +08:00
|
|
|
BlockContext *old_block_context = g->cur_block_context;
|
2015-12-08 13:11:04 +08:00
|
|
|
g->cur_block_context = block_node->codegen_node->data.block_node.block_context;
|
2015-12-02 12:19:38 +08:00
|
|
|
|
|
|
|
LLVMValueRef return_value;
|
|
|
|
for (int i = 0; i < block_node->data.block.statements.length; i += 1) {
|
|
|
|
AstNode *statement_node = block_node->data.block.statements.at(i);
|
|
|
|
return_value = gen_expr(g, statement_node);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (implicit_return_type) {
|
2015-12-13 13:55:29 +08:00
|
|
|
add_debug_source_node(g, block_node);
|
2015-12-14 02:59:34 +08:00
|
|
|
if (implicit_return_type->id == TypeTableEntryIdVoid) {
|
2015-12-02 12:19:38 +08:00
|
|
|
LLVMBuildRetVoid(g->builder);
|
2015-12-14 02:59:34 +08:00
|
|
|
} else if (implicit_return_type->id != TypeTableEntryIdUnreachable) {
|
2015-12-02 12:19:38 +08:00
|
|
|
LLVMBuildRet(g->builder, return_value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-13 13:55:29 +08:00
|
|
|
g->cur_block_context = old_block_context;
|
|
|
|
|
2015-12-02 12:19:38 +08:00
|
|
|
return return_value;
|
|
|
|
}
|
|
|
|
|
2015-12-11 18:55:26 +08:00
|
|
|
static int find_asm_index(CodeGen *g, AstNode *node, AsmToken *tok) {
|
|
|
|
const char *ptr = buf_ptr(&node->data.asm_expr.asm_template) + tok->start + 2;
|
|
|
|
int len = tok->end - tok->start - 2;
|
|
|
|
int result = 0;
|
|
|
|
for (int i = 0; i < node->data.asm_expr.output_list.length; i += 1, result += 1) {
|
|
|
|
AsmOutput *asm_output = node->data.asm_expr.output_list.at(i);
|
|
|
|
if (buf_eql_mem(&asm_output->asm_symbolic_name, ptr, len)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i = 0; i < node->data.asm_expr.input_list.length; i += 1, result += 1) {
|
|
|
|
AsmInput *asm_input = node->data.asm_expr.input_list.at(i);
|
|
|
|
if (buf_eql_mem(&asm_input->asm_symbolic_name, ptr, len)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-12-11 06:34:38 +08:00
|
|
|
static LLVMValueRef gen_asm_expr(CodeGen *g, AstNode *node) {
|
|
|
|
assert(node->type == NodeTypeAsmExpr);
|
|
|
|
|
2015-12-11 18:55:26 +08:00
|
|
|
AstNodeAsmExpr *asm_expr = &node->data.asm_expr;
|
|
|
|
|
|
|
|
Buf *src_template = &asm_expr->asm_template;
|
2015-12-11 06:34:38 +08:00
|
|
|
|
|
|
|
Buf llvm_template = BUF_INIT;
|
|
|
|
buf_resize(&llvm_template, 0);
|
|
|
|
|
2015-12-11 18:55:26 +08:00
|
|
|
for (int token_i = 0; token_i < asm_expr->token_list.length; token_i += 1) {
|
|
|
|
AsmToken *asm_token = &asm_expr->token_list.at(token_i);
|
2015-12-11 06:34:38 +08:00
|
|
|
switch (asm_token->id) {
|
|
|
|
case AsmTokenIdTemplate:
|
|
|
|
for (int offset = asm_token->start; offset < asm_token->end; offset += 1) {
|
|
|
|
uint8_t c = *((uint8_t*)(buf_ptr(src_template) + offset));
|
|
|
|
if (c == '$') {
|
|
|
|
buf_append_str(&llvm_template, "$$");
|
|
|
|
} else {
|
|
|
|
buf_append_char(&llvm_template, c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AsmTokenIdPercent:
|
|
|
|
buf_append_char(&llvm_template, '%');
|
|
|
|
break;
|
2015-12-11 18:55:26 +08:00
|
|
|
case AsmTokenIdVar:
|
|
|
|
int index = find_asm_index(g, node, asm_token);
|
|
|
|
assert(index >= 0);
|
|
|
|
buf_appendf(&llvm_template, "$%d", index);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Buf constraint_buf = BUF_INIT;
|
|
|
|
buf_resize(&constraint_buf, 0);
|
2015-12-15 15:46:56 +08:00
|
|
|
|
|
|
|
assert(asm_expr->return_count == 0 || asm_expr->return_count == 1);
|
|
|
|
|
2015-12-11 18:55:26 +08:00
|
|
|
int total_constraint_count = asm_expr->output_list.length +
|
|
|
|
asm_expr->input_list.length +
|
|
|
|
asm_expr->clobber_list.length;
|
|
|
|
int input_and_output_count = asm_expr->output_list.length +
|
2015-12-15 15:46:56 +08:00
|
|
|
asm_expr->input_list.length -
|
|
|
|
asm_expr->return_count;
|
2015-12-11 18:55:26 +08:00
|
|
|
int total_index = 0;
|
2015-12-15 15:46:56 +08:00
|
|
|
int param_index = 0;
|
2015-12-11 18:55:26 +08:00
|
|
|
LLVMTypeRef *param_types = allocate<LLVMTypeRef>(input_and_output_count);
|
|
|
|
LLVMValueRef *param_values = allocate<LLVMValueRef>(input_and_output_count);
|
|
|
|
for (int i = 0; i < asm_expr->output_list.length; i += 1, total_index += 1) {
|
|
|
|
AsmOutput *asm_output = asm_expr->output_list.at(i);
|
2015-12-15 16:55:52 +08:00
|
|
|
bool is_return = (asm_output->return_type != nullptr);
|
|
|
|
assert(*buf_ptr(&asm_output->constraint) == '=');
|
|
|
|
if (is_return) {
|
|
|
|
buf_appendf(&constraint_buf, "=%s", buf_ptr(&asm_output->constraint) + 1);
|
2015-12-11 18:55:26 +08:00
|
|
|
} else {
|
2015-12-15 16:55:52 +08:00
|
|
|
buf_appendf(&constraint_buf, "=*%s", buf_ptr(&asm_output->constraint) + 1);
|
2015-12-11 18:55:26 +08:00
|
|
|
}
|
|
|
|
if (total_index + 1 < total_constraint_count) {
|
|
|
|
buf_append_char(&constraint_buf, ',');
|
|
|
|
}
|
|
|
|
|
2015-12-15 15:46:56 +08:00
|
|
|
if (!is_return) {
|
|
|
|
VariableTableEntry *variable = find_variable(
|
|
|
|
node->codegen_node->expr_node.block_context,
|
|
|
|
&asm_output->variable_name);
|
|
|
|
assert(variable);
|
|
|
|
param_types[param_index] = LLVMTypeOf(variable->value_ref);
|
|
|
|
param_values[param_index] = variable->value_ref;
|
|
|
|
param_index += 1;
|
|
|
|
}
|
2015-12-11 18:55:26 +08:00
|
|
|
}
|
2015-12-15 15:46:56 +08:00
|
|
|
for (int i = 0; i < asm_expr->input_list.length; i += 1, total_index += 1, param_index += 1) {
|
2015-12-11 18:55:26 +08:00
|
|
|
AsmInput *asm_input = asm_expr->input_list.at(i);
|
|
|
|
buf_append_buf(&constraint_buf, &asm_input->constraint);
|
|
|
|
if (total_index + 1 < total_constraint_count) {
|
|
|
|
buf_append_char(&constraint_buf, ',');
|
|
|
|
}
|
|
|
|
|
|
|
|
TypeTableEntry *expr_type = get_expr_type(asm_input->expr);
|
2015-12-15 15:46:56 +08:00
|
|
|
param_types[param_index] = expr_type->type_ref;
|
|
|
|
param_values[param_index] = gen_expr(g, asm_input->expr);
|
2015-12-11 18:55:26 +08:00
|
|
|
}
|
|
|
|
for (int i = 0; i < asm_expr->clobber_list.length; i += 1, total_index += 1) {
|
|
|
|
Buf *clobber_buf = asm_expr->clobber_list.at(i);
|
|
|
|
buf_appendf(&constraint_buf, "~{%s}", buf_ptr(clobber_buf));
|
|
|
|
if (total_index + 1 < total_constraint_count) {
|
|
|
|
buf_append_char(&constraint_buf, ',');
|
2015-12-11 06:34:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-15 15:46:56 +08:00
|
|
|
LLVMTypeRef ret_type;
|
|
|
|
if (asm_expr->return_count == 0) {
|
|
|
|
ret_type = LLVMVoidType();
|
|
|
|
} else {
|
|
|
|
ret_type = get_expr_type(node)->type_ref;
|
|
|
|
}
|
|
|
|
LLVMTypeRef function_type = LLVMFunctionType(ret_type, param_types, input_and_output_count, false);
|
2015-12-11 06:34:38 +08:00
|
|
|
|
2015-12-11 18:55:26 +08:00
|
|
|
bool is_volatile = asm_expr->is_volatile || (asm_expr->output_list.length == 0);
|
|
|
|
LLVMValueRef asm_fn = LLVMConstInlineAsm(function_type, buf_ptr(&llvm_template),
|
|
|
|
buf_ptr(&constraint_buf), is_volatile, false);
|
2015-12-11 06:34:38 +08:00
|
|
|
|
|
|
|
add_debug_source_node(g, node);
|
2015-12-11 18:55:26 +08:00
|
|
|
return LLVMBuildCall(g->builder, asm_fn, param_values, input_and_output_count, "");
|
2015-12-11 06:34:38 +08:00
|
|
|
}
|
|
|
|
|
2015-12-24 15:00:23 +08:00
|
|
|
static LLVMValueRef gen_struct_val_expr(CodeGen *g, AstNode *node) {
|
|
|
|
assert(node->type == NodeTypeStructValueExpr);
|
|
|
|
|
|
|
|
TypeTableEntry *type_entry = get_expr_type(node);
|
|
|
|
|
|
|
|
assert(type_entry->id == TypeTableEntryIdStruct);
|
|
|
|
|
|
|
|
int field_count = type_entry->data.structure.field_count;
|
|
|
|
assert(field_count == node->data.struct_val_expr.fields.length);
|
|
|
|
|
|
|
|
StructValExprNode *struct_val_expr_node = &node->codegen_node->data.struct_val_expr_node;
|
|
|
|
LLVMValueRef tmp_struct_ptr = struct_val_expr_node->ptr;
|
|
|
|
|
|
|
|
for (int i = 0; i < field_count; i += 1) {
|
|
|
|
AstNode *field_node = node->data.struct_val_expr.fields.at(i);
|
|
|
|
int index = field_node->codegen_node->data.struct_val_field_node.index;
|
|
|
|
TypeStructField *type_struct_field = &type_entry->data.structure.fields[index];
|
|
|
|
assert(buf_eql_buf(type_struct_field->name, &field_node->data.struct_val_field.name));
|
|
|
|
|
|
|
|
add_debug_source_node(g, field_node);
|
|
|
|
LLVMValueRef field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, index, "");
|
|
|
|
LLVMValueRef value = gen_expr(g, field_node->data.struct_val_field.expr);
|
|
|
|
LLVMBuildStore(g->builder, value, field_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmp_struct_ptr;
|
|
|
|
}
|
|
|
|
|
2015-12-25 05:37:43 +08:00
|
|
|
static LLVMValueRef gen_while_expr(CodeGen *g, AstNode *node) {
|
|
|
|
assert(node->type == NodeTypeWhileExpr);
|
|
|
|
assert(node->data.while_expr.condition);
|
|
|
|
assert(node->data.while_expr.body);
|
|
|
|
|
|
|
|
LLVMBasicBlockRef cond_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "WhileCond");
|
|
|
|
LLVMBasicBlockRef body_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "WhileBody");
|
|
|
|
LLVMBasicBlockRef end_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "WhileEnd");
|
|
|
|
|
|
|
|
add_debug_source_node(g, node);
|
|
|
|
LLVMBuildBr(g->builder, cond_block);
|
|
|
|
|
|
|
|
LLVMPositionBuilderAtEnd(g->builder, cond_block);
|
|
|
|
LLVMValueRef cond_val = gen_expr(g, node->data.while_expr.condition);
|
|
|
|
add_debug_source_node(g, node->data.while_expr.condition);
|
|
|
|
LLVMBuildCondBr(g->builder, cond_val, body_block, end_block);
|
|
|
|
|
|
|
|
LLVMPositionBuilderAtEnd(g->builder, body_block);
|
2015-12-25 06:30:32 +08:00
|
|
|
g->break_block_stack.append(end_block);
|
2015-12-25 06:36:49 +08:00
|
|
|
g->continue_block_stack.append(cond_block);
|
2015-12-25 05:37:43 +08:00
|
|
|
gen_expr(g, node->data.while_expr.body);
|
2015-12-25 06:30:32 +08:00
|
|
|
g->break_block_stack.pop();
|
2015-12-25 06:36:49 +08:00
|
|
|
g->continue_block_stack.pop();
|
2015-12-25 06:30:32 +08:00
|
|
|
if (get_expr_type(node->data.while_expr.body)->id != TypeTableEntryIdUnreachable) {
|
|
|
|
LLVMBuildBr(g->builder, cond_block);
|
|
|
|
}
|
2015-12-25 05:37:43 +08:00
|
|
|
|
|
|
|
LLVMPositionBuilderAtEnd(g->builder, end_block);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-12-25 06:09:35 +08:00
|
|
|
static LLVMValueRef gen_break(CodeGen *g, AstNode *node) {
|
|
|
|
assert(node->type == NodeTypeBreak);
|
2015-12-25 06:30:32 +08:00
|
|
|
LLVMBasicBlockRef dest_block = g->break_block_stack.last();
|
2015-12-25 06:09:35 +08:00
|
|
|
|
|
|
|
add_debug_source_node(g, node);
|
2015-12-25 06:30:32 +08:00
|
|
|
return LLVMBuildBr(g->builder, dest_block);
|
2015-12-25 06:09:35 +08:00
|
|
|
}
|
|
|
|
|
2015-12-25 06:36:49 +08:00
|
|
|
static LLVMValueRef gen_continue(CodeGen *g, AstNode *node) {
|
|
|
|
assert(node->type == NodeTypeContinue);
|
|
|
|
LLVMBasicBlockRef dest_block = g->continue_block_stack.last();
|
|
|
|
|
|
|
|
add_debug_source_node(g, node);
|
|
|
|
return LLVMBuildBr(g->builder, dest_block);
|
|
|
|
}
|
|
|
|
|
2016-01-02 15:06:06 +08:00
|
|
|
static LLVMValueRef gen_var_decl_raw(CodeGen *g, AstNode *source_node, AstNodeVariableDeclaration *var_decl,
|
|
|
|
BlockContext *block_context, bool unwrap_maybe, LLVMValueRef *init_value)
|
|
|
|
{
|
|
|
|
VariableTableEntry *variable = find_variable(block_context, &var_decl->symbol);
|
|
|
|
|
|
|
|
assert(variable);
|
|
|
|
assert(variable->is_ptr);
|
|
|
|
|
|
|
|
if (var_decl->expr) {
|
|
|
|
*init_value = gen_expr(g, var_decl->expr);
|
|
|
|
} else {
|
|
|
|
*init_value = LLVMConstNull(variable->type->type_ref);
|
|
|
|
}
|
|
|
|
if (variable->type->id == TypeTableEntryIdVoid) {
|
|
|
|
return nullptr;
|
|
|
|
} else {
|
|
|
|
LLVMValueRef store_instr;
|
|
|
|
LLVMValueRef value;
|
|
|
|
if (unwrap_maybe) {
|
|
|
|
assert(var_decl->expr);
|
|
|
|
add_debug_source_node(g, source_node);
|
|
|
|
LLVMValueRef maybe_field_ptr = LLVMBuildStructGEP(g->builder, *init_value, 0, "");
|
|
|
|
// TODO if it's a struct we might not want to load the pointer
|
|
|
|
value = LLVMBuildLoad(g->builder, maybe_field_ptr, "");
|
|
|
|
} else {
|
|
|
|
value = *init_value;
|
|
|
|
}
|
|
|
|
if ((variable->type->id == TypeTableEntryIdStruct || variable->type->id == TypeTableEntryIdMaybe) &&
|
|
|
|
var_decl->expr)
|
|
|
|
{
|
|
|
|
store_instr = gen_struct_memcpy(g, source_node, value, variable->value_ref, variable->type);
|
|
|
|
} else {
|
|
|
|
add_debug_source_node(g, source_node);
|
|
|
|
store_instr = LLVMBuildStore(g->builder, value, variable->value_ref);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMZigDILocation *debug_loc = LLVMZigGetDebugLoc(source_node->line + 1, source_node->column + 1,
|
|
|
|
g->cur_block_context->di_scope);
|
|
|
|
LLVMZigInsertDeclare(g->dbuilder, variable->value_ref, variable->di_loc_var, debug_loc, store_instr);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static LLVMValueRef gen_var_decl_expr(CodeGen *g, AstNode *node) {
|
|
|
|
LLVMValueRef init_val;
|
|
|
|
return gen_var_decl_raw(g, node, &node->data.variable_declaration,
|
|
|
|
node->codegen_node->expr_node.block_context, false, &init_val);
|
|
|
|
}
|
|
|
|
|
2016-01-04 10:38:36 +08:00
|
|
|
static LLVMValueRef gen_number_literal_raw(CodeGen *g, AstNode *source_node,
|
|
|
|
NumberLiteralNode *codegen_num_lit, AstNodeNumberLiteral *num_lit_node)
|
|
|
|
{
|
|
|
|
TypeTableEntry *type_entry = codegen_num_lit->resolved_type;
|
|
|
|
assert(type_entry);
|
|
|
|
|
|
|
|
// override the expression type for number literals
|
|
|
|
source_node->codegen_node->expr_node.type_entry = type_entry;
|
|
|
|
|
|
|
|
if (type_entry->id == TypeTableEntryIdInt) {
|
|
|
|
// here the union has int64_t and uint64_t and we purposefully read
|
|
|
|
// the uint64_t value in either case, because we want the twos
|
|
|
|
// complement representation
|
|
|
|
|
|
|
|
return LLVMConstInt(type_entry->type_ref,
|
|
|
|
num_lit_node->data.x_uint,
|
|
|
|
type_entry->data.integral.is_signed);
|
|
|
|
} else if (type_entry->id == TypeTableEntryIdFloat) {
|
|
|
|
|
|
|
|
return LLVMConstReal(type_entry->type_ref,
|
|
|
|
num_lit_node->data.x_float);
|
|
|
|
} else {
|
|
|
|
zig_panic("bad number literal type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static LLVMValueRef gen_compiler_fn_type(CodeGen *g, AstNode *node) {
|
|
|
|
assert(node->type == NodeTypeCompilerFnType);
|
|
|
|
|
|
|
|
Buf *name = &node->data.compiler_fn_type.name;
|
|
|
|
if (buf_eql_str(name, "sizeof")) {
|
|
|
|
TypeTableEntry *type_entry = get_type_for_type_node(g, node->data.compiler_fn_type.type);
|
|
|
|
NumberLiteralNode *codegen_num_lit = &node->codegen_node->data.num_lit_node;
|
|
|
|
AstNodeNumberLiteral num_lit_node;
|
|
|
|
num_lit_node.kind = type_entry->data.num_lit.kind;
|
|
|
|
num_lit_node.overflow = false;
|
|
|
|
num_lit_node.data.x_uint = type_entry->size_in_bits / 8;
|
|
|
|
return gen_number_literal_raw(g, node, codegen_num_lit, &num_lit_node);
|
|
|
|
} else {
|
|
|
|
zig_unreachable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static LLVMValueRef gen_number_literal(CodeGen *g, AstNode *node) {
|
|
|
|
assert(node->type == NodeTypeNumberLiteral);
|
|
|
|
|
|
|
|
NumberLiteralNode *codegen_num_lit = &node->codegen_node->data.num_lit_node;
|
|
|
|
assert(codegen_num_lit);
|
|
|
|
|
|
|
|
return gen_number_literal_raw(g, node, codegen_num_lit, &node->data.number_literal);
|
|
|
|
}
|
|
|
|
|
2015-12-16 08:11:44 +08:00
|
|
|
static LLVMValueRef gen_expr_no_cast(CodeGen *g, AstNode *node) {
|
2015-11-28 15:40:54 +08:00
|
|
|
switch (node->type) {
|
2015-11-30 05:46:05 +08:00
|
|
|
case NodeTypeBinOpExpr:
|
|
|
|
return gen_bin_op_expr(g, node);
|
2015-11-28 15:40:54 +08:00
|
|
|
case NodeTypeReturnExpr:
|
|
|
|
return gen_return_expr(g, node);
|
2015-12-03 07:33:06 +08:00
|
|
|
case NodeTypeVariableDeclaration:
|
2016-01-02 15:06:06 +08:00
|
|
|
return gen_var_decl_expr(g, node);
|
2015-11-30 05:46:05 +08:00
|
|
|
case NodeTypeCastExpr:
|
|
|
|
return gen_cast_expr(g, node);
|
2015-11-30 05:00:34 +08:00
|
|
|
case NodeTypePrefixOpExpr:
|
|
|
|
return gen_prefix_op_expr(g, node);
|
2015-11-30 05:39:11 +08:00
|
|
|
case NodeTypeFnCallExpr:
|
|
|
|
return gen_fn_call_expr(g, node);
|
2015-12-09 05:15:34 +08:00
|
|
|
case NodeTypeArrayAccessExpr:
|
2015-12-23 04:22:40 +08:00
|
|
|
return gen_array_access_expr(g, node, false);
|
2015-12-12 15:10:37 +08:00
|
|
|
case NodeTypeFieldAccessExpr:
|
2015-12-23 04:22:40 +08:00
|
|
|
return gen_field_access_expr(g, node, false);
|
2015-11-30 08:16:32 +08:00
|
|
|
case NodeTypeUnreachable:
|
|
|
|
add_debug_source_node(g, node);
|
|
|
|
return LLVMBuildUnreachable(g->builder);
|
2015-12-02 06:54:46 +08:00
|
|
|
case NodeTypeVoid:
|
|
|
|
return nullptr;
|
2015-12-04 03:15:07 +08:00
|
|
|
case NodeTypeBoolLiteral:
|
|
|
|
if (node->data.bool_literal)
|
|
|
|
return LLVMConstAllOnes(LLVMInt1Type());
|
|
|
|
else
|
|
|
|
return LLVMConstNull(LLVMInt1Type());
|
2015-12-27 06:05:27 +08:00
|
|
|
case NodeTypeIfBoolExpr:
|
|
|
|
return gen_if_bool_expr(g, node);
|
|
|
|
case NodeTypeIfVarExpr:
|
|
|
|
return gen_if_var_expr(g, node);
|
2015-12-25 05:37:43 +08:00
|
|
|
case NodeTypeWhileExpr:
|
|
|
|
return gen_while_expr(g, node);
|
2015-12-11 06:34:38 +08:00
|
|
|
case NodeTypeAsmExpr:
|
|
|
|
return gen_asm_expr(g, node);
|
2015-11-30 08:16:32 +08:00
|
|
|
case NodeTypeNumberLiteral:
|
2016-01-04 10:38:36 +08:00
|
|
|
return gen_number_literal(g, node);
|
2015-11-30 08:16:32 +08:00
|
|
|
case NodeTypeStringLiteral:
|
|
|
|
{
|
2015-12-12 15:10:37 +08:00
|
|
|
Buf *str = &node->data.string_literal.buf;
|
|
|
|
LLVMValueRef str_val = find_or_create_string(g, str, node->data.string_literal.c);
|
2015-11-30 08:16:32 +08:00
|
|
|
LLVMValueRef indices[] = {
|
2016-01-03 10:47:36 +08:00
|
|
|
LLVMConstNull(g->builtin_types.entry_usize->type_ref),
|
|
|
|
LLVMConstNull(g->builtin_types.entry_usize->type_ref),
|
2015-11-30 08:16:32 +08:00
|
|
|
};
|
|
|
|
LLVMValueRef ptr_val = LLVMBuildInBoundsGEP(g->builder, str_val, indices, 2, "");
|
|
|
|
return ptr_val;
|
|
|
|
}
|
2016-01-02 18:38:45 +08:00
|
|
|
case NodeTypeCharLiteral:
|
|
|
|
return LLVMConstInt(LLVMInt8Type(), node->data.char_literal.value, false);
|
2015-11-30 08:28:28 +08:00
|
|
|
case NodeTypeSymbol:
|
|
|
|
{
|
2015-12-15 14:10:18 +08:00
|
|
|
VariableTableEntry *variable = find_variable(
|
2015-12-08 11:57:45 +08:00
|
|
|
node->codegen_node->expr_node.block_context,
|
|
|
|
&node->data.symbol);
|
|
|
|
assert(variable);
|
2015-12-14 02:59:34 +08:00
|
|
|
if (variable->type->id == TypeTableEntryIdVoid) {
|
2015-12-08 11:57:45 +08:00
|
|
|
return nullptr;
|
|
|
|
} else if (variable->is_ptr) {
|
2015-12-15 14:10:18 +08:00
|
|
|
assert(variable->value_ref);
|
2015-12-09 05:47:17 +08:00
|
|
|
if (variable->type->id == TypeTableEntryIdArray) {
|
|
|
|
return variable->value_ref;
|
2016-01-02 15:06:06 +08:00
|
|
|
} else if (variable->type->id == TypeTableEntryIdStruct ||
|
|
|
|
variable->type->id == TypeTableEntryIdMaybe)
|
|
|
|
{
|
2015-12-13 13:55:29 +08:00
|
|
|
return variable->value_ref;
|
2015-12-09 05:47:17 +08:00
|
|
|
} else {
|
|
|
|
add_debug_source_node(g, node);
|
|
|
|
return LLVMBuildLoad(g->builder, variable->value_ref, "");
|
|
|
|
}
|
2015-12-08 11:57:45 +08:00
|
|
|
} else {
|
|
|
|
return variable->value_ref;
|
2015-12-07 14:09:46 +08:00
|
|
|
}
|
2015-11-30 08:28:28 +08:00
|
|
|
}
|
2015-12-02 12:19:38 +08:00
|
|
|
case NodeTypeBlock:
|
|
|
|
return gen_block(g, node, nullptr);
|
2015-12-03 15:47:35 +08:00
|
|
|
case NodeTypeGoto:
|
|
|
|
add_debug_source_node(g, node);
|
|
|
|
return LLVMBuildBr(g->builder, node->codegen_node->data.label_entry->basic_block);
|
2015-12-25 06:09:35 +08:00
|
|
|
case NodeTypeBreak:
|
|
|
|
return gen_break(g, node);
|
2015-12-25 06:36:49 +08:00
|
|
|
case NodeTypeContinue:
|
|
|
|
return gen_continue(g, node);
|
2015-12-03 15:47:35 +08:00
|
|
|
case NodeTypeLabel:
|
|
|
|
{
|
2015-12-07 14:09:46 +08:00
|
|
|
LabelTableEntry *label_entry = node->codegen_node->data.label_entry;
|
|
|
|
assert(label_entry);
|
|
|
|
LLVMBasicBlockRef basic_block = label_entry->basic_block;
|
|
|
|
if (label_entry->entered_from_fallthrough) {
|
|
|
|
add_debug_source_node(g, node);
|
|
|
|
LLVMBuildBr(g->builder, basic_block);
|
|
|
|
}
|
2015-12-03 15:47:35 +08:00
|
|
|
LLVMPositionBuilderAtEnd(g->builder, basic_block);
|
2015-12-07 14:09:46 +08:00
|
|
|
return nullptr;
|
2015-12-03 15:47:35 +08:00
|
|
|
}
|
2015-12-24 15:00:23 +08:00
|
|
|
case NodeTypeStructValueExpr:
|
|
|
|
return gen_struct_val_expr(g, node);
|
2016-01-04 10:38:36 +08:00
|
|
|
case NodeTypeCompilerFnType:
|
|
|
|
return gen_compiler_fn_type(g, node);
|
2015-11-28 15:40:54 +08:00
|
|
|
case NodeTypeRoot:
|
|
|
|
case NodeTypeRootExportDecl:
|
|
|
|
case NodeTypeFnProto:
|
|
|
|
case NodeTypeFnDef:
|
|
|
|
case NodeTypeFnDecl:
|
|
|
|
case NodeTypeParamDecl:
|
|
|
|
case NodeTypeType:
|
|
|
|
case NodeTypeExternBlock:
|
|
|
|
case NodeTypeDirective:
|
2015-12-01 10:58:53 +08:00
|
|
|
case NodeTypeUse:
|
2015-12-12 15:10:37 +08:00
|
|
|
case NodeTypeStructDecl:
|
|
|
|
case NodeTypeStructField:
|
2015-12-24 15:00:23 +08:00
|
|
|
case NodeTypeStructValueField:
|
2016-01-04 10:38:36 +08:00
|
|
|
case NodeTypeCompilerFnExpr:
|
2015-11-28 15:40:54 +08:00
|
|
|
zig_unreachable();
|
2015-11-24 15:35:23 +08:00
|
|
|
}
|
|
|
|
zig_unreachable();
|
|
|
|
}
|
|
|
|
|
2016-01-02 15:06:06 +08:00
|
|
|
static LLVMValueRef gen_cast_node(CodeGen *g, AstNode *node, LLVMValueRef val, TypeTableEntry *before_type,
|
|
|
|
CastNode *cast_node)
|
|
|
|
{
|
|
|
|
return cast_node->after_type ? gen_bare_cast(g, node, val, before_type, cast_node->after_type, cast_node) : val;
|
|
|
|
}
|
|
|
|
|
2015-12-16 08:11:44 +08:00
|
|
|
static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) {
|
|
|
|
LLVMValueRef val = gen_expr_no_cast(g, node);
|
|
|
|
|
|
|
|
if (node->type == NodeTypeVoid) {
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(node->codegen_node);
|
|
|
|
|
2016-01-02 15:06:06 +08:00
|
|
|
{
|
|
|
|
TypeTableEntry *before_type = node->codegen_node->expr_node.type_entry;
|
2016-01-03 11:13:10 +08:00
|
|
|
if (before_type && before_type->id == TypeTableEntryIdUnreachable) {
|
|
|
|
return val;
|
|
|
|
}
|
2016-01-02 15:06:06 +08:00
|
|
|
val = gen_cast_node(g, node, val, before_type, &node->codegen_node->expr_node.implicit_cast);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
TypeTableEntry *before_type = node->codegen_node->expr_node.implicit_cast.after_type;
|
|
|
|
val = gen_cast_node(g, node, val, before_type, &node->codegen_node->expr_node.implicit_maybe_cast);
|
|
|
|
}
|
2015-12-16 08:11:44 +08:00
|
|
|
|
2016-01-02 15:06:06 +08:00
|
|
|
return val;
|
2015-12-16 08:11:44 +08:00
|
|
|
}
|
|
|
|
|
2015-12-03 15:47:35 +08:00
|
|
|
static void build_label_blocks(CodeGen *g, AstNode *block_node) {
|
|
|
|
assert(block_node->type == NodeTypeBlock);
|
|
|
|
for (int i = 0; i < block_node->data.block.statements.length; i += 1) {
|
|
|
|
AstNode *label_node = block_node->data.block.statements.at(i);
|
|
|
|
if (label_node->type != NodeTypeLabel)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Buf *name = &label_node->data.label.name;
|
|
|
|
label_node->codegen_node->data.label_entry->basic_block = LLVMAppendBasicBlock(
|
|
|
|
g->cur_fn->fn_value, buf_ptr(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-12-01 05:10:07 +08:00
|
|
|
static LLVMZigDISubroutineType *create_di_function_type(CodeGen *g, AstNodeFnProto *fn_proto,
|
|
|
|
LLVMZigDIFile *di_file)
|
2015-11-25 10:54:10 +08:00
|
|
|
{
|
2015-12-01 05:43:09 +08:00
|
|
|
LLVMZigDIType **types = allocate<LLVMZigDIType*>(1 + fn_proto->params.length);
|
2015-12-13 13:55:29 +08:00
|
|
|
types[0] = to_llvm_debug_type(g, fn_proto->return_type);
|
2015-12-01 05:43:09 +08:00
|
|
|
int types_len = fn_proto->params.length + 1;
|
2015-11-25 10:07:33 +08:00
|
|
|
for (int i = 0; i < fn_proto->params.length; i += 1) {
|
|
|
|
AstNode *param_node = fn_proto->params.at(i);
|
2015-11-30 05:59:41 +08:00
|
|
|
assert(param_node->type == NodeTypeParamDecl);
|
2015-12-13 13:55:29 +08:00
|
|
|
LLVMZigDIType *param_type = to_llvm_debug_type(g, param_node->data.param_decl.type);
|
2015-12-01 05:43:09 +08:00
|
|
|
types[i + 1] = param_type;
|
2015-11-25 10:07:33 +08:00
|
|
|
}
|
2015-12-01 05:43:09 +08:00
|
|
|
return LLVMZigCreateSubroutineType(g->dbuilder, di_file, types, types_len, 0);
|
2015-11-25 10:07:33 +08:00
|
|
|
}
|
|
|
|
|
2015-12-11 06:34:38 +08:00
|
|
|
static LLVMAttribute to_llvm_fn_attr(FnAttrId attr_id) {
|
|
|
|
switch (attr_id) {
|
|
|
|
case FnAttrIdNaked:
|
|
|
|
return LLVMNakedAttribute;
|
|
|
|
case FnAttrIdAlwaysInline:
|
|
|
|
return LLVMAlwaysInlineAttribute;
|
|
|
|
}
|
|
|
|
zig_unreachable();
|
|
|
|
}
|
|
|
|
|
2015-12-01 10:58:53 +08:00
|
|
|
static void do_code_gen(CodeGen *g) {
|
2015-11-26 09:17:35 +08:00
|
|
|
assert(!g->errors.length);
|
|
|
|
|
2015-12-23 18:19:22 +08:00
|
|
|
{
|
|
|
|
LLVMTypeRef param_types[] = {
|
|
|
|
LLVMPointerType(LLVMInt8Type(), 0),
|
|
|
|
LLVMPointerType(LLVMInt8Type(), 0),
|
|
|
|
LLVMIntType(g->pointer_size_bytes * 8),
|
|
|
|
LLVMInt32Type(),
|
|
|
|
LLVMInt1Type(),
|
|
|
|
};
|
|
|
|
LLVMTypeRef fn_type = LLVMFunctionType(LLVMVoidType(), param_types, 5, false);
|
|
|
|
Buf *name = buf_sprintf("llvm.memcpy.p0i8.p0i8.i%d", g->pointer_size_bytes * 8);
|
|
|
|
g->memcpy_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type);
|
|
|
|
assert(LLVMGetIntrinsicID(g->memcpy_fn_val));
|
|
|
|
}
|
|
|
|
|
2015-12-15 14:10:18 +08:00
|
|
|
// Generate module level variables
|
|
|
|
for (int i = 0; i < g->global_vars.length; i += 1) {
|
|
|
|
VariableTableEntry *var = g->global_vars.at(i);
|
|
|
|
|
|
|
|
// TODO if the global is exported, set external linkage
|
2015-12-15 14:49:56 +08:00
|
|
|
LLVMValueRef global_value = LLVMAddGlobal(g->module, var->type->type_ref, "");
|
2015-12-15 14:10:18 +08:00
|
|
|
LLVMSetLinkage(global_value, LLVMPrivateLinkage);
|
2015-12-15 14:49:56 +08:00
|
|
|
|
|
|
|
if (var->is_const) {
|
|
|
|
LLVMValueRef init_val = gen_expr(g, var->decl_node->data.variable_declaration.expr);
|
|
|
|
LLVMSetInitializer(global_value, init_val);
|
|
|
|
} else {
|
|
|
|
LLVMSetInitializer(global_value, LLVMConstNull(var->type->type_ref));
|
|
|
|
}
|
2015-12-15 14:10:18 +08:00
|
|
|
LLVMSetGlobalConstant(global_value, var->is_const);
|
|
|
|
LLVMSetUnnamedAddr(global_value, true);
|
|
|
|
|
|
|
|
var->value_ref = global_value;
|
|
|
|
}
|
|
|
|
|
2015-11-26 09:17:35 +08:00
|
|
|
// Generate function prototypes
|
2015-12-15 14:10:18 +08:00
|
|
|
for (int fn_proto_i = 0; fn_proto_i < g->fn_protos.length; fn_proto_i += 1) {
|
|
|
|
FnTableEntry *fn_table_entry = g->fn_protos.at(fn_proto_i);
|
2015-11-26 09:17:35 +08:00
|
|
|
|
|
|
|
AstNode *proto_node = fn_table_entry->proto_node;
|
|
|
|
assert(proto_node->type == NodeTypeFnProto);
|
|
|
|
AstNodeFnProto *fn_proto = &proto_node->data.fn_proto;
|
2015-11-24 15:35:23 +08:00
|
|
|
|
2015-12-23 04:22:40 +08:00
|
|
|
LLVMTypeRef ret_type = get_type_for_type_node(g, fn_proto->return_type)->type_ref;
|
2015-12-04 05:29:19 +08:00
|
|
|
int param_count = count_non_void_params(g, &fn_proto->params);
|
|
|
|
LLVMTypeRef *param_types = allocate<LLVMTypeRef>(param_count);
|
|
|
|
int gen_param_index = 0;
|
2015-11-24 17:43:45 +08:00
|
|
|
for (int param_decl_i = 0; param_decl_i < fn_proto->params.length; param_decl_i += 1) {
|
|
|
|
AstNode *param_node = fn_proto->params.at(param_decl_i);
|
2015-11-24 15:35:23 +08:00
|
|
|
assert(param_node->type == NodeTypeParamDecl);
|
2015-12-04 05:29:19 +08:00
|
|
|
if (is_param_decl_type_void(g, param_node))
|
|
|
|
continue;
|
2015-11-24 15:35:23 +08:00
|
|
|
AstNode *type_node = param_node->data.param_decl.type;
|
2015-12-13 13:55:29 +08:00
|
|
|
param_types[gen_param_index] = fn_proto_type_from_type_node(g, type_node);
|
2015-12-04 05:29:19 +08:00
|
|
|
gen_param_index += 1;
|
2015-11-24 15:35:23 +08:00
|
|
|
}
|
2015-12-09 16:03:04 +08:00
|
|
|
LLVMTypeRef function_type = LLVMFunctionType(ret_type, param_types, param_count, fn_proto->is_var_args);
|
2016-01-05 08:14:53 +08:00
|
|
|
|
|
|
|
LLVMValueRef fn = LLVMAddFunction(g->module, buf_ptr(&fn_table_entry->symbol_name), function_type);
|
2015-11-24 15:35:23 +08:00
|
|
|
|
2015-12-11 06:34:38 +08:00
|
|
|
for (int attr_i = 0; attr_i < fn_table_entry->fn_attr_list.length; attr_i += 1) {
|
|
|
|
FnAttrId attr_id = fn_table_entry->fn_attr_list.at(attr_i);
|
|
|
|
LLVMAddFunctionAttr(fn, to_llvm_fn_attr(attr_id));
|
|
|
|
}
|
|
|
|
|
2015-11-28 06:46:06 +08:00
|
|
|
LLVMSetLinkage(fn, fn_table_entry->internal_linkage ? LLVMInternalLinkage : LLVMExternalLinkage);
|
2015-11-25 10:07:33 +08:00
|
|
|
|
2015-12-01 06:36:58 +08:00
|
|
|
if (type_is_unreachable(g, fn_proto->return_type)) {
|
2015-11-25 04:37:14 +08:00
|
|
|
LLVMAddFunctionAttr(fn, LLVMNoReturnAttribute);
|
|
|
|
}
|
2015-11-28 06:46:06 +08:00
|
|
|
LLVMSetFunctionCallConv(fn, fn_table_entry->calling_convention);
|
|
|
|
if (!fn_table_entry->is_extern) {
|
2015-11-26 09:17:35 +08:00
|
|
|
LLVMAddFunctionAttr(fn, LLVMNoUnwindAttribute);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn_table_entry->fn_value = fn;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate function definitions.
|
2015-12-13 13:55:29 +08:00
|
|
|
for (int fn_i = 0; fn_i < g->fn_defs.length; fn_i += 1) {
|
|
|
|
FnTableEntry *fn_table_entry = g->fn_defs.at(fn_i);
|
2015-12-01 10:58:53 +08:00
|
|
|
ImportTableEntry *import = fn_table_entry->import_entry;
|
2015-11-26 09:17:35 +08:00
|
|
|
AstNode *fn_def_node = fn_table_entry->fn_def_node;
|
|
|
|
LLVMValueRef fn = fn_table_entry->fn_value;
|
2015-11-30 08:28:28 +08:00
|
|
|
g->cur_fn = fn_table_entry;
|
2015-11-26 09:17:35 +08:00
|
|
|
|
|
|
|
AstNode *proto_node = fn_table_entry->proto_node;
|
|
|
|
assert(proto_node->type == NodeTypeFnProto);
|
|
|
|
AstNodeFnProto *fn_proto = &proto_node->data.fn_proto;
|
2015-11-25 10:07:33 +08:00
|
|
|
|
|
|
|
// Add debug info.
|
|
|
|
unsigned line_number = fn_def_node->line + 1;
|
|
|
|
unsigned scope_line = line_number;
|
|
|
|
bool is_definition = true;
|
|
|
|
unsigned flags = 0;
|
2015-12-01 10:58:53 +08:00
|
|
|
bool is_optimized = g->build_type == CodeGenBuildTypeRelease;
|
2015-12-01 05:10:07 +08:00
|
|
|
LLVMZigDISubprogram *subprogram = LLVMZigCreateFunction(g->dbuilder,
|
2016-01-05 08:14:53 +08:00
|
|
|
import->block_context->di_scope, buf_ptr(&fn_table_entry->symbol_name), "",
|
|
|
|
import->di_file, line_number,
|
2015-12-01 10:58:53 +08:00
|
|
|
create_di_function_type(g, fn_proto, import->di_file), fn_table_entry->internal_linkage,
|
2015-12-01 05:10:07 +08:00
|
|
|
is_definition, scope_line, flags, is_optimized, fn);
|
2015-11-25 10:07:33 +08:00
|
|
|
|
2015-11-24 17:43:45 +08:00
|
|
|
LLVMBasicBlockRef entry_block = LLVMAppendBasicBlock(fn, "entry");
|
|
|
|
LLVMPositionBuilderAtEnd(g->builder, entry_block);
|
2015-11-24 15:35:23 +08:00
|
|
|
|
2015-11-28 01:52:31 +08:00
|
|
|
CodeGenNode *codegen_node = fn_def_node->codegen_node;
|
|
|
|
assert(codegen_node);
|
2015-11-30 08:28:28 +08:00
|
|
|
|
|
|
|
FnDefNode *codegen_fn_def = &codegen_node->data.fn_def_node;
|
2015-12-01 17:08:58 +08:00
|
|
|
assert(codegen_fn_def);
|
2015-12-08 13:11:04 +08:00
|
|
|
|
|
|
|
codegen_fn_def->block_context->di_scope = LLVMZigSubprogramToScope(subprogram);
|
|
|
|
|
2015-12-04 05:29:19 +08:00
|
|
|
int non_void_param_count = count_non_void_params(g, &fn_proto->params);
|
|
|
|
assert(non_void_param_count == (int)LLVMCountParams(fn));
|
|
|
|
LLVMValueRef *params = allocate<LLVMValueRef>(non_void_param_count);
|
2015-12-04 01:56:17 +08:00
|
|
|
LLVMGetParams(fn, params);
|
|
|
|
|
2015-12-04 05:29:19 +08:00
|
|
|
int non_void_index = 0;
|
2015-12-13 13:55:29 +08:00
|
|
|
for (int param_i = 0; param_i < fn_proto->params.length; param_i += 1) {
|
|
|
|
AstNode *param_decl = fn_proto->params.at(param_i);
|
2015-12-04 01:56:17 +08:00
|
|
|
assert(param_decl->type == NodeTypeParamDecl);
|
2015-12-04 05:29:19 +08:00
|
|
|
if (is_param_decl_type_void(g, param_decl))
|
|
|
|
continue;
|
2015-12-15 14:10:18 +08:00
|
|
|
VariableTableEntry *parameter_variable = fn_def_node->codegen_node->data.fn_def_node.block_context->variable_table.get(¶m_decl->data.param_decl.name);
|
2015-12-04 05:29:19 +08:00
|
|
|
parameter_variable->value_ref = params[non_void_index];
|
|
|
|
non_void_index += 1;
|
2015-12-04 01:56:17 +08:00
|
|
|
}
|
2015-11-30 08:28:28 +08:00
|
|
|
|
2016-01-03 11:42:32 +08:00
|
|
|
AstNode *body_node = fn_def_node->data.fn_def.body;
|
|
|
|
build_label_blocks(g, body_node);
|
2015-12-03 15:47:35 +08:00
|
|
|
|
2015-12-08 13:11:04 +08:00
|
|
|
// Set up debug info for blocks and variables and
|
2015-12-07 14:09:46 +08:00
|
|
|
// allocate all local variables
|
2015-12-15 13:01:39 +08:00
|
|
|
for (int bc_i = 0; bc_i < fn_table_entry->all_block_contexts.length; bc_i += 1) {
|
|
|
|
BlockContext *block_context = fn_table_entry->all_block_contexts.at(bc_i);
|
2015-12-07 14:09:46 +08:00
|
|
|
|
2015-12-15 14:10:18 +08:00
|
|
|
if (!block_context->di_scope) {
|
2015-12-08 13:11:04 +08:00
|
|
|
LLVMZigDILexicalBlock *di_block = LLVMZigCreateLexicalBlock(g->dbuilder,
|
|
|
|
block_context->parent->di_scope,
|
|
|
|
import->di_file,
|
|
|
|
block_context->node->line + 1,
|
|
|
|
block_context->node->column + 1);
|
|
|
|
block_context->di_scope = LLVMZigLexicalBlockToScope(di_block);
|
2015-12-08 11:57:45 +08:00
|
|
|
}
|
|
|
|
|
2015-12-08 13:11:04 +08:00
|
|
|
g->cur_block_context = block_context;
|
|
|
|
|
2015-12-07 14:09:46 +08:00
|
|
|
auto it = block_context->variable_table.entry_iterator();
|
|
|
|
for (;;) {
|
|
|
|
auto *entry = it.next();
|
|
|
|
if (!entry)
|
|
|
|
break;
|
|
|
|
|
2015-12-15 14:10:18 +08:00
|
|
|
VariableTableEntry *var = entry->value;
|
2015-12-14 02:59:34 +08:00
|
|
|
if (var->type->id == TypeTableEntryIdVoid)
|
2015-12-08 11:57:45 +08:00
|
|
|
continue;
|
|
|
|
|
2015-12-08 13:11:04 +08:00
|
|
|
unsigned tag;
|
|
|
|
unsigned arg_no;
|
|
|
|
if (block_context->node->type == NodeTypeFnDef) {
|
|
|
|
tag = LLVMZigTag_DW_arg_variable();
|
|
|
|
arg_no = var->arg_index + 1;
|
|
|
|
} else {
|
|
|
|
tag = LLVMZigTag_DW_auto_variable();
|
|
|
|
arg_no = 0;
|
2015-12-08 14:40:58 +08:00
|
|
|
|
|
|
|
add_debug_source_node(g, var->decl_node);
|
|
|
|
var->value_ref = LLVMBuildAlloca(g->builder, var->type->type_ref, buf_ptr(&var->name));
|
2016-01-03 12:56:33 +08:00
|
|
|
LLVMSetAlignment(var->value_ref, var->type->align_in_bits / 8);
|
2015-12-08 13:11:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var->di_loc_var = LLVMZigCreateLocalVariable(g->dbuilder, tag,
|
|
|
|
block_context->di_scope, buf_ptr(&var->name),
|
|
|
|
import->di_file, var->decl_node->line + 1,
|
|
|
|
var->type->di_type, !g->strip_debug_symbols, 0, arg_no);
|
2015-12-07 14:09:46 +08:00
|
|
|
}
|
2015-12-13 13:55:29 +08:00
|
|
|
|
|
|
|
// allocate structs which are the result of casts
|
|
|
|
for (int cea_i = 0; cea_i < block_context->cast_expr_alloca_list.length; cea_i += 1) {
|
2015-12-16 08:29:44 +08:00
|
|
|
CastNode *cast_node = block_context->cast_expr_alloca_list.at(cea_i);
|
|
|
|
add_debug_source_node(g, cast_node->source_node);
|
2016-01-02 15:06:06 +08:00
|
|
|
cast_node->ptr = LLVMBuildAlloca(g->builder, cast_node->after_type->type_ref, "");
|
2015-12-13 13:55:29 +08:00
|
|
|
}
|
2015-12-24 15:00:23 +08:00
|
|
|
|
|
|
|
// allocate structs which are struct value expressions
|
|
|
|
for (int alloca_i = 0; alloca_i < block_context->struct_val_expr_alloca_list.length; alloca_i += 1) {
|
|
|
|
StructValExprNode *struct_val_expr_node = block_context->struct_val_expr_alloca_list.at(alloca_i);
|
|
|
|
add_debug_source_node(g, struct_val_expr_node->source_node);
|
|
|
|
struct_val_expr_node->ptr = LLVMBuildAlloca(g->builder,
|
|
|
|
struct_val_expr_node->type_entry->type_ref, "");
|
|
|
|
}
|
2015-12-07 14:09:46 +08:00
|
|
|
}
|
|
|
|
|
2016-01-03 11:42:32 +08:00
|
|
|
// create debug variable declarations for parameters
|
|
|
|
for (int param_i = 0; param_i < fn_proto->params.length; param_i += 1) {
|
|
|
|
AstNode *param_decl = fn_proto->params.at(param_i);
|
|
|
|
assert(param_decl->type == NodeTypeParamDecl);
|
|
|
|
|
|
|
|
if (is_param_decl_type_void(g, param_decl))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
VariableTableEntry *variable = param_decl->codegen_node->data.param_decl_node.variable;
|
|
|
|
|
|
|
|
LLVMZigDILocation *debug_loc = LLVMZigGetDebugLoc(param_decl->line + 1, param_decl->column + 1,
|
|
|
|
codegen_fn_def->block_context->di_scope);
|
|
|
|
LLVMZigInsertDeclareAtEnd(g->dbuilder, variable->value_ref, variable->di_loc_var, debug_loc,
|
|
|
|
entry_block);
|
|
|
|
}
|
|
|
|
|
2015-12-02 06:54:46 +08:00
|
|
|
TypeTableEntry *implicit_return_type = codegen_fn_def->implicit_return_type;
|
2015-12-02 12:19:38 +08:00
|
|
|
gen_block(g, fn_def_node->data.fn_def.body, implicit_return_type);
|
2015-11-26 09:17:35 +08:00
|
|
|
|
2015-11-24 15:35:23 +08:00
|
|
|
}
|
2015-11-26 09:17:35 +08:00
|
|
|
assert(!g->errors.length);
|
2015-11-24 13:47:25 +08:00
|
|
|
|
2015-12-01 05:10:07 +08:00
|
|
|
LLVMZigDIBuilderFinalize(g->dbuilder);
|
2015-11-25 10:07:33 +08:00
|
|
|
|
2015-12-01 10:58:53 +08:00
|
|
|
if (g->verbose) {
|
|
|
|
LLVMDumpModule(g->module);
|
|
|
|
}
|
2015-11-24 18:06:10 +08:00
|
|
|
|
2015-11-28 06:46:06 +08:00
|
|
|
// in release mode, we're sooooo confident that we've generated correct ir,
|
|
|
|
// that we skip the verify module step in order to get better performance.
|
|
|
|
#ifndef NDEBUG
|
2015-11-24 18:06:10 +08:00
|
|
|
char *error = nullptr;
|
2015-11-28 06:46:06 +08:00
|
|
|
LLVMVerifyModule(g->module, LLVMAbortProcessAction, &error);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-12-14 17:46:37 +08:00
|
|
|
static const NumLit num_lit_kinds[] = {
|
|
|
|
NumLitF32,
|
|
|
|
NumLitF64,
|
|
|
|
NumLitF128,
|
|
|
|
NumLitU8,
|
|
|
|
NumLitU16,
|
|
|
|
NumLitU32,
|
|
|
|
NumLitU64,
|
|
|
|
};
|
|
|
|
|
2015-12-12 15:10:37 +08:00
|
|
|
static void define_builtin_types(CodeGen *g) {
|
2015-12-01 13:12:21 +08:00
|
|
|
{
|
|
|
|
// if this type is anywhere in the AST, we should never hit codegen.
|
2015-12-09 05:15:34 +08:00
|
|
|
TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdInvalid);
|
2015-12-01 13:12:21 +08:00
|
|
|
buf_init_from_str(&entry->name, "(invalid)");
|
|
|
|
g->builtin_types.entry_invalid = entry;
|
|
|
|
}
|
2015-12-14 17:46:37 +08:00
|
|
|
|
|
|
|
assert(NumLitCount == array_length(num_lit_kinds));
|
|
|
|
for (int i = 0; i < NumLitCount; i += 1) {
|
|
|
|
NumLit num_lit_kind = num_lit_kinds[i];
|
|
|
|
// This type should just create a constant with whatever actual number
|
|
|
|
// type is expected at the time.
|
|
|
|
TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdNumberLiteral);
|
|
|
|
buf_resize(&entry->name, 0);
|
|
|
|
buf_appendf(&entry->name, "(%s literal)", num_lit_str(num_lit_kind));
|
|
|
|
entry->data.num_lit.kind = num_lit_kind;
|
|
|
|
g->num_lit_types[i] = entry;
|
|
|
|
}
|
|
|
|
|
2015-12-02 12:19:38 +08:00
|
|
|
{
|
2015-12-09 05:15:34 +08:00
|
|
|
TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdBool);
|
2015-12-02 12:19:38 +08:00
|
|
|
entry->type_ref = LLVMInt1Type();
|
|
|
|
buf_init_from_str(&entry->name, "bool");
|
2015-12-23 18:19:22 +08:00
|
|
|
entry->size_in_bits = 8;
|
2015-12-08 11:07:46 +08:00
|
|
|
entry->align_in_bits = 8;
|
|
|
|
entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
|
|
|
|
entry->size_in_bits, entry->align_in_bits,
|
2015-12-02 12:19:38 +08:00
|
|
|
LLVMZigEncoding_DW_ATE_unsigned());
|
|
|
|
g->builtin_types.entry_bool = entry;
|
|
|
|
}
|
2015-12-01 10:58:53 +08:00
|
|
|
{
|
2015-12-09 05:15:34 +08:00
|
|
|
TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdInt);
|
2015-12-01 10:58:53 +08:00
|
|
|
entry->type_ref = LLVMInt8Type();
|
|
|
|
buf_init_from_str(&entry->name, "u8");
|
2015-12-08 11:07:46 +08:00
|
|
|
entry->size_in_bits = 8;
|
|
|
|
entry->align_in_bits = 8;
|
|
|
|
entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
|
|
|
|
entry->size_in_bits, entry->align_in_bits,
|
2015-12-01 10:58:53 +08:00
|
|
|
LLVMZigEncoding_DW_ATE_unsigned());
|
|
|
|
g->builtin_types.entry_u8 = entry;
|
|
|
|
}
|
2016-01-04 09:17:50 +08:00
|
|
|
{
|
|
|
|
TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdInt);
|
|
|
|
entry->type_ref = LLVMInt16Type();
|
|
|
|
buf_init_from_str(&entry->name, "u16");
|
|
|
|
entry->size_in_bits = 16;
|
|
|
|
entry->align_in_bits = 16;
|
|
|
|
entry->data.integral.is_signed = false;
|
|
|
|
entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
|
|
|
|
entry->size_in_bits, entry->align_in_bits,
|
|
|
|
LLVMZigEncoding_DW_ATE_unsigned());
|
|
|
|
g->builtin_types.entry_u16 = entry;
|
|
|
|
}
|
2015-12-16 09:21:59 +08:00
|
|
|
{
|
|
|
|
TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdInt);
|
|
|
|
entry->type_ref = LLVMInt32Type();
|
|
|
|
buf_init_from_str(&entry->name, "u32");
|
|
|
|
entry->size_in_bits = 32;
|
|
|
|
entry->align_in_bits = 32;
|
|
|
|
entry->data.integral.is_signed = false;
|
|
|
|
entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
|
|
|
|
entry->size_in_bits, entry->align_in_bits,
|
|
|
|
LLVMZigEncoding_DW_ATE_unsigned());
|
|
|
|
g->builtin_types.entry_u32 = entry;
|
|
|
|
}
|
2015-12-14 17:46:37 +08:00
|
|
|
{
|
|
|
|
TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdInt);
|
|
|
|
entry->type_ref = LLVMInt64Type();
|
|
|
|
buf_init_from_str(&entry->name, "u64");
|
|
|
|
entry->size_in_bits = 64;
|
|
|
|
entry->align_in_bits = 64;
|
|
|
|
entry->data.integral.is_signed = false;
|
|
|
|
entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
|
|
|
|
entry->size_in_bits, entry->align_in_bits,
|
|
|
|
LLVMZigEncoding_DW_ATE_unsigned());
|
|
|
|
g->builtin_types.entry_u64 = entry;
|
|
|
|
}
|
2015-12-12 15:10:37 +08:00
|
|
|
g->builtin_types.entry_c_string_literal = get_pointer_to_type(g, g->builtin_types.entry_u8, true);
|
2015-12-16 08:11:44 +08:00
|
|
|
{
|
|
|
|
TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdInt);
|
|
|
|
entry->type_ref = LLVMInt8Type();
|
|
|
|
buf_init_from_str(&entry->name, "i8");
|
|
|
|
entry->size_in_bits = 8;
|
|
|
|
entry->align_in_bits = 8;
|
|
|
|
entry->data.integral.is_signed = true;
|
|
|
|
entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
|
|
|
|
entry->size_in_bits, entry->align_in_bits,
|
|
|
|
LLVMZigEncoding_DW_ATE_signed());
|
|
|
|
g->builtin_types.entry_i8 = entry;
|
|
|
|
}
|
2016-01-04 09:17:50 +08:00
|
|
|
{
|
|
|
|
TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdInt);
|
|
|
|
entry->type_ref = LLVMInt16Type();
|
|
|
|
buf_init_from_str(&entry->name, "i16");
|
|
|
|
entry->size_in_bits = 16;
|
|
|
|
entry->align_in_bits = 16;
|
|
|
|
entry->data.integral.is_signed = true;
|
|
|
|
entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
|
|
|
|
entry->size_in_bits, entry->align_in_bits,
|
|
|
|
LLVMZigEncoding_DW_ATE_signed());
|
|
|
|
g->builtin_types.entry_i16 = entry;
|
|
|
|
}
|
2015-12-01 10:58:53 +08:00
|
|
|
{
|
2015-12-09 05:15:34 +08:00
|
|
|
TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdInt);
|
2015-12-01 10:58:53 +08:00
|
|
|
entry->type_ref = LLVMInt32Type();
|
|
|
|
buf_init_from_str(&entry->name, "i32");
|
2015-12-08 11:07:46 +08:00
|
|
|
entry->size_in_bits = 32;
|
|
|
|
entry->align_in_bits = 32;
|
2015-12-09 05:15:34 +08:00
|
|
|
entry->data.integral.is_signed = true;
|
2015-12-08 11:07:46 +08:00
|
|
|
entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
|
|
|
|
entry->size_in_bits, entry->align_in_bits,
|
2015-12-01 10:58:53 +08:00
|
|
|
LLVMZigEncoding_DW_ATE_signed());
|
|
|
|
g->builtin_types.entry_i32 = entry;
|
|
|
|
}
|
2015-12-14 17:46:37 +08:00
|
|
|
{
|
|
|
|
TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdInt);
|
|
|
|
entry->type_ref = LLVMInt64Type();
|
|
|
|
buf_init_from_str(&entry->name, "i64");
|
|
|
|
entry->size_in_bits = 64;
|
|
|
|
entry->align_in_bits = 64;
|
|
|
|
entry->data.integral.is_signed = true;
|
|
|
|
entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
|
|
|
|
entry->size_in_bits, entry->align_in_bits,
|
|
|
|
LLVMZigEncoding_DW_ATE_signed());
|
|
|
|
g->builtin_types.entry_i64 = entry;
|
|
|
|
}
|
2015-12-11 06:34:38 +08:00
|
|
|
{
|
|
|
|
TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdInt);
|
|
|
|
entry->type_ref = LLVMIntType(g->pointer_size_bytes * 8);
|
|
|
|
buf_init_from_str(&entry->name, "isize");
|
|
|
|
entry->size_in_bits = g->pointer_size_bytes * 8;
|
|
|
|
entry->align_in_bits = g->pointer_size_bytes * 8;
|
|
|
|
entry->data.integral.is_signed = true;
|
|
|
|
entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
|
|
|
|
entry->size_in_bits, entry->align_in_bits,
|
|
|
|
LLVMZigEncoding_DW_ATE_signed());
|
|
|
|
g->builtin_types.entry_isize = entry;
|
|
|
|
}
|
2015-12-12 15:10:37 +08:00
|
|
|
{
|
|
|
|
TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdInt);
|
|
|
|
entry->type_ref = LLVMIntType(g->pointer_size_bytes * 8);
|
|
|
|
buf_init_from_str(&entry->name, "usize");
|
|
|
|
entry->size_in_bits = g->pointer_size_bytes * 8;
|
|
|
|
entry->align_in_bits = g->pointer_size_bytes * 8;
|
|
|
|
entry->data.integral.is_signed = false;
|
|
|
|
entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
|
|
|
|
entry->size_in_bits, entry->align_in_bits,
|
|
|
|
LLVMZigEncoding_DW_ATE_unsigned());
|
|
|
|
g->builtin_types.entry_usize = entry;
|
|
|
|
}
|
2015-12-09 03:25:30 +08:00
|
|
|
{
|
2015-12-09 05:15:34 +08:00
|
|
|
TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdFloat);
|
2015-12-09 03:25:30 +08:00
|
|
|
entry->type_ref = LLVMFloatType();
|
|
|
|
buf_init_from_str(&entry->name, "f32");
|
|
|
|
entry->size_in_bits = 32;
|
|
|
|
entry->align_in_bits = 32;
|
|
|
|
entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
|
|
|
|
entry->size_in_bits, entry->align_in_bits,
|
|
|
|
LLVMZigEncoding_DW_ATE_float());
|
|
|
|
g->builtin_types.entry_f32 = entry;
|
|
|
|
}
|
2015-12-14 17:46:37 +08:00
|
|
|
{
|
|
|
|
TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdFloat);
|
2015-12-15 19:05:43 +08:00
|
|
|
entry->type_ref = LLVMDoubleType();
|
2015-12-14 17:46:37 +08:00
|
|
|
buf_init_from_str(&entry->name, "f64");
|
|
|
|
entry->size_in_bits = 64;
|
|
|
|
entry->align_in_bits = 64;
|
|
|
|
entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
|
|
|
|
entry->size_in_bits, entry->align_in_bits,
|
|
|
|
LLVMZigEncoding_DW_ATE_float());
|
|
|
|
g->builtin_types.entry_f64 = entry;
|
|
|
|
}
|
2015-12-01 10:58:53 +08:00
|
|
|
{
|
2015-12-09 05:15:34 +08:00
|
|
|
TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdVoid);
|
2015-12-01 10:58:53 +08:00
|
|
|
entry->type_ref = LLVMVoidType();
|
|
|
|
buf_init_from_str(&entry->name, "void");
|
2015-12-08 11:07:46 +08:00
|
|
|
entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
|
|
|
|
entry->size_in_bits, entry->align_in_bits,
|
2015-12-01 10:58:53 +08:00
|
|
|
LLVMZigEncoding_DW_ATE_unsigned());
|
|
|
|
g->builtin_types.entry_void = entry;
|
|
|
|
}
|
|
|
|
{
|
2015-12-09 05:15:34 +08:00
|
|
|
TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdUnreachable);
|
2015-12-01 10:58:53 +08:00
|
|
|
entry->type_ref = LLVMVoidType();
|
|
|
|
buf_init_from_str(&entry->name, "unreachable");
|
2015-12-01 13:12:21 +08:00
|
|
|
entry->di_type = g->builtin_types.entry_void->di_type;
|
2015-12-01 10:58:53 +08:00
|
|
|
g->builtin_types.entry_unreachable = entry;
|
|
|
|
}
|
2015-12-12 15:10:37 +08:00
|
|
|
{
|
|
|
|
TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdStruct);
|
|
|
|
|
|
|
|
TypeTableEntry *const_pointer_to_u8 = get_pointer_to_type(g, g->builtin_types.entry_u8, true);
|
|
|
|
|
|
|
|
unsigned element_count = 2;
|
|
|
|
LLVMTypeRef element_types[] = {
|
|
|
|
const_pointer_to_u8->type_ref,
|
|
|
|
g->builtin_types.entry_usize->type_ref
|
|
|
|
};
|
|
|
|
entry->type_ref = LLVMStructCreateNamed(LLVMGetGlobalContext(), "string");
|
|
|
|
LLVMStructSetBody(entry->type_ref, element_types, element_count, false);
|
|
|
|
|
|
|
|
buf_init_from_str(&entry->name, "string");
|
|
|
|
entry->size_in_bits = g->pointer_size_bytes * 2 * 8;
|
|
|
|
entry->align_in_bits = g->pointer_size_bytes;
|
|
|
|
entry->data.structure.is_packed = false;
|
|
|
|
entry->data.structure.field_count = element_count;
|
|
|
|
entry->data.structure.fields = allocate<TypeStructField>(element_count);
|
|
|
|
entry->data.structure.fields[0].name = buf_create_from_str("ptr");
|
|
|
|
entry->data.structure.fields[0].type_entry = const_pointer_to_u8;
|
|
|
|
entry->data.structure.fields[1].name = buf_create_from_str("len");
|
|
|
|
entry->data.structure.fields[1].type_entry = g->builtin_types.entry_usize;
|
|
|
|
|
|
|
|
LLVMZigDIType *di_element_types[] = {
|
|
|
|
const_pointer_to_u8->di_type,
|
|
|
|
g->builtin_types.entry_usize->di_type
|
|
|
|
};
|
|
|
|
LLVMZigDIScope *compile_unit_scope = LLVMZigCompileUnitToScope(g->compile_unit);
|
|
|
|
entry->di_type = LLVMZigCreateDebugStructType(g->dbuilder, compile_unit_scope,
|
2015-12-14 05:33:52 +08:00
|
|
|
"string", g->dummy_di_file, 0, entry->size_in_bits, entry->align_in_bits, 0,
|
2015-12-12 15:10:37 +08:00
|
|
|
nullptr, di_element_types, element_count, 0, nullptr, "");
|
|
|
|
|
|
|
|
g->builtin_types.entry_string = entry;
|
|
|
|
}
|
2015-11-24 13:47:25 +08:00
|
|
|
}
|
|
|
|
|
2015-12-01 10:58:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
static void init(CodeGen *g, Buf *source_path) {
|
2015-12-11 08:42:47 +08:00
|
|
|
g->lib_search_paths.append(g->root_source_dir);
|
|
|
|
g->lib_search_paths.append(buf_create_from_str(ZIG_STD_DIR));
|
|
|
|
|
2015-12-01 10:58:53 +08:00
|
|
|
LLVMInitializeAllTargets();
|
|
|
|
LLVMInitializeAllTargetMCs();
|
|
|
|
LLVMInitializeAllAsmPrinters();
|
|
|
|
LLVMInitializeAllAsmParsers();
|
|
|
|
LLVMInitializeNativeTarget();
|
|
|
|
|
|
|
|
g->is_native_target = true;
|
|
|
|
char *native_triple = LLVMGetDefaultTargetTriple();
|
|
|
|
|
2015-12-08 15:27:50 +08:00
|
|
|
g->module = LLVMModuleCreateWithName(buf_ptr(source_path));
|
|
|
|
|
|
|
|
LLVMSetTarget(g->module, native_triple);
|
|
|
|
|
2015-12-01 10:58:53 +08:00
|
|
|
LLVMTargetRef target_ref;
|
|
|
|
char *err_msg = nullptr;
|
|
|
|
if (LLVMGetTargetFromTriple(native_triple, &target_ref, &err_msg)) {
|
|
|
|
zig_panic("unable to get target from triple: %s", err_msg);
|
|
|
|
}
|
|
|
|
|
2015-12-08 15:27:50 +08:00
|
|
|
|
2015-12-01 10:58:53 +08:00
|
|
|
char *native_cpu = LLVMZigGetHostCPUName();
|
|
|
|
char *native_features = LLVMZigGetNativeFeatures();
|
|
|
|
|
|
|
|
LLVMCodeGenOptLevel opt_level = (g->build_type == CodeGenBuildTypeDebug) ?
|
|
|
|
LLVMCodeGenLevelNone : LLVMCodeGenLevelAggressive;
|
|
|
|
|
|
|
|
LLVMRelocMode reloc_mode = g->is_static ? LLVMRelocStatic : LLVMRelocPIC;
|
|
|
|
|
|
|
|
g->target_machine = LLVMCreateTargetMachine(target_ref, native_triple,
|
|
|
|
native_cpu, native_features, opt_level, reloc_mode, LLVMCodeModelDefault);
|
|
|
|
|
|
|
|
g->target_data_ref = LLVMGetTargetMachineData(g->target_machine);
|
|
|
|
|
2015-12-08 15:27:50 +08:00
|
|
|
char *layout_str = LLVMCopyStringRepOfTargetData(g->target_data_ref);
|
|
|
|
LLVMSetDataLayout(g->module, layout_str);
|
2015-12-01 10:58:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
g->pointer_size_bytes = LLVMPointerSize(g->target_data_ref);
|
|
|
|
|
|
|
|
g->builder = LLVMCreateBuilder();
|
|
|
|
g->dbuilder = LLVMZigCreateDIBuilder(g->module, true);
|
|
|
|
|
2015-12-09 03:25:30 +08:00
|
|
|
LLVMZigSetFastMath(g->builder, true);
|
|
|
|
|
2015-12-01 10:58:53 +08:00
|
|
|
|
|
|
|
Buf *producer = buf_sprintf("zig %s", ZIG_VERSION_STRING);
|
|
|
|
bool is_optimized = g->build_type == CodeGenBuildTypeRelease;
|
|
|
|
const char *flags = "";
|
|
|
|
unsigned runtime_version = 0;
|
|
|
|
g->compile_unit = LLVMZigCreateCompileUnit(g->dbuilder, LLVMZigLang_DW_LANG_C99(),
|
|
|
|
buf_ptr(source_path), buf_ptr(g->root_source_dir),
|
|
|
|
buf_ptr(producer), is_optimized, flags, runtime_version,
|
|
|
|
"", 0, !g->strip_debug_symbols);
|
|
|
|
|
2015-12-14 05:33:52 +08:00
|
|
|
// This is for debug stuff that doesn't have a real file.
|
|
|
|
g->dummy_di_file = nullptr; //LLVMZigCreateFile(g->dbuilder, "", "");
|
|
|
|
|
2015-12-12 15:10:37 +08:00
|
|
|
define_builtin_types(g);
|
2015-12-01 10:58:53 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-12-16 03:44:42 +08:00
|
|
|
static bool directives_contains_link_libc(ZigList<AstNode*> *directives) {
|
|
|
|
for (int i = 0; i < directives->length; i += 1) {
|
|
|
|
AstNode *directive_node = directives->at(i);
|
|
|
|
if (buf_eql_str(&directive_node->data.directive.name, "link") &&
|
|
|
|
buf_eql_str(&directive_node->data.directive.param, "c"))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-16 12:48:41 +08:00
|
|
|
static ImportTableEntry *codegen_add_code(CodeGen *g, Buf *abs_full_path,
|
|
|
|
Buf *src_dirname, Buf *src_basename, Buf *source_code)
|
|
|
|
{
|
2015-12-01 17:29:21 +08:00
|
|
|
int err;
|
2015-12-14 05:33:52 +08:00
|
|
|
Buf *full_path = buf_alloc();
|
|
|
|
os_path_join(src_dirname, src_basename, full_path);
|
2015-12-01 10:58:53 +08:00
|
|
|
|
|
|
|
if (g->verbose) {
|
2015-12-14 05:33:52 +08:00
|
|
|
fprintf(stderr, "\nOriginal Source (%s):\n", buf_ptr(full_path));
|
2015-12-01 10:58:53 +08:00
|
|
|
fprintf(stderr, "----------------\n");
|
|
|
|
fprintf(stderr, "%s\n", buf_ptr(source_code));
|
|
|
|
|
|
|
|
fprintf(stderr, "\nTokens:\n");
|
|
|
|
fprintf(stderr, "---------\n");
|
|
|
|
}
|
|
|
|
|
2015-12-01 15:50:11 +08:00
|
|
|
Tokenization tokenization = {0};
|
|
|
|
tokenize(source_code, &tokenization);
|
|
|
|
|
|
|
|
if (tokenization.err) {
|
|
|
|
ErrorMsg *err = allocate<ErrorMsg>(1);
|
|
|
|
err->line_start = tokenization.err_line;
|
|
|
|
err->column_start = tokenization.err_column;
|
|
|
|
err->line_end = -1;
|
|
|
|
err->column_end = -1;
|
|
|
|
err->msg = tokenization.err;
|
2015-12-14 05:33:52 +08:00
|
|
|
err->path = full_path;
|
2015-12-01 15:50:11 +08:00
|
|
|
err->source = source_code;
|
|
|
|
err->line_offsets = tokenization.line_offsets;
|
|
|
|
|
2015-12-01 16:06:10 +08:00
|
|
|
print_err_msg(err, g->err_color);
|
2015-12-01 15:50:11 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
2015-12-01 10:58:53 +08:00
|
|
|
|
|
|
|
if (g->verbose) {
|
2015-12-01 15:50:11 +08:00
|
|
|
print_tokens(source_code, tokenization.tokens);
|
2015-12-01 10:58:53 +08:00
|
|
|
|
|
|
|
fprintf(stderr, "\nAST:\n");
|
|
|
|
fprintf(stderr, "------\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
ImportTableEntry *import_entry = allocate<ImportTableEntry>(1);
|
2015-12-01 15:50:11 +08:00
|
|
|
import_entry->source_code = source_code;
|
|
|
|
import_entry->line_offsets = tokenization.line_offsets;
|
2015-12-14 05:33:52 +08:00
|
|
|
import_entry->path = full_path;
|
2015-12-01 13:53:37 +08:00
|
|
|
import_entry->fn_table.init(32);
|
2016-01-04 18:52:37 +08:00
|
|
|
import_entry->type_table.init(32);
|
|
|
|
import_entry->type_table.put(&g->builtin_types.entry_bool->name, g->builtin_types.entry_bool);
|
|
|
|
import_entry->type_table.put(&g->builtin_types.entry_u8->name, g->builtin_types.entry_u8);
|
|
|
|
import_entry->type_table.put(&g->builtin_types.entry_u16->name, g->builtin_types.entry_u16);
|
|
|
|
import_entry->type_table.put(&g->builtin_types.entry_u32->name, g->builtin_types.entry_u32);
|
|
|
|
import_entry->type_table.put(&g->builtin_types.entry_u64->name, g->builtin_types.entry_u64);
|
|
|
|
import_entry->type_table.put(&g->builtin_types.entry_i8->name, g->builtin_types.entry_i8);
|
|
|
|
import_entry->type_table.put(&g->builtin_types.entry_i16->name, g->builtin_types.entry_i16);
|
|
|
|
import_entry->type_table.put(&g->builtin_types.entry_i32->name, g->builtin_types.entry_i32);
|
|
|
|
import_entry->type_table.put(&g->builtin_types.entry_i64->name, g->builtin_types.entry_i64);
|
|
|
|
import_entry->type_table.put(&g->builtin_types.entry_isize->name, g->builtin_types.entry_isize);
|
|
|
|
import_entry->type_table.put(&g->builtin_types.entry_usize->name, g->builtin_types.entry_usize);
|
|
|
|
import_entry->type_table.put(&g->builtin_types.entry_f32->name, g->builtin_types.entry_f32);
|
|
|
|
import_entry->type_table.put(&g->builtin_types.entry_f64->name, g->builtin_types.entry_f64);
|
|
|
|
import_entry->type_table.put(&g->builtin_types.entry_void->name, g->builtin_types.entry_void);
|
|
|
|
import_entry->type_table.put(&g->builtin_types.entry_unreachable->name, g->builtin_types.entry_unreachable);
|
|
|
|
import_entry->type_table.put(&g->builtin_types.entry_string->name, g->builtin_types.entry_string);
|
|
|
|
|
2015-12-01 16:06:10 +08:00
|
|
|
import_entry->root = ast_parse(source_code, tokenization.tokens, import_entry, g->err_color);
|
2015-12-01 10:58:53 +08:00
|
|
|
assert(import_entry->root);
|
|
|
|
if (g->verbose) {
|
|
|
|
ast_print(import_entry->root, 0);
|
|
|
|
}
|
|
|
|
|
2015-12-14 05:33:52 +08:00
|
|
|
import_entry->di_file = LLVMZigCreateFile(g->dbuilder, buf_ptr(src_basename), buf_ptr(src_dirname));
|
2015-12-16 12:48:41 +08:00
|
|
|
g->import_table.put(abs_full_path, import_entry);
|
2015-12-01 10:58:53 +08:00
|
|
|
|
2015-12-15 14:10:18 +08:00
|
|
|
import_entry->block_context = new_block_context(import_entry->root, nullptr);
|
2015-12-15 13:01:39 +08:00
|
|
|
import_entry->block_context->di_scope = LLVMZigFileToScope(import_entry->di_file);
|
|
|
|
|
2015-12-01 13:53:37 +08:00
|
|
|
|
|
|
|
assert(import_entry->root->type == NodeTypeRoot);
|
|
|
|
for (int decl_i = 0; decl_i < import_entry->root->data.root.top_level_decls.length; decl_i += 1) {
|
|
|
|
AstNode *top_level_decl = import_entry->root->data.root.top_level_decls.at(decl_i);
|
|
|
|
|
2015-12-11 06:34:38 +08:00
|
|
|
if (top_level_decl->type == NodeTypeUse) {
|
2015-12-11 08:42:47 +08:00
|
|
|
Buf *import_target_path = &top_level_decl->data.use.path;
|
2015-12-16 12:48:41 +08:00
|
|
|
Buf full_path = BUF_INIT;
|
|
|
|
Buf *import_code = buf_alloc();
|
|
|
|
bool found_it = false;
|
|
|
|
|
2016-01-04 18:31:57 +08:00
|
|
|
alloc_codegen_node(top_level_decl);
|
|
|
|
|
2015-12-16 12:48:41 +08:00
|
|
|
for (int path_i = 0; path_i < g->lib_search_paths.length; path_i += 1) {
|
|
|
|
Buf *search_path = g->lib_search_paths.at(path_i);
|
|
|
|
os_path_join(search_path, import_target_path, &full_path);
|
|
|
|
|
|
|
|
Buf *abs_full_path = buf_alloc();
|
|
|
|
if ((err = os_path_real(&full_path, abs_full_path))) {
|
|
|
|
if (err == ErrorFileNotFound) {
|
|
|
|
continue;
|
|
|
|
} else {
|
2016-01-04 18:31:57 +08:00
|
|
|
g->error_during_imports = true;
|
2015-12-16 12:48:41 +08:00
|
|
|
add_node_error(g, top_level_decl,
|
|
|
|
buf_sprintf("unable to open '%s': %s", buf_ptr(&full_path), err_str(err)));
|
|
|
|
goto done_looking_at_imports;
|
|
|
|
}
|
|
|
|
}
|
2015-12-11 08:42:47 +08:00
|
|
|
|
2015-12-16 12:48:41 +08:00
|
|
|
auto entry = g->import_table.maybe_get(abs_full_path);
|
|
|
|
if (entry) {
|
|
|
|
found_it = true;
|
2016-01-04 18:31:57 +08:00
|
|
|
top_level_decl->codegen_node->data.import_node.import = entry->value;
|
2015-12-16 12:48:41 +08:00
|
|
|
} else {
|
|
|
|
if ((err = os_fetch_file_path(abs_full_path, import_code))) {
|
2015-12-11 08:42:47 +08:00
|
|
|
if (err == ErrorFileNotFound) {
|
|
|
|
continue;
|
|
|
|
} else {
|
2016-01-04 18:31:57 +08:00
|
|
|
g->error_during_imports = true;
|
2015-12-11 08:42:47 +08:00
|
|
|
add_node_error(g, top_level_decl,
|
|
|
|
buf_sprintf("unable to open '%s': %s", buf_ptr(&full_path), err_str(err)));
|
|
|
|
goto done_looking_at_imports;
|
|
|
|
}
|
|
|
|
}
|
2016-01-04 18:31:57 +08:00
|
|
|
top_level_decl->codegen_node->data.import_node.import = codegen_add_code(g,
|
|
|
|
abs_full_path, search_path, &top_level_decl->data.use.path, import_code);
|
2015-12-11 08:42:47 +08:00
|
|
|
found_it = true;
|
|
|
|
}
|
2015-12-16 12:48:41 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!found_it) {
|
2016-01-04 18:31:57 +08:00
|
|
|
g->error_during_imports = true;
|
2015-12-16 12:48:41 +08:00
|
|
|
add_node_error(g, top_level_decl,
|
|
|
|
buf_sprintf("unable to find '%s'", buf_ptr(import_target_path)));
|
2015-12-11 06:34:38 +08:00
|
|
|
}
|
|
|
|
} else if (top_level_decl->type == NodeTypeFnDef) {
|
|
|
|
AstNode *proto_node = top_level_decl->data.fn_def.fn_proto;
|
|
|
|
assert(proto_node->type == NodeTypeFnProto);
|
|
|
|
Buf *proto_name = &proto_node->data.fn_proto.name;
|
|
|
|
|
2015-12-16 03:44:42 +08:00
|
|
|
bool is_private = (proto_node->data.fn_proto.visib_mod == FnProtoVisibModPrivate);
|
2015-12-11 06:34:38 +08:00
|
|
|
|
2015-12-16 03:44:42 +08:00
|
|
|
if (buf_eql_str(proto_name, "main") && !is_private) {
|
|
|
|
g->have_exported_main = true;
|
2015-12-01 17:29:21 +08:00
|
|
|
}
|
2015-12-16 03:44:42 +08:00
|
|
|
} else if (top_level_decl->type == NodeTypeExternBlock) {
|
|
|
|
g->link_libc = directives_contains_link_libc(top_level_decl->data.extern_block.directives);
|
2015-12-01 13:53:37 +08:00
|
|
|
}
|
|
|
|
}
|
2015-12-01 14:06:29 +08:00
|
|
|
|
2015-12-11 08:42:47 +08:00
|
|
|
done_looking_at_imports:
|
|
|
|
|
2015-12-01 14:06:29 +08:00
|
|
|
return import_entry;
|
2015-12-01 13:53:37 +08:00
|
|
|
}
|
|
|
|
|
2015-12-14 05:33:52 +08:00
|
|
|
void codegen_add_root_code(CodeGen *g, Buf *src_dir, Buf *src_basename, Buf *source_code) {
|
|
|
|
Buf source_path = BUF_INIT;
|
|
|
|
os_path_join(src_dir, src_basename, &source_path);
|
|
|
|
init(g, &source_path);
|
2015-12-01 13:53:37 +08:00
|
|
|
|
2015-12-16 12:48:41 +08:00
|
|
|
Buf *abs_full_path = buf_alloc();
|
|
|
|
int err;
|
|
|
|
if ((err = os_path_real(&source_path, abs_full_path))) {
|
|
|
|
zig_panic("unable to open '%s': %s", buf_ptr(&source_path), err_str(err));
|
|
|
|
}
|
|
|
|
|
|
|
|
g->root_import = codegen_add_code(g, abs_full_path, src_dir, src_basename, source_code);
|
2015-12-01 13:53:37 +08:00
|
|
|
|
2015-12-16 03:44:42 +08:00
|
|
|
if (g->have_exported_main && !g->link_libc && g->out_type != OutTypeLib) {
|
2015-12-14 05:33:52 +08:00
|
|
|
Buf *bootstrap_dir = buf_create_from_str(ZIG_STD_DIR);
|
|
|
|
Buf *bootstrap_basename = buf_create_from_str("bootstrap.zig");
|
|
|
|
Buf path_to_bootstrap_src = BUF_INIT;
|
|
|
|
os_path_join(bootstrap_dir, bootstrap_basename, &path_to_bootstrap_src);
|
2015-12-16 12:48:41 +08:00
|
|
|
Buf *abs_full_path = buf_alloc();
|
|
|
|
if ((err = os_path_real(&path_to_bootstrap_src, abs_full_path))) {
|
|
|
|
zig_panic("unable to open '%s': %s", buf_ptr(&path_to_bootstrap_src), err_str(err));
|
|
|
|
}
|
2015-12-11 06:34:38 +08:00
|
|
|
Buf *import_code = buf_alloc();
|
|
|
|
int err;
|
2015-12-16 12:48:41 +08:00
|
|
|
if ((err = os_fetch_file_path(abs_full_path, import_code))) {
|
2015-12-14 05:33:52 +08:00
|
|
|
zig_panic("unable to open '%s': %s", buf_ptr(&path_to_bootstrap_src), err_str(err));
|
2015-12-11 06:34:38 +08:00
|
|
|
}
|
|
|
|
|
2016-01-04 18:31:57 +08:00
|
|
|
g->bootstrap_import = codegen_add_code(g, abs_full_path, bootstrap_dir, bootstrap_basename, import_code);
|
2015-12-11 06:34:38 +08:00
|
|
|
}
|
|
|
|
|
2015-12-01 13:53:37 +08:00
|
|
|
if (g->verbose) {
|
|
|
|
fprintf(stderr, "\nSemantic Analysis:\n");
|
|
|
|
fprintf(stderr, "--------------------\n");
|
|
|
|
}
|
2016-01-04 18:31:57 +08:00
|
|
|
if (!g->error_during_imports) {
|
|
|
|
semantic_analyze(g);
|
|
|
|
}
|
2015-12-01 10:58:53 +08:00
|
|
|
|
|
|
|
if (g->errors.length == 0) {
|
|
|
|
if (g->verbose) {
|
|
|
|
fprintf(stderr, "OK\n");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < g->errors.length; i += 1) {
|
2015-12-01 15:50:11 +08:00
|
|
|
ErrorMsg *err = g->errors.at(i);
|
2015-12-01 16:06:10 +08:00
|
|
|
print_err_msg(err, g->err_color);
|
2015-12-01 10:58:53 +08:00
|
|
|
}
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g->verbose) {
|
|
|
|
fprintf(stderr, "\nCode Generation:\n");
|
|
|
|
fprintf(stderr, "------------------\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
do_code_gen(g);
|
2015-11-24 13:47:25 +08:00
|
|
|
}
|
2015-11-25 04:00:38 +08:00
|
|
|
|
2015-12-04 08:30:44 +08:00
|
|
|
static void to_c_type(CodeGen *g, AstNode *type_node, Buf *out_buf) {
|
2015-11-30 08:28:28 +08:00
|
|
|
assert(type_node->type == NodeTypeType);
|
|
|
|
assert(type_node->codegen_node);
|
2015-11-28 12:24:11 +08:00
|
|
|
|
2015-11-30 08:28:28 +08:00
|
|
|
TypeTableEntry *type_entry = type_node->codegen_node->data.type_node.entry;
|
|
|
|
assert(type_entry);
|
2015-11-28 12:24:11 +08:00
|
|
|
|
2015-12-01 06:36:58 +08:00
|
|
|
if (type_entry == g->builtin_types.entry_u8) {
|
|
|
|
g->c_stdint_used = true;
|
2015-12-04 08:30:44 +08:00
|
|
|
buf_init_from_str(out_buf, "uint8_t");
|
2015-12-01 06:36:58 +08:00
|
|
|
} else if (type_entry == g->builtin_types.entry_i32) {
|
|
|
|
g->c_stdint_used = true;
|
2015-12-04 08:30:44 +08:00
|
|
|
buf_init_from_str(out_buf, "int32_t");
|
2015-12-11 06:34:38 +08:00
|
|
|
} else if (type_entry == g->builtin_types.entry_isize) {
|
|
|
|
g->c_stdint_used = true;
|
|
|
|
buf_init_from_str(out_buf, "intptr_t");
|
2015-12-09 03:25:30 +08:00
|
|
|
} else if (type_entry == g->builtin_types.entry_f32) {
|
|
|
|
buf_init_from_str(out_buf, "float");
|
2015-12-04 08:06:04 +08:00
|
|
|
} else if (type_entry == g->builtin_types.entry_unreachable) {
|
2015-12-04 08:30:44 +08:00
|
|
|
buf_init_from_str(out_buf, "__attribute__((__noreturn__)) void");
|
|
|
|
} else if (type_entry == g->builtin_types.entry_bool) {
|
|
|
|
buf_init_from_str(out_buf, "unsigned char");
|
|
|
|
} else if (type_entry == g->builtin_types.entry_void) {
|
|
|
|
buf_init_from_str(out_buf, "void");
|
2015-12-01 06:36:58 +08:00
|
|
|
} else {
|
2015-12-02 12:19:38 +08:00
|
|
|
zig_panic("TODO to_c_type");
|
2015-11-30 08:28:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void generate_h_file(CodeGen *g) {
|
2015-12-01 10:58:53 +08:00
|
|
|
Buf *h_file_out_path = buf_sprintf("%s.h", buf_ptr(g->root_out_name));
|
2015-11-30 08:28:28 +08:00
|
|
|
FILE *out_h = fopen(buf_ptr(h_file_out_path), "wb");
|
|
|
|
if (!out_h)
|
|
|
|
zig_panic("unable to open %s: %s", buf_ptr(h_file_out_path), strerror(errno));
|
|
|
|
|
2015-12-01 10:58:53 +08:00
|
|
|
Buf *export_macro = buf_sprintf("%s_EXPORT", buf_ptr(g->root_out_name));
|
2015-11-30 08:28:28 +08:00
|
|
|
buf_upcase(export_macro);
|
|
|
|
|
2015-12-01 10:58:53 +08:00
|
|
|
Buf *extern_c_macro = buf_sprintf("%s_EXTERN_C", buf_ptr(g->root_out_name));
|
2015-11-30 08:28:28 +08:00
|
|
|
buf_upcase(extern_c_macro);
|
|
|
|
|
|
|
|
Buf h_buf = BUF_INIT;
|
|
|
|
buf_resize(&h_buf, 0);
|
|
|
|
for (int fn_def_i = 0; fn_def_i < g->fn_defs.length; fn_def_i += 1) {
|
|
|
|
FnTableEntry *fn_table_entry = g->fn_defs.at(fn_def_i);
|
|
|
|
AstNode *proto_node = fn_table_entry->proto_node;
|
|
|
|
assert(proto_node->type == NodeTypeFnProto);
|
|
|
|
AstNodeFnProto *fn_proto = &proto_node->data.fn_proto;
|
|
|
|
|
|
|
|
if (fn_proto->visib_mod != FnProtoVisibModExport)
|
|
|
|
continue;
|
|
|
|
|
2015-12-04 08:30:44 +08:00
|
|
|
Buf return_type_c = BUF_INIT;
|
|
|
|
to_c_type(g, fn_proto->return_type, &return_type_c);
|
2015-12-04 08:06:04 +08:00
|
|
|
|
2015-11-30 08:28:28 +08:00
|
|
|
buf_appendf(&h_buf, "%s %s %s(",
|
|
|
|
buf_ptr(export_macro),
|
2015-12-04 08:30:44 +08:00
|
|
|
buf_ptr(&return_type_c),
|
2015-11-30 08:28:28 +08:00
|
|
|
buf_ptr(&fn_proto->name));
|
|
|
|
|
2015-12-04 08:30:44 +08:00
|
|
|
Buf param_type_c = BUF_INIT;
|
2015-11-30 08:28:28 +08:00
|
|
|
if (fn_proto->params.length) {
|
|
|
|
for (int param_i = 0; param_i < fn_proto->params.length; param_i += 1) {
|
|
|
|
AstNode *param_decl_node = fn_proto->params.at(param_i);
|
|
|
|
AstNode *param_type = param_decl_node->data.param_decl.type;
|
2015-12-04 08:30:44 +08:00
|
|
|
to_c_type(g, param_type, ¶m_type_c);
|
2015-11-30 08:28:28 +08:00
|
|
|
buf_appendf(&h_buf, "%s %s",
|
2015-12-04 08:30:44 +08:00
|
|
|
buf_ptr(¶m_type_c),
|
2015-11-30 08:28:28 +08:00
|
|
|
buf_ptr(¶m_decl_node->data.param_decl.name));
|
|
|
|
if (param_i < fn_proto->params.length - 1)
|
|
|
|
buf_appendf(&h_buf, ", ");
|
|
|
|
}
|
2015-12-04 08:06:04 +08:00
|
|
|
buf_appendf(&h_buf, ")");
|
2015-11-30 08:28:28 +08:00
|
|
|
} else {
|
2015-12-04 08:06:04 +08:00
|
|
|
buf_appendf(&h_buf, "void)");
|
2015-11-30 08:28:28 +08:00
|
|
|
}
|
2015-12-04 08:06:04 +08:00
|
|
|
|
|
|
|
buf_appendf(&h_buf, ";\n");
|
|
|
|
|
2015-11-30 08:28:28 +08:00
|
|
|
}
|
|
|
|
|
2015-12-01 10:58:53 +08:00
|
|
|
Buf *ifdef_dance_name = buf_sprintf("%s_%s_H",
|
|
|
|
buf_ptr(g->root_out_name), buf_ptr(g->root_out_name));
|
2015-11-30 08:28:28 +08:00
|
|
|
buf_upcase(ifdef_dance_name);
|
|
|
|
|
|
|
|
fprintf(out_h, "#ifndef %s\n", buf_ptr(ifdef_dance_name));
|
|
|
|
fprintf(out_h, "#define %s\n\n", buf_ptr(ifdef_dance_name));
|
|
|
|
|
|
|
|
if (g->c_stdint_used)
|
|
|
|
fprintf(out_h, "#include <stdint.h>\n");
|
|
|
|
|
|
|
|
fprintf(out_h, "\n");
|
|
|
|
|
|
|
|
fprintf(out_h, "#ifdef __cplusplus\n");
|
|
|
|
fprintf(out_h, "#define %s extern \"C\"\n", buf_ptr(extern_c_macro));
|
|
|
|
fprintf(out_h, "#else\n");
|
|
|
|
fprintf(out_h, "#define %s\n", buf_ptr(extern_c_macro));
|
|
|
|
fprintf(out_h, "#endif\n");
|
|
|
|
fprintf(out_h, "\n");
|
|
|
|
fprintf(out_h, "#if defined(_WIN32)\n");
|
|
|
|
fprintf(out_h, "#define %s %s __declspec(dllimport)\n", buf_ptr(export_macro), buf_ptr(extern_c_macro));
|
|
|
|
fprintf(out_h, "#else\n");
|
|
|
|
fprintf(out_h, "#define %s %s __attribute__((visibility (\"default\")))\n",
|
|
|
|
buf_ptr(export_macro), buf_ptr(extern_c_macro));
|
|
|
|
fprintf(out_h, "#endif\n");
|
|
|
|
fprintf(out_h, "\n");
|
|
|
|
|
|
|
|
fprintf(out_h, "%s", buf_ptr(&h_buf));
|
|
|
|
|
|
|
|
fprintf(out_h, "\n#endif\n");
|
|
|
|
|
|
|
|
if (fclose(out_h))
|
|
|
|
zig_panic("unable to close h file: %s", strerror(errno));
|
|
|
|
}
|
2015-11-28 12:24:11 +08:00
|
|
|
|
2015-12-16 03:44:42 +08:00
|
|
|
static void find_libc_path(CodeGen *g) {
|
|
|
|
if (g->libc_path && buf_len(g->libc_path))
|
|
|
|
return;
|
|
|
|
g->libc_path = buf_create_from_str(ZIG_LIBC_DIR);
|
|
|
|
if (g->libc_path && buf_len(g->libc_path))
|
|
|
|
return;
|
|
|
|
fprintf(stderr, "Unable to determine libc path. Consider using `--libc-path [path]`\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *get_libc_file(CodeGen *g, const char *file) {
|
|
|
|
Buf *out_buf = buf_alloc();
|
|
|
|
os_path_join(g->libc_path, buf_create_from_str(file), out_buf);
|
|
|
|
return buf_ptr(out_buf);
|
|
|
|
}
|
|
|
|
|
2015-12-01 10:58:53 +08:00
|
|
|
void codegen_link(CodeGen *g, const char *out_file) {
|
|
|
|
bool is_optimized = (g->build_type == CodeGenBuildTypeRelease);
|
|
|
|
if (is_optimized) {
|
|
|
|
if (g->verbose) {
|
|
|
|
fprintf(stderr, "\nOptimization:\n");
|
|
|
|
fprintf(stderr, "---------------\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMZigOptimizeModule(g->target_machine, g->module);
|
|
|
|
|
|
|
|
if (g->verbose) {
|
|
|
|
LLVMDumpModule(g->module);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (g->verbose) {
|
|
|
|
fprintf(stderr, "\nLink:\n");
|
|
|
|
fprintf(stderr, "-------\n");
|
|
|
|
}
|
|
|
|
|
2015-11-28 12:24:11 +08:00
|
|
|
if (!out_file) {
|
2015-12-01 10:58:53 +08:00
|
|
|
out_file = buf_ptr(g->root_out_name);
|
2015-11-28 12:24:11 +08:00
|
|
|
}
|
|
|
|
|
2015-11-25 06:01:37 +08:00
|
|
|
Buf out_file_o = BUF_INIT;
|
2015-11-25 04:51:36 +08:00
|
|
|
buf_init_from_str(&out_file_o, out_file);
|
2015-11-28 13:13:39 +08:00
|
|
|
|
|
|
|
if (g->out_type != OutTypeObj) {
|
|
|
|
buf_append_str(&out_file_o, ".o");
|
|
|
|
}
|
2015-11-25 04:51:36 +08:00
|
|
|
|
2015-11-25 10:07:33 +08:00
|
|
|
char *err_msg = nullptr;
|
2015-11-30 02:12:40 +08:00
|
|
|
if (LLVMTargetMachineEmitToFile(g->target_machine, g->module, buf_ptr(&out_file_o),
|
2015-11-28 06:46:06 +08:00
|
|
|
LLVMObjectFile, &err_msg))
|
|
|
|
{
|
2015-11-25 04:00:38 +08:00
|
|
|
zig_panic("unable to write object file: %s", err_msg);
|
|
|
|
}
|
2015-11-25 04:51:36 +08:00
|
|
|
|
2015-11-28 13:13:39 +08:00
|
|
|
if (g->out_type == OutTypeObj) {
|
2015-12-16 03:44:42 +08:00
|
|
|
if (g->verbose) {
|
|
|
|
fprintf(stderr, "OK\n");
|
|
|
|
}
|
2015-11-28 13:13:39 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g->out_type == OutTypeLib && g->is_static) {
|
|
|
|
// invoke `ar`
|
2015-11-30 08:28:28 +08:00
|
|
|
// example:
|
|
|
|
// # static link into libfoo.a
|
|
|
|
// ar cq libfoo.a foo1.o foo2.o
|
2015-11-28 13:13:39 +08:00
|
|
|
zig_panic("TODO invoke ar");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// invoke `ld`
|
2015-11-25 04:51:36 +08:00
|
|
|
ZigList<const char *> args = {0};
|
2015-12-16 03:44:42 +08:00
|
|
|
const char *crt1o;
|
2015-11-25 13:32:26 +08:00
|
|
|
if (g->is_static) {
|
|
|
|
args.append("-static");
|
2015-12-16 03:44:42 +08:00
|
|
|
crt1o = "crt1.o";
|
|
|
|
} else {
|
|
|
|
crt1o = "Scrt1.o";
|
2015-11-25 13:32:26 +08:00
|
|
|
}
|
2015-11-27 14:38:26 +08:00
|
|
|
|
2015-12-16 12:48:41 +08:00
|
|
|
// TODO don't pass this parameter unless linking with libc
|
2015-11-27 14:38:26 +08:00
|
|
|
char *ZIG_NATIVE_DYNAMIC_LINKER = getenv("ZIG_NATIVE_DYNAMIC_LINKER");
|
|
|
|
if (g->is_native_target && ZIG_NATIVE_DYNAMIC_LINKER) {
|
|
|
|
if (ZIG_NATIVE_DYNAMIC_LINKER[0] != 0) {
|
|
|
|
args.append("-dynamic-linker");
|
|
|
|
args.append(ZIG_NATIVE_DYNAMIC_LINKER);
|
|
|
|
}
|
|
|
|
} else {
|
2015-11-26 17:49:40 +08:00
|
|
|
args.append("-dynamic-linker");
|
2015-12-01 05:43:09 +08:00
|
|
|
args.append(buf_ptr(get_dynamic_linker(g->target_machine)));
|
2015-11-26 17:49:40 +08:00
|
|
|
}
|
2015-11-27 14:38:26 +08:00
|
|
|
|
2015-11-30 08:28:28 +08:00
|
|
|
if (g->out_type == OutTypeLib) {
|
2015-11-30 17:11:31 +08:00
|
|
|
Buf *out_lib_so = buf_sprintf("lib%s.so.%d.%d.%d",
|
2015-12-01 10:58:53 +08:00
|
|
|
buf_ptr(g->root_out_name), g->version_major, g->version_minor, g->version_patch);
|
|
|
|
Buf *soname = buf_sprintf("lib%s.so.%d", buf_ptr(g->root_out_name), g->version_major);
|
2015-11-30 08:28:28 +08:00
|
|
|
args.append("-shared");
|
|
|
|
args.append("-soname");
|
|
|
|
args.append(buf_ptr(soname));
|
|
|
|
out_file = buf_ptr(out_lib_so);
|
|
|
|
}
|
|
|
|
|
2015-11-25 04:51:36 +08:00
|
|
|
args.append("-o");
|
|
|
|
args.append(out_file);
|
2015-11-27 14:38:26 +08:00
|
|
|
|
2015-12-16 03:44:42 +08:00
|
|
|
bool link_in_crt = (g->link_libc && g->out_type == OutTypeExe);
|
|
|
|
|
|
|
|
if (link_in_crt) {
|
|
|
|
find_libc_path(g);
|
|
|
|
|
|
|
|
args.append(get_libc_file(g, crt1o));
|
|
|
|
args.append(get_libc_file(g, "crti.o"));
|
|
|
|
}
|
|
|
|
|
2015-11-25 04:51:36 +08:00
|
|
|
args.append((const char *)buf_ptr(&out_file_o));
|
2015-11-25 14:44:41 +08:00
|
|
|
|
2015-12-16 03:44:42 +08:00
|
|
|
if (link_in_crt) {
|
|
|
|
args.append(get_libc_file(g, "crtn.o"));
|
|
|
|
}
|
|
|
|
|
2015-11-25 14:44:41 +08:00
|
|
|
auto it = g->link_table.entry_iterator();
|
|
|
|
for (;;) {
|
|
|
|
auto *entry = it.next();
|
|
|
|
if (!entry)
|
|
|
|
break;
|
|
|
|
|
|
|
|
Buf *arg = buf_sprintf("-l%s", buf_ptr(entry->key));
|
|
|
|
args.append(buf_ptr(arg));
|
|
|
|
}
|
|
|
|
|
2015-12-16 03:44:42 +08:00
|
|
|
if (g->verbose) {
|
|
|
|
fprintf(stderr, "ld");
|
|
|
|
for (int i = 0; i < args.length; i += 1) {
|
|
|
|
fprintf(stderr, " %s", args.at(i));
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
int return_code;
|
|
|
|
Buf ld_stderr = BUF_INIT;
|
|
|
|
Buf ld_stdout = BUF_INIT;
|
|
|
|
os_exec_process("ld", args, &return_code, &ld_stderr, &ld_stdout);
|
|
|
|
|
|
|
|
if (return_code != 0) {
|
|
|
|
fprintf(stderr, "ld failed with return code %d\n", return_code);
|
|
|
|
fprintf(stderr, "%s\n", buf_ptr(&ld_stderr));
|
|
|
|
exit(1);
|
2015-12-16 08:11:44 +08:00
|
|
|
} else if (buf_len(&ld_stderr)) {
|
|
|
|
fprintf(stderr, "%s\n", buf_ptr(&ld_stderr));
|
2015-12-16 03:44:42 +08:00
|
|
|
}
|
2015-11-30 08:28:28 +08:00
|
|
|
|
|
|
|
if (g->out_type == OutTypeLib) {
|
|
|
|
generate_h_file(g);
|
|
|
|
}
|
2015-12-01 10:58:53 +08:00
|
|
|
|
|
|
|
if (g->verbose) {
|
|
|
|
fprintf(stderr, "OK\n");
|
|
|
|
}
|
2015-11-25 04:00:38 +08:00
|
|
|
}
|