authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-06-18 16:23:56-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-06-18 16:23:56-04:00
loge54ed9f638c33ec3091d207978ed856d92614caf
tree8f649d016093ef46834225cde79aeb4b162eb988
parent9781342042798f7a75f3f7233872bae0fbde3ecc
parent14acc65675a35f838b5e73aeaca595ced0c10a0c
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #5628 from Vexu/src

Implement @src

8 files changed, 125 insertions(+), 0 deletions(-)

lib/std/builtin.zig+9
...@@ -131,6 +131,15 @@ pub const CallingConvention = enum {...@@ -131,6 +131,15 @@ pub const CallingConvention = enum {
131 AAPCSVFP,131 AAPCSVFP,
132};132};
133133
134/// This data structure is used by the Zig language code generation and
135/// therefore must be kept in sync with the compiler implementation.
136pub const SourceLocation = struct {
137 file: []const u8,
138 fn_name: []const u8,
139 line: u32,
140 column: u32,
141};
142
134pub const TypeId = @TagType(TypeInfo);143pub const TypeId = @TagType(TypeInfo);
135144
136/// This data structure is used by the Zig language code generation and145/// This data structure is used by the Zig language code generation and
src/all_types.hpp+6
...@@ -1827,6 +1827,7 @@ enum BuiltinFnId {...@@ -1827,6 +1827,7 @@ enum BuiltinFnId {
1827 BuiltinFnIdBitSizeof,1827 BuiltinFnIdBitSizeof,
1828 BuiltinFnIdWasmMemorySize,1828 BuiltinFnIdWasmMemorySize,
1829 BuiltinFnIdWasmMemoryGrow,1829 BuiltinFnIdWasmMemoryGrow,
1830 BuiltinFnIdSrc,
1830};1831};
18311832
1832struct BuiltinFnEntry {1833struct BuiltinFnEntry {
...@@ -2754,6 +2755,7 @@ enum IrInstSrcId {...@@ -2754,6 +2755,7 @@ enum IrInstSrcId {
2754 IrInstSrcIdSpillEnd,2755 IrInstSrcIdSpillEnd,
2755 IrInstSrcIdWasmMemorySize,2756 IrInstSrcIdWasmMemorySize,
2756 IrInstSrcIdWasmMemoryGrow,2757 IrInstSrcIdWasmMemoryGrow,
2758 IrInstSrcIdSrc,
2757};2759};
27582760
2759// ir_render_* functions in codegen.cpp consume Gen instructions and produce LLVM IR.2761// ir_render_* functions in codegen.cpp consume Gen instructions and produce LLVM IR.
...@@ -3761,6 +3763,10 @@ struct IrInstGenWasmMemoryGrow {...@@ -3761,6 +3763,10 @@ struct IrInstGenWasmMemoryGrow {
3761 IrInstGen *delta;3763 IrInstGen *delta;
3762};3764};
37633765
3766struct IrInstSrcSrc {
3767 IrInstSrc base;
3768};
3769
3764struct IrInstSrcSlice {3770struct IrInstSrcSlice {
3765 IrInstSrc base;3771 IrInstSrc base;
37663772
src/codegen.cpp+1
...@@ -8714,6 +8714,7 @@ static void define_builtin_fns(CodeGen *g) {...@@ -8714,6 +8714,7 @@ static void define_builtin_fns(CodeGen *g) {
8714 create_builtin_fn(g, BuiltinFnIdBitSizeof, "bitSizeOf", 1);8714 create_builtin_fn(g, BuiltinFnIdBitSizeof, "bitSizeOf", 1);
8715 create_builtin_fn(g, BuiltinFnIdWasmMemorySize, "wasmMemorySize", 1);8715 create_builtin_fn(g, BuiltinFnIdWasmMemorySize, "wasmMemorySize", 1);
8716 create_builtin_fn(g, BuiltinFnIdWasmMemoryGrow, "wasmMemoryGrow", 2);8716 create_builtin_fn(g, BuiltinFnIdWasmMemoryGrow, "wasmMemoryGrow", 2);
8717 create_builtin_fn(g, BuiltinFnIdSrc, "src", 0);
8717}8718}
87188719
8719static const char *bool_to_str(bool b) {8720static const char *bool_to_str(bool b) {
src/ir.cpp+76
...@@ -561,6 +561,8 @@ static void destroy_instruction_src(IrInstSrc *inst) {...@@ -561,6 +561,8 @@ static void destroy_instruction_src(IrInstSrc *inst) {
561 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcWasmMemorySize *>(inst));561 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcWasmMemorySize *>(inst));
562 case IrInstSrcIdWasmMemoryGrow:562 case IrInstSrcIdWasmMemoryGrow:
563 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcWasmMemoryGrow *>(inst));563 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcWasmMemoryGrow *>(inst));
564 case IrInstSrcIdSrc:
565 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcSrc *>(inst));
564 }566 }
565 zig_unreachable();567 zig_unreachable();
566}568}
...@@ -1627,6 +1629,9 @@ static constexpr IrInstSrcId ir_inst_id(IrInstSrcWasmMemoryGrow *) {...@@ -1627,6 +1629,9 @@ static constexpr IrInstSrcId ir_inst_id(IrInstSrcWasmMemoryGrow *) {
1627 return IrInstSrcIdWasmMemoryGrow;1629 return IrInstSrcIdWasmMemoryGrow;
1628}1630}
16291631
1632static constexpr IrInstSrcId ir_inst_id(IrInstSrcSrc *) {
1633 return IrInstSrcIdSrc;
1634}
16301635
1631static constexpr IrInstGenId ir_inst_id(IrInstGenDeclVar *) {1636static constexpr IrInstGenId ir_inst_id(IrInstGenDeclVar *) {
1632 return IrInstGenIdDeclVar;1637 return IrInstGenIdDeclVar;
...@@ -5030,6 +5035,11 @@ static IrInstGen *ir_build_wasm_memory_grow_gen(IrAnalyze *ira, IrInst *source_i...@@ -5030,6 +5035,11 @@ static IrInstGen *ir_build_wasm_memory_grow_gen(IrAnalyze *ira, IrInst *source_i
5030 return &instruction->base;5035 return &instruction->base;
5031}5036}
50325037
5038static IrInstSrc *ir_build_src(IrBuilderSrc *irb, Scope *scope, AstNode *source_node) {
5039 IrInstSrcSrc *instruction = ir_build_instruction<IrInstSrcSrc>(irb, scope, source_node);
5040
5041 return &instruction->base;
5042}
50335043
5034static void ir_count_defers(IrBuilderSrc *irb, Scope *inner_scope, Scope *outer_scope, size_t *results) {5044static void ir_count_defers(IrBuilderSrc *irb, Scope *inner_scope, Scope *outer_scope, size_t *results) {
5035 results[ReturnKindUnconditional] = 0;5045 results[ReturnKindUnconditional] = 0;
...@@ -7450,6 +7460,11 @@ static IrInstSrc *ir_gen_builtin_fn_call(IrBuilderSrc *irb, Scope *scope, AstNod...@@ -7450,6 +7460,11 @@ static IrInstSrc *ir_gen_builtin_fn_call(IrBuilderSrc *irb, Scope *scope, AstNod
7450 return ir_gen_union_init_expr(irb, scope, node, union_type_inst, name_inst, init_node,7460 return ir_gen_union_init_expr(irb, scope, node, union_type_inst, name_inst, init_node,
7451 lval, result_loc);7461 lval, result_loc);
7452 }7462 }
7463 case BuiltinFnIdSrc:
7464 {
7465 IrInstSrc *src_inst = ir_build_src(irb, scope, node);
7466 return ir_lval_wrap(irb, scope, src_inst, lval, result_loc);
7467 }
7453 }7468 }
7454 zig_unreachable();7469 zig_unreachable();
7455}7470}
...@@ -30859,6 +30874,64 @@ static IrInstGen *ir_analyze_instruction_spill_end(IrAnalyze *ira, IrInstSrcSpil...@@ -30859,6 +30874,64 @@ static IrInstGen *ir_analyze_instruction_spill_end(IrAnalyze *ira, IrInstSrcSpil
30859 return ir_build_spill_end_gen(ira, &instruction->base.base, begin, operand->value->type);30874 return ir_build_spill_end_gen(ira, &instruction->base.base, begin, operand->value->type);
30860}30875}
3086130876
30877static IrInstGen *ir_analyze_instruction_src(IrAnalyze *ira, IrInstSrcSrc *instruction) {
30878 ZigFn *fn_entry = scope_fn_entry(instruction->base.base.scope);
30879 if (fn_entry == nullptr) {
30880 ir_add_error(ira, &instruction->base.base, buf_sprintf("@src outside function"));
30881 return ira->codegen->invalid_inst_gen;
30882 }
30883
30884 ZigType *u8_ptr = get_pointer_to_type_extra(
30885 ira->codegen, ira->codegen->builtin_types.entry_u8,
30886 true, false, PtrLenUnknown,
30887 0, 0, 0, false);
30888 ZigType *u8_slice = get_slice_type(ira->codegen, u8_ptr);
30889
30890 ZigType *source_location_type = get_builtin_type(ira->codegen, "SourceLocation");
30891 if (type_resolve(ira->codegen, source_location_type, ResolveStatusSizeKnown)) {
30892 zig_unreachable();
30893 }
30894
30895 ZigValue *result = ira->codegen->pass1_arena->create<ZigValue>();
30896 result->special = ConstValSpecialStatic;
30897 result->type = source_location_type;
30898
30899 ZigValue **fields = alloc_const_vals_ptrs(ira->codegen, 4);
30900 result->data.x_struct.fields = fields;
30901
30902 // file: []const u8
30903 ensure_field_index(source_location_type, "file", 0);
30904 fields[0]->special = ConstValSpecialStatic;
30905 fields[0]->type = u8_slice;
30906
30907 ZigType *import = instruction->base.base.source_node->owner;
30908 Buf *path = import->data.structure.root_struct->path;
30909 ZigValue *file_name = create_const_str_lit(ira->codegen, path)->data.x_ptr.data.ref.pointee;
30910 init_const_slice(ira->codegen, fields[0], file_name, 0, buf_len(path), true);
30911
30912 // fn_name: []const u8
30913 ensure_field_index(source_location_type, "fn_name", 1);
30914 fields[1]->special = ConstValSpecialStatic;
30915 fields[1]->type = u8_slice;
30916
30917 ZigValue *fn_name = create_const_str_lit(ira->codegen, &fn_entry->symbol_name)->data.x_ptr.data.ref.pointee;
30918 init_const_slice(ira->codegen, fields[1], fn_name, 0, buf_len(&fn_entry->symbol_name), true);
30919
30920 // line: u32
30921 ensure_field_index(source_location_type, "line", 2);
30922 fields[2]->special = ConstValSpecialStatic;
30923 fields[2]->type = ira->codegen->builtin_types.entry_u32;
30924 bigint_init_unsigned(&fields[2]->data.x_bigint, instruction->base.base.source_node->line + 1);
30925
30926 // column: u32
30927 ensure_field_index(source_location_type, "column", 3);
30928 fields[3]->special = ConstValSpecialStatic;
30929 fields[3]->type = ira->codegen->builtin_types.entry_u32;
30930 bigint_init_unsigned(&fields[3]->data.x_bigint, instruction->base.base.source_node->column + 1);
30931
30932 return ir_const_move(ira, &instruction->base.base, result);
30933}
30934
30862static IrInstGen *ir_analyze_instruction_base(IrAnalyze *ira, IrInstSrc *instruction) {30935static IrInstGen *ir_analyze_instruction_base(IrAnalyze *ira, IrInstSrc *instruction) {
30863 switch (instruction->id) {30936 switch (instruction->id) {
30864 case IrInstSrcIdInvalid:30937 case IrInstSrcIdInvalid:
...@@ -31130,6 +31203,8 @@ static IrInstGen *ir_analyze_instruction_base(IrAnalyze *ira, IrInstSrc *instruc...@@ -31130,6 +31203,8 @@ static IrInstGen *ir_analyze_instruction_base(IrAnalyze *ira, IrInstSrc *instruc
31130 return ir_analyze_instruction_wasm_memory_size(ira, (IrInstSrcWasmMemorySize *)instruction);31203 return ir_analyze_instruction_wasm_memory_size(ira, (IrInstSrcWasmMemorySize *)instruction);
31131 case IrInstSrcIdWasmMemoryGrow:31204 case IrInstSrcIdWasmMemoryGrow:
31132 return ir_analyze_instruction_wasm_memory_grow(ira, (IrInstSrcWasmMemoryGrow *)instruction);31205 return ir_analyze_instruction_wasm_memory_grow(ira, (IrInstSrcWasmMemoryGrow *)instruction);
31206 case IrInstSrcIdSrc:
31207 return ir_analyze_instruction_src(ira, (IrInstSrcSrc *)instruction);
31133 }31208 }
31134 zig_unreachable();31209 zig_unreachable();
31135}31210}
...@@ -31525,6 +31600,7 @@ bool ir_inst_src_has_side_effects(IrInstSrc *instruction) {...@@ -31525,6 +31600,7 @@ bool ir_inst_src_has_side_effects(IrInstSrc *instruction) {
31525 case IrInstSrcIdAlloca:31600 case IrInstSrcIdAlloca:
31526 case IrInstSrcIdSpillEnd:31601 case IrInstSrcIdSpillEnd:
31527 case IrInstSrcIdWasmMemorySize:31602 case IrInstSrcIdWasmMemorySize:
31603 case IrInstSrcIdSrc:
31528 return false;31604 return false;
3152931605
31530 case IrInstSrcIdAsm:31606 case IrInstSrcIdAsm:
src/ir_print.cpp+9
...@@ -325,6 +325,8 @@ const char* ir_inst_src_type_str(IrInstSrcId id) {...@@ -325,6 +325,8 @@ const char* ir_inst_src_type_str(IrInstSrcId id) {
325 return "SrcWasmMemorySize";325 return "SrcWasmMemorySize";
326 case IrInstSrcIdWasmMemoryGrow:326 case IrInstSrcIdWasmMemoryGrow:
327 return "SrcWasmMemoryGrow";327 return "SrcWasmMemoryGrow";
328 case IrInstSrcIdSrc:
329 return "SrcSrc";
328 }330 }
329 zig_unreachable();331 zig_unreachable();
330}332}
...@@ -1744,6 +1746,10 @@ static void ir_print_wasm_memory_grow(IrPrintGen *irp, IrInstGenWasmMemoryGrow *...@@ -1744,6 +1746,10 @@ static void ir_print_wasm_memory_grow(IrPrintGen *irp, IrInstGenWasmMemoryGrow *
1744 fprintf(irp->f, ")");1746 fprintf(irp->f, ")");
1745}1747}
17461748
1749static void ir_print_builtin_src(IrPrintSrc *irp, IrInstSrcSrc *instruction) {
1750 fprintf(irp->f, "@src()");
1751}
1752
1747static void ir_print_memset(IrPrintSrc *irp, IrInstSrcMemset *instruction) {1753static void ir_print_memset(IrPrintSrc *irp, IrInstSrcMemset *instruction) {
1748 fprintf(irp->f, "@memset(");1754 fprintf(irp->f, "@memset(");
1749 ir_print_other_inst_src(irp, instruction->dest_ptr);1755 ir_print_other_inst_src(irp, instruction->dest_ptr);
...@@ -2994,6 +3000,9 @@ static void ir_print_inst_src(IrPrintSrc *irp, IrInstSrc *instruction, bool trai...@@ -2994,6 +3000,9 @@ static void ir_print_inst_src(IrPrintSrc *irp, IrInstSrc *instruction, bool trai
2994 case IrInstSrcIdWasmMemoryGrow:3000 case IrInstSrcIdWasmMemoryGrow:
2995 ir_print_wasm_memory_grow(irp, (IrInstSrcWasmMemoryGrow *)instruction);3001 ir_print_wasm_memory_grow(irp, (IrInstSrcWasmMemoryGrow *)instruction);
2996 break;3002 break;
3003 case IrInstSrcIdSrc:
3004 ir_print_builtin_src(irp, (IrInstSrcSrc *)instruction);
3005 break;
2997 }3006 }
2998 fprintf(irp->f, "\n");3007 fprintf(irp->f, "\n");
2999}3008}
test/compile_errors.zig+8
...@@ -2,6 +2,14 @@ const tests = @import("tests.zig");...@@ -2,6 +2,14 @@ const tests = @import("tests.zig");
2const std = @import("std");2const std = @import("std");
33
4pub fn addCases(cases: *tests.CompileErrorContext) void {4pub fn addCases(cases: *tests.CompileErrorContext) void {
5 cases.add("@src outside function",
6 \\comptime {
7 \\ @src();
8 \\}
9 , &[_][]const u8{
10 "tmp.zig:2:5: error: @src outside function",
11 });
12
5 cases.add("call assigned to constant",13 cases.add("call assigned to constant",
6 \\const Foo = struct {14 \\const Foo = struct {
7 \\ x: i32,15 \\ x: i32,
test/stage1/behavior.zig+1
...@@ -131,4 +131,5 @@ comptime {...@@ -131,4 +131,5 @@ comptime {
131 }131 }
132 _ = @import("behavior/while.zig");132 _ = @import("behavior/while.zig");
133 _ = @import("behavior/widening.zig");133 _ = @import("behavior/widening.zig");
134 _ = @import("behavior/src.zig");
134}135}
test/stage1/behavior/src.zig created+15
...@@ -0,0 +1,15 @@
1const std = @import("std");
2const expect = std.testing.expect;
3
4test "@src" {
5 doTheTest();
6}
7
8fn doTheTest() void {
9 const src = @src();
10
11 expect(src.line == 9);
12 expect(src.column == 17);
13 expect(std.mem.endsWith(u8, src.fn_name, "doTheTest"));
14 expect(std.mem.endsWith(u8, src.file, "src.zig"));
15}