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 Value = @import("value.zig").Value;
const TypedValue = @import("TypedValue.zig"); const TypedValue = @import("TypedValue.zig");
const link = @import("link.zig"); const link = @import("link.zig");
const Module = @import("Module.zig");
const ErrorMsg = Module.ErrorMsg;
const Target = std.Target; const Target = std.Target;
const Allocator = mem.Allocator; const Allocator = mem.Allocator;
@ -14,7 +16,7 @@ pub const Result = union(enum) {
appended: void, appended: void,
/// The value is available externally, `code` is unused. /// The value is available externally, `code` is unused.
externally_managed: []const u8, externally_managed: []const u8,
fail: *ir.ErrorMsg, fail: *Module.ErrorMsg,
}; };
pub fn generateSymbol( pub fn generateSymbol(
@ -77,7 +79,7 @@ pub fn generateSymbol(
} }
} }
return Result{ return Result{
.fail = try ir.ErrorMsg.create( .fail = try ErrorMsg.create(
bin_file.allocator, bin_file.allocator,
src, src,
"TODO implement generateSymbol for more kinds of arrays", "TODO implement generateSymbol for more kinds of arrays",
@ -107,7 +109,7 @@ pub fn generateSymbol(
return Result{ .appended = {} }; return Result{ .appended = {} };
} }
return Result{ return Result{
.fail = try ir.ErrorMsg.create( .fail = try ErrorMsg.create(
bin_file.allocator, bin_file.allocator,
src, src,
"TODO implement generateSymbol for pointer {}", "TODO implement generateSymbol for pointer {}",
@ -123,7 +125,7 @@ pub fn generateSymbol(
return Result{ .appended = {} }; return Result{ .appended = {} };
} }
return Result{ return Result{
.fail = try ir.ErrorMsg.create( .fail = try ErrorMsg.create(
bin_file.allocator, bin_file.allocator,
src, src,
"TODO implement generateSymbol for int type '{}'", "TODO implement generateSymbol for int type '{}'",
@ -133,7 +135,7 @@ pub fn generateSymbol(
}, },
else => |t| { else => |t| {
return Result{ return Result{
.fail = try ir.ErrorMsg.create( .fail = try ErrorMsg.create(
bin_file.allocator, bin_file.allocator,
src, src,
"TODO implement generateSymbol for type '{}'", "TODO implement generateSymbol for type '{}'",
@ -147,10 +149,10 @@ pub fn generateSymbol(
const Function = struct { const Function = struct {
bin_file: *link.ElfFile, bin_file: *link.ElfFile,
target: *const std.Target, target: *const std.Target,
mod_fn: *const ir.Module.Fn, mod_fn: *const Module.Fn,
code: *std.ArrayList(u8), code: *std.ArrayList(u8),
inst_table: std.AutoHashMap(*ir.Inst, MCValue), inst_table: std.AutoHashMap(*ir.Inst, MCValue),
err_msg: ?*ir.ErrorMsg, err_msg: ?*ErrorMsg,
const MCValue = union(enum) { const MCValue = union(enum) {
none, none,
@ -570,7 +572,7 @@ const Function = struct {
fn fail(self: *Function, src: usize, comptime format: []const u8, args: var) error{ CodegenFail, OutOfMemory } { fn fail(self: *Function, src: usize, comptime format: []const u8, args: var) error{ CodegenFail, OutOfMemory } {
@setCold(true); @setCold(true);
assert(self.err_msg == null); 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; 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 assert = std.debug.assert;
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const ir = @import("ir.zig"); const ir = @import("ir.zig");
const Module = @import("Module.zig");
const fs = std.fs; const fs = std.fs;
const elf = std.elf; const elf = std.elf;
const codegen = @import("codegen.zig"); const codegen = @import("codegen.zig");
@ -45,8 +46,8 @@ pub fn writeFilePath(
allocator: *Allocator, allocator: *Allocator,
dir: fs.Dir, dir: fs.Dir,
sub_path: []const u8, sub_path: []const u8,
module: ir.Module, module: Module,
errors: *std.ArrayList(ir.ErrorMsg), errors: *std.ArrayList(Module.ErrorMsg),
) !void { ) !void {
const options: Options = .{ const options: Options = .{
.target = module.target, .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; if (decl.link.local_sym_index != 0) return;
try self.local_symbols.ensureCapacity(self.allocator, self.local_symbols.items.len + 1); 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); var code_buffer = std.ArrayList(u8).init(self.allocator);
defer code_buffer.deinit(); defer code_buffer.deinit();
@ -878,16 +879,16 @@ pub const ElfFile = struct {
try self.file.pwriteAll(code, file_offset); try self.file.pwriteAll(code, file_offset);
// Since we updated the vaddr and the size, each corresponding export symbol also needs to be updated. // 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); return self.updateDeclExports(module, decl, decl_exports);
} }
/// Must be called only after a successful call to `updateDecl`. /// Must be called only after a successful call to `updateDecl`.
pub fn updateDeclExports( pub fn updateDeclExports(
self: *ElfFile, self: *ElfFile,
module: *ir.Module, module: *Module,
decl: *const ir.Module.Decl, decl: *const Module.Decl,
exports: []const *ir.Module.Export, exports: []const *Module.Export,
) !void { ) !void {
try self.global_symbols.ensureCapacity(self.allocator, self.global_symbols.items.len + exports.len); try self.global_symbols.ensureCapacity(self.allocator, self.global_symbols.items.len + exports.len);
const typed_value = decl.typed_value.most_recent.typed_value; 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); try module.failed_exports.ensureCapacity(module.failed_exports.size + 1);
module.failed_exports.putAssumeCapacityNoClobber( module.failed_exports.putAssumeCapacityNoClobber(
exp, exp,
try ir.ErrorMsg.create(self.allocator, 0, "Unimplemented: ExportOptions.section", .{}), try Module.ErrorMsg.create(self.allocator, 0, "Unimplemented: ExportOptions.section", .{}),
); );
continue; continue;
} }
@ -918,7 +919,7 @@ pub const ElfFile = struct {
try module.failed_exports.ensureCapacity(module.failed_exports.size + 1); try module.failed_exports.ensureCapacity(module.failed_exports.size + 1);
module.failed_exports.putAssumeCapacityNoClobber( module.failed_exports.putAssumeCapacityNoClobber(
exp, exp,
try ir.ErrorMsg.create(self.allocator, 0, "Unimplemented: GlobalLinkage.LinkOnce", .{}), try Module.ErrorMsg.create(self.allocator, 0, "Unimplemented: GlobalLinkage.LinkOnce", .{}),
); );
continue; continue;
}, },

View File

@ -6,9 +6,10 @@ const process = std.process;
const Allocator = mem.Allocator; const Allocator = mem.Allocator;
const ArrayList = std.ArrayList; const ArrayList = std.ArrayList;
const ast = std.zig.ast; const ast = std.zig.ast;
const ir = @import("ir.zig"); const Module = @import("Module.zig");
const link = @import("link.zig"); const link = @import("link.zig");
const Package = @import("Package.zig"); const Package = @import("Package.zig");
const zir = @import("zir.zig");
const LibCInstallation = @import("libc_installation.zig").LibCInstallation; const LibCInstallation = @import("libc_installation.zig").LibCInstallation;
@ -438,7 +439,7 @@ fn buildOutputType(
const root_pkg = try Package.create(gpa, fs.cwd(), ".", src_path); const root_pkg = try Package.create(gpa, fs.cwd(), ".", src_path);
errdefer root_pkg.destroy(); 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); errdefer gpa.destroy(root_scope);
root_scope.* = .{ root_scope.* = .{
.sub_file_path = root_pkg.root_src_path, .sub_file_path = root_pkg.root_src_path,
@ -447,19 +448,19 @@ fn buildOutputType(
.status = .never_loaded, .status = .never_loaded,
}; };
break :blk ir.Module{ break :blk Module{
.allocator = gpa, .allocator = gpa,
.root_pkg = root_pkg, .root_pkg = root_pkg,
.root_scope = root_scope, .root_scope = root_scope,
.bin_file = &bin_file, .bin_file = &bin_file,
.optimize_mode = .Debug, .optimize_mode = .Debug,
.decl_table = std.AutoHashMap(ir.Module.Decl.Hash, *ir.Module.Decl).init(gpa), .decl_table = std.AutoHashMap(Module.Decl.Hash, *Module.Decl).init(gpa),
.decl_exports = std.AutoHashMap(*ir.Module.Decl, []*ir.Module.Export).init(gpa), .decl_exports = std.AutoHashMap(*Module.Decl, []*Module.Export).init(gpa),
.export_owners = std.AutoHashMap(*ir.Module.Decl, []*ir.Module.Export).init(gpa), .export_owners = std.AutoHashMap(*Module.Decl, []*Module.Export).init(gpa),
.failed_decls = std.AutoHashMap(*ir.Module.Decl, *ir.ErrorMsg).init(gpa), .failed_decls = std.AutoHashMap(*Module.Decl, *Module.ErrorMsg).init(gpa),
.failed_files = std.AutoHashMap(*ir.Module.Scope.ZIRModule, *ir.ErrorMsg).init(gpa), .failed_files = std.AutoHashMap(*Module.Scope.ZIRModule, *Module.ErrorMsg).init(gpa),
.failed_exports = std.AutoHashMap(*ir.Module.Export, *ir.ErrorMsg).init(gpa), .failed_exports = std.AutoHashMap(*Module.Export, *Module.ErrorMsg).init(gpa),
.work_queue = std.fifo.LinearFifo(ir.Module.WorkItem, .Dynamic).init(gpa), .work_queue = std.fifo.LinearFifo(Module.WorkItem, .Dynamic).init(gpa),
}; };
}; };
defer module.deinit(); 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(); try module.update();
var errors = try module.getAllErrorsAlloc(); 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| { 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); defer new_zir_module.deinit(gpa);
const baf = try io.BufferedAtomicFile.create(gpa, fs.cwd(), zop, .{}); 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 BigIntMutable = std.math.big.int.Mutable;
const Target = std.Target; const Target = std.Target;
const Allocator = std.mem.Allocator; 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, /// This is the raw data, with no bookkeeping, no memory awareness,
/// no de-duplication, and no type system awareness. /// no de-duplication, and no type system awareness.
@ -904,7 +904,7 @@ pub const Value = extern union {
pub const Function = struct { pub const Function = struct {
base: Payload = Payload{ .tag = .function }, base: Payload = Payload{ .tag = .function },
func: *ir.Module.Fn, func: *Module.Fn,
}; };
pub const ArraySentinel0_u8_Type = struct { 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. /// Represents a pointer to a decl, not the value of the decl.
pub const DeclRef = struct { pub const DeclRef = struct {
base: Payload = Payload{ .tag = .decl_ref }, base: Payload = Payload{ .tag = .decl_ref },
decl: *ir.Module.Decl, decl: *Module.Decl,
}; };
pub const ElemPtr = struct { pub const ElemPtr = struct {

View File

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