| ... | @@ -1149,61 +1149,38 @@ pub const Object = struct { | ... | @@ -1149,61 +1149,38 @@ pub const Object = struct { |
| 1149 | }; | 1149 | }; |
| 1150 | | 1150 | |
| 1151 | pub fn emit(self: *Object, options: EmitOptions) !void { | 1151 | pub fn emit(self: *Object, options: EmitOptions) !void { |
| 1152 | try self.resolveExportExternCollisions(); | 1152 | { |
| 1153 | try self.genErrorNameTable(); | 1153 | try self.resolveExportExternCollisions(); |
| 1154 | try self.genCmpLtErrorsLenFunction(); | 1154 | try self.genErrorNameTable(); |
| 1155 | try self.genModuleLevelAssembly(); | 1155 | try self.genCmpLtErrorsLenFunction(); |
| | 1156 | try self.genModuleLevelAssembly(); |
| 1156 | | 1157 | |
| 1157 | if (!self.builder.strip) { | 1158 | if (!self.builder.strip) { |
| 1158 | { | 1159 | { |
| 1159 | var i: usize = 0; | 1160 | var i: usize = 0; |
| 1160 | while (i < self.debug_unresolved_namespace_scopes.count()) : (i += 1) { | 1161 | while (i < self.debug_unresolved_namespace_scopes.count()) : (i += 1) { |
| 1161 | const namespace_index = self.debug_unresolved_namespace_scopes.keys()[i]; | 1162 | const namespace_index = self.debug_unresolved_namespace_scopes.keys()[i]; |
| 1162 | const fwd_ref = self.debug_unresolved_namespace_scopes.values()[i]; | 1163 | const fwd_ref = self.debug_unresolved_namespace_scopes.values()[i]; |
| 1163 | | 1164 | |
| 1164 | const namespace = self.module.namespacePtr(namespace_index); | 1165 | const namespace = self.module.namespacePtr(namespace_index); |
| 1165 | const debug_type = try self.lowerDebugType(namespace.ty); | 1166 | const debug_type = try self.lowerDebugType(namespace.ty); |
| 1166 | | 1167 | |
| 1167 | self.builder.debugForwardReferenceSetType(fwd_ref, debug_type); | 1168 | self.builder.debugForwardReferenceSetType(fwd_ref, debug_type); |
| | 1169 | } |
| 1168 | } | 1170 | } |
| 1169 | } | | |
| 1170 | | 1171 | |
| 1171 | self.builder.debugForwardReferenceSetType( | 1172 | self.builder.debugForwardReferenceSetType( |
| 1172 | self.debug_enums_fwd_ref, | 1173 | self.debug_enums_fwd_ref, |
| 1173 | try self.builder.debugTuple(self.debug_enums.items), | 1174 | try self.builder.debugTuple(self.debug_enums.items), |
| 1174 | ); | 1175 | ); |
| 1175 | | | |
| 1176 | self.builder.debugForwardReferenceSetType( | | |
| 1177 | self.debug_globals_fwd_ref, | | |
| 1178 | try self.builder.debugTuple(self.debug_globals.items), | | |
| 1179 | ); | | |
| 1180 | } | | |
| 1181 | | 1176 | |
| 1182 | if (options.pre_ir_path) |path| { | 1177 | self.builder.debugForwardReferenceSetType( |
| 1183 | if (std.mem.eql(u8, path, "-")) { | 1178 | self.debug_globals_fwd_ref, |
| 1184 | self.builder.dump(); | 1179 | try self.builder.debugTuple(self.debug_globals.items), |
| 1185 | } else { | 1180 | ); |
| 1186 | _ = try self.builder.printToFile(path); | | |
| 1187 | } | 1181 | } |
| 1188 | } | 1182 | } |
| 1189 | | 1183 | |
| 1190 | var bitcode_arena_allocator = std.heap.ArenaAllocator.init( | | |
| 1191 | std.heap.page_allocator, | | |
| 1192 | ); | | |
| 1193 | errdefer bitcode_arena_allocator.deinit(); | | |
| 1194 | | | |
| 1195 | const bitcode = try self.builder.toBitcode( | | |
| 1196 | bitcode_arena_allocator.allocator(), | | |
| 1197 | ); | | |
| 1198 | | | |
| 1199 | if (options.pre_bc_path) |path| { | | |
| 1200 | var file = try std.fs.cwd().createFile(path, .{}); | | |
| 1201 | defer file.close(); | | |
| 1202 | | | |
| 1203 | const ptr: [*]const u8 = @ptrCast(bitcode.ptr); | | |
| 1204 | try file.writeAll(ptr[0..(bitcode.len * 4)]); | | |
| 1205 | } | | |
| 1206 | | | |
| 1207 | const emit_asm_msg = options.asm_path orelse "(none)"; | 1184 | const emit_asm_msg = options.asm_path orelse "(none)"; |
| 1208 | const emit_bin_msg = options.bin_path orelse "(none)"; | 1185 | const emit_bin_msg = options.bin_path orelse "(none)"; |
| 1209 | const post_llvm_ir_msg = options.post_ir_path orelse "(none)"; | 1186 | const post_llvm_ir_msg = options.post_ir_path orelse "(none)"; |
| ... | @@ -1212,28 +1189,47 @@ pub const Object = struct { | ... | @@ -1212,28 +1189,47 @@ pub const Object = struct { |
| 1212 | emit_asm_msg, emit_bin_msg, post_llvm_ir_msg, post_llvm_bc_msg, | 1189 | emit_asm_msg, emit_bin_msg, post_llvm_ir_msg, post_llvm_bc_msg, |
| 1213 | }); | 1190 | }); |
| 1214 | | 1191 | |
| 1215 | if (options.asm_path == null and options.bin_path == null and | 1192 | const context, const module = emit: { |
| 1216 | options.post_ir_path == null and options.post_bc_path == null) return; | 1193 | if (options.pre_ir_path) |path| { |
| | 1194 | if (std.mem.eql(u8, path, "-")) { |
| | 1195 | self.builder.dump(); |
| | 1196 | } else { |
| | 1197 | _ = try self.builder.printToFile(path); |
| | 1198 | } |
| | 1199 | } |
| 1217 | | 1200 | |
| 1218 | if (options.post_bc_path) |path| { | 1201 | const bitcode = try self.builder.toBitcode(self.gpa); |
| 1219 | var file = try std.fs.cwd().createFileZ(path, .{}); | 1202 | defer self.gpa.free(bitcode); |
| 1220 | defer file.close(); | | |
| 1221 | | 1203 | |
| 1222 | const ptr: [*]const u8 = @ptrCast(bitcode.ptr); | 1204 | if (options.pre_bc_path) |path| { |
| 1223 | try file.writeAll(ptr[0..(bitcode.len * 4)]); | 1205 | var file = try std.fs.cwd().createFile(path, .{}); |
| 1224 | } | 1206 | defer file.close(); |
| 1225 | | 1207 | |
| 1226 | if (!build_options.have_llvm or !self.module.comp.config.use_lib_llvm) { | 1208 | const ptr: [*]const u8 = @ptrCast(bitcode.ptr); |
| 1227 | log.err("emitting without libllvm not implemented", .{}); | 1209 | try file.writeAll(ptr[0..(bitcode.len * 4)]); |
| 1228 | return error.FailedToEmit; | 1210 | } |
| 1229 | } | | |
| 1230 | | 1211 | |
| 1231 | initializeLLVMTarget(self.module.comp.root_mod.resolved_target.result.cpu.arch); | 1212 | if (options.asm_path == null and options.bin_path == null and |
| | 1213 | options.post_ir_path == null and options.post_bc_path == null) return; |
| 1232 | | 1214 | |
| 1233 | const context: *llvm.Context = llvm.Context.create(); | 1215 | if (options.post_bc_path) |path| { |
| 1234 | defer context.dispose(); | 1216 | var file = try std.fs.cwd().createFileZ(path, .{}); |
| | 1217 | defer file.close(); |
| | 1218 | |
| | 1219 | const ptr: [*]const u8 = @ptrCast(bitcode.ptr); |
| | 1220 | try file.writeAll(ptr[0..(bitcode.len * 4)]); |
| | 1221 | } |
| | 1222 | |
| | 1223 | if (!build_options.have_llvm or !self.module.comp.config.use_lib_llvm) { |
| | 1224 | log.err("emitting without libllvm not implemented", .{}); |
| | 1225 | return error.FailedToEmit; |
| | 1226 | } |
| | 1227 | |
| | 1228 | initializeLLVMTarget(self.module.comp.root_mod.resolved_target.result.cpu.arch); |
| | 1229 | |
| | 1230 | const context: *llvm.Context = llvm.Context.create(); |
| | 1231 | errdefer context.dispose(); |
| 1235 | | 1232 | |
| 1236 | const module = blk: { | | |
| 1237 | const bitcode_memory_buffer = llvm.MemoryBuffer.createMemoryBufferWithMemoryRange( | 1233 | const bitcode_memory_buffer = llvm.MemoryBuffer.createMemoryBufferWithMemoryRange( |
| 1238 | @ptrCast(bitcode.ptr), | 1234 | @ptrCast(bitcode.ptr), |
| 1239 | bitcode.len * 4, | 1235 | bitcode.len * 4, |
| ... | @@ -1247,10 +1243,9 @@ pub const Object = struct { | ... | @@ -1247,10 +1243,9 @@ pub const Object = struct { |
| 1247 | std.debug.print("Failed to parse bitcode\n", .{}); | 1243 | std.debug.print("Failed to parse bitcode\n", .{}); |
| 1248 | return error.FailedToEmit; | 1244 | return error.FailedToEmit; |
| 1249 | } | 1245 | } |
| 1250 | | 1246 | break :emit .{ context, module }; |
| 1251 | break :blk module; | | |
| 1252 | }; | 1247 | }; |
| 1253 | bitcode_arena_allocator.deinit(); | 1248 | defer context.dispose(); |
| 1254 | | 1249 | |
| 1255 | const target_triple_sentinel = | 1250 | const target_triple_sentinel = |
| 1256 | try self.gpa.dupeZ(u8, self.builder.target_triple.slice(&self.builder).?); | 1251 | try self.gpa.dupeZ(u8, self.builder.target_triple.slice(&self.builder).?); |