add basics of stack-stored arguments

this requires some changes regarding the const-ability of the passed
nodes into codegen.
This commit is contained in:
Luna 2019-09-28 11:40:56 -03:00
parent 58713b20e3
commit 61c8493484
2 changed files with 20 additions and 4 deletions

View file

@ -389,7 +389,7 @@ pub const Codegen = struct {
fn genNode( fn genNode(
self: *Codegen, self: *Codegen,
mod: llvm.LLVMModuleRef, mod: llvm.LLVMModuleRef,
node: *const ast.Node, node: *ast.Node,
) !void { ) !void {
switch (node.*) { switch (node.*) {
.Root => @panic("Should not have gotten Root"), .Root => @panic("Should not have gotten Root"),
@ -430,6 +430,20 @@ pub const Codegen = struct {
var builder = llvm.LLVMCreateBuilder(); var builder = llvm.LLVMCreateBuilder();
llvm.LLVMPositionBuilderAtEnd(builder, entry); llvm.LLVMPositionBuilderAtEnd(builder, entry);
// to have the ability to mutate parameters, we must allocate them on
// the stack
for (params_slice) |param_node, idx| {
var param = fn_sym.parameters.get(param_node.name.lexeme).?.value;
const name_cstr = try std.cstr.addNullByte(self.allocator, param_node.name.lexeme);
errdefer self.allocator.free(name_cstr);
var alloca = llvm.LLVMBuildAlloca(builder, try self.typeToLLVM(param.typ), name_cstr.ptr);
param.llvm_alloca = alloca;
// TODO store register into stack param
}
self.ctx.setScope(fn_sym.scope); self.ctx.setScope(fn_sym.scope);
defer self.ctx.dumpScope(); defer self.ctx.dumpScope();
@ -480,9 +494,9 @@ pub const Codegen = struct {
var mod = llvm.LLVMModuleCreateWithName(c"awoo").?; var mod = llvm.LLVMModuleCreateWithName(c"awoo").?;
defer llvm.LLVMDisposeModule(mod); defer llvm.LLVMDisposeModule(mod);
for (root.Root.toSlice()) |child| { var root_slice = root.Root.toSlice();
std.debug.warn("cgen: gen {}\n", @tagName(child)); for (root_slice) |_, idx| {
try self.genNode(mod, &child); try self.genNode(mod, &root_slice[idx]);
} }
var err: ?[*]u8 = null; var err: ?[*]u8 = null;

View file

@ -79,6 +79,7 @@ pub const Scope = struct {
pub const Parameter = struct { pub const Parameter = struct {
idx: usize, idx: usize,
name: []const u8,
typ: SymbolUnderlyingType, typ: SymbolUnderlyingType,
alloca: ?llvm.LLVMValueRef = null, alloca: ?llvm.LLVMValueRef = null,
}; };
@ -271,6 +272,7 @@ pub const CompilationContext = struct {
for (decl.params.toSlice()) |param, idx| { for (decl.params.toSlice()) |param, idx| {
_ = try param_map.put(param.name.lexeme, Parameter{ _ = try param_map.put(param.name.lexeme, Parameter{
.name = param.name.lexeme,
.idx = idx, .idx = idx,
.typ = param_types.at(idx), .typ = param_types.at(idx),
}); });