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(
self: *Codegen,
mod: llvm.LLVMModuleRef,
node: *const ast.Node,
node: *ast.Node,
) !void {
switch (node.*) {
.Root => @panic("Should not have gotten Root"),
@ -430,6 +430,20 @@ pub const Codegen = struct {
var builder = llvm.LLVMCreateBuilder();
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);
defer self.ctx.dumpScope();
@ -480,9 +494,9 @@ pub const Codegen = struct {
var mod = llvm.LLVMModuleCreateWithName(c"awoo").?;
defer llvm.LLVMDisposeModule(mod);
for (root.Root.toSlice()) |child| {
std.debug.warn("cgen: gen {}\n", @tagName(child));
try self.genNode(mod, &child);
var root_slice = root.Root.toSlice();
for (root_slice) |_, idx| {
try self.genNode(mod, &root_slice[idx]);
}
var err: ?[*]u8 = null;

View File

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