aboutsummaryrefslogtreecommitdiff
path: root/src/codegen.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2020-12-30 22:31:56 -0700
committerAndrew Kelley <andrew@ziglang.org>2020-12-30 21:41:02 -0800
commit3f7d9b5fc19e4081236b3b63aebbc80e1b17f5b5 (patch)
treea577bd97edf5d5da357d576c777861d443210aec /src/codegen.zig
parent133da8692e80532797dd91b32539cf2175280a95 (diff)
downloadzig-3f7d9b5fc19e4081236b3b63aebbc80e1b17f5b5.tar.gz
zig-3f7d9b5fc19e4081236b3b63aebbc80e1b17f5b5.zip
stage2: rework Value Payload layout
This is the same as the previous commit but for Value instead of Type. Add `Value.castTag` and note that it is preferable to call than `Value.cast`. This matches other abstractions in the codebase. Added a convenience function `Value.Tag.create` which really cleans up the callsites of creating `Value` objects. `Value` tags can now share payload types. This is in preparation for another improvement that I want to do.
Diffstat (limited to 'src/codegen.zig')
-rw-r--r--src/codegen.zig52
1 files changed, 26 insertions, 26 deletions
diff --git a/src/codegen.zig b/src/codegen.zig
index d98a87a440..f978115ebc 100644
--- a/src/codegen.zig
+++ b/src/codegen.zig
@@ -137,7 +137,7 @@ pub fn generateSymbol(
},
.Array => {
// TODO populate .debug_info for the array
- if (typed_value.val.cast(Value.Payload.Bytes)) |payload| {
+ if (typed_value.val.castTag(.bytes)) |payload| {
if (typed_value.ty.sentinel()) |sentinel| {
try code.ensureCapacity(code.items.len + payload.data.len + 1);
code.appendSliceAssumeCapacity(payload.data);
@@ -168,8 +168,8 @@ pub fn generateSymbol(
},
.Pointer => {
// TODO populate .debug_info for the pointer
- if (typed_value.val.cast(Value.Payload.DeclRef)) |payload| {
- const decl = payload.decl;
+ if (typed_value.val.castTag(.decl_ref)) |payload| {
+ const decl = payload.data;
if (decl.analysis != .complete) return error.AnalysisFail;
// TODO handle the dependency of this symbol on the decl's vaddr.
// If the decl changes vaddr, then this symbol needs to get regenerated.
@@ -432,7 +432,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
@panic("Attempted to compile for architecture that was disabled by build configuration");
}
- const module_fn = typed_value.val.cast(Value.Payload.Function).?.func;
+ const module_fn = typed_value.val.castTag(.function).?.data;
const fn_type = module_fn.owner_decl.typed_value.most_recent.typed_value.ty;
@@ -1579,9 +1579,9 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
}
}
- if (inst.func.cast(ir.Inst.Constant)) |func_inst| {
- if (func_inst.val.cast(Value.Payload.Function)) |func_val| {
- const func = func_val.func;
+ if (inst.func.value()) |func_value| {
+ if (func_value.castTag(.function)) |func_payload| {
+ const func = func_payload.data;
const ptr_bits = self.target.cpu.arch.ptrBitWidth();
const ptr_bytes: u64 = @divExact(ptr_bits, 8);
@@ -1607,9 +1607,9 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
.riscv64 => {
if (info.args.len > 0) return self.fail(inst.base.src, "TODO implement fn args for {}", .{self.target.cpu.arch});
- if (inst.func.cast(ir.Inst.Constant)) |func_inst| {
- if (func_inst.val.cast(Value.Payload.Function)) |func_val| {
- const func = func_val.func;
+ if (inst.func.value()) |func_value| {
+ if (func_value.castTag(.function)) |func_payload| {
+ const func = func_payload.data;
const ptr_bits = self.target.cpu.arch.ptrBitWidth();
const ptr_bytes: u64 = @divExact(ptr_bits, 8);
@@ -1631,12 +1631,12 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
}
},
.spu_2 => {
- if (inst.func.cast(ir.Inst.Constant)) |func_inst| {
+ if (inst.func.value()) |func_value| {
if (info.args.len != 0) {
return self.fail(inst.base.src, "TODO implement call with more than 0 parameters", .{});
}
- if (func_inst.val.cast(Value.Payload.Function)) |func_val| {
- const func = func_val.func;
+ if (func_value.castTag(.function)) |func_payload| {
+ const func = func_payload.data;
const got_addr = if (self.bin_file.cast(link.File.Elf)) |elf_file| blk: {
const got = &elf_file.program_headers.items[elf_file.phdr_got_index.?];
break :blk @intCast(u16, got.p_vaddr + func.owner_decl.link.elf.offset_table_index * 2);
@@ -1705,9 +1705,9 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
}
}
- if (inst.func.cast(ir.Inst.Constant)) |func_inst| {
- if (func_inst.val.cast(Value.Payload.Function)) |func_val| {
- const func = func_val.func;
+ if (inst.func.value()) |func_value| {
+ if (func_value.castTag(.function)) |func_payload| {
+ const func = func_payload.data;
const ptr_bits = self.target.cpu.arch.ptrBitWidth();
const ptr_bytes: u64 = @divExact(ptr_bits, 8);
const got_addr = if (self.bin_file.cast(link.File.Elf)) |elf_file| blk: {
@@ -1766,9 +1766,9 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
}
}
- if (inst.func.cast(ir.Inst.Constant)) |func_inst| {
- if (func_inst.val.cast(Value.Payload.Function)) |func_val| {
- const func = func_val.func;
+ if (inst.func.value()) |func_value| {
+ if (func_value.castTag(.function)) |func_payload| {
+ const func = func_payload.data;
const ptr_bits = self.target.cpu.arch.ptrBitWidth();
const ptr_bytes: u64 = @divExact(ptr_bits, 8);
const got_addr = if (self.bin_file.cast(link.File.Elf)) |elf_file| blk: {
@@ -1825,9 +1825,9 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
}
}
- if (inst.func.cast(ir.Inst.Constant)) |func_inst| {
- if (func_inst.val.cast(Value.Payload.Function)) |func_val| {
- const func = func_val.func;
+ if (inst.func.value()) |func_value| {
+ if (func_value.castTag(.function)) |func_payload| {
+ const func = func_payload.data;
const text_segment = &macho_file.load_commands.items[macho_file.text_segment_cmd_index.?].Segment;
const got = &text_segment.sections.items[macho_file.got_section_index.?];
const got_addr = got.addr + func.owner_decl.link.macho.offset_table_index * @sizeOf(u64);
@@ -3223,20 +3223,20 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
const ptr_bytes: u64 = @divExact(ptr_bits, 8);
switch (typed_value.ty.zigTypeTag()) {
.Pointer => {
- if (typed_value.val.cast(Value.Payload.DeclRef)) |payload| {
+ if (typed_value.val.castTag(.decl_ref)) |payload| {
if (self.bin_file.cast(link.File.Elf)) |elf_file| {
- const decl = payload.decl;
+ const decl = payload.data;
const got = &elf_file.program_headers.items[elf_file.phdr_got_index.?];
const got_addr = got.p_vaddr + decl.link.elf.offset_table_index * ptr_bytes;
return MCValue{ .memory = got_addr };
} else if (self.bin_file.cast(link.File.MachO)) |macho_file| {
- const decl = payload.decl;
+ const decl = payload.data;
const text_segment = &macho_file.load_commands.items[macho_file.text_segment_cmd_index.?].Segment;
const got = &text_segment.sections.items[macho_file.got_section_index.?];
const got_addr = got.addr + decl.link.macho.offset_table_index * ptr_bytes;
return MCValue{ .memory = got_addr };
} else if (self.bin_file.cast(link.File.Coff)) |coff_file| {
- const decl = payload.decl;
+ const decl = payload.data;
const got_addr = coff_file.offset_table_virtual_address + decl.link.coff.offset_table_index * ptr_bytes;
return MCValue{ .memory = got_addr };
} else {