commit
7d494b3e7b
@ -3844,6 +3844,25 @@ pub fn main() void {
|
||||
{#header_open|@ArgType#}
|
||||
<p>TODO</p>
|
||||
{#header_close#}
|
||||
{#header_open|@atomicRmw#}
|
||||
<pre><code class="zig">@atomicRmw(comptime T: type, ptr: &T, comptime op: builtin.AtomicRmwOp, operand: T, comptime ordering: builtin.AtomicOrder) -> T</code></pre>
|
||||
<p>
|
||||
This builtin function atomically modifies memory and then returns the previous value.
|
||||
</p>
|
||||
<p>
|
||||
<code>T</code> must be a pointer type, a <code>bool</code>,
|
||||
or an integer whose bit count meets these requirements:
|
||||
</p>
|
||||
<ul>
|
||||
<li>At least 8</li>
|
||||
<li>At most the same as usize</li>
|
||||
<li>Power of 2</li>
|
||||
</ul>
|
||||
<p>
|
||||
TODO right now bool is not accepted. Also I think we could make non powers of 2 work fine, maybe
|
||||
we can remove this restriction
|
||||
</p>
|
||||
{#header_close#}
|
||||
{#header_open|@bitCast#}
|
||||
<pre><code class="zig">@bitCast(comptime DestType: type, value: var) -> DestType</code></pre>
|
||||
<p>
|
||||
@ -5714,7 +5733,7 @@ UseDecl = "use" Expression ";"
|
||||
|
||||
ExternDecl = "extern" option(String) (FnProto | VariableDeclaration) ";"
|
||||
|
||||
FnProto = option("nakedcc" | "stdcallcc" | "extern") "fn" option(Symbol) ParamDeclList option("align" "(" Expression ")") option("section" "(" Expression ")") option("!") TypeExpr
|
||||
FnProto = option("nakedcc" | "stdcallcc" | "extern" | ("async" option("(" Expression ")"))) "fn" option(Symbol) ParamDeclList option("align" "(" Expression ")") option("section" "(" Expression ")") option("!") TypeExpr
|
||||
|
||||
FnDef = option("inline" | "export") FnProto Block
|
||||
|
||||
@ -5732,7 +5751,7 @@ ErrorSetExpr = (PrefixOpExpression "!" PrefixOpExpression) | PrefixOpExpression
|
||||
|
||||
BlockOrExpression = Block | Expression
|
||||
|
||||
Expression = TryExpression | ReturnExpression | BreakExpression | AssignmentExpression
|
||||
Expression = TryExpression | ReturnExpression | BreakExpression | AssignmentExpression | CancelExpression | ResumeExpression
|
||||
|
||||
AsmExpression = "asm" option("volatile") "(" String option(AsmOutput) ")"
|
||||
|
||||
@ -5756,7 +5775,7 @@ AssignmentExpression = UnwrapExpression AssignmentOperator UnwrapExpression | Un
|
||||
|
||||
AssignmentOperator = "=" | "*=" | "/=" | "%=" | "+=" | "-=" | "<<=" | ">>=" | "&=" | "^=" | "|=" | "*%=" | "+%=" | "-%="
|
||||
|
||||
BlockExpression(body) = Block | IfExpression(body) | IfErrorExpression(body) | TestExpression(body) | WhileExpression(body) | ForExpression(body) | SwitchExpression | CompTimeExpression(body)
|
||||
BlockExpression(body) = Block | IfExpression(body) | IfErrorExpression(body) | TestExpression(body) | WhileExpression(body) | ForExpression(body) | SwitchExpression | CompTimeExpression(body) | SuspendExpression(body)
|
||||
|
||||
CompTimeExpression(body) = "comptime" body
|
||||
|
||||
@ -5774,12 +5793,20 @@ ReturnExpression = "return" option(Expression)
|
||||
|
||||
TryExpression = "try" Expression
|
||||
|
||||
AwaitExpression = "await" Expression
|
||||
|
||||
BreakExpression = "break" option(":" Symbol) option(Expression)
|
||||
|
||||
CancelExpression = "cancel" Expression;
|
||||
|
||||
ResumeExpression = "resume" Expression;
|
||||
|
||||
Defer(body) = ("defer" | "deferror") body
|
||||
|
||||
IfExpression(body) = "if" "(" Expression ")" body option("else" BlockExpression(body))
|
||||
|
||||
SuspendExpression(body) = "suspend" option(("|" Symbol "|" body))
|
||||
|
||||
IfErrorExpression(body) = "if" "(" Expression ")" option("|" option("*") Symbol "|") body "else" "|" Symbol "|" BlockExpression(body)
|
||||
|
||||
TestExpression(body) = "if" "(" Expression ")" option("|" option("*") Symbol "|") body option("else" BlockExpression(body))
|
||||
@ -5814,7 +5841,7 @@ MultiplyOperator = "||" | "*" | "/" | "%" | "**" | "*%"
|
||||
|
||||
PrefixOpExpression = PrefixOp ErrorSetExpr | SuffixOpExpression
|
||||
|
||||
SuffixOpExpression = PrimaryExpression option(FnCallExpression | ArrayAccessExpression | FieldAccessExpression | SliceExpression)
|
||||
SuffixOpExpression = ("async" option("(" Expression ")") PrimaryExpression FnCallExpression) | PrimaryExpression option(FnCallExpression | ArrayAccessExpression | FieldAccessExpression | SliceExpression)
|
||||
|
||||
FieldAccessExpression = "." Symbol
|
||||
|
||||
@ -5830,7 +5857,7 @@ ContainerInitBody = list(StructLiteralField, ",") | list(Expression, ",")
|
||||
|
||||
StructLiteralField = "." Symbol "=" Expression
|
||||
|
||||
PrefixOp = "!" | "-" | "~" | "*" | ("&" option("align" "(" Expression option(":" Integer ":" Integer) ")" ) option("const") option("volatile")) | "?" | "??" | "-%" | "try"
|
||||
PrefixOp = "!" | "-" | "~" | "*" | ("&" option("align" "(" Expression option(":" Integer ":" Integer) ")" ) option("const") option("volatile")) | "?" | "??" | "-%" | "try" | "await"
|
||||
|
||||
PrimaryExpression = Integer | Float | String | CharLiteral | KeywordLiteral | GroupedExpression | BlockExpression(BlockOrExpression) | Symbol | ("@" Symbol FnCallExpression) | ArrayType | FnProto | AsmExpression | ContainerDecl | ("continue" option(":" Symbol)) | ErrorSetDecl
|
||||
|
||||
@ -5838,7 +5865,7 @@ ArrayType : "[" option(Expression) "]" option("align" "(" Expression option(":"
|
||||
|
||||
GroupedExpression = "(" Expression ")"
|
||||
|
||||
KeywordLiteral = "true" | "false" | "null" | "undefined" | "error" | "this" | "unreachable"
|
||||
KeywordLiteral = "true" | "false" | "null" | "undefined" | "error" | "this" | "unreachable" | "suspend"
|
||||
|
||||
ErrorSetDecl = "error" "{" list(Symbol, ",") "}"
|
||||
|
||||
@ -5922,7 +5949,7 @@ hljs.registerLanguage("zig", function(t) {
|
||||
a = t.IR + "\\s*\\(",
|
||||
c = {
|
||||
keyword: "const align var extern stdcallcc nakedcc volatile export pub noalias inline struct packed enum union break return try catch test continue unreachable comptime and or asm defer errdefer if else switch while for fn use bool f32 f64 void type noreturn error i8 u8 i16 u16 i32 u32 i64 u64 isize usize i8w u8w i16w i32w u32w i64w u64w isizew usizew c_short c_ushort c_int c_uint c_long c_ulong c_longlong c_ulonglong",
|
||||
built_in: "breakpoint returnAddress frameAddress fieldParentPtr setFloatMode IntType OpaqueType compileError compileLog setCold setRuntimeSafety setEvalBranchQuota offsetOf memcpy inlineCall setGlobalLinkage setGlobalSection divTrunc divFloor enumTagName intToPtr ptrToInt panic canImplicitCast ptrCast bitCast rem mod memset sizeOf alignOf alignCast maxValue minValue memberCount memberName memberType typeOf addWithOverflow subWithOverflow mulWithOverflow shlWithOverflow shlExact shrExact cInclude cDefine cUndef ctz clz import cImport errorName embedFile cmpxchg fence divExact truncate",
|
||||
built_in: "breakpoint returnAddress frameAddress fieldParentPtr setFloatMode IntType OpaqueType compileError compileLog setCold setRuntimeSafety setEvalBranchQuota offsetOf memcpy inlineCall setGlobalLinkage setGlobalSection divTrunc divFloor enumTagName intToPtr ptrToInt panic canImplicitCast ptrCast bitCast rem mod memset sizeOf alignOf alignCast maxValue minValue memberCount memberName memberType typeOf addWithOverflow subWithOverflow mulWithOverflow shlWithOverflow shlExact shrExact cInclude cDefine cUndef ctz clz import cImport errorName embedFile cmpxchg fence divExact truncate atomicRmw",
|
||||
literal: "true false null undefined"
|
||||
},
|
||||
n = [e, t.CLCM, t.CBCM, s, r];
|
||||
|
@ -56,6 +56,16 @@ struct IrExecutable {
|
||||
IrAnalyze *analysis;
|
||||
Scope *begin_scope;
|
||||
ZigList<Tld *> tld_list;
|
||||
|
||||
IrInstruction *coro_handle;
|
||||
IrInstruction *coro_awaiter_field_ptr; // this one is shared and in the promise
|
||||
IrInstruction *coro_result_ptr_field_ptr;
|
||||
IrInstruction *await_handle_var_ptr; // this one is where we put the one we extracted from the promise
|
||||
IrBasicBlock *coro_early_final;
|
||||
IrBasicBlock *coro_normal_final;
|
||||
IrBasicBlock *coro_suspend_block;
|
||||
IrBasicBlock *coro_final_cleanup_block;
|
||||
VariableTableEntry *coro_allocator_var;
|
||||
};
|
||||
|
||||
enum OutType {
|
||||
@ -393,6 +403,10 @@ enum NodeType {
|
||||
NodeTypeIfErrorExpr,
|
||||
NodeTypeTestExpr,
|
||||
NodeTypeErrorSetDecl,
|
||||
NodeTypeCancel,
|
||||
NodeTypeResume,
|
||||
NodeTypeAwaitExpr,
|
||||
NodeTypeSuspend,
|
||||
};
|
||||
|
||||
struct AstNodeRoot {
|
||||
@ -405,6 +419,7 @@ enum CallingConvention {
|
||||
CallingConventionCold,
|
||||
CallingConventionNaked,
|
||||
CallingConventionStdcall,
|
||||
CallingConventionAsync,
|
||||
};
|
||||
|
||||
struct AstNodeFnProto {
|
||||
@ -426,6 +441,7 @@ struct AstNodeFnProto {
|
||||
AstNode *section_expr;
|
||||
|
||||
bool auto_err_set;
|
||||
AstNode *async_allocator_type;
|
||||
};
|
||||
|
||||
struct AstNodeFnDef {
|
||||
@ -567,6 +583,8 @@ struct AstNodeFnCallExpr {
|
||||
AstNode *fn_ref_expr;
|
||||
ZigList<AstNode *> params;
|
||||
bool is_builtin;
|
||||
bool is_async;
|
||||
AstNode *async_allocator;
|
||||
};
|
||||
|
||||
struct AstNodeArrayAccessExpr {
|
||||
@ -829,6 +847,14 @@ struct AstNodeBreakExpr {
|
||||
AstNode *expr; // may be null
|
||||
};
|
||||
|
||||
struct AstNodeCancelExpr {
|
||||
AstNode *expr;
|
||||
};
|
||||
|
||||
struct AstNodeResumeExpr {
|
||||
AstNode *expr;
|
||||
};
|
||||
|
||||
struct AstNodeContinueExpr {
|
||||
Buf *name;
|
||||
};
|
||||
@ -843,6 +869,15 @@ struct AstNodeErrorType {
|
||||
struct AstNodeVarLiteral {
|
||||
};
|
||||
|
||||
struct AstNodeAwaitExpr {
|
||||
AstNode *expr;
|
||||
};
|
||||
|
||||
struct AstNodeSuspend {
|
||||
AstNode *block;
|
||||
AstNode *promise_symbol;
|
||||
};
|
||||
|
||||
struct AstNode {
|
||||
enum NodeType type;
|
||||
size_t line;
|
||||
@ -900,6 +935,10 @@ struct AstNode {
|
||||
AstNodeErrorType error_type;
|
||||
AstNodeVarLiteral var_literal;
|
||||
AstNodeErrorSetDecl err_set_decl;
|
||||
AstNodeCancelExpr cancel_expr;
|
||||
AstNodeResumeExpr resume_expr;
|
||||
AstNodeAwaitExpr await_expr;
|
||||
AstNodeSuspend suspend;
|
||||
} data;
|
||||
};
|
||||
|
||||
@ -926,6 +965,7 @@ struct FnTypeId {
|
||||
bool is_var_args;
|
||||
CallingConvention cc;
|
||||
uint32_t alignment;
|
||||
TypeTableEntry *async_allocator_type;
|
||||
};
|
||||
|
||||
uint32_t fn_type_id_hash(FnTypeId*);
|
||||
@ -1087,6 +1127,11 @@ struct TypeTableEntryBoundFn {
|
||||
TypeTableEntry *fn_type;
|
||||
};
|
||||
|
||||
struct TypeTableEntryPromise {
|
||||
// null if `promise` instead of `promise->T`
|
||||
TypeTableEntry *result_type;
|
||||
};
|
||||
|
||||
enum TypeTableEntryId {
|
||||
TypeTableEntryIdInvalid,
|
||||
TypeTableEntryIdVar,
|
||||
@ -1114,6 +1159,7 @@ enum TypeTableEntryId {
|
||||
TypeTableEntryIdBoundFn,
|
||||
TypeTableEntryIdArgTuple,
|
||||
TypeTableEntryIdOpaque,
|
||||
TypeTableEntryIdPromise,
|
||||
};
|
||||
|
||||
struct TypeTableEntry {
|
||||
@ -1140,11 +1186,14 @@ struct TypeTableEntry {
|
||||
TypeTableEntryUnion unionation;
|
||||
TypeTableEntryFn fn;
|
||||
TypeTableEntryBoundFn bound_fn;
|
||||
TypeTableEntryPromise promise;
|
||||
} data;
|
||||
|
||||
// use these fields to make sure we don't duplicate type table entries for the same type
|
||||
TypeTableEntry *pointer_parent[2]; // [0 - mut, 1 - const]
|
||||
TypeTableEntry *maybe_parent;
|
||||
TypeTableEntry *promise_parent;
|
||||
TypeTableEntry *promise_frame_parent;
|
||||
// If we generate a constant name value for this type, we memoize it here.
|
||||
// The type of this is array
|
||||
ConstExprValue *cached_const_name_val;
|
||||
@ -1297,6 +1346,7 @@ enum BuiltinFnId {
|
||||
BuiltinFnIdArgType,
|
||||
BuiltinFnIdExport,
|
||||
BuiltinFnIdErrorReturnTrace,
|
||||
BuiltinFnIdAtomicRmw,
|
||||
};
|
||||
|
||||
struct BuiltinFnEntry {
|
||||
@ -1470,6 +1520,7 @@ struct CodeGen {
|
||||
TypeTableEntry *entry_u8;
|
||||
TypeTableEntry *entry_u16;
|
||||
TypeTableEntry *entry_u32;
|
||||
TypeTableEntry *entry_u29;
|
||||
TypeTableEntry *entry_u64;
|
||||
TypeTableEntry *entry_u128;
|
||||
TypeTableEntry *entry_i8;
|
||||
@ -1495,6 +1546,7 @@ struct CodeGen {
|
||||
TypeTableEntry *entry_var;
|
||||
TypeTableEntry *entry_global_error_set;
|
||||
TypeTableEntry *entry_arg_tuple;
|
||||
TypeTableEntry *entry_promise;
|
||||
} builtin_types;
|
||||
|
||||
EmitFileType emit_file_type;
|
||||
@ -1581,6 +1633,18 @@ struct CodeGen {
|
||||
LLVMValueRef trap_fn_val;
|
||||
LLVMValueRef return_address_fn_val;
|
||||
LLVMValueRef frame_address_fn_val;
|
||||
LLVMValueRef coro_destroy_fn_val;
|
||||
LLVMValueRef coro_id_fn_val;
|
||||
LLVMValueRef coro_alloc_fn_val;
|
||||
LLVMValueRef coro_size_fn_val;
|
||||
LLVMValueRef coro_begin_fn_val;
|
||||
LLVMValueRef coro_suspend_fn_val;
|
||||
LLVMValueRef coro_end_fn_val;
|
||||
LLVMValueRef coro_free_fn_val;
|
||||
LLVMValueRef coro_resume_fn_val;
|
||||
LLVMValueRef coro_save_fn_val;
|
||||
LLVMValueRef coro_promise_fn_val;
|
||||
LLVMValueRef coro_alloc_helper_fn_val;
|
||||
bool error_during_imports;
|
||||
|
||||
const char **clang_argv;
|
||||
@ -1803,6 +1867,19 @@ enum AtomicOrder {
|
||||
AtomicOrderSeqCst,
|
||||
};
|
||||
|
||||
// synchronized with the code in define_builtin_compile_vars
|
||||
enum AtomicRmwOp {
|
||||
AtomicRmwOp_xchg,
|
||||
AtomicRmwOp_add,
|
||||
AtomicRmwOp_sub,
|
||||
AtomicRmwOp_and,
|
||||
AtomicRmwOp_nand,
|
||||
AtomicRmwOp_or,
|
||||
AtomicRmwOp_xor,
|
||||
AtomicRmwOp_max,
|
||||
AtomicRmwOp_min,
|
||||
};
|
||||
|
||||
// A basic block contains no branching. Branches send control flow
|
||||
// to another basic block.
|
||||
// Phi instructions must be first in a basic block.
|
||||
@ -1939,6 +2016,22 @@ enum IrInstructionId {
|
||||
IrInstructionIdExport,
|
||||
IrInstructionIdErrorReturnTrace,
|
||||
IrInstructionIdErrorUnion,
|
||||
IrInstructionIdCancel,
|
||||
IrInstructionIdGetImplicitAllocator,
|
||||
IrInstructionIdCoroId,
|
||||
IrInstructionIdCoroAlloc,
|
||||
IrInstructionIdCoroSize,
|
||||
IrInstructionIdCoroBegin,
|
||||
IrInstructionIdCoroAllocFail,
|
||||
IrInstructionIdCoroSuspend,
|
||||
IrInstructionIdCoroEnd,
|
||||
IrInstructionIdCoroFree,
|
||||
IrInstructionIdCoroResume,
|
||||
IrInstructionIdCoroSave,
|
||||
IrInstructionIdCoroPromise,
|
||||
IrInstructionIdCoroAllocHelper,
|
||||
IrInstructionIdAtomicRmw,
|
||||
IrInstructionIdPromiseResultType,
|
||||
};
|
||||
|
||||
struct IrInstruction {
|
||||
@ -2142,6 +2235,9 @@ struct IrInstructionCall {
|
||||
bool is_comptime;
|
||||
LLVMValueRef tmp_ptr;
|
||||
FnInline fn_inline;
|
||||
bool is_async;
|
||||
|
||||
IrInstruction *async_allocator;
|
||||
};
|
||||
|
||||
struct IrInstructionConst {
|
||||
@ -2776,6 +2872,113 @@ struct IrInstructionErrorUnion {
|
||||
IrInstruction *payload;
|
||||
};
|
||||
|
||||
struct IrInstructionCancel {
|
||||
IrInstruction base;
|
||||
|
||||
IrInstruction *target;
|
||||
};
|
||||
|
||||
enum ImplicitAllocatorId {
|
||||
ImplicitAllocatorIdArg,
|
||||
ImplicitAllocatorIdLocalVar,
|
||||
};
|
||||
|
||||
struct IrInstructionGetImplicitAllocator {
|
||||
IrInstruction base;
|
||||
|
||||
ImplicitAllocatorId id;
|
||||
};
|
||||
|
||||
struct IrInstructionCoroId {
|
||||
IrInstruction base;
|
||||
|
||||
IrInstruction *promise_ptr;
|
||||
};
|
||||
|
||||
struct IrInstructionCoroAlloc {
|
||||
IrInstruction base;
|
||||
|
||||
IrInstruction *coro_id;
|
||||
};
|
||||
|
||||
struct IrInstructionCoroSize {
|
||||
IrInstruction base;
|
||||
};
|
||||
|
||||
struct IrInstructionCoroBegin {
|
||||
IrInstruction base;
|
||||
|
||||
IrInstruction *coro_id;
|
||||
IrInstruction *coro_mem_ptr;
|
||||
};
|
||||
|
||||
struct IrInstructionCoroAllocFail {
|
||||
IrInstruction base;
|
||||
|
||||
IrInstruction *err_val;
|
||||
};
|
||||
|
||||
struct IrInstructionCoroSuspend {
|
||||
IrInstruction base;
|
||||
|
||||
IrInstruction *save_point;
|
||||
IrInstruction *is_final;
|
||||
};
|
||||
|
||||
struct IrInstructionCoroEnd {
|
||||
IrInstruction base;
|
||||
};
|
||||
|
||||
struct IrInstructionCoroFree {
|
||||
IrInstruction base;
|
||||
|
||||
IrInstruction *coro_id;
|
||||
IrInstruction *coro_handle;
|
||||
};
|
||||
|
||||
struct IrInstructionCoroResume {
|
||||
IrInstruction base;
|
||||
|
||||
IrInstruction *awaiter_handle;
|
||||
};
|
||||
|
||||
struct IrInstructionCoroSave {
|
||||
IrInstruction base;
|
||||
|
||||
IrInstruction *coro_handle;
|
||||
};
|
||||
|
||||
struct IrInstructionCoroPromise {
|
||||
IrInstruction base;
|
||||
|
||||
IrInstruction *coro_handle;
|
||||
};
|
||||
|
||||
struct IrInstructionCoroAllocHelper {
|
||||
IrInstruction base;
|
||||
|
||||
IrInstruction *alloc_fn;
|
||||
IrInstruction *coro_size;
|
||||
};
|
||||
|
||||
struct IrInstructionAtomicRmw {
|
||||
IrInstruction base;
|
||||
|
||||
IrInstruction *operand_type;
|
||||
IrInstruction *ptr;
|
||||
IrInstruction *op;
|
||||
AtomicRmwOp resolved_op;
|
||||
IrInstruction *operand;
|
||||
IrInstruction *ordering;
|
||||
AtomicOrder resolved_ordering;
|
||||
};
|
||||
|
||||
struct IrInstructionPromiseResultType {
|
||||
IrInstruction base;
|
||||
|
||||
IrInstruction *promise_type;
|
||||
};
|
||||
|
||||
static const size_t slice_ptr_index = 0;
|
||||
static const size_t slice_len_index = 1;
|
||||
|
||||
@ -2785,6 +2988,13 @@ static const size_t maybe_null_index = 1;
|
||||
static const size_t err_union_err_index = 0;
|
||||
static const size_t err_union_payload_index = 1;
|
||||
|
||||
#define ASYNC_ALLOC_FIELD_NAME "allocFn"
|
||||
#define ASYNC_FREE_FIELD_NAME "freeFn"
|
||||
#define AWAITER_HANDLE_FIELD_NAME "awaiter_handle"
|
||||
#define RESULT_FIELD_NAME "result"
|
||||
#define RESULT_PTR_FIELD_NAME "result_ptr"
|
||||
|
||||
|
||||
enum FloatMode {
|
||||
FloatModeOptimized,
|
||||
FloatModeStrict,
|
||||
|
209
src/analyze.cpp
209
src/analyze.cpp
@ -230,6 +230,7 @@ bool type_is_complete(TypeTableEntry *type_entry) {
|
||||
case TypeTableEntryIdBlock:
|
||||
case TypeTableEntryIdBoundFn:
|
||||
case TypeTableEntryIdArgTuple:
|
||||
case TypeTableEntryIdPromise:
|
||||
return true;
|
||||
}
|
||||
zig_unreachable();
|
||||
@ -267,6 +268,7 @@ bool type_has_zero_bits_known(TypeTableEntry *type_entry) {
|
||||
case TypeTableEntryIdBoundFn:
|
||||
case TypeTableEntryIdArgTuple:
|
||||
case TypeTableEntryIdOpaque:
|
||||
case TypeTableEntryIdPromise:
|
||||
return true;
|
||||
}
|
||||
zig_unreachable();
|
||||
@ -339,6 +341,32 @@ TypeTableEntry *get_smallest_unsigned_int_type(CodeGen *g, uint64_t x) {
|
||||
return get_int_type(g, false, bits_needed_for_unsigned(x));
|
||||
}
|
||||
|
||||
TypeTableEntry *get_promise_type(CodeGen *g, TypeTableEntry *result_type) {
|
||||
if (result_type != nullptr && result_type->promise_parent != nullptr) {
|
||||
return result_type->promise_parent;
|
||||
} else if (result_type == nullptr && g->builtin_types.entry_promise != nullptr) {
|
||||
return g->builtin_types.entry_promise;
|
||||
}
|
||||
|
||||
TypeTableEntry *u8_ptr_type = get_pointer_to_type(g, g->builtin_types.entry_u8, false);
|
||||
TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdPromise);
|
||||
entry->type_ref = u8_ptr_type->type_ref;
|
||||
entry->zero_bits = false;
|
||||
entry->data.promise.result_type = result_type;
|
||||
buf_init_from_str(&entry->name, "promise");
|
||||
if (result_type != nullptr) {
|
||||
buf_appendf(&entry->name, "->%s", buf_ptr(&result_type->name));
|
||||
}
|
||||
entry->di_type = u8_ptr_type->di_type;
|
||||
|
||||
if (result_type != nullptr) {
|
||||
result_type->promise_parent = entry;
|
||||
} else if (result_type == nullptr) {
|
||||
g->builtin_types.entry_promise = entry;
|
||||
}
|
||||
return entry;
|
||||
}
|
||||
|
||||
TypeTableEntry *get_pointer_to_type_extra(CodeGen *g, TypeTableEntry *child_type, bool is_const,
|
||||
bool is_volatile, uint32_t byte_alignment, uint32_t bit_offset, uint32_t unaligned_bit_count)
|
||||
{
|
||||
@ -429,6 +457,23 @@ TypeTableEntry *get_pointer_to_type(CodeGen *g, TypeTableEntry *child_type, bool
|
||||
return get_pointer_to_type_extra(g, child_type, is_const, false, get_abi_alignment(g, child_type), 0, 0);
|
||||
}
|
||||
|
||||
TypeTableEntry *get_promise_frame_type(CodeGen *g, TypeTableEntry *return_type) {
|
||||
if (return_type->promise_frame_parent != nullptr) {
|
||||
return return_type->promise_frame_parent;
|
||||
}
|
||||
|
||||
TypeTableEntry *awaiter_handle_type = get_maybe_type(g, g->builtin_types.entry_promise);
|
||||
TypeTableEntry *result_ptr_type = get_pointer_to_type(g, return_type, false);
|
||||
const char *field_names[] = {AWAITER_HANDLE_FIELD_NAME, RESULT_FIELD_NAME, RESULT_PTR_FIELD_NAME};
|
||||
TypeTableEntry *field_types[] = {awaiter_handle_type, return_type, result_ptr_type};
|
||||
size_t field_count = type_has_bits(result_ptr_type) ? 3 : 1;
|
||||
Buf *name = buf_sprintf("AsyncFramePromise(%s)", buf_ptr(&return_type->name));
|
||||
TypeTableEntry *entry = get_struct_type(g, buf_ptr(name), field_names, field_types, field_count);
|
||||
|
||||
return_type->promise_frame_parent = entry;
|
||||
return entry;
|
||||
}
|
||||
|
||||
TypeTableEntry *get_maybe_type(CodeGen *g, TypeTableEntry *child_type) {
|
||||
if (child_type->maybe_parent) {
|
||||
TypeTableEntry *entry = child_type->maybe_parent;
|
||||
@ -447,9 +492,7 @@ TypeTableEntry *get_maybe_type(CodeGen *g, TypeTableEntry *child_type) {
|
||||
if (child_type->zero_bits) {
|
||||
entry->type_ref = LLVMInt1Type();
|
||||
entry->di_type = g->builtin_types.entry_bool->di_type;
|
||||
} else if (child_type->id == TypeTableEntryIdPointer ||
|
||||
child_type->id == TypeTableEntryIdFn)
|
||||
{
|
||||
} else if (type_is_codegen_pointer(child_type)) {
|
||||
// this is an optimization but also is necessary for calling C
|
||||
// functions where all pointers are maybe pointers
|
||||
// function types are technically pointers
|
||||
@ -884,6 +927,7 @@ static const char *calling_convention_name(CallingConvention cc) {
|
||||
case CallingConventionCold: return "coldcc";
|
||||
case CallingConventionNaked: return "nakedcc";
|
||||
case CallingConventionStdcall: return "stdcallcc";
|
||||
case CallingConventionAsync: return "async";
|
||||
}
|
||||
zig_unreachable();
|
||||
}
|
||||
@ -895,6 +939,21 @@ static const char *calling_convention_fn_type_str(CallingConvention cc) {
|
||||
case CallingConventionCold: return "coldcc ";
|
||||
case CallingConventionNaked: return "nakedcc ";
|
||||
case CallingConventionStdcall: return "stdcallcc ";
|
||||
case CallingConventionAsync: return "async ";
|
||||
}
|
||||
zig_unreachable();
|
||||
}
|
||||
|
||||
static bool calling_convention_allows_zig_types(CallingConvention cc) {
|
||||
switch (cc) {
|
||||
case CallingConventionUnspecified:
|
||||
case CallingConventionAsync:
|
||||
return true;
|
||||
case CallingConventionC:
|
||||
case CallingConventionCold:
|
||||
case CallingConventionNaked:
|
||||
case CallingConventionStdcall:
|
||||
return false;
|
||||
}
|
||||
zig_unreachable();
|
||||
}
|
||||
@ -924,8 +983,13 @@ TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
|
||||
|
||||
// populate the name of the type
|
||||
buf_resize(&fn_type->name, 0);
|
||||
const char *cc_str = calling_convention_fn_type_str(fn_type->data.fn.fn_type_id.cc);
|
||||
buf_appendf(&fn_type->name, "%sfn(", cc_str);
|
||||
if (fn_type->data.fn.fn_type_id.cc == CallingConventionAsync) {
|
||||
buf_appendf(&fn_type->name, "async(%s) ", buf_ptr(&fn_type_id->async_allocator_type->name));
|
||||
} else {
|
||||
const char *cc_str = calling_convention_fn_type_str(fn_type->data.fn.fn_type_id.cc);
|
||||
buf_appendf(&fn_type->name, "%s", cc_str);
|
||||
}
|
||||
buf_appendf(&fn_type->name, "fn(");
|
||||
for (size_t i = 0; i < fn_type_id->param_count; i += 1) {
|
||||
FnTypeParamInfo *param_info = &fn_type_id->param_info[i];
|
||||
|
||||
@ -953,20 +1017,23 @@ TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
|
||||
if (!skip_debug_info) {
|
||||
bool first_arg_return = calling_convention_does_first_arg_return(fn_type_id->cc) &&
|
||||
handle_is_ptr(fn_type_id->return_type);
|
||||
bool prefix_arg_error_return_trace = g->have_err_ret_tracing &&
|
||||
(fn_type_id->return_type->id == TypeTableEntryIdErrorUnion ||
|
||||
fn_type_id->return_type->id == TypeTableEntryIdErrorSet);
|
||||
bool is_async = fn_type_id->cc == CallingConventionAsync;
|
||||
bool prefix_arg_error_return_trace = g->have_err_ret_tracing && fn_type_can_fail(fn_type_id);
|
||||
// +1 for maybe making the first argument the return value
|
||||
// +1 for maybe last argument the error return trace
|
||||
LLVMTypeRef *gen_param_types = allocate<LLVMTypeRef>(2 + fn_type_id->param_count);
|
||||
// +1 for maybe first argument the error return trace
|
||||
// +2 for maybe arguments async allocator and error code pointer
|
||||
LLVMTypeRef *gen_param_types = allocate<LLVMTypeRef>(4 + fn_type_id->param_count);
|
||||
// +1 because 0 is the return type and
|
||||
// +1 for maybe making first arg ret val and
|
||||
// +1 for maybe last argument the error return trace
|
||||
ZigLLVMDIType **param_di_types = allocate<ZigLLVMDIType*>(3 + fn_type_id->param_count);
|
||||
// +1 for maybe first argument the error return trace
|
||||
// +2 for maybe arguments async allocator and error code pointer
|
||||
ZigLLVMDIType **param_di_types = allocate<ZigLLVMDIType*>(5 + fn_type_id->param_count);
|
||||
param_di_types[0] = fn_type_id->return_type->di_type;
|
||||
size_t gen_param_index = 0;
|
||||
TypeTableEntry *gen_return_type;
|
||||
if (!type_has_bits(fn_type_id->return_type)) {
|
||||
if (is_async) {
|
||||
gen_return_type = get_pointer_to_type(g, g->builtin_types.entry_u8, false);
|
||||
} else if (!type_has_bits(fn_type_id->return_type)) {
|
||||
gen_return_type = g->builtin_types.entry_void;
|
||||
} else if (first_arg_return) {
|
||||
TypeTableEntry *gen_type = get_pointer_to_type(g, fn_type_id->return_type, false);
|
||||
@ -987,6 +1054,25 @@ TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
|
||||
// after the gen_param_index += 1 because 0 is the return type
|
||||
param_di_types[gen_param_index] = gen_type->di_type;
|
||||
}
|
||||
if (is_async) {
|
||||
{
|
||||
// async allocator param
|
||||
TypeTableEntry *gen_type = fn_type_id->async_allocator_type;
|
||||
gen_param_types[gen_param_index] = gen_type->type_ref;
|
||||
gen_param_index += 1;
|
||||
// after the gen_param_index += 1 because 0 is the return type
|
||||
param_di_types[gen_param_index] = gen_type->di_type;
|
||||
}
|
||||
|
||||
{
|
||||
// error code pointer
|
||||
TypeTableEntry *gen_type = get_pointer_to_type(g, g->builtin_types.entry_global_error_set, false);
|
||||
gen_param_types[gen_param_index] = gen_type->type_ref;
|
||||
gen_param_index += 1;
|
||||
// after the gen_param_index += 1 because 0 is the return type
|
||||
param_di_types[gen_param_index] = gen_type->di_type;
|
||||
}
|
||||
}
|
||||
|
||||
fn_type->data.fn.gen_param_info = allocate<FnGenParamInfo>(fn_type_id->param_count);
|
||||
for (size_t i = 0; i < fn_type_id->param_count; i += 1) {
|
||||
@ -1096,7 +1182,16 @@ TypeTableEntry *analyze_type_expr(CodeGen *g, Scope *scope, AstNode *node) {
|
||||
TypeTableEntry *get_generic_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
|
||||
TypeTableEntry *fn_type = new_type_table_entry(TypeTableEntryIdFn);
|
||||
fn_type->is_copyable = false;
|
||||
buf_init_from_str(&fn_type->name, "fn(");
|
||||
buf_resize(&fn_type->name, 0);
|
||||
if (fn_type->data.fn.fn_type_id.cc == CallingConventionAsync) {
|
||||
const char *async_allocator_type_str = (fn_type->data.fn.fn_type_id.async_allocator_type == nullptr) ?
|
||||
"var" : buf_ptr(&fn_type_id->async_allocator_type->name);
|
||||
buf_appendf(&fn_type->name, "async(%s) ", async_allocator_type_str);
|
||||
} else {
|
||||
const char *cc_str = calling_convention_fn_type_str(fn_type->data.fn.fn_type_id.cc);
|
||||
buf_appendf(&fn_type->name, "%s", cc_str);
|
||||
}
|
||||
buf_appendf(&fn_type->name, "fn(");
|
||||
size_t i = 0;
|
||||
for (; i < fn_type_id->next_param_index; i += 1) {
|
||||
const char *comma_str = (i == 0) ? "" : ",";
|
||||
@ -1201,6 +1296,7 @@ static bool type_allowed_in_packed_struct(TypeTableEntry *type_entry) {
|
||||
case TypeTableEntryIdBoundFn:
|
||||
case TypeTableEntryIdArgTuple:
|
||||
case TypeTableEntryIdOpaque:
|
||||
case TypeTableEntryIdPromise:
|
||||
return false;
|
||||
case TypeTableEntryIdVoid:
|
||||
case TypeTableEntryIdBool:
|
||||
@ -1217,7 +1313,7 @@ static bool type_allowed_in_packed_struct(TypeTableEntry *type_entry) {
|
||||
case TypeTableEntryIdMaybe:
|
||||
{
|
||||
TypeTableEntry *child_type = type_entry->data.maybe.child_type;
|
||||
return child_type->id == TypeTableEntryIdPointer || child_type->id == TypeTableEntryIdFn;
|
||||
return type_is_codegen_pointer(child_type);
|
||||
}
|
||||
case TypeTableEntryIdEnum:
|
||||
return type_entry->data.enumeration.decl_node->data.container_decl.init_arg_expr != nullptr;
|
||||
@ -1241,6 +1337,7 @@ static bool type_allowed_in_extern(CodeGen *g, TypeTableEntry *type_entry) {
|
||||
case TypeTableEntryIdBlock:
|
||||
case TypeTableEntryIdBoundFn:
|
||||
case TypeTableEntryIdArgTuple:
|
||||
case TypeTableEntryIdPromise:
|
||||
return false;
|
||||
case TypeTableEntryIdOpaque:
|
||||
case TypeTableEntryIdUnreachable:
|
||||
@ -1312,7 +1409,7 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c
|
||||
bool param_is_var_args = param_node->data.param_decl.is_var_args;
|
||||
|
||||
if (param_is_comptime) {
|
||||
if (fn_type_id.cc != CallingConventionUnspecified) {
|
||||
if (!calling_convention_allows_zig_types(fn_type_id.cc)) {
|
||||
add_node_error(g, param_node,
|
||||
buf_sprintf("comptime parameter not allowed in function with calling convention '%s'",
|
||||
calling_convention_name(fn_type_id.cc)));
|
||||
@ -1323,7 +1420,7 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c
|
||||
if (fn_type_id.cc == CallingConventionC) {
|
||||
fn_type_id.param_count = fn_type_id.next_param_index;
|
||||
continue;
|
||||
} else if (fn_type_id.cc == CallingConventionUnspecified) {
|
||||
} else if (calling_convention_allows_zig_types(fn_type_id.cc)) {
|
||||
return get_generic_fn_type(g, &fn_type_id);
|
||||
} else {
|
||||
add_node_error(g, param_node,
|
||||
@ -1337,7 +1434,7 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c
|
||||
if (type_is_invalid(type_entry)) {
|
||||
return g->builtin_types.entry_invalid;
|
||||
}
|
||||
if (fn_type_id.cc != CallingConventionUnspecified) {
|
||||
if (!calling_convention_allows_zig_types(fn_type_id.cc)) {
|
||||
type_ensure_zero_bits_known(g, type_entry);
|
||||
if (!type_has_bits(type_entry)) {
|
||||
add_node_error(g, param_node->data.param_decl.type,
|
||||
@ -1347,7 +1444,7 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c
|
||||
}
|
||||
}
|
||||
|
||||
if (fn_type_id.cc != CallingConventionUnspecified && !type_allowed_in_extern(g, type_entry)) {
|
||||
if (!calling_convention_allows_zig_types(fn_type_id.cc) && !type_allowed_in_extern(g, type_entry)) {
|
||||
add_node_error(g, param_node->data.param_decl.type,
|
||||
buf_sprintf("parameter of type '%s' not allowed in function with calling convention '%s'",
|
||||
buf_ptr(&type_entry->name),
|
||||
@ -1367,7 +1464,7 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c
|
||||
buf_sprintf("parameter of type '%s' not allowed", buf_ptr(&type_entry->name)));
|
||||
return g->builtin_types.entry_invalid;
|
||||
case TypeTableEntryIdVar:
|
||||
if (fn_type_id.cc != CallingConventionUnspecified) {
|
||||
if (!calling_convention_allows_zig_types(fn_type_id.cc)) {
|
||||
add_node_error(g, param_node->data.param_decl.type,
|
||||
buf_sprintf("parameter of type 'var' not allowed in function with calling convention '%s'",
|
||||
calling_convention_name(fn_type_id.cc)));
|
||||
@ -1381,7 +1478,7 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c
|
||||
case TypeTableEntryIdBoundFn:
|
||||
case TypeTableEntryIdMetaType:
|
||||
add_node_error(g, param_node->data.param_decl.type,
|
||||
buf_sprintf("parameter of type '%s' must be declared inline",
|
||||
buf_sprintf("parameter of type '%s' must be declared comptime",
|
||||
buf_ptr(&type_entry->name)));
|
||||
return g->builtin_types.entry_invalid;
|
||||
case TypeTableEntryIdVoid:
|
||||
@ -1397,8 +1494,9 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c
|
||||
case TypeTableEntryIdEnum:
|
||||
case TypeTableEntryIdUnion:
|
||||
case TypeTableEntryIdFn:
|
||||
case TypeTableEntryIdPromise:
|
||||
ensure_complete_type(g, type_entry);
|
||||
if (fn_type_id.cc == CallingConventionUnspecified && !type_is_copyable(g, type_entry)) {
|
||||
if (calling_convention_allows_zig_types(fn_type_id.cc) && !type_is_copyable(g, type_entry)) {
|
||||
add_node_error(g, param_node->data.param_decl.type,
|
||||
buf_sprintf("type '%s' is not copyable; cannot pass by value", buf_ptr(&type_entry->name)));
|
||||
return g->builtin_types.entry_invalid;
|
||||
@ -1429,7 +1527,7 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c
|
||||
fn_type_id.return_type = specified_return_type;
|
||||
}
|
||||
|
||||
if (fn_type_id.cc != CallingConventionUnspecified && !type_allowed_in_extern(g, fn_type_id.return_type)) {
|
||||
if (!calling_convention_allows_zig_types(fn_type_id.cc) && !type_allowed_in_extern(g, fn_type_id.return_type)) {
|
||||
add_node_error(g, fn_proto->return_type,
|
||||
buf_sprintf("return type '%s' not allowed in function with calling convention '%s'",
|
||||
buf_ptr(&fn_type_id.return_type->name),
|
||||
@ -1456,7 +1554,7 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c
|
||||
case TypeTableEntryIdBoundFn:
|
||||
case TypeTableEntryIdVar:
|
||||
case TypeTableEntryIdMetaType:
|
||||
if (fn_type_id.cc != CallingConventionUnspecified) {
|
||||
if (!calling_convention_allows_zig_types(fn_type_id.cc)) {
|
||||
add_node_error(g, fn_proto->return_type,
|
||||
buf_sprintf("return type '%s' not allowed in function with calling convention '%s'",
|
||||
buf_ptr(&fn_type_id.return_type->name),
|
||||
@ -1478,9 +1576,20 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c
|
||||
case TypeTableEntryIdEnum:
|
||||
case TypeTableEntryIdUnion:
|
||||
case TypeTableEntryIdFn:
|
||||
case TypeTableEntryIdPromise:
|
||||
break;
|
||||
}
|
||||
|
||||
if (fn_type_id.cc == CallingConventionAsync) {
|
||||
if (fn_proto->async_allocator_type == nullptr) {
|
||||
return get_generic_fn_type(g, &fn_type_id);
|
||||
}
|
||||
fn_type_id.async_allocator_type = analyze_type_expr(g, child_scope, fn_proto->async_allocator_type);
|
||||
if (type_is_invalid(fn_type_id.async_allocator_type)) {
|
||||
return g->builtin_types.entry_invalid;
|
||||
}
|
||||
}
|
||||
|
||||
return get_fn_type(g, &fn_type_id);
|
||||
}
|
||||
|
||||
@ -1615,6 +1724,8 @@ TypeTableEntry *get_struct_type(CodeGen *g, const char *type_name, const char *f
|
||||
field->src_index = i;
|
||||
field->gen_index = i;
|
||||
|
||||
assert(type_has_bits(field->type_entry));
|
||||
|
||||
auto prev_entry = struct_type->data.structure.fields_by_name.put_unique(field->name, field);
|
||||
assert(prev_entry == nullptr);
|
||||
}
|
||||
@ -3119,6 +3230,10 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {
|
||||
case NodeTypeIfErrorExpr:
|
||||
case NodeTypeTestExpr:
|
||||
case NodeTypeErrorSetDecl:
|
||||
case NodeTypeCancel:
|
||||
case NodeTypeResume:
|
||||
case NodeTypeAwaitExpr:
|
||||
case NodeTypeSuspend:
|
||||
zig_unreachable();
|
||||
}
|
||||
}
|
||||
@ -3174,6 +3289,7 @@ TypeTableEntry *validate_var_type(CodeGen *g, AstNode *source_node, TypeTableEnt
|
||||
case TypeTableEntryIdUnion:
|
||||
case TypeTableEntryIdFn:
|
||||
case TypeTableEntryIdBoundFn:
|
||||
case TypeTableEntryIdPromise:
|
||||
return type_entry;
|
||||
}
|
||||
zig_unreachable();
|
||||
@ -3552,6 +3668,7 @@ static bool is_container(TypeTableEntry *type_entry) {
|
||||
case TypeTableEntryIdBoundFn:
|
||||
case TypeTableEntryIdArgTuple:
|
||||
case TypeTableEntryIdOpaque:
|
||||
case TypeTableEntryIdPromise:
|
||||
return false;
|
||||
}
|
||||
zig_unreachable();
|
||||
@ -3602,6 +3719,7 @@ void resolve_container_type(CodeGen *g, TypeTableEntry *type_entry) {
|
||||
case TypeTableEntryIdVar:
|
||||
case TypeTableEntryIdArgTuple:
|
||||
case TypeTableEntryIdOpaque:
|
||||
case TypeTableEntryIdPromise:
|
||||
zig_unreachable();
|
||||
}
|
||||
}
|
||||
@ -3609,15 +3727,17 @@ void resolve_container_type(CodeGen *g, TypeTableEntry *type_entry) {
|
||||
TypeTableEntry *get_codegen_ptr_type(TypeTableEntry *type) {
|
||||
if (type->id == TypeTableEntryIdPointer) return type;
|
||||
if (type->id == TypeTableEntryIdFn) return type;
|
||||
if (type->id == TypeTableEntryIdPromise) return type;
|
||||
if (type->id == TypeTableEntryIdMaybe) {
|
||||
if (type->data.maybe.child_type->id == TypeTableEntryIdPointer) return type->data.maybe.child_type;
|
||||
if (type->data.maybe.child_type->id == TypeTableEntryIdFn) return type->data.maybe.child_type;
|
||||
if (type->data.maybe.child_type->id == TypeTableEntryIdPromise) return type->data.maybe.child_type;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool type_is_codegen_pointer(TypeTableEntry *type) {
|
||||
return get_codegen_ptr_type(type) != nullptr;
|
||||
return get_codegen_ptr_type(type) == type;
|
||||
}
|
||||
|
||||
uint32_t get_ptr_align(TypeTableEntry *type) {
|
||||
@ -3626,6 +3746,8 @@ uint32_t get_ptr_align(TypeTableEntry *type) {
|
||||
return ptr_type->data.pointer.alignment;
|
||||
} else if (ptr_type->id == TypeTableEntryIdFn) {
|
||||
return (ptr_type->data.fn.fn_type_id.alignment == 0) ? 1 : ptr_type->data.fn.fn_type_id.alignment;
|
||||
} else if (ptr_type->id == TypeTableEntryIdPromise) {
|
||||
return 1;
|
||||
} else {
|
||||
zig_unreachable();
|
||||
}
|
||||
@ -3640,7 +3762,7 @@ AstNode *get_param_decl_node(FnTableEntry *fn_entry, size_t index) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void define_local_param_variables(CodeGen *g, FnTableEntry *fn_table_entry, VariableTableEntry **arg_vars) {
|
||||
static void define_local_param_variables(CodeGen *g, FnTableEntry *fn_table_entry, VariableTableEntry **arg_vars) {
|
||||
TypeTableEntry *fn_type = fn_table_entry->type_entry;
|
||||
assert(!fn_type->data.fn.is_generic);
|
||||
FnTypeId *fn_type_id = &fn_type->data.fn.fn_type_id;
|
||||
@ -3661,7 +3783,7 @@ void define_local_param_variables(CodeGen *g, FnTableEntry *fn_table_entry, Vari
|
||||
TypeTableEntry *param_type = param_info->type;
|
||||
bool is_noalias = param_info->is_noalias;
|
||||
|
||||
if (is_noalias && !type_is_codegen_pointer(param_type)) {
|
||||
if (is_noalias && get_codegen_ptr_type(param_type) == nullptr) {
|
||||
add_node_error(g, param_decl_node, buf_sprintf("noalias on non-pointer parameter"));
|
||||
}
|
||||
|
||||
@ -4094,6 +4216,7 @@ bool handle_is_ptr(TypeTableEntry *type_entry) {
|
||||
case TypeTableEntryIdErrorSet:
|
||||
case TypeTableEntryIdFn:
|
||||
case TypeTableEntryIdEnum:
|
||||
case TypeTableEntryIdPromise:
|
||||
return false;
|
||||
case TypeTableEntryIdArray:
|
||||
case TypeTableEntryIdStruct:
|
||||
@ -4102,8 +4225,7 @@ bool handle_is_ptr(TypeTableEntry *type_entry) {
|
||||
return type_has_bits(type_entry->data.error_union.payload_type);
|
||||
case TypeTableEntryIdMaybe:
|
||||
return type_has_bits(type_entry->data.maybe.child_type) &&
|
||||
type_entry->data.maybe.child_type->id != TypeTableEntryIdPointer &&
|
||||
type_entry->data.maybe.child_type->id != TypeTableEntryIdFn;
|
||||
!type_is_codegen_pointer(type_entry->data.maybe.child_type);
|
||||
case TypeTableEntryIdUnion:
|
||||
assert(type_entry->data.unionation.complete);
|
||||
if (type_entry->data.unionation.gen_field_count == 0)
|
||||
@ -4205,6 +4327,7 @@ uint32_t fn_type_id_hash(FnTypeId *id) {
|
||||
result += ((uint32_t)(id->cc)) * (uint32_t)3349388391;
|
||||
result += id->is_var_args ? (uint32_t)1931444534 : 0;
|
||||
result += hash_ptr(id->return_type);
|
||||
result += hash_ptr(id->async_allocator_type);
|
||||
result += id->alignment * 0xd3b3f3e2;
|
||||
for (size_t i = 0; i < id->param_count; i += 1) {
|
||||
FnTypeParamInfo *info = &id->param_info[i];
|
||||
@ -4219,7 +4342,8 @@ bool fn_type_id_eql(FnTypeId *a, FnTypeId *b) {
|
||||
a->return_type != b->return_type ||
|
||||
a->is_var_args != b->is_var_args ||
|
||||
a->param_count != b->param_count ||
|
||||
a->alignment != b->alignment)
|
||||
a->alignment != b->alignment ||
|
||||
a->async_allocator_type != b->async_allocator_type)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@ -4341,6 +4465,9 @@ static uint32_t hash_const_val(ConstExprValue *const_val) {
|
||||
}
|
||||
zig_unreachable();
|
||||
}
|
||||
case TypeTableEntryIdPromise:
|
||||
// TODO better hashing algorithm
|
||||
return 223048345;
|
||||
case TypeTableEntryIdUndefLit:
|
||||
return 162837799;
|
||||
case TypeTableEntryIdNullLit:
|
||||
@ -4500,6 +4627,7 @@ bool type_requires_comptime(TypeTableEntry *type_entry) {
|
||||
case TypeTableEntryIdPointer:
|
||||
case TypeTableEntryIdVoid:
|
||||
case TypeTableEntryIdUnreachable:
|
||||
case TypeTableEntryIdPromise:
|
||||
return false;
|
||||
}
|
||||
zig_unreachable();
|
||||
@ -4969,6 +5097,7 @@ bool const_values_equal(ConstExprValue *a, ConstExprValue *b) {
|
||||
case TypeTableEntryIdInvalid:
|
||||
case TypeTableEntryIdUnreachable:
|
||||
case TypeTableEntryIdVar:
|
||||
case TypeTableEntryIdPromise:
|
||||
zig_unreachable();
|
||||
}
|
||||
zig_unreachable();
|
||||
@ -5243,6 +5372,8 @@ void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val) {
|
||||
buf_appendf(buf, "(args value)");
|
||||
return;
|
||||
}
|
||||
case TypeTableEntryIdPromise:
|
||||
zig_unreachable();
|
||||
}
|
||||
zig_unreachable();
|
||||
}
|
||||
@ -5304,6 +5435,7 @@ uint32_t type_id_hash(TypeId x) {
|
||||
case TypeTableEntryIdBlock:
|
||||
case TypeTableEntryIdBoundFn:
|
||||
case TypeTableEntryIdArgTuple:
|
||||
case TypeTableEntryIdPromise:
|
||||
zig_unreachable();
|
||||
case TypeTableEntryIdErrorUnion:
|
||||
return hash_ptr(x.data.error_union.err_set_type) ^ hash_ptr(x.data.error_union.payload_type);
|
||||
@ -5341,6 +5473,7 @@ bool type_id_eql(TypeId a, TypeId b) {
|
||||
case TypeTableEntryIdUndefLit:
|
||||
case TypeTableEntryIdNullLit:
|
||||
case TypeTableEntryIdMaybe:
|
||||
case TypeTableEntryIdPromise:
|
||||
case TypeTableEntryIdErrorSet:
|
||||
case TypeTableEntryIdEnum:
|
||||
case TypeTableEntryIdUnion:
|
||||
@ -5468,6 +5601,7 @@ static const TypeTableEntryId all_type_ids[] = {
|
||||
TypeTableEntryIdBoundFn,
|
||||
TypeTableEntryIdArgTuple,
|
||||
TypeTableEntryIdOpaque,
|
||||
TypeTableEntryIdPromise,
|
||||
};
|
||||
|
||||
TypeTableEntryId type_id_at_index(size_t index) {
|
||||
@ -5532,6 +5666,8 @@ size_t type_id_index(TypeTableEntryId id) {
|
||||
return 22;
|
||||
case TypeTableEntryIdOpaque:
|
||||
return 23;
|
||||
case TypeTableEntryIdPromise:
|
||||
return 24;
|
||||
}
|
||||
zig_unreachable();
|
||||
}
|
||||
@ -5589,6 +5725,8 @@ const char *type_id_name(TypeTableEntryId id) {
|
||||
return "ArgTuple";
|
||||
case TypeTableEntryIdOpaque:
|
||||
return "Opaque";
|
||||
case TypeTableEntryIdPromise:
|
||||
return "Promise";
|
||||
}
|
||||
zig_unreachable();
|
||||
}
|
||||
@ -5671,3 +5809,14 @@ bool type_is_global_error_set(TypeTableEntry *err_set_type) {
|
||||
assert(err_set_type->data.error_set.infer_fn == nullptr);
|
||||
return err_set_type->data.error_set.err_count == UINT32_MAX;
|
||||
}
|
||||
|
||||
uint32_t get_coro_frame_align_bytes(CodeGen *g) {
|
||||
return g->pointer_size_bytes * 2;
|
||||
}
|
||||
|
||||
bool fn_type_can_fail(FnTypeId *fn_type_id) {
|
||||
TypeTableEntry *return_type = fn_type_id->return_type;
|
||||
return return_type->id == TypeTableEntryIdErrorUnion || return_type->id == TypeTableEntryIdErrorSet ||
|
||||
fn_type_id->cc == CallingConventionAsync;
|
||||
}
|
||||
|
||||
|
@ -35,6 +35,8 @@ TypeTableEntry *get_bound_fn_type(CodeGen *g, FnTableEntry *fn_entry);
|
||||
TypeTableEntry *get_opaque_type(CodeGen *g, Scope *scope, AstNode *source_node, const char *name);
|
||||
TypeTableEntry *get_struct_type(CodeGen *g, const char *type_name, const char *field_names[],
|
||||
TypeTableEntry *field_types[], size_t field_count);
|
||||
TypeTableEntry *get_promise_type(CodeGen *g, TypeTableEntry *result_type);
|
||||
TypeTableEntry *get_promise_frame_type(CodeGen *g, TypeTableEntry *return_type);
|
||||
TypeTableEntry *get_test_fn_type(CodeGen *g);
|
||||
bool handle_is_ptr(TypeTableEntry *type_entry);
|
||||
void find_libc_include_path(CodeGen *g);
|
||||
@ -50,6 +52,7 @@ VariableTableEntry *find_variable(CodeGen *g, Scope *orig_context, Buf *name);
|
||||
Tld *find_decl(CodeGen *g, Scope *scope, Buf *name);
|
||||
void resolve_top_level_decl(CodeGen *g, Tld *tld, bool pointer_only, AstNode *source_node);
|
||||
bool type_is_codegen_pointer(TypeTableEntry *type);
|
||||
|
||||
TypeTableEntry *get_codegen_ptr_type(TypeTableEntry *type);
|
||||
uint32_t get_ptr_align(TypeTableEntry *type);
|
||||
TypeTableEntry *validate_var_type(CodeGen *g, AstNode *source_node, TypeTableEntry *type_entry);
|
||||
@ -92,7 +95,6 @@ void eval_min_max_value(CodeGen *g, TypeTableEntry *type_entry, ConstExprValue *
|
||||
void eval_min_max_value_int(CodeGen *g, TypeTableEntry *int_type, BigInt *bigint, bool is_max);
|
||||
|
||||
void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val);
|
||||
void define_local_param_variables(CodeGen *g, FnTableEntry *fn_table_entry, VariableTableEntry **arg_vars);
|
||||
void analyze_fn_ir(CodeGen *g, FnTableEntry *fn_table_entry, AstNode *return_type_node);
|
||||
|
||||
ScopeBlock *create_block_scope(AstNode *node, Scope *parent);
|
||||
@ -190,4 +192,7 @@ void analyze_fn_body(CodeGen *g, FnTableEntry *fn_table_entry);
|
||||
|
||||
TypeTableEntry *get_auto_err_set_type(CodeGen *g, FnTableEntry *fn_entry);
|
||||
|
||||
uint32_t get_coro_frame_align_bytes(CodeGen *g);
|
||||
bool fn_type_can_fail(FnTypeId *fn_type_id);
|
||||
|
||||
#endif
|
||||
|
@ -244,6 +244,14 @@ static const char *node_type_str(NodeType node_type) {
|
||||
return "TestExpr";
|
||||
case NodeTypeErrorSetDecl:
|
||||
return "ErrorSetDecl";
|
||||
case NodeTypeCancel:
|
||||
return "Cancel";
|
||||
case NodeTypeResume:
|
||||
return "Resume";
|
||||
case NodeTypeAwaitExpr:
|
||||
return "AwaitExpr";
|
||||
case NodeTypeSuspend:
|
||||
return "Suspend";
|
||||
}
|
||||
zig_unreachable();
|
||||
}
|
||||
@ -1037,6 +1045,35 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
|
||||
fprintf(ar->f, "}");
|
||||
break;
|
||||
}
|
||||
case NodeTypeCancel:
|
||||
{
|
||||
fprintf(ar->f, "cancel ");
|
||||
render_node_grouped(ar, node->data.cancel_expr.expr);
|
||||
break;
|
||||
}
|
||||
case NodeTypeResume:
|
||||
{
|
||||
fprintf(ar->f, "resume ");
|
||||
render_node_grouped(ar, node->data.resume_expr.expr);
|
||||
break;
|
||||
}
|
||||
case NodeTypeAwaitExpr:
|
||||
{
|
||||
fprintf(ar->f, "await ");
|
||||
render_node_grouped(ar, node->data.await_expr.expr);
|
||||
break;
|
||||
}
|
||||
case NodeTypeSuspend:
|
||||
{
|
||||
fprintf(ar->f, "suspend");
|
||||
if (node->data.suspend.block != nullptr) {
|
||||
fprintf(ar->f, " |");
|
||||
render_node_grouped(ar, node->data.suspend.promise_symbol);
|
||||
fprintf(ar->f, "| ");
|
||||
render_node_grouped(ar, node->data.suspend.block);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case NodeTypeFnDecl:
|
||||
case NodeTypeParamDecl:
|
||||
case NodeTypeTestDecl:
|
||||
|
589
src/codegen.cpp
589
src/codegen.cpp
@ -381,6 +381,8 @@ static LLVMCallConv get_llvm_cc(CodeGen *g, CallingConvention cc) {
|
||||
} else {
|
||||
return LLVMCCallConv;
|
||||
}
|
||||
case CallingConventionAsync:
|
||||
return LLVMFastCallConv;
|
||||
}
|
||||
zig_unreachable();
|
||||
}
|
||||
@ -410,10 +412,10 @@ static uint32_t get_err_ret_trace_arg_index(CodeGen *g, FnTableEntry *fn_table_e
|
||||
return UINT32_MAX;
|
||||
}
|
||||
TypeTableEntry *fn_type = fn_table_entry->type_entry;
|
||||
TypeTableEntry *return_type = fn_type->data.fn.fn_type_id.return_type;
|
||||
if (return_type->id != TypeTableEntryIdErrorUnion && return_type->id != TypeTableEntryIdErrorSet) {
|
||||
if (!fn_type_can_fail(&fn_type->data.fn.fn_type_id)) {
|
||||
return UINT32_MAX;
|
||||
}
|
||||
TypeTableEntry *return_type = fn_type->data.fn.fn_type_id.return_type;
|
||||
bool first_arg_ret = type_has_bits(return_type) && handle_is_ptr(return_type);
|
||||
return first_arg_ret ? 1 : 0;
|
||||
}
|
||||
@ -540,7 +542,7 @@ static LLVMValueRef fn_llvm_value(CodeGen *g, FnTableEntry *fn_table_entry) {
|
||||
|
||||
if (!type_has_bits(return_type)) {
|
||||
// nothing to do
|
||||
} else if (return_type->id == TypeTableEntryIdPointer || return_type->id == TypeTableEntryIdFn) {
|
||||
} else if (type_is_codegen_pointer(return_type)) {
|
||||
addLLVMAttr(fn_table_entry->llvm_value, 0, "nonnull");
|
||||
} else if (handle_is_ptr(return_type) &&
|
||||
calling_convention_does_first_arg_return(fn_type->data.fn.fn_type_id.cc))
|
||||
@ -925,6 +927,177 @@ static LLVMValueRef get_memcpy_fn_val(CodeGen *g) {
|
||||
return g->memcpy_fn_val;
|
||||
}
|
||||
|
||||
static LLVMValueRef get_coro_destroy_fn_val(CodeGen *g) {
|
||||
if (g->coro_destroy_fn_val)
|
||||
return g->coro_destroy_fn_val;
|
||||
|
||||
LLVMTypeRef param_types[] = {
|
||||
LLVMPointerType(LLVMInt8Type(), 0),
|
||||
};
|
||||
LLVMTypeRef fn_type = LLVMFunctionType(LLVMVoidType(), param_types, 1, false);
|
||||
Buf *name = buf_sprintf("llvm.coro.destroy");
|
||||
g->coro_destroy_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type);
|
||||
assert(LLVMGetIntrinsicID(g->coro_destroy_fn_val));
|
||||
|
||||
return g->coro_destroy_fn_val;
|
||||
}
|
||||
|
||||
static LLVMValueRef get_coro_id_fn_val(CodeGen *g) {
|
||||
if (g->coro_id_fn_val)
|
||||
return g->coro_id_fn_val;
|
||||
|
||||
LLVMTypeRef param_types[] = {
|
||||
LLVMInt32Type(),
|
||||
LLVMPointerType(LLVMInt8Type(), 0),
|
||||
LLVMPointerType(LLVMInt8Type(), 0),
|
||||
LLVMPointerType(LLVMInt8Type(), 0),
|
||||
};
|
||||
LLVMTypeRef fn_type = LLVMFunctionType(ZigLLVMTokenTypeInContext(LLVMGetGlobalContext()), param_types, 4, false);
|
||||
Buf *name = buf_sprintf("llvm.coro.id");
|
||||
g->coro_id_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type);
|
||||
assert(LLVMGetIntrinsicID(g->coro_id_fn_val));
|
||||
|
||||
return g->coro_id_fn_val;
|
||||
}
|
||||
|
||||
static LLVMValueRef get_coro_alloc_fn_val(CodeGen *g) {
|
||||
if (g->coro_alloc_fn_val)
|
||||
return g->coro_alloc_fn_val;
|
||||
|
||||
LLVMTypeRef param_types[] = {
|
||||
ZigLLVMTokenTypeInContext(LLVMGetGlobalContext()),
|
||||
};
|
||||
LLVMTypeRef fn_type = LLVMFunctionType(LLVMInt1Type(), param_types, 1, false);
|
||||
Buf *name = buf_sprintf("llvm.coro.alloc");
|
||||
g->coro_alloc_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type);
|
||||
assert(LLVMGetIntrinsicID(g->coro_alloc_fn_val));
|
||||
|
||||
return g->coro_alloc_fn_val;
|
||||
}
|
||||
|
||||
static LLVMValueRef get_coro_size_fn_val(CodeGen *g) {
|
||||
if (g->coro_size_fn_val)
|
||||
return g->coro_size_fn_val;
|
||||
|
||||
LLVMTypeRef fn_type = LLVMFunctionType(g->builtin_types.entry_usize->type_ref, nullptr, 0, false);
|
||||
Buf *name = buf_sprintf("llvm.coro.size.i%d", g->pointer_size_bytes * 8);
|
||||
g->coro_size_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type);
|
||||
assert(LLVMGetIntrinsicID(g->coro_size_fn_val));
|
||||
|
||||
return g->coro_size_fn_val;
|
||||
}
|
||||
|
||||
static LLVMValueRef get_coro_begin_fn_val(CodeGen *g) {
|
||||
if (g->coro_begin_fn_val)
|
||||
return g->coro_begin_fn_val;
|
||||
|
||||
LLVMTypeRef param_types[] = {
|
||||
ZigLLVMTokenTypeInContext(LLVMGetGlobalContext()),
|
||||
LLVMPointerType(LLVMInt8Type(), 0),
|
||||
};
|
||||
LLVMTypeRef fn_type = LLVMFunctionType(LLVMPointerType(LLVMInt8Type(), 0), param_types, 2, false);
|
||||
Buf *name = buf_sprintf("llvm.coro.begin");
|
||||
g->coro_begin_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type);
|
||||
assert(LLVMGetIntrinsicID(g->coro_begin_fn_val));
|
||||
|
||||
return g->coro_begin_fn_val;
|
||||
}
|
||||
|
||||
static LLVMValueRef get_coro_suspend_fn_val(CodeGen *g) {
|
||||
if (g->coro_suspend_fn_val)
|
||||
return g->coro_suspend_fn_val;
|
||||
|
||||
LLVMTypeRef param_types[] = {
|
||||
ZigLLVMTokenTypeInContext(LLVMGetGlobalContext()),
|
||||
LLVMInt1Type(),
|
||||
};
|
||||
LLVMTypeRef fn_type = LLVMFunctionType(LLVMInt8Type(), param_types, 2, false);
|
||||
Buf *name = buf_sprintf("llvm.coro.suspend");
|
||||
g->coro_suspend_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type);
|
||||
assert(LLVMGetIntrinsicID(g->coro_suspend_fn_val));
|
||||
|
||||
return g->coro_suspend_fn_val;
|
||||
}
|
||||
|
||||
static LLVMValueRef get_coro_end_fn_val(CodeGen *g) {
|
||||
if (g->coro_end_fn_val)
|
||||
return g->coro_end_fn_val;
|
||||
|
||||
LLVMTypeRef param_types[] = {
|
||||
LLVMPointerType(LLVMInt8Type(), 0),
|
||||
LLVMInt1Type(),
|
||||
};
|
||||
LLVMTypeRef fn_type = LLVMFunctionType(LLVMInt1Type(), param_types, 2, false);
|
||||
Buf *name = buf_sprintf("llvm.coro.end");
|
||||
g->coro_end_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type);
|
||||
assert(LLVMGetIntrinsicID(g->coro_end_fn_val));
|
||||
|
||||
return g->coro_end_fn_val;
|
||||
}
|
||||
|
||||
static LLVMValueRef get_coro_free_fn_val(CodeGen *g) {
|
||||
if (g->coro_free_fn_val)
|
||||
return g->coro_free_fn_val;
|
||||
|
||||
LLVMTypeRef param_types[] = {
|
||||
ZigLLVMTokenTypeInContext(LLVMGetGlobalContext()),
|
||||
LLVMPointerType(LLVMInt8Type(), 0),
|
||||
};
|
||||
LLVMTypeRef fn_type = LLVMFunctionType(LLVMPointerType(LLVMInt8Type(), 0), param_types, 2, false);
|
||||
Buf *name = buf_sprintf("llvm.coro.free");
|
||||
g->coro_free_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type);
|
||||
assert(LLVMGetIntrinsicID(g->coro_free_fn_val));
|
||||
|
||||
return g->coro_free_fn_val;
|
||||
}
|
||||
|
||||
static LLVMValueRef get_coro_resume_fn_val(CodeGen *g) {
|
||||
if (g->coro_resume_fn_val)
|
||||
return g->coro_resume_fn_val;
|
||||
|
||||
LLVMTypeRef param_types[] = {
|
||||
LLVMPointerType(LLVMInt8Type(), 0),
|
||||
};
|
||||
LLVMTypeRef fn_type = LLVMFunctionType(LLVMVoidType(), param_types, 1, false);
|
||||
Buf *name = buf_sprintf("llvm.coro.resume");
|
||||
g->coro_resume_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type);
|
||||
assert(LLVMGetIntrinsicID(g->coro_resume_fn_val));
|
||||
|
||||
return g->coro_resume_fn_val;
|
||||
}
|
||||
|
||||
static LLVMValueRef get_coro_save_fn_val(CodeGen *g) {
|
||||
if (g->coro_save_fn_val)
|
||||
return g->coro_save_fn_val;
|
||||
|
||||
LLVMTypeRef param_types[] = {
|
||||
LLVMPointerType(LLVMInt8Type(), 0),
|
||||
};
|
||||
LLVMTypeRef fn_type = LLVMFunctionType(ZigLLVMTokenTypeInContext(LLVMGetGlobalContext()), param_types, 1, false);
|
||||
Buf *name = buf_sprintf("llvm.coro.save");
|
||||
g->coro_save_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type);
|
||||
assert(LLVMGetIntrinsicID(g->coro_save_fn_val));
|
||||
|
||||
return g->coro_save_fn_val;
|
||||
}
|
||||
|
||||
static LLVMValueRef get_coro_promise_fn_val(CodeGen *g) {
|
||||
if (g->coro_promise_fn_val)
|
||||
return g->coro_promise_fn_val;
|
||||
|
||||
LLVMTypeRef param_types[] = {
|
||||
LLVMPointerType(LLVMInt8Type(), 0),
|
||||
LLVMInt32Type(),
|
||||
LLVMInt1Type(),
|
||||
};
|
||||
LLVMTypeRef fn_type = LLVMFunctionType(LLVMPointerType(LLVMInt8Type(), 0), param_types, 3, false);
|
||||
Buf *name = buf_sprintf("llvm.coro.promise");
|
||||
g->coro_promise_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type);
|
||||
assert(LLVMGetIntrinsicID(g->coro_promise_fn_val));
|
||||
|
||||
return g->coro_promise_fn_val;
|
||||
}
|
||||
|
||||
static LLVMValueRef get_return_address_fn_val(CodeGen *g) {
|
||||
if (g->return_address_fn_val)
|
||||
return g->return_address_fn_val;
|
||||
@ -2506,6 +2679,25 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutable *executable, IrI
|
||||
}
|
||||
}
|
||||
|
||||
static bool get_prefix_arg_err_ret_stack(CodeGen *g, FnTypeId *fn_type_id) {
|
||||
return g->have_err_ret_tracing &&
|
||||
(fn_type_id->return_type->id == TypeTableEntryIdErrorUnion ||
|
||||
fn_type_id->return_type->id == TypeTableEntryIdErrorSet ||
|
||||
fn_type_id->cc == CallingConventionAsync);
|
||||
}
|
||||
|
||||
static size_t get_async_allocator_arg_index(CodeGen *g, FnTypeId *fn_type_id) {
|
||||
// 0 1 2 3
|
||||
// err_ret_stack allocator_ptr err_code other_args...
|
||||
return get_prefix_arg_err_ret_stack(g, fn_type_id) ? 1 : 0;
|
||||
}
|
||||
|
||||
static size_t get_async_err_code_arg_index(CodeGen *g, FnTypeId *fn_type_id) {
|
||||
// 0 1 2 3
|
||||
// err_ret_stack allocator_ptr err_code other_args...
|
||||
return 1 + get_async_allocator_arg_index(g, fn_type_id);
|
||||
}
|
||||
|
||||
static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstructionCall *instruction) {
|
||||
LLVMValueRef fn_val;
|
||||
TypeTableEntry *fn_type;
|
||||
@ -2519,11 +2711,15 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr
|
||||
}
|
||||
|
||||
FnTypeId *fn_type_id = &fn_type->data.fn.fn_type_id;
|
||||
|
||||
TypeTableEntry *src_return_type = fn_type_id->return_type;
|
||||
bool ret_has_bits = type_has_bits(src_return_type);
|
||||
bool first_arg_ret = ret_has_bits && handle_is_ptr(src_return_type);
|
||||
bool prefix_arg_err_ret_stack = g->have_err_ret_tracing && (src_return_type->id == TypeTableEntryIdErrorUnion || src_return_type->id == TypeTableEntryIdErrorSet);
|
||||
size_t actual_param_count = instruction->arg_count + (first_arg_ret ? 1 : 0) + (prefix_arg_err_ret_stack ? 1 : 0);
|
||||
|
||||
bool first_arg_ret = ret_has_bits && handle_is_ptr(src_return_type) &&
|
||||
calling_convention_does_first_arg_return(fn_type->data.fn.fn_type_id.cc);
|
||||
bool prefix_arg_err_ret_stack = get_prefix_arg_err_ret_stack(g, fn_type_id);
|
||||
// +2 for the async args
|
||||
size_t actual_param_count = instruction->arg_count + (first_arg_ret ? 1 : 0) + (prefix_arg_err_ret_stack ? 1 : 0) + 2;
|
||||
bool is_var_args = fn_type_id->is_var_args;
|
||||
LLVMValueRef *gen_param_values = allocate<LLVMValueRef>(actual_param_count);
|
||||
size_t gen_param_index = 0;
|
||||
@ -2535,6 +2731,14 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr
|
||||
gen_param_values[gen_param_index] = g->cur_err_ret_trace_val;
|
||||
gen_param_index += 1;
|
||||
}
|
||||
if (instruction->is_async) {
|
||||
gen_param_values[gen_param_index] = ir_llvm_value(g, instruction->async_allocator);
|
||||
gen_param_index += 1;
|
||||
|
||||
LLVMValueRef err_val_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, err_union_err_index, "");
|
||||
gen_param_values[gen_param_index] = err_val_ptr;
|
||||
gen_param_index += 1;
|
||||
}
|
||||
for (size_t call_i = 0; call_i < instruction->arg_count; call_i += 1) {
|
||||
IrInstruction *param_instruction = instruction->args[call_i];
|
||||
TypeTableEntry *param_type = param_instruction->value.type;
|
||||
@ -2572,6 +2776,12 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr
|
||||
}
|
||||
}
|
||||
|
||||
if (instruction->is_async) {
|
||||
LLVMValueRef payload_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, err_union_payload_index, "");
|
||||
LLVMBuildStore(g->builder, result, payload_ptr);
|
||||
return instruction->tmp_ptr;
|
||||
}
|
||||
|
||||
if (src_return_type->id == TypeTableEntryIdUnreachable) {
|
||||
return LLVMBuildUnreachable(g->builder);
|
||||
} else if (!ret_has_bits) {
|
||||
@ -2783,7 +2993,7 @@ static LLVMValueRef gen_non_null_bit(CodeGen *g, TypeTableEntry *maybe_type, LLV
|
||||
if (child_type->zero_bits) {
|
||||
return maybe_handle;
|
||||
} else {
|
||||
bool maybe_is_ptr = (child_type->id == TypeTableEntryIdPointer || child_type->id == TypeTableEntryIdFn);
|
||||
bool maybe_is_ptr = type_is_codegen_pointer(child_type);
|
||||
if (maybe_is_ptr) {
|
||||
return LLVMBuildICmp(g->builder, LLVMIntNE, maybe_handle, LLVMConstNull(maybe_type->type_ref), "");
|
||||
} else {
|
||||
@ -2823,7 +3033,7 @@ static LLVMValueRef ir_render_unwrap_maybe(CodeGen *g, IrExecutable *executable,
|
||||
if (child_type->zero_bits) {
|
||||
return nullptr;
|
||||
} else {
|
||||
bool maybe_is_ptr = (child_type->id == TypeTableEntryIdPointer || child_type->id == TypeTableEntryIdFn);
|
||||
bool maybe_is_ptr = type_is_codegen_pointer(child_type);
|
||||
if (maybe_is_ptr) {
|
||||
return maybe_ptr;
|
||||
} else {
|
||||
@ -3046,6 +3256,10 @@ static LLVMValueRef ir_render_align_cast(CodeGen *g, IrExecutable *executable, I
|
||||
{
|
||||
align_bytes = target_type->data.maybe.child_type->data.fn.fn_type_id.alignment;
|
||||
ptr_val = target_val;
|
||||
} else if (target_type->id == TypeTableEntryIdMaybe &&
|
||||
target_type->data.maybe.child_type->id == TypeTableEntryIdPromise)
|
||||
{
|
||||
zig_panic("TODO audit this function");
|
||||
} else if (target_type->id == TypeTableEntryIdStruct && target_type->data.structure.is_slice) {
|
||||
TypeTableEntry *slice_ptr_type = target_type->data.structure.fields[slice_ptr_index].type_entry;
|
||||
align_bytes = slice_ptr_type->data.pointer.alignment;
|
||||
@ -3088,6 +3302,20 @@ static LLVMValueRef ir_render_error_return_trace(CodeGen *g, IrExecutable *execu
|
||||
return g->cur_err_ret_trace_val;
|
||||
}
|
||||
|
||||
static LLVMValueRef ir_render_cancel(CodeGen *g, IrExecutable *executable, IrInstructionCancel *instruction) {
|
||||
LLVMValueRef target_handle = ir_llvm_value(g, instruction->target);
|
||||
LLVMBuildCall(g->builder, get_coro_destroy_fn_val(g), &target_handle, 1, "");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static LLVMValueRef ir_render_get_implicit_allocator(CodeGen *g, IrExecutable *executable,
|
||||
IrInstructionGetImplicitAllocator *instruction)
|
||||
{
|
||||
assert(instruction->id == ImplicitAllocatorIdArg);
|
||||
size_t allocator_arg_index = get_async_allocator_arg_index(g, &g->cur_fn->type_entry->data.fn.fn_type_id);
|
||||
return LLVMGetParam(g->cur_fn_val, allocator_arg_index);
|
||||
}
|
||||
|
||||
static LLVMAtomicOrdering to_LLVMAtomicOrdering(AtomicOrder atomic_order) {
|
||||
switch (atomic_order) {
|
||||
case AtomicOrderUnordered: return LLVMAtomicOrderingUnordered;
|
||||
@ -3100,6 +3328,23 @@ static LLVMAtomicOrdering to_LLVMAtomicOrdering(AtomicOrder atomic_order) {
|
||||
zig_unreachable();
|
||||
}
|
||||
|
||||
static LLVMAtomicRMWBinOp to_LLVMAtomicRMWBinOp(AtomicRmwOp op, bool is_signed) {
|
||||
switch (op) {
|
||||
case AtomicRmwOp_xchg: return LLVMAtomicRMWBinOpXchg;
|
||||
case AtomicRmwOp_add: return LLVMAtomicRMWBinOpAdd;
|
||||
case AtomicRmwOp_sub: return LLVMAtomicRMWBinOpSub;
|
||||
case AtomicRmwOp_and: return LLVMAtomicRMWBinOpAnd;
|
||||
case AtomicRmwOp_nand: return LLVMAtomicRMWBinOpNand;
|
||||
case AtomicRmwOp_or: return LLVMAtomicRMWBinOpOr;
|
||||
case AtomicRmwOp_xor: return LLVMAtomicRMWBinOpXor;
|
||||
case AtomicRmwOp_max:
|
||||
return is_signed ? LLVMAtomicRMWBinOpMax : LLVMAtomicRMWBinOpUMax;
|
||||
case AtomicRmwOp_min:
|
||||
return is_signed ? LLVMAtomicRMWBinOpMin : LLVMAtomicRMWBinOpUMin;
|
||||
}
|
||||
zig_unreachable();
|
||||
}
|
||||
|
||||
static LLVMValueRef ir_render_cmpxchg(CodeGen *g, IrExecutable *executable, IrInstructionCmpxchg *instruction) {
|
||||
LLVMValueRef ptr_val = ir_llvm_value(g, instruction->ptr);
|
||||
LLVMValueRef cmp_val = ir_llvm_value(g, instruction->cmp_value);
|
||||
@ -3508,9 +3753,7 @@ static LLVMValueRef ir_render_maybe_wrap(CodeGen *g, IrExecutable *executable, I
|
||||
}
|
||||
|
||||
LLVMValueRef payload_val = ir_llvm_value(g, instruction->value);
|
||||
if (child_type->id == TypeTableEntryIdPointer ||
|
||||
child_type->id == TypeTableEntryIdFn)
|
||||
{
|
||||
if (type_is_codegen_pointer(child_type)) {
|
||||
return payload_val;
|
||||
}
|
||||
|
||||
@ -3682,6 +3925,264 @@ static LLVMValueRef ir_render_panic(CodeGen *g, IrExecutable *executable, IrInst
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static LLVMValueRef ir_render_coro_id(CodeGen *g, IrExecutable *executable, IrInstructionCoroId *instruction) {
|
||||
LLVMValueRef promise_ptr = ir_llvm_value(g, instruction->promise_ptr);
|
||||
LLVMValueRef align_val = LLVMConstInt(LLVMInt32Type(), get_coro_frame_align_bytes(g), false);
|
||||
LLVMValueRef null = LLVMConstIntToPtr(LLVMConstNull(g->builtin_types.entry_usize->type_ref),
|
||||
LLVMPointerType(LLVMInt8Type(), 0));
|
||||
LLVMValueRef params[] = {
|
||||
align_val,
|
||||
promise_ptr,
|
||||
null,
|
||||
null,
|
||||
};
|
||||
return LLVMBuildCall(g->builder, get_coro_id_fn_val(g), params, 4, "");
|
||||
}
|
||||
|
||||
static LLVMValueRef ir_render_coro_alloc(CodeGen *g, IrExecutable *executable, IrInstructionCoroAlloc *instruction) {
|
||||
LLVMValueRef token = ir_llvm_value(g, instruction->coro_id);
|
||||
return LLVMBuildCall(g->builder, get_coro_alloc_fn_val(g), &token, 1, "");
|
||||
}
|
||||
|
||||
static LLVMValueRef ir_render_coro_size(CodeGen *g, IrExecutable *executable, IrInstructionCoroSize *instruction) {
|
||||
return LLVMBuildCall(g->builder, get_coro_size_fn_val(g), nullptr, 0, "");
|
||||
}
|
||||
|
||||
static LLVMValueRef ir_render_coro_begin(CodeGen *g, IrExecutable *executable, IrInstructionCoroBegin *instruction) {
|
||||
LLVMValueRef coro_id = ir_llvm_value(g, instruction->coro_id);
|
||||
LLVMValueRef coro_mem_ptr = ir_llvm_value(g, instruction->coro_mem_ptr);
|
||||
LLVMValueRef params[] = {
|
||||
coro_id,
|
||||
coro_mem_ptr,
|
||||
};
|
||||
return LLVMBuildCall(g->builder, get_coro_begin_fn_val(g), params, 2, "");
|
||||
}
|
||||
|
||||
static LLVMValueRef ir_render_coro_alloc_fail(CodeGen *g, IrExecutable *executable,
|
||||
IrInstructionCoroAllocFail *instruction)
|
||||
{
|
||||
size_t err_code_ptr_arg_index = get_async_err_code_arg_index(g, &g->cur_fn->type_entry->data.fn.fn_type_id);
|
||||
LLVMValueRef err_code_ptr_val = LLVMGetParam(g->cur_fn_val, err_code_ptr_arg_index);
|
||||
LLVMValueRef err_code = ir_llvm_value(g, instruction->err_val);
|
||||
LLVMBuildStore(g->builder, err_code, err_code_ptr_val);
|
||||
|
||||
LLVMValueRef return_value;
|
||||
if (ir_want_runtime_safety(g, &instruction->base)) {
|
||||
return_value = LLVMConstNull(LLVMPointerType(LLVMInt8Type(), 0));
|
||||
} else {
|
||||
return_value = LLVMGetUndef(LLVMPointerType(LLVMInt8Type(), 0));
|
||||
}
|
||||
LLVMBuildRet(g->builder, return_value);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static LLVMValueRef ir_render_coro_suspend(CodeGen *g, IrExecutable *executable, IrInstructionCoroSuspend *instruction) {
|
||||
LLVMValueRef save_point;
|
||||
if (instruction->save_point == nullptr) {
|
||||
save_point = LLVMConstNull(ZigLLVMTokenTypeInContext(LLVMGetGlobalContext()));
|
||||
} else {
|
||||
save_point = ir_llvm_value(g, instruction->save_point);
|
||||
}
|
||||
LLVMValueRef is_final = ir_llvm_value(g, instruction->is_final);
|
||||
LLVMValueRef params[] = {
|
||||
save_point,
|
||||
is_final,
|
||||
};
|
||||
return LLVMBuildCall(g->builder, get_coro_suspend_fn_val(g), params, 2, "");
|
||||
}
|
||||
|
||||
static LLVMValueRef ir_render_coro_end(CodeGen *g, IrExecutable *executable, IrInstructionCoroEnd *instruction) {
|
||||
LLVMValueRef params[] = {
|
||||
LLVMConstNull(LLVMPointerType(LLVMInt8Type(), 0)),
|
||||
LLVMConstNull(LLVMInt1Type()),
|
||||
};
|
||||
return LLVMBuildCall(g->builder, get_coro_end_fn_val(g), params, 2, "");
|
||||
}
|
||||
|
||||
static LLVMValueRef ir_render_coro_free(CodeGen *g, IrExecutable *executable, IrInstructionCoroFree *instruction) {
|
||||
LLVMValueRef coro_id = ir_llvm_value(g, instruction->coro_id);
|
||||
LLVMValueRef coro_handle = ir_llvm_value(g, instruction->coro_handle);
|
||||
LLVMValueRef params[] = {
|
||||
coro_id,
|
||||
coro_handle,
|
||||
};
|
||||
return LLVMBuildCall(g->builder, get_coro_free_fn_val(g), params, 2, "");
|
||||
}
|
||||
|
||||
static LLVMValueRef ir_render_coro_resume(CodeGen *g, IrExecutable *executable, IrInstructionCoroResume *instruction) {
|
||||
LLVMValueRef awaiter_handle = ir_llvm_value(g, instruction->awaiter_handle);
|
||||
return LLVMBuildCall(g->builder, get_coro_resume_fn_val(g), &awaiter_handle, 1, "");
|
||||
}
|
||||
|
||||
static LLVMValueRef ir_render_coro_save(CodeGen *g, IrExecutable *executable, IrInstructionCoroSave *instruction) {
|
||||
LLVMValueRef coro_handle = ir_llvm_value(g, instruction->coro_handle);
|
||||
return LLVMBuildCall(g->builder, get_coro_save_fn_val(g), &coro_handle, 1, "");
|
||||
}
|
||||
|
||||
static LLVMValueRef ir_render_coro_promise(CodeGen *g, IrExecutable *executable, IrInstructionCoroPromise *instruction) {
|
||||
LLVMValueRef coro_handle = ir_llvm_value(g, instruction->coro_handle);
|
||||
LLVMValueRef params[] = {
|
||||
coro_handle,
|
||||
LLVMConstInt(LLVMInt32Type(), get_coro_frame_align_bytes(g), false),
|
||||
LLVMConstNull(LLVMInt1Type()),
|
||||
};
|
||||
LLVMValueRef uncasted_result = LLVMBuildCall(g->builder, get_coro_promise_fn_val(g), params, 3, "");
|
||||
return LLVMBuildBitCast(g->builder, uncasted_result, instruction->base.value.type->type_ref, "");
|
||||
}
|
||||
|
||||
static LLVMValueRef get_coro_alloc_helper_fn_val(CodeGen *g, LLVMTypeRef alloc_fn_type_ref, TypeTableEntry *fn_type) {
|
||||
if (g->coro_alloc_helper_fn_val != nullptr)
|
||||
return g->coro_alloc_helper_fn_val;
|
||||
|
||||
assert(fn_type->id == TypeTableEntryIdFn);
|
||||
|
||||
TypeTableEntry *ptr_to_err_code_type = get_pointer_to_type(g, g->builtin_types.entry_global_error_set, false);
|
||||
|
||||
LLVMTypeRef alloc_raw_fn_type_ref = LLVMGetElementType(alloc_fn_type_ref);
|
||||
LLVMTypeRef *alloc_fn_arg_types = allocate<LLVMTypeRef>(LLVMCountParamTypes(alloc_raw_fn_type_ref));
|
||||
LLVMGetParamTypes(alloc_raw_fn_type_ref, alloc_fn_arg_types);
|
||||
|
||||
ZigList<LLVMTypeRef> arg_types = {};
|
||||
arg_types.append(alloc_fn_type_ref);
|
||||
if (g->have_err_ret_tracing) {
|
||||
arg_types.append(alloc_fn_arg_types[1]);
|
||||
}
|
||||
arg_types.append(alloc_fn_arg_types[g->have_err_ret_tracing ? 2 : 1]);
|
||||
arg_types.append(ptr_to_err_code_type->type_ref);
|
||||
arg_types.append(g->builtin_types.entry_usize->type_ref);
|
||||
|
||||
LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMPointerType(LLVMInt8Type(), 0),
|
||||
arg_types.items, arg_types.length, false);
|
||||
|
||||
Buf *fn_name = get_mangled_name(g, buf_create_from_str("__zig_coro_alloc_helper"), false);
|
||||
LLVMValueRef fn_val = LLVMAddFunction(g->module, buf_ptr(fn_name), fn_type_ref);
|
||||
LLVMSetLinkage(fn_val, LLVMInternalLinkage);
|
||||
LLVMSetFunctionCallConv(fn_val, get_llvm_cc(g, CallingConventionUnspecified));
|
||||
addLLVMFnAttr(fn_val, "nounwind");
|
||||
addLLVMArgAttr(fn_val, (unsigned)0, "nonnull");
|
||||
addLLVMArgAttr(fn_val, (unsigned)1, "nonnull");
|
||||
|
||||
LLVMBasicBlockRef prev_block = LLVMGetInsertBlock(g->builder);
|
||||
LLVMValueRef prev_debug_location = LLVMGetCurrentDebugLocation(g->builder);
|
||||
FnTableEntry *prev_cur_fn = g->cur_fn;
|
||||
LLVMValueRef prev_cur_fn_val = g->cur_fn_val;
|
||||
|
||||
LLVMBasicBlockRef entry_block = LLVMAppendBasicBlock(fn_val, "Entry");
|
||||
LLVMPositionBuilderAtEnd(g->builder, entry_block);
|
||||
ZigLLVMClearCurrentDebugLocation(g->builder);
|
||||
g->cur_fn = nullptr;
|
||||
g->cur_fn_val = fn_val;
|
||||
|
||||
LLVMValueRef sret_ptr = LLVMBuildAlloca(g->builder, LLVMGetElementType(alloc_fn_arg_types[0]), "");
|
||||
|
||||
size_t next_arg = 0;
|
||||
LLVMValueRef alloc_fn_val = LLVMGetParam(fn_val, next_arg);
|
||||
next_arg += 1;
|
||||
|
||||
LLVMValueRef stack_trace_val;
|
||||
if (g->have_err_ret_tracing) {
|
||||
stack_trace_val = LLVMGetParam(fn_val, next_arg);
|
||||
next_arg += 1;
|
||||
}
|
||||
|
||||
LLVMValueRef allocator_val = LLVMGetParam(fn_val, next_arg);
|
||||
next_arg += 1;
|
||||
LLVMValueRef err_code_ptr = LLVMGetParam(fn_val, next_arg);
|
||||
next_arg += 1;
|
||||
LLVMValueRef coro_size = LLVMGetParam(fn_val, next_arg);
|
||||
next_arg += 1;
|
||||
LLVMValueRef alignment_val = LLVMConstInt(g->builtin_types.entry_u29->type_ref,
|
||||
get_coro_frame_align_bytes(g), false);
|
||||
|
||||
ZigList<LLVMValueRef> args = {};
|
||||
args.append(sret_ptr);
|
||||
if (g->have_err_ret_tracing) {
|
||||
args.append(stack_trace_val);
|
||||
}
|
||||
args.append(allocator_val);
|
||||
args.append(coro_size);
|
||||
args.append(alignment_val);
|
||||
ZigLLVMBuildCall(g->builder, alloc_fn_val, args.items, args.length,
|
||||
get_llvm_cc(g, CallingConventionUnspecified), ZigLLVM_FnInlineAuto, "");
|
||||
LLVMValueRef err_val_ptr = LLVMBuildStructGEP(g->builder, sret_ptr, err_union_err_index, "");
|
||||
LLVMValueRef err_val = LLVMBuildLoad(g->builder, err_val_ptr, "");
|
||||
LLVMBuildStore(g->builder, err_val, err_code_ptr);
|
||||
LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, err_val, LLVMConstNull(LLVMTypeOf(err_val)), "");
|
||||
LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(fn_val, "AllocOk");
|
||||
LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(fn_val, "AllocFail");
|
||||
LLVMBuildCondBr(g->builder, ok_bit, ok_block, fail_block);
|
||||
|
||||
LLVMPositionBuilderAtEnd(g->builder, ok_block);
|
||||
LLVMValueRef payload_ptr = LLVMBuildStructGEP(g->builder, sret_ptr, err_union_payload_index, "");
|
||||
TypeTableEntry *u8_ptr_type = get_pointer_to_type(g, g->builtin_types.entry_u8, false);
|
||||
TypeTableEntry *slice_type = get_slice_type(g, u8_ptr_type);
|
||||
size_t ptr_field_index = slice_type->data.structure.fields[slice_ptr_index].gen_index;
|
||||
LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, payload_ptr, ptr_field_index, "");
|
||||
LLVMValueRef ptr_val = LLVMBuildLoad(g->builder, ptr_field_ptr, "");
|
||||
LLVMBuildRet(g->builder, ptr_val);
|
||||
|
||||
LLVMPositionBuilderAtEnd(g->builder, fail_block);
|
||||
LLVMBuildRet(g->builder, LLVMConstNull(LLVMPointerType(LLVMInt8Type(), 0)));
|
||||
|
||||
g->cur_fn = prev_cur_fn;
|
||||
g->cur_fn_val = prev_cur_fn_val;
|
||||
LLVMPositionBuilderAtEnd(g->builder, prev_block);
|
||||
LLVMSetCurrentDebugLocation(g->builder, prev_debug_location);
|
||||
|
||||
g->coro_alloc_helper_fn_val = fn_val;
|
||||
return fn_val;
|
||||
}
|
||||
|
||||
static LLVMValueRef ir_render_coro_alloc_helper(CodeGen *g, IrExecutable *executable,
|
||||
IrInstructionCoroAllocHelper *instruction)
|
||||
{
|
||||
LLVMValueRef alloc_fn = ir_llvm_value(g, instruction->alloc_fn);
|
||||
LLVMValueRef coro_size = ir_llvm_value(g, instruction->coro_size);
|
||||
LLVMValueRef fn_val = get_coro_alloc_helper_fn_val(g, LLVMTypeOf(alloc_fn), instruction->alloc_fn->value.type);
|
||||
size_t err_code_ptr_arg_index = get_async_err_code_arg_index(g, &g->cur_fn->type_entry->data.fn.fn_type_id);
|
||||
size_t allocator_arg_index = get_async_allocator_arg_index(g, &g->cur_fn->type_entry->data.fn.fn_type_id);
|
||||
|
||||
ZigList<LLVMValueRef> params = {};
|
||||
params.append(alloc_fn);
|
||||
uint32_t err_ret_trace_arg_index = get_err_ret_trace_arg_index(g, g->cur_fn);
|
||||
if (err_ret_trace_arg_index != UINT32_MAX) {
|
||||
params.append(LLVMGetParam(g->cur_fn_val, err_ret_trace_arg_index));
|
||||
}
|
||||
params.append(LLVMGetParam(g->cur_fn_val, allocator_arg_index));
|
||||
params.append(LLVMGetParam(g->cur_fn_val, err_code_ptr_arg_index));
|
||||
params.append(coro_size);
|
||||
|
||||
return ZigLLVMBuildCall(g->builder, fn_val, params.items, params.length,
|
||||
get_llvm_cc(g, CallingConventionUnspecified), ZigLLVM_FnInlineAuto, "");
|
||||
}
|
||||
|
||||
static LLVMValueRef ir_render_atomic_rmw(CodeGen *g, IrExecutable *executable,
|
||||
IrInstructionAtomicRmw *instruction)
|
||||
{
|
||||
bool is_signed;
|
||||
TypeTableEntry *operand_type = instruction->operand->value.type;
|
||||
if (operand_type->id == TypeTableEntryIdInt) {
|
||||
is_signed = operand_type->data.integral.is_signed;
|
||||
} else {
|
||||
is_signed = false;
|
||||
}
|
||||
LLVMAtomicRMWBinOp op = to_LLVMAtomicRMWBinOp(instruction->resolved_op, is_signed);
|
||||
LLVMAtomicOrdering ordering = to_LLVMAtomicOrdering(instruction->resolved_ordering);
|
||||
LLVMValueRef ptr = ir_llvm_value(g, instruction->ptr);
|
||||
LLVMValueRef operand = ir_llvm_value(g, instruction->operand);
|
||||
|
||||
if (get_codegen_ptr_type(operand_type) == nullptr) {
|
||||
return LLVMBuildAtomicRMW(g->builder, op, ptr, operand, ordering, false);
|
||||
}
|
||||
|
||||
// it's a pointer but we need to treat it as an int
|
||||
LLVMValueRef casted_ptr = LLVMBuildBitCast(g->builder, ptr,
|
||||
LLVMPointerType(g->builtin_types.entry_usize->type_ref, 0), "");
|
||||
LLVMValueRef casted_operand = LLVMBuildPtrToInt(g->builder, operand, g->builtin_types.entry_usize->type_ref, "");
|
||||
LLVMValueRef uncasted_result = LLVMBuildAtomicRMW(g->builder, op, casted_ptr, casted_operand, ordering, false);
|
||||
return LLVMBuildIntToPtr(g->builder, uncasted_result, operand_type->type_ref, "");
|
||||
}
|
||||
|
||||
static void set_debug_location(CodeGen *g, IrInstruction *instruction) {
|
||||
AstNode *source_node = instruction->source_node;
|
||||
Scope *scope = instruction->scope;
|
||||
@ -3745,7 +4246,9 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
|
||||
case IrInstructionIdTagType:
|
||||
case IrInstructionIdExport:
|
||||
case IrInstructionIdErrorUnion:
|
||||
case IrInstructionIdPromiseResultType:
|
||||
zig_unreachable();
|
||||
|
||||
case IrInstructionIdReturn:
|
||||
return ir_render_return(g, executable, (IrInstructionReturn *)instruction);
|
||||
case IrInstructionIdDeclVar:
|
||||
@ -3862,12 +4365,43 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
|
||||
return ir_render_align_cast(g, executable, (IrInstructionAlignCast *)instruction);
|
||||
case IrInstructionIdErrorReturnTrace:
|
||||
return ir_render_error_return_trace(g, executable, (IrInstructionErrorReturnTrace *)instruction);
|
||||
case IrInstructionIdCancel:
|
||||
return ir_render_cancel(g, executable, (IrInstructionCancel *)instruction);
|
||||
case IrInstructionIdGetImplicitAllocator:
|
||||
return ir_render_get_implicit_allocator(g, executable, (IrInstructionGetImplicitAllocator *)instruction);
|
||||
case IrInstructionIdCoroId:
|
||||
return ir_render_coro_id(g, executable, (IrInstructionCoroId *)instruction);
|
||||
case IrInstructionIdCoroAlloc:
|
||||
return ir_render_coro_alloc(g, executable, (IrInstructionCoroAlloc *)instruction);
|
||||
case IrInstructionIdCoroSize:
|
||||
return ir_render_coro_size(g, executable, (IrInstructionCoroSize *)instruction);
|
||||
case IrInstructionIdCoroBegin:
|
||||
return ir_render_coro_begin(g, executable, (IrInstructionCoroBegin *)instruction);
|
||||
case IrInstructionIdCoroAllocFail:
|
||||
return ir_render_coro_alloc_fail(g, executable, (IrInstructionCoroAllocFail *)instruction);
|
||||
case IrInstructionIdCoroSuspend:
|
||||
return ir_render_coro_suspend(g, executable, (IrInstructionCoroSuspend *)instruction);
|
||||
case IrInstructionIdCoroEnd:
|
||||
return ir_render_coro_end(g, executable, (IrInstructionCoroEnd *)instruction);
|
||||
case IrInstructionIdCoroFree:
|
||||
return ir_render_coro_free(g, executable, (IrInstructionCoroFree *)instruction);
|
||||
case IrInstructionIdCoroResume:
|
||||
return ir_render_coro_resume(g, executable, (IrInstructionCoroResume *)instruction);
|
||||
case IrInstructionIdCoroSave:
|
||||
return ir_render_coro_save(g, executable, (IrInstructionCoroSave *)instruction);
|
||||
case IrInstructionIdCoroPromise:
|
||||
return ir_render_coro_promise(g, executable, (IrInstructionCoroPromise *)instruction);
|
||||
case IrInstructionIdCoroAllocHelper:
|
||||
return ir_render_coro_alloc_helper(g, executable, (IrInstructionCoroAllocHelper *)instruction);
|
||||
case IrInstructionIdAtomicRmw:
|
||||
return ir_render_atomic_rmw(g, executable, (IrInstructionAtomicRmw *)instruction);
|
||||
}
|
||||
zig_unreachable();
|
||||
}
|
||||
|
||||
static void ir_render(CodeGen *g, FnTableEntry *fn_entry) {
|
||||
assert(fn_entry);
|
||||
|
||||
IrExecutable *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) {
|
||||
@ -4009,6 +4543,7 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con
|
||||
case TypeTableEntryIdPointer:
|
||||
case TypeTableEntryIdFn:
|
||||
case TypeTableEntryIdMaybe:
|
||||
case TypeTableEntryIdPromise:
|
||||
{
|
||||
LLVMValueRef ptr_val = gen_const_val(g, const_val, "");
|
||||
LLVMValueRef ptr_size_int_val = LLVMConstPtrToInt(ptr_val, g->builtin_types.entry_usize->type_ref);
|
||||
@ -4104,9 +4639,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
|
||||
TypeTableEntry *child_type = type_entry->data.maybe.child_type;
|
||||
if (child_type->zero_bits) {
|
||||
return LLVMConstInt(LLVMInt1Type(), const_val->data.x_maybe ? 1 : 0, false);
|
||||
} else if (child_type->id == TypeTableEntryIdPointer ||
|
||||
child_type->id == TypeTableEntryIdFn)
|
||||
{
|
||||
} else if (type_is_codegen_pointer(child_type)) {
|
||||
if (const_val->data.x_maybe) {
|
||||
return gen_const_val(g, const_val->data.x_maybe, "");
|
||||
} else {
|
||||
@ -4426,6 +4959,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
|
||||
case TypeTableEntryIdVar:
|
||||
case TypeTableEntryIdArgTuple:
|
||||
case TypeTableEntryIdOpaque:
|
||||
case TypeTableEntryIdPromise:
|
||||
zig_unreachable();
|
||||
|
||||
}
|
||||
@ -5235,6 +5769,7 @@ static void define_builtin_types(CodeGen *g) {
|
||||
|
||||
g->builtin_types.entry_u8 = get_int_type(g, false, 8);
|
||||
g->builtin_types.entry_u16 = get_int_type(g, false, 16);
|
||||
g->builtin_types.entry_u29 = get_int_type(g, false, 29);
|
||||
g->builtin_types.entry_u32 = get_int_type(g, false, 32);
|
||||
g->builtin_types.entry_u64 = get_int_type(g, false, 64);
|
||||
g->builtin_types.entry_u128 = get_int_type(g, false, 128);
|
||||
@ -5271,6 +5806,10 @@ static void define_builtin_types(CodeGen *g) {
|
||||
|
||||
g->primitive_type_table.put(&entry->name, entry);
|
||||
}
|
||||
{
|
||||
TypeTableEntry *entry = get_promise_type(g, nullptr);
|
||||
g->primitive_type_table.put(&entry->name, entry);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -5348,6 +5887,7 @@ static void define_builtin_fns(CodeGen *g) {
|
||||
create_builtin_fn(g, BuiltinFnIdArgType, "ArgType", 2);
|
||||
create_builtin_fn(g, BuiltinFnIdExport, "export", 3);
|
||||
create_builtin_fn(g, BuiltinFnIdErrorReturnTrace, "errorReturnTrace", 0);
|
||||
create_builtin_fn(g, BuiltinFnIdAtomicRmw, "atomicRmw", 5);
|
||||
}
|
||||
|
||||
static const char *bool_to_str(bool b) {
|
||||
@ -5477,6 +6017,20 @@ static void define_builtin_compile_vars(CodeGen *g) {
|
||||
" SeqCst,\n"
|
||||
"};\n\n");
|
||||
}
|
||||
{
|
||||
buf_appendf(contents,
|
||||
"pub const AtomicRmwOp = enum {\n"
|
||||
" Xchg,\n"
|
||||
" Add,\n"
|
||||
" Sub,\n"
|
||||
" And,\n"
|
||||
" Nand,\n"
|
||||
" Or,\n"
|
||||
" Xor,\n"
|
||||
" Max,\n"
|
||||
" Min,\n"
|
||||
"};\n\n");
|
||||
}
|
||||
{
|
||||
buf_appendf(contents,
|
||||
"pub const Mode = enum {\n"
|
||||
@ -5898,6 +6452,7 @@ static void prepend_c_type_to_decl_list(CodeGen *g, GenH *gen_h, TypeTableEntry
|
||||
case TypeTableEntryIdArgTuple:
|
||||
case TypeTableEntryIdErrorUnion:
|
||||
case TypeTableEntryIdErrorSet:
|
||||
case TypeTableEntryIdPromise:
|
||||
zig_unreachable();
|
||||
case TypeTableEntryIdVoid:
|
||||
case TypeTableEntryIdUnreachable:
|
||||
@ -6027,9 +6582,7 @@ static void get_c_type(CodeGen *g, GenH *gen_h, TypeTableEntry *type_entry, Buf
|
||||
if (child_type->zero_bits) {
|
||||
buf_init_from_str(out_buf, "bool");
|
||||
return;
|
||||
} else if (child_type->id == TypeTableEntryIdPointer ||
|
||||
child_type->id == TypeTableEntryIdFn)
|
||||
{
|
||||
} else if (type_is_codegen_pointer(child_type)) {
|
||||
return get_c_type(g, gen_h, child_type, out_buf);
|
||||
} else {
|
||||
zig_unreachable();
|
||||
@ -6084,6 +6637,7 @@ static void get_c_type(CodeGen *g, GenH *gen_h, TypeTableEntry *type_entry, Buf
|
||||
case TypeTableEntryIdNullLit:
|
||||
case TypeTableEntryIdVar:
|
||||
case TypeTableEntryIdArgTuple:
|
||||
case TypeTableEntryIdPromise:
|
||||
zig_unreachable();
|
||||
}
|
||||
}
|
||||
@ -6244,6 +6798,7 @@ static void gen_h_file(CodeGen *g) {
|
||||
case TypeTableEntryIdArgTuple:
|
||||
case TypeTableEntryIdMaybe:
|
||||
case TypeTableEntryIdFn:
|
||||
case TypeTableEntryIdPromise:
|
||||
zig_unreachable();
|
||||
case TypeTableEntryIdEnum:
|
||||
assert(type_entry->data.enumeration.layout == ContainerLayoutExtern);
|
||||
|
1393
src/ir.cpp
1393
src/ir.cpp
File diff suppressed because it is too large
Load Diff
193
src/ir_print.cpp
193
src/ir_print.cpp
@ -198,6 +198,15 @@ static void ir_print_cast(IrPrint *irp, IrInstructionCast *cast_instruction) {
|
||||
}
|
||||
|
||||
static void ir_print_call(IrPrint *irp, IrInstructionCall *call_instruction) {
|
||||
if (call_instruction->is_async) {
|
||||
fprintf(irp->f, "async");
|
||||
if (call_instruction->async_allocator != nullptr) {
|
||||
fprintf(irp->f, "(");
|
||||
ir_print_other_instruction(irp, call_instruction->async_allocator);
|
||||
fprintf(irp->f, ")");
|
||||
}
|
||||
fprintf(irp->f, " ");
|
||||
}
|
||||
if (call_instruction->fn_entry) {
|
||||
fprintf(irp->f, "%s", buf_ptr(&call_instruction->fn_entry->symbol_name));
|
||||
} else {
|
||||
@ -830,6 +839,12 @@ static void ir_print_ptr_to_int(IrPrint *irp, IrInstructionPtrToInt *instruction
|
||||
|
||||
static void ir_print_int_to_ptr(IrPrint *irp, IrInstructionIntToPtr *instruction) {
|
||||
fprintf(irp->f, "@intToPtr(");
|
||||
if (instruction->dest_type == nullptr) {
|
||||
fprintf(irp->f, "(null)");
|
||||
} else {
|
||||
ir_print_other_instruction(irp, instruction->dest_type);
|
||||
}
|
||||
fprintf(irp->f, ",");
|
||||
ir_print_other_instruction(irp, instruction->target);
|
||||
fprintf(irp->f, ")");
|
||||
}
|
||||
@ -1010,6 +1025,136 @@ static void ir_print_error_union(IrPrint *irp, IrInstructionErrorUnion *instruct
|
||||
ir_print_other_instruction(irp, instruction->payload);
|
||||
}
|
||||
|
||||
static void ir_print_cancel(IrPrint *irp, IrInstructionCancel *instruction) {
|
||||
fprintf(irp->f, "cancel ");
|
||||
ir_print_other_instruction(irp, instruction->target);
|
||||
}
|
||||
|
||||
static void ir_print_get_implicit_allocator(IrPrint *irp, IrInstructionGetImplicitAllocator *instruction) {
|
||||
fprintf(irp->f, "@getImplicitAllocator(");
|
||||
switch (instruction->id) {
|
||||
case ImplicitAllocatorIdArg:
|
||||
fprintf(irp->f, "Arg");
|
||||
break;
|
||||
case ImplicitAllocatorIdLocalVar:
|
||||
fprintf(irp->f, "LocalVar");
|
||||
break;
|
||||
}
|
||||
fprintf(irp->f, ")");
|
||||
}
|
||||
|
||||
static void ir_print_coro_id(IrPrint *irp, IrInstructionCoroId *instruction) {
|
||||
fprintf(irp->f, "@coroId(");
|
||||
ir_print_other_instruction(irp, instruction->promise_ptr);
|
||||
fprintf(irp->f, ")");
|
||||
}
|
||||
|
||||
static void ir_print_coro_alloc(IrPrint *irp, IrInstructionCoroAlloc *instruction) {
|
||||
fprintf(irp->f, "@coroAlloc(");
|
||||
ir_print_other_instruction(irp, instruction->coro_id);
|
||||
fprintf(irp->f, ")");
|
||||
}
|
||||
|
||||
static void ir_print_coro_size(IrPrint *irp, IrInstructionCoroSize *instruction) {
|
||||
fprintf(irp->f, "@coroSize()");
|
||||
}
|
||||
|
||||
static void ir_print_coro_begin(IrPrint *irp, IrInstructionCoroBegin *instruction) {
|
||||
fprintf(irp->f, "@coroBegin(");
|
||||
ir_print_other_instruction(irp, instruction->coro_id);
|
||||
fprintf(irp->f, ",");
|
||||
ir_print_other_instruction(irp, instruction->coro_mem_ptr);
|
||||
fprintf(irp->f, ")");
|
||||
}
|
||||
|
||||
static void ir_print_coro_alloc_fail(IrPrint *irp, IrInstructionCoroAllocFail *instruction) {
|
||||
fprintf(irp->f, "@coroAllocFail(");
|
||||
ir_print_other_instruction(irp, instruction->err_val);
|
||||
fprintf(irp->f, ")");
|
||||
}
|
||||
|
||||
static void ir_print_coro_suspend(IrPrint *irp, IrInstructionCoroSuspend *instruction) {
|
||||
fprintf(irp->f, "@coroSuspend(");
|
||||
if (instruction->save_point != nullptr) {
|
||||
ir_print_other_instruction(irp, instruction->save_point);
|
||||
} else {
|
||||
fprintf(irp->f, "null");
|
||||
}
|
||||
fprintf(irp->f, ",");
|
||||
ir_print_other_instruction(irp, instruction->is_final);
|
||||
fprintf(irp->f, ")");
|
||||
}
|
||||
|
||||
static void ir_print_coro_end(IrPrint *irp, IrInstructionCoroEnd *instruction) {
|
||||
fprintf(irp->f, "@coroEnd()");
|
||||
}
|
||||
|
||||
static void ir_print_coro_free(IrPrint *irp, IrInstructionCoroFree *instruction) {
|
||||
fprintf(irp->f, "@coroFree(");
|
||||
ir_print_other_instruction(irp, instruction->coro_id);
|
||||
fprintf(irp->f, ",");
|
||||
ir_print_other_instruction(irp, instruction->coro_handle);
|
||||
fprintf(irp->f, ")");
|
||||
}
|
||||
|
||||
static void ir_print_coro_resume(IrPrint *irp, IrInstructionCoroResume *instruction) {
|
||||
fprintf(irp->f, "@coroResume(");
|
||||
ir_print_other_instruction(irp, instruction->awaiter_handle);
|
||||
fprintf(irp->f, ")");
|
||||
}
|
||||
|
||||
static void ir_print_coro_save(IrPrint *irp, IrInstructionCoroSave *instruction) {
|
||||
fprintf(irp->f, "@coroSave(");
|
||||
ir_print_other_instruction(irp, instruction->coro_handle);
|
||||
fprintf(irp->f, ")");
|
||||
}
|
||||
|
||||
static void ir_print_coro_promise(IrPrint *irp, IrInstructionCoroPromise *instruction) {
|
||||
fprintf(irp->f, "@coroPromise(");
|
||||
ir_print_other_instruction(irp, instruction->coro_handle);
|
||||
fprintf(irp->f, ")");
|
||||
}
|
||||
|
||||
static void ir_print_promise_result_type(IrPrint *irp, IrInstructionPromiseResultType *instruction) {
|
||||
fprintf(irp->f, "@PromiseResultType(");
|
||||
ir_print_other_instruction(irp, instruction->promise_type);
|
||||
fprintf(irp->f, ")");
|
||||
}
|
||||
|
||||
static void ir_print_coro_alloc_helper(IrPrint *irp, IrInstructionCoroAllocHelper *instruction) {
|
||||
fprintf(irp->f, "@coroAllocHelper(");
|
||||
ir_print_other_instruction(irp, instruction->alloc_fn);
|
||||
fprintf(irp->f, ",");
|
||||
ir_print_other_instruction(irp, instruction->coro_size);
|
||||
fprintf(irp->f, ")");
|
||||
}
|
||||
|
||||
static void ir_print_atomic_rmw(IrPrint *irp, IrInstructionAtomicRmw *instruction) {
|
||||
fprintf(irp->f, "@atomicRmw(");
|
||||
if (instruction->operand_type != nullptr) {
|
||||
ir_print_other_instruction(irp, instruction->operand_type);
|
||||
} else {
|
||||
fprintf(irp->f, "[TODO print]");
|
||||
}
|
||||
fprintf(irp->f, ",");
|
||||
ir_print_other_instruction(irp, instruction->ptr);
|
||||
fprintf(irp->f, ",");
|
||||
if (instruction->op != nullptr) {
|
||||
ir_print_other_instruction(irp, instruction->op);
|
||||
} else {
|
||||
fprintf(irp->f, "[TODO print]");
|
||||
}
|
||||
fprintf(irp->f, ",");
|
||||
ir_print_other_instruction(irp, instruction->operand);
|
||||
fprintf(irp->f, ",");
|
||||
if (instruction->ordering != nullptr) {
|
||||
ir_print_other_instruction(irp, instruction->ordering);
|
||||
} else {
|
||||
fprintf(irp->f, "[TODO print]");
|
||||
}
|
||||
fprintf(irp->f, ")");
|
||||
}
|
||||
|
||||
static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
|
||||
ir_print_prefix(irp, instruction);
|
||||
switch (instruction->id) {
|
||||
@ -1330,6 +1475,54 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
|
||||
case IrInstructionIdErrorUnion:
|
||||
ir_print_error_union(irp, (IrInstructionErrorUnion *)instruction);
|
||||
break;
|
||||
case IrInstructionIdCancel:
|
||||
ir_print_cancel(irp, (IrInstructionCancel *)instruction);
|
||||
break;
|
||||
case IrInstructionIdGetImplicitAllocator:
|
||||
ir_print_get_implicit_allocator(irp, (IrInstructionGetImplicitAllocator *)instruction);
|
||||
break;
|
||||
case IrInstructionIdCoroId:
|
||||
ir_print_coro_id(irp, (IrInstructionCoroId *)instruction);
|
||||
break;
|
||||
case IrInstructionIdCoroAlloc:
|
||||
ir_print_coro_alloc(irp, (IrInstructionCoroAlloc *)instruction);
|
||||
break;
|
||||
case IrInstructionIdCoroSize:
|
||||
ir_print_coro_size(irp, (IrInstructionCoroSize *)instruction);
|
||||
break;
|
||||
case IrInstructionIdCoroBegin:
|
||||
ir_print_coro_begin(irp, (IrInstructionCoroBegin *)instruction);
|
||||
break;
|
||||
case IrInstructionIdCoroAllocFail:
|
||||
ir_print_coro_alloc_fail(irp, (IrInstructionCoroAllocFail *)instruction);
|
||||
break;
|
||||
case IrInstructionIdCoroSuspend:
|
||||
ir_print_coro_suspend(irp, (IrInstructionCoroSuspend *)instruction);
|
||||
break;
|
||||
case IrInstructionIdCoroEnd:
|
||||
ir_print_coro_end(irp, (IrInstructionCoroEnd *)instruction);
|
||||
break;
|
||||
case IrInstructionIdCoroFree:
|
||||
ir_print_coro_free(irp, (IrInstructionCoroFree *)instruction);
|
||||
break;
|
||||
case IrInstructionIdCoroResume:
|
||||
ir_print_coro_resume(irp, (IrInstructionCoroResume *)instruction);
|
||||
break;
|
||||
case IrInstructionIdCoroSave:
|
||||
ir_print_coro_save(irp, (IrInstructionCoroSave *)instruction);
|
||||
break;
|
||||
case IrInstructionIdCoroAllocHelper:
|
||||
ir_print_coro_alloc_helper(irp, (IrInstructionCoroAllocHelper *)instruction);
|
||||
break;
|
||||
case IrInstructionIdAtomicRmw:
|
||||
ir_print_atomic_rmw(irp, (IrInstructionAtomicRmw *)instruction);
|
||||
break;
|
||||
case IrInstructionIdCoroPromise:
|
||||
ir_print_coro_promise(irp, (IrInstructionCoroPromise *)instruction);
|
||||
break;
|
||||
case IrInstructionIdPromiseResultType:
|
||||
ir_print_promise_result_type(irp, (IrInstructionPromiseResultType *)instruction);
|
||||
break;
|
||||
}
|
||||
fprintf(irp->f, "\n");
|
||||
}
|
||||
|
179
src/parser.cpp
179
src/parser.cpp
@ -221,6 +221,7 @@ static AstNode *ast_parse_grouped_expr(ParseContext *pc, size_t *token_index, bo
|
||||
static AstNode *ast_parse_container_decl(ParseContext *pc, size_t *token_index, bool mandatory);
|
||||
static AstNode *ast_parse_primary_expr(ParseContext *pc, size_t *token_index, bool mandatory);
|
||||
static AstNode *ast_parse_try_expr(ParseContext *pc, size_t *token_index);
|
||||
static AstNode *ast_parse_await_expr(ParseContext *pc, size_t *token_index);
|
||||
static AstNode *ast_parse_symbol(ParseContext *pc, size_t *token_index);
|
||||
|
||||
static void ast_expect_token(ParseContext *pc, Token *token, TokenId token_id) {
|
||||
@ -650,6 +651,41 @@ static AstNode *ast_parse_asm_expr(ParseContext *pc, size_t *token_index, bool m
|
||||
return node;
|
||||
}
|
||||
|
||||
/*
|
||||
SuspendExpression(body) = "suspend" "|" Symbol "|" body
|
||||
*/
|
||||
static AstNode *ast_parse_suspend_block(ParseContext *pc, size_t *token_index, bool mandatory) {
|
||||
size_t orig_token_index = *token_index;
|
||||
|
||||
Token *suspend_token = &pc->tokens->at(*token_index);
|
||||
if (suspend_token->id == TokenIdKeywordSuspend) {
|
||||
*token_index += 1;
|
||||
} else if (mandatory) {
|
||||
ast_expect_token(pc, suspend_token, TokenIdKeywordSuspend);
|
||||
zig_unreachable();
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Token *bar_token = &pc->tokens->at(*token_index);
|
||||
if (bar_token->id == TokenIdBinOr) {
|
||||
*token_index += 1;
|
||||
} else if (mandatory) {
|
||||
ast_expect_token(pc, suspend_token, TokenIdBinOr);
|
||||
zig_unreachable();
|
||||
} else {
|
||||
*token_index = orig_token_index;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
AstNode *node = ast_create_node(pc, NodeTypeSuspend, suspend_token);
|
||||
node->data.suspend.promise_symbol = ast_parse_symbol(pc, token_index);
|
||||
ast_eat_token(pc, token_index, TokenIdBinOr);
|
||||
node->data.suspend.block = ast_parse_block(pc, token_index, true);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
/*
|
||||
CompTimeExpression(body) = "comptime" body
|
||||
*/
|
||||
@ -674,7 +710,7 @@ static AstNode *ast_parse_comptime_expr(ParseContext *pc, size_t *token_index, b
|
||||
|
||||
/*
|
||||
PrimaryExpression = Integer | Float | String | CharLiteral | KeywordLiteral | GroupedExpression | BlockExpression(BlockOrExpression) | Symbol | ("@" Symbol FnCallExpression) | ArrayType | FnProto | AsmExpression | ContainerDecl | ("continue" option(":" Symbol)) | ErrorSetDecl
|
||||
KeywordLiteral = "true" | "false" | "null" | "undefined" | "error" | "this" | "unreachable"
|
||||
KeywordLiteral = "true" | "false" | "null" | "undefined" | "error" | "this" | "unreachable" | "suspend"
|
||||
ErrorSetDecl = "error" "{" list(Symbol, ",") "}"
|
||||
*/
|
||||
static AstNode *ast_parse_primary_expr(ParseContext *pc, size_t *token_index, bool mandatory) {
|
||||
@ -738,6 +774,10 @@ static AstNode *ast_parse_primary_expr(ParseContext *pc, size_t *token_index, bo
|
||||
AstNode *node = ast_create_node(pc, NodeTypeUnreachable, token);
|
||||
*token_index += 1;
|
||||
return node;
|
||||
} else if (token->id == TokenIdKeywordSuspend) {
|
||||
AstNode *node = ast_create_node(pc, NodeTypeSuspend, token);
|
||||
*token_index += 1;
|
||||
return node;
|
||||
} else if (token->id == TokenIdKeywordError) {
|
||||
Token *next_token = &pc->tokens->at(*token_index + 1);
|
||||
if (next_token->id == TokenIdLBrace) {
|
||||
@ -920,7 +960,7 @@ static AstNode *ast_parse_curly_suffix_expr(ParseContext *pc, size_t *token_inde
|
||||
}
|
||||
|
||||
/*
|
||||
SuffixOpExpression = PrimaryExpression option(FnCallExpression | ArrayAccessExpression | FieldAccessExpression | SliceExpression)
|
||||
SuffixOpExpression = ("async" option("(" Expression ")") PrimaryExpression FnCallExpression) | PrimaryExpression option(FnCallExpression | ArrayAccessExpression | FieldAccessExpression | SliceExpression)
|
||||
FnCallExpression : token(LParen) list(Expression, token(Comma)) token(RParen)
|
||||
ArrayAccessExpression : token(LBracket) Expression token(RBracket)
|
||||
SliceExpression = "[" Expression ".." option(Expression) "]"
|
||||
@ -928,9 +968,34 @@ FieldAccessExpression : token(Dot) token(Symbol)
|
||||
StructLiteralField : token(Dot) token(Symbol) token(Eq) Expression
|
||||
*/
|
||||
static AstNode *ast_parse_suffix_op_expr(ParseContext *pc, size_t *token_index, bool mandatory) {
|
||||
AstNode *primary_expr = ast_parse_primary_expr(pc, token_index, mandatory);
|
||||
if (!primary_expr)
|
||||
return nullptr;
|
||||
AstNode *primary_expr;
|
||||
|
||||
Token *async_token = &pc->tokens->at(*token_index);
|
||||
if (async_token->id == TokenIdKeywordAsync) {
|
||||
*token_index += 1;
|
||||
|
||||
AstNode *allocator_expr_node = nullptr;
|
||||
Token *async_lparen_tok = &pc->tokens->at(*token_index);
|
||||
if (async_lparen_tok->id == TokenIdLParen) {
|
||||
*token_index += 1;
|
||||
allocator_expr_node = ast_parse_expression(pc, token_index, true);
|
||||
ast_eat_token(pc, token_index, TokenIdRParen);
|
||||
}
|
||||
|
||||
AstNode *fn_ref_expr_node = ast_parse_primary_expr(pc, token_index, true);
|
||||
Token *lparen_tok = ast_eat_token(pc, token_index, TokenIdLParen);
|
||||
AstNode *node = ast_create_node(pc, NodeTypeFnCallExpr, lparen_tok);
|
||||
node->data.fn_call_expr.is_async = true;
|
||||
node->data.fn_call_expr.async_allocator = allocator_expr_node;
|
||||
node->data.fn_call_expr.fn_ref_expr = fn_ref_expr_node;
|
||||
ast_parse_fn_call_param_list(pc, token_index, &node->data.fn_call_expr.params);
|
||||
|
||||
primary_expr = node;
|
||||
} else {
|
||||
primary_expr = ast_parse_primary_expr(pc, token_index, mandatory);
|
||||
if (!primary_expr)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
while (true) {
|
||||
Token *first_token = &pc->tokens->at(*token_index);
|
||||
@ -1042,7 +1107,7 @@ static AstNode *ast_parse_addr_of(ParseContext *pc, size_t *token_index) {
|
||||
|
||||
/*
|
||||
PrefixOpExpression = PrefixOp ErrorSetExpr | SuffixOpExpression
|
||||
PrefixOp = "!" | "-" | "~" | "*" | ("&" option("align" "(" Expression option(":" Integer ":" Integer) ")" ) option("const") option("volatile")) | "?" | "??" | "-%" | "try"
|
||||
PrefixOp = "!" | "-" | "~" | "*" | ("&" option("align" "(" Expression option(":" Integer ":" Integer) ")" ) option("const") option("volatile")) | "?" | "??" | "-%" | "try" | "await"
|
||||
*/
|
||||
static AstNode *ast_parse_prefix_op_expr(ParseContext *pc, size_t *token_index, bool mandatory) {
|
||||
Token *token = &pc->tokens->at(*token_index);
|
||||
@ -1052,6 +1117,9 @@ static AstNode *ast_parse_prefix_op_expr(ParseContext *pc, size_t *token_index,
|
||||
if (token->id == TokenIdKeywordTry) {
|
||||
return ast_parse_try_expr(pc, token_index);
|
||||
}
|
||||
if (token->id == TokenIdKeywordAwait) {
|
||||
return ast_parse_await_expr(pc, token_index);
|
||||
}
|
||||
PrefixOp prefix_op = tok_to_prefix_op(token);
|
||||
if (prefix_op == PrefixOpInvalid) {
|
||||
return ast_parse_suffix_op_expr(pc, token_index, mandatory);
|
||||
@ -1510,6 +1578,23 @@ static AstNode *ast_parse_try_expr(ParseContext *pc, size_t *token_index) {
|
||||
return node;
|
||||
}
|
||||
|
||||
/*
|
||||
AwaitExpression : "await" Expression
|
||||
*/
|
||||
static AstNode *ast_parse_await_expr(ParseContext *pc, size_t *token_index) {
|
||||
Token *token = &pc->tokens->at(*token_index);
|
||||
|
||||
if (token->id != TokenIdKeywordAwait) {
|
||||
return nullptr;
|
||||
}
|
||||
*token_index += 1;
|
||||
|
||||
AstNode *node = ast_create_node(pc, NodeTypeAwaitExpr, token);
|
||||
node->data.await_expr.expr = ast_parse_expression(pc, token_index, true);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
/*
|
||||
BreakExpression = "break" option(":" Symbol) option(Expression)
|
||||
*/
|
||||
@ -1535,6 +1620,42 @@ static AstNode *ast_parse_break_expr(ParseContext *pc, size_t *token_index) {
|
||||
return node;
|
||||
}
|
||||
|
||||
/*
|
||||
CancelExpression = "cancel" Expression;
|
||||
*/
|
||||
static AstNode *ast_parse_cancel_expr(ParseContext *pc, size_t *token_index) {
|
||||
Token *token = &pc->tokens->at(*token_index);
|
||||
|
||||
if (token->id != TokenIdKeywordCancel) {
|
||||
return nullptr;
|
||||
}
|
||||
*token_index += 1;
|
||||
|
||||
AstNode *node = ast_create_node(pc, NodeTypeCancel, token);
|
||||
|
||||
node->data.cancel_expr.expr = ast_parse_expression(pc, token_index, false);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
/*
|
||||
ResumeExpression = "resume" Expression;
|
||||
*/
|
||||
static AstNode *ast_parse_resume_expr(ParseContext *pc, size_t *token_index) {
|
||||
Token *token = &pc->tokens->at(*token_index);
|
||||
|
||||
if (token->id != TokenIdKeywordResume) {
|
||||
return nullptr;
|
||||
}
|
||||
*token_index += 1;
|
||||
|
||||
AstNode *node = ast_create_node(pc, NodeTypeResume, token);
|
||||
|
||||
node->data.resume_expr.expr = ast_parse_expression(pc, token_index, false);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
/*
|
||||
Defer(body) = ("defer" | "errdefer") body
|
||||
*/
|
||||
@ -2001,7 +2122,7 @@ static AstNode *ast_parse_switch_expr(ParseContext *pc, size_t *token_index, boo
|
||||
}
|
||||
|
||||
/*
|
||||
BlockExpression(body) = Block | IfExpression(body) | TryExpression(body) | TestExpression(body) | WhileExpression(body) | ForExpression(body) | SwitchExpression | CompTimeExpression(body)
|
||||
BlockExpression(body) = Block | IfExpression(body) | IfErrorExpression(body) | TestExpression(body) | WhileExpression(body) | ForExpression(body) | SwitchExpression | CompTimeExpression(body) | SuspendExpression(body)
|
||||
*/
|
||||
static AstNode *ast_parse_block_expr(ParseContext *pc, size_t *token_index, bool mandatory) {
|
||||
Token *token = &pc->tokens->at(*token_index);
|
||||
@ -2030,6 +2151,10 @@ static AstNode *ast_parse_block_expr(ParseContext *pc, size_t *token_index, bool
|
||||
if (comptime_node)
|
||||
return comptime_node;
|
||||
|
||||
AstNode *suspend_node = ast_parse_suspend_block(pc, token_index, false);
|
||||
if (suspend_node)
|
||||
return suspend_node;
|
||||
|
||||
if (mandatory)
|
||||
ast_invalid_token_error(pc, token);
|
||||
|
||||
@ -2159,7 +2284,7 @@ static AstNode *ast_parse_block_or_expression(ParseContext *pc, size_t *token_in
|
||||
}
|
||||
|
||||
/*
|
||||
Expression = TryExpression | ReturnExpression | BreakExpression | AssignmentExpression
|
||||
Expression = TryExpression | ReturnExpression | BreakExpression | AssignmentExpression | CancelExpression | ResumeExpression
|
||||
*/
|
||||
static AstNode *ast_parse_expression(ParseContext *pc, size_t *token_index, bool mandatory) {
|
||||
Token *token = &pc->tokens->at(*token_index);
|
||||
@ -2176,6 +2301,14 @@ static AstNode *ast_parse_expression(ParseContext *pc, size_t *token_index, bool
|
||||
if (break_expr)
|
||||
return break_expr;
|
||||
|
||||
AstNode *cancel_expr = ast_parse_cancel_expr(pc, token_index);
|
||||
if (cancel_expr)
|
||||
return cancel_expr;
|
||||
|
||||
AstNode *resume_expr = ast_parse_resume_expr(pc, token_index);
|
||||
if (resume_expr)
|
||||
return resume_expr;
|
||||
|
||||
AstNode *ass_expr = ast_parse_ass_expr(pc, token_index, false);
|
||||
if (ass_expr)
|
||||
return ass_expr;
|
||||
@ -2208,6 +2341,8 @@ static bool statement_terminates_without_semicolon(AstNode *node) {
|
||||
return node->data.comptime_expr.expr->type == NodeTypeBlock;
|
||||
case NodeTypeDefer:
|
||||
return node->data.defer.expr->type == NodeTypeBlock;
|
||||
case NodeTypeSuspend:
|
||||
return node->data.suspend.block != nullptr && node->data.suspend.block->type == NodeTypeBlock;
|
||||
case NodeTypeSwitchExpr:
|
||||
case NodeTypeBlock:
|
||||
return true;
|
||||
@ -2286,7 +2421,7 @@ static AstNode *ast_parse_block(ParseContext *pc, size_t *token_index, bool mand
|
||||
}
|
||||
|
||||
/*
|
||||
FnProto = option("nakedcc" | "stdcallcc" | "extern") "fn" option(Symbol) ParamDeclList option("align" "(" Expression ")") option("section" "(" Expression ")") option("!") TypeExpr
|
||||
FnProto = option("nakedcc" | "stdcallcc" | "extern" | ("async" option("(" Expression ")"))) "fn" option(Symbol) ParamDeclList option("align" "(" Expression ")") option("section" "(" Expression ")") option("!") TypeExpr
|
||||
*/
|
||||
static AstNode *ast_parse_fn_proto(ParseContext *pc, size_t *token_index, bool mandatory, VisibMod visib_mod) {
|
||||
Token *first_token = &pc->tokens->at(*token_index);
|
||||
@ -2294,10 +2429,20 @@ static AstNode *ast_parse_fn_proto(ParseContext *pc, size_t *token_index, bool m
|
||||
|
||||
CallingConvention cc;
|
||||
bool is_extern = false;
|
||||
AstNode *async_allocator_type_node = nullptr;
|
||||
if (first_token->id == TokenIdKeywordNakedCC) {
|
||||
*token_index += 1;
|
||||
fn_token = ast_eat_token(pc, token_index, TokenIdKeywordFn);
|
||||
cc = CallingConventionNaked;
|
||||
} else if (first_token->id == TokenIdKeywordAsync) {
|
||||
*token_index += 1;
|
||||
Token *next_token = &pc->tokens->at(*token_index);
|
||||
if (next_token->id == TokenIdLParen) {
|
||||
async_allocator_type_node = ast_parse_type_expr(pc, token_index, true);
|
||||
ast_eat_token(pc, token_index, TokenIdRParen);
|
||||
}
|
||||
fn_token = ast_eat_token(pc, token_index, TokenIdKeywordFn);
|
||||
cc = CallingConventionAsync;
|
||||
} else if (first_token->id == TokenIdKeywordStdcallCC) {
|
||||
*token_index += 1;
|
||||
fn_token = ast_eat_token(pc, token_index, TokenIdKeywordFn);
|
||||
@ -2332,6 +2477,7 @@ static AstNode *ast_parse_fn_proto(ParseContext *pc, size_t *token_index, bool m
|
||||
node->data.fn_proto.visib_mod = visib_mod;
|
||||
node->data.fn_proto.cc = cc;
|
||||
node->data.fn_proto.is_extern = is_extern;
|
||||
node->data.fn_proto.async_allocator_type = async_allocator_type_node;
|
||||
|
||||
Token *fn_name = &pc->tokens->at(*token_index);
|
||||
|
||||
@ -2747,6 +2893,7 @@ void ast_visit_node_children(AstNode *node, void (*visit)(AstNode **, void *cont
|
||||
visit_node_list(&node->data.fn_proto.params, visit, context);
|
||||
visit_field(&node->data.fn_proto.align_expr, visit, context);
|
||||
visit_field(&node->data.fn_proto.section_expr, visit, context);
|
||||
visit_field(&node->data.fn_proto.async_allocator_type, visit, context);
|
||||
break;
|
||||
case NodeTypeFnDef:
|
||||
visit_field(&node->data.fn_def.fn_proto, visit, context);
|
||||
@ -2809,6 +2956,7 @@ void ast_visit_node_children(AstNode *node, void (*visit)(AstNode **, void *cont
|
||||
case NodeTypeFnCallExpr:
|
||||
visit_field(&node->data.fn_call_expr.fn_ref_expr, visit, context);
|
||||
visit_node_list(&node->data.fn_call_expr.params, visit, context);
|
||||
visit_field(&node->data.fn_call_expr.async_allocator, visit, context);
|
||||
break;
|
||||
case NodeTypeArrayAccessExpr:
|
||||
visit_field(&node->data.array_access_expr.array_ref_expr, visit, context);
|
||||
@ -2931,5 +3079,18 @@ void ast_visit_node_children(AstNode *node, void (*visit)(AstNode **, void *cont
|
||||
case NodeTypeErrorSetDecl:
|
||||
visit_node_list(&node->data.err_set_decl.decls, visit, context);
|
||||
break;
|
||||
case NodeTypeCancel:
|
||||
visit_field(&node->data.cancel_expr.expr, visit, context);
|
||||
break;
|
||||
case NodeTypeResume:
|
||||
visit_field(&node->data.resume_expr.expr, visit, context);
|
||||
break;
|
||||
case NodeTypeAwaitExpr:
|
||||
visit_field(&node->data.await_expr.expr, visit, context);
|
||||
break;
|
||||
case NodeTypeSuspend:
|
||||
visit_field(&node->data.suspend.promise_symbol, visit, context);
|
||||
visit_field(&node->data.suspend.block, visit, context);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -110,7 +110,10 @@ static const struct ZigKeyword zig_keywords[] = {
|
||||
{"align", TokenIdKeywordAlign},
|
||||
{"and", TokenIdKeywordAnd},
|
||||
{"asm", TokenIdKeywordAsm},
|
||||
{"async", TokenIdKeywordAsync},
|
||||
{"await", TokenIdKeywordAwait},
|
||||
{"break", TokenIdKeywordBreak},
|
||||
{"cancel", TokenIdKeywordCancel},
|
||||
{"catch", TokenIdKeywordCatch},
|
||||
{"comptime", TokenIdKeywordCompTime},
|
||||
{"const", TokenIdKeywordConst},
|
||||
@ -133,10 +136,12 @@ static const struct ZigKeyword zig_keywords[] = {
|
||||
{"or", TokenIdKeywordOr},
|
||||
{"packed", TokenIdKeywordPacked},
|
||||
{"pub", TokenIdKeywordPub},
|
||||
{"resume", TokenIdKeywordResume},
|
||||
{"return", TokenIdKeywordReturn},
|
||||
{"section", TokenIdKeywordSection},
|
||||
{"stdcallcc", TokenIdKeywordStdcallCC},
|
||||
{"struct", TokenIdKeywordStruct},
|
||||
{"suspend", TokenIdKeywordSuspend},
|
||||
{"switch", TokenIdKeywordSwitch},
|
||||
{"test", TokenIdKeywordTest},
|
||||
{"this", TokenIdKeywordThis},
|
||||
@ -1523,6 +1528,11 @@ const char * token_name(TokenId id) {
|
||||
case TokenIdFatArrow: return "=>";
|
||||
case TokenIdFloatLiteral: return "FloatLiteral";
|
||||
case TokenIdIntLiteral: return "IntLiteral";
|
||||
case TokenIdKeywordAsync: return "async";
|
||||
case TokenIdKeywordAwait: return "await";
|
||||
case TokenIdKeywordResume: return "resume";
|
||||
case TokenIdKeywordSuspend: return "suspend";
|
||||
case TokenIdKeywordCancel: return "cancel";
|
||||
case TokenIdKeywordAlign: return "align";
|
||||
case TokenIdKeywordAnd: return "and";
|
||||
case TokenIdKeywordAsm: return "asm";
|
||||
|
@ -51,7 +51,10 @@ enum TokenId {
|
||||
TokenIdKeywordAlign,
|
||||
TokenIdKeywordAnd,
|
||||
TokenIdKeywordAsm,
|
||||
TokenIdKeywordAsync,
|
||||
TokenIdKeywordAwait,
|
||||
TokenIdKeywordBreak,
|
||||
TokenIdKeywordCancel,
|
||||
TokenIdKeywordCatch,
|
||||
TokenIdKeywordCompTime,
|
||||
TokenIdKeywordConst,
|
||||
@ -74,10 +77,12 @@ enum TokenId {
|
||||
TokenIdKeywordOr,
|
||||
TokenIdKeywordPacked,
|
||||
TokenIdKeywordPub,
|
||||
TokenIdKeywordResume,
|
||||
TokenIdKeywordReturn,
|
||||
TokenIdKeywordSection,
|
||||
TokenIdKeywordStdcallCC,
|
||||
TokenIdKeywordStruct,
|
||||
TokenIdKeywordSuspend,
|
||||
TokenIdKeywordSwitch,
|
||||
TokenIdKeywordTest,
|
||||
TokenIdKeywordThis,
|
||||
|
@ -32,6 +32,7 @@
|
||||
#include <llvm/Support/TargetParser.h>
|
||||
#include <llvm/Support/raw_ostream.h>
|
||||
#include <llvm/Target/TargetMachine.h>
|
||||
#include <llvm/Transforms/Coroutines.h>
|
||||
#include <llvm/Transforms/IPO.h>
|
||||
#include <llvm/Transforms/IPO/PassManagerBuilder.h>
|
||||
#include <llvm/Transforms/IPO/AlwaysInliner.h>
|
||||
@ -129,6 +130,8 @@ bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMM
|
||||
PMBuilder->Inliner = createFunctionInliningPass(PMBuilder->OptLevel, PMBuilder->SizeLevel, false);
|
||||
}
|
||||
|
||||
addCoroutinePassesToExtensionPoints(*PMBuilder);
|
||||
|
||||
// Set up the per-function pass manager.
|
||||
legacy::FunctionPassManager FPM = legacy::FunctionPassManager(module);
|
||||
auto tliwp = new(std::nothrow) TargetLibraryInfoWrapperPass(tlii);
|
||||
@ -182,6 +185,9 @@ bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMM
|
||||
return false;
|
||||
}
|
||||
|
||||
ZIG_EXTERN_C LLVMTypeRef ZigLLVMTokenTypeInContext(LLVMContextRef context_ref) {
|
||||
return wrap(Type::getTokenTy(*unwrap(context_ref)));
|
||||
}
|
||||
|
||||
LLVMValueRef ZigLLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn, LLVMValueRef *Args,
|
||||
unsigned NumArgs, unsigned CC, ZigLLVM_FnInline fn_inline, const char *Name)
|
||||
|
@ -54,6 +54,8 @@ enum ZigLLVM_EmitOutputType {
|
||||
ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMModuleRef module_ref,
|
||||
const char *filename, enum ZigLLVM_EmitOutputType output_type, char **error_message, bool is_debug);
|
||||
|
||||
ZIG_EXTERN_C LLVMTypeRef ZigLLVMTokenTypeInContext(LLVMContextRef context_ref);
|
||||
|
||||
enum ZigLLVM_FnInline {
|
||||
ZigLLVM_FnInlineAuto,
|
||||
ZigLLVM_FnInlineAlways,
|
||||
|
@ -98,21 +98,18 @@ pub fn assertOrPanic(ok: bool) void {
|
||||
}
|
||||
}
|
||||
|
||||
var panicking = false;
|
||||
var panicking: u8 = 0; // TODO make this a bool
|
||||
/// This is the default panic implementation.
|
||||
pub fn panic(comptime format: []const u8, args: ...) noreturn {
|
||||
// TODO an intrinsic that labels this as unlikely to be reached
|
||||
@setCold(true);
|
||||
|
||||
// TODO
|
||||
// if (@atomicRmw(AtomicOp.XChg, &panicking, true, AtomicOrder.SeqCst)) { }
|
||||
if (panicking) {
|
||||
if (@atomicRmw(u8, &panicking, builtin.AtomicRmwOp.Xchg, 1, builtin.AtomicOrder.SeqCst) == 1) {
|
||||
// Panicked during a panic.
|
||||
|
||||
// TODO detect if a different thread caused the panic, because in that case
|
||||
// we would want to return here instead of calling abort, so that the thread
|
||||
// which first called panic can finish printing a stack trace.
|
||||
os.abort();
|
||||
} else {
|
||||
panicking = true;
|
||||
}
|
||||
|
||||
const stderr = getStderrStream() catch os.abort();
|
||||
@ -123,10 +120,11 @@ pub fn panic(comptime format: []const u8, args: ...) noreturn {
|
||||
}
|
||||
|
||||
pub fn panicWithTrace(trace: &const builtin.StackTrace, comptime format: []const u8, args: ...) noreturn {
|
||||
if (panicking) {
|
||||
@setCold(true);
|
||||
|
||||
if (@atomicRmw(u8, &panicking, builtin.AtomicRmwOp.Xchg, 1, builtin.AtomicOrder.SeqCst) == 1) {
|
||||
// See TODO in above function
|
||||
os.abort();
|
||||
} else {
|
||||
panicking = true;
|
||||
}
|
||||
const stderr = getStderrStream() catch os.abort();
|
||||
stderr.print(format ++ "\n", args) catch os.abort();
|
||||
|
@ -235,7 +235,7 @@ pub fn HashMap(comptime K: type, comptime V: type,
|
||||
};
|
||||
}
|
||||
|
||||
test "basicHashMapTest" {
|
||||
test "basic hash map usage" {
|
||||
var map = HashMap(i32, i32, hash_i32, eql_i32).init(debug.global_allocator);
|
||||
defer map.deinit();
|
||||
|
||||
|
@ -1,3 +1,5 @@
|
||||
const builtin = @import("builtin");
|
||||
|
||||
comptime {
|
||||
_ = @import("cases/align.zig");
|
||||
_ = @import("cases/alignof.zig");
|
||||
@ -34,8 +36,8 @@ comptime {
|
||||
_ = @import("cases/sizeof_and_typeof.zig");
|
||||
_ = @import("cases/slice.zig");
|
||||
_ = @import("cases/struct.zig");
|
||||
_ = @import("cases/struct_contains_slice_of_itself.zig");
|
||||
_ = @import("cases/struct_contains_null_ptr_itself.zig");
|
||||
_ = @import("cases/struct_contains_slice_of_itself.zig");
|
||||
_ = @import("cases/switch.zig");
|
||||
_ = @import("cases/switch_prong_err_enum.zig");
|
||||
_ = @import("cases/switch_prong_implicit_cast.zig");
|
||||
@ -47,4 +49,15 @@ comptime {
|
||||
_ = @import("cases/var_args.zig");
|
||||
_ = @import("cases/void.zig");
|
||||
_ = @import("cases/while.zig");
|
||||
|
||||
|
||||
// LLVM 5.0.1, 6.0.0, and trunk crash when attempting to optimize coroutine code.
|
||||
// So, Zig does not support ReleaseFast or ReleaseSafe for coroutines yet.
|
||||
// Luckily, Clang users are running into the same crashes, so folks from the LLVM
|
||||
// community are working on fixes. If we're really lucky they'll be fixed in 6.0.1.
|
||||
// Otherwise we can hope for 7.0.0.
|
||||
if (builtin.mode == builtin.Mode.Debug) {
|
||||
_ = @import("cases/coroutines.zig");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
const assert = @import("std").debug.assert;
|
||||
const AtomicOrder = @import("builtin").AtomicOrder;
|
||||
const builtin = @import("builtin");
|
||||
const AtomicRmwOp = builtin.AtomicRmwOp;
|
||||
const AtomicOrder = builtin.AtomicOrder;
|
||||
|
||||
test "cmpxchg" {
|
||||
var x: i32 = 1234;
|
||||
@ -12,3 +14,14 @@ test "fence" {
|
||||
@fence(AtomicOrder.SeqCst);
|
||||
x = 5678;
|
||||
}
|
||||
|
||||
test "atomicrmw" {
|
||||
var data: u8 = 200;
|
||||
testAtomicRmw(&data);
|
||||
assert(data == 42);
|
||||
}
|
||||
|
||||
fn testAtomicRmw(ptr: &u8) void {
|
||||
const prev_value = @atomicRmw(u8, ptr, AtomicRmwOp.Xchg, 42, AtomicOrder.SeqCst);
|
||||
assert(prev_value == 200);
|
||||
}
|
||||
|
132
test/cases/coroutines.zig
Normal file
132
test/cases/coroutines.zig
Normal file
@ -0,0 +1,132 @@
|
||||
const std = @import("std");
|
||||
const assert = std.debug.assert;
|
||||
|
||||
var x: i32 = 1;
|
||||
|
||||
test "create a coroutine and cancel it" {
|
||||
const p = try async(std.debug.global_allocator) simpleAsyncFn();
|
||||
cancel p;
|
||||
assert(x == 2);
|
||||
}
|
||||
|
||||
async fn simpleAsyncFn() void {
|
||||
x += 1;
|
||||
suspend;
|
||||
x += 1;
|
||||
}
|
||||
|
||||
test "coroutine suspend, resume, cancel" {
|
||||
seq('a');
|
||||
const p = try async(std.debug.global_allocator) testAsyncSeq();
|
||||
seq('c');
|
||||
resume p;
|
||||
seq('f');
|
||||
cancel p;
|
||||
seq('g');
|
||||
|
||||
assert(std.mem.eql(u8, points, "abcdefg"));
|
||||
}
|
||||
|
||||
async fn testAsyncSeq() void {
|
||||
defer seq('e');
|
||||
|
||||
seq('b');
|
||||
suspend;
|
||||
seq('d');
|
||||
}
|
||||
var points = []u8{0} ** "abcdefg".len;
|
||||
var index: usize = 0;
|
||||
|
||||
fn seq(c: u8) void {
|
||||
points[index] = c;
|
||||
index += 1;
|
||||
}
|
||||
|
||||
test "coroutine suspend with block" {
|
||||
const p = try async(std.debug.global_allocator) testSuspendBlock();
|
||||
std.debug.assert(!result);
|
||||
resume a_promise;
|
||||
std.debug.assert(result);
|
||||
cancel p;
|
||||
}
|
||||
|
||||
var a_promise: promise = undefined;
|
||||
var result = false;
|
||||
|
||||
async fn testSuspendBlock() void {
|
||||
suspend |p| {
|
||||
a_promise = p;
|
||||
}
|
||||
result = true;
|
||||
}
|
||||
|
||||
var await_a_promise: promise = undefined;
|
||||
var await_final_result: i32 = 0;
|
||||
|
||||
test "coroutine await" {
|
||||
await_seq('a');
|
||||
const p = async(std.debug.global_allocator) await_amain() catch unreachable;
|
||||
await_seq('f');
|
||||
resume await_a_promise;
|
||||
await_seq('i');
|
||||
assert(await_final_result == 1234);
|
||||
assert(std.mem.eql(u8, await_points, "abcdefghi"));
|
||||
}
|
||||
|
||||
async fn await_amain() void {
|
||||
await_seq('b');
|
||||
const p = async await_another() catch unreachable;
|
||||
await_seq('e');
|
||||
await_final_result = await p;
|
||||
await_seq('h');
|
||||
}
|
||||
|
||||
async fn await_another() i32 {
|
||||
await_seq('c');
|
||||
suspend |p| {
|
||||
await_seq('d');
|
||||
await_a_promise = p;
|
||||
}
|
||||
await_seq('g');
|
||||
return 1234;
|
||||
}
|
||||
|
||||
var await_points = []u8{0} ** "abcdefghi".len;
|
||||
var await_seq_index: usize = 0;
|
||||
|
||||
fn await_seq(c: u8) void {
|
||||
await_points[await_seq_index] = c;
|
||||
await_seq_index += 1;
|
||||
}
|
||||
|
||||
|
||||
var early_final_result: i32 = 0;
|
||||
|
||||
test "coroutine await early return" {
|
||||
early_seq('a');
|
||||
const p = async(std.debug.global_allocator) early_amain() catch unreachable;
|
||||
early_seq('f');
|
||||
assert(early_final_result == 1234);
|
||||
assert(std.mem.eql(u8, early_points, "abcdef"));
|
||||
}
|
||||
|
||||
async fn early_amain() void {
|
||||
early_seq('b');
|
||||
const p = async early_another() catch unreachable;
|
||||
early_seq('d');
|
||||
early_final_result = await p;
|
||||
early_seq('e');
|
||||
}
|
||||
|
||||
async fn early_another() i32 {
|
||||
early_seq('c');
|
||||
return 1234;
|
||||
}
|
||||
|
||||
var early_points = []u8{0} ** "abcdef".len;
|
||||
var early_seq_index: usize = 0;
|
||||
|
||||
fn early_seq(c: u8) void {
|
||||
early_points[early_seq_index] = c;
|
||||
early_seq_index += 1;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user