Skip to content

Commit

Permalink
stage2: Add code generation for Load instruction in LLVM backend
Browse files Browse the repository at this point in the history
The following now works:
```
export fn _start() noreturn {
    var x: bool = true;
    var y: bool = x;
    exit();
}

fn exit() noreturn {
    unreachable;
}
```
  • Loading branch information
FireFox317 committed Jan 3, 2021
1 parent 19cfd31 commit 47a4d43
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/llvm_backend.zig
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ pub const LLVMIRModule = struct {
.arg => try self.genArg(inst.castTag(.arg).?),
.alloc => try self.genAlloc(inst.castTag(.alloc).?),
.store => try self.genStore(inst.castTag(.store).?),
.load => try self.genLoad(inst.castTag(.load).?),
.dbg_stmt => blk: {
// TODO: implement debug info
break :blk null;
Expand Down Expand Up @@ -396,6 +397,11 @@ pub const LLVMIRModule = struct {
return null;
}

fn genLoad(self: *LLVMIRModule, inst: *Inst.UnOp) !?*const llvm.ValueRef {
const ptr_val = try self.resolveInst(inst.operand);
return self.builder.buildLoad(ptr_val, "");
}

fn genBreakpoint(self: *LLVMIRModule, inst: *Inst.NoOp) !?*const llvm.ValueRef {
// TODO: Store this function somewhere such that we dont have to add it again
const fn_type = llvm.TypeRef.functionType(llvm.voidType(), null, 0, false);
Expand Down
3 changes: 3 additions & 0 deletions src/llvm_bindings.zig
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ pub const BuilderRef = opaque {

pub const buildStore = LLVMBuildStore;
extern fn LLVMBuildStore(*const BuilderRef, Val: *const ValueRef, Ptr: *const ValueRef) *const ValueRef;

pub const buildLoad = LLVMBuildLoad;
extern fn LLVMBuildLoad(*const BuilderRef, PointerVal: *const ValueRef, Name: [*:0]const u8) *const ValueRef;
};

pub const BasicBlockRef = opaque {
Expand Down

0 comments on commit 47a4d43

Please sign in to comment.