codegen: emit load instruction for (now-stack) params

- comp_ctx: make ParameterMap use ptrs to Parameter
This commit is contained in:
Luna 2019-09-28 13:30:05 -03:00
parent 518d2daeb4
commit 21dcb3e65d
3 changed files with 15 additions and 7 deletions

BIN
hello Executable file

Binary file not shown.

View file

@ -236,11 +236,16 @@ pub const Codegen = struct {
return switch (metadata.using) {
.Function => blk: {
var param = metadata.from_function.?.parameters.get(vari.lexeme).?.value;
// var llvm_func = self.llvm_table.get(self.current_function_name.?).?.value;
// break :blk llvm.LLVMGetParam(llvm_func, @intCast(c_uint, param.idx));
std.debug.warn("fn param alloca {} {}\n", param.name, param.llvm_alloca);
break :blk param.llvm_alloca.?;
var buf = try self.allocator.alloc(u8, 512);
errdefer self.allocator.free(buf);
var load_str = try std.fmt.bufPrint(buf, "{}_loaded", param.name);
var load_cstr = try std.cstr.addNullByte(self.allocator, load_str);
errdefer self.allocator.free(load_cstr);
break :blk llvm.LLVMBuildLoad(builder, param.llvm_alloca.?, load_cstr.ptr);
},
.Scope => @panic("TODO local variables"),

View file

@ -85,7 +85,7 @@ pub const Parameter = struct {
llvm_alloca: llvm.LLVMValueRef = null,
};
pub const ParameterMap = std.StringHashMap(Parameter);
pub const ParameterMap = std.StringHashMap(*Parameter);
// functions, for our purposes, other than symbols, have:
// - a return type
@ -274,11 +274,14 @@ pub const CompilationContext = struct {
var param_map = ParameterMap.init(self.allocator);
for (decl.params.toSlice()) |param, idx| {
_ = try param_map.put(param.name.lexeme, Parameter{
var param_sym = try self.allocator.create(Parameter);
param_sym.* = Parameter{
.name = param.name.lexeme,
.idx = idx,
.typ = param_types.at(idx),
});
};
_ = try param_map.put(param.name.lexeme, param_sym);
}
const lex = decl.func_name.lexeme;