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 00:14:58 +08:00
|
|
|
#include "semantic_info.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->fn_table.init(32);
|
|
|
|
g->str_table.init(32);
|
2015-11-25 10:07:33 +08:00
|
|
|
g->type_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-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-01 00:14:58 +08:00
|
|
|
static LLVMValueRef gen_expr(CodeGen *g, AstNode *expr_node);
|
2015-11-24 13:47:25 +08:00
|
|
|
|
2015-11-24 17:43:45 +08:00
|
|
|
static LLVMTypeRef to_llvm_type(AstNode *type_node) {
|
|
|
|
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);
|
|
|
|
|
|
|
|
return type_node->codegen_node->data.type_node.entry->type_ref;
|
|
|
|
}
|
|
|
|
|
2015-12-01 05:10:07 +08:00
|
|
|
static LLVMZigDIType *to_llvm_debug_type(AstNode *type_node) {
|
2015-11-25 10:07:33 +08:00
|
|
|
assert(type_node->type == NodeTypeType);
|
|
|
|
assert(type_node->codegen_node);
|
|
|
|
assert(type_node->codegen_node->data.type_node.entry);
|
2015-11-24 17:43:45 +08:00
|
|
|
|
2015-11-25 10:07:33 +08:00
|
|
|
return type_node->codegen_node->data.type_node.entry->di_type;
|
2015-11-25 04:37:14 +08:00
|
|
|
}
|
|
|
|
|
2015-12-01 06:36:58 +08:00
|
|
|
static bool type_is_unreachable(CodeGen *g, AstNode *type_node) {
|
2015-11-25 04:37:14 +08:00
|
|
|
assert(type_node->type == NodeTypeType);
|
2015-11-26 09:17:35 +08:00
|
|
|
assert(type_node->codegen_node);
|
|
|
|
assert(type_node->codegen_node->data.type_node.entry);
|
2015-12-01 06:36:58 +08:00
|
|
|
return type_node->codegen_node->data.type_node.entry == g->builtin_types.entry_unreachable;
|
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-01 05:10:07 +08:00
|
|
|
LLVMZigSetCurrentDebugLocation(g->builder, node->line + 1, node->column + 1, g->block_scopes.last());
|
2015-11-25 10:54:10 +08:00
|
|
|
}
|
|
|
|
|
2015-11-24 17:43:45 +08:00
|
|
|
static LLVMValueRef find_or_create_string(CodeGen *g, Buf *str) {
|
|
|
|
auto entry = g->str_table.maybe_get(str);
|
|
|
|
if (entry) {
|
|
|
|
return entry->value;
|
|
|
|
}
|
|
|
|
LLVMValueRef text = LLVMConstString(buf_ptr(str), buf_len(str), false);
|
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-11-30 08:28:28 +08:00
|
|
|
static LLVMValueRef get_variable_value(CodeGen *g, Buf *name) {
|
|
|
|
assert(g->cur_fn->proto_node->type == NodeTypeFnProto);
|
|
|
|
int param_count = g->cur_fn->proto_node->data.fn_proto.params.length;
|
|
|
|
for (int i = 0; i < param_count; i += 1) {
|
|
|
|
AstNode *param_decl_node = g->cur_fn->proto_node->data.fn_proto.params.at(i);
|
|
|
|
assert(param_decl_node->type == NodeTypeParamDecl);
|
|
|
|
Buf *param_name = ¶m_decl_node->data.param_decl.name;
|
|
|
|
if (buf_eql_buf(name, param_name)) {
|
|
|
|
CodeGenNode *codegen_node = g->cur_fn->fn_def_node->codegen_node;
|
|
|
|
assert(codegen_node);
|
|
|
|
FnDefNode *codegen_fn_def = &codegen_node->data.fn_def_node;
|
|
|
|
return codegen_fn_def->params[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
zig_unreachable();
|
|
|
|
}
|
|
|
|
|
2015-11-30 05:39:11 +08:00
|
|
|
static LLVMValueRef gen_fn_call_expr(CodeGen *g, AstNode *node) {
|
|
|
|
assert(node->type == NodeTypeFnCallExpr);
|
|
|
|
|
|
|
|
Buf *name = hack_get_fn_call_name(g, node->data.fn_call_expr.fn_ref_expr);
|
|
|
|
|
2015-12-01 17:08:58 +08:00
|
|
|
FnTableEntry *fn_table_entry;
|
|
|
|
auto entry = g->cur_fn->import_entry->fn_table.maybe_get(name);
|
|
|
|
if (entry)
|
|
|
|
fn_table_entry = entry->value;
|
|
|
|
else
|
|
|
|
fn_table_entry = g->fn_table.get(name);
|
|
|
|
|
2015-11-30 05:39:11 +08:00
|
|
|
assert(fn_table_entry->proto_node->type == NodeTypeFnProto);
|
|
|
|
int expected_param_count = fn_table_entry->proto_node->data.fn_proto.params.length;
|
|
|
|
int actual_param_count = node->data.fn_call_expr.params.length;
|
|
|
|
assert(expected_param_count == actual_param_count);
|
|
|
|
|
|
|
|
LLVMValueRef *param_values = allocate<LLVMValueRef>(actual_param_count);
|
|
|
|
for (int i = 0; i < actual_param_count; i += 1) {
|
|
|
|
AstNode *expr_node = node->data.fn_call_expr.params.at(i);
|
|
|
|
param_values[i] = gen_expr(g, expr_node);
|
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,
|
|
|
|
param_values, actual_param_count, fn_table_entry->calling_convention, "");
|
|
|
|
|
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-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-11-30 05:00:34 +08:00
|
|
|
LLVMValueRef expr = gen_expr(g, 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) {
|
|
|
|
case PrefixOpNegation:
|
2015-11-30 04:37:55 +08:00
|
|
|
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
|
|
|
{
|
|
|
|
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-11-30 04:37:55 +08:00
|
|
|
add_debug_source_node(g, node);
|
|
|
|
return LLVMBuildNot(g->builder, expr, "");
|
2015-11-30 05:00:34 +08:00
|
|
|
case PrefixOpInvalid:
|
2015-11-30 04:37:55 +08:00
|
|
|
zig_unreachable();
|
|
|
|
}
|
2015-11-28 15:40:54 +08:00
|
|
|
zig_unreachable();
|
|
|
|
}
|
|
|
|
|
|
|
|
static LLVMValueRef gen_cast_expr(CodeGen *g, AstNode *node) {
|
|
|
|
assert(node->type == NodeTypeCastExpr);
|
|
|
|
|
2015-11-30 05:00:34 +08:00
|
|
|
LLVMValueRef expr = gen_expr(g, node->data.cast_expr.prefix_op_expr);
|
2015-11-28 15:40:54 +08:00
|
|
|
|
|
|
|
if (!node->data.cast_expr.type)
|
|
|
|
return expr;
|
|
|
|
|
|
|
|
zig_panic("TODO cast expression");
|
|
|
|
}
|
|
|
|
|
2015-11-30 06:08:25 +08:00
|
|
|
static LLVMValueRef gen_arithmetic_bin_op_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-11-30 05:46:05 +08:00
|
|
|
switch (node->data.bin_op_expr.bin_op) {
|
2015-11-30 06:08:25 +08:00
|
|
|
case BinOpTypeBinOr:
|
2015-11-28 15:40:54 +08:00
|
|
|
add_debug_source_node(g, node);
|
2015-11-30 06:08:25 +08:00
|
|
|
return LLVMBuildOr(g->builder, val1, val2, "");
|
|
|
|
case BinOpTypeBinXor:
|
2015-11-28 15:40:54 +08:00
|
|
|
add_debug_source_node(g, node);
|
2015-11-30 06:08:25 +08:00
|
|
|
return LLVMBuildXor(g->builder, val1, val2, "");
|
|
|
|
case BinOpTypeBinAnd:
|
2015-11-28 15:40:54 +08:00
|
|
|
add_debug_source_node(g, 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-11-28 15:40:54 +08:00
|
|
|
add_debug_source_node(g, node);
|
|
|
|
return LLVMBuildShl(g->builder, val1, val2, "");
|
2015-11-30 05:46:05 +08:00
|
|
|
case BinOpTypeBitShiftRight:
|
2015-11-28 15:40:54 +08:00
|
|
|
// TODO implement type system so that we know whether to do
|
|
|
|
// logical or arithmetic shifting here.
|
|
|
|
// signed -> arithmetic, unsigned -> logical
|
|
|
|
add_debug_source_node(g, node);
|
|
|
|
return LLVMBuildLShr(g->builder, val1, val2, "");
|
2015-11-30 06:08:25 +08:00
|
|
|
case BinOpTypeAdd:
|
|
|
|
add_debug_source_node(g, node);
|
|
|
|
return LLVMBuildAdd(g->builder, val1, val2, "");
|
|
|
|
case BinOpTypeSub:
|
|
|
|
add_debug_source_node(g, node);
|
|
|
|
return LLVMBuildSub(g->builder, val1, val2, "");
|
|
|
|
case BinOpTypeMult:
|
|
|
|
// TODO types so we know float vs int
|
|
|
|
add_debug_source_node(g, node);
|
|
|
|
return LLVMBuildMul(g->builder, val1, val2, "");
|
|
|
|
case BinOpTypeDiv:
|
|
|
|
// TODO types so we know float vs int and signed vs unsigned
|
|
|
|
add_debug_source_node(g, node);
|
|
|
|
return LLVMBuildSDiv(g->builder, val1, val2, "");
|
|
|
|
case BinOpTypeMod:
|
|
|
|
// TODO types so we know float vs int and signed vs unsigned
|
|
|
|
add_debug_source_node(g, node);
|
|
|
|
return LLVMBuildSRem(g->builder, val1, val2, "");
|
|
|
|
case BinOpTypeBoolOr:
|
|
|
|
case BinOpTypeBoolAnd:
|
|
|
|
case BinOpTypeCmpEq:
|
|
|
|
case BinOpTypeCmpNotEq:
|
|
|
|
case BinOpTypeCmpLessThan:
|
|
|
|
case BinOpTypeCmpGreaterThan:
|
|
|
|
case BinOpTypeCmpLessOrEq:
|
|
|
|
case BinOpTypeCmpGreaterOrEq:
|
|
|
|
case BinOpTypeInvalid:
|
2015-11-28 15:40:54 +08:00
|
|
|
zig_unreachable();
|
|
|
|
}
|
|
|
|
zig_unreachable();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
// TODO implement type system so that we know whether to do signed or unsigned comparison here
|
2015-11-30 05:46:05 +08:00
|
|
|
LLVMIntPredicate pred = cmp_op_to_int_predicate(node->data.bin_op_expr.bin_op, true);
|
2015-11-28 15:40:54 +08:00
|
|
|
add_debug_source_node(g, node);
|
|
|
|
return LLVMBuildICmp(g->builder, pred, val1, val2, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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
|
|
|
|
|
|
|
LLVMValueRef zero = LLVMConstNull(LLVMTypeOf(val1));
|
|
|
|
add_debug_source_node(g, node);
|
|
|
|
LLVMValueRef val1_i1 = LLVMBuildICmp(g->builder, LLVMIntEQ, val1, zero, "");
|
|
|
|
LLVMBuildCondBr(g->builder, val1_i1, false_block, true_block);
|
|
|
|
|
|
|
|
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-11-28 15:40:54 +08:00
|
|
|
add_debug_source_node(g, node);
|
|
|
|
LLVMValueRef val2_i1 = LLVMBuildICmp(g->builder, LLVMIntEQ, val2, zero, "");
|
|
|
|
|
|
|
|
LLVMPositionBuilderAtEnd(g->builder, false_block);
|
|
|
|
add_debug_source_node(g, node);
|
|
|
|
LLVMValueRef phi = LLVMBuildPhi(g->builder, LLVMInt1Type(), "");
|
|
|
|
LLVMValueRef one_i1 = LLVMConstAllOnes(LLVMInt1Type());
|
|
|
|
LLVMValueRef incoming_values[2] = {one_i1, val2_i1};
|
|
|
|
LLVMBasicBlockRef incoming_blocks[2] = {LLVMGetInsertBlock(g->builder), true_block};
|
|
|
|
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-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
|
|
|
|
|
|
|
LLVMValueRef zero = LLVMConstNull(LLVMTypeOf(val1));
|
|
|
|
add_debug_source_node(g, expr_node);
|
|
|
|
LLVMValueRef val1_i1 = LLVMBuildICmp(g->builder, LLVMIntEQ, val1, zero, "");
|
|
|
|
LLVMBuildCondBr(g->builder, val1_i1, false_block, true_block);
|
|
|
|
|
|
|
|
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-11-28 15:40:54 +08:00
|
|
|
add_debug_source_node(g, expr_node);
|
|
|
|
LLVMValueRef val2_i1 = LLVMBuildICmp(g->builder, LLVMIntEQ, val2, zero, "");
|
|
|
|
|
|
|
|
LLVMPositionBuilderAtEnd(g->builder, true_block);
|
|
|
|
add_debug_source_node(g, expr_node);
|
|
|
|
LLVMValueRef phi = LLVMBuildPhi(g->builder, LLVMInt1Type(), "");
|
|
|
|
LLVMValueRef one_i1 = LLVMConstAllOnes(LLVMInt1Type());
|
|
|
|
LLVMValueRef incoming_values[2] = {one_i1, val2_i1};
|
|
|
|
LLVMBasicBlockRef incoming_blocks[2] = {LLVMGetInsertBlock(g->builder), false_block};
|
|
|
|
LLVMAddIncoming(phi, incoming_values, incoming_blocks, 2);
|
|
|
|
|
|
|
|
return phi;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
Expression : BoolOrExpression | ReturnExpression
|
|
|
|
*/
|
|
|
|
static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) {
|
|
|
|
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-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-11-30 08:16:32 +08:00
|
|
|
case NodeTypeUnreachable:
|
|
|
|
add_debug_source_node(g, node);
|
|
|
|
return LLVMBuildUnreachable(g->builder);
|
|
|
|
case NodeTypeNumberLiteral:
|
|
|
|
{
|
|
|
|
Buf *number_str = &node->data.number;
|
|
|
|
LLVMTypeRef number_type = LLVMInt32Type();
|
|
|
|
LLVMValueRef number_val = LLVMConstIntOfStringAndSize(number_type,
|
|
|
|
buf_ptr(number_str), buf_len(number_str), 10);
|
|
|
|
return number_val;
|
|
|
|
}
|
|
|
|
case NodeTypeStringLiteral:
|
|
|
|
{
|
|
|
|
Buf *str = &node->data.string;
|
|
|
|
LLVMValueRef str_val = find_or_create_string(g, str);
|
|
|
|
LLVMValueRef indices[] = {
|
|
|
|
LLVMConstInt(LLVMInt32Type(), 0, false),
|
|
|
|
LLVMConstInt(LLVMInt32Type(), 0, false)
|
|
|
|
};
|
|
|
|
LLVMValueRef ptr_val = LLVMBuildInBoundsGEP(g->builder, str_val, indices, 2, "");
|
|
|
|
return ptr_val;
|
|
|
|
}
|
2015-11-30 08:28:28 +08:00
|
|
|
case NodeTypeSymbol:
|
|
|
|
{
|
|
|
|
Buf *name = &node->data.symbol;
|
|
|
|
return get_variable_value(g, name);
|
|
|
|
}
|
2015-11-28 15:40:54 +08:00
|
|
|
case NodeTypeRoot:
|
|
|
|
case NodeTypeRootExportDecl:
|
|
|
|
case NodeTypeFnProto:
|
|
|
|
case NodeTypeFnDef:
|
|
|
|
case NodeTypeFnDecl:
|
|
|
|
case NodeTypeParamDecl:
|
|
|
|
case NodeTypeType:
|
|
|
|
case NodeTypeBlock:
|
|
|
|
case NodeTypeExternBlock:
|
|
|
|
case NodeTypeDirective:
|
2015-12-01 10:58:53 +08:00
|
|
|
case NodeTypeUse:
|
2015-11-28 15:40:54 +08:00
|
|
|
zig_unreachable();
|
2015-11-24 15:35:23 +08:00
|
|
|
}
|
|
|
|
zig_unreachable();
|
|
|
|
}
|
|
|
|
|
2015-12-01 10:58:53 +08:00
|
|
|
static void gen_block(CodeGen *g, ImportTableEntry *import, AstNode *block_node, bool add_implicit_return) {
|
2015-11-24 15:35:23 +08:00
|
|
|
assert(block_node->type == NodeTypeBlock);
|
|
|
|
|
2015-12-01 05:10:07 +08:00
|
|
|
LLVMZigDILexicalBlock *di_block = LLVMZigCreateLexicalBlock(g->dbuilder, g->block_scopes.last(),
|
2015-12-01 10:58:53 +08:00
|
|
|
import->di_file, block_node->line + 1, block_node->column + 1);
|
2015-12-01 05:10:07 +08:00
|
|
|
g->block_scopes.append(LLVMZigLexicalBlockToScope(di_block));
|
2015-11-25 10:54:10 +08:00
|
|
|
|
2015-11-28 06:46:06 +08:00
|
|
|
add_debug_source_node(g, block_node);
|
|
|
|
|
2015-11-24 15:35:23 +08:00
|
|
|
for (int i = 0; i < block_node->data.block.statements.length; i += 1) {
|
|
|
|
AstNode *statement_node = block_node->data.block.statements.at(i);
|
2015-11-28 15:40:54 +08:00
|
|
|
gen_expr(g, statement_node);
|
2015-11-24 15:35:23 +08:00
|
|
|
}
|
2015-11-25 10:54:10 +08:00
|
|
|
|
2015-11-28 01:52:31 +08:00
|
|
|
if (add_implicit_return) {
|
|
|
|
LLVMBuildRetVoid(g->builder);
|
|
|
|
}
|
|
|
|
|
2015-11-25 10:54:10 +08:00
|
|
|
g->block_scopes.pop();
|
2015-11-24 15:35:23 +08:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
types[0] = to_llvm_debug_type(fn_proto->return_type);
|
|
|
|
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-01 05:43:09 +08:00
|
|
|
LLVMZigDIType *param_type = to_llvm_debug_type(param_node->data.param_decl.type);
|
|
|
|
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-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-01 05:10:07 +08:00
|
|
|
g->block_scopes.append(LLVMZigCompileUnitToScope(g->compile_unit));
|
2015-11-25 10:54:10 +08:00
|
|
|
|
2015-11-26 09:17:35 +08:00
|
|
|
|
|
|
|
// Generate function prototypes
|
2015-12-01 17:08:58 +08:00
|
|
|
for (int i = 0; i < g->fn_protos.length; i += 1) {
|
|
|
|
FnTableEntry *fn_table_entry = g->fn_protos.at(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-11-24 17:43:45 +08:00
|
|
|
LLVMTypeRef ret_type = to_llvm_type(fn_proto->return_type);
|
|
|
|
LLVMTypeRef *param_types = allocate<LLVMTypeRef>(fn_proto->params.length);
|
|
|
|
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);
|
|
|
|
AstNode *type_node = param_node->data.param_decl.type;
|
|
|
|
param_types[param_decl_i] = to_llvm_type(type_node);
|
|
|
|
}
|
2015-11-24 17:43:45 +08:00
|
|
|
LLVMTypeRef function_type = LLVMFunctionType(ret_type, param_types, fn_proto->params.length, 0);
|
2015-11-28 06:46:06 +08:00
|
|
|
LLVMValueRef fn = LLVMAddFunction(g->module, buf_ptr(&fn_proto->name), function_type);
|
2015-11-24 15:35:23 +08:00
|
|
|
|
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.
|
|
|
|
for (int i = 0; i < g->fn_defs.length; i += 1) {
|
|
|
|
FnTableEntry *fn_table_entry = g->fn_defs.at(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.
|
2015-12-01 10:58:53 +08:00
|
|
|
LLVMZigDIScope *fn_scope = LLVMZigFileToScope(import->di_file);
|
2015-11-25 10:07:33 +08:00
|
|
|
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,
|
2015-12-01 10:58:53 +08:00
|
|
|
fn_scope, buf_ptr(&fn_proto->name), "", import->di_file, line_number,
|
|
|
|
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-12-01 05:10:07 +08:00
|
|
|
g->block_scopes.append(LLVMZigSubprogramToScope(subprogram));
|
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-11-30 08:28:28 +08:00
|
|
|
codegen_fn_def->params = allocate<LLVMValueRef>(LLVMCountParams(fn));
|
|
|
|
LLVMGetParams(fn, codegen_fn_def->params);
|
|
|
|
|
|
|
|
bool add_implicit_return = codegen_fn_def->add_implicit_return;
|
2015-12-01 10:58:53 +08:00
|
|
|
gen_block(g, import, fn_def_node->data.fn_def.body, add_implicit_return);
|
2015-11-26 09:17:35 +08:00
|
|
|
|
|
|
|
g->block_scopes.pop();
|
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-01 10:58:53 +08:00
|
|
|
static void define_primitive_types(CodeGen *g) {
|
2015-12-01 13:12:21 +08:00
|
|
|
{
|
|
|
|
// if this type is anywhere in the AST, we should never hit codegen.
|
|
|
|
TypeTableEntry *entry = allocate<TypeTableEntry>(1);
|
|
|
|
buf_init_from_str(&entry->name, "(invalid)");
|
|
|
|
g->builtin_types.entry_invalid = entry;
|
|
|
|
}
|
2015-12-01 10:58:53 +08:00
|
|
|
{
|
|
|
|
TypeTableEntry *entry = allocate<TypeTableEntry>(1);
|
|
|
|
entry->type_ref = LLVMInt8Type();
|
|
|
|
buf_init_from_str(&entry->name, "u8");
|
|
|
|
entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name), 8, 8,
|
|
|
|
LLVMZigEncoding_DW_ATE_unsigned());
|
|
|
|
g->type_table.put(&entry->name, entry);
|
|
|
|
g->builtin_types.entry_u8 = entry;
|
|
|
|
}
|
2015-12-02 05:41:03 +08:00
|
|
|
g->builtin_types.entry_string_literal = get_pointer_to_type(g, g->builtin_types.entry_u8, true);
|
2015-12-01 10:58:53 +08:00
|
|
|
{
|
|
|
|
TypeTableEntry *entry = allocate<TypeTableEntry>(1);
|
|
|
|
entry->type_ref = LLVMInt32Type();
|
|
|
|
buf_init_from_str(&entry->name, "i32");
|
|
|
|
entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name), 32, 32,
|
|
|
|
LLVMZigEncoding_DW_ATE_signed());
|
|
|
|
g->type_table.put(&entry->name, entry);
|
|
|
|
g->builtin_types.entry_i32 = entry;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
TypeTableEntry *entry = allocate<TypeTableEntry>(1);
|
|
|
|
entry->type_ref = LLVMVoidType();
|
|
|
|
buf_init_from_str(&entry->name, "void");
|
|
|
|
entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name), 0, 0,
|
|
|
|
LLVMZigEncoding_DW_ATE_unsigned());
|
|
|
|
g->type_table.put(&entry->name, entry);
|
|
|
|
g->builtin_types.entry_void = entry;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
TypeTableEntry *entry = allocate<TypeTableEntry>(1);
|
|
|
|
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->type_table.put(&entry->name, entry);
|
|
|
|
g->builtin_types.entry_unreachable = 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) {
|
|
|
|
LLVMInitializeAllTargets();
|
|
|
|
LLVMInitializeAllTargetMCs();
|
|
|
|
LLVMInitializeAllAsmPrinters();
|
|
|
|
LLVMInitializeAllAsmParsers();
|
|
|
|
LLVMInitializeNativeTarget();
|
|
|
|
|
|
|
|
g->is_native_target = true;
|
|
|
|
char *native_triple = LLVMGetDefaultTargetTriple();
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
g->module = LLVMModuleCreateWithName("ZigModule");
|
|
|
|
|
|
|
|
g->pointer_size_bytes = LLVMPointerSize(g->target_data_ref);
|
|
|
|
|
|
|
|
g->builder = LLVMCreateBuilder();
|
|
|
|
g->dbuilder = LLVMZigCreateDIBuilder(g->module, true);
|
|
|
|
|
|
|
|
|
|
|
|
define_primitive_types(g);
|
|
|
|
|
|
|
|
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-01 14:06:29 +08:00
|
|
|
static ImportTableEntry *codegen_add_code(CodeGen *g, Buf *source_path, Buf *source_code) {
|
2015-12-01 17:29:21 +08:00
|
|
|
int err;
|
2015-12-01 10:58:53 +08:00
|
|
|
Buf full_path = BUF_INIT;
|
|
|
|
os_path_join(g->root_source_dir, source_path, &full_path);
|
|
|
|
|
|
|
|
Buf dirname = BUF_INIT;
|
|
|
|
Buf basename = BUF_INIT;
|
|
|
|
os_path_split(&full_path, &dirname, &basename);
|
|
|
|
|
|
|
|
if (g->verbose) {
|
|
|
|
fprintf(stderr, "\nOriginal Source (%s):\n", buf_ptr(source_path));
|
|
|
|
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;
|
|
|
|
err->path = source_path;
|
|
|
|
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;
|
|
|
|
import_entry->path = source_path;
|
2015-12-01 13:53:37 +08:00
|
|
|
import_entry->fn_table.init(32);
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
import_entry->di_file = LLVMZigCreateFile(g->dbuilder, buf_ptr(&basename), buf_ptr(&dirname));
|
|
|
|
g->import_table.put(source_path, import_entry);
|
|
|
|
|
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);
|
|
|
|
if (top_level_decl->type != NodeTypeUse)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto entry = g->import_table.maybe_get(&top_level_decl->data.use.path);
|
|
|
|
if (!entry) {
|
|
|
|
Buf full_path = BUF_INIT;
|
|
|
|
os_path_join(g->root_source_dir, &top_level_decl->data.use.path, &full_path);
|
2015-12-01 17:08:58 +08:00
|
|
|
Buf *import_code = buf_alloc();
|
2015-12-01 17:29:21 +08:00
|
|
|
if ((err = os_fetch_file_path(&full_path, import_code))) {
|
|
|
|
add_node_error(g, top_level_decl,
|
|
|
|
buf_sprintf("unable to open \"%s\": %s", buf_ptr(&full_path), err_str(err)));
|
|
|
|
break;
|
|
|
|
}
|
2015-12-01 17:08:58 +08:00
|
|
|
codegen_add_code(g, &top_level_decl->data.use.path, import_code);
|
2015-12-01 13:53:37 +08:00
|
|
|
}
|
|
|
|
}
|
2015-12-01 14:06:29 +08:00
|
|
|
|
|
|
|
return import_entry;
|
2015-12-01 13:53:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void codegen_add_root_code(CodeGen *g, Buf *source_path, Buf *source_code) {
|
|
|
|
init(g, source_path);
|
|
|
|
|
2015-12-01 14:06:29 +08:00
|
|
|
g->root_import = codegen_add_code(g, source_path, source_code);
|
2015-12-01 13:53:37 +08:00
|
|
|
|
|
|
|
if (g->verbose) {
|
|
|
|
fprintf(stderr, "\nSemantic Analysis:\n");
|
|
|
|
fprintf(stderr, "--------------------\n");
|
|
|
|
}
|
|
|
|
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-11-30 08:28:28 +08:00
|
|
|
static Buf *to_c_type(CodeGen *g, AstNode *type_node) {
|
|
|
|
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;
|
|
|
|
return buf_create_from_str("uint8_t");
|
|
|
|
} else if (type_entry == g->builtin_types.entry_i32) {
|
|
|
|
g->c_stdint_used = true;
|
|
|
|
return buf_create_from_str("int32_t");
|
|
|
|
} else {
|
|
|
|
zig_panic("TODO");
|
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;
|
|
|
|
|
|
|
|
buf_appendf(&h_buf, "%s %s %s(",
|
|
|
|
buf_ptr(export_macro),
|
|
|
|
buf_ptr(to_c_type(g, fn_proto->return_type)),
|
|
|
|
buf_ptr(&fn_proto->name));
|
|
|
|
|
|
|
|
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;
|
|
|
|
buf_appendf(&h_buf, "%s %s",
|
|
|
|
buf_ptr(to_c_type(g, param_type)),
|
|
|
|
buf_ptr(¶m_decl_node->data.param_decl.name));
|
|
|
|
if (param_i < fn_proto->params.length - 1)
|
|
|
|
buf_appendf(&h_buf, ", ");
|
|
|
|
}
|
|
|
|
buf_appendf(&h_buf, ");\n");
|
|
|
|
} else {
|
|
|
|
buf_appendf(&h_buf, "void);\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-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) {
|
|
|
|
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-11-25 13:32:26 +08:00
|
|
|
if (g->is_static) {
|
|
|
|
args.append("-static");
|
|
|
|
}
|
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-11-25 04:51:36 +08:00
|
|
|
args.append((const char *)buf_ptr(&out_file_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-11-25 04:51:36 +08:00
|
|
|
os_spawn_process("ld", args, false);
|
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
|
|
|
}
|