| ... | @@ -35,14 +35,13 @@ pub fn openBinFilePath( | ... | @@ -35,14 +35,13 @@ pub fn openBinFilePath( |
| 35 | sub_path: []const u8, | 35 | sub_path: []const u8, |
| 36 | options: Options, | 36 | options: Options, |
| 37 | ) !*File { | 37 | ) !*File { |
| 38 | const file = try dir.createFile(sub_path, .{ .truncate = false, .read = true, .mode = determineMode(options) }); | 38 | const file = try dir.createFile(sub_path, .{ .truncate = options.cbe, .read = true, .mode = determineMode(options) }); |
| 39 | errdefer file.close(); | 39 | errdefer file.close(); |
| 40 | | 40 | |
| 41 | if (options.cbe) { | 41 | if (options.cbe) { |
| 42 | var bin_file = try allocator.create(File.C); | 42 | var bin_file = try allocator.create(File.C); |
| 43 | errdefer allocator.destroy(bin_file); | 43 | errdefer allocator.destroy(bin_file); |
| 44 | bin_file.* = try openCFile(allocator, file, options); | 44 | bin_file.* = try openCFile(allocator, file, options); |
| 45 | bin_file.owns_file_handle = true; | | |
| 46 | return &bin_file.base; | 45 | return &bin_file.base; |
| 47 | } else { | 46 | } else { |
| 48 | var bin_file = try allocator.create(File.Elf); | 47 | var bin_file = try allocator.create(File.Elf); |
| ... | @@ -88,17 +87,14 @@ pub fn writeFilePath( | ... | @@ -88,17 +87,14 @@ pub fn writeFilePath( |
| 88 | } | 87 | } |
| 89 | | 88 | |
| 90 | pub fn openCFile(allocator: *Allocator, file: fs.File, options: Options) !File.C { | 89 | pub fn openCFile(allocator: *Allocator, file: fs.File, options: Options) !File.C { |
| 91 | var self: File.C = .{ | 90 | return File.C{ |
| 92 | .allocator = allocator, | 91 | .allocator = allocator, |
| 93 | .file = file, | 92 | .file = file, |
| 94 | .options = options, | 93 | .options = options, |
| 95 | .owns_file_handle = false, | | |
| 96 | .main = std.ArrayList(u8).init(allocator), | 94 | .main = std.ArrayList(u8).init(allocator), |
| 97 | .header = std.ArrayList(u8).init(allocator), | 95 | .header = std.ArrayList(u8).init(allocator), |
| 98 | .called = std.StringHashMap(void).init(allocator), | 96 | .called = std.StringHashMap(void).init(allocator), |
| 99 | }; | 97 | }; |
| 100 | errdefer self.deinit(); | | |
| 101 | return self; | | |
| 102 | } | 98 | } |
| 103 | | 99 | |
| 104 | /// Attempts incremental linking, if the file already exists. | 100 | /// Attempts incremental linking, if the file already exists. |
| ... | @@ -127,7 +123,7 @@ pub const File = struct { | ... | @@ -127,7 +123,7 @@ pub const File = struct { |
| 127 | pub fn makeWritable(base: *File, dir: fs.Dir, sub_path: []const u8) !void { | 123 | pub fn makeWritable(base: *File, dir: fs.Dir, sub_path: []const u8) !void { |
| 128 | switch (base.tag) { | 124 | switch (base.tag) { |
| 129 | .Elf => return @fieldParentPtr(Elf, "base", base).makeWritable(dir, sub_path), | 125 | .Elf => return @fieldParentPtr(Elf, "base", base).makeWritable(dir, sub_path), |
| 130 | .C => return @fieldParentPtr(C, "base", base).makeWritable(dir, sub_path), | 126 | .C => {}, |
| 131 | else => unreachable, | 127 | else => unreachable, |
| 132 | } | 128 | } |
| 133 | } | 129 | } |
| ... | @@ -210,31 +206,18 @@ pub const File = struct { | ... | @@ -210,31 +206,18 @@ pub const File = struct { |
| 210 | header: std.ArrayList(u8), | 206 | header: std.ArrayList(u8), |
| 211 | main: std.ArrayList(u8), | 207 | main: std.ArrayList(u8), |
| 212 | file: ?fs.File, | 208 | file: ?fs.File, |
| 213 | owns_file_handle: bool, | | |
| 214 | options: Options, | 209 | options: Options, |
| 215 | called: std.StringHashMap(void), | 210 | called: std.StringHashMap(void), |
| 216 | need_stddef: bool = false, | 211 | need_stddef: bool = false, |
| 217 | need_stdint: bool = false, | 212 | need_stdint: bool = false, |
| 218 | need_noreturn: bool = false, | 213 | need_noreturn: bool = false, |
| 219 | | 214 | |
| 220 | pub fn makeWritable(self: *File.C, dir: fs.Dir, sub_path: []const u8) !void { | | |
| 221 | assert(self.owns_file_handle); | | |
| 222 | if (self.file != null) return; | | |
| 223 | self.file = try dir.createFile(sub_path, .{ | | |
| 224 | .truncate = false, | | |
| 225 | .read = true, | | |
| 226 | .mode = determineMode(self.options), | | |
| 227 | }); | | |
| 228 | } | | |
| 229 | | | |
| 230 | pub fn deinit(self: *File.C) void { | 215 | pub fn deinit(self: *File.C) void { |
| 231 | self.main.deinit(); | 216 | self.main.deinit(); |
| 232 | self.header.deinit(); | 217 | self.header.deinit(); |
| 233 | self.called.deinit(); | 218 | self.called.deinit(); |
| 234 | if (self.owns_file_handle) { | 219 | if (self.file) |f| |
| 235 | if (self.file) |f| | 220 | f.close(); |
| 236 | f.close(); | | |
| 237 | } | | |
| 238 | } | 221 | } |
| 239 | | 222 | |
| 240 | pub fn updateDecl(self: *File.C, module: *Module, decl: *Module.Decl) !void { | 223 | pub fn updateDecl(self: *File.C, module: *Module, decl: *Module.Decl) !void { |