authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-19 17:59:37-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-19 17:59:37-07:00
loga30950706f858e052376fb2d526895970f59d0ca
tree31f0da7dad51bcf707cba8555d7aa33f0ab0a1ba
parentb0846b6ecbb3e2557c5c95ddee04ecc055881d75
parent9ec9c0f5e57820f4baa04b9674a6a4a88235b863

Merge branch 'Vexu-stage2'

closes #6093

9 files changed, 415 insertions(+), 23 deletions(-)

src-self-hosted/Module.zig+240-8
......@@ -170,6 +170,9 @@ pub const Decl = struct {
170170 /// This flag is set when this Decl is added to a check_for_deletion set, and cleared
171171 /// when removed.
172172 deletion_flag: bool,
173 /// Whether the corresponding AST decl has a `pub` keyword.
174 is_pub: bool,
175
173176 /// An integer that can be checked against the corresponding incrementing
174177 /// generation field of Module. This is used to determine whether `complete` status
175178 /// represents pre- or post- re-analysis.
......@@ -320,6 +323,16 @@ pub const Fn = struct {
320323 }
321324};
322325
326pub const Var = struct {
327 init: Value,
328 owner_decl: *Decl,
329
330 has_init: bool,
331 is_extern: bool,
332 is_mutable: bool,
333 is_threadlocal: bool,
334};
335
323336pub const Scope = struct {
324337 tag: Tag,
325338
......@@ -1235,6 +1248,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
12351248 };
12361249 defer fn_type_scope.instructions.deinit(self.gpa);
12371250
1251 decl.is_pub = fn_proto.getTrailer("visib_token") != null;
12381252 const body_node = fn_proto.getTrailer("body_node") orelse
12391253 return self.failTok(&fn_type_scope.base, fn_proto.fn_token, "TODO implement extern functions", .{});
12401254
......@@ -1419,7 +1433,173 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
14191433 }
14201434 return type_changed;
14211435 },
1422 .VarDecl => @panic("TODO var decl"),
1436 .VarDecl => {
1437 const var_decl = @fieldParentPtr(ast.Node.VarDecl, "base", ast_node);
1438
1439 decl.analysis = .in_progress;
1440
1441 // We need the memory for the Type to go into the arena for the Decl
1442 var decl_arena = std.heap.ArenaAllocator.init(self.gpa);
1443 errdefer decl_arena.deinit();
1444 const decl_arena_state = try decl_arena.allocator.create(std.heap.ArenaAllocator.State);
1445
1446 var block_scope: Scope.Block = .{
1447 .parent = null,
1448 .func = null,
1449 .decl = decl,
1450 .instructions = .{},
1451 .arena = &decl_arena.allocator,
1452 };
1453 defer block_scope.instructions.deinit(self.gpa);
1454
1455 decl.is_pub = var_decl.getTrailer("visib_token") != null;
1456 const is_extern = blk: {
1457 const maybe_extern_token = var_decl.getTrailer("extern_export_token") orelse
1458 break :blk false;
1459 break :blk tree.token_ids[maybe_extern_token] == .Keyword_extern;
1460 };
1461 if (var_decl.getTrailer("lib_name")) |lib_name| {
1462 assert(is_extern);
1463 return self.failNode(&block_scope.base, lib_name, "TODO implement function library name", .{});
1464 }
1465 const is_mutable = tree.token_ids[var_decl.mut_token] == .Keyword_var;
1466 const is_threadlocal = if (var_decl.getTrailer("thread_local_token")) |some| blk: {
1467 if (!is_mutable) {
1468 return self.failTok(&block_scope.base, some, "threadlocal variable cannot be constant", .{});
1469 }
1470 break :blk true;
1471 } else false;
1472 assert(var_decl.getTrailer("comptime_token") == null);
1473 if (var_decl.getTrailer("align_node")) |align_expr| {
1474 return self.failNode(&block_scope.base, align_expr, "TODO implement function align expression", .{});
1475 }
1476 if (var_decl.getTrailer("section_node")) |sect_expr| {
1477 return self.failNode(&block_scope.base, sect_expr, "TODO implement function section expression", .{});
1478 }
1479
1480 const explicit_type = blk: {
1481 const type_node = var_decl.getTrailer("type_node") orelse
1482 break :blk null;
1483
1484 var type_scope_arena = std.heap.ArenaAllocator.init(self.gpa);
1485 defer type_scope_arena.deinit();
1486 var type_scope: Scope.GenZIR = .{
1487 .decl = decl,
1488 .arena = &type_scope_arena.allocator,
1489 .parent = decl.scope,
1490 };
1491 defer type_scope.instructions.deinit(self.gpa);
1492
1493 const src = tree.token_locs[type_node.firstToken()].start;
1494 const type_type = try astgen.addZIRInstConst(self, &type_scope.base, src, .{
1495 .ty = Type.initTag(.type),
1496 .val = Value.initTag(.type_type),
1497 });
1498 const var_type = try astgen.expr(self, &type_scope.base, .{ .ty = type_type }, type_node);
1499 _ = try astgen.addZIRUnOp(self, &type_scope.base, src, .@"return", var_type);
1500
1501 break :blk try zir_sema.analyzeBodyValueAsType(self, &block_scope, .{
1502 .instructions = type_scope.instructions.items,
1503 });
1504 };
1505
1506 var var_type: Type = undefined;
1507 const value: ?Value = if (var_decl.getTrailer("init_node")) |init_node| blk: {
1508 var gen_scope_arena = std.heap.ArenaAllocator.init(self.gpa);
1509 defer gen_scope_arena.deinit();
1510 var gen_scope: Scope.GenZIR = .{
1511 .decl = decl,
1512 .arena = &gen_scope_arena.allocator,
1513 .parent = decl.scope,
1514 };
1515 defer gen_scope.instructions.deinit(self.gpa);
1516 const src = tree.token_locs[init_node.firstToken()].start;
1517
1518 // TODO comptime scope here
1519 const init_inst = try astgen.expr(self, &gen_scope.base, .none, init_node);
1520 _ = try astgen.addZIRUnOp(self, &gen_scope.base, src, .@"return", init_inst);
1521
1522 var inner_block: Scope.Block = .{
1523 .parent = null,
1524 .func = null,
1525 .decl = decl,
1526 .instructions = .{},
1527 .arena = &gen_scope_arena.allocator,
1528 };
1529 defer inner_block.instructions.deinit(self.gpa);
1530 try zir_sema.analyzeBody(self, &inner_block.base, .{ .instructions = gen_scope.instructions.items });
1531
1532 for (inner_block.instructions.items) |inst| {
1533 if (inst.castTag(.ret)) |ret| {
1534 const coerced = if (explicit_type) |some|
1535 try self.coerce(&inner_block.base, some, ret.operand)
1536 else
1537 ret.operand;
1538 const val = try self.resolveConstValue(&inner_block.base, coerced);
1539
1540 var_type = explicit_type orelse try ret.operand.ty.copy(block_scope.arena);
1541 break :blk try val.copy(block_scope.arena);
1542 } else {
1543 return self.fail(&block_scope.base, inst.src, "unable to resolve comptime value", .{});
1544 }
1545 }
1546 unreachable;
1547 } else if (!is_extern) {
1548 return self.failTok(&block_scope.base, var_decl.firstToken(), "variables must be initialized", .{});
1549 } else if (explicit_type) |some| blk: {
1550 var_type = some;
1551 break :blk null;
1552 } else {
1553 return self.failTok(&block_scope.base, var_decl.firstToken(), "unable to infer variable type", .{});
1554 };
1555
1556 if (is_mutable and !var_type.isValidVarType(is_extern)) {
1557 return self.failTok(&block_scope.base, var_decl.firstToken(), "variable of type '{}' must be const", .{var_type});
1558 }
1559
1560 var type_changed = true;
1561 if (decl.typedValueManaged()) |tvm| {
1562 type_changed = !tvm.typed_value.ty.eql(var_type);
1563
1564 tvm.deinit(self.gpa);
1565 }
1566
1567 const new_variable = try decl_arena.allocator.create(Var);
1568 const var_payload = try decl_arena.allocator.create(Value.Payload.Variable);
1569 new_variable.* = .{
1570 .owner_decl = decl,
1571 .init = value orelse undefined,
1572 .has_init = value != null,
1573 .is_extern = is_extern,
1574 .is_mutable = is_mutable,
1575 .is_threadlocal = is_threadlocal,
1576 };
1577 var_payload.* = .{ .variable = new_variable };
1578
1579 decl_arena_state.* = decl_arena.state;
1580 decl.typed_value = .{
1581 .most_recent = .{
1582 .typed_value = .{
1583 .ty = var_type,
1584 .val = Value.initPayload(&var_payload.base),
1585 },
1586 .arena = decl_arena_state,
1587 },
1588 };
1589 decl.analysis = .complete;
1590 decl.generation = self.generation;
1591
1592 if (var_decl.getTrailer("extern_export_token")) |maybe_export_token| {
1593 if (tree.token_ids[maybe_export_token] == .Keyword_export) {
1594 const export_src = tree.token_locs[maybe_export_token].start;
1595 const name_loc = tree.token_locs[var_decl.name_token];
1596 const name = tree.tokenSliceLoc(name_loc);
1597 // The scope needs to have the decl in it.
1598 try self.analyzeExport(&block_scope.base, export_src, name, decl);
1599 }
1600 }
1601 return type_changed;
1602 },
14231603 .Comptime => @panic("TODO comptime decl"),
14241604 .Use => @panic("TODO usingnamespace decl"),
14251605 else => unreachable,
......@@ -1584,7 +1764,32 @@ fn analyzeRootSrcFile(self: *Module, root_scope: *Scope.File) !void {
15841764 }
15851765 }
15861766 } else if (src_decl.castTag(.VarDecl)) |var_decl| {
1587 log.err("TODO: analyze var decl", .{});
1767 const name_loc = tree.token_locs[var_decl.name_token];
1768 const name = tree.tokenSliceLoc(name_loc);
1769 const name_hash = root_scope.fullyQualifiedNameHash(name);
1770 const contents_hash = std.zig.hashSrc(tree.getNodeSource(src_decl));
1771 if (self.decl_table.get(name_hash)) |decl| {
1772 // Update the AST Node index of the decl, even if its contents are unchanged, it may
1773 // have been re-ordered.
1774 decl.src_index = decl_i;
1775 if (deleted_decls.remove(decl) == null) {
1776 decl.analysis = .sema_failure;
1777 const err_msg = try ErrorMsg.create(self.gpa, name_loc.start, "redefinition of '{}'", .{decl.name});
1778 errdefer err_msg.destroy(self.gpa);
1779 try self.failed_decls.putNoClobber(self.gpa, decl, err_msg);
1780 } else if (!srcHashEql(decl.contents_hash, contents_hash)) {
1781 try self.markOutdatedDecl(decl);
1782 decl.contents_hash = contents_hash;
1783 }
1784 } else {
1785 const new_decl = try self.createNewDecl(&root_scope.base, name, decl_i, name_hash, contents_hash);
1786 root_scope.decls.appendAssumeCapacity(new_decl);
1787 if (var_decl.getTrailer("extern_export_token")) |maybe_export_token| {
1788 if (tree.token_ids[maybe_export_token] == .Keyword_export) {
1789 self.work_queue.writeItemAssumeCapacity(.{ .analyze_decl = new_decl });
1790 }
1791 }
1792 }
15881793 } else if (src_decl.castTag(.Comptime)) |comptime_node| {
15891794 log.err("TODO: analyze comptime decl", .{});
15901795 } else if (src_decl.castTag(.ContainerField)) |container_field| {
......@@ -1798,6 +2003,7 @@ fn allocateNewDecl(
17982003 .wasm => .{ .wasm = null },
17992004 },
18002005 .generation = 0,
2006 .is_pub = false,
18012007 };
18022008 return new_decl;
18032009}
......@@ -2217,20 +2423,46 @@ pub fn analyzeDeclRef(self: *Module, scope: *Scope, src: usize, decl: *Decl) Inn
22172423 };
22182424
22192425 const decl_tv = try decl.typedValue();
2220 const ty_payload = try scope.arena().create(Type.Payload.Pointer);
2221 ty_payload.* = .{
2222 .base = .{ .tag = .single_const_pointer },
2223 .pointee_type = decl_tv.ty,
2224 };
2426 if (decl_tv.val.tag() == .variable) {
2427 return self.analyzeVarRef(scope, src, decl_tv);
2428 }
2429 const ty = try self.singlePtrType(scope, src, false, decl_tv.ty);
22252430 const val_payload = try scope.arena().create(Value.Payload.DeclRef);
22262431 val_payload.* = .{ .decl = decl };
22272432
22282433 return self.constInst(scope, src, .{
2229 .ty = Type.initPayload(&ty_payload.base),
2434 .ty = ty,
22302435 .val = Value.initPayload(&val_payload.base),
22312436 });
22322437}
22332438
2439fn analyzeVarRef(self: *Module, scope: *Scope, src: usize, tv: TypedValue) InnerError!*Inst {
2440 const variable = tv.val.cast(Value.Payload.Variable).?.variable;
2441
2442 const ty = try self.singlePtrType(scope, src, variable.is_mutable, tv.ty);
2443 if (!variable.is_mutable and !variable.is_extern and variable.has_init) {
2444 const val_payload = try scope.arena().create(Value.Payload.RefVal);
2445 val_payload.* = .{ .val = variable.init };
2446 return self.constInst(scope, src, .{
2447 .ty = ty,
2448 .val = Value.initPayload(&val_payload.base),
2449 });
2450 }
2451
2452 const b = try self.requireRuntimeBlock(scope, src);
2453 const inst = try b.arena.create(Inst.VarPtr);
2454 inst.* = .{
2455 .base = .{
2456 .tag = .varptr,
2457 .ty = ty,
2458 .src = src,
2459 },
2460 .variable = variable,
2461 };
2462 try b.instructions.append(self.gpa, &inst.base);
2463 return &inst.base;
2464}
2465
22342466pub fn analyzeDeref(self: *Module, scope: *Scope, src: usize, ptr: *Inst, ptr_src: usize) InnerError!*Inst {
22352467 const elem_ty = switch (ptr.ty.zigTypeTag()) {
22362468 .Pointer => ptr.ty.elemType(),
src-self-hosted/astgen.zig+8-6
......@@ -1223,9 +1223,10 @@ fn identifier(mod: *Module, scope: *Scope, rl: ResultLoc, ident: *ast.Node.OneTo
12231223 }
12241224
12251225 if (mod.lookupDeclName(scope, ident_name)) |decl| {
1226 // TODO handle lvalues
12271226 const result = try addZIRInst(mod, scope, src, zir.Inst.DeclValInModule, .{ .decl = decl }, .{});
1228 return rlWrap(mod, scope, rl, result);
1227 if (rl == .lvalue or rl == .ref)
1228 return result;
1229 return rlWrap(mod, scope, rl, try addZIRUnOp(mod, scope, src, .deref, result));
12291230 }
12301231
12311232 return mod.failNode(scope, &ident.base, "use of undeclared identifier '{}'", .{ident_name});
......@@ -1258,7 +1259,8 @@ fn multilineStrLiteral(mod: *Module, scope: *Scope, node: *ast.Node.MultilineStr
12581259 // line lengths and new lines
12591260 var len = lines.len - 1;
12601261 for (lines) |line| {
1261 len += tree.tokenSlice(line).len - 2;
1262 // 2 for the '//' + 1 for '\n'
1263 len += tree.tokenSlice(line).len - 3;
12621264 }
12631265
12641266 const bytes = try scope.arena().alloc(u8, len);
......@@ -1268,9 +1270,9 @@ fn multilineStrLiteral(mod: *Module, scope: *Scope, node: *ast.Node.MultilineStr
12681270 bytes[i] = '\n';
12691271 i += 1;
12701272 }
1271 const slice = tree.tokenSlice(line)[2..];
1272 mem.copy(u8, bytes[i..], slice);
1273 i += slice.len;
1273 const slice = tree.tokenSlice(line);
1274 mem.copy(u8, bytes[i..], slice[2..slice.len - 1]);
1275 i += slice.len - 3;
12741276 }
12751277
12761278 return addZIRInst(mod, scope, src, zir.Inst.Str, .{ .bytes = bytes }, .{});
src-self-hosted/codegen.zig+11
......@@ -684,6 +684,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
684684 .unreach => return MCValue{ .unreach = {} },
685685 .unwrap_optional => return self.genUnwrapOptional(inst.castTag(.unwrap_optional).?),
686686 .wrap_optional => return self.genWrapOptional(inst.castTag(.wrap_optional).?),
687 .varptr => return self.genVarPtr(inst.castTag(.varptr).?),
687688 }
688689 }
689690
......@@ -858,6 +859,16 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
858859 }
859860 }
860861
862 fn genVarPtr(self: *Self, inst: *ir.Inst.VarPtr) !MCValue {
863 // No side effects, so if it's unreferenced, do nothing.
864 if (inst.base.isUnused())
865 return MCValue.dead;
866
867 switch (arch) {
868 else => return self.fail(inst.base.src, "TODO implement varptr for {}", .{self.target.cpu.arch}),
869 }
870 }
871
861872 fn reuseOperand(inst: *ir.Inst, op_index: ir.Inst.DeathsBitIndex, mcv: MCValue) bool {
862873 if (!inst.operandDies(op_index) or !mcv.isMutable())
863874 return false;
src-self-hosted/ir.zig+16
......@@ -81,6 +81,7 @@ pub const Inst = struct {
8181 ref,
8282 ret,
8383 retvoid,
84 varptr,
8485 /// Write a value to a pointer. LHS is pointer, RHS is value.
8586 store,
8687 sub,
......@@ -135,6 +136,7 @@ pub const Inst = struct {
135136 .condbr => CondBr,
136137 .constant => Constant,
137138 .loop => Loop,
139 .varptr => VarPtr,
138140 };
139141 }
140142
......@@ -434,6 +436,20 @@ pub const Inst = struct {
434436 return null;
435437 }
436438 };
439
440 pub const VarPtr = struct {
441 pub const base_tag = Tag.varptr;
442
443 base: Inst,
444 variable: *Module.Var,
445
446 pub fn operandCount(self: *const VarPtr) usize {
447 return 0;
448 }
449 pub fn getOperand(self: *const VarPtr, index: usize) ?*Inst {
450 return null;
451 }
452 };
437453};
438454
439455pub const Body = struct {
src-self-hosted/type.zig+5-4
......@@ -457,7 +457,8 @@ pub const Type = extern union {
457457 try param_type.format("", .{}, out_stream);
458458 }
459459 try out_stream.writeAll(") ");
460 try payload.return_type.format("", .{}, out_stream);
460 ty = payload.return_type;
461 continue;
461462 },
462463
463464 .array_u8 => {
......@@ -1074,7 +1075,7 @@ pub const Type = extern union {
10741075 }
10751076
10761077 /// Returns if type can be used for a runtime variable
1077 pub fn isValidVarType(self: Type) bool {
1078 pub fn isValidVarType(self: Type, is_extern: bool) bool {
10781079 var ty = self;
10791080 while (true) switch (ty.zigTypeTag()) {
10801081 .Bool,
......@@ -1087,6 +1088,7 @@ pub const Type = extern union {
10871088 .Vector,
10881089 => return true,
10891090
1091 .Opaque => return is_extern,
10901092 .BoundFn,
10911093 .ComptimeFloat,
10921094 .ComptimeInt,
......@@ -1096,12 +1098,11 @@ pub const Type = extern union {
10961098 .Void,
10971099 .Undefined,
10981100 .Null,
1099 .Opaque,
11001101 => return false,
11011102
11021103 .Optional => {
11031104 var buf: Payload.Pointer = undefined;
1104 return ty.optionalChild(&buf).isValidVarType();
1105 return ty.optionalChild(&buf).isValidVarType(is_extern);
11051106 },
11061107 .Pointer, .Array => ty = ty.elemType(),
11071108
src-self-hosted/value.zig+29-1
......@@ -79,6 +79,7 @@ pub const Value = extern union {
7979 int_big_positive,
8080 int_big_negative,
8181 function,
82 variable,
8283 ref_val,
8384 decl_ref,
8485 elem_ptr,
......@@ -196,6 +197,7 @@ pub const Value = extern union {
196197 @panic("TODO implement copying of big ints");
197198 },
198199 .function => return self.copyPayloadShallow(allocator, Payload.Function),
200 .variable => return self.copyPayloadShallow(allocator, Payload.Variable),
199201 .ref_val => {
200202 const payload = @fieldParentPtr(Payload.RefVal, "base", self.ptr_otherwise);
201203 const new_payload = try allocator.create(Payload.RefVal);
......@@ -216,7 +218,7 @@ pub const Value = extern union {
216218 };
217219 return Value{ .ptr_otherwise = &new_payload.base };
218220 },
219 .enum_literal, .bytes => return self.copyPayloadShallow(allocator, Payload.Bytes),
221 .bytes => return self.copyPayloadShallow(allocator, Payload.Bytes),
220222 .repeated => {
221223 const payload = @fieldParentPtr(Payload.Repeated, "base", self.ptr_otherwise);
222224 const new_payload = try allocator.create(Payload.Repeated);
......@@ -230,6 +232,15 @@ pub const Value = extern union {
230232 .float_32 => return self.copyPayloadShallow(allocator, Payload.Float_32),
231233 .float_64 => return self.copyPayloadShallow(allocator, Payload.Float_64),
232234 .float_128 => return self.copyPayloadShallow(allocator, Payload.Float_128),
235 .enum_literal => {
236 const payload = @fieldParentPtr(Payload.Bytes, "base", self.ptr_otherwise);
237 const new_payload = try allocator.create(Payload.Bytes);
238 new_payload.* = .{
239 .base = payload.base,
240 .data = try allocator.dupe(u8, payload.data),
241 };
242 return Value{ .ptr_otherwise = &new_payload.base };
243 },
233244 }
234245 }
235246
......@@ -310,6 +321,7 @@ pub const Value = extern union {
310321 .int_big_positive => return out_stream.print("{}", .{val.cast(Payload.IntBigPositive).?.asBigInt()}),
311322 .int_big_negative => return out_stream.print("{}", .{val.cast(Payload.IntBigNegative).?.asBigInt()}),
312323 .function => return out_stream.writeAll("(function)"),
324 .variable => return out_stream.writeAll("(variable)"),
313325 .ref_val => {
314326 const ref_val = val.cast(Payload.RefVal).?;
315327 try out_stream.writeAll("&const ");
......@@ -410,6 +422,7 @@ pub const Value = extern union {
410422 .int_big_positive,
411423 .int_big_negative,
412424 .function,
425 .variable,
413426 .ref_val,
414427 .decl_ref,
415428 .elem_ptr,
......@@ -471,6 +484,7 @@ pub const Value = extern union {
471484 .enum_literal_type,
472485 .null_value,
473486 .function,
487 .variable,
474488 .ref_val,
475489 .decl_ref,
476490 .elem_ptr,
......@@ -548,6 +562,7 @@ pub const Value = extern union {
548562 .enum_literal_type,
549563 .null_value,
550564 .function,
565 .variable,
551566 .ref_val,
552567 .decl_ref,
553568 .elem_ptr,
......@@ -625,6 +640,7 @@ pub const Value = extern union {
625640 .enum_literal_type,
626641 .null_value,
627642 .function,
643 .variable,
628644 .ref_val,
629645 .decl_ref,
630646 .elem_ptr,
......@@ -728,6 +744,7 @@ pub const Value = extern union {
728744 .enum_literal_type,
729745 .null_value,
730746 .function,
747 .variable,
731748 .ref_val,
732749 .decl_ref,
733750 .elem_ptr,
......@@ -810,6 +827,7 @@ pub const Value = extern union {
810827 .enum_literal_type,
811828 .null_value,
812829 .function,
830 .variable,
813831 .ref_val,
814832 .decl_ref,
815833 .elem_ptr,
......@@ -974,6 +992,7 @@ pub const Value = extern union {
974992 .bool_false,
975993 .null_value,
976994 .function,
995 .variable,
977996 .ref_val,
978997 .decl_ref,
979998 .elem_ptr,
......@@ -1046,6 +1065,7 @@ pub const Value = extern union {
10461065 .enum_literal_type,
10471066 .null_value,
10481067 .function,
1068 .variable,
10491069 .ref_val,
10501070 .decl_ref,
10511071 .elem_ptr,
......@@ -1182,6 +1202,7 @@ pub const Value = extern union {
11821202 .bool_false,
11831203 .null_value,
11841204 .function,
1205 .variable,
11851206 .int_u64,
11861207 .int_i64,
11871208 .int_big_positive,
......@@ -1260,6 +1281,7 @@ pub const Value = extern union {
12601281 .bool_false,
12611282 .null_value,
12621283 .function,
1284 .variable,
12631285 .int_u64,
12641286 .int_i64,
12651287 .int_big_positive,
......@@ -1355,6 +1377,7 @@ pub const Value = extern union {
13551377 .bool_true,
13561378 .bool_false,
13571379 .function,
1380 .variable,
13581381 .int_u64,
13591382 .int_i64,
13601383 .int_big_positive,
......@@ -1429,6 +1452,11 @@ pub const Value = extern union {
14291452 func: *Module.Fn,
14301453 };
14311454
1455 pub const Variable = struct {
1456 base: Payload = Payload{ .tag = .variable },
1457 variable: *Module.Var,
1458 };
1459
14321460 pub const ArraySentinel0_u8_Type = struct {
14331461 base: Payload = Payload{ .tag = .array_sentinel_0_u8_type },
14341462 len: u64,
src-self-hosted/zir.zig+70
......@@ -1752,6 +1752,9 @@ const EmitZIR = struct {
17521752 const decl_ref = try self.emitDeclRef(inst.src, declref.decl);
17531753 try new_body.instructions.append(decl_ref);
17541754 break :blk decl_ref;
1755 } else if (const_inst.val.cast(Value.Payload.Variable)) |var_pl| blk: {
1756 const owner_decl = var_pl.variable.owner_decl;
1757 break :blk try self.emitDeclVal(inst.src, mem.spanZ(owner_decl.name));
17551758 } else blk: {
17561759 break :blk (try self.emitTypedValue(inst.src, .{ .ty = inst.ty, .val = const_inst.val })).inst;
17571760 };
......@@ -1875,6 +1878,11 @@ const EmitZIR = struct {
18751878 if (typed_value.val.cast(Value.Payload.DeclRef)) |decl_ref| {
18761879 const decl = decl_ref.decl;
18771880 return try self.emitUnnamedDecl(try self.emitDeclRef(src, decl));
1881 } else if (typed_value.val.cast(Value.Payload.Variable)) |variable| {
1882 return self.emitTypedValue(src, .{
1883 .ty = typed_value.ty,
1884 .val = variable.variable.init,
1885 });
18781886 }
18791887 if (typed_value.val.isUndef()) {
18801888 const as_inst = try self.arena.allocator.create(Inst.BinOp);
......@@ -1964,6 +1972,21 @@ const EmitZIR = struct {
19641972 return self.emitPrimitive(src, .@"true")
19651973 else
19661974 return self.emitPrimitive(src, .@"false"),
1975 .EnumLiteral => {
1976 const enum_literal = @fieldParentPtr(Value.Payload.Bytes, "base", typed_value.val.ptr_otherwise);
1977 const inst = try self.arena.allocator.create(Inst.Str);
1978 inst.* = .{
1979 .base = .{
1980 .src = src,
1981 .tag = .enum_literal,
1982 },
1983 .positionals = .{
1984 .bytes = enum_literal.data,
1985 },
1986 .kw_args = .{},
1987 };
1988 return self.emitUnnamedDecl(&inst.base);
1989 },
19671990 else => |t| std.debug.panic("TODO implement emitTypedValue for {}", .{@tagName(t)}),
19681991 }
19691992 }
......@@ -2311,6 +2334,8 @@ const EmitZIR = struct {
23112334 };
23122335 break :blk &new_inst.base;
23132336 },
2337
2338 .varptr => @panic("TODO"),
23142339 };
23152340 try self.metadata.put(new_inst, .{ .deaths = inst.deaths });
23162341 try instructions.append(new_inst);
......@@ -2432,6 +2457,51 @@ const EmitZIR = struct {
24322457 };
24332458 return self.emitUnnamedDecl(&inst.base);
24342459 },
2460 .Array => {
2461 var len_pl = Value.Payload.Int_u64{ .int = ty.arrayLen() };
2462 const len = Value.initPayload(&len_pl.base);
2463
2464 const inst = if (ty.arraySentinel()) |sentinel| blk: {
2465 const inst = try self.arena.allocator.create(Inst.ArrayTypeSentinel);
2466 inst.* = .{
2467 .base = .{
2468 .src = src,
2469 .tag = .array_type,
2470 },
2471 .positionals = .{
2472 .len = (try self.emitTypedValue(src, .{
2473 .ty = Type.initTag(.usize),
2474 .val = len,
2475 })).inst,
2476 .sentinel = (try self.emitTypedValue(src, .{
2477 .ty = ty.elemType(),
2478 .val = sentinel,
2479 })).inst,
2480 .elem_type = (try self.emitType(src, ty.elemType())).inst,
2481 },
2482 .kw_args = .{},
2483 };
2484 break :blk &inst.base;
2485 } else blk: {
2486 const inst = try self.arena.allocator.create(Inst.BinOp);
2487 inst.* = .{
2488 .base = .{
2489 .src = src,
2490 .tag = .array_type,
2491 },
2492 .positionals = .{
2493 .lhs = (try self.emitTypedValue(src, .{
2494 .ty = Type.initTag(.usize),
2495 .val = len,
2496 })).inst,
2497 .rhs = (try self.emitType(src, ty.elemType())).inst,
2498 },
2499 .kw_args = .{},
2500 };
2501 break :blk &inst.base;
2502 };
2503 return self.emitUnnamedDecl(inst);
2504 },
24352505 else => std.debug.panic("TODO implement emitType for {}", .{ty}),
24362506 },
24372507 }
src-self-hosted/zir_sema.zig+3-4
......@@ -366,7 +366,7 @@ fn analyzeInstEnsureResultNonError(mod: *Module, scope: *Scope, inst: *zir.Inst.
366366fn analyzeInstAlloc(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
367367 const var_type = try resolveType(mod, scope, inst.positionals.operand);
368368 // TODO this should happen only for var allocs
369 if (!var_type.isValidVarType()) {
369 if (!var_type.isValidVarType(false)) {
370370 return mod.fail(scope, inst.base.src, "variable of type '{}' must be const or comptime", .{var_type});
371371 }
372372 const ptr_type = try mod.singlePtrType(scope, inst.base.src, true, var_type);
......@@ -581,8 +581,7 @@ fn analyzeInstDeclVal(mod: *Module, scope: *Scope, inst: *zir.Inst.DeclVal) Inne
581581
582582fn analyzeInstDeclValInModule(mod: *Module, scope: *Scope, inst: *zir.Inst.DeclValInModule) InnerError!*Inst {
583583 const decl = inst.positionals.decl;
584 const ptr = try mod.analyzeDeclRef(scope, inst.base.src, decl);
585 return mod.analyzeDeref(scope, inst.base.src, ptr, inst.base.src);
584 return mod.analyzeDeclRef(scope, inst.base.src, decl);
586585}
587586
588587fn analyzeInstCall(mod: *Module, scope: *Scope, inst: *zir.Inst.Call) InnerError!*Inst {
......@@ -779,7 +778,7 @@ fn analyzeInstFnType(mod: *Module, scope: *Scope, fntype: *zir.Inst.FnType) Inne
779778 for (fntype.positionals.param_types) |param_type, i| {
780779 const resolved = try resolveType(mod, scope, param_type);
781780 // TODO skip for comptime params
782 if (!resolved.isValidVarType()) {
781 if (!resolved.isValidVarType(false)) {
783782 return mod.fail(scope, param_type.src, "parameter of type '{}' must be declared comptime", .{resolved});
784783 }
785784 param_types[i] = resolved;
test/stage2/compare_output.zig+33
......@@ -586,6 +586,7 @@ pub fn addCases(ctx: *TestContext) !void {
586586 "",
587587 );
588588
589 // Character literals and multiline strings.
589590 case.addCompareOutput(
590591 \\export fn _start() noreturn {
591592 \\ const ignore =
......@@ -617,6 +618,38 @@ pub fn addCases(ctx: *TestContext) !void {
617618 ,
618619 "",
619620 );
621
622 // Global const.
623 case.addCompareOutput(
624 \\export fn _start() noreturn {
625 \\ add(aa, bb);
626 \\
627 \\ exit();
628 \\}
629 \\
630 \\const aa = 'ぁ';
631 \\const bb = '\x03';
632 \\
633 \\fn add(a: u32, b: u32) void {
634 \\ assert(a + b == 12356);
635 \\}
636 \\
637 \\pub fn assert(ok: bool) void {
638 \\ if (!ok) unreachable; // assertion failure
639 \\}
640 \\
641 \\fn exit() noreturn {
642 \\ asm volatile ("syscall"
643 \\ :
644 \\ : [number] "{rax}" (231),
645 \\ [arg1] "{rdi}" (0)
646 \\ : "rcx", "r11", "memory"
647 \\ );
648 \\ unreachable;
649 \\}
650 ,
651 "",
652 );
620653 }
621654
622655 {