move Module to its own file

This commit is contained in:
Andrew Kelley 2020-05-15 21:44:33 -04:00
parent 64f4ef7556
commit f2feb4e47a
7 changed files with 2065 additions and 2056 deletions

2018
src-self-hosted/Module.zig Normal file

File diff suppressed because it is too large Load Diff

View File

@ -6,6 +6,8 @@ const Type = @import("type.zig").Type;
const Value = @import("value.zig").Value;
const TypedValue = @import("TypedValue.zig");
const link = @import("link.zig");
const Module = @import("Module.zig");
const ErrorMsg = Module.ErrorMsg;
const Target = std.Target;
const Allocator = mem.Allocator;
@ -14,7 +16,7 @@ pub const Result = union(enum) {
appended: void,
/// The value is available externally, `code` is unused.
externally_managed: []const u8,
fail: *ir.ErrorMsg,
fail: *Module.ErrorMsg,
};
pub fn generateSymbol(
@ -77,7 +79,7 @@ pub fn generateSymbol(
}
}
return Result{
.fail = try ir.ErrorMsg.create(
.fail = try ErrorMsg.create(
bin_file.allocator,
src,
"TODO implement generateSymbol for more kinds of arrays",
@ -107,7 +109,7 @@ pub fn generateSymbol(
return Result{ .appended = {} };
}
return Result{
.fail = try ir.ErrorMsg.create(
.fail = try ErrorMsg.create(
bin_file.allocator,
src,
"TODO implement generateSymbol for pointer {}",
@ -123,7 +125,7 @@ pub fn generateSymbol(
return Result{ .appended = {} };
}
return Result{
.fail = try ir.ErrorMsg.create(
.fail = try ErrorMsg.create(
bin_file.allocator,
src,
"TODO implement generateSymbol for int type '{}'",
@ -133,7 +135,7 @@ pub fn generateSymbol(
},
else => |t| {
return Result{
.fail = try ir.ErrorMsg.create(
.fail = try ErrorMsg.create(
bin_file.allocator,
src,
"TODO implement generateSymbol for type '{}'",
@ -147,10 +149,10 @@ pub fn generateSymbol(
const Function = struct {
bin_file: *link.ElfFile,
target: *const std.Target,
mod_fn: *const ir.Module.Fn,
mod_fn: *const Module.Fn,
code: *std.ArrayList(u8),
inst_table: std.AutoHashMap(*ir.Inst, MCValue),
err_msg: ?*ir.ErrorMsg,
err_msg: ?*ErrorMsg,
const MCValue = union(enum) {
none,
@ -570,7 +572,7 @@ const Function = struct {
fn fail(self: *Function, src: usize, comptime format: []const u8, args: var) error{ CodegenFail, OutOfMemory } {
@setCold(true);
assert(self.err_msg == null);
self.err_msg = try ir.ErrorMsg.create(self.code.allocator, src, format, args);
self.err_msg = try ErrorMsg.create(self.code.allocator, src, format, args);
return error.CodegenFail;
}
};

File diff suppressed because it is too large Load Diff

View File

@ -3,6 +3,7 @@ const mem = std.mem;
const assert = std.debug.assert;
const Allocator = std.mem.Allocator;
const ir = @import("ir.zig");
const Module = @import("Module.zig");
const fs = std.fs;
const elf = std.elf;
const codegen = @import("codegen.zig");
@ -45,8 +46,8 @@ pub fn writeFilePath(
allocator: *Allocator,
dir: fs.Dir,
sub_path: []const u8,
module: ir.Module,
errors: *std.ArrayList(ir.ErrorMsg),
module: Module,
errors: *std.ArrayList(Module.ErrorMsg),
) !void {
const options: Options = .{
.target = module.target,
@ -755,7 +756,7 @@ pub const ElfFile = struct {
};
}
pub fn allocateDeclIndexes(self: *ElfFile, decl: *ir.Module.Decl) !void {
pub fn allocateDeclIndexes(self: *ElfFile, decl: *Module.Decl) !void {
if (decl.link.local_sym_index != 0) return;
try self.local_symbols.ensureCapacity(self.allocator, self.local_symbols.items.len + 1);
@ -784,7 +785,7 @@ pub const ElfFile = struct {
};
}
pub fn updateDecl(self: *ElfFile, module: *ir.Module, decl: *ir.Module.Decl) !void {
pub fn updateDecl(self: *ElfFile, module: *Module, decl: *Module.Decl) !void {
var code_buffer = std.ArrayList(u8).init(self.allocator);
defer code_buffer.deinit();
@ -878,16 +879,16 @@ pub const ElfFile = struct {
try self.file.pwriteAll(code, file_offset);
// Since we updated the vaddr and the size, each corresponding export symbol also needs to be updated.
const decl_exports = module.decl_exports.getValue(decl) orelse &[0]*ir.Module.Export{};
const decl_exports = module.decl_exports.getValue(decl) orelse &[0]*Module.Export{};
return self.updateDeclExports(module, decl, decl_exports);
}
/// Must be called only after a successful call to `updateDecl`.
pub fn updateDeclExports(
self: *ElfFile,
module: *ir.Module,
decl: *const ir.Module.Decl,
exports: []const *ir.Module.Export,
module: *Module,
decl: *const Module.Decl,
exports: []const *Module.Export,
) !void {
try self.global_symbols.ensureCapacity(self.allocator, self.global_symbols.items.len + exports.len);
const typed_value = decl.typed_value.most_recent.typed_value;
@ -900,7 +901,7 @@ pub const ElfFile = struct {
try module.failed_exports.ensureCapacity(module.failed_exports.size + 1);
module.failed_exports.putAssumeCapacityNoClobber(
exp,
try ir.ErrorMsg.create(self.allocator, 0, "Unimplemented: ExportOptions.section", .{}),
try Module.ErrorMsg.create(self.allocator, 0, "Unimplemented: ExportOptions.section", .{}),
);
continue;
}
@ -918,7 +919,7 @@ pub const ElfFile = struct {
try module.failed_exports.ensureCapacity(module.failed_exports.size + 1);
module.failed_exports.putAssumeCapacityNoClobber(
exp,
try ir.ErrorMsg.create(self.allocator, 0, "Unimplemented: GlobalLinkage.LinkOnce", .{}),
try Module.ErrorMsg.create(self.allocator, 0, "Unimplemented: GlobalLinkage.LinkOnce", .{}),
);
continue;
},

View File

@ -6,9 +6,10 @@ const process = std.process;
const Allocator = mem.Allocator;
const ArrayList = std.ArrayList;
const ast = std.zig.ast;
const ir = @import("ir.zig");
const Module = @import("Module.zig");
const link = @import("link.zig");
const Package = @import("Package.zig");
const zir = @import("zir.zig");
const LibCInstallation = @import("libc_installation.zig").LibCInstallation;
@ -438,7 +439,7 @@ fn buildOutputType(
const root_pkg = try Package.create(gpa, fs.cwd(), ".", src_path);
errdefer root_pkg.destroy();
const root_scope = try gpa.create(ir.Module.Scope.ZIRModule);
const root_scope = try gpa.create(Module.Scope.ZIRModule);
errdefer gpa.destroy(root_scope);
root_scope.* = .{
.sub_file_path = root_pkg.root_src_path,
@ -447,19 +448,19 @@ fn buildOutputType(
.status = .never_loaded,
};
break :blk ir.Module{
break :blk Module{
.allocator = gpa,
.root_pkg = root_pkg,
.root_scope = root_scope,
.bin_file = &bin_file,
.optimize_mode = .Debug,
.decl_table = std.AutoHashMap(ir.Module.Decl.Hash, *ir.Module.Decl).init(gpa),
.decl_exports = std.AutoHashMap(*ir.Module.Decl, []*ir.Module.Export).init(gpa),
.export_owners = std.AutoHashMap(*ir.Module.Decl, []*ir.Module.Export).init(gpa),
.failed_decls = std.AutoHashMap(*ir.Module.Decl, *ir.ErrorMsg).init(gpa),
.failed_files = std.AutoHashMap(*ir.Module.Scope.ZIRModule, *ir.ErrorMsg).init(gpa),
.failed_exports = std.AutoHashMap(*ir.Module.Export, *ir.ErrorMsg).init(gpa),
.work_queue = std.fifo.LinearFifo(ir.Module.WorkItem, .Dynamic).init(gpa),
.decl_table = std.AutoHashMap(Module.Decl.Hash, *Module.Decl).init(gpa),
.decl_exports = std.AutoHashMap(*Module.Decl, []*Module.Export).init(gpa),
.export_owners = std.AutoHashMap(*Module.Decl, []*Module.Export).init(gpa),
.failed_decls = std.AutoHashMap(*Module.Decl, *Module.ErrorMsg).init(gpa),
.failed_files = std.AutoHashMap(*Module.Scope.ZIRModule, *Module.ErrorMsg).init(gpa),
.failed_exports = std.AutoHashMap(*Module.Export, *Module.ErrorMsg).init(gpa),
.work_queue = std.fifo.LinearFifo(Module.WorkItem, .Dynamic).init(gpa),
};
};
defer module.deinit();
@ -491,7 +492,7 @@ fn buildOutputType(
}
}
fn updateModule(gpa: *Allocator, module: *ir.Module, zir_out_path: ?[]const u8) !void {
fn updateModule(gpa: *Allocator, module: *Module, zir_out_path: ?[]const u8) !void {
try module.update();
var errors = try module.getAllErrorsAlloc();
@ -509,7 +510,7 @@ fn updateModule(gpa: *Allocator, module: *ir.Module, zir_out_path: ?[]const u8)
}
if (zir_out_path) |zop| {
var new_zir_module = try ir.text.emit_zir(gpa, module.*);
var new_zir_module = try zir.emit(gpa, module.*);
defer new_zir_module.deinit(gpa);
const baf = try io.BufferedAtomicFile.create(gpa, fs.cwd(), zop, .{});

View File

@ -6,7 +6,7 @@ const BigIntConst = std.math.big.int.Const;
const BigIntMutable = std.math.big.int.Mutable;
const Target = std.Target;
const Allocator = std.mem.Allocator;
const ir = @import("ir.zig");
const Module = @import("Module.zig");
/// This is the raw data, with no bookkeeping, no memory awareness,
/// no de-duplication, and no type system awareness.
@ -904,7 +904,7 @@ pub const Value = extern union {
pub const Function = struct {
base: Payload = Payload{ .tag = .function },
func: *ir.Module.Fn,
func: *Module.Fn,
};
pub const ArraySentinel0_u8_Type = struct {
@ -926,7 +926,7 @@ pub const Value = extern union {
/// Represents a pointer to a decl, not the value of the decl.
pub const DeclRef = struct {
base: Payload = Payload{ .tag = .decl_ref },
decl: *ir.Module.Decl,
decl: *Module.Decl,
};
pub const ElemPtr = struct {

View File

@ -6,10 +6,11 @@ const Allocator = std.mem.Allocator;
const assert = std.debug.assert;
const BigIntConst = std.math.big.int.Const;
const BigIntMutable = std.math.big.int.Mutable;
const Type = @import("../type.zig").Type;
const Value = @import("../value.zig").Value;
const TypedValue = @import("../TypedValue.zig");
const ir = @import("../ir.zig");
const Type = @import("type.zig").Type;
const Value = @import("value.zig").Value;
const TypedValue = @import("TypedValue.zig");
const ir = @import("ir.zig");
const IrModule = @import("Module.zig");
/// These are instructions that correspond to the ZIR text format. See `ir.Inst` for
/// in-memory, analyzed instructions with types and values.
@ -990,7 +991,7 @@ const Parser = struct {
}
};
pub fn emit_zir(allocator: *Allocator, old_module: ir.Module) !Module {
pub fn emit(allocator: *Allocator, old_module: IrModule) !Module {
var ctx: EmitZIR = .{
.allocator = allocator,
.decls = .{},
@ -1013,7 +1014,7 @@ pub fn emit_zir(allocator: *Allocator, old_module: ir.Module) !Module {
const EmitZIR = struct {
allocator: *Allocator,
arena: std.heap.ArenaAllocator,
old_module: *const ir.Module,
old_module: *const IrModule,
decls: std.ArrayListUnmanaged(*Inst),
decl_table: std.AutoHashMap(*ir.Inst, *Inst),
@ -1171,7 +1172,7 @@ const EmitZIR = struct {
fn emitBody(
self: *EmitZIR,
body: ir.Module.Body,
body: IrModule.Body,
inst_table: *std.AutoHashMap(*ir.Inst, *Inst),
instructions: *std.ArrayList(*Inst),
) Allocator.Error!void {