mirror of
https://github.com/ziglang/zig.git
synced 2024-11-29 00:22:33 +00:00
Merge remote-tracking branch 'origin/master' into llvm6
This commit is contained in:
commit
a217c764db
@ -2782,30 +2782,96 @@ test "fn reflection" {
|
||||
{#header_close#}
|
||||
{#header_close#}
|
||||
{#header_open|Errors#}
|
||||
{#header_open|Error Set Type#}
|
||||
<p>
|
||||
One of the distinguishing features of Zig is its exception handling strategy.
|
||||
An error set is like an {#link|enum#}.
|
||||
However, each error name across the entire compilation gets assigned an unsigned integer
|
||||
greater than 0. You are allowed to declare the same error name more than once, and if you do, it
|
||||
gets assigned the same integer value.
|
||||
</p>
|
||||
<p>
|
||||
TODO rewrite the errors section to take into account error sets
|
||||
The number of unique error values across the entire compilation should determine the size of the error set type.
|
||||
However right now it is hard coded to be a <code>u16</code>. See <a href="https://github.com/zig-lang/zig/issues/786">#768</a>.
|
||||
</p>
|
||||
<p>
|
||||
These error values are assigned an unsigned integer value greater than 0 at
|
||||
compile time. You are allowed to declare the same error value more than once,
|
||||
and if you do, it gets assigned the same integer value.
|
||||
You can implicitly cast an error from a subset to its superset:
|
||||
</p>
|
||||
{#code_begin|test#}
|
||||
const std = @import("std");
|
||||
|
||||
const FileOpenError = error {
|
||||
AccessDenied,
|
||||
OutOfMemory,
|
||||
FileNotFound,
|
||||
};
|
||||
|
||||
const AllocationError = error {
|
||||
OutOfMemory,
|
||||
};
|
||||
|
||||
test "implicit cast subset to superset" {
|
||||
const err = foo(AllocationError.OutOfMemory);
|
||||
std.debug.assert(err == FileOpenError.OutOfMemory);
|
||||
}
|
||||
|
||||
fn foo(err: AllocationError) FileOpenError {
|
||||
return err;
|
||||
}
|
||||
{#code_end#}
|
||||
<p>
|
||||
But you cannot implicitly cast an error from a superset to a subset:
|
||||
</p>
|
||||
{#code_begin|test_err|not a member of destination error set#}
|
||||
const FileOpenError = error {
|
||||
AccessDenied,
|
||||
OutOfMemory,
|
||||
FileNotFound,
|
||||
};
|
||||
|
||||
const AllocationError = error {
|
||||
OutOfMemory,
|
||||
};
|
||||
|
||||
test "implicit cast superset to subset" {
|
||||
foo(FileOpenError.OutOfMemory) catch {};
|
||||
}
|
||||
|
||||
fn foo(err: FileOpenError) AllocationError {
|
||||
return err;
|
||||
}
|
||||
{#code_end#}
|
||||
<p>
|
||||
There is a shortcut for declaring an error set with only 1 value, and then getting that value:
|
||||
</p>
|
||||
{#code_begin|syntax#}
|
||||
const err = error.FileNotFound;
|
||||
{#code_end#}
|
||||
<p>This is equivalent to:</p>
|
||||
{#code_begin|syntax#}
|
||||
const err = (error {FileNotFound}).FileNotFound;
|
||||
{#code_end#}
|
||||
<p>
|
||||
This becomes useful when using {#link|Inferred Error Sets#}.
|
||||
</p>
|
||||
{#header_open|The Global Error Set#}
|
||||
<p><code>error</code> refers to the global error set.
|
||||
This is the error set that contains all errors in the entire compilation unit.
|
||||
It is a superset of all other error sets and a subset of none of them.
|
||||
</p>
|
||||
<p>
|
||||
You can refer to these error values with the error namespace such as
|
||||
<code>error.FileNotFound</code>.
|
||||
You can implicitly cast any error set to the global one, and you can explicitly
|
||||
cast an error of global error set to a non-global one. This inserts a language-level
|
||||
assert to make sure the error value is in fact in the destination error set.
|
||||
</p>
|
||||
<p>
|
||||
Each error value across the entire compilation unit gets a unique integer,
|
||||
and this determines the size of the error set type.
|
||||
The global error set should generally be avoided when possible, because it prevents
|
||||
the compiler from knowing what errors are possible at compile-time. Knowing
|
||||
the error set at compile-time is better for generated documentationt and for
|
||||
helpful error messages such as forgetting a possible error value in a {#link|switch#}.
|
||||
</p>
|
||||
<p>
|
||||
The error set type is one of the error values, and in the same way that pointers
|
||||
cannot be null, a error set instance is always an error.
|
||||
</p>
|
||||
{#code_begin|syntax#}const pure_error = error.FileNotFound;{#code_end#}
|
||||
{#header_close#}
|
||||
{#header_close#}
|
||||
{#header_open|Error Union Type#}
|
||||
<p>
|
||||
Most of the time you will not find yourself using an error set type. Instead,
|
||||
likely you will be using the error union type. This is when you take an error set
|
||||
@ -2918,7 +2984,6 @@ fn doAThing(str: []u8) !void {
|
||||
a panic in Debug and ReleaseSafe modes and undefined behavior in ReleaseFast mode. So, while we're debugging the
|
||||
application, if there <em>was</em> a surprise error here, the application would crash
|
||||
appropriately.
|
||||
TODO: mention error return traces
|
||||
</p>
|
||||
<p>
|
||||
Finally, you may want to take a different action for every situation. For that, we combine
|
||||
@ -2986,7 +3051,7 @@ fn createFoo(param: i32) !Foo {
|
||||
</li>
|
||||
</ul>
|
||||
{#see_also|defer|if|switch#}
|
||||
{#header_open|Error Union Type#}
|
||||
|
||||
<p>An error union is created with the <code>!</code> binary operator.
|
||||
You can use compile-time reflection to access the child type of an error union:</p>
|
||||
{#code_begin|test#}
|
||||
@ -3008,8 +3073,12 @@ test "error union" {
|
||||
comptime assert(@typeOf(foo).ErrorSet == error);
|
||||
}
|
||||
{#code_end#}
|
||||
<p>TODO the <code>||</code> operator for error sets</p>
|
||||
{#header_open|Inferred Error Sets#}
|
||||
<p>TODO</p>
|
||||
{#header_close#}
|
||||
{#header_open|Error Set Type#}
|
||||
{#header_close#}
|
||||
{#header_open|Error Return Traces#}
|
||||
<p>TODO</p>
|
||||
{#header_close#}
|
||||
{#header_close#}
|
||||
@ -3775,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>
|
||||
@ -5645,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
|
||||
|
||||
@ -5663,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) ")"
|
||||
|
||||
@ -5687,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
|
||||
|
||||
@ -5705,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))
|
||||
@ -5745,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
|
||||
|
||||
@ -5761,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
|
||||
|
||||
@ -5769,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, ",") "}"
|
||||
|
||||
@ -5853,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,
|
||||
|
213
src/analyze.cpp
213
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) {
|
||||
@ -997,7 +1083,7 @@ TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
|
||||
gen_param_info->src_index = i;
|
||||
gen_param_info->gen_index = SIZE_MAX;
|
||||
|
||||
ensure_complete_type(g, type_entry);
|
||||
type_ensure_zero_bits_known(g, type_entry);
|
||||
if (type_has_bits(type_entry)) {
|
||||
TypeTableEntry *gen_type;
|
||||
if (handle_is_ptr(type_entry)) {
|
||||
@ -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);
|
||||
}
|
||||
@ -2129,6 +2240,7 @@ static void resolve_enum_zero_bits(CodeGen *g, TypeTableEntry *enum_type) {
|
||||
|
||||
if (enum_type->data.enumeration.zero_bits_loop_flag) {
|
||||
enum_type->data.enumeration.zero_bits_known = true;
|
||||
enum_type->data.enumeration.zero_bits_loop_flag = false;
|
||||
return;
|
||||
}
|
||||
|
||||
@ -2283,6 +2395,7 @@ static void resolve_struct_zero_bits(CodeGen *g, TypeTableEntry *struct_type) {
|
||||
// the alignment is pointer width, then assert that the first field is within that
|
||||
// alignment
|
||||
struct_type->data.structure.zero_bits_known = true;
|
||||
struct_type->data.structure.zero_bits_loop_flag = false;
|
||||
if (struct_type->data.structure.abi_alignment == 0) {
|
||||
if (struct_type->data.structure.layout == ContainerLayoutPacked) {
|
||||
struct_type->data.structure.abi_alignment = 1;
|
||||
@ -3117,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();
|
||||
}
|
||||
}
|
||||
@ -3172,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();
|
||||
@ -3550,6 +3668,7 @@ static bool is_container(TypeTableEntry *type_entry) {
|
||||
case TypeTableEntryIdBoundFn:
|
||||
case TypeTableEntryIdArgTuple:
|
||||
case TypeTableEntryIdOpaque:
|
||||
case TypeTableEntryIdPromise:
|
||||
return false;
|
||||
}
|
||||
zig_unreachable();
|
||||
@ -3600,6 +3719,7 @@ void resolve_container_type(CodeGen *g, TypeTableEntry *type_entry) {
|
||||
case TypeTableEntryIdVar:
|
||||
case TypeTableEntryIdArgTuple:
|
||||
case TypeTableEntryIdOpaque:
|
||||
case TypeTableEntryIdPromise:
|
||||
zig_unreachable();
|
||||
}
|
||||
}
|
||||
@ -3607,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) {
|
||||
@ -3624,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();
|
||||
}
|
||||
@ -3638,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;
|
||||
@ -3659,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"));
|
||||
}
|
||||
|
||||
@ -4092,6 +4216,7 @@ bool handle_is_ptr(TypeTableEntry *type_entry) {
|
||||
case TypeTableEntryIdErrorSet:
|
||||
case TypeTableEntryIdFn:
|
||||
case TypeTableEntryIdEnum:
|
||||
case TypeTableEntryIdPromise:
|
||||
return false;
|
||||
case TypeTableEntryIdArray:
|
||||
case TypeTableEntryIdStruct:
|
||||
@ -4100,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)
|
||||
@ -4203,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];
|
||||
@ -4217,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;
|
||||
}
|
||||
@ -4339,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:
|
||||
@ -4498,6 +4627,7 @@ bool type_requires_comptime(TypeTableEntry *type_entry) {
|
||||
case TypeTableEntryIdPointer:
|
||||
case TypeTableEntryIdVoid:
|
||||
case TypeTableEntryIdUnreachable:
|
||||
case TypeTableEntryIdPromise:
|
||||
return false;
|
||||
}
|
||||
zig_unreachable();
|
||||
@ -4967,6 +5097,7 @@ bool const_values_equal(ConstExprValue *a, ConstExprValue *b) {
|
||||
case TypeTableEntryIdInvalid:
|
||||
case TypeTableEntryIdUnreachable:
|
||||
case TypeTableEntryIdVar:
|
||||
case TypeTableEntryIdPromise:
|
||||
zig_unreachable();
|
||||
}
|
||||
zig_unreachable();
|
||||
@ -5241,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();
|
||||
}
|
||||
@ -5302,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);
|
||||
@ -5339,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:
|
||||
@ -5466,6 +5601,7 @@ static const TypeTableEntryId all_type_ids[] = {
|
||||
TypeTableEntryIdBoundFn,
|
||||
TypeTableEntryIdArgTuple,
|
||||
TypeTableEntryIdOpaque,
|
||||
TypeTableEntryIdPromise,
|
||||
};
|
||||
|
||||
TypeTableEntryId type_id_at_index(size_t index) {
|
||||
@ -5530,6 +5666,8 @@ size_t type_id_index(TypeTableEntryId id) {
|
||||
return 22;
|
||||
case TypeTableEntryIdOpaque:
|
||||
return 23;
|
||||
case TypeTableEntryIdPromise:
|
||||
return 24;
|
||||
}
|
||||
zig_unreachable();
|
||||
}
|
||||
@ -5587,6 +5725,8 @@ const char *type_id_name(TypeTableEntryId id) {
|
||||
return "ArgTuple";
|
||||
case TypeTableEntryIdOpaque:
|
||||
return "Opaque";
|
||||
case TypeTableEntryIdPromise:
|
||||
return "Promise";
|
||||
}
|
||||
zig_unreachable();
|
||||
}
|
||||
@ -5669,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);
|
||||
|
1411
src/ir.cpp
1411
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();
|
||||
|
||||
|
@ -32,9 +32,6 @@ pub const ChildProcess = struct {
|
||||
|
||||
pub argv: []const []const u8,
|
||||
|
||||
/// Possibly called from a signal handler. Must set this before calling `spawn`.
|
||||
pub onTerm: ?fn(&ChildProcess)void,
|
||||
|
||||
/// Leave as null to use the current env map using the supplied allocator.
|
||||
pub env_map: ?&const BufMap,
|
||||
|
||||
@ -102,7 +99,6 @@ pub const ChildProcess = struct {
|
||||
.err_pipe = undefined,
|
||||
.llnode = undefined,
|
||||
.term = null,
|
||||
.onTerm = null,
|
||||
.env_map = null,
|
||||
.cwd = null,
|
||||
.uid = if (is_windows) {} else null,
|
||||
@ -124,7 +120,6 @@ pub const ChildProcess = struct {
|
||||
self.gid = user_info.gid;
|
||||
}
|
||||
|
||||
/// onTerm can be called before `spawn` returns.
|
||||
/// On success must call `kill` or `wait`.
|
||||
pub fn spawn(self: &ChildProcess) !void {
|
||||
if (is_windows) {
|
||||
@ -165,9 +160,6 @@ pub const ChildProcess = struct {
|
||||
}
|
||||
|
||||
pub fn killPosix(self: &ChildProcess) !Term {
|
||||
block_SIGCHLD();
|
||||
defer restore_SIGCHLD();
|
||||
|
||||
if (self.term) |term| {
|
||||
self.cleanupStreams();
|
||||
return term;
|
||||
@ -246,9 +238,6 @@ pub const ChildProcess = struct {
|
||||
}
|
||||
|
||||
fn waitPosix(self: &ChildProcess) !Term {
|
||||
block_SIGCHLD();
|
||||
defer restore_SIGCHLD();
|
||||
|
||||
if (self.term) |term| {
|
||||
self.cleanupStreams();
|
||||
return term;
|
||||
@ -298,10 +287,6 @@ pub const ChildProcess = struct {
|
||||
|
||||
fn handleWaitResult(self: &ChildProcess, status: i32) void {
|
||||
self.term = self.cleanupAfterWait(status);
|
||||
|
||||
if (self.onTerm) |onTerm| {
|
||||
onTerm(self);
|
||||
}
|
||||
}
|
||||
|
||||
fn cleanupStreams(self: &ChildProcess) void {
|
||||
@ -347,9 +332,6 @@ pub const ChildProcess = struct {
|
||||
}
|
||||
|
||||
fn spawnPosix(self: &ChildProcess) !void {
|
||||
// TODO atomically set a flag saying that we already did this
|
||||
install_SIGCHLD_handler();
|
||||
|
||||
const stdin_pipe = if (self.stdin_behavior == StdIo.Pipe) try makePipe() else undefined;
|
||||
errdefer if (self.stdin_behavior == StdIo.Pipe) { destroyPipe(stdin_pipe); };
|
||||
|
||||
@ -387,11 +369,9 @@ pub const ChildProcess = struct {
|
||||
const err_pipe = try makePipe();
|
||||
errdefer destroyPipe(err_pipe);
|
||||
|
||||
block_SIGCHLD();
|
||||
const pid_result = posix.fork();
|
||||
const pid_err = posix.getErrno(pid_result);
|
||||
if (pid_err > 0) {
|
||||
restore_SIGCHLD();
|
||||
return switch (pid_err) {
|
||||
posix.EAGAIN, posix.ENOMEM, posix.ENOSYS => error.SystemResources,
|
||||
else => os.unexpectedErrorPosix(pid_err),
|
||||
@ -399,7 +379,6 @@ pub const ChildProcess = struct {
|
||||
}
|
||||
if (pid_result == 0) {
|
||||
// we are the child
|
||||
restore_SIGCHLD();
|
||||
|
||||
setUpChildIo(self.stdin_behavior, stdin_pipe[0], posix.STDIN_FILENO, dev_null_fd) catch
|
||||
|err| forkChildErrReport(err_pipe[1], err);
|
||||
@ -451,8 +430,6 @@ pub const ChildProcess = struct {
|
||||
// TODO make this atomic so it works even with threads
|
||||
children_nodes.prepend(&self.llnode);
|
||||
|
||||
restore_SIGCHLD();
|
||||
|
||||
if (self.stdin_behavior == StdIo.Pipe) { os.close(stdin_pipe[0]); }
|
||||
if (self.stdout_behavior == StdIo.Pipe) { os.close(stdout_pipe[1]); }
|
||||
if (self.stderr_behavior == StdIo.Pipe) { os.close(stderr_pipe[1]); }
|
||||
@ -824,30 +801,3 @@ fn handleTerm(pid: i32, status: i32) void {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const sigchld_set = x: {
|
||||
var signal_set = posix.empty_sigset;
|
||||
posix.sigaddset(&signal_set, posix.SIGCHLD);
|
||||
break :x signal_set;
|
||||
};
|
||||
|
||||
fn block_SIGCHLD() void {
|
||||
const err = posix.getErrno(posix.sigprocmask(posix.SIG_BLOCK, &sigchld_set, null));
|
||||
assert(err == 0);
|
||||
}
|
||||
|
||||
fn restore_SIGCHLD() void {
|
||||
const err = posix.getErrno(posix.sigprocmask(posix.SIG_UNBLOCK, &sigchld_set, null));
|
||||
assert(err == 0);
|
||||
}
|
||||
|
||||
const sigchld_action = posix.Sigaction {
|
||||
.handler = sigchld_handler,
|
||||
.mask = posix.empty_sigset,
|
||||
.flags = posix.SA_RESTART | posix.SA_NOCLDSTOP,
|
||||
};
|
||||
|
||||
fn install_SIGCHLD_handler() void {
|
||||
const err = posix.getErrno(posix.sigaction(posix.SIGCHLD, &sigchld_action, null));
|
||||
assert(err == 0);
|
||||
}
|
||||
|
149
std/unicode.zig
149
std/unicode.zig
@ -1,4 +1,5 @@
|
||||
const std = @import("./index.zig");
|
||||
const debug = std.debug;
|
||||
|
||||
/// Given the first byte of a UTF-8 codepoint,
|
||||
/// returns a number 1-4 indicating the total length of the codepoint in bytes.
|
||||
@ -25,8 +26,8 @@ pub fn utf8Decode(bytes: []const u8) !u32 {
|
||||
};
|
||||
}
|
||||
pub fn utf8Decode2(bytes: []const u8) !u32 {
|
||||
std.debug.assert(bytes.len == 2);
|
||||
std.debug.assert(bytes[0] & 0b11100000 == 0b11000000);
|
||||
debug.assert(bytes.len == 2);
|
||||
debug.assert(bytes[0] & 0b11100000 == 0b11000000);
|
||||
var value: u32 = bytes[0] & 0b00011111;
|
||||
|
||||
if (bytes[1] & 0b11000000 != 0b10000000) return error.Utf8ExpectedContinuation;
|
||||
@ -38,8 +39,8 @@ pub fn utf8Decode2(bytes: []const u8) !u32 {
|
||||
return value;
|
||||
}
|
||||
pub fn utf8Decode3(bytes: []const u8) !u32 {
|
||||
std.debug.assert(bytes.len == 3);
|
||||
std.debug.assert(bytes[0] & 0b11110000 == 0b11100000);
|
||||
debug.assert(bytes.len == 3);
|
||||
debug.assert(bytes[0] & 0b11110000 == 0b11100000);
|
||||
var value: u32 = bytes[0] & 0b00001111;
|
||||
|
||||
if (bytes[1] & 0b11000000 != 0b10000000) return error.Utf8ExpectedContinuation;
|
||||
@ -56,8 +57,8 @@ pub fn utf8Decode3(bytes: []const u8) !u32 {
|
||||
return value;
|
||||
}
|
||||
pub fn utf8Decode4(bytes: []const u8) !u32 {
|
||||
std.debug.assert(bytes.len == 4);
|
||||
std.debug.assert(bytes[0] & 0b11111000 == 0b11110000);
|
||||
debug.assert(bytes.len == 4);
|
||||
debug.assert(bytes[0] & 0b11111000 == 0b11110000);
|
||||
var value: u32 = bytes[0] & 0b00000111;
|
||||
|
||||
if (bytes[1] & 0b11000000 != 0b10000000) return error.Utf8ExpectedContinuation;
|
||||
@ -78,6 +79,136 @@ pub fn utf8Decode4(bytes: []const u8) !u32 {
|
||||
return value;
|
||||
}
|
||||
|
||||
pub fn utf8ValidateSlice(s: []const u8) bool {
|
||||
var i: usize = 0;
|
||||
while (i < s.len) {
|
||||
if (utf8ByteSequenceLength(s[i])) |cp_len| {
|
||||
if (i + cp_len > s.len) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (utf8Decode(s[i..i+cp_len])) |_| {} else |_| { return false; }
|
||||
i += cp_len;
|
||||
} else |err| {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
const Utf8View = struct {
|
||||
bytes: []const u8,
|
||||
|
||||
pub fn init(s: []const u8) !Utf8View {
|
||||
if (!utf8ValidateSlice(s)) {
|
||||
return error.InvalidUtf8;
|
||||
}
|
||||
|
||||
return initUnchecked(s);
|
||||
}
|
||||
|
||||
pub fn initUnchecked(s: []const u8) Utf8View {
|
||||
return Utf8View {
|
||||
.bytes = s,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn initComptime(comptime s: []const u8) Utf8View {
|
||||
if (comptime init(s)) |r| {
|
||||
return r;
|
||||
} else |err| switch (err) {
|
||||
error.InvalidUtf8 => {
|
||||
@compileError("invalid utf8");
|
||||
unreachable;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn Iterator(s: &const Utf8View) Utf8Iterator {
|
||||
return Utf8Iterator {
|
||||
.bytes = s.bytes,
|
||||
.i = 0,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
const Utf8Iterator = struct {
|
||||
bytes: []const u8,
|
||||
i: usize,
|
||||
|
||||
pub fn nextCodepointSlice(it: &Utf8Iterator) ?[]const u8 {
|
||||
if (it.i >= it.bytes.len) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const cp_len = utf8ByteSequenceLength(it.bytes[it.i]) catch unreachable;
|
||||
|
||||
it.i += cp_len;
|
||||
return it.bytes[it.i-cp_len..it.i];
|
||||
}
|
||||
|
||||
pub fn nextCodepoint(it: &Utf8Iterator) ?u32 {
|
||||
const slice = it.nextCodepointSlice() ?? return null;
|
||||
|
||||
const r = switch (slice.len) {
|
||||
1 => u32(slice[0]),
|
||||
2 => utf8Decode2(slice),
|
||||
3 => utf8Decode3(slice),
|
||||
4 => utf8Decode4(slice),
|
||||
else => unreachable,
|
||||
};
|
||||
|
||||
return r catch unreachable;
|
||||
}
|
||||
};
|
||||
|
||||
test "utf8 iterator on ascii" {
|
||||
const s = Utf8View.initComptime("abc");
|
||||
|
||||
var it1 = s.Iterator();
|
||||
debug.assert(std.mem.eql(u8, "a", ??it1.nextCodepointSlice()));
|
||||
debug.assert(std.mem.eql(u8, "b", ??it1.nextCodepointSlice()));
|
||||
debug.assert(std.mem.eql(u8, "c", ??it1.nextCodepointSlice()));
|
||||
debug.assert(it1.nextCodepointSlice() == null);
|
||||
|
||||
var it2 = s.Iterator();
|
||||
debug.assert(??it2.nextCodepoint() == 'a');
|
||||
debug.assert(??it2.nextCodepoint() == 'b');
|
||||
debug.assert(??it2.nextCodepoint() == 'c');
|
||||
debug.assert(it2.nextCodepoint() == null);
|
||||
}
|
||||
|
||||
test "utf8 view bad" {
|
||||
// Compile-time error.
|
||||
// const s3 = Utf8View.initComptime("\xfe\xf2");
|
||||
|
||||
const s = Utf8View.init("hel\xadlo");
|
||||
if (s) |_| { unreachable; } else |err| { debug.assert(err == error.InvalidUtf8); }
|
||||
}
|
||||
|
||||
test "utf8 view ok" {
|
||||
const s = Utf8View.initComptime("東京市");
|
||||
|
||||
var it1 = s.Iterator();
|
||||
debug.assert(std.mem.eql(u8, "東", ??it1.nextCodepointSlice()));
|
||||
debug.assert(std.mem.eql(u8, "京", ??it1.nextCodepointSlice()));
|
||||
debug.assert(std.mem.eql(u8, "市", ??it1.nextCodepointSlice()));
|
||||
debug.assert(it1.nextCodepointSlice() == null);
|
||||
|
||||
var it2 = s.Iterator();
|
||||
debug.assert(??it2.nextCodepoint() == 0x6771);
|
||||
debug.assert(??it2.nextCodepoint() == 0x4eac);
|
||||
debug.assert(??it2.nextCodepoint() == 0x5e02);
|
||||
debug.assert(it2.nextCodepoint() == null);
|
||||
}
|
||||
|
||||
test "bad utf8 slice" {
|
||||
debug.assert(utf8ValidateSlice("abc"));
|
||||
debug.assert(!utf8ValidateSlice("abc\xc0"));
|
||||
debug.assert(!utf8ValidateSlice("abc\xc0abc"));
|
||||
debug.assert(utf8ValidateSlice("abc\xdf\xbf"));
|
||||
}
|
||||
|
||||
test "valid utf8" {
|
||||
testValid("\x00", 0x0);
|
||||
testValid("\x20", 0x20);
|
||||
@ -145,17 +276,17 @@ fn testError(bytes: []const u8, expected_err: error) void {
|
||||
if (testDecode(bytes)) |_| {
|
||||
unreachable;
|
||||
} else |err| {
|
||||
std.debug.assert(err == expected_err);
|
||||
debug.assert(err == expected_err);
|
||||
}
|
||||
}
|
||||
|
||||
fn testValid(bytes: []const u8, expected_codepoint: u32) void {
|
||||
std.debug.assert((testDecode(bytes) catch unreachable) == expected_codepoint);
|
||||
debug.assert((testDecode(bytes) catch unreachable) == expected_codepoint);
|
||||
}
|
||||
|
||||
fn testDecode(bytes: []const u8) !u32 {
|
||||
const length = try utf8ByteSequenceLength(bytes[0]);
|
||||
if (bytes.len < length) return error.UnexpectedEof;
|
||||
std.debug.assert(bytes.len == length);
|
||||
debug.assert(bytes.len == length);
|
||||
return utf8Decode(bytes);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -32,6 +32,108 @@ fn funcWithConstPtrPtr(x: &const &i32) void {
|
||||
**x += 1;
|
||||
}
|
||||
|
||||
test "implicitly cast a container to a const pointer of it" {
|
||||
const z = Struct(void) { .x = void{} };
|
||||
assert(0 == @sizeOf(@typeOf(z)));
|
||||
assert(void{} == Struct(void).pointer(z).x);
|
||||
assert(void{} == Struct(void).pointer(&z).x);
|
||||
assert(void{} == Struct(void).maybePointer(z).x);
|
||||
assert(void{} == Struct(void).maybePointer(&z).x);
|
||||
assert(void{} == Struct(void).maybePointer(null).x);
|
||||
const s = Struct(u8) { .x = 42 };
|
||||
assert(0 != @sizeOf(@typeOf(s)));
|
||||
assert(42 == Struct(u8).pointer(s).x);
|
||||
assert(42 == Struct(u8).pointer(&s).x);
|
||||
assert(42 == Struct(u8).maybePointer(s).x);
|
||||
assert(42 == Struct(u8).maybePointer(&s).x);
|
||||
assert(0 == Struct(u8).maybePointer(null).x);
|
||||
const u = Union { .x = 42 };
|
||||
assert(42 == Union.pointer(u).x);
|
||||
assert(42 == Union.pointer(&u).x);
|
||||
assert(42 == Union.maybePointer(u).x);
|
||||
assert(42 == Union.maybePointer(&u).x);
|
||||
assert(0 == Union.maybePointer(null).x);
|
||||
const e = Enum.Some;
|
||||
assert(Enum.Some == Enum.pointer(e));
|
||||
assert(Enum.Some == Enum.pointer(&e));
|
||||
assert(Enum.Some == Enum.maybePointer(e));
|
||||
assert(Enum.Some == Enum.maybePointer(&e));
|
||||
assert(Enum.None == Enum.maybePointer(null));
|
||||
}
|
||||
|
||||
fn Struct(comptime T: type) type {
|
||||
return struct {
|
||||
const Self = this;
|
||||
x: T,
|
||||
|
||||
fn pointer(self: &const Self) Self {
|
||||
return *self;
|
||||
}
|
||||
|
||||
fn maybePointer(self: ?&const Self) Self {
|
||||
const none = Self { .x = if (T == void) void{} else 0 };
|
||||
return *(self ?? &none);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const Union = union {
|
||||
x: u8,
|
||||
|
||||
fn pointer(self: &const Union) Union {
|
||||
return *self;
|
||||
}
|
||||
|
||||
fn maybePointer(self: ?&const Union) Union {
|
||||
const none = Union { .x = 0 };
|
||||
return *(self ?? &none);
|
||||
}
|
||||
};
|
||||
|
||||
const Enum = enum {
|
||||
None,
|
||||
Some,
|
||||
|
||||
fn pointer(self: &const Enum) Enum {
|
||||
return *self;
|
||||
}
|
||||
|
||||
fn maybePointer(self: ?&const Enum) Enum {
|
||||
return *(self ?? &Enum.None);
|
||||
}
|
||||
};
|
||||
|
||||
test "implicitly cast indirect pointer to maybe-indirect pointer" {
|
||||
const S = struct {
|
||||
const Self = this;
|
||||
x: u8,
|
||||
fn constConst(p: &const &const Self) u8 {
|
||||
return (*p).x;
|
||||
}
|
||||
fn maybeConstConst(p: ?&const &const Self) u8 {
|
||||
return (*??p).x;
|
||||
}
|
||||
fn constConstConst(p: &const &const &const Self) u8 {
|
||||
return (**p).x;
|
||||
}
|
||||
fn maybeConstConstConst(p: ?&const &const &const Self) u8 {
|
||||
return (**??p).x;
|
||||
}
|
||||
};
|
||||
const s = S { .x = 42 };
|
||||
const p = &s;
|
||||
const q = &p;
|
||||
const r = &q;
|
||||
assert(42 == S.constConst(p));
|
||||
assert(42 == S.constConst(q));
|
||||
assert(42 == S.maybeConstConst(p));
|
||||
assert(42 == S.maybeConstConst(q));
|
||||
assert(42 == S.constConstConst(q));
|
||||
assert(42 == S.constConstConst(r));
|
||||
assert(42 == S.maybeConstConstConst(q));
|
||||
assert(42 == S.maybeConstConstConst(r));
|
||||
}
|
||||
|
||||
test "explicit cast from integer to error type" {
|
||||
testCastIntToErr(error.ItBroke);
|
||||
comptime testCastIntToErr(error.ItBroke);
|
||||
|
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;
|
||||
}
|
@ -3090,4 +3090,16 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
|
||||
,
|
||||
".tmp_source.zig:11:20: error: runtime cast to union 'Value' which has non-void fields",
|
||||
".tmp_source.zig:3:5: note: field 'A' has type 'i32'");
|
||||
|
||||
cases.add("self-referencing function pointer field",
|
||||
\\const S = struct {
|
||||
\\ f: fn(_: S) void,
|
||||
\\};
|
||||
\\fn f(_: S) void {
|
||||
\\}
|
||||
\\export fn entry() void {
|
||||
\\ var _ = S { .f = f };
|
||||
\\}
|
||||
,
|
||||
".tmp_source.zig:4:9: error: type 'S' is not copyable; cannot pass by value");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user