Skip to content

Commit

Permalink
rename Module to Compilation
Browse files Browse the repository at this point in the history
and CompilationUnit to ObjectFile
  • Loading branch information
andrewrk committed Jul 14, 2018
1 parent 69e50ad commit 28c3d48
Show file tree
Hide file tree
Showing 9 changed files with 387 additions and 389 deletions.
42 changes: 20 additions & 22 deletions src-self-hosted/codegen.zig
@@ -1,7 +1,5 @@
const std = @import("std");
// TODO codegen pretends that Module is renamed to Build because I plan to
// do that refactor at some point
const Build = @import("module.zig").Module;
const Compilation = @import("compilation.zig").Compilation;
// we go through llvm instead of c for 2 reasons:
// 1. to avoid accidentally calling the non-thread-safe functions
// 2. patch up some of the types to remove nullability
Expand All @@ -11,51 +9,51 @@ const Value = @import("value.zig").Value;
const Type = @import("type.zig").Type;
const event = std.event;

pub async fn renderToLlvm(build: *Build, fn_val: *Value.Fn, code: *ir.Code) !void {
pub async fn renderToLlvm(comp: *Compilation, fn_val: *Value.Fn, code: *ir.Code) !void {
fn_val.base.ref();
defer fn_val.base.deref(build);
defer code.destroy(build.a());
defer fn_val.base.deref(comp);
defer code.destroy(comp.a());

const llvm_handle = try build.event_loop_local.getAnyLlvmContext();
defer llvm_handle.release(build.event_loop_local);
const llvm_handle = try comp.event_loop_local.getAnyLlvmContext();
defer llvm_handle.release(comp.event_loop_local);

const context = llvm_handle.node.data;

const module = llvm.ModuleCreateWithNameInContext(build.name.ptr(), context) orelse return error.OutOfMemory;
const module = llvm.ModuleCreateWithNameInContext(comp.name.ptr(), context) orelse return error.OutOfMemory;
defer llvm.DisposeModule(module);

const builder = llvm.CreateBuilderInContext(context) orelse return error.OutOfMemory;
defer llvm.DisposeBuilder(builder);

var cunit = CompilationUnit{
.build = build,
var ofile = ObjectFile{
.comp = comp,
.module = module,
.builder = builder,
.context = context,
.lock = event.Lock.init(build.loop),
.lock = event.Lock.init(comp.loop),
};

try renderToLlvmModule(&cunit, fn_val, code);
try renderToLlvmModule(&ofile, fn_val, code);

if (build.verbose_llvm_ir) {
llvm.DumpModule(cunit.module);
if (comp.verbose_llvm_ir) {
llvm.DumpModule(ofile.module);
}
}

pub const CompilationUnit = struct {
build: *Build,
pub const ObjectFile = struct {
comp: *Compilation,
module: llvm.ModuleRef,
builder: llvm.BuilderRef,
context: llvm.ContextRef,
lock: event.Lock,

fn a(self: *CompilationUnit) *std.mem.Allocator {
return self.build.a();
fn a(self: *ObjectFile) *std.mem.Allocator {
return self.comp.a();
}
};

pub fn renderToLlvmModule(cunit: *CompilationUnit, fn_val: *Value.Fn, code: *ir.Code) !void {
pub fn renderToLlvmModule(ofile: *ObjectFile, fn_val: *Value.Fn, code: *ir.Code) !void {
// TODO audit more of codegen.cpp:fn_llvm_value and port more logic
const llvm_fn_type = try fn_val.base.typeof.getLlvmType(cunit);
const llvm_fn = llvm.AddFunction(cunit.module, fn_val.symbol_name.ptr(), llvm_fn_type);
const llvm_fn_type = try fn_val.base.typeof.getLlvmType(ofile);
const llvm_fn = llvm.AddFunction(ofile.module, fn_val.symbol_name.ptr(), llvm_fn_type);
}

0 comments on commit 28c3d48

Please sign in to comment.