Merge pull request #8918 from ziglang/stage1-tokenizer

stage1: rework tokenizer to match stage2
This commit is contained in:
Andrew Kelley 2021-05-28 21:54:01 -04:00 committed by GitHub
commit c12704a339
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
28 changed files with 10755 additions and 11758 deletions

View File

@ -292,7 +292,7 @@ set(ZIG0_SOURCES
set(STAGE1_SOURCES
"${CMAKE_SOURCE_DIR}/src/stage1/analyze.cpp"
"${CMAKE_SOURCE_DIR}/src/stage1/ast_render.cpp"
"${CMAKE_SOURCE_DIR}/src/stage1/astgen.cpp"
"${CMAKE_SOURCE_DIR}/src/stage1/bigfloat.cpp"
"${CMAKE_SOURCE_DIR}/src/stage1/bigint.cpp"
"${CMAKE_SOURCE_DIR}/src/stage1/buffer.cpp"
@ -307,11 +307,11 @@ set(STAGE1_SOURCES
"${CMAKE_SOURCE_DIR}/src/stage1/os.cpp"
"${CMAKE_SOURCE_DIR}/src/stage1/parser.cpp"
"${CMAKE_SOURCE_DIR}/src/stage1/range_set.cpp"
"${CMAKE_SOURCE_DIR}/src/stage1/softfloat_ext.cpp"
"${CMAKE_SOURCE_DIR}/src/stage1/stage1.cpp"
"${CMAKE_SOURCE_DIR}/src/stage1/target.cpp"
"${CMAKE_SOURCE_DIR}/src/stage1/tokenizer.cpp"
"${CMAKE_SOURCE_DIR}/src/stage1/util.cpp"
"${CMAKE_SOURCE_DIR}/src/stage1/softfloat_ext.cpp"
)
set(OPTIMIZED_C_SOURCES
"${CMAKE_SOURCE_DIR}/src/stage1/parse_f128.c"

View File

@ -107,6 +107,7 @@ pub fn parseAppend(buf: *std.ArrayList(u8), bytes: []const u8) error{OutOfMemory
const hex_str = slice[index + 2 .. index_end];
if (std.fmt.parseUnsigned(u32, hex_str, 16)) |uint| {
if (uint <= 0x10ffff) {
// TODO this incorrectly depends on endianness
try buf.appendSlice(std.mem.toBytes(uint)[0..]);
state = State.Start;
index = index_end; // loop-header increments

View File

@ -252,6 +252,8 @@ const Error = extern enum {
ZigIsTheCCompiler,
FileBusy,
Locked,
InvalidCharacter,
UnicodePointTooLarge,
};
// ABI warning

View File

@ -38,7 +38,7 @@ struct IrInstGenCast;
struct IrInstGenAlloca;
struct IrInstGenCall;
struct IrInstGenAwait;
struct IrBasicBlockSrc;
struct Stage1ZirBasicBlock;
struct IrBasicBlockGen;
struct ScopeDecls;
struct ZigWindowsSDK;
@ -51,7 +51,7 @@ struct ResultLocPeerParent;
struct ResultLocBitCast;
struct ResultLocCast;
struct ResultLocReturn;
struct IrExecutableGen;
struct Stage1Air;
enum FileExt {
FileExtUnknown,
@ -110,51 +110,33 @@ enum X64CABIClass {
X64CABIClass_SSE,
};
struct IrExecutableSrc {
ZigList<IrBasicBlockSrc *> basic_block_list;
struct Stage1Zir {
ZigList<Stage1ZirBasicBlock *> basic_block_list;
Buf *name;
ZigFn *name_fn;
size_t mem_slot_count;
size_t next_debug_id;
size_t *backward_branch_count;
size_t *backward_branch_quota;
ZigFn *fn_entry;
Buf *c_import_buf;
AstNode *source_node;
IrExecutableGen *parent_exec;
IrAnalyze *analysis;
Scope *begin_scope;
ErrorMsg *first_err_trace_msg;
ZigList<Tld *> tld_list;
bool is_inline;
bool is_generic_instantiation;
bool need_err_code_spill;
// This is a function for use in the debugger to print
// the source location.
void src();
};
struct IrExecutableGen {
struct Stage1Air {
ZigList<IrBasicBlockGen *> basic_block_list;
Buf *name;
ZigFn *name_fn;
size_t mem_slot_count;
size_t next_debug_id;
size_t *backward_branch_count;
size_t *backward_branch_quota;
ZigFn *fn_entry;
Buf *c_import_buf;
AstNode *source_node;
IrExecutableGen *parent_exec;
IrExecutableSrc *source_exec;
Stage1Air *parent_exec;
Stage1Zir *source_exec;
Scope *begin_scope;
ErrorMsg *first_err_trace_msg;
ZigList<Tld *> tld_list;
bool is_inline;
bool is_generic_instantiation;
bool need_err_code_spill;
// This is a function for use in the debugger to print
@ -670,7 +652,7 @@ enum NodeType {
NodeTypeIntLiteral,
NodeTypeStringLiteral,
NodeTypeCharLiteral,
NodeTypeSymbol,
NodeTypeIdentifier,
NodeTypePrefixOpExpr,
NodeTypePointerType,
NodeTypeFnCallExpr,
@ -710,6 +692,7 @@ enum NodeType {
NodeTypeAwaitExpr,
NodeTypeSuspend,
NodeTypeAnyFrameType,
// main_token points to the identifier.
NodeTypeEnumLiteral,
NodeTypeAnyTypeField,
};
@ -733,7 +716,8 @@ struct AstNodeFnProto {
AstNode *section_expr;
// populated if the "callconv(S)" is present
AstNode *callconv_expr;
Buf doc_comments;
TokenIndex doc_comments;
// This is set based only on the existence of a noinline or inline keyword.
// This is then resolved to an is_noinline bool and (potentially .Inline)
@ -755,8 +739,8 @@ struct AstNodeFnDef {
struct AstNodeParamDecl {
Buf *name;
AstNode *type;
Token *anytype_token;
Buf doc_comments;
TokenIndex doc_comments;
TokenIndex anytype_token;
bool is_noalias;
bool is_comptime;
bool is_var_args;
@ -799,9 +783,9 @@ struct AstNodeVariableDeclaration {
AstNode *align_expr;
// populated if the "section(S)" is present
AstNode *section_expr;
Token *threadlocal_tok;
Buf doc_comments;
TokenIndex doc_comments;
TokenIndex threadlocal_tok;
VisibMod visib_mod;
bool is_const;
bool is_comptime;
@ -935,13 +919,14 @@ struct AstNodePrefixOpExpr {
};
struct AstNodePointerType {
Token *star_token;
TokenIndex star_token;
TokenIndex allow_zero_token;
TokenIndex bit_offset_start;
TokenIndex host_int_bytes;
AstNode *sentinel;
AstNode *align_expr;
BigInt *bit_offset_start;
BigInt *host_int_bytes;
AstNode *op_expr;
Token *allow_zero_token;
bool is_const;
bool is_volatile;
};
@ -956,7 +941,7 @@ struct AstNodeArrayType {
AstNode *sentinel;
AstNode *child_type;
AstNode *align_expr;
Token *allow_zero_token;
TokenIndex allow_zero_token;
bool is_const;
bool is_volatile;
};
@ -974,11 +959,11 @@ struct AstNodeIfBoolExpr {
struct AstNodeTryExpr {
Buf *var_symbol;
bool var_is_ptr;
AstNode *target_node;
AstNode *then_node;
AstNode *else_node;
Buf *err_symbol;
bool var_is_ptr;
};
struct AstNodeTestExpr {
@ -993,12 +978,12 @@ struct AstNodeWhileExpr {
Buf *name;
AstNode *condition;
Buf *var_symbol;
bool var_is_ptr;
AstNode *continue_expr;
AstNode *body;
AstNode *else_node;
Buf *err_symbol;
bool is_inline;
bool var_is_ptr;
};
struct AstNodeForExpr {
@ -1070,7 +1055,7 @@ struct AsmToken {
};
struct AstNodeAsmExpr {
Token *volatile_token;
TokenIndex volatile_token;
AstNode *asm_template;
ZigList<AsmOutput*> output_list;
ZigList<AsmInput*> input_list;
@ -1094,7 +1079,7 @@ struct AstNodeContainerDecl {
AstNode *init_arg_expr; // enum(T), struct(endianness), or union(T), or union(enum(T))
ZigList<AstNode *> fields;
ZigList<AstNode *> decls;
Buf doc_comments;
TokenIndex doc_comments;
ContainerKind kind;
ContainerLayout layout;
@ -1103,7 +1088,7 @@ struct AstNodeContainerDecl {
};
struct AstNodeErrorSetField {
Buf doc_comments;
TokenIndex doc_comments;
AstNode *field_name;
};
@ -1118,28 +1103,8 @@ struct AstNodeStructField {
AstNode *value;
// populated if the "align(A)" is present
AstNode *align_expr;
Buf doc_comments;
Token *comptime_token;
};
struct AstNodeStringLiteral {
Buf *buf;
};
struct AstNodeCharLiteral {
uint32_t value;
};
struct AstNodeFloatLiteral {
BigFloat *bigfloat;
// overflow is true if when parsing the number, we discovered it would not
// fit without losing data in a double
bool overflow;
};
struct AstNodeIntLiteral {
BigInt *bigint;
TokenIndex doc_comments;
TokenIndex comptime_token;
};
struct AstNodeStructValueField {
@ -1158,17 +1123,12 @@ struct AstNodeContainerInitExpr {
ContainerInitKind kind;
};
struct AstNodeNullLiteral {
struct AstNodeIdentifier {
Buf *name;
};
struct AstNodeUndefinedLiteral {
};
struct AstNodeThisLiteral {
};
struct AstNodeSymbolExpr {
Buf *symbol;
struct AstNodeEnumLiteral {
Buf *name;
};
struct AstNodeBoolLiteral {
@ -1188,13 +1148,6 @@ struct AstNodeContinueExpr {
Buf *name;
};
struct AstNodeUnreachableExpr {
};
struct AstNodeErrorType {
};
struct AstNodeAwaitExpr {
AstNode *expr;
};
@ -1207,16 +1160,10 @@ struct AstNodeAnyFrameType {
AstNode *payload_type; // can be NULL
};
struct AstNodeEnumLiteral {
Token *period;
Token *identifier;
};
struct AstNode {
enum NodeType type;
TokenIndex main_token;
bool already_traced_this_node;
size_t line;
size_t column;
ZigType *owner;
union {
AstNodeFnDef fn_def;
@ -1252,29 +1199,24 @@ struct AstNode {
AstNodePtrDerefExpr ptr_deref_expr;
AstNodeContainerDecl container_decl;
AstNodeStructField struct_field;
AstNodeStringLiteral string_literal;
AstNodeCharLiteral char_literal;
AstNodeFloatLiteral float_literal;
AstNodeIntLiteral int_literal;
AstNodeContainerInitExpr container_init_expr;
AstNodeStructValueField struct_val_field;
AstNodeNullLiteral null_literal;
AstNodeUndefinedLiteral undefined_literal;
AstNodeThisLiteral this_literal;
AstNodeSymbolExpr symbol_expr;
AstNodeBoolLiteral bool_literal;
AstNodeBreakExpr break_expr;
AstNodeContinueExpr continue_expr;
AstNodeUnreachableExpr unreachable_expr;
AstNodeArrayType array_type;
AstNodeInferredArrayType inferred_array_type;
AstNodeErrorType error_type;
AstNodeErrorSetDecl err_set_decl;
AstNodeErrorSetField err_set_field;
AstNodeResumeExpr resume_expr;
AstNodeAwaitExpr await_expr;
AstNodeSuspend suspend;
AstNodeAnyFrameType anyframe_type;
// These are part of an astgen workaround to use less memory by
// memoizing into the AST. Once astgen is modified to only run once
// per corresponding source, this workaround can be removed.
AstNodeIdentifier identifier;
AstNodeEnumLiteral enum_literal;
} data;
@ -1409,9 +1351,11 @@ struct ZigPackage {
struct RootStruct {
ZigPackage *package;
Buf *path; // relative to root_package->root_src_dir
ZigList<size_t> *line_offsets;
Buf *source_code;
ZigLLVMDIFile *di_file;
size_t token_count;
TokenId *token_ids;
TokenLoc *token_locs;
};
enum StructSpecial {
@ -1703,10 +1647,9 @@ struct ZigFn {
// in the case of async functions this is the implicit return type according to the
// zig source code, not according to zig ir
ZigType *src_implicit_return_type;
IrExecutableSrc *ir_executable;
IrExecutableGen analyzed_executable;
size_t prealloc_bbc;
size_t prealloc_backward_branch_quota;
Stage1Zir *stage1_zir;
Stage1Air analyzed_executable;
size_t branch_quota;
AstNode **param_source_nodes;
Buf **param_names;
IrInstGen *err_code_spill;
@ -2326,11 +2269,11 @@ struct ScopeBlock {
Scope base;
Buf *name;
IrBasicBlockSrc *end_block;
Stage1ZirBasicBlock *end_block;
IrInstSrc *is_comptime;
ResultLocPeerParent *peer_parent;
ZigList<IrInstSrc *> *incoming_values;
ZigList<IrBasicBlockSrc *> *incoming_blocks;
ZigList<Stage1ZirBasicBlock *> *incoming_blocks;
AstNode *safety_set_node;
AstNode *fast_math_set_node;
@ -2382,11 +2325,11 @@ struct ScopeLoop {
LVal lval;
Buf *name;
IrBasicBlockSrc *break_block;
IrBasicBlockSrc *continue_block;
Stage1ZirBasicBlock *break_block;
Stage1ZirBasicBlock *continue_block;
IrInstSrc *is_comptime;
ZigList<IrInstSrc *> *incoming_values;
ZigList<IrBasicBlockSrc *> *incoming_blocks;
ZigList<Stage1ZirBasicBlock *> *incoming_blocks;
ResultLocPeerParent *peer_parent;
ScopeExpr *spill_scope;
@ -2497,7 +2440,7 @@ enum AtomicRmwOp {
// to another basic block.
// Phi instructions must be first in a basic block.
// The last instruction in a basic block must be of type unreachable.
struct IrBasicBlockSrc {
struct Stage1ZirBasicBlock {
ZigList<IrInstSrc *> instruction_list;
IrBasicBlockGen *child;
Scope *scope;
@ -2772,8 +2715,7 @@ enum IrInstGenId {
IrInstGenIdExtern,
};
// Common fields between IrInstSrc and IrInstGen. This allows future passes
// after pass2 to be added to zig.
// Common fields between IrInstSrc and IrInstGen.
struct IrInst {
// if ref_count is zero and the instruction has no side effects,
// the instruction can be omitted in codegen
@ -2801,7 +2743,7 @@ struct IrInstSrc {
// can find the instruction that corresponds to this value in the "new ir"
// with this child field.
IrInstGen *child;
IrBasicBlockSrc *owner_bb;
Stage1ZirBasicBlock *owner_bb;
// for debugging purposes, these are useful to call to inspect the instruction
void dump();
@ -2846,8 +2788,8 @@ struct IrInstSrcCondBr {
IrInstSrc base;
IrInstSrc *condition;
IrBasicBlockSrc *then_block;
IrBasicBlockSrc *else_block;
Stage1ZirBasicBlock *then_block;
Stage1ZirBasicBlock *else_block;
IrInstSrc *is_comptime;
ResultLoc *result_loc;
};
@ -2863,7 +2805,7 @@ struct IrInstGenCondBr {
struct IrInstSrcBr {
IrInstSrc base;
IrBasicBlockSrc *dest_block;
Stage1ZirBasicBlock *dest_block;
IrInstSrc *is_comptime;
};
@ -2875,14 +2817,14 @@ struct IrInstGenBr {
struct IrInstSrcSwitchBrCase {
IrInstSrc *value;
IrBasicBlockSrc *block;
Stage1ZirBasicBlock *block;
};
struct IrInstSrcSwitchBr {
IrInstSrc base;
IrInstSrc *target_value;
IrBasicBlockSrc *else_block;
Stage1ZirBasicBlock *else_block;
size_t case_count;
IrInstSrcSwitchBrCase *cases;
IrInstSrc *is_comptime;
@ -2928,7 +2870,7 @@ struct IrInstSrcPhi {
IrInstSrc base;
size_t incoming_count;
IrBasicBlockSrc **incoming_blocks;
Stage1ZirBasicBlock **incoming_blocks;
IrInstSrc **incoming_values;
ResultLocPeerParent *peer_parent;
};
@ -4592,7 +4534,7 @@ struct ResultLocPeerParent {
bool skipped;
bool done_resuming;
IrBasicBlockSrc *end_bb;
Stage1ZirBasicBlock *end_bb;
ResultLoc *parent;
ZigList<ResultLocPeer *> peers;
ZigType *resolved_type;
@ -4603,7 +4545,7 @@ struct ResultLocPeer {
ResultLoc base;
ResultLocPeerParent *parent;
IrBasicBlockSrc *next_bb;
Stage1ZirBasicBlock *next_bb;
IrSuspendPosition suspend_pos;
};

View File

@ -6,9 +6,9 @@
*/
#include "analyze.hpp"
#include "ast_render.hpp"
#include "codegen.hpp"
#include "error.hpp"
#include "astgen.hpp"
#include "ir.hpp"
#include "ir_print.hpp"
#include "os.hpp"
@ -41,41 +41,44 @@ static bool is_top_level_struct(ZigType *import) {
return import->id == ZigTypeIdStruct && import->data.structure.root_struct != nullptr;
}
static ErrorMsg *add_error_note_token(CodeGen *g, ErrorMsg *parent_msg, ZigType *owner, Token *token, Buf *msg) {
static ErrorMsg *add_error_note_token(CodeGen *g, ErrorMsg *parent_msg, ZigType *owner,
TokenIndex token, Buf *msg)
{
assert(is_top_level_struct(owner));
RootStruct *root_struct = owner->data.structure.root_struct;
ErrorMsg *err = err_msg_create_with_line(root_struct->path, token->start_line, token->start_column,
root_struct->source_code, root_struct->line_offsets, msg);
uint32_t byte_offset = root_struct->token_locs[token].offset;
ErrorMsg *err = err_msg_create_with_offset(root_struct->path, byte_offset,
buf_ptr(root_struct->source_code), msg);
err_msg_add_note(parent_msg, err);
return err;
}
ErrorMsg *add_token_error(CodeGen *g, ZigType *owner, Token *token, Buf *msg) {
ErrorMsg *add_token_error_offset(CodeGen *g, ZigType *owner, TokenIndex token, Buf *msg,
uint32_t bad_index)
{
assert(is_top_level_struct(owner));
RootStruct *root_struct = owner->data.structure.root_struct;
ErrorMsg *err = err_msg_create_with_line(root_struct->path, token->start_line, token->start_column,
root_struct->source_code, root_struct->line_offsets, msg);
uint32_t byte_offset = root_struct->token_locs[token].offset + bad_index;
ErrorMsg *err = err_msg_create_with_offset(root_struct->path, byte_offset,
buf_ptr(root_struct->source_code), msg);
g->errors.append(err);
g->trace_err = err;
return err;
}
ErrorMsg *add_token_error(CodeGen *g, ZigType *owner, TokenIndex token, Buf *msg) {
return add_token_error_offset(g, owner, token, msg, 0);
}
ErrorMsg *add_node_error(CodeGen *g, AstNode *node, Buf *msg) {
Token fake_token;
fake_token.start_line = node->line;
fake_token.start_column = node->column;
node->already_traced_this_node = true;
return add_token_error(g, node->owner, &fake_token, msg);
return add_token_error(g, node->owner, node->main_token, msg);
}
ErrorMsg *add_error_note(CodeGen *g, ErrorMsg *parent_msg, const AstNode *node, Buf *msg) {
Token fake_token;
fake_token.start_line = node->line;
fake_token.start_column = node->column;
return add_error_note_token(g, parent_msg, node->owner, &fake_token, msg);
return add_error_note_token(g, parent_msg, node->owner, node->main_token, msg);
}
ZigType *new_type_table_entry(ZigTypeId id) {
@ -913,13 +916,27 @@ ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type) {
return entry;
}
ZigType *get_opaque_type(CodeGen *g, Scope *scope, AstNode *source_node, const char *full_name, Buf *bare_name) {
static uint32_t node_line_onebased(AstNode *node) {
RootStruct *root_struct = node->owner->data.structure.root_struct;
assert(node->main_token < root_struct->token_count);
return root_struct->token_locs[node->main_token].line + 1;
}
static uint32_t node_column_onebased(AstNode *node) {
RootStruct *root_struct = node->owner->data.structure.root_struct;
assert(node->main_token < root_struct->token_count);
return root_struct->token_locs[node->main_token].column + 1;
}
ZigType *get_opaque_type(CodeGen *g, Scope *scope, AstNode *source_node, const char *full_name,
Buf *bare_name)
{
ZigType *entry = new_type_table_entry(ZigTypeIdOpaque);
buf_init_from_str(&entry->name, full_name);
ZigType *import = scope ? get_scope_import(scope) : nullptr;
unsigned line = source_node ? (unsigned)(source_node->line + 1) : 0;
unsigned line = source_node ? node_line_onebased(source_node) : 0;
// Note: duplicated in get_partial_container_type
entry->llvm_type = LLVMInt8Type();
@ -1142,7 +1159,7 @@ ZigType *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind
break;
case ContainerKindOpaque: {
ZigType *import = scope ? get_scope_import(scope) : nullptr;
unsigned line = decl_node ? (unsigned)(decl_node->line + 1) : 0;
unsigned line = decl_node ? node_line_onebased(decl_node) : 0;
// Note: duplicated in get_opaque_type
entry->llvm_type = LLVMInt8Type();
entry->llvm_di_type = ZigLLVMCreateDebugForwardDeclType(g->dbuilder,
@ -1584,8 +1601,9 @@ static OnePossibleValue type_val_resolve_has_one_possible_value(CodeGen *g, ZigV
ZigType *analyze_type_expr(CodeGen *g, Scope *scope, AstNode *node) {
Error err;
// Hot path for simple identifiers, to avoid unnecessary memory allocations.
if (node->type == NodeTypeSymbol) {
Buf *variable_name = node->data.symbol_expr.symbol;
if (node->type == NodeTypeIdentifier) {
RootStruct *root_struct = node->owner->data.structure.root_struct;
Buf *variable_name = token_identifier_buf(root_struct, node->main_token);
if (buf_eql_str(variable_name, "_"))
goto abort_hot_path;
ZigType *primitive_type;
@ -2034,7 +2052,7 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc
buf_sprintf("var args only allowed in functions with C calling convention"));
return g->builtin_types.entry_invalid;
}
} else if (param_node->data.param_decl.anytype_token != nullptr) {
} else if (param_node->data.param_decl.anytype_token != 0) {
if (!calling_convention_allows_zig_types(fn_type_id.cc)) {
add_node_error(g, param_node,
buf_sprintf("parameter of type 'anytype' not allowed in function with calling convention '%s'",
@ -3021,7 +3039,7 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
field_node = decl_node->data.container_decl.fields.at(i);
type_struct_field->name = field_node->data.struct_field.name;
type_struct_field->decl_node = field_node;
if (field_node->data.struct_field.comptime_token != nullptr) {
if (field_node->data.struct_field.comptime_token != 0) {
if (field_node->data.struct_field.value == nullptr) {
add_token_error(g, field_node->owner,
field_node->data.struct_field.comptime_token,
@ -3635,14 +3653,7 @@ static void get_fully_qualified_decl_name(CodeGen *g, Buf *buf, Tld *tld, bool i
static ZigFn *create_fn_raw(CodeGen *g, bool is_noinline) {
ZigFn *fn_entry = heap::c_allocator.create<ZigFn>();
fn_entry->ir_executable = heap::c_allocator.create<IrExecutableSrc>();
fn_entry->prealloc_backward_branch_quota = default_backward_branch_quota;
fn_entry->analyzed_executable.backward_branch_count = &fn_entry->prealloc_bbc;
fn_entry->analyzed_executable.backward_branch_quota = &fn_entry->prealloc_backward_branch_quota;
fn_entry->analyzed_executable.fn_entry = fn_entry;
fn_entry->ir_executable->fn_entry = fn_entry;
fn_entry->stage1_zir = heap::c_allocator.create<Stage1Zir>();
fn_entry->is_noinline = is_noinline;
return fn_entry;
@ -4042,7 +4053,7 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {
case NodeTypeBoolLiteral:
case NodeTypeNullLiteral:
case NodeTypeUndefinedLiteral:
case NodeTypeSymbol:
case NodeTypeIdentifier:
case NodeTypePrefixOpExpr:
case NodeTypePointerType:
case NodeTypeIfBoolExpr:
@ -4238,7 +4249,7 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var, bool allow_lazy) {
bool is_const = var_decl->is_const;
bool is_extern = var_decl->is_extern;
bool is_export = var_decl->is_export;
bool is_thread_local = var_decl->threadlocal_tok != nullptr;
bool is_thread_local = var_decl->threadlocal_tok != 0;
ZigType *explicit_type = nullptr;
if (var_decl->type) {
@ -5116,8 +5127,11 @@ static void analyze_fn_ir(CodeGen *g, ZigFn *fn, AstNode *return_type_node) {
if (fn->analyzed_executable.source_node == nullptr) {
fn->analyzed_executable.source_node = fn->body_node;
}
ZigType *block_return_type = ir_analyze(g, fn->ir_executable,
&fn->analyzed_executable, fn_type_id->return_type, return_type_node, nullptr);
size_t backward_branch_count = 0;
size_t backward_branch_quota = max(fn->branch_quota, default_backward_branch_quota);
ZigType *block_return_type = ir_analyze(g, fn->stage1_zir, &fn->analyzed_executable,
&backward_branch_count, &backward_branch_quota,
fn_type_id->return_type, return_type_node, nullptr, fn);
fn->src_implicit_return_type = block_return_type;
if (type_is_invalid(block_return_type) || fn->analyzed_executable.first_err_trace_msg != nullptr) {
@ -5214,21 +5228,19 @@ static void analyze_fn_body(CodeGen *g, ZigFn *fn_table_entry) {
ZigType *fn_type = fn_table_entry->type_entry;
assert(!fn_type->data.fn.is_generic);
if (!ir_gen_fn(g, fn_table_entry)) {
if (!stage1_astgen_fn(g, fn_table_entry)) {
fn_table_entry->anal_state = FnAnalStateInvalid;
return;
}
if (fn_table_entry->ir_executable->first_err_trace_msg != nullptr) {
if (fn_table_entry->stage1_zir->first_err_trace_msg != nullptr) {
fn_table_entry->anal_state = FnAnalStateInvalid;
return;
}
if (g->verbose_ir) {
fprintf(stderr, "\n");
ast_render(stderr, fn_table_entry->body_node, 4);
fprintf(stderr, "\nfn %s() { // (IR)\n", buf_ptr(&fn_table_entry->symbol_name));
ir_print_src(g, stderr, fn_table_entry->ir_executable, 4);
ir_print_src(g, stderr, fn_table_entry->stage1_zir, 4);
fprintf(stderr, "}\n");
}
@ -5238,33 +5250,17 @@ static void analyze_fn_body(CodeGen *g, ZigFn *fn_table_entry) {
ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *resolved_path, Buf *source_code,
SourceKind source_kind)
{
if (g->verbose_tokenize) {
fprintf(stderr, "\nOriginal Source (%s):\n", buf_ptr(resolved_path));
fprintf(stderr, "----------------\n");
fprintf(stderr, "%s\n", buf_ptr(source_code));
fprintf(stderr, "\nTokens:\n");
fprintf(stderr, "---------\n");
}
Tokenization tokenization = {0};
tokenize(source_code, &tokenization);
tokenize(buf_ptr(source_code), &tokenization);
if (tokenization.err) {
ErrorMsg *err = err_msg_create_with_line(resolved_path, tokenization.err_line, tokenization.err_column,
source_code, tokenization.line_offsets, tokenization.err);
ErrorMsg *err = err_msg_create_with_offset(resolved_path, tokenization.err_byte_offset,
buf_ptr(source_code), tokenization.err);
print_err_msg(err, g->err_color);
exit(1);
}
if (g->verbose_tokenize) {
print_tokens(source_code, tokenization.tokens);
fprintf(stderr, "\nAST:\n");
fprintf(stderr, "------\n");
}
Buf *src_dirname = buf_alloc();
Buf *src_basename = buf_alloc();
os_path_split(resolved_path, src_dirname, src_basename);
@ -5297,9 +5293,20 @@ ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *resolved_path, Bu
RootStruct *root_struct = heap::c_allocator.create<RootStruct>();
root_struct->package = package;
root_struct->source_code = source_code;
root_struct->line_offsets = tokenization.line_offsets;
root_struct->path = resolved_path;
root_struct->di_file = ZigLLVMCreateFile(g->dbuilder, buf_ptr(src_basename), buf_ptr(src_dirname));
assert(tokenization.ids.length == tokenization.locs.length);
size_t token_count = tokenization.ids.length;
root_struct->token_count = token_count;
root_struct->token_ids = g->pass1_arena->allocate<TokenId>(token_count);
memcpy(root_struct->token_ids, tokenization.ids.items, token_count * sizeof(TokenId));
root_struct->token_locs = g->pass1_arena->allocate<TokenLoc>(token_count);
memcpy(root_struct->token_locs, tokenization.locs.items, token_count * sizeof(TokenLoc));
tokenization.ids.deinit();
tokenization.locs.deinit();
ZigType *import_entry = get_root_container_type(g, buf_ptr(namespace_name), bare_name, root_struct);
if (source_kind == SourceKindRoot) {
assert(g->root_import == nullptr);
@ -5307,14 +5314,11 @@ ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *resolved_path, Bu
}
g->import_table.put(resolved_path, import_entry);
AstNode *root_node = ast_parse(source_code, tokenization.tokens, import_entry, g->err_color);
AstNode *root_node = ast_parse(source_code, import_entry, g->err_color);
assert(root_node != nullptr);
assert(root_node->type == NodeTypeContainerDecl);
import_entry->data.structure.decl_node = root_node;
import_entry->data.structure.decls_scope->base.source_node = root_node;
if (g->verbose_ast) {
ast_print(stderr, root_node, 0);
}
for (size_t decl_i = 0; decl_i < root_node->data.container_decl.decls.length; decl_i += 1) {
AstNode *top_level_decl = root_node->data.container_decl.decls.at(decl_i);
@ -6254,9 +6258,12 @@ ReqCompTime type_requires_comptime(CodeGen *g, ZigType *ty) {
zig_unreachable();
}
void init_const_str_lit(CodeGen *g, ZigValue *const_val, Buf *str) {
void init_const_str_lit(CodeGen *g, ZigValue *const_val, Buf *str, bool move_str) {
auto entry = g->string_literals_table.maybe_get(str);
if (entry != nullptr) {
if (move_str) {
buf_destroy(str);
}
memcpy(const_val, entry->value, sizeof(ZigValue));
return;
}
@ -6280,7 +6287,7 @@ void init_const_str_lit(CodeGen *g, ZigValue *const_val, Buf *str) {
ZigValue *create_const_str_lit(CodeGen *g, Buf *str) {
ZigValue *const_val = g->pass1_arena->create<ZigValue>();
init_const_str_lit(g, const_val, str);
init_const_str_lit(g, const_val, str, false);
return const_val;
}
@ -8626,7 +8633,7 @@ static void resolve_llvm_types_struct(CodeGen *g, ZigType *struct_type, ResolveS
ZigType *import = get_scope_import(scope);
di_file = import->data.structure.root_struct->di_file;
di_scope = ZigLLVMFileToScope(di_file);
line = decl_node->line + 1;
line = node_line_onebased(decl_node);
} else {
di_file = nullptr;
di_scope = ZigLLVMCompileUnitToScope(g->compile_unit);
@ -8830,7 +8837,7 @@ static void resolve_llvm_types_struct(CodeGen *g, ZigType *struct_type, ResolveS
unsigned line;
if (decl_node != nullptr) {
AstNode *field_node = field->decl_node;
line = field_node->line + 1;
line = node_line_onebased(field_node);
} else {
line = 0;
}
@ -8881,7 +8888,7 @@ static ZigLLVMDIType *make_empty_namespace_llvm_di_type(CodeGen *g, ZigType *imp
return ZigLLVMCreateDebugStructType(g->dbuilder,
ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
name,
import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
import->data.structure.root_struct->di_file, node_line_onebased(decl_node),
debug_size_in_bits,
debug_align_in_bits,
ZigLLVM_DIFlags_Zero,
@ -8926,7 +8933,7 @@ static void resolve_llvm_types_enum(CodeGen *g, ZigType *enum_type, ResolveStatu
uint64_t tag_debug_align_in_bits = 8*tag_int_type->abi_align;
ZigLLVMDIType *tag_di_type = ZigLLVMCreateDebugEnumerationType(g->dbuilder,
ZigLLVMFileToScope(import->data.structure.root_struct->di_file), buf_ptr(&enum_type->name),
import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
import->data.structure.root_struct->di_file, node_line_onebased(decl_node),
tag_debug_size_in_bits,
tag_debug_align_in_bits,
di_enumerators, field_count,
@ -8966,12 +8973,12 @@ static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type, ResolveSta
if (union_type->data.unionation.resolve_status < ResolveStatusLLVMFwdDecl) {
union_type->llvm_type = LLVMStructCreateNamed(LLVMGetGlobalContext(), buf_ptr(&union_type->name));
size_t line = decl_node ? decl_node->line : 0;
unsigned line = decl_node ? node_line_onebased(decl_node) : 0;
unsigned dwarf_kind = ZigLLVMTag_DW_structure_type();
union_type->llvm_di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
dwarf_kind, buf_ptr(&union_type->name),
ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
import->data.structure.root_struct->di_file, (unsigned)(line + 1));
import->data.structure.root_struct->di_file, line);
union_type->data.unionation.resolve_status = ResolveStatusLLVMFwdDecl;
if (ResolveStatusLLVMFwdDecl >= wanted_resolve_status) return;
@ -8992,7 +8999,7 @@ static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type, ResolveSta
AstNode *field_node = union_field->decl_node;
union_inner_di_types[union_field->gen_index] = ZigLLVMCreateDebugMemberType(g->dbuilder,
ZigLLVMTypeToScope(union_type->llvm_di_type), buf_ptr(union_field->enum_field->name),
import->data.structure.root_struct->di_file, (unsigned)(field_node->line + 1),
import->data.structure.root_struct->di_file, node_line_onebased(field_node),
store_size_in_bits,
abi_align_in_bits,
0,
@ -9022,7 +9029,7 @@ static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type, ResolveSta
// create debug type for union
ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugUnionType(g->dbuilder,
ZigLLVMFileToScope(import->data.structure.root_struct->di_file), buf_ptr(&union_type->name),
import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
import->data.structure.root_struct->di_file, node_line_onebased(decl_node),
union_type->data.unionation.union_abi_size * 8,
most_aligned_union_member->align * 8,
ZigLLVM_DIFlags_Zero, union_inner_di_types,
@ -9057,7 +9064,7 @@ static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type, ResolveSta
// create debug type for union
ZigLLVMDIType *union_di_type = ZigLLVMCreateDebugUnionType(g->dbuilder,
ZigLLVMTypeToScope(union_type->llvm_di_type), "AnonUnion",
import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
import->data.structure.root_struct->di_file, node_line_onebased(decl_node),
most_aligned_union_member->type_entry->size_in_bits, 8*most_aligned_union_member->align,
ZigLLVM_DIFlags_Zero, union_inner_di_types, gen_field_count, 0, "");
@ -9068,7 +9075,7 @@ static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type, ResolveSta
ZigLLVMDIType *union_member_di_type = ZigLLVMCreateDebugMemberType(g->dbuilder,
ZigLLVMTypeToScope(union_type->llvm_di_type), "payload",
import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
import->data.structure.root_struct->di_file, node_line_onebased(decl_node),
most_aligned_union_member->type_entry->size_in_bits,
8*most_aligned_union_member->align,
union_offset_in_bits,
@ -9079,7 +9086,7 @@ static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type, ResolveSta
ZigLLVMDIType *tag_member_di_type = ZigLLVMCreateDebugMemberType(g->dbuilder,
ZigLLVMTypeToScope(union_type->llvm_di_type), "tag",
import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
import->data.structure.root_struct->di_file, node_line_onebased(decl_node),
tag_debug_size_in_bits,
tag_debug_align_in_bits,
tag_offset_in_bits,
@ -9094,7 +9101,7 @@ static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type, ResolveSta
ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
buf_ptr(&union_type->name),
import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
import->data.structure.root_struct->di_file, node_line_onebased(decl_node),
debug_size_in_bits,
debug_align_in_bits,
ZigLLVM_DIFlags_Zero, nullptr, di_root_members, 2, 0, nullptr, "");
@ -9774,9 +9781,9 @@ void src_assert_impl(bool ok, AstNode *source_node, char const *file, unsigned i
if (source_node == nullptr) {
fprintf(stderr, "when analyzing (unknown source location) ");
} else {
fprintf(stderr, "when analyzing %s:%u:%u ",
buf_ptr(source_node->owner->data.structure.root_struct->path),
(unsigned)source_node->line + 1, (unsigned)source_node->column + 1);
RootStruct *root_struct = source_node->owner->data.structure.root_struct;
fprintf(stderr, "when analyzing %s:%u:%u ", buf_ptr(root_struct->path),
node_line_onebased(source_node), node_column_onebased(source_node));
}
fprintf(stderr, "in compiler source at %s:%u: ", file, line);
const char *msg = "assertion failed. This is a bug in the Zig compiler.";
@ -9842,18 +9849,17 @@ Error analyze_import(CodeGen *g, ZigType *source_import, Buf *import_target_str,
return ErrorNone;
}
void IrExecutableSrc::src() {
if (this->source_node != nullptr) {
this->source_node->src();
}
if (this->parent_exec != nullptr) {
this->parent_exec->src();
}
void AstNode::src() {
RootStruct *root_struct = this->owner->data.structure.root_struct;
uint32_t line = root_struct->token_locs[this->main_token].line + 1;
uint32_t column = root_struct->token_locs[this->main_token].column + 1;
fprintf(stderr, "%s:%" PRIu32 ":%" PRIu32 "\n",
buf_ptr(root_struct->path),
line, column);
}
void IrExecutableGen::src() {
IrExecutableGen *it;
void Stage1Air::src() {
Stage1Air *it;
for (it = this; it != nullptr && it->source_node != nullptr; it = it->parent_exec) {
it->source_node->src();
}

View File

@ -12,7 +12,9 @@
void semantic_analyze(CodeGen *g);
ErrorMsg *add_node_error(CodeGen *g, AstNode *node, Buf *msg);
ErrorMsg *add_token_error(CodeGen *g, ZigType *owner, Token *token, Buf *msg);
ErrorMsg *add_token_error(CodeGen *g, ZigType *owner, TokenIndex token, Buf *msg);
ErrorMsg *add_token_error_offset(CodeGen *g, ZigType *owner, TokenIndex token, Buf *msg,
uint32_t bad_index);
ErrorMsg *add_error_note(CodeGen *g, ErrorMsg *parent_msg, const AstNode *node, Buf *msg);
ZigType *new_type_table_entry(ZigTypeId id);
ZigType *get_fn_frame_type(CodeGen *g, ZigFn *fn);
@ -140,7 +142,7 @@ Scope *create_runtime_scope(CodeGen *g, AstNode *node, Scope *parent, IrInstSrc
Scope *create_typeof_scope(CodeGen *g, AstNode *node, Scope *parent);
ScopeExpr *create_expr_scope(CodeGen *g, AstNode *node, Scope *parent);
void init_const_str_lit(CodeGen *g, ZigValue *const_val, Buf *str);
void init_const_str_lit(CodeGen *g, ZigValue *const_val, Buf *str, bool move_str);
ZigValue *create_const_str_lit(CodeGen *g, Buf *str);
void init_const_bigint(ZigValue *const_val, ZigType *type, const BigInt *bigint);

File diff suppressed because it is too large Load Diff

View File

@ -1,20 +0,0 @@
/*
* Copyright (c) 2015 Andrew Kelley
*
* This file is part of zig, which is MIT licensed.
* See http://opensource.org/licenses/MIT
*/
#ifndef ZIG_AST_RENDER_HPP
#define ZIG_AST_RENDER_HPP
#include "all_types.hpp"
#include "parser.hpp"
#include <stdio.h>
void ast_print(FILE *f, AstNode *node, int indent);
void ast_render(FILE *f, AstNode *node, int indent_size);
#endif

8119
src/stage1/astgen.cpp Normal file

File diff suppressed because it is too large Load Diff

37
src/stage1/astgen.hpp Normal file
View File

@ -0,0 +1,37 @@
/*
* Copyright (c) 2021 Andrew Kelley
*
* This file is part of zig, which is MIT licensed.
* See http://opensource.org/licenses/MIT
*/
#ifndef ZIG_ASTGEN_HPP
#define ZIG_ASTGEN_HPP
#include "all_types.hpp"
bool stage1_astgen(CodeGen *g, AstNode *node, Scope *scope, Stage1Zir *stage1_zir,
ZigFn *fn, bool in_c_import_scope);
bool stage1_astgen_fn(CodeGen *g, ZigFn *fn_entry);
bool ir_inst_src_has_side_effects(IrInstSrc *inst);
ZigVar *create_local_var(CodeGen *codegen, AstNode *node, Scope *parent_scope,
Buf *name, bool src_is_const, bool gen_is_const, bool is_shadowable, IrInstSrc *is_comptime,
bool skip_name_check);
ResultLoc *no_result_loc(void);
void invalidate_exec(Stage1Zir *exec, ErrorMsg *msg);
AstNode *ast_field_to_symbol_node(AstNode *err_set_field_node);
void ir_add_call_stack_errors_gen(CodeGen *codegen, Stage1Air *exec, ErrorMsg *err_msg,
int limit);
void destroy_instruction_src(IrInstSrc *inst);
bool ir_should_inline(Stage1Zir *exec, Scope *scope);
Buf *get_anon_type_name(CodeGen *codegen, Stage1Zir *exec, const char *kind_name,
Scope *scope, AstNode *source_node, Buf *out_bare_name);
#endif

View File

@ -69,7 +69,7 @@ void bigfloat_init_bigint(BigFloat *dest, const BigInt *op) {
}
}
Error bigfloat_init_buf(BigFloat *dest, const uint8_t *buf_ptr, size_t buf_len) {
Error bigfloat_init_buf(BigFloat *dest, const uint8_t *buf_ptr) {
char *str_begin = (char *)buf_ptr;
char *str_end;
@ -79,7 +79,6 @@ Error bigfloat_init_buf(BigFloat *dest, const uint8_t *buf_ptr, size_t buf_len)
return ErrorOverflow;
}
assert(str_end <= ((char*)buf_ptr) + buf_len);
return ErrorNone;
}

View File

@ -28,7 +28,7 @@ void bigfloat_init_64(BigFloat *dest, double x);
void bigfloat_init_128(BigFloat *dest, float128_t x);
void bigfloat_init_bigfloat(BigFloat *dest, const BigFloat *x);
void bigfloat_init_bigint(BigFloat *dest, const BigInt *op);
Error bigfloat_init_buf(BigFloat *dest, const uint8_t *buf_ptr, size_t buf_len);
Error bigfloat_init_buf(BigFloat *dest, const uint8_t *buf_ptr);
float16_t bigfloat_to_f16(const BigFloat *bigfloat);
float bigfloat_to_f32(const BigFloat *bigfloat);

View File

@ -6,7 +6,6 @@
*/
#include "analyze.hpp"
#include "ast_render.hpp"
#include "codegen.hpp"
#include "errmsg.hpp"
#include "error.hpp"
@ -642,6 +641,18 @@ static LLVMValueRef fn_llvm_value(CodeGen *g, ZigFn *fn) {
return fn->llvm_value;
}
static uint32_t node_line_onebased(AstNode *node) {
RootStruct *root_struct = node->owner->data.structure.root_struct;
assert(node->main_token < root_struct->token_count);
return root_struct->token_locs[node->main_token].line + 1;
}
static uint32_t node_column_onebased(AstNode *node) {
RootStruct *root_struct = node->owner->data.structure.root_struct;
assert(node->main_token < root_struct->token_count);
return root_struct->token_locs[node->main_token].column + 1;
}
static ZigLLVMDIScope *get_di_scope(CodeGen *g, Scope *scope) {
if (scope->di_scope)
return scope->di_scope;
@ -657,8 +668,7 @@ static ZigLLVMDIScope *get_di_scope(CodeGen *g, Scope *scope) {
ZigFn *fn_table_entry = fn_scope->fn_entry;
if (!fn_table_entry->proto_node)
return get_di_scope(g, scope->parent);
unsigned line_number = (unsigned)(fn_table_entry->proto_node->line == 0) ?
0 : (fn_table_entry->proto_node->line + 1);
unsigned line_number = node_line_onebased(fn_table_entry->proto_node);
unsigned scope_line = line_number;
bool is_definition = fn_table_entry->body_node != nullptr;
bool is_optimized = g->build_mode != BuildModeDebug;
@ -696,8 +706,8 @@ static ZigLLVMDIScope *get_di_scope(CodeGen *g, Scope *scope) {
ZigLLVMDILexicalBlock *di_block = ZigLLVMCreateLexicalBlock(g->dbuilder,
get_di_scope(g, scope->parent),
import->data.structure.root_struct->di_file,
(unsigned)scope->source_node->line + 1,
(unsigned)scope->source_node->column + 1);
node_line_onebased(scope->source_node),
node_column_onebased(scope->source_node));
scope->di_scope = ZigLLVMLexicalBlockToScope(di_block);
return scope->di_scope;
}
@ -1798,8 +1808,8 @@ static void gen_var_debug_decl(CodeGen *g, ZigVar *var) {
if (g->strip_debug_symbols) return;
assert(var->di_loc_var != nullptr);
AstNode *source_node = var->decl_node;
ZigLLVMDILocation *debug_loc = ZigLLVMGetDebugLoc((unsigned)source_node->line + 1,
(unsigned)source_node->column + 1, get_di_scope(g, var->parent_scope));
ZigLLVMDILocation *debug_loc = ZigLLVMGetDebugLoc(node_line_onebased(source_node),
node_column_onebased(source_node), get_di_scope(g, var->parent_scope));
ZigLLVMInsertDeclareAtEnd(g->dbuilder, var->value_ref, var->di_loc_var, debug_loc,
LLVMGetInsertBlock(g->builder));
}
@ -2198,7 +2208,7 @@ var_ok:
// arg index + 1 because the 0 index is return value
var->di_loc_var = ZigLLVMCreateParameterVariable(g->dbuilder, get_di_scope(g, var->parent_scope),
var->name, fn_walk->data.vars.import->data.structure.root_struct->di_file,
(unsigned)(var->decl_node->line + 1),
node_line_onebased(var->decl_node),
get_llvm_di_type(g, dest_ty), !g->strip_debug_symbols, 0, di_arg_index + 1);
}
return true;
@ -2433,7 +2443,7 @@ static LLVMValueRef get_merge_err_ret_traces_fn_val(CodeGen *g) {
return fn_val;
}
static LLVMValueRef ir_render_save_err_ret_addr(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_save_err_ret_addr(CodeGen *g, Stage1Air *executable,
IrInstGenSaveErrRetAddr *save_err_ret_addr_instruction)
{
assert(g->have_err_ret_tracing);
@ -2626,7 +2636,7 @@ static void gen_async_return(CodeGen *g, IrInstGenReturn *instruction) {
LLVMBuildRetVoid(g->builder);
}
static LLVMValueRef ir_render_return(CodeGen *g, IrExecutableGen *executable, IrInstGenReturn *instruction) {
static LLVMValueRef ir_render_return(CodeGen *g, Stage1Air *executable, IrInstGenReturn *instruction) {
if (fn_is_async(g->cur_fn)) {
gen_async_return(g, instruction);
return nullptr;
@ -3051,7 +3061,7 @@ static void gen_shift_rhs_check(CodeGen *g, ZigType *lhs_type, ZigType *rhs_type
}
}
static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_bin_op(CodeGen *g, Stage1Air *executable,
IrInstGenBinOp *bin_op_instruction)
{
IrBinOp op_id = bin_op_instruction->op_id;
@ -3273,7 +3283,7 @@ static void add_error_range_check(CodeGen *g, ZigType *err_set_type, ZigType *in
}
}
static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_cast(CodeGen *g, Stage1Air *executable,
IrInstGenCast *cast_instruction)
{
Error err;
@ -3357,7 +3367,7 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutableGen *executable,
zig_unreachable();
}
static LLVMValueRef ir_render_ptr_of_array_to_slice(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_ptr_of_array_to_slice(CodeGen *g, Stage1Air *executable,
IrInstGenPtrOfArrayToSlice *instruction)
{
ZigType *actual_type = instruction->operand->value->type;
@ -3394,7 +3404,7 @@ static LLVMValueRef ir_render_ptr_of_array_to_slice(CodeGen *g, IrExecutableGen
return result_loc;
}
static LLVMValueRef ir_render_ptr_cast(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_ptr_cast(CodeGen *g, Stage1Air *executable,
IrInstGenPtrCast *instruction)
{
ZigType *wanted_type = instruction->base.value->type;
@ -3420,7 +3430,7 @@ static LLVMValueRef ir_render_ptr_cast(CodeGen *g, IrExecutableGen *executable,
return result_ptr;
}
static LLVMValueRef ir_render_bit_cast(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_bit_cast(CodeGen *g, Stage1Air *executable,
IrInstGenBitCast *instruction)
{
ZigType *wanted_type = instruction->base.value->type;
@ -3448,7 +3458,7 @@ static LLVMValueRef ir_render_bit_cast(CodeGen *g, IrExecutableGen *executable,
}
}
static LLVMValueRef ir_render_widen_or_shorten(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_widen_or_shorten(CodeGen *g, Stage1Air *executable,
IrInstGenWidenOrShorten *instruction)
{
ZigType *actual_type = instruction->target->value->type;
@ -3465,7 +3475,7 @@ static LLVMValueRef ir_render_widen_or_shorten(CodeGen *g, IrExecutableGen *exec
instruction->base.value->type, target_val);
}
static LLVMValueRef ir_render_int_to_ptr(CodeGen *g, IrExecutableGen *executable, IrInstGenIntToPtr *instruction) {
static LLVMValueRef ir_render_int_to_ptr(CodeGen *g, Stage1Air *executable, IrInstGenIntToPtr *instruction) {
ZigType *wanted_type = instruction->base.value->type;
LLVMValueRef target_val = ir_llvm_value(g, instruction->target);
const uint32_t align_bytes = get_ptr_align(g, wanted_type);
@ -3503,13 +3513,13 @@ static LLVMValueRef ir_render_int_to_ptr(CodeGen *g, IrExecutableGen *executable
return LLVMBuildIntToPtr(g->builder, target_val, get_llvm_type(g, wanted_type), "");
}
static LLVMValueRef ir_render_ptr_to_int(CodeGen *g, IrExecutableGen *executable, IrInstGenPtrToInt *instruction) {
static LLVMValueRef ir_render_ptr_to_int(CodeGen *g, Stage1Air *executable, IrInstGenPtrToInt *instruction) {
ZigType *wanted_type = instruction->base.value->type;
LLVMValueRef target_val = ir_llvm_value(g, instruction->target);
return LLVMBuildPtrToInt(g->builder, target_val, get_llvm_type(g, wanted_type), "");
}
static LLVMValueRef ir_render_int_to_enum(CodeGen *g, IrExecutableGen *executable, IrInstGenIntToEnum *instruction) {
static LLVMValueRef ir_render_int_to_enum(CodeGen *g, Stage1Air *executable, IrInstGenIntToEnum *instruction) {
ZigType *wanted_type = instruction->base.value->type;
assert(wanted_type->id == ZigTypeIdEnum);
ZigType *tag_int_type = wanted_type->data.enumeration.tag_int_type;
@ -3549,7 +3559,7 @@ static LLVMValueRef ir_render_int_to_enum(CodeGen *g, IrExecutableGen *executabl
return tag_int_value;
}
static LLVMValueRef ir_render_int_to_err(CodeGen *g, IrExecutableGen *executable, IrInstGenIntToErr *instruction) {
static LLVMValueRef ir_render_int_to_err(CodeGen *g, Stage1Air *executable, IrInstGenIntToErr *instruction) {
ZigType *wanted_type = instruction->base.value->type;
assert(wanted_type->id == ZigTypeIdErrorSet);
@ -3566,7 +3576,7 @@ static LLVMValueRef ir_render_int_to_err(CodeGen *g, IrExecutableGen *executable
return gen_widen_or_shorten(g, false, actual_type, g->err_tag_type, target_val);
}
static LLVMValueRef ir_render_err_to_int(CodeGen *g, IrExecutableGen *executable, IrInstGenErrToInt *instruction) {
static LLVMValueRef ir_render_err_to_int(CodeGen *g, Stage1Air *executable, IrInstGenErrToInt *instruction) {
ZigType *wanted_type = instruction->base.value->type;
assert(wanted_type->id == ZigTypeIdInt);
assert(!wanted_type->data.integral.is_signed);
@ -3592,7 +3602,7 @@ static LLVMValueRef ir_render_err_to_int(CodeGen *g, IrExecutableGen *executable
}
}
static LLVMValueRef ir_render_unreachable(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_unreachable(CodeGen *g, Stage1Air *executable,
IrInstGenUnreachable *unreachable_instruction)
{
if (ir_want_runtime_safety(g, &unreachable_instruction->base)) {
@ -3603,7 +3613,7 @@ static LLVMValueRef ir_render_unreachable(CodeGen *g, IrExecutableGen *executabl
return nullptr;
}
static LLVMValueRef ir_render_cond_br(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_cond_br(CodeGen *g, Stage1Air *executable,
IrInstGenCondBr *cond_br_instruction)
{
LLVMBuildCondBr(g->builder,
@ -3613,12 +3623,12 @@ static LLVMValueRef ir_render_cond_br(CodeGen *g, IrExecutableGen *executable,
return nullptr;
}
static LLVMValueRef ir_render_br(CodeGen *g, IrExecutableGen *executable, IrInstGenBr *br_instruction) {
static LLVMValueRef ir_render_br(CodeGen *g, Stage1Air *executable, IrInstGenBr *br_instruction) {
LLVMBuildBr(g->builder, br_instruction->dest_block->llvm_block);
return nullptr;
}
static LLVMValueRef ir_render_binary_not(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_binary_not(CodeGen *g, Stage1Air *executable,
IrInstGenBinaryNot *inst)
{
LLVMValueRef operand = ir_llvm_value(g, inst->operand);
@ -3650,13 +3660,13 @@ static LLVMValueRef ir_gen_negation(CodeGen *g, IrInstGen *inst, IrInstGen *oper
}
}
static LLVMValueRef ir_render_negation(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_negation(CodeGen *g, Stage1Air *executable,
IrInstGenNegation *inst)
{
return ir_gen_negation(g, &inst->base, inst->operand, inst->wrapping);
}
static LLVMValueRef ir_render_bool_not(CodeGen *g, IrExecutableGen *executable, IrInstGenBoolNot *instruction) {
static LLVMValueRef ir_render_bool_not(CodeGen *g, Stage1Air *executable, IrInstGenBoolNot *instruction) {
LLVMValueRef value = ir_llvm_value(g, instruction->value);
LLVMValueRef zero = LLVMConstNull(LLVMTypeOf(value));
return LLVMBuildICmp(g->builder, LLVMIntEQ, value, zero, "");
@ -3670,14 +3680,14 @@ static void render_decl_var(CodeGen *g, ZigVar *var) {
gen_var_debug_decl(g, var);
}
static LLVMValueRef ir_render_decl_var(CodeGen *g, IrExecutableGen *executable, IrInstGenDeclVar *instruction) {
static LLVMValueRef ir_render_decl_var(CodeGen *g, Stage1Air *executable, IrInstGenDeclVar *instruction) {
instruction->var->ptr_instruction = instruction->var_ptr;
instruction->var->did_the_decl_codegen = true;
render_decl_var(g, instruction->var);
return nullptr;
}
static LLVMValueRef ir_render_load_ptr(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_load_ptr(CodeGen *g, Stage1Air *executable,
IrInstGenLoadPtr *instruction)
{
ZigType *child_type = instruction->base.value->type;
@ -3879,7 +3889,7 @@ static void gen_undef_init(CodeGen *g, ZigType *ptr_type, ZigType *value_type, L
gen_assign_raw(g, ptr, ptr_type, zero);
}
static LLVMValueRef ir_render_store_ptr(CodeGen *g, IrExecutableGen *executable, IrInstGenStorePtr *instruction) {
static LLVMValueRef ir_render_store_ptr(CodeGen *g, Stage1Air *executable, IrInstGenStorePtr *instruction) {
Error err;
ZigType *ptr_type = instruction->ptr->value->type;
@ -3911,7 +3921,7 @@ static LLVMValueRef ir_render_store_ptr(CodeGen *g, IrExecutableGen *executable,
return nullptr;
}
static LLVMValueRef ir_render_vector_store_elem(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_vector_store_elem(CodeGen *g, Stage1Air *executable,
IrInstGenVectorStoreElem *instruction)
{
LLVMValueRef vector_ptr = ir_llvm_value(g, instruction->vector_ptr);
@ -3924,7 +3934,7 @@ static LLVMValueRef ir_render_vector_store_elem(CodeGen *g, IrExecutableGen *exe
return nullptr;
}
static LLVMValueRef ir_render_var_ptr(CodeGen *g, IrExecutableGen *executable, IrInstGenVarPtr *instruction) {
static LLVMValueRef ir_render_var_ptr(CodeGen *g, Stage1Air *executable, IrInstGenVarPtr *instruction) {
Error err;
ZigType *ptr_type = instruction->base.value->type;
@ -3951,7 +3961,7 @@ static LLVMValueRef ir_render_var_ptr(CodeGen *g, IrExecutableGen *executable, I
get_llvm_type(g, ptr_type), "");
}
static LLVMValueRef ir_render_return_ptr(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_return_ptr(CodeGen *g, Stage1Air *executable,
IrInstGenReturnPtr *instruction)
{
if (!type_has_bits(g, instruction->base.value->type))
@ -3960,7 +3970,7 @@ static LLVMValueRef ir_render_return_ptr(CodeGen *g, IrExecutableGen *executable
return g->cur_ret_ptr;
}
static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutableGen *executable, IrInstGenElemPtr *instruction) {
static LLVMValueRef ir_render_elem_ptr(CodeGen *g, Stage1Air *executable, IrInstGenElemPtr *instruction) {
LLVMValueRef array_ptr_ptr = ir_llvm_value(g, instruction->array_ptr);
ZigType *array_ptr_type = instruction->array_ptr->value->type;
assert(array_ptr_type->id == ZigTypeIdPointer);
@ -4126,7 +4136,7 @@ static void render_async_spills(CodeGen *g) {
if (var->decl_node) {
var->di_loc_var = ZigLLVMCreateAutoVariable(g->dbuilder, get_di_scope(g, var->parent_scope),
var->name, import->data.structure.root_struct->di_file,
(unsigned)(var->decl_node->line + 1),
node_line_onebased(var->decl_node),
get_llvm_di_type(g, var->var_type), !g->strip_debug_symbols, 0);
gen_var_debug_decl(g, var);
}
@ -4212,7 +4222,7 @@ static void gen_init_stack_trace(CodeGen *g, LLVMValueRef trace_field_ptr, LLVMV
LLVMBuildStore(g->builder, LLVMConstInt(usize_type_ref, stack_trace_ptr_count, false), addrs_len_ptr);
}
static LLVMValueRef ir_render_call(CodeGen *g, IrExecutableGen *executable, IrInstGenCall *instruction) {
static LLVMValueRef ir_render_call(CodeGen *g, Stage1Air *executable, IrInstGenCall *instruction) {
Error err;
LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->llvm_type;
@ -4632,7 +4642,7 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutableGen *executable, IrIn
}
}
static LLVMValueRef ir_render_struct_field_ptr(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_struct_field_ptr(CodeGen *g, Stage1Air *executable,
IrInstGenStructFieldPtr *instruction)
{
Error err;
@ -4683,7 +4693,7 @@ static LLVMValueRef ir_render_struct_field_ptr(CodeGen *g, IrExecutableGen *exec
return field_ptr_val;
}
static LLVMValueRef ir_render_union_field_ptr(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_union_field_ptr(CodeGen *g, Stage1Air *executable,
IrInstGenUnionFieldPtr *instruction)
{
if (instruction->base.value->special != ConstValSpecialRuntime)
@ -4783,7 +4793,7 @@ static size_t find_asm_index(CodeGen *g, AstNode *node, AsmToken *tok, Buf *src_
return SIZE_MAX;
}
static LLVMValueRef ir_render_asm_gen(CodeGen *g, IrExecutableGen *executable, IrInstGenAsm *instruction) {
static LLVMValueRef ir_render_asm_gen(CodeGen *g, Stage1Air *executable, IrInstGenAsm *instruction) {
AstNode *asm_node = instruction->base.base.source_node;
assert(asm_node->type == NodeTypeAsmExpr);
AstNodeAsmExpr *asm_expr = &asm_node->data.asm_expr;
@ -4960,13 +4970,13 @@ static LLVMValueRef gen_non_null_bit(CodeGen *g, ZigType *maybe_type, LLVMValueR
return gen_load_untyped(g, maybe_field_ptr, 0, false, "");
}
static LLVMValueRef ir_render_test_non_null(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_test_non_null(CodeGen *g, Stage1Air *executable,
IrInstGenTestNonNull *instruction)
{
return gen_non_null_bit(g, instruction->value->value->type, ir_llvm_value(g, instruction->value));
}
static LLVMValueRef ir_render_optional_unwrap_ptr(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_optional_unwrap_ptr(CodeGen *g, Stage1Air *executable,
IrInstGenOptionalUnwrapPtr *instruction)
{
if (instruction->base.value->special != ConstValSpecialRuntime)
@ -5073,7 +5083,7 @@ static LLVMValueRef get_int_builtin_fn(CodeGen *g, ZigType *expr_type, BuiltinFn
return fn_val;
}
static LLVMValueRef ir_render_clz(CodeGen *g, IrExecutableGen *executable, IrInstGenClz *instruction) {
static LLVMValueRef ir_render_clz(CodeGen *g, Stage1Air *executable, IrInstGenClz *instruction) {
ZigType *int_type = instruction->op->value->type;
LLVMValueRef fn_val = get_int_builtin_fn(g, int_type, BuiltinFnIdClz);
LLVMValueRef operand = ir_llvm_value(g, instruction->op);
@ -5085,7 +5095,7 @@ static LLVMValueRef ir_render_clz(CodeGen *g, IrExecutableGen *executable, IrIns
return gen_widen_or_shorten(g, false, int_type, instruction->base.value->type, wrong_size_int);
}
static LLVMValueRef ir_render_ctz(CodeGen *g, IrExecutableGen *executable, IrInstGenCtz *instruction) {
static LLVMValueRef ir_render_ctz(CodeGen *g, Stage1Air *executable, IrInstGenCtz *instruction) {
ZigType *int_type = instruction->op->value->type;
LLVMValueRef fn_val = get_int_builtin_fn(g, int_type, BuiltinFnIdCtz);
LLVMValueRef operand = ir_llvm_value(g, instruction->op);
@ -5097,7 +5107,7 @@ static LLVMValueRef ir_render_ctz(CodeGen *g, IrExecutableGen *executable, IrIns
return gen_widen_or_shorten(g, false, int_type, instruction->base.value->type, wrong_size_int);
}
static LLVMValueRef ir_render_shuffle_vector(CodeGen *g, IrExecutableGen *executable, IrInstGenShuffleVector *instruction) {
static LLVMValueRef ir_render_shuffle_vector(CodeGen *g, Stage1Air *executable, IrInstGenShuffleVector *instruction) {
uint64_t len_a = instruction->a->value->type->data.vector.len;
uint64_t len_mask = instruction->mask->value->type->data.vector.len;
@ -5127,7 +5137,7 @@ static LLVMValueRef ir_render_shuffle_vector(CodeGen *g, IrExecutableGen *execut
llvm_mask_value, "");
}
static LLVMValueRef ir_render_splat(CodeGen *g, IrExecutableGen *executable, IrInstGenSplat *instruction) {
static LLVMValueRef ir_render_splat(CodeGen *g, Stage1Air *executable, IrInstGenSplat *instruction) {
ZigType *result_type = instruction->base.value->type;
ir_assert(result_type->id == ZigTypeIdVector, &instruction->base);
uint32_t len = result_type->data.vector.len;
@ -5139,7 +5149,7 @@ static LLVMValueRef ir_render_splat(CodeGen *g, IrExecutableGen *executable, IrI
return LLVMBuildShuffleVector(g->builder, op_vector, undef_vector, LLVMConstNull(mask_llvm_type), "");
}
static LLVMValueRef ir_render_pop_count(CodeGen *g, IrExecutableGen *executable, IrInstGenPopCount *instruction) {
static LLVMValueRef ir_render_pop_count(CodeGen *g, Stage1Air *executable, IrInstGenPopCount *instruction) {
ZigType *int_type = instruction->op->value->type;
LLVMValueRef fn_val = get_int_builtin_fn(g, int_type, BuiltinFnIdPopCount);
LLVMValueRef operand = ir_llvm_value(g, instruction->op);
@ -5147,7 +5157,7 @@ static LLVMValueRef ir_render_pop_count(CodeGen *g, IrExecutableGen *executable,
return gen_widen_or_shorten(g, false, int_type, instruction->base.value->type, wrong_size_int);
}
static LLVMValueRef ir_render_switch_br(CodeGen *g, IrExecutableGen *executable, IrInstGenSwitchBr *instruction) {
static LLVMValueRef ir_render_switch_br(CodeGen *g, Stage1Air *executable, IrInstGenSwitchBr *instruction) {
ZigType *target_type = instruction->target_value->value->type;
LLVMBasicBlockRef else_block = instruction->else_block->llvm_block;
@ -5175,7 +5185,7 @@ static LLVMValueRef ir_render_switch_br(CodeGen *g, IrExecutableGen *executable,
return nullptr;
}
static LLVMValueRef ir_render_phi(CodeGen *g, IrExecutableGen *executable, IrInstGenPhi *instruction) {
static LLVMValueRef ir_render_phi(CodeGen *g, Stage1Air *executable, IrInstGenPhi *instruction) {
if (!type_has_bits(g, instruction->base.value->type))
return nullptr;
@ -5199,7 +5209,7 @@ static LLVMValueRef ir_render_phi(CodeGen *g, IrExecutableGen *executable, IrIns
return phi;
}
static LLVMValueRef ir_render_ref(CodeGen *g, IrExecutableGen *executable, IrInstGenRef *instruction) {
static LLVMValueRef ir_render_ref(CodeGen *g, Stage1Air *executable, IrInstGenRef *instruction) {
if (!type_has_bits(g, instruction->base.value->type)) {
return nullptr;
}
@ -5219,7 +5229,7 @@ static LLVMValueRef ir_render_ref(CodeGen *g, IrExecutableGen *executable, IrIns
}
}
static LLVMValueRef ir_render_err_name(CodeGen *g, IrExecutableGen *executable, IrInstGenErrName *instruction) {
static LLVMValueRef ir_render_err_name(CodeGen *g, Stage1Air *executable, IrInstGenErrName *instruction) {
assert(g->generate_error_name_table);
assert(g->errors_by_index.length > 0);
@ -5346,7 +5356,7 @@ static LLVMValueRef get_enum_tag_name_function(CodeGen *g, ZigType *enum_type) {
return fn_val;
}
static LLVMValueRef ir_render_enum_tag_name(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_enum_tag_name(CodeGen *g, Stage1Air *executable,
IrInstGenTagName *instruction)
{
ZigType *enum_type = instruction->target->value->type;
@ -5359,7 +5369,7 @@ static LLVMValueRef ir_render_enum_tag_name(CodeGen *g, IrExecutableGen *executa
get_llvm_cc(g, CallingConventionUnspecified), ZigLLVM_CallAttrAuto, "");
}
static LLVMValueRef ir_render_field_parent_ptr(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_field_parent_ptr(CodeGen *g, Stage1Air *executable,
IrInstGenFieldParentPtr *instruction)
{
ZigType *container_ptr_type = instruction->base.value->type;
@ -5386,7 +5396,7 @@ static LLVMValueRef ir_render_field_parent_ptr(CodeGen *g, IrExecutableGen *exec
}
}
static LLVMValueRef ir_render_align_cast(CodeGen *g, IrExecutableGen *executable, IrInstGenAlignCast *instruction) {
static LLVMValueRef ir_render_align_cast(CodeGen *g, Stage1Air *executable, IrInstGenAlignCast *instruction) {
LLVMValueRef target_val = ir_llvm_value(g, instruction->target);
assert(target_val);
@ -5449,7 +5459,7 @@ static LLVMValueRef ir_render_align_cast(CodeGen *g, IrExecutableGen *executable
return target_val;
}
static LLVMValueRef ir_render_error_return_trace(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_error_return_trace(CodeGen *g, Stage1Air *executable,
IrInstGenErrorReturnTrace *instruction)
{
bool is_llvm_alloca;
@ -5522,7 +5532,7 @@ static LLVMTypeRef get_atomic_abi_type(CodeGen *g, IrInstGen *instruction, bool
}
}
static LLVMValueRef ir_render_cmpxchg(CodeGen *g, IrExecutableGen *executable, IrInstGenCmpxchg *instruction) {
static LLVMValueRef ir_render_cmpxchg(CodeGen *g, Stage1Air *executable, IrInstGenCmpxchg *instruction) {
LLVMValueRef ptr_val = ir_llvm_value(g, instruction->ptr);
LLVMValueRef cmp_val = ir_llvm_value(g, instruction->cmp_value);
LLVMValueRef new_val = ir_llvm_value(g, instruction->new_value);
@ -5584,7 +5594,7 @@ static LLVMValueRef ir_render_cmpxchg(CodeGen *g, IrExecutableGen *executable, I
return result_loc;
}
static LLVMValueRef ir_render_reduce(CodeGen *g, IrExecutableGen *executable, IrInstGenReduce *instruction) {
static LLVMValueRef ir_render_reduce(CodeGen *g, Stage1Air *executable, IrInstGenReduce *instruction) {
LLVMValueRef value = ir_llvm_value(g, instruction->value);
ZigType *value_type = instruction->value->value->type;
@ -5648,13 +5658,13 @@ static LLVMValueRef ir_render_reduce(CodeGen *g, IrExecutableGen *executable, Ir
return result_val;
}
static LLVMValueRef ir_render_fence(CodeGen *g, IrExecutableGen *executable, IrInstGenFence *instruction) {
static LLVMValueRef ir_render_fence(CodeGen *g, Stage1Air *executable, IrInstGenFence *instruction) {
LLVMAtomicOrdering atomic_order = to_LLVMAtomicOrdering(instruction->order);
LLVMBuildFence(g->builder, atomic_order, false, "");
return nullptr;
}
static LLVMValueRef ir_render_truncate(CodeGen *g, IrExecutableGen *executable, IrInstGenTruncate *instruction) {
static LLVMValueRef ir_render_truncate(CodeGen *g, Stage1Air *executable, IrInstGenTruncate *instruction) {
LLVMValueRef target_val = ir_llvm_value(g, instruction->target);
ZigType *dest_type = instruction->base.value->type;
ZigType *src_type = instruction->target->value->type;
@ -5669,7 +5679,7 @@ static LLVMValueRef ir_render_truncate(CodeGen *g, IrExecutableGen *executable,
}
}
static LLVMValueRef ir_render_memset(CodeGen *g, IrExecutableGen *executable, IrInstGenMemset *instruction) {
static LLVMValueRef ir_render_memset(CodeGen *g, Stage1Air *executable, IrInstGenMemset *instruction) {
LLVMValueRef dest_ptr = ir_llvm_value(g, instruction->dest_ptr);
LLVMValueRef len_val = ir_llvm_value(g, instruction->count);
@ -5699,7 +5709,7 @@ static LLVMValueRef ir_render_memset(CodeGen *g, IrExecutableGen *executable, Ir
return nullptr;
}
static LLVMValueRef ir_render_memcpy(CodeGen *g, IrExecutableGen *executable, IrInstGenMemcpy *instruction) {
static LLVMValueRef ir_render_memcpy(CodeGen *g, Stage1Air *executable, IrInstGenMemcpy *instruction) {
LLVMValueRef dest_ptr = ir_llvm_value(g, instruction->dest_ptr);
LLVMValueRef src_ptr = ir_llvm_value(g, instruction->src_ptr);
LLVMValueRef len_val = ir_llvm_value(g, instruction->count);
@ -5721,14 +5731,14 @@ static LLVMValueRef ir_render_memcpy(CodeGen *g, IrExecutableGen *executable, Ir
return nullptr;
}
static LLVMValueRef ir_render_wasm_memory_size(CodeGen *g, IrExecutableGen *executable, IrInstGenWasmMemorySize *instruction) {
static LLVMValueRef ir_render_wasm_memory_size(CodeGen *g, Stage1Air *executable, IrInstGenWasmMemorySize *instruction) {
// TODO adjust for wasm64
LLVMValueRef param = ir_llvm_value(g, instruction->index);
LLVMValueRef val = LLVMBuildCall(g->builder, gen_wasm_memory_size(g), &param, 1, "");
return val;
}
static LLVMValueRef ir_render_wasm_memory_grow(CodeGen *g, IrExecutableGen *executable, IrInstGenWasmMemoryGrow *instruction) {
static LLVMValueRef ir_render_wasm_memory_grow(CodeGen *g, Stage1Air *executable, IrInstGenWasmMemoryGrow *instruction) {
// TODO adjust for wasm64
LLVMValueRef params[] = {
ir_llvm_value(g, instruction->index),
@ -5738,7 +5748,7 @@ static LLVMValueRef ir_render_wasm_memory_grow(CodeGen *g, IrExecutableGen *exec
return val;
}
static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutableGen *executable, IrInstGenSlice *instruction) {
static LLVMValueRef ir_render_slice(CodeGen *g, Stage1Air *executable, IrInstGenSlice *instruction) {
Error err;
LLVMValueRef array_ptr_ptr = ir_llvm_value(g, instruction->ptr);
@ -5975,12 +5985,12 @@ static LLVMValueRef get_trap_fn_val(CodeGen *g) {
}
static LLVMValueRef ir_render_breakpoint(CodeGen *g, IrExecutableGen *executable, IrInstGenBreakpoint *instruction) {
static LLVMValueRef ir_render_breakpoint(CodeGen *g, Stage1Air *executable, IrInstGenBreakpoint *instruction) {
LLVMBuildCall(g->builder, get_trap_fn_val(g), nullptr, 0, "");
return nullptr;
}
static LLVMValueRef ir_render_return_address(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_return_address(CodeGen *g, Stage1Air *executable,
IrInstGenReturnAddress *instruction)
{
if (target_is_wasm(g->zig_target) && g->zig_target->os != OsEmscripten) {
@ -6008,7 +6018,7 @@ static LLVMValueRef get_frame_address_fn_val(CodeGen *g) {
return g->frame_address_fn_val;
}
static LLVMValueRef ir_render_frame_address(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_frame_address(CodeGen *g, Stage1Air *executable,
IrInstGenFrameAddress *instruction)
{
LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_i32->llvm_type);
@ -6016,7 +6026,7 @@ static LLVMValueRef ir_render_frame_address(CodeGen *g, IrExecutableGen *executa
return LLVMBuildPtrToInt(g->builder, ptr_val, g->builtin_types.entry_usize->llvm_type, "");
}
static LLVMValueRef ir_render_handle(CodeGen *g, IrExecutableGen *executable, IrInstGenFrameHandle *instruction) {
static LLVMValueRef ir_render_handle(CodeGen *g, Stage1Air *executable, IrInstGenFrameHandle *instruction) {
return g->cur_frame_ptr;
}
@ -6045,7 +6055,7 @@ static LLVMValueRef render_shl_with_overflow(CodeGen *g, IrInstGenOverflowOp *in
return overflow_bit;
}
static LLVMValueRef ir_render_overflow_op(CodeGen *g, IrExecutableGen *executable, IrInstGenOverflowOp *instruction) {
static LLVMValueRef ir_render_overflow_op(CodeGen *g, Stage1Air *executable, IrInstGenOverflowOp *instruction) {
AddSubMul add_sub_mul;
switch (instruction->op) {
case IrOverflowOpAdd:
@ -6083,7 +6093,7 @@ static LLVMValueRef ir_render_overflow_op(CodeGen *g, IrExecutableGen *executabl
return overflow_bit;
}
static LLVMValueRef ir_render_test_err(CodeGen *g, IrExecutableGen *executable, IrInstGenTestErr *instruction) {
static LLVMValueRef ir_render_test_err(CodeGen *g, Stage1Air *executable, IrInstGenTestErr *instruction) {
ZigType *err_union_type = instruction->err_union->value->type;
ZigType *payload_type = err_union_type->data.error_union.payload_type;
LLVMValueRef err_union_handle = ir_llvm_value(g, instruction->err_union);
@ -6100,7 +6110,7 @@ static LLVMValueRef ir_render_test_err(CodeGen *g, IrExecutableGen *executable,
return LLVMBuildICmp(g->builder, LLVMIntNE, err_val, zero, "");
}
static LLVMValueRef ir_render_unwrap_err_code(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_unwrap_err_code(CodeGen *g, Stage1Air *executable,
IrInstGenUnwrapErrCode *instruction)
{
if (instruction->base.value->special != ConstValSpecialRuntime)
@ -6120,7 +6130,7 @@ static LLVMValueRef ir_render_unwrap_err_code(CodeGen *g, IrExecutableGen *execu
}
}
static LLVMValueRef ir_render_unwrap_err_payload(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_unwrap_err_payload(CodeGen *g, Stage1Air *executable,
IrInstGenUnwrapErrPayload *instruction)
{
Error err;
@ -6189,7 +6199,7 @@ static LLVMValueRef ir_render_unwrap_err_payload(CodeGen *g, IrExecutableGen *ex
}
}
static LLVMValueRef ir_render_optional_wrap(CodeGen *g, IrExecutableGen *executable, IrInstGenOptionalWrap *instruction) {
static LLVMValueRef ir_render_optional_wrap(CodeGen *g, Stage1Air *executable, IrInstGenOptionalWrap *instruction) {
ZigType *wanted_type = instruction->base.value->type;
assert(wanted_type->id == ZigTypeIdOptional);
@ -6225,7 +6235,7 @@ static LLVMValueRef ir_render_optional_wrap(CodeGen *g, IrExecutableGen *executa
return result_loc;
}
static LLVMValueRef ir_render_err_wrap_code(CodeGen *g, IrExecutableGen *executable, IrInstGenErrWrapCode *instruction) {
static LLVMValueRef ir_render_err_wrap_code(CodeGen *g, Stage1Air *executable, IrInstGenErrWrapCode *instruction) {
ZigType *wanted_type = instruction->base.value->type;
assert(wanted_type->id == ZigTypeIdErrorUnion);
@ -6245,7 +6255,7 @@ static LLVMValueRef ir_render_err_wrap_code(CodeGen *g, IrExecutableGen *executa
return result_loc;
}
static LLVMValueRef ir_render_err_wrap_payload(CodeGen *g, IrExecutableGen *executable, IrInstGenErrWrapPayload *instruction) {
static LLVMValueRef ir_render_err_wrap_payload(CodeGen *g, Stage1Air *executable, IrInstGenErrWrapPayload *instruction) {
ZigType *wanted_type = instruction->base.value->type;
assert(wanted_type->id == ZigTypeIdErrorUnion);
@ -6276,7 +6286,7 @@ static LLVMValueRef ir_render_err_wrap_payload(CodeGen *g, IrExecutableGen *exec
return result_loc;
}
static LLVMValueRef ir_render_union_tag(CodeGen *g, IrExecutableGen *executable, IrInstGenUnionTag *instruction) {
static LLVMValueRef ir_render_union_tag(CodeGen *g, Stage1Air *executable, IrInstGenUnionTag *instruction) {
ZigType *union_type = instruction->value->value->type;
ZigType *tag_type = union_type->data.unionation.tag_type;
@ -6294,14 +6304,14 @@ static LLVMValueRef ir_render_union_tag(CodeGen *g, IrExecutableGen *executable,
return get_handle_value(g, tag_field_ptr, tag_type, ptr_type);
}
static LLVMValueRef ir_render_panic(CodeGen *g, IrExecutableGen *executable, IrInstGenPanic *instruction) {
static LLVMValueRef ir_render_panic(CodeGen *g, Stage1Air *executable, IrInstGenPanic *instruction) {
bool is_llvm_alloca;
LLVMValueRef err_ret_trace_val = get_cur_err_ret_trace_val(g, instruction->base.base.scope, &is_llvm_alloca);
gen_panic(g, ir_llvm_value(g, instruction->msg), err_ret_trace_val, is_llvm_alloca);
return nullptr;
}
static LLVMValueRef ir_render_atomic_rmw(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_atomic_rmw(CodeGen *g, Stage1Air *executable,
IrInstGenAtomicRmw *instruction)
{
bool is_signed;
@ -6353,7 +6363,7 @@ static LLVMValueRef ir_render_atomic_rmw(CodeGen *g, IrExecutableGen *executable
return LLVMBuildIntToPtr(g->builder, uncasted_result, get_llvm_type(g, operand_type), "");
}
static LLVMValueRef ir_render_atomic_load(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_atomic_load(CodeGen *g, Stage1Air *executable,
IrInstGenAtomicLoad *instruction)
{
LLVMAtomicOrdering ordering = to_LLVMAtomicOrdering(instruction->ordering);
@ -6374,7 +6384,7 @@ static LLVMValueRef ir_render_atomic_load(CodeGen *g, IrExecutableGen *executabl
return load_inst;
}
static LLVMValueRef ir_render_atomic_store(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_atomic_store(CodeGen *g, Stage1Air *executable,
IrInstGenAtomicStore *instruction)
{
LLVMAtomicOrdering ordering = to_LLVMAtomicOrdering(instruction->ordering);
@ -6397,13 +6407,13 @@ static LLVMValueRef ir_render_atomic_store(CodeGen *g, IrExecutableGen *executab
return nullptr;
}
static LLVMValueRef ir_render_float_op(CodeGen *g, IrExecutableGen *executable, IrInstGenFloatOp *instruction) {
static LLVMValueRef ir_render_float_op(CodeGen *g, Stage1Air *executable, IrInstGenFloatOp *instruction) {
LLVMValueRef operand = ir_llvm_value(g, instruction->operand);
LLVMValueRef fn_val = get_float_fn(g, instruction->base.value->type, ZigLLVMFnIdFloatOp, instruction->fn_id);
return LLVMBuildCall(g->builder, fn_val, &operand, 1, "");
}
static LLVMValueRef ir_render_mul_add(CodeGen *g, IrExecutableGen *executable, IrInstGenMulAdd *instruction) {
static LLVMValueRef ir_render_mul_add(CodeGen *g, Stage1Air *executable, IrInstGenMulAdd *instruction) {
LLVMValueRef op1 = ir_llvm_value(g, instruction->op1);
LLVMValueRef op2 = ir_llvm_value(g, instruction->op2);
LLVMValueRef op3 = ir_llvm_value(g, instruction->op3);
@ -6418,7 +6428,7 @@ static LLVMValueRef ir_render_mul_add(CodeGen *g, IrExecutableGen *executable, I
return LLVMBuildCall(g->builder, fn_val, args, 3, "");
}
static LLVMValueRef ir_render_bswap(CodeGen *g, IrExecutableGen *executable, IrInstGenBswap *instruction) {
static LLVMValueRef ir_render_bswap(CodeGen *g, Stage1Air *executable, IrInstGenBswap *instruction) {
LLVMValueRef op = ir_llvm_value(g, instruction->op);
ZigType *expr_type = instruction->base.value->type;
bool is_vector = expr_type->id == ZigTypeIdVector;
@ -6452,7 +6462,7 @@ static LLVMValueRef ir_render_bswap(CodeGen *g, IrExecutableGen *executable, IrI
return LLVMBuildTrunc(g->builder, shifted, get_llvm_type(g, expr_type), "");
}
static LLVMValueRef ir_render_extern(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_extern(CodeGen *g, Stage1Air *executable,
IrInstGenExtern *instruction)
{
ZigType *expr_type = instruction->base.value->type;
@ -6476,7 +6486,7 @@ static LLVMValueRef ir_render_extern(CodeGen *g, IrExecutableGen *executable,
return LLVMBuildBitCast(g->builder, global_value, get_llvm_type(g, expr_type), "");
}
static LLVMValueRef ir_render_bit_reverse(CodeGen *g, IrExecutableGen *executable, IrInstGenBitReverse *instruction) {
static LLVMValueRef ir_render_bit_reverse(CodeGen *g, Stage1Air *executable, IrInstGenBitReverse *instruction) {
LLVMValueRef op = ir_llvm_value(g, instruction->op);
ZigType *int_type = instruction->base.value->type;
assert(int_type->id == ZigTypeIdInt);
@ -6484,7 +6494,7 @@ static LLVMValueRef ir_render_bit_reverse(CodeGen *g, IrExecutableGen *executabl
return LLVMBuildCall(g->builder, fn_val, &op, 1, "");
}
static LLVMValueRef ir_render_vector_to_array(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_vector_to_array(CodeGen *g, Stage1Air *executable,
IrInstGenVectorToArray *instruction)
{
ZigType *array_type = instruction->base.value->type;
@ -6518,7 +6528,7 @@ static LLVMValueRef ir_render_vector_to_array(CodeGen *g, IrExecutableGen *execu
return result_loc;
}
static LLVMValueRef ir_render_array_to_vector(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_array_to_vector(CodeGen *g, Stage1Air *executable,
IrInstGenArrayToVector *instruction)
{
ZigType *vector_type = instruction->base.value->type;
@ -6555,7 +6565,7 @@ static LLVMValueRef ir_render_array_to_vector(CodeGen *g, IrExecutableGen *execu
}
}
static LLVMValueRef ir_render_assert_zero(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_assert_zero(CodeGen *g, Stage1Air *executable,
IrInstGenAssertZero *instruction)
{
LLVMValueRef target = ir_llvm_value(g, instruction->target);
@ -6566,7 +6576,7 @@ static LLVMValueRef ir_render_assert_zero(CodeGen *g, IrExecutableGen *executabl
return nullptr;
}
static LLVMValueRef ir_render_assert_non_null(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_assert_non_null(CodeGen *g, Stage1Air *executable,
IrInstGenAssertNonNull *instruction)
{
LLVMValueRef target = ir_llvm_value(g, instruction->target);
@ -6591,7 +6601,7 @@ static LLVMValueRef ir_render_assert_non_null(CodeGen *g, IrExecutableGen *execu
return nullptr;
}
static LLVMValueRef ir_render_suspend_begin(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_suspend_begin(CodeGen *g, Stage1Air *executable,
IrInstGenSuspendBegin *instruction)
{
if (fn_is_async(g->cur_fn)) {
@ -6600,7 +6610,7 @@ static LLVMValueRef ir_render_suspend_begin(CodeGen *g, IrExecutableGen *executa
return nullptr;
}
static LLVMValueRef ir_render_suspend_finish(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_suspend_finish(CodeGen *g, Stage1Air *executable,
IrInstGenSuspendFinish *instruction)
{
LLVMBuildRetVoid(g->builder);
@ -6652,7 +6662,7 @@ static LLVMValueRef gen_await_early_return(CodeGen *g, IrInstGen *source_instr,
}
}
static LLVMValueRef ir_render_await(CodeGen *g, IrExecutableGen *executable, IrInstGenAwait *instruction) {
static LLVMValueRef ir_render_await(CodeGen *g, Stage1Air *executable, IrInstGenAwait *instruction) {
LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->llvm_type;
LLVMValueRef zero = LLVMConstNull(usize_type_ref);
LLVMValueRef target_frame_ptr = ir_llvm_value(g, instruction->frame);
@ -6739,7 +6749,7 @@ static LLVMValueRef ir_render_await(CodeGen *g, IrExecutableGen *executable, IrI
return nullptr;
}
static LLVMValueRef ir_render_resume(CodeGen *g, IrExecutableGen *executable, IrInstGenResume *instruction) {
static LLVMValueRef ir_render_resume(CodeGen *g, Stage1Air *executable, IrInstGenResume *instruction) {
LLVMValueRef frame = ir_llvm_value(g, instruction->frame);
ZigType *frame_type = instruction->frame->value->type;
assert(frame_type->id == ZigTypeIdAnyFrame);
@ -6748,14 +6758,14 @@ static LLVMValueRef ir_render_resume(CodeGen *g, IrExecutableGen *executable, Ir
return nullptr;
}
static LLVMValueRef ir_render_frame_size(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_frame_size(CodeGen *g, Stage1Air *executable,
IrInstGenFrameSize *instruction)
{
LLVMValueRef fn_val = ir_llvm_value(g, instruction->fn);
return gen_frame_size(g, fn_val);
}
static LLVMValueRef ir_render_spill_begin(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_spill_begin(CodeGen *g, Stage1Air *executable,
IrInstGenSpillBegin *instruction)
{
if (!fn_is_async(g->cur_fn))
@ -6775,7 +6785,7 @@ static LLVMValueRef ir_render_spill_begin(CodeGen *g, IrExecutableGen *executabl
zig_unreachable();
}
static LLVMValueRef ir_render_spill_end(CodeGen *g, IrExecutableGen *executable, IrInstGenSpillEnd *instruction) {
static LLVMValueRef ir_render_spill_end(CodeGen *g, Stage1Air *executable, IrInstGenSpillEnd *instruction) {
if (!fn_is_async(g->cur_fn))
return ir_llvm_value(g, instruction->begin->operand);
@ -6791,7 +6801,7 @@ static LLVMValueRef ir_render_spill_end(CodeGen *g, IrExecutableGen *executable,
zig_unreachable();
}
static LLVMValueRef ir_render_vector_extract_elem(CodeGen *g, IrExecutableGen *executable,
static LLVMValueRef ir_render_vector_extract_elem(CodeGen *g, Stage1Air *executable,
IrInstGenVectorExtractElem *instruction)
{
LLVMValueRef vector = ir_llvm_value(g, instruction->vector);
@ -6806,11 +6816,11 @@ static void set_debug_location(CodeGen *g, IrInstGen *instruction) {
assert(source_node);
assert(scope);
ZigLLVMSetCurrentDebugLocation(g->builder, (int)source_node->line + 1,
(int)source_node->column + 1, get_di_scope(g, scope));
ZigLLVMSetCurrentDebugLocation(g->builder, node_line_onebased(source_node),
node_column_onebased(source_node), get_di_scope(g, scope));
}
static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutableGen *executable, IrInstGen *instruction) {
static LLVMValueRef ir_render_instruction(CodeGen *g, Stage1Air *executable, IrInstGen *instruction) {
switch (instruction->id) {
case IrInstGenIdInvalid:
case IrInstGenIdConst:
@ -6998,7 +7008,7 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutableGen *executabl
static void ir_render(CodeGen *g, ZigFn *fn_entry) {
assert(fn_entry);
IrExecutableGen *executable = &fn_entry->analyzed_executable;
Stage1Air *executable = &fn_entry->analyzed_executable;
assert(executable->basic_block_list.length > 0);
for (size_t block_i = 0; block_i < executable->basic_block_list.length; block_i += 1) {
@ -7996,7 +8006,7 @@ static void generate_error_name_table(CodeGen *g) {
}
static void build_all_basic_blocks(CodeGen *g, ZigFn *fn) {
IrExecutableGen *executable = &fn->analyzed_executable;
Stage1Air *executable = &fn->analyzed_executable;
assert(executable->basic_block_list.length > 0);
LLVMValueRef fn_val = fn_llvm_value(g, fn);
LLVMBasicBlockRef first_bb = nullptr;
@ -8030,7 +8040,7 @@ static void gen_global_var(CodeGen *g, ZigVar *var, LLVMValueRef init_val,
bool is_local_to_unit = true;
ZigLLVMCreateGlobalVariable(g->dbuilder, get_di_scope(g, var->parent_scope), var->name,
var->name, import->data.structure.root_struct->di_file,
(unsigned)(var->decl_node->line + 1),
node_line_onebased(var->decl_node),
get_llvm_di_type(g, type_entry), is_local_to_unit);
// TODO ^^ make an actual global variable
@ -8305,7 +8315,8 @@ static void do_code_gen(CodeGen *g) {
if (var->src_arg_index == SIZE_MAX) {
var->di_loc_var = ZigLLVMCreateAutoVariable(g->dbuilder, get_di_scope(g, var->parent_scope),
var->name, import->data.structure.root_struct->di_file, (unsigned)(var->decl_node->line + 1),
var->name, import->data.structure.root_struct->di_file,
node_line_onebased(var->decl_node),
get_llvm_di_type(g, var->var_type), !g->strip_debug_symbols, 0);
} else if (is_c_abi) {
@ -8330,7 +8341,7 @@ static void do_code_gen(CodeGen *g) {
if (var->decl_node) {
var->di_loc_var = ZigLLVMCreateParameterVariable(g->dbuilder, get_di_scope(g, var->parent_scope),
var->name, import->data.structure.root_struct->di_file,
(unsigned)(var->decl_node->line + 1),
node_line_onebased(var->decl_node),
get_llvm_di_type(g, gen_type), !g->strip_debug_symbols, 0, (unsigned)(gen_info->gen_index+1));
}
@ -8374,10 +8385,11 @@ static void do_code_gen(CodeGen *g) {
if (!g->strip_debug_symbols) {
AstNode *source_node = fn_table_entry->proto_node;
ZigLLVMSetCurrentDebugLocation(g->builder, (int)source_node->line + 1,
(int)source_node->column + 1, get_di_scope(g, fn_table_entry->child_scope));
ZigLLVMSetCurrentDebugLocation(g->builder,
node_line_onebased(source_node), node_column_onebased(source_node),
get_di_scope(g, fn_table_entry->child_scope));
}
IrExecutableGen *executable = &fn_table_entry->analyzed_executable;
Stage1Air *executable = &fn_table_entry->analyzed_executable;
LLVMBasicBlockRef bad_resume_block = LLVMAppendBasicBlock(g->cur_fn_val, "BadResume");
LLVMPositionBuilderAtEnd(g->builder, bad_resume_block);
gen_assertion_scope(g, PanicMsgIdBadResume, fn_table_entry->child_scope);

View File

@ -1084,19 +1084,43 @@ static void anal_dump_type(AnalDumpCtx *ctx, ZigType *ty) {
jw_end_object(jw);
}
static Buf *collect_doc_comments(RootStruct *root_struct, TokenIndex first_token) {
if (first_token == 0)
return nullptr;
TokenId *token_ids = root_struct->token_ids;
TokenLoc *token_locs = root_struct->token_locs;
Buf *str = buf_alloc();
const char *source = buf_ptr(root_struct->source_code);
TokenIndex doc_token = first_token;
for (;token_ids[doc_token] == TokenIdDocComment; doc_token += 1) {
// chops off '///' but leaves '\n'
uint32_t start_pos = token_locs[doc_token].offset;
uint32_t token_len = 0;
while (source[start_pos + token_len] != '\n' &&
source[start_pos + token_len] != 0)
{
token_len += 1;
}
buf_append_mem(str, source + start_pos + 3, token_len - 3);
}
return str;
}
static void anal_dump_node(AnalDumpCtx *ctx, const AstNode *node) {
JsonWriter *jw = &ctx->jw;
jw_begin_object(jw);
jw_object_field(jw, "file");
anal_dump_file_ref(ctx, node->owner->data.structure.root_struct->path);
RootStruct *root_struct = node->owner->data.structure.root_struct;
anal_dump_file_ref(ctx, root_struct->path);
jw_object_field(jw, "line");
jw_int(jw, node->line);
jw_int(jw, root_struct->token_locs[node->main_token].line);
jw_object_field(jw, "col");
jw_int(jw, node->column);
jw_int(jw, root_struct->token_locs[node->main_token].column);
const Buf *doc_comments_buf = nullptr;
const Buf *name_buf = nullptr;
@ -1107,30 +1131,30 @@ static void anal_dump_node(AnalDumpCtx *ctx, const AstNode *node) {
switch (node->type) {
case NodeTypeParamDecl:
doc_comments_buf = &node->data.param_decl.doc_comments;
doc_comments_buf = collect_doc_comments(root_struct, node->data.param_decl.doc_comments);
name_buf = node->data.param_decl.name;
is_var_args = node->data.param_decl.is_var_args;
is_noalias = node->data.param_decl.is_noalias;
is_comptime = node->data.param_decl.is_comptime;
break;
case NodeTypeFnProto:
doc_comments_buf = &node->data.fn_proto.doc_comments;
doc_comments_buf = collect_doc_comments(root_struct, node->data.fn_proto.doc_comments);
field_nodes = &node->data.fn_proto.params;
is_var_args = node->data.fn_proto.is_var_args;
break;
case NodeTypeVariableDeclaration:
doc_comments_buf = &node->data.variable_declaration.doc_comments;
doc_comments_buf = collect_doc_comments(root_struct, node->data.variable_declaration.doc_comments);
break;
case NodeTypeErrorSetField:
doc_comments_buf = &node->data.err_set_field.doc_comments;
doc_comments_buf = collect_doc_comments(root_struct, node->data.err_set_field.doc_comments);
break;
case NodeTypeStructField:
doc_comments_buf = &node->data.struct_field.doc_comments;
doc_comments_buf = collect_doc_comments(root_struct, node->data.struct_field.doc_comments);
name_buf = node->data.struct_field.name;
break;
case NodeTypeContainerDecl:
field_nodes = &node->data.container_decl.fields;
doc_comments_buf = &node->data.container_decl.doc_comments;
doc_comments_buf = collect_doc_comments(root_struct, node->data.container_decl.doc_comments);
break;
default:
break;

View File

@ -96,14 +96,14 @@ void err_msg_add_note(ErrorMsg *parent, ErrorMsg *note) {
parent->notes.append(note);
}
ErrorMsg *err_msg_create_with_offset(Buf *path, size_t line, size_t column, size_t offset,
const char *source, Buf *msg)
ErrorMsg *err_msg_create_with_offset(Buf *path, uint32_t byte_offset, const char *source,
Buf *msg)
{
ErrorMsg *err_msg = heap::c_allocator.create<ErrorMsg>();
err_msg->path = path;
err_msg->line_start = line;
err_msg->column_start = column;
err_msg->msg = msg;
err_msg->line_start = 0;
err_msg->column_start = 0;
if (source == nullptr) {
// Must initialize the buffer anyway
@ -111,46 +111,25 @@ ErrorMsg *err_msg_create_with_offset(Buf *path, size_t line, size_t column, size
return err_msg;
}
size_t line_start_offset = offset;
for (;;) {
if (line_start_offset == 0) {
break;
}
line_start_offset -= 1;
if (source[line_start_offset] == '\n') {
line_start_offset += 1;
break;
size_t line_start = 0;
size_t i = 0;
for (;i < byte_offset; i += 1) {
switch (source[i]) {
case '\n':
err_msg->line_start += 1;
err_msg->column_start = 0;
line_start = i + 1;
continue;
default:
err_msg->column_start += 1;
continue;
}
}
size_t line_end_offset = offset;
while (source[line_end_offset] && source[line_end_offset] != '\n') {
line_end_offset += 1;
while (source[i] != '\n' && source[i] != 0) {
i += 1;
}
buf_init_from_mem(&err_msg->line_buf, source + line_start_offset, line_end_offset - line_start_offset);
return err_msg;
}
ErrorMsg *err_msg_create_with_line(Buf *path, size_t line, size_t column,
Buf *source, ZigList<size_t> *line_offsets, Buf *msg)
{
ErrorMsg *err_msg = heap::c_allocator.create<ErrorMsg>();
err_msg->path = path;
err_msg->line_start = line;
err_msg->column_start = column;
err_msg->msg = msg;
size_t line_start_offset = line_offsets->at(line);
size_t end_line = line + 1;
size_t line_end_offset = (end_line >= line_offsets->length) ? buf_len(source) : line_offsets->at(line + 1);
size_t len = (line_end_offset + 1 > line_start_offset) ? (line_end_offset - line_start_offset - 1) : 0;
if (len == SIZE_MAX) len = 0;
buf_init_from_mem(&err_msg->line_buf, buf_ptr(source) + line_start_offset, len);
buf_init_from_mem(&err_msg->line_buf, source + line_start, i - line_start);
return err_msg;
}

View File

@ -25,10 +25,6 @@ struct ErrorMsg {
void print_err_msg(ErrorMsg *msg, ErrColor color);
void err_msg_add_note(ErrorMsg *parent, ErrorMsg *note);
ErrorMsg *err_msg_create_with_offset(Buf *path, size_t line, size_t column, size_t offset,
const char *source, Buf *msg);
ErrorMsg *err_msg_create_with_line(Buf *path, size_t line, size_t column,
Buf *source, ZigList<size_t> *line_offsets, Buf *msg);
ErrorMsg *err_msg_create_with_offset(Buf *path, uint32_t byte_offset, const char *source, Buf *msg);
#endif

View File

@ -88,6 +88,8 @@ const char *err_str(Error err) {
case ErrorZigIsTheCCompiler: return "Zig was not provided with libc installation information, and so it does not know where the libc paths are on the system. Zig attempted to use the system C compiler to find out where the libc paths are, but discovered that Zig is being used as the system C compiler.";
case ErrorFileBusy: return "file is busy";
case ErrorLocked: return "file is locked by another process";
case ErrorInvalidCharacter: return "invalid character";
case ErrorUnicodePointTooLarge: return "unicode codepoint too large";
}
return "(invalid error)";
}

File diff suppressed because it is too large Load Diff

View File

@ -10,31 +10,25 @@
#include "all_types.hpp"
bool ir_gen(CodeGen *g, AstNode *node, Scope *scope, IrExecutableSrc *ir_executable);
bool ir_gen_fn(CodeGen *g, ZigFn *fn_entry);
IrInstGen *ir_create_alloca(CodeGen *g, Scope *scope, AstNode *source_node, ZigFn *fn,
ZigType *var_type, const char *name_hint);
Error ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,
ZigValue *return_ptr, size_t *backward_branch_count, size_t *backward_branch_quota,
ZigFn *fn_entry, Buf *c_import_buf, AstNode *source_node, Buf *exec_name,
IrExecutableGen *parent_exec, AstNode *expected_type_source_node, UndefAllowed undef);
Stage1Air *parent_exec, AstNode *expected_type_source_node, UndefAllowed undef);
Error ir_resolve_lazy(CodeGen *codegen, AstNode *source_node, ZigValue *val);
ZigType *ir_analyze(CodeGen *g, IrExecutableSrc *old_executable, IrExecutableGen *new_executable,
ZigType *expected_type, AstNode *expected_type_source_node, ZigValue *return_ptr);
ZigType *ir_analyze(CodeGen *codegen, Stage1Zir *stage1_zir, Stage1Air *stage1_air,
size_t *backward_branch_count, size_t *backward_branch_quota,
ZigType *expected_type, AstNode *expected_type_source_node, ZigValue *result_ptr,
ZigFn *fn);
bool ir_inst_gen_has_side_effects(IrInstGen *inst);
bool ir_inst_src_has_side_effects(IrInstSrc *inst);
struct IrAnalyze;
ZigValue *const_ptr_pointee(IrAnalyze *ira, CodeGen *codegen, ZigValue *const_val,
AstNode *source_node);
// for debugging purposes
void dbg_ir_break(const char *src_file, uint32_t line);
void dbg_ir_clear(void);
#endif

View File

@ -8,6 +8,7 @@
#include "all_types.hpp"
#include "analyze.hpp"
#include "ir.hpp"
#include "astgen.hpp"
#include "ir_print.hpp"
#include "os.hpp"
@ -632,7 +633,7 @@ static void ir_print_other_inst_gen(IrPrintGen *irp, IrInstGen *inst) {
}
}
static void ir_print_other_block(IrPrintSrc *irp, IrBasicBlockSrc *bb) {
static void ir_print_other_block(IrPrintSrc *irp, Stage1ZirBasicBlock *bb) {
if (bb == nullptr) {
fprintf(irp->f, "(null block)");
} else {
@ -1005,7 +1006,7 @@ static void ir_print_phi(IrPrintSrc *irp, IrInstSrcPhi *phi_instruction) {
assert(phi_instruction->incoming_count != 0);
assert(phi_instruction->incoming_count != SIZE_MAX);
for (size_t i = 0; i < phi_instruction->incoming_count; i += 1) {
IrBasicBlockSrc *incoming_block = phi_instruction->incoming_blocks[i];
Stage1ZirBasicBlock *incoming_block = phi_instruction->incoming_blocks[i];
IrInstSrc *incoming_value = phi_instruction->incoming_values[i];
if (i != 0)
fprintf(irp->f, " ");
@ -3349,7 +3350,7 @@ static void ir_print_inst_gen(IrPrintGen *irp, IrInstGen *instruction, bool trai
fprintf(irp->f, "\n");
}
static void irp_print_basic_block_src(IrPrintSrc *irp, IrBasicBlockSrc *current_block) {
static void irp_print_basic_block_src(IrPrintSrc *irp, Stage1ZirBasicBlock *current_block) {
fprintf(irp->f, "%s_%" PRIu32 ":\n", current_block->name_hint, current_block->debug_id);
for (size_t instr_i = 0; instr_i < current_block->instruction_list.length; instr_i += 1) {
IrInstSrc *instruction = current_block->instruction_list.at(instr_i);
@ -3369,7 +3370,7 @@ static void irp_print_basic_block_gen(IrPrintGen *irp, IrBasicBlockGen *current_
}
}
void ir_print_basic_block_src(CodeGen *codegen, FILE *f, IrBasicBlockSrc *bb, int indent_size) {
void ir_print_basic_block_src(CodeGen *codegen, FILE *f, Stage1ZirBasicBlock *bb, int indent_size) {
IrPrintSrc ir_print = {};
ir_print.codegen = codegen;
ir_print.f = f;
@ -3395,7 +3396,7 @@ void ir_print_basic_block_gen(CodeGen *codegen, FILE *f, IrBasicBlockGen *bb, in
ir_print.printed.deinit();
}
void ir_print_src(CodeGen *codegen, FILE *f, IrExecutableSrc *executable, int indent_size) {
void ir_print_src(CodeGen *codegen, FILE *f, Stage1Zir *executable, int indent_size) {
IrPrintSrc ir_print = {};
IrPrintSrc *irp = &ir_print;
irp->codegen = codegen;
@ -3408,7 +3409,7 @@ void ir_print_src(CodeGen *codegen, FILE *f, IrExecutableSrc *executable, int in
}
}
void ir_print_gen(CodeGen *codegen, FILE *f, IrExecutableGen *executable, int indent_size) {
void ir_print_gen(CodeGen *codegen, FILE *f, Stage1Air *executable, int indent_size) {
IrPrintGen ir_print = {};
IrPrintGen *irp = &ir_print;
irp->codegen = codegen;

View File

@ -12,11 +12,11 @@
#include <stdio.h>
void ir_print_src(CodeGen *codegen, FILE *f, IrExecutableSrc *executable, int indent_size);
void ir_print_gen(CodeGen *codegen, FILE *f, IrExecutableGen *executable, int indent_size);
void ir_print_src(CodeGen *codegen, FILE *f, Stage1Zir *executable, int indent_size);
void ir_print_gen(CodeGen *codegen, FILE *f, Stage1Air *executable, int indent_size);
void ir_print_inst_src(CodeGen *codegen, FILE *f, IrInstSrc *inst, int indent_size);
void ir_print_inst_gen(CodeGen *codegen, FILE *f, IrInstGen *inst, int indent_size);
void ir_print_basic_block_src(CodeGen *codegen, FILE *f, IrBasicBlockSrc *bb, int indent_size);
void ir_print_basic_block_src(CodeGen *codegen, FILE *f, Stage1ZirBasicBlock *bb, int indent_size);
void ir_print_basic_block_gen(CodeGen *codegen, FILE *f, IrBasicBlockGen *bb, int indent_size);
const char* ir_inst_src_type_str(IrInstSrcId id);

File diff suppressed because it is too large Load Diff

View File

@ -12,14 +12,19 @@
#include "tokenizer.hpp"
#include "errmsg.hpp"
ATTRIBUTE_PRINTF(2, 3)
void ast_token_error(Token *token, const char *format, ...);
AstNode * ast_parse(Buf *buf, ZigList<Token> *tokens, ZigType *owner, ErrColor err_color);
AstNode * ast_parse(Buf *buf, ZigType *owner, ErrColor err_color);
void ast_print(AstNode *node, int indent);
void ast_visit_node_children(AstNode *node, void (*visit)(AstNode **, void *context), void *context);
Buf *node_identifier_buf(AstNode *node);
Buf *token_identifier_buf(RootStruct *root_struct, TokenIndex token);
void token_number_literal_bigint(RootStruct *root_struct, BigInt *result, TokenIndex token);
Error source_string_literal_buf(const char *source, Buf *out, size_t *bad_index);
Error source_char_literal(const char *source, uint32_t *out, size_t *bad_index);
#endif

View File

@ -112,6 +112,8 @@ enum Error {
ErrorZigIsTheCCompiler,
ErrorFileBusy,
ErrorLocked,
ErrorInvalidCharacter,
ErrorUnicodePointTooLarge,
};
// ABI warning

File diff suppressed because it is too large Load Diff

View File

@ -12,10 +12,9 @@
#include "bigint.hpp"
#include "bigfloat.hpp"
enum TokenId {
enum TokenId : uint8_t {
TokenIdAmpersand,
TokenIdArrow,
TokenIdAtSign,
TokenIdBang,
TokenIdBarBar,
TokenIdBinOr,
@ -27,6 +26,7 @@ enum TokenId {
TokenIdBitShiftRight,
TokenIdBitShiftRightEq,
TokenIdBitXorEq,
TokenIdBuiltin,
TokenIdCharLiteral,
TokenIdCmpEq,
TokenIdCmpGreaterOrEq,
@ -109,9 +109,7 @@ enum TokenId {
TokenIdMinusPercent,
TokenIdMinusPercentEq,
TokenIdModEq,
TokenIdNumberSign,
TokenIdPercent,
TokenIdPercentDot,
TokenIdPlus,
TokenIdPlusEq,
TokenIdPlusPercent,
@ -125,75 +123,35 @@ enum TokenId {
TokenIdStar,
TokenIdStarStar,
TokenIdStringLiteral,
TokenIdMultilineStringLiteral,
TokenIdSymbol,
TokenIdMultilineStringLiteralLine,
TokenIdIdentifier,
TokenIdTilde,
TokenIdTimesEq,
TokenIdTimesPercent,
TokenIdTimesPercentEq,
TokenIdCount,
};
struct TokenFloatLit {
BigFloat bigfloat;
// overflow is true if when parsing the number, we discovered it would not fit
// without losing data
bool overflow;
typedef uint32_t TokenIndex;
struct TokenLoc {
uint32_t offset;
uint32_t line;
uint32_t column;
};
struct TokenIntLit {
BigInt bigint;
};
struct TokenStrLit {
Buf str;
};
struct TokenCharLit {
uint32_t c;
};
struct Token {
TokenId id;
size_t start_pos;
size_t end_pos;
size_t start_line;
size_t start_column;
union {
// TokenIdIntLiteral
TokenIntLit int_lit;
// TokenIdFloatLiteral
TokenFloatLit float_lit;
// TokenIdStringLiteral, TokenIdMultilineStringLiteral or TokenIdSymbol
TokenStrLit str_lit;
// TokenIdCharLiteral
TokenCharLit char_lit;
} data;
};
// work around conflicting name Token which is also found in libclang
typedef Token ZigToken;
struct Tokenization {
ZigList<Token> *tokens;
ZigList<size_t> *line_offsets;
ZigList<TokenId> ids;
ZigList<TokenLoc> locs;
// if an error occurred
Buf *err;
size_t err_line;
size_t err_column;
uint32_t err_byte_offset;
};
void tokenize(Buf *buf, Tokenization *out_tokenization);
void print_tokens(Buf *buf, ZigList<Token> *tokens);
void tokenize(const char *source, Tokenization *out_tokenization);
const char * token_name(TokenId id);
bool valid_symbol_starter(uint8_t c);
bool is_zig_keyword(Buf *buf);
#endif

View File

@ -1,6 +1,7 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqualSlices = std.testing.expectEqualSlices;
const expectEqualStrings = std.testing.expectEqualStrings;
const mem = std.mem;
const builtin = @import("builtin");
@ -147,13 +148,13 @@ test "array mult operator" {
}
test "string escapes" {
try expect(mem.eql(u8, "\"", "\x22"));
try expect(mem.eql(u8, "\'", "\x27"));
try expect(mem.eql(u8, "\n", "\x0a"));
try expect(mem.eql(u8, "\r", "\x0d"));
try expect(mem.eql(u8, "\t", "\x09"));
try expect(mem.eql(u8, "\\", "\x5c"));
try expect(mem.eql(u8, "\u{1234}\u{069}\u{1}", "\xe1\x88\xb4\x69\x01"));
try expectEqualStrings("\"", "\x22");
try expectEqualStrings("\'", "\x27");
try expectEqualStrings("\n", "\x0a");
try expectEqualStrings("\r", "\x0d");
try expectEqualStrings("\t", "\x09");
try expectEqualStrings("\\", "\x5c");
try expectEqualStrings("\u{1234}\u{069}\u{1}", "\xe1\x88\xb4\x69\x01");
}
test "multiline string" {

View File

@ -34,9 +34,9 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\ const c: *S = &.{};
\\}
, &[_][]const u8{
"mp.zig:2:31: error: expected type '[][]const u8', found '*const struct:2:31'",
"mp.zig:5:33: error: expected type '*[2][]const u8', found '*const struct:5:33'",
"mp.zig:9:21: error: expected type '*S', found '*const struct:9:21'",
"tmp.zig:2:31: error: expected type '[][]const u8', found '*const struct:2:31'",
"tmp.zig:5:33: error: expected type '*[2][]const u8', found '*const struct:5:33'",
"tmp.zig:9:21: error: expected type '*S', found '*const struct:9:21'",
});
cases.add("@Type() union payload is undefined",
@ -8416,6 +8416,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\ var sequence = "repeat".*** 10;
\\}
, &[_][]const u8{
"tmp.zig:2:30: error: `.*` can't be followed by `*`. Are you missing a space?",
"tmp.zig:2:30: error: `.*` cannot be followed by `*`. Are you missing a space?",
});
}