diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2018-07-20 01:46:49 -0400 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2018-07-20 01:46:49 -0400 |
| commit | f5a67dba08b81c7109c52f6ad957aefdd646b56e (patch) | |
| tree | fa89431ab0e1c1149ff86cde8ab6c4aff1c79dcb /src-self-hosted | |
| parent | 33fbd8c1d333922efa696b5b9384859a2664f8de (diff) | |
| download | zig-f5a67dba08b81c7109c52f6ad957aefdd646b56e.tar.gz zig-f5a67dba08b81c7109c52f6ad957aefdd646b56e.zip | |
self-hosted: implicit cast comptime ints to other ints
we now have successful exit codes from main linking
against libc
Diffstat (limited to 'src-self-hosted')
| -rw-r--r-- | src-self-hosted/ir.zig | 39 | ||||
| -rw-r--r-- | src-self-hosted/value.zig | 49 |
2 files changed, 87 insertions, 1 deletions
diff --git a/src-self-hosted/ir.zig b/src-self-hosted/ir.zig index 259c0a99f4..04023980e6 100644 --- a/src-self-hosted/ir.zig +++ b/src-self-hosted/ir.zig @@ -139,7 +139,15 @@ pub const Inst = struct { } } + fn copyVal(base: *Inst, comp: *Compilation) !*Value { + if (base.parent.?.ref_count == 0) { + return base.val.KnownValue.derefAndCopy(comp); + } + return base.val.KnownValue.copy(comp); + } + fn getAsParam(param: *Inst) !*Inst { + param.ref_count -= 1; const child = param.child orelse return error.SemanticAnalysisFailed; switch (child.val) { IrVal.Unknown => return error.SemanticAnalysisFailed, @@ -1715,8 +1723,37 @@ const Analyze = struct { // } //} + // cast from comptime-known integer to another integer where the value fits + if (target.isCompTime() and (from_type.id == Type.Id.Int or from_type.id == Type.Id.ComptimeInt)) cast: { + const target_val = target.val.KnownValue; + const from_int = &target_val.cast(Value.Int).?.big_int; + const fits = fits: { + if (dest_type.cast(Type.ComptimeInt)) |ctint| { + break :fits true; + } + if (dest_type.cast(Type.Int)) |int| { + break :fits (from_int.positive or from_int.eqZero() or int.key.is_signed) and + int.key.bit_count >= from_int.bitcount(); + } + break :cast; + }; + if (!fits) { + try ira.addCompileError( + source_instr.span, + "integer value '{}' cannot be stored in type '{}'", + from_int, + dest_type.name, + ); + return error.SemanticAnalysisFailed; + } + + const new_val = try target.copyVal(ira.irb.comp); + new_val.setType(dest_type, ira.irb.comp); + return ira.irb.buildConstValue(source_instr.scope, source_instr.span, new_val); + } + // cast from number literal to another type - //// cast from number literal to *const integer + // cast from number literal to *const integer //if (actual_type->id == TypeTableEntryIdComptimeFloat || // actual_type->id == TypeTableEntryIdComptimeInt) //{ diff --git a/src-self-hosted/value.zig b/src-self-hosted/value.zig index b8563b00b8..16c1488838 100644 --- a/src-self-hosted/value.zig +++ b/src-self-hosted/value.zig @@ -5,6 +5,7 @@ const Compilation = @import("compilation.zig").Compilation; const ObjectFile = @import("codegen.zig").ObjectFile; const llvm = @import("llvm.zig"); const Buffer = std.Buffer; +const assert = std.debug.assert; /// Values are ref-counted, heap-allocated, and copy-on-write /// If there is only 1 ref then write need not copy @@ -34,6 +35,12 @@ pub const Value = struct { } } + pub fn setType(base: *Value, new_type: *Type, comp: *Compilation) void { + base.typeof.base.deref(comp); + new_type.base.ref(); + base.typeof = new_type; + } + pub fn getRef(base: *Value) *Value { base.ref(); return base; @@ -60,6 +67,28 @@ pub const Value = struct { } } + pub fn derefAndCopy(self: *Value, comp: *Compilation) (error{OutOfMemory}!*Value) { + if (self.ref_count.get() == 1) { + // ( ͡° ͜ʖ ͡°) + return self; + } + + assert(self.ref_count.decr() != 1); + return self.copy(comp); + } + + pub fn copy(base: *Value, comp: *Compilation) (error{OutOfMemory}!*Value) { + switch (base.id) { + Id.Type => unreachable, + Id.Fn => unreachable, + Id.Void => unreachable, + Id.Bool => unreachable, + Id.NoReturn => unreachable, + Id.Ptr => unreachable, + Id.Int => return &(try @fieldParentPtr(Int, "base", base).copy(comp)).base, + } + } + pub const Id = enum { Type, Fn, @@ -256,6 +285,26 @@ pub const Value = struct { } } + pub fn copy(old: *Int, comp: *Compilation) !*Int { + old.base.typeof.base.ref(); + errdefer old.base.typeof.base.deref(comp); + + const new = try comp.gpa().create(Value.Int{ + .base = Value{ + .id = Value.Id.Int, + .typeof = old.base.typeof, + .ref_count = std.atomic.Int(usize).init(1), + }, + .big_int = undefined, + }); + errdefer comp.gpa().destroy(new); + + new.big_int = try old.big_int.clone(); + errdefer new.big_int.deinit(); + + return new; + } + pub fn destroy(self: *Int, comp: *Compilation) void { self.big_int.deinit(); comp.gpa().destroy(self); |
