IR: progress toward compiling standard library

* comptime fn call
 * is_comptime doesn't count as an instruction dependency
 * update more std code to latest zig
This commit is contained in:
Andrew Kelley 2016-12-31 17:10:29 -05:00
parent 5f89393acb
commit 69132bdeda
18 changed files with 101 additions and 81 deletions

View File

@ -123,7 +123,7 @@ MultiplyOperator = "*" | "/" | "%" | "**" | "*%"
PrefixOpExpression = PrefixOp PrefixOpExpression | SuffixOpExpression
SuffixOpExpression = PrimaryExpression option(FnCallExpression | ArrayAccessExpression | FieldAccessExpression | SliceExpression)
SuffixOpExpression = option("inline") PrimaryExpression option(FnCallExpression | ArrayAccessExpression | FieldAccessExpression | SliceExpression)
FieldAccessExpression = "." Symbol

View File

@ -445,6 +445,7 @@ struct AstNodeFnCallExpr {
AstNode *fn_ref_expr;
ZigList<AstNode *> params;
bool is_builtin;
bool is_comptime;
};
struct AstNodeArrayAccessExpr {
@ -1669,7 +1670,7 @@ struct IrInstructionCall {
FnTableEntry *fn_entry;
size_t arg_count;
IrInstruction **args;
bool is_inline;
bool is_comptime;
LLVMValueRef tmp_ptr;
};

View File

@ -808,13 +808,15 @@ static IrInstruction *ir_build_enum_field_ptr_from(IrBuilder *irb, IrInstruction
}
static IrInstruction *ir_build_call(IrBuilder *irb, Scope *scope, AstNode *source_node,
FnTableEntry *fn_entry, IrInstruction *fn_ref, size_t arg_count, IrInstruction **args)
FnTableEntry *fn_entry, IrInstruction *fn_ref, size_t arg_count, IrInstruction **args,
bool is_comptime)
{
IrInstructionCall *call_instruction = ir_build_instruction<IrInstructionCall>(irb, scope, source_node);
call_instruction->fn_entry = fn_entry;
call_instruction->fn_ref = fn_ref;
call_instruction->arg_count = arg_count;
call_instruction->is_comptime = is_comptime;
call_instruction->args = args;
call_instruction->arg_count = arg_count;
if (fn_ref)
ir_ref_instruction(fn_ref);
@ -825,10 +827,11 @@ static IrInstruction *ir_build_call(IrBuilder *irb, Scope *scope, AstNode *sourc
}
static IrInstruction *ir_build_call_from(IrBuilder *irb, IrInstruction *old_instruction,
FnTableEntry *fn_entry, IrInstruction *fn_ref, size_t arg_count, IrInstruction **args)
FnTableEntry *fn_entry, IrInstruction *fn_ref, size_t arg_count, IrInstruction **args,
bool is_comptime)
{
IrInstruction *new_instruction = ir_build_call(irb, old_instruction->scope,
old_instruction->source_node, fn_entry, fn_ref, arg_count, args);
old_instruction->source_node, fn_entry, fn_ref, arg_count, args, is_comptime);
ir_link_new_instruction(new_instruction, old_instruction);
return new_instruction;
}
@ -1950,16 +1953,12 @@ static IrInstruction *ir_build_int_to_enum(IrBuilder *irb, Scope *scope, AstNode
}
static IrInstruction *ir_instruction_br_get_dep(IrInstructionBr *instruction, size_t index) {
switch (index) {
case 0: return instruction->is_comptime;
default: return nullptr;
}
return nullptr;
}
static IrInstruction *ir_instruction_condbr_get_dep(IrInstructionCondBr *instruction, size_t index) {
switch (index) {
case 0: return instruction->condition;
case 1: return instruction->is_comptime;
default: return nullptr;
}
}
@ -1967,7 +1966,6 @@ static IrInstruction *ir_instruction_condbr_get_dep(IrInstructionCondBr *instruc
static IrInstruction *ir_instruction_switchbr_get_dep(IrInstructionSwitchBr *instruction, size_t index) {
switch (index) {
case 0: return instruction->target_value;
case 1: return instruction->is_comptime;
}
size_t case_index = index - 2;
if (case_index < instruction->case_count) return instruction->cases[case_index].value;
@ -3887,7 +3885,8 @@ static IrInstruction *ir_gen_fn_call(IrBuilder *irb, Scope *scope, AstNode *node
args[i] = ir_gen_node(irb, arg_node, scope);
}
return ir_build_call(irb, scope, node, nullptr, fn_ref, arg_count, args);
bool is_comptime = node->data.fn_call_expr.is_comptime;
return ir_build_call(irb, scope, node, nullptr, fn_ref, arg_count, args, is_comptime);
}
static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, Scope *scope, AstNode *node) {
@ -5601,6 +5600,8 @@ static IrBasicBlock *ir_get_new_bb(IrAnalyze *ira, IrBasicBlock *old_bb) {
IrInstruction *dep_instruction = ir_instruction_get_dep(instruction, dep_i);
if (dep_instruction == nullptr)
break;
if (dep_instruction->owner_bb == old_bb)
continue;
ir_get_new_bb(ira, dep_instruction->owner_bb);
}
}
@ -6537,6 +6538,14 @@ static bool ir_resolve_bool(IrAnalyze *ira, IrInstruction *value, bool *out) {
return true;
}
static bool ir_resolve_comptime(IrAnalyze *ira, IrInstruction *value, bool *out) {
if (!value) {
*out = false;
return true;
}
return ir_resolve_bool(ira, value, out);
}
static bool ir_resolve_atomic_order(IrAnalyze *ira, IrInstruction *value, AtomicOrder *out) {
if (value->value.type->id == TypeTableEntryIdInvalid)
return false;
@ -7521,7 +7530,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
size_t impl_param_count = impl_fn->type_entry->data.fn.fn_type_id.param_count;
IrInstruction *new_call_instruction = ir_build_call_from(&ira->new_irb, &call_instruction->base,
impl_fn, nullptr, impl_param_count, casted_args);
impl_fn, nullptr, impl_param_count, casted_args, false);
TypeTableEntry *return_type = impl_fn->type_entry->data.fn.fn_type_id.return_type;
ir_add_alloca(ira, new_call_instruction, return_type);
@ -7574,7 +7583,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
return ira->codegen->builtin_types.entry_invalid;
IrInstruction *new_call_instruction = ir_build_call_from(&ira->new_irb, &call_instruction->base,
fn_entry, fn_ref, call_param_count, casted_args);
fn_entry, fn_ref, call_param_count, casted_args, false);
ir_add_alloca(ira, new_call_instruction, return_type);
return ir_finish_anal(ira, return_type);
@ -7585,7 +7594,7 @@ static TypeTableEntry *ir_analyze_instruction_call(IrAnalyze *ira, IrInstruction
if (fn_ref->value.type->id == TypeTableEntryIdInvalid)
return ira->codegen->builtin_types.entry_invalid;
bool is_inline = call_instruction->is_inline || ir_should_inline(&ira->new_irb);
bool is_inline = call_instruction->is_comptime || ir_should_inline(&ira->new_irb);
if (is_inline || fn_ref->value.special != ConstValSpecialRuntime) {
if (fn_ref->value.type->id == TypeTableEntryIdMetaType) {
@ -7862,7 +7871,7 @@ static TypeTableEntry *ir_analyze_instruction_br(IrAnalyze *ira, IrInstructionBr
IrBasicBlock *old_dest_block = br_instruction->dest_block;
bool is_comptime;
if (!ir_resolve_bool(ira, br_instruction->is_comptime->other, &is_comptime))
if (!ir_resolve_comptime(ira, br_instruction->is_comptime->other, &is_comptime))
return ir_unreach_error(ira);
if (is_comptime || old_dest_block->ref_count == 1)
@ -7879,7 +7888,7 @@ static TypeTableEntry *ir_analyze_instruction_cond_br(IrAnalyze *ira, IrInstruct
return ir_unreach_error(ira);
bool is_comptime;
if (!ir_resolve_bool(ira, cond_br_instruction->is_comptime->other, &is_comptime))
if (!ir_resolve_comptime(ira, cond_br_instruction->is_comptime->other, &is_comptime))
return ir_unreach_error(ira);
if (is_comptime || instr_is_comptime(condition)) {
@ -9129,7 +9138,7 @@ static TypeTableEntry *ir_analyze_instruction_switch_br(IrAnalyze *ira,
size_t case_count = switch_br_instruction->case_count;
bool is_comptime;
if (!ir_resolve_bool(ira, switch_br_instruction->is_comptime->other, &is_comptime))
if (!ir_resolve_comptime(ira, switch_br_instruction->is_comptime->other, &is_comptime))
return ira->codegen->builtin_types.entry_invalid;
if (is_comptime || instr_is_comptime(target_value)) {

View File

@ -837,7 +837,7 @@ static AstNode *ast_parse_curly_suffix_expr(ParseContext *pc, size_t *token_inde
}
/*
SuffixOpExpression : PrimaryExpression option(FnCallExpression | ArrayAccessExpression | FieldAccessExpression | SliceExpression)
SuffixOpExpression = option("inline") PrimaryExpression option(FnCallExpression | ArrayAccessExpression | FieldAccessExpression | SliceExpression)
FnCallExpression : token(LParen) list(Expression, token(Comma)) token(RParen)
ArrayAccessExpression : token(LBracket) Expression token(RBracket)
SliceExpression : token(LBracket) Expression token(Ellipsis) option(Expression) token(RBracket) option(token(Const))
@ -845,8 +845,22 @@ FieldAccessExpression : token(Dot) token(Symbol)
StructLiteralField : token(Dot) token(Symbol) token(Eq) Expression
*/
static AstNode *ast_parse_suffix_op_expr(ParseContext *pc, size_t *token_index, bool mandatory) {
Token *inline_token = &pc->tokens->at(*token_index);
bool is_comptime;
if (inline_token->id == TokenIdKeywordInline) {
// TODO make it an error if something other than function call has the comptime keyword
is_comptime = true;
*token_index += 1;
} else {
is_comptime = false;
}
AstNode *primary_expr = ast_parse_primary_expr(pc, token_index, mandatory);
if (!primary_expr) {
if (is_comptime) {
*token_index -= 1;
}
return nullptr;
}
@ -857,6 +871,7 @@ static AstNode *ast_parse_suffix_op_expr(ParseContext *pc, size_t *token_index,
AstNode *node = ast_create_node(pc, NodeTypeFnCallExpr, first_token);
node->data.fn_call_expr.fn_ref_expr = primary_expr;
node->data.fn_call_expr.is_comptime = is_comptime;
ast_parse_fn_call_param_list(pc, token_index, &node->data.fn_call_expr.params);
primary_expr = node;

View File

@ -214,7 +214,7 @@
//}
//
//fn test_umoddi3() {
// @setFnTest(this, true);
// @setFnTest(this);
//
// test_one_umoddi3(0, 1, 0);
// test_one_umoddi3(2, 1, 0);
@ -229,7 +229,7 @@
//}
//
//fn test_udivmoddi4() {
// @setFnTest(this, true);
// @setFnTest(this);
//
// const cases = [][4]du_int {
// []du_int{0x0000000000000000, 0x0000000000000001, 0x0000000000000000, 0x0000000000000000},

View File

@ -15,12 +15,12 @@ pub fn len(ptr: &const u8) -> usize {
pub fn cmp(a: &const u8, b: &const u8) -> i8 {
var index: usize = 0;
while (a[index] == b[index] && a[index] != 0; index += 1) {}
return if (a[index] > b[index]) {
1
if (a[index] > b[index]) {
return 1;
} else if (a[index] < b[index]) {
-1
return -1;
} else {
0
return 0;
};
}
@ -127,7 +127,7 @@ pub const CBuf = struct {
};
fn testSimpleCBuf() {
@setFnTest(this, true);
@setFnTest(this);
var buf = %%CBuf.initEmpty(&debug.global_allocator);
assert(buf.len() == 0);
@ -148,13 +148,13 @@ fn testSimpleCBuf() {
}
fn testCompileTimeStrCmp() {
@setFnTest(this, true);
@setFnTest(this);
assert(@constEval(cmp(c"aoeu", c"aoez") == -1));
assert(@staticEval(cmp(c"aoeu", c"aoez") == -1));
}
fn testCompileTimeStrLen() {
@setFnTest(this, true);
@setFnTest(this);
assert(@constEval(len(c"123456789") == 9));
assert(@staticEval(len(c"123456789") == 9));
}

View File

@ -35,7 +35,7 @@ pub fn writeStackTrace(out_stream: &io.OutStream) -> %void {
defer st.self_exe_stream.close() %% {};
%return st.elf.openStream(&global_allocator, &st.self_exe_stream);
defer %return st.elf.close();
defer st.elf.close();
st.debug_info = (%return st.elf.findSection(".debug_info")) ?? return error.MissingDebugInfo;
st.debug_abbrev = (%return st.elf.findSection(".debug_abbrev")) ?? return error.MissingDebugInfo;
@ -154,7 +154,7 @@ const Die = struct {
fn getAttrAddr(self: &const Die, id: u64) -> %u64 {
const form_value = self.getAttr(id) ?? return error.InvalidDebugInfo;
return switch (*form_value) {
Address => |value| value,
FormValue.Address => |value| value,
else => error.InvalidDebugInfo,
};
}
@ -162,7 +162,7 @@ const Die = struct {
fn getAttrUnsignedLe(self: &const Die, id: u64) -> %u64 {
const form_value = self.getAttr(id) ?? return error.InvalidDebugInfo;
return switch (*form_value) {
Const => |value| value.asUnsignedLe(),
FormValue.Const => |value| value.asUnsignedLe(),
else => error.InvalidDebugInfo,
};
}
@ -170,8 +170,8 @@ const Die = struct {
fn getAttrString(self: &const Die, st: &ElfStackTrace, id: u64) -> %[]u8 {
const form_value = self.getAttr(id) ?? return error.InvalidDebugInfo;
return switch (*form_value) {
String => |value| value,
StrPtr => |offset| getString(st, offset),
FormValue.String => |value| value,
FormValue.StrPtr => |offset| getString(st, offset),
else => error.InvalidDebugInfo,
}
}
@ -406,8 +406,8 @@ fn scanAllCompileUnits(st: &ElfStackTrace) -> %void {
const high_pc_value = compile_unit_die.getAttr(DW.AT_high_pc) ?? return error.MissingDebugInfo;
const pc_end = switch (*high_pc_value) {
Address => |value| value,
Const => |value| {
FormValue.Address => |value| value,
FormValue.Const => |value| {
const offset = %return value.asUnsignedLe();
low_pc + offset
},

View File

@ -230,9 +230,11 @@ pub const Elf = struct {
}
}
pub fn close(elf: &Elf) -> %void {
pub fn close(elf: &Elf) {
elf.allocator.free(SectionHeader, elf.section_headers);
if (elf.auto_close_stream) %return elf.in_stream.close();
if (elf.auto_close_stream)
elf.in_stream.close();
}
pub fn findSection(elf: &Elf, name: []u8) -> %?&SectionHeader {
@ -247,8 +249,10 @@ pub const Elf = struct {
if (target_c == 0 || expected_c != target_c) goto next_section;
}
const null_byte = %return elf.in_stream.readByte();
if (null_byte == 0) return (?&SectionHeader)(section);
{
const null_byte = %return elf.in_stream.readByte();
if (null_byte == 0) return (?&SectionHeader)(section);
}
next_section:
}

View File

@ -216,7 +216,7 @@ pub fn HashMap(inline K: type, inline V: type, inline hash: fn(key: K)->u32,
}
fn basicHashMapTest() {
@setFnTest(this, true);
@setFnTest(this);
var map: HashMap(i32, i32, hash_i32, eql_i32) = undefined;
map.init(&debug.global_allocator);

View File

@ -137,19 +137,12 @@ pub const OutStream = struct {
}
}
pub fn close(self: &OutStream) -> %void {
pub fn close(self: &OutStream) {
while (true) {
const close_ret = system.close(self.fd);
const close_err = system.getErrno(close_ret);
if (close_err > 0) {
return switch (close_err) {
errno.EINTR => continue,
errno.EIO => error.Io,
errno.EBADF => error.BadFd,
else => error.Unexpected,
}
}
if (close_err > 0 && close_err == errno.EINTR)
continue;
return;
}
}
@ -163,7 +156,7 @@ pub const InStream = struct {
/// Call close to clean up.
pub fn open(is: &InStream, path: []const u8) -> %void {
switch (@compileVar("os")) {
linux, darwin => {
Os.linux, Os.darwin => {
while (true) {
const result = system.open(path, system.O_LARGEFILE|system.O_RDONLY, 0);
const err = system.getErrno(result);
@ -202,7 +195,7 @@ pub const InStream = struct {
/// you must use the open() function.
pub fn close(is: &InStream) -> %void {
switch (@compileVar("os")) {
linux, darwin => {
Os.linux, Os.darwin => {
while (true) {
const close_ret = system.close(is.fd);
const close_err = system.getErrno(close_ret);
@ -226,7 +219,7 @@ pub const InStream = struct {
/// the stream reached End Of File.
pub fn read(is: &InStream, buf: []u8) -> %usize {
switch (@compileVar("os")) {
linux, darwin => {
Os.linux, Os.darwin => {
var index: usize = 0;
while (index < buf.len) {
const amt_read = system.read(is.fd, &buf[index], buf.len - index);
@ -288,7 +281,7 @@ pub const InStream = struct {
pub fn seekForward(is: &InStream, amount: usize) -> %void {
switch (@compileVar("os")) {
linux, darwin => {
Os.linux, Os.darwin => {
const result = system.lseek(is.fd, amount, system.SEEK_CUR);
const err = system.getErrno(result);
if (err > 0) {
@ -308,7 +301,7 @@ pub const InStream = struct {
pub fn seekTo(is: &InStream, pos: usize) -> %void {
switch (@compileVar("os")) {
linux, darwin => {
Os.linux, Os.darwin => {
const result = system.lseek(is.fd, pos, system.SEEK_SET);
const err = system.getErrno(result);
if (err > 0) {
@ -328,7 +321,7 @@ pub const InStream = struct {
pub fn getPos(is: &InStream) -> %usize {
switch (@compileVar("os")) {
linux, darwin => {
Os.linux, Os.darwin => {
const result = system.lseek(is.fd, 0, system.SEEK_CUR);
const err = system.getErrno(result);
if (err > 0) {
@ -424,7 +417,7 @@ fn bufPrintUnsigned(inline T: type, out_buf: []u8, x: T) -> usize {
}
fn parseU64DigitTooBig() {
@setFnTest(this, true);
@setFnTest(this);
parseUnsigned(u64, "123a", 10) %% |err| {
if (err == error.InvalidChar) return;
@ -435,10 +428,10 @@ fn parseU64DigitTooBig() {
pub fn openSelfExe(stream: &InStream) -> %void {
switch (@compileVar("os")) {
linux => {
Os.linux => {
%return stream.open("/proc/self/exe");
},
darwin => {
Os.darwin => {
%%stderr.printf("TODO: openSelfExe on Darwin\n");
os.abort();
},

View File

@ -261,7 +261,7 @@ pub fn open_c(path: &const u8, flags: usize, perm: usize) -> usize {
}
pub fn open(path: []const u8, flags: usize, perm: usize) -> usize {
var buf: [path.len + 1]u8 = undefined;
const buf = @alloca(u8, path.len + 1);
@memcpy(&buf[0], &path[0], path.len);
buf[path.len] = 0;
return open_c(buf.ptr, flags, perm);
@ -272,7 +272,7 @@ pub fn create_c(path: &const u8, perm: usize) -> usize {
}
pub fn create(path: []const u8, perm: usize) -> usize {
var buf: [path.len + 1]u8 = undefined;
const buf = @alloca(u8, path.len + 1);
@memcpy(&buf[0], &path[0], path.len);
buf[path.len] = 0;
return create_c(buf.ptr, perm);
@ -283,7 +283,7 @@ pub fn openat_c(dirfd: i32, path: &const u8, flags: usize, mode: usize) -> usize
}
pub fn openat(dirfd: i32, path: []const u8, flags: usize, mode: usize) -> usize {
var buf: [path.len + 1]u8 = undefined;
const buf = @alloca(u8, path.len + 1);
@memcpy(&buf[0], &path[0], path.len);
buf[path.len] = 0;
return openat_c(dirfd, buf.ptr, flags, mode);

View File

@ -52,7 +52,7 @@ pub fn List(inline T: type) -> type{
}
fn basicListTest() {
@setFnTest(this, true);
@setFnTest(this);
var list = List(i32).init(&debug.global_allocator);
defer list.deinit();

View File

@ -83,7 +83,7 @@ pub fn sliceAsInt(buf: []u8, is_be: bool, inline T: type) -> T {
}
fn testSliceAsInt() {
@setFnTest(this, true);
@setFnTest(this);
{
const buf = []u8{0x00, 0x00, 0x12, 0x34};
const answer = sliceAsInt(buf[0...], true, u64);

View File

@ -181,8 +181,6 @@ error JunkAtEnd;
error Incomplete;
fn parseIp6(buf: []const u8) -> %Address {
@setFnStaticEval(this, false);
var result: Address = undefined;
result.family = linux.AF_INET6;
result.scope_id = 0;
@ -320,7 +318,7 @@ fn parseIp4(buf: []const u8) -> %u32 {
fn testParseIp4() {
@setFnTest(this, true);
@setFnTest(this);
assert(%%parseIp4("127.0.0.1") == endian.swapIfLe(u32, 0x7f000001));
switch (parseIp4("256.0.0.1")) { Overflow => {}, else => @unreachable(), }
@ -331,7 +329,7 @@ fn testParseIp4() {
}
fn testParseIp6() {
@setFnTest(this, true);
@setFnTest(this);
{
const addr = %%parseIp6("FF01:0:0:0:0:0:0:FB");
@ -342,7 +340,7 @@ fn testParseIp6() {
}
fn testLookupSimpleIp() {
@setFnTest(this, true);
@setFnTest(this);
{
var addrs_buf: [5]Address = undefined;

View File

@ -10,8 +10,8 @@ error Unexpected;
pub fn getRandomBytes(buf: []u8) -> %void {
while (true) {
const ret = switch (@compileVar("os")) {
linux => system.getrandom(buf.ptr, buf.len, 0),
darwin => system.getrandom(buf.ptr, buf.len),
Os.linux => system.getrandom(buf.ptr, buf.len, 0),
Os.darwin => system.getrandom(buf.ptr, buf.len),
else => @compileError("unsupported os"),
};
const err = system.getErrno(ret);
@ -29,7 +29,7 @@ pub fn getRandomBytes(buf: []u8) -> %void {
pub coldcc fn abort() -> unreachable {
switch (@compileVar("os")) {
linux, darwin => {
Os.linux, Os.darwin => {
system.raise(system.SIGABRT);
system.raise(system.SIGKILL);
while (true) {}

View File

@ -87,7 +87,7 @@ pub const Rand = struct {
} else if (T == f64) {
9007199254740992
} else {
@compileError("unknown floating point type" ++ @typeName(T))
@compileError("unknown floating point type")
};
return T(r.rangeUnsigned(int_type, 0, precision)) / T(precision);
}
@ -156,7 +156,7 @@ fn MersenneTwister(
}
fn testFloat32() {
@setFnTest(this, true);
@setFnTest(this);
var r: Rand = undefined;
r.init(42);
@ -169,7 +169,7 @@ fn testFloat32() {
}
fn testMT19937_64() {
@setFnTest(this, true);
@setFnTest(this);
var rng: MT19937_64 = undefined;
rng.init(rand_test.mt64_seed);
@ -179,7 +179,7 @@ fn testMT19937_64() {
}
fn testMT19937_32() {
@setFnTest(this, true);
@setFnTest(this);
var rng: MT19937_32 = undefined;
rng.init(rand_test.mt32_seed);

View File

@ -13,7 +13,7 @@ pub fn sliceEql(inline T: type, a: []const T, b: []const T) -> bool {
}
fn testStringEquality() {
@setFnTest(this, true);
@setFnTest(this);
assert(eql("abcd", "abcd"));
assert(!eql("abcdef", "abZdef"));

View File

@ -167,7 +167,7 @@ const DivResult = struct {
fn binaryNot() {
@setFnTest(this);
assert(~u16(0b1010101010101010) == 0b0101010101010101);
assert(@staticEval(~u16(0b1010101010101010) == 0b0101010101010101));
testBinaryNot(0b1010101010101010);
}