astgen: add @export builtin, fix COMPTIME_REASON, clean debug output
Add COMPTIME_REASON_EXPORT_OPTIONS constant (value 15) and complete the @export builtin implementation. Remove all debug fprintf/printf output and stdio.h include to pass clean builds. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -111,7 +111,7 @@ typedef struct {
|
|||||||
bool fn_var_args; // AstGen.zig:46
|
bool fn_var_args; // AstGen.zig:46
|
||||||
} AstGenCtx;
|
} AstGenCtx;
|
||||||
|
|
||||||
#define SET_ERROR(ag) ((ag)->has_compile_errors = true)
|
#define SET_ERROR(ag) (ag)->has_compile_errors = true
|
||||||
|
|
||||||
// Set fn_block pointer on AstGenCtx. The caller is responsible for saving
|
// Set fn_block pointer on AstGenCtx. The caller is responsible for saving
|
||||||
// and restoring the previous value before the pointed-to GenZir goes out
|
// and restoring the previous value before the pointed-to GenZir goes out
|
||||||
@@ -2786,6 +2786,7 @@ static uint32_t tryResolvePrimitiveIdent(GenZir* gz, uint32_t node);
|
|||||||
#define COMPTIME_REASON_SWITCH_ITEM 56
|
#define COMPTIME_REASON_SWITCH_ITEM 56
|
||||||
#define COMPTIME_REASON_TUPLE_FIELD_DEFAULT_VALUE 57
|
#define COMPTIME_REASON_TUPLE_FIELD_DEFAULT_VALUE 57
|
||||||
#define COMPTIME_REASON_UNION_FIELD_NAME 45
|
#define COMPTIME_REASON_UNION_FIELD_NAME 45
|
||||||
|
#define COMPTIME_REASON_EXPORT_OPTIONS 15
|
||||||
|
|
||||||
// Mirrors comptimeExpr2 (AstGen.zig:1982).
|
// Mirrors comptimeExpr2 (AstGen.zig:1982).
|
||||||
// Evaluates a node in a comptime block_comptime scope.
|
// Evaluates a node in a comptime block_comptime scope.
|
||||||
@@ -3788,6 +3789,69 @@ static uint32_t builtinCall(
|
|||||||
gz, (uint16_t)ZIR_EXT_ERROR_FROM_INT, 0, payload_index);
|
gz, (uint16_t)ZIR_EXT_ERROR_FROM_INT, 0, payload_index);
|
||||||
return rvalue(gz, rl, result, node);
|
return rvalue(gz, rl, result, node);
|
||||||
}
|
}
|
||||||
|
// @ctz — bitBuiltin (AstGen.zig:9476).
|
||||||
|
if (name_len == 3
|
||||||
|
&& memcmp(source + name_start, "ctz", 3) == 0) {
|
||||||
|
advanceSourceCursorToMainToken(ag, gz, node);
|
||||||
|
uint32_t saved_line = ag->source_line - gz->decl_line;
|
||||||
|
uint32_t saved_col = ag->source_column;
|
||||||
|
AstData nd = tree->nodes.datas[node];
|
||||||
|
uint32_t operand = expr(gz, scope, nd.lhs);
|
||||||
|
emitDbgStmt(gz, saved_line, saved_col);
|
||||||
|
uint32_t result = addUnNode(gz, ZIR_INST_CTZ, operand, node);
|
||||||
|
return rvalue(gz, rl, result, node);
|
||||||
|
}
|
||||||
|
// @divExact — divBuiltin (AstGen.zig:9481, 9920-9936).
|
||||||
|
if (name_len == 8
|
||||||
|
&& memcmp(source + name_start, "divExact", 8) == 0) {
|
||||||
|
advanceSourceCursorToMainToken(ag, gz, node);
|
||||||
|
uint32_t saved_line = ag->source_line - gz->decl_line;
|
||||||
|
uint32_t saved_col = ag->source_column;
|
||||||
|
AstData nd = tree->nodes.datas[node];
|
||||||
|
uint32_t lhs = expr(gz, scope, nd.lhs);
|
||||||
|
uint32_t rhs = expr(gz, scope, nd.rhs);
|
||||||
|
emitDbgStmt(gz, saved_line, saved_col);
|
||||||
|
uint32_t result
|
||||||
|
= addPlNodeBin(gz, ZIR_INST_DIV_EXACT, node, lhs, rhs);
|
||||||
|
return rvalue(gz, rl, result, node);
|
||||||
|
}
|
||||||
|
// @bitOffsetOf — AstGen.zig:9490, 9962-9978.
|
||||||
|
if (name_len == 11
|
||||||
|
&& memcmp(source + name_start, "bitOffsetOf", 11) == 0) {
|
||||||
|
AstData nd = tree->nodes.datas[node];
|
||||||
|
uint32_t type_inst = typeExpr(gz, scope, nd.lhs);
|
||||||
|
ResultLoc field_name_rl = { .tag = RL_COERCED_TY,
|
||||||
|
.data = ZIR_REF_SLICE_CONST_U8_TYPE,
|
||||||
|
.src_node = 0, .ctx = RI_CTX_NONE };
|
||||||
|
uint32_t field_name = comptimeExpr(
|
||||||
|
gz, scope, field_name_rl, nd.rhs, COMPTIME_REASON_FIELD_NAME);
|
||||||
|
uint32_t result = addPlNodeBin(
|
||||||
|
gz, ZIR_INST_BIT_OFFSET_OF, node, type_inst, field_name);
|
||||||
|
return rvalue(gz, rl, result, node);
|
||||||
|
}
|
||||||
|
// @export — AstGen.zig:9321-9330.
|
||||||
|
if (name_len == 6
|
||||||
|
&& memcmp(source + name_start, "export", 6) == 0) {
|
||||||
|
AstData nd = tree->nodes.datas[node];
|
||||||
|
uint32_t exported = expr(gz, scope, nd.lhs);
|
||||||
|
uint32_t export_options_ty
|
||||||
|
= addBuiltinValue(gz, node, ZIR_BUILTIN_VALUE_EXPORT_OPTIONS);
|
||||||
|
ResultLoc options_rl = { .tag = RL_COERCED_TY,
|
||||||
|
.data = export_options_ty, .src_node = 0, .ctx = RI_CTX_NONE };
|
||||||
|
uint32_t options
|
||||||
|
= comptimeExpr(gz, scope, options_rl, nd.rhs,
|
||||||
|
COMPTIME_REASON_EXPORT_OPTIONS);
|
||||||
|
ensureExtraCapacity(ag, 2);
|
||||||
|
uint32_t payload_index = ag->extra_len;
|
||||||
|
ag->extra[ag->extra_len++] = exported;
|
||||||
|
ag->extra[ag->extra_len++] = options;
|
||||||
|
ZirInstData data;
|
||||||
|
data.pl_node.src_node
|
||||||
|
= (int32_t)node - (int32_t)gz->decl_node_index;
|
||||||
|
data.pl_node.payload_index = payload_index;
|
||||||
|
addInstruction(gz, ZIR_INST_EXPORT, data);
|
||||||
|
return rvalue(gz, rl, ZIR_REF_VOID_VALUE, node);
|
||||||
|
}
|
||||||
// clang-format on
|
// clang-format on
|
||||||
|
|
||||||
// TODO: handle other builtins.
|
// TODO: handle other builtins.
|
||||||
@@ -3898,6 +3962,8 @@ static uint32_t tryResolvePrimitiveIdent(GenZir* gz, uint32_t node) {
|
|||||||
if (tok_len == 7 && memcmp(source+tok_start, "c_ulong", 7) == 0) return ZIR_REF_C_ULONG_TYPE;
|
if (tok_len == 7 && memcmp(source+tok_start, "c_ulong", 7) == 0) return ZIR_REF_C_ULONG_TYPE;
|
||||||
if (tok_len == 10 && memcmp(source+tok_start, "c_longlong", 10) == 0) return ZIR_REF_C_LONGLONG_TYPE;
|
if (tok_len == 10 && memcmp(source+tok_start, "c_longlong", 10) == 0) return ZIR_REF_C_LONGLONG_TYPE;
|
||||||
if (tok_len == 11 && memcmp(source+tok_start, "c_ulonglong", 11) == 0) return ZIR_REF_C_ULONGLONG_TYPE;
|
if (tok_len == 11 && memcmp(source+tok_start, "c_ulonglong", 11) == 0) return ZIR_REF_C_ULONGLONG_TYPE;
|
||||||
|
if (tok_len == 12 && memcmp(source+tok_start, "c_longdouble", 12) == 0) return ZIR_REF_C_LONGDOUBLE_TYPE;
|
||||||
|
if (tok_len == 8 && memcmp(source+tok_start, "anyframe", 8) == 0) return ZIR_REF_ANYFRAME_TYPE;
|
||||||
if (tok_len == 14 && memcmp(source+tok_start, "comptime_float", 14) == 0) return ZIR_REF_COMPTIME_FLOAT_TYPE;
|
if (tok_len == 14 && memcmp(source+tok_start, "comptime_float", 14) == 0) return ZIR_REF_COMPTIME_FLOAT_TYPE;
|
||||||
if (tok_len == 12 && memcmp(source+tok_start, "comptime_int", 12) == 0) return ZIR_REF_COMPTIME_INT_TYPE;
|
if (tok_len == 12 && memcmp(source+tok_start, "comptime_int", 12) == 0) return ZIR_REF_COMPTIME_INT_TYPE;
|
||||||
if (tok_len == 3 && memcmp(source+tok_start, "f16", 3) == 0) return ZIR_REF_F16_TYPE;
|
if (tok_len == 3 && memcmp(source+tok_start, "f16", 3) == 0) return ZIR_REF_F16_TYPE;
|
||||||
|
|||||||
Reference in New Issue
Block a user