Compare commits

...

2 Commits

Author SHA1 Message Date
Luna a95d0e7fd8 comp_ctx: make resolveVarType's return type be nullable
- add a parameter to prevent unwanted side-effects of resolveVarType
 - add basics of resolving scope variables
2019-09-28 21:30:52 -03:00
Luna 182d831408 comp_ctx: use more pointers to variable metadata 2019-09-28 20:52:45 -03:00
3 changed files with 75 additions and 34 deletions

View File

@ -279,9 +279,9 @@ pub const TypeSolver = struct {
.Variable => |vari| {
self.setErrToken(vari);
var metadata = try ctx.resolveVarType(vari.lexeme);
try ctx.insertMetadata(expr, metadata);
return metadata.typ;
var metadata = try ctx.resolveVarType(vari.lexeme, true);
try ctx.insertMetadata(expr, metadata.?);
return metadata.?.typ;
},
.Get => |get| {

View File

@ -100,10 +100,7 @@ pub const Codegen = struct {
builder: var,
expr: *const ast.Expr,
) anyerror!llvm.LLVMValueRef {
// TODO if expr is Variable, we should do a variable lookup
// in a symbol table, going up in scope, etc.
// TODO Assign modify symbol table
// TODO Assign modifies the symbol table
return switch (expr.*) {
// TODO handle all literals, construct llvm values for them
@ -242,22 +239,27 @@ pub const Codegen = struct {
var metadata = kv_opt.?.value;
var buf = try self.allocator.alloc(u8, 512);
errdefer self.allocator.free(buf);
var load_str = try std.fmt.bufPrint(buf, "{}_loaded", vari.lexeme);
var load_cstr = try std.cstr.addNullByte(self.allocator, load_str);
errdefer self.allocator.free(load_cstr);
std.debug.warn("!! LOAD FROM VAR META {}\n", @ptrToInt(metadata));
return switch (metadata.using) {
.Function => blk: {
var param = metadata.from_function.?.parameters.get(vari.lexeme).?.value;
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"),
.Scope => blk: {
var llvm_alloca = metadata.llvm_alloca.?;
//var var_typ = metadata.from_scope.?.env.get(vari.lexeme).?.value;
break :blk llvm.LLVMBuildLoad(builder, llvm_alloca, load_cstr.ptr);
},
};
},
@ -367,7 +369,7 @@ pub const Codegen = struct {
// analyze pass and the current scope contains the variable's
// type(hopefully), so we resolve it
const name = vardecl.name.lexeme;
var var_metadata = try self.ctx.resolveVarType(name);
var var_metadata = (try self.ctx.resolveVarType(name, false)).?;
var name_cstr = try std.cstr.addNullByte(self.allocator, name);
errdefer self.allocator.free(name_cstr);
@ -382,6 +384,10 @@ pub const Codegen = struct {
stmt.*.VarDecl.llvm_alloca = variable;
var_metadata.*.llvm_alloca = variable;
std.debug.warn("!! DECL VAR {} => {}\n", @ptrToInt(var_metadata), variable);
var llvm_expr = try self.emitExpr(builder, vardecl.value);
_ = llvm.LLVMBuildStore(builder, llvm_expr, variable);
},

View File

@ -146,12 +146,26 @@ pub const VariableMetadata = struct {
from_scope: ?*Scope = null,
from_function: ?*FunctionSymbol = null,
pub fn withScope(scope: *Scope, typ: SymbolUnderlyingType) VariableMetadata {
return VariableMetadata{ .typ = typ, .from_scope = scope, .using = .Scope };
pub fn withScope(
allocator: *std.mem.Allocator,
scope: *Scope,
typ: SymbolUnderlyingType,
) !*VariableMetadata {
var meta = try allocator.create(VariableMetadata);
std.debug.warn("VARMETA create from scope={}, meta={}\n", @ptrToInt(scope), @ptrToInt(meta));
meta.* = VariableMetadata{ .typ = typ, .from_scope = scope, .using = .Scope };
return meta;
}
pub fn withParam(func: *FunctionSymbol, typ: SymbolUnderlyingType) VariableMetadata {
return VariableMetadata{ .typ = typ, .from_function = func, .using = .Function };
pub fn withParam(
allocator: *std.mem.Allocator,
func: *FunctionSymbol,
typ: SymbolUnderlyingType,
) !*VariableMetadata {
var meta = try allocator.create(VariableMetadata);
std.debug.warn("VARMETA create from fndecl={}, meta={}\n", @ptrToInt(func), @ptrToInt(meta));
meta.* = VariableMetadata{ .typ = typ, .from_function = func, .using = .Function };
return meta;
}
};
@ -350,41 +364,62 @@ pub const CompilationContext = struct {
self: *@This(),
scope_opt: ?*Scope,
name: []const u8,
) ?VariableMetadata {
create_meta: bool,
) error{OutOfMemory}!?*VariableMetadata {
if (scope_opt == null) return null;
var scope = scope_opt.?;
var kv_opt = scope.env.get(name);
if (kv_opt) |kv| {
return VariableMetadata.withScope(scope, kv.value);
if (create_meta) {
return try VariableMetadata.withScope(
self.allocator,
scope,
kv.value,
);
} else {
return null;
}
} else {
return self.resolveVarTypeInScope(scope.parent, name);
return self.resolveVarTypeInScope(scope.parent, name, create_meta);
}
}
/// Resolve a given name's type, assuming it is a variable.
pub fn resolveVarType(self: *@This(), name: []const u8) !VariableMetadata {
var var_type: ?VariableMetadata = null;
pub fn resolveVarType(
self: *@This(),
name: []const u8,
create_meta: bool,
) !?*VariableMetadata {
var var_type: ?*VariableMetadata = null;
if (self.current_scope) |scope| {
var_type = self.resolveVarTypeInScope(scope, name);
var_type = try self.resolveVarTypeInScope(scope, name, create_meta);
if (var_type) |typ| return typ;
}
if (self.cur_function) |cur_function| {
var kv_opt = cur_function.parameters.get(name);
if (kv_opt) |kv| return VariableMetadata.withParam(cur_function, kv.value.typ);
if (kv_opt) |kv| {
if (create_meta) {
return try VariableMetadata.withParam(
self.allocator,
cur_function,
kv.value.typ,
);
} else {
return null;
}
}
}
std.debug.warn("Unknown name {}\n", name);
return CompilationError.UnknownName;
}
pub fn insertMetadata(self: *@This(), ptr: *const ast.Expr, metadata: VariableMetadata) !void {
var meta = try self.allocator.create(VariableMetadata);
meta.* = metadata;
pub fn insertMetadata(self: *@This(), ptr: *const ast.Expr, metadata: *VariableMetadata) !void {
std.debug.assert(ast.ExprType(ptr.*) == .Variable);
_ = try self.metadata_map.put(ptr, meta);
std.debug.warn("COMP CTX inserting metadata, expr={}, meta={}\n", @ptrToInt(ptr), @ptrToInt(metadata));
_ = try self.metadata_map.put(ptr, metadata);
}
};