Merge pull request #8 from birth-software/main-function

main function
This commit is contained in:
David 2023-10-02 13:25:51 -06:00 committed by GitHub
commit 7977bccf64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 2182 additions and 977 deletions

View File

@ -3,6 +3,7 @@ comptime {
} }
const _start = fn () noreturn { const _start = fn () noreturn {
_ = #syscall(231, 0); const result = #import("main").main();
_ = #syscall(231, result);
unreachable; unreachable;
}; };

View File

@ -64,35 +64,57 @@ pub const Struct = struct {
pub const List = BlockList(@This()); pub const List = BlockList(@This());
pub const Index = List.Index; pub const Index = List.Index;
pub const Allocation = List.Allocation;
}; };
pub const Type = union(enum) { pub const Type = union(enum) {
void, void,
noreturn, noreturn,
bool, bool,
integer: Integer, integer: Type.Integer,
@"struct": Struct.Index, @"struct": Struct.Index,
pub const List = BlockList(@This()); pub const List = BlockList(@This());
pub const Index = List.Index; pub const Index = List.Index;
}; pub const Allocation = List.Allocation;
pub const Integer = struct { pub const Integer = struct {
bit_count: u16, bit_count: u16,
signedness: Signedness, signedness: Signedness,
pub const Signedness = enum(u1) { pub const Signedness = enum(u1) {
unsigned = 0, unsigned = 0,
signed = 1, signed = 1,
};
pub fn getSize(integer: Type.Integer) u64 {
return integer.bit_count / @bitSizeOf(u8) + @intFromBool(integer.bit_count % @bitSizeOf(u8) != 0);
}
}; };
pub fn getSize(type_info: Type) u64 {
return switch (type_info) {
.integer => |integer| integer.getSize(),
else => |t| @panic(@tagName(t)),
};
}
pub fn getAlignment(type_info: Type) u64 {
return switch (type_info) {
.integer => |integer| @min(16, integer.getSize()),
else => |t| @panic(@tagName(t)),
};
}
}; };
/// A scope contains a bunch of declarations /// A scope contains a bunch of declarations
pub const Scope = struct { pub const Scope = struct {
parent: Scope.Index,
type: Type.Index = Type.Index.invalid,
declarations: AutoHashMap(u32, Declaration.Index) = .{}, declarations: AutoHashMap(u32, Declaration.Index) = .{},
parent: Scope.Index,
file: File.Index,
type: Type.Index = Type.Index.invalid,
pub const List = BlockList(@This()); pub const List = BlockList(@This());
pub const Index = List.Index; pub const Index = List.Index;
pub const Allocation = List.Allocation;
}; };
pub const ScopeType = enum(u1) { pub const ScopeType = enum(u1) {
@ -113,6 +135,7 @@ pub const Declaration = struct {
pub const List = BlockList(@This()); pub const List = BlockList(@This());
pub const Index = List.Index; pub const Index = List.Index;
pub const Allocation = List.Allocation;
}; };
pub const Function = struct { pub const Function = struct {
@ -133,6 +156,7 @@ pub const Function = struct {
pub const List = BlockList(@This()); pub const List = BlockList(@This());
pub const Index = List.Index; pub const Index = List.Index;
pub const Allocation = List.Allocation;
}; };
pub const Block = struct { pub const Block = struct {
@ -140,6 +164,7 @@ pub const Block = struct {
reaches_end: bool, reaches_end: bool,
pub const List = BlockList(@This()); pub const List = BlockList(@This());
pub const Index = List.Index; pub const Index = List.Index;
pub const Allocation = List.Allocation;
}; };
pub const Field = struct { pub const Field = struct {
@ -147,6 +172,7 @@ pub const Field = struct {
pub const List = BlockList(@This()); pub const List = BlockList(@This());
pub const Index = List.Index; pub const Index = List.Index;
pub const Allocation = List.Allocation;
}; };
pub const Loop = struct { pub const Loop = struct {
@ -156,6 +182,7 @@ pub const Loop = struct {
pub const List = BlockList(@This()); pub const List = BlockList(@This());
pub const Index = List.Index; pub const Index = List.Index;
pub const Allocation = List.Allocation;
}; };
const Runtime = struct { const Runtime = struct {
@ -172,6 +199,7 @@ pub const Assignment = struct {
pub const List = BlockList(@This()); pub const List = BlockList(@This());
pub const Index = List.Index; pub const Index = List.Index;
pub const Allocation = List.Allocation;
}; };
pub const Syscall = struct { pub const Syscall = struct {
@ -185,11 +213,36 @@ pub const Syscall = struct {
pub const List = BlockList(@This()); pub const List = BlockList(@This());
pub const Index = List.Index; pub const Index = List.Index;
pub const Allocation = List.Allocation;
};
pub const Call = struct {
value: Value.Index,
arguments: ArgumentList.Index,
type: Type.Index,
pub const List = BlockList(@This());
pub const Index = List.Index;
pub const Allocation = List.Allocation;
};
pub const ArgumentList = struct {
array: ArrayList(Value.Index),
pub const List = BlockList(@This());
pub const Index = List.Index;
pub const Allocation = List.Allocation;
};
pub const Return = struct {
value: Value.Index,
pub const List = BlockList(@This());
pub const Index = List.Index;
pub const Allocation = List.Allocation;
}; };
pub const Value = union(enum) { pub const Value = union(enum) {
unresolved: Unresolved, unresolved: Unresolved,
declaration: Declaration.Index, declaration: Declaration.Index,
declaration_reference: Declaration.Index,
void, void,
bool: bool, bool: bool,
undefined, undefined,
@ -200,11 +253,15 @@ pub const Value = union(enum) {
runtime: Runtime, runtime: Runtime,
assign: Assignment.Index, assign: Assignment.Index,
type: Type.Index, type: Type.Index,
integer: u64, integer: Integer,
syscall: Syscall.Index, syscall: Syscall.Index,
call: Call.Index,
argument_list: ArgumentList,
@"return": Return.Index,
pub const List = BlockList(@This()); pub const List = BlockList(@This());
pub const Index = List.Index; pub const Index = List.Index;
pub const Allocation = List.Allocation;
pub fn isComptime(value: Value) bool { pub fn isComptime(value: Value) bool {
return switch (value) { return switch (value) {
@ -213,14 +270,19 @@ pub const Value = union(enum) {
}; };
} }
pub fn getType(value: *Value) !void { pub fn getType(value: *Value, module: *Module) Type.Index {
switch (value.*) { return switch (value.*) {
.call => |call_index| module.calls.get(call_index).type,
else => |t| @panic(@tagName(t)), else => |t| @panic(@tagName(t)),
} };
unreachable;
} }
}; };
pub const Integer = struct {
value: u64,
type: Type.Integer,
};
pub const Module = struct { pub const Module = struct {
main_package: *Package, main_package: *Package,
import_table: StringArrayHashMap(*File) = .{}, import_table: StringArrayHashMap(*File) = .{},
@ -238,23 +300,27 @@ pub const Module = struct {
loops: BlockList(Loop) = .{}, loops: BlockList(Loop) = .{},
assignments: BlockList(Assignment) = .{}, assignments: BlockList(Assignment) = .{},
syscalls: BlockList(Syscall) = .{}, syscalls: BlockList(Syscall) = .{},
calls: BlockList(Call) = .{},
argument_list: BlockList(ArgumentList) = .{},
returns: BlockList(Return) = .{},
entry_point: ?u32 = null,
pub const Descriptor = struct { pub const Descriptor = struct {
main_package_path: []const u8, main_package_path: []const u8,
}; };
const ImportFileResult = struct { const ImportFileResult = struct {
file: *File, ptr: *File,
index: File.Index,
is_new: bool, is_new: bool,
}; };
const ImportPackageResult = struct { const ImportPackageResult = struct {
file: *File, file: ImportFileResult,
is_new: bool,
is_package: bool, is_package: bool,
}; };
pub fn importFile(module: *Module, allocator: Allocator, current_file: *File, import_name: []const u8) !ImportPackageResult { pub fn importFile(module: *Module, allocator: Allocator, current_file_index: File.Index, import_name: []const u8) !ImportPackageResult {
print("import: '{s}'\n", .{import_name}); print("import: '{s}'\n", .{import_name});
if (equal(u8, import_name, "std")) { if (equal(u8, import_name, "std")) {
return module.importPackage(allocator, module.main_package.dependencies.get("std").?); return module.importPackage(allocator, module.main_package.dependencies.get("std").?);
@ -268,6 +334,7 @@ pub const Module = struct {
return module.importPackage(allocator, module.main_package); return module.importPackage(allocator, module.main_package);
} }
const current_file = module.files.get(current_file_index);
if (current_file.package.dependencies.get(import_name)) |package| { if (current_file.package.dependencies.get(import_name)) |package| {
return module.importPackage(allocator, package); return module.importPackage(allocator, package);
} }
@ -279,55 +346,73 @@ pub const Module = struct {
const full_path = try std.fs.path.join(allocator, &.{ current_file.package.directory.path, import_name }); const full_path = try std.fs.path.join(allocator, &.{ current_file.package.directory.path, import_name });
const file_relative_path = std.fs.path.basename(full_path); const file_relative_path = std.fs.path.basename(full_path);
const package = current_file.package; const package = current_file.package;
const import = try module.getFile(allocator, full_path, file_relative_path, package); const import_file = try module.getFile(allocator, full_path, file_relative_path, package);
try import.file.addFileReference(allocator, current_file); try import_file.ptr.addFileReference(allocator, current_file);
const result = ImportPackageResult{ const result = ImportPackageResult{
.file = import.file, .file = import_file,
.is_new = import.is_new,
.is_package = false, .is_package = false,
}; };
return result; return result;
} }
fn lookupDeclaration(module: *Module, hashed: u32) !noreturn {
_ = hashed;
_ = module;
while (true) {}
}
fn getFile(module: *Module, allocator: Allocator, full_path: []const u8, relative_path: []const u8, package: *Package) !ImportFileResult { fn getFile(module: *Module, allocator: Allocator, full_path: []const u8, relative_path: []const u8, package: *Package) !ImportFileResult {
const path_lookup = try module.import_table.getOrPut(allocator, full_path); const path_lookup = try module.import_table.getOrPut(allocator, full_path);
const file: *File = switch (path_lookup.found_existing) { const file, const index = switch (path_lookup.found_existing) {
true => path_lookup.value_ptr.*, true => blk: {
const result = path_lookup.value_ptr.*;
const index = module.files.indexOf(result);
break :blk .{
result,
index,
};
},
false => blk: { false => blk: {
const new_file_index = try module.files.append(allocator, File{ const file_allocation = try module.files.append(allocator, File{
.relative_path = relative_path, .relative_path = relative_path,
.package = package, .package = package,
}); });
const file = module.files.get(new_file_index); std.debug.print("Adding file #{}: {s}\n", .{ file_allocation.index.uniqueInteger(), full_path });
path_lookup.value_ptr.* = file; path_lookup.value_ptr.* = file_allocation.ptr;
break :blk file; // break :blk file;
break :blk .{
file_allocation.ptr,
file_allocation.index,
};
}, },
}; };
return .{ return .{
.file = file, .ptr = file,
.index = index,
.is_new = !path_lookup.found_existing, .is_new = !path_lookup.found_existing,
}; };
} }
pub fn importPackage(module: *Module, allocator: Allocator, package: *Package) !ImportPackageResult { pub fn importPackage(module: *Module, allocator: Allocator, package: *Package) !ImportPackageResult {
const full_path = try std.fs.path.resolve(allocator, &.{ package.directory.path, package.source_path }); const full_path = try std.fs.path.resolve(allocator, &.{ package.directory.path, package.source_path });
const import = try module.getFile(allocator, full_path, package.source_path, package); const import_file = try module.getFile(allocator, full_path, package.source_path, package);
try import.file.addPackageReference(allocator, package); try import_file.ptr.addPackageReference(allocator, package);
return .{ return .{
.file = import.file, .file = import_file,
.is_new = import.is_new,
.is_package = true, .is_package = true,
}; };
} }
pub fn generateAbstractSyntaxTreeForFile(module: *Module, allocator: Allocator, file: *File) !void { pub fn generateAbstractSyntaxTreeForFile(module: *Module, allocator: Allocator, file: *File) !void {
_ = module; _ = module;
const source_file = try file.package.directory.handle.openFile(file.relative_path, .{}); const source_file = file.package.directory.handle.openFile(file.relative_path, .{}) catch |err| {
std.debug.panic("Can't find file {s} in directory {s} for error {s}", .{ file.relative_path, file.package.directory.path, @errorName(err) });
};
const file_size = try source_file.getEndPos(); const file_size = try source_file.getEndPos();
var file_buffer = try allocator.alloc(u8, file_size); var file_buffer = try allocator.alloc(u8, file_size);
@ -426,14 +511,11 @@ pub fn compileModule(compilation: *Compilation, descriptor: Module.Descriptor) !
try module.generateAbstractSyntaxTreeForFile(compilation.base_allocator, import); try module.generateAbstractSyntaxTreeForFile(compilation.base_allocator, import);
} }
const main_declaration = try semantic_analyzer.initialize(compilation, module, packages[0]); const main_declaration = try semantic_analyzer.initialize(compilation, module, packages[0], .{ .block = 0, .index = 0 });
var ir = try intermediate_representation.initialize(compilation, module, packages[0], main_declaration); var ir = try intermediate_representation.initialize(compilation, module, packages[0], main_declaration);
switch (@import("builtin").cpu.arch) { try emit.get(.x86_64).initialize(compilation.base_allocator, &ir);
.x86_64 => |arch| try emit.get(arch).initialize(compilation.base_allocator, &ir),
else => {},
}
} }
fn generateAST() !void {} fn generateAST() !void {}
@ -465,6 +547,9 @@ pub const File = struct {
relative_path: []const u8, relative_path: []const u8,
package: *Package, package: *Package,
pub const List = BlockList(@This());
pub const Index = List.Index;
const Status = enum { const Status = enum {
not_loaded, not_loaded,
loaded_into_memory, loaded_into_memory,
@ -484,15 +569,6 @@ pub const File = struct {
try file.file_references.append(allocator, affected); try file.file_references.append(allocator, affected);
} }
pub fn fromRelativePath(allocator: Allocator, file_relative_path: []const u8) *File {
const file_content = try std.fs.cwd().readFileAlloc(allocator, file_relative_path, std.math.maxInt(usize));
_ = file_content;
const file = try allocator.create(File);
file.* = File{};
return file;
}
fn lex(file: *File, allocator: Allocator) !void { fn lex(file: *File, allocator: Allocator) !void {
assert(file.status == .loaded_into_memory); assert(file.status == .loaded_into_memory);
file.lexical_analyzer_result = try lexical_analyzer.analyze(allocator, file.source_code); file.lexical_analyzer_result = try lexical_analyzer.analyze(allocator, file.source_code);

View File

@ -27,7 +27,7 @@ pub const Result = struct {
}, },
entry_point: u32 = 0, entry_point: u32 = 0,
fn create() !Result { pub fn create() !Result {
return Result{ return Result{
.sections = .{ .sections = .{
.text = .{ .content = try mmap(page_size, .{ .executable = true }) }, .text = .{ .content = try mmap(page_size, .{ .executable = true }) },
@ -46,13 +46,18 @@ pub const Result = struct {
break :blk @as([*]align(0x1000) u8, @ptrCast(@alignCast(try windows.VirtualAlloc(null, size, windows.MEM_COMMIT | windows.MEM_RESERVE, windows.PAGE_EXECUTE_READWRITE))))[0..size]; break :blk @as([*]align(0x1000) u8, @ptrCast(@alignCast(try windows.VirtualAlloc(null, size, windows.MEM_COMMIT | windows.MEM_RESERVE, windows.PAGE_EXECUTE_READWRITE))))[0..size];
}, },
.linux, .macos => |os_tag| blk: { .linux, .macos => |os_tag| blk: {
const jit = switch (os_tag) {
.macos => 0x800,
.linux => 0,
else => unreachable,
};
const execute_flag: switch (os_tag) { const execute_flag: switch (os_tag) {
.linux => u32, .linux => u32,
.macos => c_int, .macos => c_int,
else => unreachable, else => unreachable,
} = if (flags.executable) std.os.PROT.EXEC else 0; } = if (flags.executable) std.os.PROT.EXEC else 0;
const protection_flags: u32 = @intCast(std.os.PROT.READ | std.os.PROT.WRITE | execute_flag); const protection_flags: u32 = @intCast(std.os.PROT.READ | std.os.PROT.WRITE | execute_flag);
const mmap_flags = std.os.MAP.ANONYMOUS | std.os.MAP.PRIVATE; const mmap_flags = std.os.MAP.ANONYMOUS | std.os.MAP.PRIVATE | jit;
break :blk std.os.mmap(null, size, protection_flags, mmap_flags, -1, 0); break :blk std.os.mmap(null, size, protection_flags, mmap_flags, -1, 0);
}, },
@ -77,14 +82,6 @@ pub const Result = struct {
image.sections.text.index += 1; image.sections.text.index += 1;
} }
// fn appendOnlyOpcodeSkipInstructionBytes(image: *Result, instruction: Instruction) void {
// const instruction_descriptor = instruction_descriptors.get(instruction);
// assert(instruction_descriptor.opcode_byte_count == instruction_descriptor.operand_offset);
// image.appendCode(instruction_descriptor.getOpcode());
//
// image.sections.text.index += instruction_descriptor.size - instruction_descriptor.opcode_byte_count;
// }
fn getEntryPoint(image: *const Result, comptime FunctionType: type) *const FunctionType { fn getEntryPoint(image: *const Result, comptime FunctionType: type) *const FunctionType {
comptime { comptime {
assert(@typeInfo(FunctionType) == .Fn); assert(@typeInfo(FunctionType) == .Fn);
@ -102,16 +99,14 @@ pub fn InstructionSelector(comptime Instruction: type) type {
pub const Function = struct { pub const Function = struct {
instructions: ArrayList(Instruction) = .{}, instructions: ArrayList(Instruction) = .{},
block_byte_counts: ArrayList(u16),
block_offsets: ArrayList(u32),
relocations: ArrayList(u32) = .{}, relocations: ArrayList(u32) = .{},
block_map: AutoHashMap(ir.BasicBlock.Index, u32) = .{}, block_map: AutoHashMap(ir.BasicBlock.Index, u32) = .{},
byte_count: u32 = 0,
block_byte_count: u16 = 0,
pub fn selectInstruction(function: *Function, allocator: Allocator, instruction: Instruction) !void { pub fn addInstruction(function: *Function, allocator: Allocator, instruction: Instruction) !u32 {
const index = function.instructions.items.len;
try function.instructions.append(allocator, instruction); try function.instructions.append(allocator, instruction);
function.block_byte_count += Instruction.descriptors.get(instruction).size;
return @intCast(index);
} }
}; };
@ -124,81 +119,13 @@ pub fn get(comptime arch: std.Target.Cpu.Arch) type {
.x86_64 => @import("x86_64.zig"), .x86_64 => @import("x86_64.zig"),
else => @compileError("Architecture not supported"), else => @compileError("Architecture not supported"),
}; };
const Instruction = backend.Instruction;
return struct { return struct {
pub fn initialize(allocator: Allocator, intermediate: *ir.Result) !void { pub fn initialize(allocator: Allocator, intermediate: *ir.Result) !void {
var result = try Result.create(); std.debug.print("Entry point: {}\n", .{intermediate.entry_point});
var function_iterator = intermediate.functions.iterator(); var mir = try backend.MIR.generate(allocator, intermediate);
const IS = InstructionSelector(Instruction); try mir.allocateRegisters(allocator, intermediate);
var instruction_selector = IS{ const result = try mir.encode(intermediate);
.functions = try ArrayList(IS.Function).initCapacity(allocator, intermediate.functions.len),
.allocator = allocator,
};
while (function_iterator.next()) |ir_function| {
const function = instruction_selector.functions.addOneAssumeCapacity();
function.* = .{
.block_byte_counts = try ArrayList(u16).initCapacity(allocator, ir_function.blocks.items.len),
.block_offsets = try ArrayList(u32).initCapacity(allocator, ir_function.blocks.items.len),
};
try function.block_map.ensureTotalCapacity(allocator, @intCast(ir_function.blocks.items.len));
for (ir_function.blocks.items, 0..) |block_index, index| {
function.block_map.putAssumeCapacity(block_index, @intCast(index));
}
for (ir_function.blocks.items) |block_index| {
const block = intermediate.blocks.get(block_index);
function.block_offsets.appendAssumeCapacity(function.byte_count);
function.block_byte_count = 0;
for (block.instructions.items) |instruction_index| {
const instruction = intermediate.instructions.get(instruction_index).*;
try backend.selectInstruction(&instruction_selector, function, intermediate, instruction);
}
function.block_byte_counts.appendAssumeCapacity(function.block_byte_count);
function.byte_count += function.block_byte_count;
}
}
for (instruction_selector.functions.items) |function| {
for (function.instructions.items) |instruction| backend.emitInstruction(&result, instruction, intermediate);
}
// for (instruction_selector.functions.items) |function| {
// var fix_size: bool = false;
// _ = fix_size;
// for (function.relocations.items) |instruction_index| {
// const instruction = function.instructions.items[instruction_index];
// const relative = instruction.jmp_rel_8;
// const source_block = relative.source;
// const destination_block = relative.destination;
// const source_offset = function.block_offsets.items[source_block];
// const destination_offset = function.block_offsets.items[destination_block];
// std.debug.print("Source offset: {}. Destination: {}\n", .{ source_offset, destination_offset });
// const instruction_descriptor = instruction_descriptors.get(relative.instruction);
// const instruction_offset = source_offset + relative.block_offset;
// const really_source_offset = instruction_offset + instruction_descriptor.size;
// const displacement = @as(i64, destination_offset) - @as(i64, really_source_offset);
//
// const operands = instruction_descriptor.getOperands();
// switch (operands.len) {
// 1 => switch (operands[0].size) {
// @sizeOf(u8) => {
// if (displacement >= std.math.minInt(i8) and displacement <= std.math.maxInt(i8)) {
// const writer_index = instruction_offset + instruction_descriptor.operand_offset;
// std.debug.print("Instruction offset: {}. Operand offset: {}. Writer index: {}. displacement: {}\n", .{ instruction_offset, instruction_descriptor.operand_offset, writer_index, displacement });
// result.sections.text.content[writer_index] = @bitCast(@as(i8, @intCast(displacement)));
// } else {
// unreachable;
// }
// },
// else => unreachable,
// },
// else => unreachable,
// }
// }
// }
const text_section = result.sections.text.content[0..result.sections.text.index]; const text_section = result.sections.text.content[0..result.sections.text.index];
for (text_section) |byte| { for (text_section) |byte| {

View File

@ -10,15 +10,22 @@ const Package = Compilation.Package;
const data_structures = @import("../data_structures.zig"); const data_structures = @import("../data_structures.zig");
const ArrayList = data_structures.ArrayList; const ArrayList = data_structures.ArrayList;
const BlockList = data_structures.BlockList; const BlockList = data_structures.BlockList;
const AutoArrayHashMap = data_structures.AutoArrayHashMap;
const AutoHashMap = data_structures.AutoHashMap;
pub const Result = struct { pub const Result = struct {
functions: BlockList(Function) = .{},
blocks: BlockList(BasicBlock) = .{}, blocks: BlockList(BasicBlock) = .{},
calls: BlockList(Call) = .{},
functions: BlockList(Function) = .{},
instructions: BlockList(Instruction) = .{}, instructions: BlockList(Instruction) = .{},
jumps: BlockList(Jump) = .{}, jumps: BlockList(Jump) = .{},
values: BlockList(Value) = .{},
syscalls: BlockList(Syscall) = .{},
loads: BlockList(Load) = .{}, loads: BlockList(Load) = .{},
phis: BlockList(Phi) = .{},
stores: BlockList(Store) = .{},
syscalls: BlockList(Syscall) = .{},
values: BlockList(Value) = .{},
stack_references: BlockList(StackReference) = .{},
entry_point: u32 = 0,
}; };
pub fn initialize(compilation: *Compilation, module: *Module, package: *Package, main_file: Compilation.Type.Index) !Result { pub fn initialize(compilation: *Compilation, module: *Module, package: *Package, main_file: Compilation.Type.Index) !Result {
@ -32,10 +39,16 @@ pub fn initialize(compilation: *Compilation, module: *Module, package: *Package,
.module = module, .module = module,
}; };
while (function_iterator.next()) |sema_function| { builder.ir.entry_point = module.entry_point orelse unreachable;
print("\nFunction: {}\n", .{sema_function});
try builder.function(sema_function); while (function_iterator.next()) |sema_function| {
const function_index = try builder.buildFunction(sema_function);
try builder.optimizeFunction(function_index);
}
var ir_function_iterator = builder.ir.functions.iterator();
while (ir_function_iterator.nextPointer()) |function| {
print("\n{}\n", .{function});
} }
return builder.ir; return builder.ir;
@ -61,11 +74,14 @@ pub const BasicBlock = struct {
}; };
pub const Instruction = union(enum) { pub const Instruction = union(enum) {
call: Call.Index,
jump: Jump.Index, jump: Jump.Index,
load: Load.Index, load: Load.Index,
phi: Phi.Index, phi: Phi.Index,
ret: Ret, ret: Value.Index,
syscall: Syscall.Index, store: Store.Index,
syscall: Value.Index,
copy: Value.Index,
@"unreachable", @"unreachable",
pub const List = BlockList(@This()); pub const List = BlockList(@This());
@ -73,15 +89,14 @@ pub const Instruction = union(enum) {
}; };
const Phi = struct { const Phi = struct {
foo: u32 = 0, value: Value.Index,
jump: Jump.Index,
block: BasicBlock.Index,
next: Phi.Index,
pub const List = BlockList(@This()); pub const List = BlockList(@This());
pub const Index = List.Index; pub const Index = List.Index;
}; };
const Ret = struct {
value: Instruction.Index,
};
pub const Jump = struct { pub const Jump = struct {
source: BasicBlock.Index, source: BasicBlock.Index,
destination: BasicBlock.Index, destination: BasicBlock.Index,
@ -102,9 +117,37 @@ const Load = struct {
pub const Index = List.Index; pub const Index = List.Index;
}; };
const Store = struct {
source: Value.Index,
destination: Value.Index,
pub const List = BlockList(@This());
pub const Index = List.Index;
};
pub const StackReference = struct {
size: u64,
alignment: u64,
offset: u64,
pub const List = BlockList(@This());
pub const Index = List.Index;
};
pub const Call = struct {
function: Function.Index,
pub const List = BlockList(@This());
pub const Index = List.Index;
pub const Allocation = List.Allocation;
};
pub const Value = union(enum) { pub const Value = union(enum) {
integer: Integer, integer: Compilation.Integer,
load: Load.Index, load: Load.Index,
call: Call.Index,
stack_reference: StackReference.Index,
phi: Phi.Index,
instruction: Instruction.Index,
syscall: Syscall.Index,
pub const List = BlockList(@This()); pub const List = BlockList(@This());
pub const Index = List.Index; pub const Index = List.Index;
@ -112,75 +155,301 @@ pub const Value = union(enum) {
return switch (value) { return switch (value) {
.integer => false, .integer => false,
.load => true, .load => true,
.call => true,
.stack_reference => true,
.phi => unreachable,
.instruction => unreachable,
.syscall => unreachable,
}; };
} }
}; };
const Integer = struct { pub const Function = struct {
value: u64,
sign: bool,
};
const Function = struct {
blocks: ArrayList(BasicBlock.Index) = .{}, blocks: ArrayList(BasicBlock.Index) = .{},
stack_map: AutoHashMap(Compilation.Declaration.Index, Value.Index) = .{},
current_basic_block: BasicBlock.Index = BasicBlock.Index.invalid,
return_phi_node: Instruction.Index = Instruction.Index.invalid,
return_phi_block: BasicBlock.Index = BasicBlock.Index.invalid,
ir: *Result,
current_stack_offset: usize = 0,
pub const List = BlockList(@This()); pub const List = BlockList(@This());
pub const Index = List.Index; pub const Index = List.Index;
pub fn format(function: *const Function, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {
try writer.writeAll("Function:\n");
for (function.blocks.items, 0..) |block_index, function_block_index| {
try writer.print("#{}:\n", .{function_block_index});
const block = function.ir.blocks.get(block_index);
for (block.instructions.items, 0..) |instruction_index, block_instruction_index| {
try writer.print("%{}: ", .{block_instruction_index});
const instruction = function.ir.instructions.get(instruction_index).*;
try writer.print("{s}", .{@tagName(instruction)});
try writer.writeByte('\n');
}
try writer.writeByte('\n');
}
_ = options;
_ = fmt;
}
}; };
pub const Builder = struct { pub const Builder = struct {
allocator: Allocator, allocator: Allocator,
ir: Result = .{}, ir: Result = .{},
module: *Module, module: *Module,
current_basic_block: BasicBlock.Index = BasicBlock.Index.invalid,
current_function_index: Function.Index = Function.Index.invalid, current_function_index: Function.Index = Function.Index.invalid,
fn function(builder: *Builder, sema_function: Compilation.Function) !void { fn currentFunction(builder: *Builder) *Function {
builder.current_function_index = try builder.ir.functions.append(builder.allocator, .{}); return builder.ir.functions.get(builder.current_function_index);
}
fn buildFunction(builder: *Builder, sema_function: Compilation.Function) !Function.Index {
const function_allocation = try builder.ir.functions.append(builder.allocator, .{
.ir = &builder.ir,
});
builder.current_function_index = function_allocation.index;
const function = function_allocation.ptr;
// TODO: arguments // TODO: arguments
builder.current_basic_block = try builder.newBlock(); function.current_basic_block = try builder.newBlock();
const return_type = builder.module.types.get(builder.module.function_prototypes.get(sema_function.prototype).return_type); const return_type = builder.module.types.get(builder.module.function_prototypes.get(sema_function.prototype).return_type);
const is_noreturn = return_type.* == .noreturn; const is_noreturn = return_type.* == .noreturn;
if (!is_noreturn) { if (!is_noreturn) {
const exit_block = try builder.newBlock(); const exit_block = try builder.newBlock();
const phi = try builder.appendToBlock(exit_block, .{ const phi_instruction = try builder.appendToBlock(exit_block, .{
.phi = Phi.Index.invalid, .phi = Phi.Index.invalid,
}); });
// phi.ptr.* = .{
// .value = Value.Index.invalid,
// .jump = Jump.Index.invalid,
// .block = exit_block,
// .next = Phi.Index.invalid,
// };
const ret = try builder.appendToBlock(exit_block, .{ const ret = try builder.appendToBlock(exit_block, .{
.ret = .{ .ret = (try builder.ir.values.append(builder.allocator, .{
.value = phi, .instruction = phi_instruction,
}, })).index,
}); });
_ = ret; _ = ret;
function.return_phi_node = phi_instruction;
function.return_phi_block = exit_block;
} }
const sema_block = sema_function.getBodyBlock(builder.module); const sema_block = sema_function.getBodyBlock(builder.module);
try builder.block(sema_block, .{ .emit_exit_block = !is_noreturn }); try builder.block(sema_block, .{ .emit_exit_block = !is_noreturn });
try builder.dumpFunction(std.io.getStdErr().writer(), builder.current_function_index); builder.currentFunction().current_stack_offset = std.mem.alignForward(usize, builder.currentFunction().current_stack_offset, 0x10);
return builder.current_function_index;
} }
fn dumpFunction(builder: *Builder, writer: anytype, index: Function.Index) !void { const BlockSearcher = struct {
const f = builder.ir.functions.get(index); to_visit: ArrayList(BasicBlock.Index) = .{},
try writer.writeAll("Hello world!\n"); visited: AutoArrayHashMap(BasicBlock.Index, void) = .{},
print("Function blocks: {}\n", .{f.blocks.items.len}); };
var function_instruction_index: usize = 0;
for (f.blocks.items, 0..) |block_index, function_block_index| { fn findReachableBlocks(builder: *Builder, first: BasicBlock.Index) ![]const BasicBlock.Index {
print("#{}:\n", .{function_block_index}); var searcher = BlockSearcher{};
const function_block = builder.ir.blocks.get(block_index); try searcher.to_visit.append(builder.allocator, first);
for (function_block.instructions.items) |instruction_index| { try searcher.visited.put(builder.allocator, first, {});
while (searcher.to_visit.items.len > 0) {
const block_index = searcher.to_visit.swapRemove(0);
const block_to_visit = builder.ir.blocks.get(block_index);
const last_instruction_index = block_to_visit.instructions.items[block_to_visit.instructions.items.len - 1];
const last_instruction = builder.ir.instructions.get(last_instruction_index);
switch (last_instruction.*) {
.jump => |jump_index| {
const ir_jump = builder.ir.jumps.get(jump_index);
assert(ir_jump.source.eq(block_index));
const new_block = ir_jump.destination;
if (searcher.visited.get(new_block) == null) {
try searcher.to_visit.append(builder.allocator, new_block);
try searcher.visited.put(builder.allocator, new_block, {});
}
},
.@"unreachable", .ret => {},
else => |t| @panic(@tagName(t)),
}
}
return searcher.visited.keys();
}
fn optimizeFunction(builder: *Builder, function_index: Function.Index) !void {
const function = builder.ir.functions.get(function_index);
const reachable_blocks = try builder.findReachableBlocks(function.blocks.items[0]);
var did_something = true;
while (did_something) {
did_something = false;
for (reachable_blocks) |basic_block_index| {
const basic_block = builder.ir.blocks.get(basic_block_index);
for (basic_block.instructions.items) |instruction_index| {
did_something = did_something or try builder.removeUnreachablePhis(reachable_blocks, instruction_index);
did_something = did_something or try builder.removeTrivialPhis(instruction_index);
const copy = try builder.removeCopyReferences(instruction_index);
did_something = did_something or copy;
}
}
}
var instructions_to_delete = ArrayList(u32){};
for (reachable_blocks) |basic_block_index| {
instructions_to_delete.clearRetainingCapacity();
const basic_block = builder.ir.blocks.get(basic_block_index);
for (basic_block.instructions.items, 0..) |instruction_index, index| {
const instruction = builder.ir.instructions.get(instruction_index); const instruction = builder.ir.instructions.get(instruction_index);
print("%{}: {}\n", .{ function_instruction_index, instruction }); switch (instruction.*) {
function_instruction_index += 1; .copy => try instructions_to_delete.append(builder.allocator, @intCast(index)),
else => {},
}
} }
print("\n", .{}); var deleted_instruction_count: usize = 0;
for (instructions_to_delete.items) |instruction_to_delete| {
_ = basic_block.instructions.orderedRemove(instruction_to_delete - deleted_instruction_count);
}
} }
} }
fn removeUnreachablePhis(builder: *Builder, reachable_blocks: []const BasicBlock.Index, instruction_index: Instruction.Index) !bool {
const instruction = builder.ir.instructions.get(instruction_index);
return switch (instruction.*) {
.phi => blk: {
var did_something = false;
var head = &instruction.phi;
next: while (head.valid) {
const phi = builder.ir.phis.get(head.*);
const phi_jump = builder.ir.jumps.get(phi.jump);
assert(phi_jump.source.valid);
for (reachable_blocks) |block_index| {
if (phi_jump.source.eq(block_index)) {
head = &phi.next;
continue :next;
}
}
head.* = phi.next;
did_something = true;
}
break :blk did_something;
},
else => false,
};
}
fn removeTrivialPhis(builder: *Builder, instruction_index: Instruction.Index) !bool {
const instruction = builder.ir.instructions.get(instruction_index);
return switch (instruction.*) {
.phi => |phi_index| blk: {
const trivial_phi: ?Value.Index = trivial_blk: {
var only_value = Value.Index.invalid;
var it = phi_index;
while (it.valid) {
const phi = builder.ir.phis.get(it);
const phi_value = builder.ir.values.get(phi.value);
if (phi_value.* == .phi) unreachable;
// TODO: undefined
if (only_value.valid) {
if (!only_value.eq(phi.value)) {
break :trivial_blk null;
}
} else {
only_value = phi.value;
}
it = phi.next;
}
break :trivial_blk only_value;
};
if (trivial_phi) |trivial_value| {
if (trivial_value.valid) {
// Option to delete
const delete = false;
if (delete) {
unreachable;
} else {
instruction.* = .{
.copy = trivial_value,
};
}
} else {
unreachable;
}
}
break :blk instruction.* != .phi;
},
else => false,
};
}
fn removeCopyReferences(builder: *Builder, instruction_index: Instruction.Index) !bool {
const instruction = builder.ir.instructions.get(instruction_index);
return switch (instruction.*) {
.copy => false,
else => {
var did_something = false;
const operands: []const *Value.Index = switch (instruction.*) {
.jump, .@"unreachable" => &.{},
.ret => &.{&instruction.ret},
// TODO: arguments
.call => blk: {
var list = ArrayList(*Value.Index){};
break :blk list.items;
},
.store => |store_index| blk: {
const store_instr = builder.ir.stores.get(store_index);
break :blk &.{ &store_instr.source, &store_instr.destination };
},
.syscall => |syscall_value_index| blk: {
const syscall_value = builder.ir.values.get(syscall_value_index);
const syscall = builder.ir.syscalls.get(syscall_value.syscall);
var list = ArrayList(*Value.Index){};
try list.ensureTotalCapacity(builder.allocator, syscall.arguments.items.len);
for (syscall.arguments.items) |*arg| {
list.appendAssumeCapacity(arg);
}
break :blk list.items;
},
else => |t| @panic(@tagName(t)),
};
for (operands) |operand_value_index| {
const operand_value = builder.ir.values.get(operand_value_index.*);
switch (operand_value.*) {
.instruction => |operand_instruction_index| {
const operand_instruction = builder.ir.instructions.get(operand_instruction_index);
switch (operand_instruction.*) {
.copy => |copy_value| {
operand_value_index.* = copy_value;
did_something = true;
},
else => |t| @panic(@tagName(t)),
}
},
.integer, .stack_reference, .call => {},
else => |t| @panic(@tagName(t)),
}
}
return did_something;
},
};
}
fn blockInsideBasicBlock(builder: *Builder, sema_block: *Compilation.Block, block_index: BasicBlock.Index) !BasicBlock.Index { fn blockInsideBasicBlock(builder: *Builder, sema_block: *Compilation.Block, block_index: BasicBlock.Index) !BasicBlock.Index {
builder.current_basic_block = block_index; const current_function = builder.currentFunction();
current_function.current_basic_block = block_index;
try builder.block(sema_block, .{}); try builder.block(sema_block, .{});
return builder.current_basic_block; return current_function.current_basic_block;
} }
const BlockOptions = packed struct { const BlockOptions = packed struct {
@ -203,7 +472,7 @@ pub const Builder = struct {
else => |t| @panic(@tagName(t)), else => |t| @panic(@tagName(t)),
}; };
const original_block = builder.current_basic_block; const original_block = builder.currentFunction().current_basic_block;
const jump_to_loop = try builder.append(.{ const jump_to_loop = try builder.append(.{
.jump = undefined, .jump = undefined,
}); });
@ -221,7 +490,7 @@ pub const Builder = struct {
}); });
const sema_body_block = builder.module.blocks.get(sema_loop_body.block); const sema_body_block = builder.module.blocks.get(sema_loop_body.block);
builder.current_basic_block = try builder.blockInsideBasicBlock(sema_body_block, loop_body_block); builder.currentFunction().current_basic_block = try builder.blockInsideBasicBlock(sema_body_block, loop_body_block);
if (loop_prologue_block.valid) { if (loop_prologue_block.valid) {
builder.ir.blocks.get(loop_prologue_block).seal(); builder.ir.blocks.get(loop_prologue_block).seal();
} }
@ -229,20 +498,20 @@ pub const Builder = struct {
if (sema_body_block.reaches_end) { if (sema_body_block.reaches_end) {
_ = try builder.append(.{ _ = try builder.append(.{
.jump = try builder.jump(.{ .jump = try builder.jump(.{
.source = builder.current_basic_block, .source = builder.currentFunction().current_basic_block,
.destination = loop_head_block, .destination = loop_head_block,
}), }),
}); });
} }
builder.ir.blocks.get(builder.current_basic_block).filled = true; builder.ir.blocks.get(builder.currentFunction().current_basic_block).filled = true;
builder.ir.blocks.get(loop_body_block).seal(); builder.ir.blocks.get(loop_body_block).seal();
if (!loop_head_block.eq(loop_body_block)) { if (!loop_head_block.eq(loop_body_block)) {
unreachable; unreachable;
} }
if (loop_prologue_block.valid) { if (loop_prologue_block.valid) {
builder.current_basic_block = loop_prologue_block; builder.currentFunction().current_basic_block = loop_prologue_block;
} }
}, },
.syscall => |syscall_index| { .syscall => |syscall_index| {
@ -257,80 +526,216 @@ pub const Builder = struct {
for (sema_syscall.getArguments()) |sema_syscall_argument| { for (sema_syscall.getArguments()) |sema_syscall_argument| {
assert(sema_syscall_argument.valid); assert(sema_syscall_argument.valid);
const argument_value_index = try builder.emitValue(sema_syscall_argument); var argument_value_index = try builder.emitValue(sema_syscall_argument);
arguments.appendAssumeCapacity(argument_value_index); arguments.appendAssumeCapacity(argument_value_index);
} }
// TODO: undo this mess
_ = try builder.append(.{ _ = try builder.append(.{
.syscall = try builder.ir.syscalls.append(builder.allocator, .{ .syscall = (try builder.ir.values.append(builder.allocator, .{
.arguments = arguments, .syscall = (try builder.ir.syscalls.append(builder.allocator, .{
}), .arguments = arguments,
})).index,
})).index,
}); });
}, },
.@"unreachable" => _ = try builder.append(.{ .@"unreachable" => _ = try builder.append(.{
.@"unreachable" = {}, .@"unreachable" = {},
}), }),
.@"return" => |sema_ret_index| {
const sema_ret = builder.module.returns.get(sema_ret_index);
const return_value = try builder.emitValue(sema_ret.value);
const phi_instruction = builder.ir.instructions.get(builder.currentFunction().return_phi_node);
const phi = switch (phi_instruction.phi.valid) {
true => unreachable,
false => (try builder.ir.phis.append(builder.allocator, std.mem.zeroes(Phi))).ptr,
}; //builder.ir.phis.get(phi_instruction.phi);
const exit_jump = try builder.jump(.{
.source = builder.currentFunction().current_basic_block,
.destination = switch (phi_instruction.phi.valid) {
true => phi.block,
false => builder.currentFunction().return_phi_block,
},
});
print("Previous phi: {}\n", .{phi_instruction.phi});
phi_instruction.phi = (try builder.ir.phis.append(builder.allocator, .{
.value = return_value,
.jump = exit_jump,
.next = phi_instruction.phi,
.block = phi.block,
})).index;
_ = try builder.append(.{
.jump = exit_jump,
});
},
.declaration => |sema_declaration_index| {
const sema_declaration = builder.module.declarations.get(sema_declaration_index);
assert(sema_declaration.scope_type == .local);
const sema_init_value = builder.module.values.get(sema_declaration.init_value);
const declaration_type = builder.module.types.get(sema_init_value.getType(builder.module));
const size = declaration_type.getSize();
const alignment = declaration_type.getAlignment();
const stack_offset = switch (size > 0) {
true => builder.allocateStack(size, alignment),
false => 0,
};
var value_index = try builder.emitValue(sema_declaration.init_value);
const value = builder.ir.values.get(value_index);
print("Value: {}\n", .{value.*});
value_index = switch (value.isInMemory()) {
false => try builder.load(value_index),
true => value_index,
};
if (stack_offset > 0) {
_ = try builder.store(.{
.source = value_index,
.destination = try builder.stackReference(stack_offset, declaration_type.*, sema_declaration_index),
});
}
},
else => |t| @panic(@tagName(t)), else => |t| @panic(@tagName(t)),
} }
} }
} }
fn stackReference(builder: *Builder, stack_offset: u64, t: Compilation.Type, sema_declaration: Compilation.Declaration.Index) !Value.Index {
const stack_reference_allocation = try builder.ir.stack_references.append(builder.allocator, .{
.offset = stack_offset,
.size = t.getSize(),
.alignment = t.getAlignment(),
});
const value_allocation = try builder.ir.values.append(builder.allocator, .{
.stack_reference = stack_reference_allocation.index,
});
try builder.currentFunction().stack_map.put(builder.allocator, sema_declaration, value_allocation.index);
return value_allocation.index;
}
fn store(builder: *Builder, descriptor: Store) !void {
const store_allocation = try builder.ir.stores.append(builder.allocator, descriptor);
_ = try builder.append(.{
.store = store_allocation.index,
});
}
fn allocateStack(builder: *Builder, size: u64, alignment: u64) u64 {
builder.currentFunction().current_stack_offset = std.mem.alignForward(u64, builder.currentFunction().current_stack_offset, alignment);
builder.currentFunction().current_stack_offset += size;
return builder.currentFunction().current_stack_offset;
}
fn load(builder: *Builder, value_index: Value.Index) !Value.Index { fn load(builder: *Builder, value_index: Value.Index) !Value.Index {
print("Doing load!\n", .{}); print("Doing load!\n", .{});
const load_index = try builder.ir.loads.append(builder.allocator, .{ const load_allocation = try builder.ir.loads.append(builder.allocator, .{
.value = value_index, .value = value_index,
}); });
const instruction_index = try builder.append(.{ const instruction_index = try builder.append(.{
.load = load_index, .load = load_allocation.index,
}); });
_ = instruction_index; _ = instruction_index;
const result = try builder.ir.values.append(builder.allocator, .{ const result = try builder.ir.values.append(builder.allocator, .{
.load = load_index, .load = load_allocation.index,
}); });
return result; return result.index;
} }
fn emitValue(builder: *Builder, sema_value_index: Compilation.Value.Index) !Value.Index { fn emitValue(builder: *Builder, sema_value_index: Compilation.Value.Index) !Value.Index {
const sema_value = builder.module.values.get(sema_value_index).*; const sema_value = builder.module.values.get(sema_value_index).*;
return switch (sema_value) { return switch (sema_value) {
// TODO // TODO
.integer => |integer| try builder.ir.values.append(builder.allocator, .{ .integer => |integer| (try builder.ir.values.append(builder.allocator, .{
.integer = .{ .integer = integer,
.value = integer, })).index,
.sign = false, .call => |sema_call_index| {
}, const sema_call = builder.module.calls.get(sema_call_index);
}), const argument_list_index = sema_call.arguments;
if (argument_list_index.valid) {
unreachable;
}
const call_index = try builder.call(.{
.function = switch (builder.module.values.get(sema_call.value).*) {
.function => |function_index| .{
.index = function_index.index,
.block = function_index.block,
},
else => |t| @panic(@tagName(t)),
},
});
_ = try builder.append(.{
.call = call_index,
});
const value_allocation = try builder.ir.values.append(builder.allocator, .{
.call = call_index,
});
return value_allocation.index;
},
.declaration_reference => |sema_declaration_index| {
const sema_declaration = builder.module.declarations.get(sema_declaration_index);
const sema_init_value = builder.module.values.get(sema_declaration.init_value);
const init_type = sema_init_value.getType(builder.module);
_ = init_type;
switch (sema_declaration.scope_type) {
.local => {
const stack_reference = builder.currentFunction().stack_map.get(sema_declaration_index).?;
return stack_reference;
},
.global => unreachable,
}
// switch (sema_declaration.*) {
// else => |t| @panic(@tagName(t)),
// }
},
else => |t| @panic(@tagName(t)), else => |t| @panic(@tagName(t)),
}; };
} }
fn jump(builder: *Builder, jump_descriptor: Jump) !Jump.Index { fn call(builder: *Builder, descriptor: Call) !Call.Index {
const destination_block = builder.ir.blocks.get(jump_descriptor.destination); const call_allocation = try builder.ir.calls.append(builder.allocator, descriptor);
return call_allocation.index;
}
fn jump(builder: *Builder, descriptor: Jump) !Jump.Index {
const destination_block = builder.ir.blocks.get(descriptor.destination);
assert(!destination_block.sealed); assert(!destination_block.sealed);
return try builder.ir.jumps.append(builder.allocator, jump_descriptor); const jump_allocation = try builder.ir.jumps.append(builder.allocator, descriptor);
return jump_allocation.index;
} }
fn append(builder: *Builder, instruction: Instruction) !Instruction.Index { fn append(builder: *Builder, instruction: Instruction) !Instruction.Index {
assert(builder.current_basic_block.valid); assert(builder.current_function_index.valid);
return builder.appendToBlock(builder.current_basic_block, instruction); const current_function = builder.currentFunction();
assert(current_function.current_basic_block.valid);
return builder.appendToBlock(current_function.current_basic_block, instruction);
} }
fn appendToBlock(builder: *Builder, block_index: BasicBlock.Index, instruction: Instruction) !Instruction.Index { fn appendToBlock(builder: *Builder, block_index: BasicBlock.Index, instruction: Instruction) !Instruction.Index {
const instruction_index = try builder.ir.instructions.append(builder.allocator, instruction); if (instruction == .phi) {
try builder.ir.blocks.get(block_index).instructions.append(builder.allocator, instruction_index); print("Adding phi: {}\n", .{instruction});
}
const instruction_allocation = try builder.ir.instructions.append(builder.allocator, instruction);
try builder.ir.blocks.get(block_index).instructions.append(builder.allocator, instruction_allocation.index);
return instruction_index; return instruction_allocation.index;
} }
fn newBlock(builder: *Builder) !BasicBlock.Index { fn newBlock(builder: *Builder) !BasicBlock.Index {
const new_block_index = try builder.ir.blocks.append(builder.allocator, .{}); const new_block_allocation = try builder.ir.blocks.append(builder.allocator, .{});
const current_function = builder.ir.functions.get(builder.current_function_index); const current_function = builder.ir.functions.get(builder.current_function_index);
const function_block_index = current_function.blocks.items.len; const function_block_index = current_function.blocks.items.len;
try current_function.blocks.append(builder.allocator, new_block_index); try current_function.blocks.append(builder.allocator, new_block_allocation.index);
print("Adding block: {}\n", .{function_block_index}); print("Adding block: {}\n", .{function_block_index});
return new_block_index; return new_block_allocation.index;
} }
}; };

File diff suppressed because it is too large Load Diff

View File

@ -2,6 +2,7 @@ const std = @import("std");
const assert = std.debug.assert; const assert = std.debug.assert;
pub const Allocator = std.mem.Allocator; pub const Allocator = std.mem.Allocator;
pub const AutoArrayHashMap = std.AutoArrayHashMapUnmanaged;
pub const ArrayList = std.ArrayListUnmanaged; pub const ArrayList = std.ArrayListUnmanaged;
pub const AutoHashMap = std.AutoHashMapUnmanaged; pub const AutoHashMap = std.AutoHashMapUnmanaged;
pub const HashMap = std.HashMapUnmanaged; pub const HashMap = std.HashMapUnmanaged;
@ -36,8 +37,8 @@ pub fn BlockList(comptime T: type) type {
const List = @This(); const List = @This();
pub const Index = packed struct(u32) { pub const Index = packed struct(u32) {
block: u24,
index: u6, index: u6,
block: u24,
_reserved: bool = false, _reserved: bool = false,
valid: bool = true, valid: bool = true,
@ -50,6 +51,11 @@ pub fn BlockList(comptime T: type) type {
pub fn eq(index: Index, other: Index) bool { pub fn eq(index: Index, other: Index) bool {
return @as(u32, @bitCast(index)) == @as(u32, @bitCast(other)); return @as(u32, @bitCast(index)) == @as(u32, @bitCast(other));
} }
pub fn uniqueInteger(index: Index) u32 {
assert(index.valid);
return @as(u30, @truncate(@as(u32, @bitCast(index))));
}
}; };
pub const Iterator = struct { pub const Iterator = struct {
@ -81,6 +87,11 @@ pub fn BlockList(comptime T: type) type {
} }
}; };
pub const Allocation = struct {
ptr: *T,
index: Index,
};
pub fn iterator(list: *const List) Iterator { pub fn iterator(list: *const List) Iterator {
return .{ return .{
.block_index = 0, .block_index = 0,
@ -94,33 +105,50 @@ pub fn BlockList(comptime T: type) type {
return &list.blocks.items[index.block].items[index.index]; return &list.blocks.items[index.block].items[index.index];
} }
pub fn append(list: *List, allocator: Allocator, element: T) !Index { pub fn append(list: *List, allocator: Allocator, element: T) !Allocation {
const result = try list.addOne(allocator);
result.ptr.* = element;
return result;
}
pub fn addOne(list: *List, allocator: Allocator) !Allocation {
try list.ensureCapacity(allocator, list.len + 1); try list.ensureCapacity(allocator, list.len + 1);
const max_allocation = list.blocks.items.len * item_count; const max_allocation = list.blocks.items.len * item_count;
if (list.len < max_allocation) { const result = switch (list.len < max_allocation) {
// Follow the guess true => blk: {
if (list.blocks.items[list.first_block].allocateIndex()) |index| { const block = &list.blocks.items[list.first_block];
list.blocks.items[list.first_block].items[index] = element; if (block.allocateIndex()) |index| {
list.len += 1; const ptr = &block.items[index];
return .{ break :blk Allocation{
.index = index, .ptr = ptr,
.block = @intCast(list.first_block), .index = .{
.index = index,
.block = @intCast(list.first_block),
},
};
} else |_| {
@panic("TODO");
}
},
false => blk: {
const block_index = list.blocks.items.len;
const new_block = list.blocks.addOneAssumeCapacity();
new_block.* = .{};
const index = new_block.allocateIndex() catch unreachable;
const ptr = &new_block.items[index];
break :blk Allocation{
.ptr = ptr,
.index = .{
.index = index,
.block = @intCast(block_index),
},
}; };
} else |_| { },
@panic("TODO"); };
}
} else { list.len += 1;
const block_index = list.blocks.items.len;
const new_block = list.blocks.addOneAssumeCapacity(); return result;
new_block.* = .{};
const index = new_block.allocateIndex() catch unreachable;
new_block.items[index] = element;
list.len += 1;
return .{
.index = index,
.block = @intCast(block_index),
};
}
} }
pub fn ensureCapacity(list: *List, allocator: Allocator, new_capacity: usize) !void { pub fn ensureCapacity(list: *List, allocator: Allocator, new_capacity: usize) !void {
@ -131,6 +159,24 @@ pub fn BlockList(comptime T: type) type {
} }
} }
pub fn indexOf(list: *List, elem: *T) Index {
const address = @intFromPtr(elem);
std.debug.print("Items: {}. Block count: {}\n", .{ list.len, list.blocks.items.len });
for (list.blocks.items, 0..) |*block, block_index| {
const base = @intFromPtr(&block.items[0]);
const top = base + @sizeOf(T) * item_count;
std.debug.print("Bitset: {}. address: 0x{x}. Base: 0x{x}. Top: 0x{x}\n", .{ block.bitset, address, base, top });
if (address >= base and address < top) {
return .{
.block = @intCast(block_index),
.index = @intCast(@divExact(address - base, @sizeOf(T))),
};
}
}
@panic("not found");
}
test "Bitset index allocation" { test "Bitset index allocation" {
const expect = std.testing.expect; const expect = std.testing.expect;
var block = Block{}; var block = Block{};

View File

@ -34,6 +34,9 @@ pub const Token = packed struct(u64) {
fixed_keyword_false = 0x0d, fixed_keyword_false = 0x0d,
fixed_keyword_fn = 0x0e, fixed_keyword_fn = 0x0e,
fixed_keyword_unreachable = 0x0f, fixed_keyword_unreachable = 0x0f,
fixed_keyword_return = 0x10,
keyword_unsigned_integer = 0x1f,
keyword_signed_integer = 0x20,
bang = '!', // 0x21 bang = '!', // 0x21
hash = '#', // 0x23 hash = '#', // 0x23
dollar_sign = '$', // 0x24 dollar_sign = '$', // 0x24
@ -82,6 +85,7 @@ pub const FixedKeyword = enum {
false, false,
@"fn", @"fn",
@"unreachable", @"unreachable",
@"return",
}; };
pub const Result = struct { pub const Result = struct {
@ -109,8 +113,9 @@ pub fn analyze(allocator: Allocator, text: []const u8) !Result {
break; break;
} }
const identifier = text[start_index..][0 .. index - start_index]; // const identifier = text[start_index..][0 .. index - start_index];
std.debug.print("Identifier: {s}\n", .{identifier}); // _ = identifier;
// std.debug.print("Identifier: {s}\n", .{identifier});
if (start_character == 'u' or start_character == 's') { if (start_character == 'u' or start_character == 's') {
var index_integer = start_index + 1; var index_integer = start_index + 1;
@ -119,7 +124,13 @@ pub fn analyze(allocator: Allocator, text: []const u8) !Result {
} }
if (index_integer == index) { if (index_integer == index) {
unreachable; const id: Token.Id = switch (start_character) {
'u' => .keyword_unsigned_integer,
's' => .keyword_signed_integer,
else => unreachable,
};
break :blk id;
} }
} }
@ -127,7 +138,7 @@ pub fn analyze(allocator: Allocator, text: []const u8) !Result {
inline else => |comptime_fixed_keyword| @field(Token.Id, "fixed_keyword_" ++ @tagName(comptime_fixed_keyword)), inline else => |comptime_fixed_keyword| @field(Token.Id, "fixed_keyword_" ++ @tagName(comptime_fixed_keyword)),
} else .identifier; } else .identifier;
}, },
'(', ')', '{', '}', '-', '=', ';', '#', '@', ',' => |operator| blk: { '(', ')', '{', '}', '-', '=', ';', '#', '@', ',', '.' => |operator| blk: {
index += 1; index += 1;
break :blk @enumFromInt(operator); break :blk @enumFromInt(operator);
}, },

File diff suppressed because it is too large Load Diff

View File

@ -14,6 +14,7 @@ const Token = lexical_analyzer.Token;
pub const Result = struct { pub const Result = struct {
nodes: ArrayList(Node), nodes: ArrayList(Node),
node_lists: ArrayList(Node.List),
time: u64, time: u64,
}; };
@ -47,6 +48,11 @@ pub const Node = packed struct(u128) {
assert(index.valid); assert(index.valid);
return index.value; return index.value;
} }
pub fn uniqueInteger(index: Index) u32 {
assert(index.valid);
return index.value;
}
}; };
pub const Range = struct { pub const Range = struct {
@ -81,6 +87,15 @@ pub const Node = packed struct(u128) {
comptime_block_two = 23, comptime_block_two = 23,
block_two = 24, block_two = 24,
@"unreachable" = 25, @"unreachable" = 25,
field_access = 26,
call_one = 27,
comptime_block = 28,
block = 29,
unsigned_integer_type = 30,
signed_integer_type = 31,
main_one = 32,
main_two = 33,
main_zero = 34,
}; };
}; };
@ -109,10 +124,37 @@ const Analyzer = struct {
} }
} }
fn getIdentifier(analyzer: *const Analyzer, token: Token) []const u8 { fn bytes(analyzer: *const Analyzer, token_index: Token.Index) []const u8 {
assert(token.id == .identifier); const token = analyzer.tokens[token_index];
const identifier = analyzer.file[token.start..][0..token.len]; return analyzer.file[token.start..][0..token.len];
return identifier; }
fn symbolDeclaration(analyzer: *Analyzer) !Node.Index {
const first = analyzer.token_i;
assert(analyzer.tokens[first].id == .fixed_keyword_var or analyzer.tokens[first].id == .fixed_keyword_const);
analyzer.token_i += 1;
_ = try analyzer.expectToken(.identifier);
// TODO: type
_ = try analyzer.expectToken(.equal);
const init_node = try analyzer.expression();
_ = try analyzer.expectToken(.semicolon);
// TODO:
const type_node = Node.Index.invalid;
const declaration = Node{
.id = .simple_variable_declaration,
.token = first,
.left = type_node,
.right = init_node,
};
const declaration_init_node = analyzer.nodes.items[init_node.unwrap()];
std.debug.print("Declaration init node: {}\n", .{declaration_init_node});
return analyzer.addNode(declaration);
} }
fn containerMembers(analyzer: *Analyzer) !Members { fn containerMembers(analyzer: *Analyzer) !Members {
@ -121,58 +163,26 @@ const Analyzer = struct {
while (analyzer.token_i < analyzer.tokens.len) { while (analyzer.token_i < analyzer.tokens.len) {
const first = analyzer.token_i; const first = analyzer.token_i;
const member_node: Node = switch (analyzer.tokens[first].id) { const member_node_index: Node.Index = switch (analyzer.tokens[first].id) {
.fixed_keyword_comptime => switch (analyzer.tokens[analyzer.token_i + 1].id) { .fixed_keyword_comptime => switch (analyzer.tokens[analyzer.token_i + 1].id) {
.left_brace => blk: { .left_brace => blk: {
analyzer.token_i += 1; analyzer.token_i += 1;
const comptime_block = try analyzer.block(.{ .is_comptime = true }); const comptime_block = try analyzer.block(.{ .is_comptime = true });
break :blk .{ break :blk try analyzer.addNode(.{
.id = .@"comptime", .id = .@"comptime",
.token = first, .token = first,
.left = comptime_block, .left = comptime_block,
.right = Node.Index.invalid, .right = Node.Index.invalid,
}; });
}, },
else => |foo| std.debug.panic("NI: {s}", .{@tagName(foo)}), else => |foo| @panic(@tagName(foo)),
}, },
.fixed_keyword_const, .fixed_keyword_var => blk: { .fixed_keyword_const, .fixed_keyword_var => try analyzer.symbolDeclaration(),
analyzer.token_i += 1; else => |t| @panic(@tagName(t)),
_ = try analyzer.expectToken(.identifier);
// TODO: type
_ = try analyzer.expectToken(.equal);
// TODO: do this in a function
const init_node = try analyzer.expression();
// const init_node = switch (analyzer.tokens[analyzer.token_i].id) {
// .identifier => unreachable,
// .hash => try analyzer.compilerIntrinsic(),
// .left_parenthesis => try analyzer.function(),
// else => |t| std.debug.panic("NI: {s}", .{@tagName(t)}),
// };
_ = try analyzer.expectToken(.semicolon);
// TODO:
const type_node = Node.Index.invalid;
const top_level_decl = .{
.id = .simple_variable_declaration,
.token = first,
.left = type_node,
.right = init_node,
};
break :blk top_level_decl;
},
.identifier => {
unreachable;
},
else => |t| std.debug.panic("NI: {s}", .{@tagName(t)}),
}; };
const member_index = try analyzer.addNode(member_node); try analyzer.temporal_node_heap.append(analyzer.allocator, member_node_index);
try analyzer.temporal_node_heap.append(analyzer.allocator, member_index);
} }
const members_array = analyzer.temporal_node_heap.items[node_heap_top..]; const members_array = analyzer.temporal_node_heap.items[node_heap_top..];
@ -263,10 +273,12 @@ const Analyzer = struct {
}, },
else => try analyzer.assignExpressionStatement(), else => try analyzer.assignExpressionStatement(),
}, },
.fixed_keyword_unreachable => try analyzer.assignExpressionStatement(), .fixed_keyword_unreachable, .fixed_keyword_return => try analyzer.assignExpressionStatement(),
.fixed_keyword_while => try analyzer.whileStatement(options), .fixed_keyword_while => try analyzer.whileStatement(options),
else => unreachable, .fixed_keyword_const, .fixed_keyword_var => try analyzer.symbolDeclaration(),
else => |t| @panic(@tagName(t)),
}; };
try analyzer.temporal_node_heap.append(analyzer.allocator, statement_index); try analyzer.temporal_node_heap.append(analyzer.allocator, statement_index);
} }
@ -301,7 +313,15 @@ const Analyzer = struct {
.left = statement_array[0], .left = statement_array[0],
.right = statement_array[1], .right = statement_array[1],
}, },
else => |len| std.debug.panic("len: {}", .{len}), else => .{
.id = switch (options.is_comptime) {
true => .comptime_block,
false => .block,
},
.token = left_brace,
.left = try analyzer.nodeList(statement_array),
.right = Node.Index.invalid,
},
}; };
return analyzer.addNode(node); return analyzer.addNode(node);
} }
@ -329,7 +349,7 @@ const Analyzer = struct {
const expression_id: Node.Id = switch (analyzer.tokens[analyzer.token_i].id) { const expression_id: Node.Id = switch (analyzer.tokens[analyzer.token_i].id) {
.semicolon => return expr, .semicolon => return expr,
.equal => .assign, .equal => .assign,
else => unreachable, else => |t| @panic(@tagName(t)),
}; };
const node = Node{ const node = Node{
@ -398,8 +418,8 @@ const Analyzer = struct {
while (analyzer.token_i < analyzer.tokens.len) { while (analyzer.token_i < analyzer.tokens.len) {
const precedence: i32 = switch (analyzer.tokens[analyzer.token_i].id) { const precedence: i32 = switch (analyzer.tokens[analyzer.token_i].id) {
.equal, .semicolon, .right_parenthesis, .right_brace, .comma => -1, .equal, .semicolon, .right_parenthesis, .right_brace, .comma, .period => -1,
else => |foo| std.debug.panic("Foo: ({s}) {}", .{ @tagName(foo), foo }), else => |t| @panic(@tagName(t)),
}; };
if (precedence < minimum_precedence) { if (precedence < minimum_precedence) {
@ -446,6 +466,16 @@ const Analyzer = struct {
}, },
.string_literal, .number_literal, .fixed_keyword_true, .fixed_keyword_false, .hash, .fixed_keyword_unreachable => try analyzer.curlySuffixExpression(), .string_literal, .number_literal, .fixed_keyword_true, .fixed_keyword_false, .hash, .fixed_keyword_unreachable => try analyzer.curlySuffixExpression(),
.fixed_keyword_fn => analyzer.function(), .fixed_keyword_fn => analyzer.function(),
.fixed_keyword_return => try analyzer.addNode(.{
.id = .@"return",
.token = blk: {
const token = analyzer.token_i;
analyzer.token_i += 1;
break :blk token;
},
.left = try analyzer.expression(),
.right = Node.Index.invalid,
}),
// todo:? // todo:?
// .left_brace => try analyzer.block(), // .left_brace => try analyzer.block(),
else => |id| { else => |id| {
@ -492,14 +522,8 @@ const Analyzer = struct {
fn typeExpression(analyzer: *Analyzer) !Node.Index { fn typeExpression(analyzer: *Analyzer) !Node.Index {
return switch (analyzer.tokens[analyzer.token_i].id) { return switch (analyzer.tokens[analyzer.token_i].id) {
.identifier, .fixed_keyword_noreturn, .fixed_keyword_true, .fixed_keyword_false, .hash => try analyzer.errorUnionExpression(), .identifier, .fixed_keyword_noreturn, .fixed_keyword_true, .fixed_keyword_false, .hash, .string_literal, .number_literal, .fixed_keyword_unreachable, .keyword_unsigned_integer, .keyword_signed_integer => try analyzer.errorUnionExpression(),
else => |id| blk: { else => |id| @panic(@tagName(id)),
log.warn("By default, calling errorUnionExpression with {s}", .{@tagName(id)});
const result = try analyzer.errorUnionExpression();
break :blk result;
},
}; };
} }
@ -516,14 +540,17 @@ const Analyzer = struct {
var result = try analyzer.primaryTypeExpression(); var result = try analyzer.primaryTypeExpression();
while (true) { while (true) {
if (analyzer.suffixOperator()) |_| { const suffix_operator = try analyzer.suffixOperator(result);
unreachable; if (suffix_operator.valid) {
result = suffix_operator;
} else { } else {
if (analyzer.tokens[analyzer.token_i].id == .left_parenthesis) { if (analyzer.tokens[analyzer.token_i].id == .left_parenthesis) {
const left_parenthesis = analyzer.token_i;
analyzer.token_i += 1; analyzer.token_i += 1;
var expression_list = ArrayList(Node.Index){}; var expression_list = ArrayList(Node.Index){};
while (analyzer.tokens[analyzer.token_i].id != .right_parenthesis) { while (analyzer.tokens[analyzer.token_i].id != .right_parenthesis) {
std.debug.print("Loop\n", .{});
const parameter = try analyzer.expression(); const parameter = try analyzer.expression();
try expression_list.append(analyzer.allocator, parameter); try expression_list.append(analyzer.allocator, parameter);
analyzer.token_i += @intFromBool(switch (analyzer.tokens[analyzer.token_i].id) { analyzer.token_i += @intFromBool(switch (analyzer.tokens[analyzer.token_i].id) {
@ -534,7 +561,16 @@ const Analyzer = struct {
} }
_ = try analyzer.expectToken(.right_parenthesis); _ = try analyzer.expectToken(.right_parenthesis);
@panic("TODO"); // const is_comma = analyzer.tokens[analyzer.token_i].id == .comma;
return analyzer.addNode(switch (expression_list.items.len) {
0 => .{
.id = .call_one,
.token = left_parenthesis,
.left = result,
.right = Node.Index.invalid,
},
else => |len| std.debug.panic("len: {}", .{len}),
});
} else { } else {
return result; return result;
} }
@ -569,8 +605,8 @@ const Analyzer = struct {
.identifier => switch (analyzer.tokens[token_i + 1].id) { .identifier => switch (analyzer.tokens[token_i + 1].id) {
.colon => unreachable, .colon => unreachable,
else => blk: { else => blk: {
const identifier = analyzer.getIdentifier(token); const identifier = analyzer.bytes(token_i);
std.debug.print("identifier: {s}\n", .{identifier}); // std.debug.print("identifier: {s}\n", .{identifier});
analyzer.token_i += 1; analyzer.token_i += 1;
if (equal(u8, identifier, "_")) { if (equal(u8, identifier, "_")) {
break :blk Node.Index.invalid; break :blk Node.Index.invalid;
@ -594,20 +630,55 @@ const Analyzer = struct {
.right = Node.Index.invalid, .right = Node.Index.invalid,
}), }),
.hash => analyzer.compilerIntrinsic(), .hash => analyzer.compilerIntrinsic(),
.keyword_unsigned_integer, .keyword_signed_integer => |signedness| try analyzer.addNode(.{
.id = switch (signedness) {
.keyword_unsigned_integer => .unsigned_integer_type,
.keyword_signed_integer => .signed_integer_type,
else => unreachable,
},
.token = blk: {
analyzer.token_i += 1;
break :blk token_i;
},
.left = @bitCast(@as(u32, std.fmt.parseInt(u16, analyzer.bytes(token_i)[1..], 10) catch unreachable)),
.right = Node.Index.invalid,
}),
else => |foo| { else => |foo| {
switch (foo) { switch (foo) {
.identifier => std.debug.panic("{s}: {s}", .{ @tagName(foo), analyzer.getIdentifier(analyzer.tokens[token_i]) }), .identifier => std.debug.panic("{s}: {s}", .{ @tagName(foo), analyzer.bytes(token_i) }),
else => std.debug.panic("{s}", .{@tagName(foo)}), else => @panic(@tagName(foo)),
} }
}, },
}; };
} }
// TODO: // TODO:
fn suffixOperator(analyzer: *Analyzer) ?bool { fn suffixOperator(analyzer: *Analyzer, left: Node.Index) !Node.Index {
_ = analyzer; const token = analyzer.tokens[analyzer.token_i];
return switch (token.id) {
return null; .left_bracket => unreachable,
.period => switch (analyzer.tokens[analyzer.token_i + 1].id) {
.identifier => analyzer.addNode(.{
.id = .field_access,
.token = blk: {
const main_token = analyzer.token_i;
analyzer.token_i += 1;
break :blk main_token;
},
.left = left,
.right = blk: {
//TODO ???
const right_token = analyzer.token_i;
analyzer.token_i += 1;
const result: Node.Index = @bitCast(right_token);
std.debug.print("WARNING: rhs has node index {} but it's token #{}\n", .{ result, right_token });
break :blk result;
},
}),
else => |t| @panic(@tagName(t)),
},
else => Node.Index.invalid,
};
} }
fn addNode(analyzer: *Analyzer, node: Node) !Node.Index { fn addNode(analyzer: *Analyzer, node: Node) !Node.Index {
@ -618,27 +689,23 @@ const Analyzer = struct {
.value = @intCast(index), .value = @intCast(index),
}; };
} }
fn nodeList(analyzer: *Analyzer, input: []const Node.Index) !Node.Index {
const index = analyzer.node_lists.items.len;
var new_node_list = try ArrayList(Node.Index).initCapacity(analyzer.allocator, input.len);
try new_node_list.appendSlice(analyzer.allocator, input);
try analyzer.node_lists.append(analyzer.allocator, new_node_list);
return .{
.value = @intCast(index),
};
}
}; };
const Members = struct { const Members = struct {
len: usize, len: usize,
left: Node.Index, left: Node.Index,
right: Node.Index, right: Node.Index,
pub fn toRange(members: Members) Node.Range {
return switch (members.len) {
0 => unreachable,
1 => .{
.start = members.left.value,
.end = members.left.value,
},
2 => .{
.start = members.left.value,
.end = members.right.value,
},
else => unreachable,
};
}
}; };
pub fn analyze(allocator: Allocator, tokens: []const Token, file: []const u8) !Result { pub fn analyze(allocator: Allocator, tokens: []const Token, file: []const u8) !Result {
@ -657,10 +724,22 @@ pub fn analyze(allocator: Allocator, tokens: []const Token, file: []const u8) !R
assert(node_index.value == 0); assert(node_index.value == 0);
assert(node_index.valid); assert(node_index.valid);
const members = try analyzer.containerMembers(); const members = try analyzer.containerMembers();
const member_range = members.toRange();
analyzer.nodes.items[0].left = .{ .value = @intCast(member_range.start) }; switch (members.len) {
analyzer.nodes.items[0].right = .{ .value = @intCast(member_range.end) }; 0 => unreachable,
1 => {
analyzer.nodes.items[0].id = .main_one;
analyzer.nodes.items[0].left = members.left;
},
2 => {
analyzer.nodes.items[0].id = .main_two;
analyzer.nodes.items[0].left = members.left;
analyzer.nodes.items[0].right = members.right;
},
else => unreachable,
}
const end = std.time.Instant.now() catch unreachable; const end = std.time.Instant.now() catch unreachable;
@ -668,6 +747,7 @@ pub fn analyze(allocator: Allocator, tokens: []const Token, file: []const u8) !R
return .{ return .{
.nodes = analyzer.nodes, .nodes = analyzer.nodes,
.node_lists = analyzer.node_lists,
.time = end.since(start), .time = end.since(start),
}; };
} }

View File

@ -1,8 +1,6 @@
const std = @import("std"); const std = @import("std");
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
pub const first = "src/test/main.b";
pub fn readFile(allocator: Allocator, file_relative_path: []const u8) ![]const u8 { pub fn readFile(allocator: Allocator, file_relative_path: []const u8) ![]const u8 {
const file = try std.fs.cwd().readFileAlloc(allocator, file_relative_path, std.math.maxInt(usize)); const file = try std.fs.cwd().readFileAlloc(allocator, file_relative_path, std.math.maxInt(usize));
return file; return file;

View File

@ -5,7 +5,7 @@ const assert = std.debug.assert;
const Compilation = @import("Compilation.zig"); const Compilation = @import("Compilation.zig");
pub const seed = std.math.maxInt(u64); pub const seed = std.math.maxInt(u64);
const default_src_file = "src/test/main.b"; const default_src_file = "src/test/main.nat";
pub fn main() !void { pub fn main() !void {
try singleCompilation(default_src_file); try singleCompilation(default_src_file);

View File

@ -1,3 +1,3 @@
const main = fn() i32 { const main = fn() s32 {
return 0; return 0;
}; };