Skip to content

Commit

Permalink
Merge pull request #1266 from ziglang/self-hosted-libc-hello-world
Browse files Browse the repository at this point in the history
Self hosted libc hello world
  • Loading branch information
andrewrk committed Jul 24, 2018
2 parents 99153ac + 72599d4 commit 10bdf73
Show file tree
Hide file tree
Showing 46 changed files with 5,624 additions and 1,204 deletions.
9 changes: 9 additions & 0 deletions CMakeLists.txt
Expand Up @@ -426,6 +426,7 @@ set(ZIG_SOURCES
)
set(ZIG_CPP_SOURCES
"${CMAKE_SOURCE_DIR}/src/zig_llvm.cpp"
"${CMAKE_SOURCE_DIR}/src/windows_sdk.cpp"
)

set(ZIG_STD_FILES
Expand Down Expand Up @@ -489,6 +490,7 @@ set(ZIG_STD_FILES
"math/atan.zig"
"math/atan2.zig"
"math/atanh.zig"
"math/big/index.zig"
"math/big/int.zig"
"math/cbrt.zig"
"math/ceil.zig"
Expand Down Expand Up @@ -566,8 +568,14 @@ set(ZIG_STD_FILES
"os/linux/x86_64.zig"
"os/path.zig"
"os/time.zig"
"os/windows/advapi32.zig"
"os/windows/error.zig"
"os/windows/index.zig"
"os/windows/kernel32.zig"
"os/windows/ole32.zig"
"os/windows/shell32.zig"
"os/windows/shlwapi.zig"
"os/windows/user32.zig"
"os/windows/util.zig"
"os/zen.zig"
"rand/index.zig"
Expand Down Expand Up @@ -616,6 +624,7 @@ set(ZIG_STD_FILES
"zig/ast.zig"
"zig/index.zig"
"zig/parse.zig"
"zig/parse_string_literal.zig"
"zig/render.zig"
"zig/tokenizer.zig"
)
Expand Down
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -21,19 +21,19 @@ clarity.
* Compatible with C libraries with no wrapper necessary. Directly include
C .h files and get access to the functions and symbols therein.
* Provides standard library which competes with the C standard library and is
always compiled against statically in source form. Compile units do not
always compiled against statically in source form. Zig binaries do not
depend on libc unless explicitly linked.
* Nullable type instead of null pointers.
* Optional type instead of null pointers.
* Safe unions, tagged unions, and C ABI compatible unions.
* Generics so that one can write efficient data structures that work for any
data type.
* No header files required. Top level declarations are entirely
order-independent.
* Compile-time code execution. Compile-time reflection.
* Partial compile-time function evaluation with eliminates the need for
* Partial compile-time function evaluation which eliminates the need for
a preprocessor or macros.
* The binaries produced by Zig have complete debugging information so you can,
for example, use GDB or MSVC to debug your software.
for example, use GDB, MSVC, or LLDB to debug your software.
* Built-in unit tests with `zig test`.
* Friendly toward package maintainers. Reproducible build, bootstrapping
process carefully documented. Issues filed by package maintainers are
Expand Down
1 change: 1 addition & 0 deletions src-self-hosted/c.zig
Expand Up @@ -4,4 +4,5 @@ pub use @cImport({
@cInclude("inttypes.h");
@cInclude("config.h");
@cInclude("zig_llvm.h");
@cInclude("windows_sdk.h");
});
68 changes: 68 additions & 0 deletions src-self-hosted/c_int.zig
@@ -0,0 +1,68 @@
pub const CInt = struct {
id: Id,
zig_name: []const u8,
c_name: []const u8,
is_signed: bool,

pub const Id = enum {
Short,
UShort,
Int,
UInt,
Long,
ULong,
LongLong,
ULongLong,
};

pub const list = []CInt{
CInt{
.id = Id.Short,
.zig_name = "c_short",
.c_name = "short",
.is_signed = true,
},
CInt{
.id = Id.UShort,
.zig_name = "c_ushort",
.c_name = "unsigned short",
.is_signed = false,
},
CInt{
.id = Id.Int,
.zig_name = "c_int",
.c_name = "int",
.is_signed = true,
},
CInt{
.id = Id.UInt,
.zig_name = "c_uint",
.c_name = "unsigned int",
.is_signed = false,
},
CInt{
.id = Id.Long,
.zig_name = "c_long",
.c_name = "long",
.is_signed = true,
},
CInt{
.id = Id.ULong,
.zig_name = "c_ulong",
.c_name = "unsigned long",
.is_signed = false,
},
CInt{
.id = Id.LongLong,
.zig_name = "c_longlong",
.c_name = "long long",
.is_signed = true,
},
CInt{
.id = Id.ULongLong,
.zig_name = "c_ulonglong",
.c_name = "unsigned long long",
.is_signed = false,
},
};
};
8 changes: 5 additions & 3 deletions src-self-hosted/codegen.zig
Expand Up @@ -15,7 +15,7 @@ pub async fn renderToLlvm(comp: *Compilation, fn_val: *Value.Fn, code: *ir.Code)
defer fn_val.base.deref(comp);
defer code.destroy(comp.gpa());

var output_path = try await (async comp.createRandomOutputPath(comp.target.oFileExt()) catch unreachable);
var output_path = try await (async comp.createRandomOutputPath(comp.target.objFileExt()) catch unreachable);
errdefer output_path.deinit();

const llvm_handle = try comp.event_loop_local.getAnyLlvmContext();
Expand Down Expand Up @@ -78,6 +78,7 @@ pub async fn renderToLlvm(comp: *Compilation, fn_val: *Value.Fn, code: *ir.Code)
.dibuilder = dibuilder,
.context = context,
.lock = event.Lock.init(comp.loop),
.arena = &code.arena.allocator,
};

try renderToLlvmModule(&ofile, fn_val, code);
Expand Down Expand Up @@ -139,6 +140,7 @@ pub const ObjectFile = struct {
dibuilder: *llvm.DIBuilder,
context: llvm.ContextRef,
lock: event.Lock,
arena: *std.mem.Allocator,

fn gpa(self: *ObjectFile) *std.mem.Allocator {
return self.comp.gpa();
Expand All @@ -147,7 +149,7 @@ pub const ObjectFile = struct {

pub fn renderToLlvmModule(ofile: *ObjectFile, fn_val: *Value.Fn, code: *ir.Code) !void {
// TODO audit more of codegen.cpp:fn_llvm_value and port more logic
const llvm_fn_type = try fn_val.base.typeof.getLlvmType(ofile);
const llvm_fn_type = try fn_val.base.typ.getLlvmType(ofile.arena, ofile.context);
const llvm_fn = llvm.AddFunction(
ofile.module,
fn_val.symbol_name.ptr(),
Expand All @@ -165,7 +167,7 @@ pub fn renderToLlvmModule(ofile: *ObjectFile, fn_val: *Value.Fn, code: *ir.Code)
// try addLLVMFnAttrInt(ofile, llvm_fn, "alignstack", align_stack);
//}

const fn_type = fn_val.base.typeof.cast(Type.Fn).?;
const fn_type = fn_val.base.typ.cast(Type.Fn).?;

try addLLVMFnAttr(ofile, llvm_fn, "nounwind");
//add_uwtable_attr(g, fn_table_entry->llvm_value);
Expand Down

0 comments on commit 10bdf73

Please sign in to comment.