2017-12-22 21:29:39 -08:00
|
|
|
const std = @import("std");
|
|
|
|
const os = std.os;
|
|
|
|
const io = std.io;
|
|
|
|
const mem = std.mem;
|
|
|
|
const Buffer = std.Buffer;
|
|
|
|
const llvm = @import("llvm.zig");
|
|
|
|
const c = @import("c.zig");
|
|
|
|
const builtin = @import("builtin");
|
|
|
|
const Target = @import("target.zig").Target;
|
|
|
|
const warn = std.debug.warn;
|
|
|
|
const Tokenizer = @import("tokenizer.zig").Tokenizer;
|
|
|
|
const Token = @import("tokenizer.zig").Token;
|
|
|
|
const Parser = @import("parser.zig").Parser;
|
|
|
|
const ArrayList = std.ArrayList;
|
|
|
|
|
|
|
|
pub const Module = struct {
|
|
|
|
allocator: &mem.Allocator,
|
|
|
|
name: Buffer,
|
|
|
|
root_src_path: ?[]const u8,
|
|
|
|
module: llvm.ModuleRef,
|
|
|
|
context: llvm.ContextRef,
|
|
|
|
builder: llvm.BuilderRef,
|
|
|
|
target: Target,
|
|
|
|
build_mode: builtin.Mode,
|
|
|
|
zig_lib_dir: []const u8,
|
|
|
|
|
|
|
|
version_major: u32,
|
|
|
|
version_minor: u32,
|
|
|
|
version_patch: u32,
|
|
|
|
|
|
|
|
linker_script: ?[]const u8,
|
|
|
|
cache_dir: []const u8,
|
|
|
|
libc_lib_dir: ?[]const u8,
|
|
|
|
libc_static_lib_dir: ?[]const u8,
|
|
|
|
libc_include_dir: ?[]const u8,
|
|
|
|
msvc_lib_dir: ?[]const u8,
|
|
|
|
kernel32_lib_dir: ?[]const u8,
|
|
|
|
dynamic_linker: ?[]const u8,
|
|
|
|
out_h_path: ?[]const u8,
|
|
|
|
|
|
|
|
is_test: bool,
|
|
|
|
each_lib_rpath: bool,
|
|
|
|
strip: bool,
|
|
|
|
is_static: bool,
|
|
|
|
linker_rdynamic: bool,
|
|
|
|
|
|
|
|
clang_argv: []const []const u8,
|
|
|
|
llvm_argv: []const []const u8,
|
|
|
|
lib_dirs: []const []const u8,
|
|
|
|
rpath_list: []const []const u8,
|
|
|
|
assembly_files: []const []const u8,
|
|
|
|
link_objects: []const []const u8,
|
|
|
|
|
|
|
|
windows_subsystem_windows: bool,
|
|
|
|
windows_subsystem_console: bool,
|
|
|
|
|
|
|
|
link_libs_list: ArrayList(&LinkLib),
|
|
|
|
libc_link_lib: ?&LinkLib,
|
|
|
|
|
|
|
|
err_color: ErrColor,
|
|
|
|
|
|
|
|
verbose_tokenize: bool,
|
|
|
|
verbose_ast_tree: bool,
|
|
|
|
verbose_ast_fmt: bool,
|
|
|
|
verbose_cimport: bool,
|
|
|
|
verbose_ir: bool,
|
|
|
|
verbose_llvm_ir: bool,
|
|
|
|
verbose_link: bool,
|
|
|
|
|
|
|
|
darwin_frameworks: []const []const u8,
|
|
|
|
darwin_version_min: DarwinVersionMin,
|
|
|
|
|
|
|
|
test_filters: []const []const u8,
|
|
|
|
test_name_prefix: ?[]const u8,
|
|
|
|
|
|
|
|
emit_file_type: Emit,
|
|
|
|
|
|
|
|
kind: Kind,
|
|
|
|
|
|
|
|
pub const DarwinVersionMin = union(enum) {
|
|
|
|
None,
|
|
|
|
MacOS: []const u8,
|
|
|
|
Ios: []const u8,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const Kind = enum {
|
|
|
|
Exe,
|
|
|
|
Lib,
|
|
|
|
Obj,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const ErrColor = enum {
|
|
|
|
Auto,
|
|
|
|
Off,
|
|
|
|
On,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const LinkLib = struct {
|
|
|
|
name: []const u8,
|
|
|
|
path: ?[]const u8,
|
|
|
|
/// the list of symbols we depend on from this lib
|
|
|
|
symbols: ArrayList([]u8),
|
|
|
|
provided_explicitly: bool,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const Emit = enum {
|
|
|
|
Binary,
|
|
|
|
Assembly,
|
|
|
|
LlvmIr,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub fn create(allocator: &mem.Allocator, name: []const u8, root_src_path: ?[]const u8, target: &const Target,
|
|
|
|
kind: Kind, build_mode: builtin.Mode, zig_lib_dir: []const u8, cache_dir: []const u8) -> %&Module
|
|
|
|
{
|
2018-01-07 13:51:46 -08:00
|
|
|
var name_buffer = try Buffer.init(allocator, name);
|
2017-12-22 21:29:39 -08:00
|
|
|
%defer name_buffer.deinit();
|
|
|
|
|
|
|
|
const context = c.LLVMContextCreate() ?? return error.OutOfMemory;
|
|
|
|
%defer c.LLVMContextDispose(context);
|
|
|
|
|
|
|
|
const module = c.LLVMModuleCreateWithNameInContext(name_buffer.ptr(), context) ?? return error.OutOfMemory;
|
|
|
|
%defer c.LLVMDisposeModule(module);
|
|
|
|
|
|
|
|
const builder = c.LLVMCreateBuilderInContext(context) ?? return error.OutOfMemory;
|
|
|
|
%defer c.LLVMDisposeBuilder(builder);
|
|
|
|
|
2018-01-07 13:51:46 -08:00
|
|
|
const module_ptr = try allocator.create(Module);
|
2017-12-22 21:29:39 -08:00
|
|
|
%defer allocator.destroy(module_ptr);
|
|
|
|
|
|
|
|
*module_ptr = Module {
|
|
|
|
.allocator = allocator,
|
|
|
|
.name = name_buffer,
|
|
|
|
.root_src_path = root_src_path,
|
|
|
|
.module = module,
|
|
|
|
.context = context,
|
|
|
|
.builder = builder,
|
|
|
|
.target = *target,
|
|
|
|
.kind = kind,
|
|
|
|
.build_mode = build_mode,
|
|
|
|
.zig_lib_dir = zig_lib_dir,
|
|
|
|
.cache_dir = cache_dir,
|
|
|
|
|
|
|
|
.version_major = 0,
|
|
|
|
.version_minor = 0,
|
|
|
|
.version_patch = 0,
|
|
|
|
|
|
|
|
.verbose_tokenize = false,
|
|
|
|
.verbose_ast_tree = false,
|
|
|
|
.verbose_ast_fmt = false,
|
|
|
|
.verbose_cimport = false,
|
|
|
|
.verbose_ir = false,
|
|
|
|
.verbose_llvm_ir = false,
|
|
|
|
.verbose_link = false,
|
|
|
|
|
|
|
|
.linker_script = null,
|
|
|
|
.libc_lib_dir = null,
|
|
|
|
.libc_static_lib_dir = null,
|
|
|
|
.libc_include_dir = null,
|
|
|
|
.msvc_lib_dir = null,
|
|
|
|
.kernel32_lib_dir = null,
|
|
|
|
.dynamic_linker = null,
|
|
|
|
.out_h_path = null,
|
|
|
|
.is_test = false,
|
|
|
|
.each_lib_rpath = false,
|
|
|
|
.strip = false,
|
|
|
|
.is_static = false,
|
|
|
|
.linker_rdynamic = false,
|
|
|
|
.clang_argv = [][]const u8{},
|
|
|
|
.llvm_argv = [][]const u8{},
|
|
|
|
.lib_dirs = [][]const u8{},
|
|
|
|
.rpath_list = [][]const u8{},
|
|
|
|
.assembly_files = [][]const u8{},
|
|
|
|
.link_objects = [][]const u8{},
|
|
|
|
.windows_subsystem_windows = false,
|
|
|
|
.windows_subsystem_console = false,
|
|
|
|
.link_libs_list = ArrayList(&LinkLib).init(allocator),
|
|
|
|
.libc_link_lib = null,
|
|
|
|
.err_color = ErrColor.Auto,
|
|
|
|
.darwin_frameworks = [][]const u8{},
|
|
|
|
.darwin_version_min = DarwinVersionMin.None,
|
|
|
|
.test_filters = [][]const u8{},
|
|
|
|
.test_name_prefix = null,
|
|
|
|
.emit_file_type = Emit.Binary,
|
|
|
|
};
|
|
|
|
return module_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dump(self: &Module) {
|
|
|
|
c.LLVMDumpModule(self.module);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn destroy(self: &Module) {
|
|
|
|
c.LLVMDisposeBuilder(self.builder);
|
|
|
|
c.LLVMDisposeModule(self.module);
|
|
|
|
c.LLVMContextDispose(self.context);
|
|
|
|
self.name.deinit();
|
|
|
|
|
|
|
|
self.allocator.destroy(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn build(self: &Module) -> %void {
|
2017-12-26 16:44:08 -08:00
|
|
|
if (self.llvm_argv.len != 0) {
|
2018-01-07 13:51:46 -08:00
|
|
|
var c_compatible_args = try std.cstr.NullTerminated2DArray.fromSlices(self.allocator,
|
2017-12-26 16:44:08 -08:00
|
|
|
[][]const []const u8 { [][]const u8{"zig (LLVM option parsing)"}, self.llvm_argv, });
|
|
|
|
defer c_compatible_args.deinit();
|
|
|
|
c.ZigLLVMParseCommandLineOptions(self.llvm_argv.len + 1, c_compatible_args.ptr);
|
|
|
|
}
|
|
|
|
|
2017-12-22 21:29:39 -08:00
|
|
|
const root_src_path = self.root_src_path ?? @panic("TODO handle null root src path");
|
2018-01-07 14:28:20 -08:00
|
|
|
const root_src_real_path = os.path.real(self.allocator, root_src_path) catch |err| {
|
2018-01-07 13:51:46 -08:00
|
|
|
try printError("unable to get real path '{}': {}", root_src_path, err);
|
2017-12-22 21:29:39 -08:00
|
|
|
return err;
|
|
|
|
};
|
|
|
|
%defer self.allocator.free(root_src_real_path);
|
|
|
|
|
2018-01-07 14:28:20 -08:00
|
|
|
const source_code = io.readFileAllocExtra(root_src_real_path, self.allocator, 3) catch |err| {
|
2018-01-07 13:51:46 -08:00
|
|
|
try printError("unable to open '{}': {}", root_src_real_path, err);
|
2017-12-22 21:29:39 -08:00
|
|
|
return err;
|
|
|
|
};
|
|
|
|
%defer self.allocator.free(source_code);
|
2017-12-26 22:17:33 -08:00
|
|
|
source_code[source_code.len - 3] = '\n';
|
|
|
|
source_code[source_code.len - 2] = '\n';
|
|
|
|
source_code[source_code.len - 1] = '\n';
|
2017-12-22 21:29:39 -08:00
|
|
|
|
|
|
|
warn("====input:====\n");
|
|
|
|
|
|
|
|
warn("{}", source_code);
|
|
|
|
|
|
|
|
warn("====tokenization:====\n");
|
|
|
|
{
|
|
|
|
var tokenizer = Tokenizer.init(source_code);
|
|
|
|
while (true) {
|
|
|
|
const token = tokenizer.next();
|
|
|
|
tokenizer.dump(token);
|
|
|
|
if (token.id == Token.Id.Eof) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
warn("====parse:====\n");
|
|
|
|
|
|
|
|
var tokenizer = Tokenizer.init(source_code);
|
|
|
|
var parser = Parser.init(&tokenizer, self.allocator, root_src_real_path);
|
|
|
|
defer parser.deinit();
|
|
|
|
|
2018-01-07 13:51:46 -08:00
|
|
|
const root_node = try parser.parse();
|
2017-12-22 21:29:39 -08:00
|
|
|
defer parser.freeAst(root_node);
|
|
|
|
|
2018-01-07 13:51:46 -08:00
|
|
|
var stderr_file = try std.io.getStdErr();
|
2017-12-22 21:29:39 -08:00
|
|
|
var stderr_file_out_stream = std.io.FileOutStream.init(&stderr_file);
|
|
|
|
const out_stream = &stderr_file_out_stream.stream;
|
2018-01-07 13:51:46 -08:00
|
|
|
try parser.renderAst(out_stream, root_node);
|
2017-12-22 21:29:39 -08:00
|
|
|
|
|
|
|
warn("====fmt:====\n");
|
2018-01-07 13:51:46 -08:00
|
|
|
try parser.renderSource(out_stream, root_node);
|
2017-12-22 21:29:39 -08:00
|
|
|
|
|
|
|
warn("====ir:====\n");
|
|
|
|
warn("TODO\n\n");
|
|
|
|
|
|
|
|
warn("====llvm ir:====\n");
|
|
|
|
self.dump();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn link(self: &Module, out_file: ?[]const u8) -> %void {
|
|
|
|
warn("TODO link");
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn addLinkLib(self: &Module, name: []const u8, provided_explicitly: bool) -> %&LinkLib {
|
|
|
|
const is_libc = mem.eql(u8, name, "c");
|
|
|
|
|
|
|
|
if (is_libc) {
|
|
|
|
if (self.libc_link_lib) |libc_link_lib| {
|
|
|
|
return libc_link_lib;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (self.link_libs_list.toSliceConst()) |existing_lib| {
|
|
|
|
if (mem.eql(u8, name, existing_lib.name)) {
|
|
|
|
return existing_lib;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-07 13:51:46 -08:00
|
|
|
const link_lib = try self.allocator.create(LinkLib);
|
2017-12-22 21:29:39 -08:00
|
|
|
*link_lib = LinkLib {
|
|
|
|
.name = name,
|
|
|
|
.path = null,
|
|
|
|
.provided_explicitly = provided_explicitly,
|
|
|
|
.symbols = ArrayList([]u8).init(self.allocator),
|
|
|
|
};
|
2018-01-07 13:51:46 -08:00
|
|
|
try self.link_libs_list.append(link_lib);
|
2017-12-22 21:29:39 -08:00
|
|
|
if (is_libc) {
|
|
|
|
self.libc_link_lib = link_lib;
|
|
|
|
}
|
|
|
|
return link_lib;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
fn printError(comptime format: []const u8, args: ...) -> %void {
|
2018-01-07 13:51:46 -08:00
|
|
|
var stderr_file = try std.io.getStdErr();
|
2017-12-22 21:29:39 -08:00
|
|
|
var stderr_file_out_stream = std.io.FileOutStream.init(&stderr_file);
|
|
|
|
const out_stream = &stderr_file_out_stream.stream;
|
2018-01-07 13:51:46 -08:00
|
|
|
try out_stream.print(format, args);
|
2017-12-22 21:29:39 -08:00
|
|
|
}
|