diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-04-27 18:36:12 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2021-04-28 16:57:01 -0700 |
| commit | f86469bc5eea2b7bd95222d00a11bd287bfdfedf (patch) | |
| tree | c34ef71820e4c7b962a631f2b9cdecdff3f5aa79 /src/codegen | |
| parent | fa6bb4b662155e4d6a61cc551b5d02a2a7d5d144 (diff) | |
| download | zig-f86469bc5eea2b7bd95222d00a11bd287bfdfedf.tar.gz zig-f86469bc5eea2b7bd95222d00a11bd287bfdfedf.zip | |
stage2: semaDecl properly analyzes the decl block
Also flattened out Decl TypedValue fields into
ty, val, has_tv
and add relevant fields to Decl for alignment and link section.
Diffstat (limited to 'src/codegen')
| -rw-r--r-- | src/codegen/c.zig | 26 | ||||
| -rw-r--r-- | src/codegen/llvm.zig | 22 | ||||
| -rw-r--r-- | src/codegen/wasm.zig | 3 |
3 files changed, 29 insertions, 22 deletions
diff --git a/src/codegen/c.zig b/src/codegen/c.zig index 441449707a..43b851019b 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -190,8 +190,8 @@ pub const DeclGen = struct { const decl = val.castTag(.decl_ref).?.data; // Determine if we must pointer cast. - const decl_tv = decl.typed_value.most_recent.typed_value; - if (t.eql(decl_tv.ty)) { + assert(decl.has_tv); + if (t.eql(decl.ty)) { try writer.print("&{s}", .{decl.name}); } else { try writer.writeAll("("); @@ -326,12 +326,11 @@ pub const DeclGen = struct { if (!is_global) { try w.writeAll("static "); } - const tv = dg.decl.typed_value.most_recent.typed_value; - try dg.renderType(w, tv.ty.fnReturnType()); + try dg.renderType(w, dg.decl.ty.fnReturnType()); const decl_name = mem.span(dg.decl.name); try w.print(" {s}(", .{decl_name}); - const param_len = tv.ty.fnParamLen(); - const is_var_args = tv.ty.fnIsVarArgs(); + const param_len = dg.decl.ty.fnParamLen(); + const is_var_args = dg.decl.ty.fnIsVarArgs(); if (param_len == 0 and !is_var_args) try w.writeAll("void") else { @@ -340,7 +339,7 @@ pub const DeclGen = struct { if (index > 0) { try w.writeAll(", "); } - try dg.renderType(w, tv.ty.fnParamType(index)); + try dg.renderType(w, dg.decl.ty.fnParamType(index)); try w.print(" a{d}", .{index}); } } @@ -545,8 +544,10 @@ pub fn genDecl(o: *Object) !void { const tracy = trace(@src()); defer tracy.end(); - const tv = o.dg.decl.typed_value.most_recent.typed_value; - + const tv: TypedValue = .{ + .ty = o.dg.decl.ty, + .val = o.dg.decl.val, + }; if (tv.val.castTag(.function)) |func_payload| { const is_global = o.dg.functionIsGlobal(tv); const fwd_decl_writer = o.dg.fwd_decl.writer(); @@ -589,7 +590,10 @@ pub fn genHeader(dg: *DeclGen) error{ AnalysisFail, OutOfMemory }!void { const tracy = trace(@src()); defer tracy.end(); - const tv = dg.decl.typed_value.most_recent.typed_value; + const tv: TypedValue = .{ + .ty = dg.decl.ty, + .val = dg.decl.val, + }; const writer = dg.fwd_decl.writer(); switch (tv.ty.zigTypeTag()) { @@ -842,7 +846,7 @@ fn genCall(o: *Object, inst: *Inst.Call) !CValue { else unreachable; - const fn_ty = fn_decl.typed_value.most_recent.typed_value.ty; + const fn_ty = fn_decl.ty; const ret_ty = fn_ty.fnReturnType(); const unused_result = inst.base.isUnused(); var result_local: CValue = .none; diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index 94fb1de8c4..0ba14f98ba 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -325,17 +325,17 @@ pub const DeclGen = struct { fn genDecl(self: *DeclGen) !void { const decl = self.decl; - const typed_value = decl.typed_value.most_recent.typed_value; + assert(decl.has_tv); - log.debug("gen: {s} type: {}, value: {}", .{ decl.name, typed_value.ty, typed_value.val }); + log.debug("gen: {s} type: {}, value: {}", .{ decl.name, decl.ty, decl.val }); - if (typed_value.val.castTag(.function)) |func_payload| { + if (decl.val.castTag(.function)) |func_payload| { const func = func_payload.data; const llvm_func = try self.resolveLLVMFunction(func.owner_decl); // This gets the LLVM values from the function and stores them in `self.args`. - const fn_param_len = func.owner_decl.typed_value.most_recent.typed_value.ty.fnParamLen(); + const fn_param_len = func.owner_decl.ty.fnParamLen(); var args = try self.gpa.alloc(*const llvm.Value, fn_param_len); for (args) |*arg, i| { @@ -368,7 +368,7 @@ pub const DeclGen = struct { defer fg.deinit(); try fg.genBody(func.body); - } else if (typed_value.val.castTag(.extern_fn)) |extern_fn| { + } else if (decl.val.castTag(.extern_fn)) |extern_fn| { _ = try self.resolveLLVMFunction(extern_fn.data); } else { _ = try self.resolveGlobalDecl(decl); @@ -380,7 +380,8 @@ pub const DeclGen = struct { // TODO: do we want to store this in our own datastructure? if (self.llvmModule().getNamedFunction(func.name)) |llvm_fn| return llvm_fn; - const zig_fn_type = func.typed_value.most_recent.typed_value.ty; + assert(func.has_tv); + const zig_fn_type = func.ty; const return_type = zig_fn_type.fnReturnType(); const fn_param_len = zig_fn_type.fnParamLen(); @@ -415,11 +416,11 @@ pub const DeclGen = struct { // TODO: do we want to store this in our own datastructure? if (self.llvmModule().getNamedGlobal(decl.name)) |val| return val; - const typed_value = decl.typed_value.most_recent.typed_value; + assert(decl.has_tv); // TODO: remove this redundant `getLLVMType`, it is also called in `genTypedValue`. - const llvm_type = try self.getLLVMType(typed_value.ty); - const val = try self.genTypedValue(typed_value, null); + const llvm_type = try self.getLLVMType(decl.ty); + const val = try self.genTypedValue(.{ .ty = decl.ty, .val = decl.val }, null); const global = self.llvmModule().addGlobal(llvm_type, decl.name); llvm.setInitializer(global, val); @@ -688,7 +689,8 @@ pub const FuncGen = struct { else unreachable; - const zig_fn_type = fn_decl.typed_value.most_recent.typed_value.ty; + assert(fn_decl.has_tv); + const zig_fn_type = fn_decl.ty; const llvm_fn = try self.dg.resolveLLVMFunction(fn_decl); const num_args = inst.args.len; diff --git a/src/codegen/wasm.zig b/src/codegen/wasm.zig index 400a5cd1a3..97155b80dd 100644 --- a/src/codegen/wasm.zig +++ b/src/codegen/wasm.zig @@ -591,7 +591,8 @@ pub const Context = struct { } fn genFunctype(self: *Context) InnerError!void { - const ty = self.decl.typed_value.most_recent.typed_value.ty; + assert(self.decl.has_tv); + const ty = self.decl.ty; const writer = self.func_type_data.writer(); try writer.writeByte(wasm.function_type); |
