| ... | ... | @@ -27,9 +27,11 @@ pub const Inst = struct { |
| 27 | 27 | pub const Tag = enum { |
| 28 | 28 | breakpoint, |
| 29 | 29 | call, |
| 30 | | /// Represents a reference to a global decl by name. |
| 31 | | /// The syntax `@foo` is equivalent to `declref("foo")`. |
| 30 | /// Represents a pointer to a global decl by name. |
| 32 | 31 | declref, |
| 32 | /// The syntax `@foo` is equivalent to `declval("foo")`. |
| 33 | /// declval is equivalent to declref followed by deref. |
| 34 | declval, |
| 33 | 35 | str, |
| 34 | 36 | int, |
| 35 | 37 | ptrtoint, |
| ... | ... | @@ -59,6 +61,7 @@ pub const Inst = struct { |
| 59 | 61 | .breakpoint => Breakpoint, |
| 60 | 62 | .call => Call, |
| 61 | 63 | .declref => DeclRef, |
| 64 | .declval => DeclVal, |
| 62 | 65 | .str => Str, |
| 63 | 66 | .int => Int, |
| 64 | 67 | .ptrtoint => PtrToInt, |
| ... | ... | @@ -122,6 +125,16 @@ pub const Inst = struct { |
| 122 | 125 | kw_args: struct {}, |
| 123 | 126 | }; |
| 124 | 127 | |
| 128 | pub const DeclVal = struct { |
| 129 | pub const base_tag = Tag.declval; |
| 130 | base: Inst, |
| 131 | |
| 132 | positionals: struct { |
| 133 | name: []const u8, |
| 134 | }, |
| 135 | kw_args: struct {}, |
| 136 | }; |
| 137 | |
| 125 | 138 | pub const Str = struct { |
| 126 | 139 | pub const base_tag = Tag.str; |
| 127 | 140 | base: Inst, |
| ... | ... | @@ -254,11 +267,11 @@ pub const Inst = struct { |
| 254 | 267 | base: Inst, |
| 255 | 268 | |
| 256 | 269 | positionals: struct { |
| 257 | | tag: BuiltinType, |
| 270 | tag: Builtin, |
| 258 | 271 | }, |
| 259 | 272 | kw_args: struct {}, |
| 260 | 273 | |
| 261 | | pub const BuiltinType = enum { |
| 274 | pub const Builtin = enum { |
| 262 | 275 | isize, |
| 263 | 276 | usize, |
| 264 | 277 | c_short, |
| ... | ... | @@ -282,32 +295,42 @@ pub const Inst = struct { |
| 282 | 295 | anyerror, |
| 283 | 296 | comptime_int, |
| 284 | 297 | comptime_float, |
| 298 | @"true", |
| 299 | @"false", |
| 300 | @"null", |
| 301 | @"undefined", |
| 302 | void_value, |
| 285 | 303 | |
| 286 | | pub fn toType(self: BuiltinType) Type { |
| 304 | pub fn toTypedValue(self: Builtin) TypedValue { |
| 287 | 305 | return switch (self) { |
| 288 | | .isize => Type.initTag(.isize), |
| 289 | | .usize => Type.initTag(.usize), |
| 290 | | .c_short => Type.initTag(.c_short), |
| 291 | | .c_ushort => Type.initTag(.c_ushort), |
| 292 | | .c_int => Type.initTag(.c_int), |
| 293 | | .c_uint => Type.initTag(.c_uint), |
| 294 | | .c_long => Type.initTag(.c_long), |
| 295 | | .c_ulong => Type.initTag(.c_ulong), |
| 296 | | .c_longlong => Type.initTag(.c_longlong), |
| 297 | | .c_ulonglong => Type.initTag(.c_ulonglong), |
| 298 | | .c_longdouble => Type.initTag(.c_longdouble), |
| 299 | | .c_void => Type.initTag(.c_void), |
| 300 | | .f16 => Type.initTag(.f16), |
| 301 | | .f32 => Type.initTag(.f32), |
| 302 | | .f64 => Type.initTag(.f64), |
| 303 | | .f128 => Type.initTag(.f128), |
| 304 | | .bool => Type.initTag(.bool), |
| 305 | | .void => Type.initTag(.void), |
| 306 | | .noreturn => Type.initTag(.noreturn), |
| 307 | | .type => Type.initTag(.type), |
| 308 | | .anyerror => Type.initTag(.anyerror), |
| 309 | | .comptime_int => Type.initTag(.comptime_int), |
| 310 | | .comptime_float => Type.initTag(.comptime_float), |
| 306 | .isize => .{ .ty = Type.initTag(.type), .val = Value.initTag(.isize_type) }, |
| 307 | .usize => .{ .ty = Type.initTag(.type), .val = Value.initTag(.usize_type) }, |
| 308 | .c_short => .{ .ty = Type.initTag(.type), .val = Value.initTag(.c_short_type) }, |
| 309 | .c_ushort => .{ .ty = Type.initTag(.type), .val = Value.initTag(.c_ushort_type) }, |
| 310 | .c_int => .{ .ty = Type.initTag(.type), .val = Value.initTag(.c_int_type) }, |
| 311 | .c_uint => .{ .ty = Type.initTag(.type), .val = Value.initTag(.c_uint_type) }, |
| 312 | .c_long => .{ .ty = Type.initTag(.type), .val = Value.initTag(.c_long_type) }, |
| 313 | .c_ulong => .{ .ty = Type.initTag(.type), .val = Value.initTag(.c_ulong_type) }, |
| 314 | .c_longlong => .{ .ty = Type.initTag(.type), .val = Value.initTag(.c_longlong_type) }, |
| 315 | .c_ulonglong => .{ .ty = Type.initTag(.type), .val = Value.initTag(.c_ulonglong_type) }, |
| 316 | .c_longdouble => .{ .ty = Type.initTag(.type), .val = Value.initTag(.c_longdouble_type) }, |
| 317 | .c_void => .{ .ty = Type.initTag(.type), .val = Value.initTag(.c_void_type) }, |
| 318 | .f16 => .{ .ty = Type.initTag(.type), .val = Value.initTag(.f16_type) }, |
| 319 | .f32 => .{ .ty = Type.initTag(.type), .val = Value.initTag(.f32_type) }, |
| 320 | .f64 => .{ .ty = Type.initTag(.type), .val = Value.initTag(.f64_type) }, |
| 321 | .f128 => .{ .ty = Type.initTag(.type), .val = Value.initTag(.f128_type) }, |
| 322 | .bool => .{ .ty = Type.initTag(.type), .val = Value.initTag(.bool_type) }, |
| 323 | .void => .{ .ty = Type.initTag(.type), .val = Value.initTag(.void_type) }, |
| 324 | .noreturn => .{ .ty = Type.initTag(.type), .val = Value.initTag(.noreturn_type) }, |
| 325 | .type => .{ .ty = Type.initTag(.type), .val = Value.initTag(.type_type) }, |
| 326 | .anyerror => .{ .ty = Type.initTag(.type), .val = Value.initTag(.anyerror_type) }, |
| 327 | .comptime_int => .{ .ty = Type.initTag(.type), .val = Value.initTag(.comptime_int_type) }, |
| 328 | .comptime_float => .{ .ty = Type.initTag(.type), .val = Value.initTag(.comptime_float_type) }, |
| 329 | .@"true" => .{ .ty = Type.initTag(.bool), .val = Value.initTag(.bool_true) }, |
| 330 | .@"false" => .{ .ty = Type.initTag(.bool), .val = Value.initTag(.bool_false) }, |
| 331 | .@"null" => .{ .ty = Type.initTag(.@"null"), .val = Value.initTag(.null_value) }, |
| 332 | .@"undefined" => .{ .ty = Type.initTag(.@"undefined"), .val = Value.initTag(.undef) }, |
| 333 | .void_value => .{ .ty = Type.initTag(.void), .val = Value.initTag(.the_one_possible_value) }, |
| 311 | 334 | }; |
| 312 | 335 | } |
| 313 | 336 | }; |
| ... | ... | @@ -440,7 +463,7 @@ pub const Module = struct { |
| 440 | 463 | self.writeToStream(std.heap.page_allocator, std.io.getStdErr().outStream()) catch {}; |
| 441 | 464 | } |
| 442 | 465 | |
| 443 | | const InstPtrTable = std.AutoHashMap(*Inst, struct { index: usize, fn_body: ?*Module.Body }); |
| 466 | const InstPtrTable = std.AutoHashMap(*Inst, struct { inst: *Inst, index: ?usize }); |
| 444 | 467 | |
| 445 | 468 | /// TODO Look into making a table to speed this up. |
| 446 | 469 | pub fn findDecl(self: Module, name: []const u8) ?*Inst { |
| ... | ... | @@ -462,17 +485,17 @@ pub const Module = struct { |
| 462 | 485 | try inst_table.ensureCapacity(self.decls.len); |
| 463 | 486 | |
| 464 | 487 | for (self.decls) |decl, decl_i| { |
| 465 | | try inst_table.putNoClobber(decl, .{ .index = decl_i, .fn_body = null }); |
| 488 | try inst_table.putNoClobber(decl, .{ .inst = decl, .index = null }); |
| 466 | 489 | |
| 467 | 490 | if (decl.cast(Inst.Fn)) |fn_inst| { |
| 468 | 491 | for (fn_inst.positionals.body.instructions) |inst, inst_i| { |
| 469 | | try inst_table.putNoClobber(inst, .{ .index = inst_i, .fn_body = &fn_inst.positionals.body }); |
| 492 | try inst_table.putNoClobber(inst, .{ .inst = inst, .index = inst_i }); |
| 470 | 493 | } |
| 471 | 494 | } |
| 472 | 495 | } |
| 473 | 496 | |
| 474 | 497 | for (self.decls) |decl, i| { |
| 475 | | try stream.print("@{} ", .{i}); |
| 498 | try stream.print("@{} ", .{decl.name}); |
| 476 | 499 | try self.writeInstToStream(stream, decl, &inst_table); |
| 477 | 500 | try stream.writeByte('\n'); |
| 478 | 501 | } |
| ... | ... | @@ -489,6 +512,7 @@ pub const Module = struct { |
| 489 | 512 | .breakpoint => return self.writeInstToStreamGeneric(stream, .breakpoint, decl, inst_table), |
| 490 | 513 | .call => return self.writeInstToStreamGeneric(stream, .call, decl, inst_table), |
| 491 | 514 | .declref => return self.writeInstToStreamGeneric(stream, .declref, decl, inst_table), |
| 515 | .declval => return self.writeInstToStreamGeneric(stream, .declval, decl, inst_table), |
| 492 | 516 | .str => return self.writeInstToStreamGeneric(stream, .str, decl, inst_table), |
| 493 | 517 | .int => return self.writeInstToStreamGeneric(stream, .int, decl, inst_table), |
| 494 | 518 | .ptrtoint => return self.writeInstToStreamGeneric(stream, .ptrtoint, decl, inst_table), |
| ... | ... | @@ -587,9 +611,18 @@ pub const Module = struct { |
| 587 | 611 | } |
| 588 | 612 | |
| 589 | 613 | fn writeInstParamToStream(self: Module, stream: var, inst: *Inst, inst_table: *const InstPtrTable) !void { |
| 590 | | const info = inst_table.getValue(inst).?; |
| 591 | | const prefix = if (info.fn_body == null) "@" else "%"; |
| 592 | | try stream.print("{}{}", .{ prefix, info.index }); |
| 614 | if (inst_table.getValue(inst)) |info| { |
| 615 | if (info.index) |i| { |
| 616 | try stream.print("%{}", .{info.index}); |
| 617 | } else { |
| 618 | try stream.print("@{}", .{info.inst.name}); |
| 619 | } |
| 620 | } else if (inst.cast(Inst.DeclVal)) |decl_val| { |
| 621 | try stream.print("@{}", .{decl_val.positionals.name}); |
| 622 | } else { |
| 623 | //try stream.print("?", .{}); |
| 624 | unreachable; |
| 625 | } |
| 593 | 626 | } |
| 594 | 627 | }; |
| 595 | 628 | |
| ... | ... | @@ -964,47 +997,17 @@ const Parser = struct { |
| 964 | 997 | self.i = src; |
| 965 | 998 | return self.fail("unrecognized identifier: {}", .{bad_name}); |
| 966 | 999 | } else { |
| 967 | | const name_array = try self.arena.allocator.create(Inst.Str); |
| 968 | | name_array.* = .{ |
| 969 | | .base = .{ |
| 970 | | .name = try self.generateName(), |
| 971 | | .src = src, |
| 972 | | .tag = Inst.Str.base_tag, |
| 973 | | }, |
| 974 | | .positionals = .{ .bytes = ident }, |
| 975 | | .kw_args = .{}, |
| 976 | | }; |
| 977 | | const name = try self.arena.allocator.create(Inst.Ref); |
| 978 | | name.* = .{ |
| 979 | | .base = .{ |
| 980 | | .name = try self.generateName(), |
| 981 | | .src = src, |
| 982 | | .tag = Inst.Ref.base_tag, |
| 983 | | }, |
| 984 | | .positionals = .{ .operand = &name_array.base }, |
| 985 | | .kw_args = .{}, |
| 986 | | }; |
| 987 | | const declref = try self.arena.allocator.create(Inst.DeclRef); |
| 988 | | declref.* = .{ |
| 989 | | .base = .{ |
| 990 | | .name = try self.generateName(), |
| 991 | | .src = src, |
| 992 | | .tag = Inst.DeclRef.base_tag, |
| 993 | | }, |
| 994 | | .positionals = .{ .name = &name.base }, |
| 995 | | .kw_args = .{}, |
| 996 | | }; |
| 997 | | const deref = try self.arena.allocator.create(Inst.Deref); |
| 998 | | deref.* = .{ |
| 1000 | const declval = try self.arena.allocator.create(Inst.DeclVal); |
| 1001 | declval.* = .{ |
| 999 | 1002 | .base = .{ |
| 1000 | 1003 | .name = try self.generateName(), |
| 1001 | 1004 | .src = src, |
| 1002 | | .tag = Inst.Deref.base_tag, |
| 1005 | .tag = Inst.DeclVal.base_tag, |
| 1003 | 1006 | }, |
| 1004 | | .positionals = .{ .ptr = &declref.base }, |
| 1007 | .positionals = .{ .name = ident }, |
| 1005 | 1008 | .kw_args = .{}, |
| 1006 | 1009 | }; |
| 1007 | | return &deref.base; |
| 1010 | return &declval.base; |
| 1008 | 1011 | } |
| 1009 | 1012 | }; |
| 1010 | 1013 | if (local_ref) { |
| ... | ... | @@ -1025,12 +1028,15 @@ pub fn emit(allocator: *Allocator, old_module: IrModule) !Module { |
| 1025 | 1028 | var ctx: EmitZIR = .{ |
| 1026 | 1029 | .allocator = allocator, |
| 1027 | 1030 | .decls = .{}, |
| 1028 | | .decl_table = std.AutoHashMap(*ir.Inst, *Inst).init(allocator), |
| 1029 | 1031 | .arena = std.heap.ArenaAllocator.init(allocator), |
| 1030 | 1032 | .old_module = &old_module, |
| 1033 | .next_auto_name = 0, |
| 1034 | .names = std.StringHashMap(void).init(allocator), |
| 1035 | .primitive_table = std.AutoHashMap(Inst.Primitive.Builtin, *Inst).init(allocator), |
| 1031 | 1036 | }; |
| 1032 | 1037 | defer ctx.decls.deinit(allocator); |
| 1033 | | defer ctx.decl_table.deinit(); |
| 1038 | defer ctx.names.deinit(); |
| 1039 | defer ctx.primitive_table.deinit(); |
| 1034 | 1040 | errdefer ctx.arena.deinit(); |
| 1035 | 1041 | |
| 1036 | 1042 | try ctx.emit(); |
| ... | ... | @@ -1046,47 +1052,90 @@ const EmitZIR = struct { |
| 1046 | 1052 | arena: std.heap.ArenaAllocator, |
| 1047 | 1053 | old_module: *const IrModule, |
| 1048 | 1054 | decls: std.ArrayListUnmanaged(*Inst), |
| 1049 | | decl_table: std.AutoHashMap(*ir.Inst, *Inst), |
| 1055 | names: std.StringHashMap(void), |
| 1056 | next_auto_name: usize, |
| 1057 | primitive_table: std.AutoHashMap(Inst.Primitive.Builtin, *Inst), |
| 1050 | 1058 | |
| 1051 | 1059 | fn emit(self: *EmitZIR) !void { |
| 1052 | | var it = self.old_module.decl_exports.iterator(); |
| 1053 | | while (it.next()) |kv| { |
| 1054 | | const decl = kv.key; |
| 1055 | | const exports = kv.value; |
| 1056 | | const export_value = try self.emitTypedValue(decl.src, decl.typed_value.most_recent.typed_value); |
| 1057 | | for (exports) |module_export| { |
| 1058 | | const symbol_name = try self.emitStringLiteral(module_export.src, module_export.options.name); |
| 1059 | | const export_inst = try self.arena.allocator.create(Inst.Export); |
| 1060 | | export_inst.* = .{ |
| 1061 | | .base = .{ |
| 1062 | | .name = try self.autoName(), |
| 1063 | | .src = module_export.src, |
| 1064 | | .tag = Inst.Export.base_tag, |
| 1065 | | }, |
| 1066 | | .positionals = .{ |
| 1067 | | .symbol_name = symbol_name, |
| 1068 | | .value = export_value, |
| 1069 | | }, |
| 1070 | | .kw_args = .{}, |
| 1071 | | }; |
| 1072 | | try self.decls.append(self.allocator, &export_inst.base); |
| 1060 | // Put all the Decls in a list and sort them by name to avoid nondeterminism introduced |
| 1061 | // by the hash table. |
| 1062 | var src_decls = std.ArrayList(*IrModule.Decl).init(self.allocator); |
| 1063 | defer src_decls.deinit(); |
| 1064 | try src_decls.ensureCapacity(self.old_module.decl_table.size); |
| 1065 | try self.decls.ensureCapacity(self.allocator, self.old_module.decl_table.size); |
| 1066 | try self.names.ensureCapacity(self.old_module.decl_table.size); |
| 1067 | |
| 1068 | var decl_it = self.old_module.decl_table.iterator(); |
| 1069 | while (decl_it.next()) |kv| { |
| 1070 | const decl = kv.value; |
| 1071 | src_decls.appendAssumeCapacity(decl); |
| 1072 | self.names.putAssumeCapacityNoClobber(mem.spanZ(decl.name), {}); |
| 1073 | } |
| 1074 | std.sort.sort(*IrModule.Decl, src_decls.items, {}, (struct { |
| 1075 | fn lessThan(context: void, a: *IrModule.Decl, b: *IrModule.Decl) bool { |
| 1076 | return a.src < b.src; |
| 1077 | } |
| 1078 | }).lessThan); |
| 1079 | |
| 1080 | // Emit all the decls. |
| 1081 | for (src_decls.items) |ir_decl| { |
| 1082 | if (self.old_module.export_owners.getValue(ir_decl)) |exports| { |
| 1083 | for (exports) |module_export| { |
| 1084 | const declval = try self.emitDeclVal(ir_decl.src, mem.spanZ(module_export.exported_decl.name)); |
| 1085 | const symbol_name = try self.emitStringLiteral(module_export.src, module_export.options.name); |
| 1086 | const export_inst = try self.arena.allocator.create(Inst.Export); |
| 1087 | export_inst.* = .{ |
| 1088 | .base = .{ |
| 1089 | .name = try self.autoName(), |
| 1090 | .src = module_export.src, |
| 1091 | .tag = Inst.Export.base_tag, |
| 1092 | }, |
| 1093 | .positionals = .{ |
| 1094 | .symbol_name = symbol_name, |
| 1095 | .value = declval, |
| 1096 | }, |
| 1097 | .kw_args = .{}, |
| 1098 | }; |
| 1099 | try self.decls.append(self.allocator, &export_inst.base); |
| 1100 | } |
| 1101 | } else { |
| 1102 | const new_decl = try self.emitTypedValue(ir_decl.src, ir_decl.typed_value.most_recent.typed_value); |
| 1103 | new_decl.name = try self.arena.allocator.dupe(u8, mem.spanZ(ir_decl.name)); |
| 1073 | 1104 | } |
| 1074 | 1105 | } |
| 1075 | 1106 | } |
| 1076 | 1107 | |
| 1077 | | fn resolveInst(self: *EmitZIR, inst_table: *const std.AutoHashMap(*ir.Inst, *Inst), inst: *ir.Inst) !*Inst { |
| 1108 | fn resolveInst(self: *EmitZIR, inst_table: *std.AutoHashMap(*ir.Inst, *Inst), inst: *ir.Inst) !*Inst { |
| 1078 | 1109 | if (inst.cast(ir.Inst.Constant)) |const_inst| { |
| 1079 | | if (self.decl_table.getValue(inst)) |decl| { |
| 1080 | | return decl; |
| 1081 | | } |
| 1082 | | const new_decl = try self.emitTypedValue(inst.src, .{ .ty = inst.ty, .val = const_inst.val }); |
| 1083 | | try self.decl_table.putNoClobber(inst, new_decl); |
| 1110 | const new_decl = if (const_inst.val.cast(Value.Payload.Function)) |func_pl| blk: { |
| 1111 | const owner_decl = func_pl.func.owner_decl; |
| 1112 | break :blk try self.emitDeclVal(inst.src, mem.spanZ(owner_decl.name)); |
| 1113 | } else if (const_inst.val.cast(Value.Payload.DeclRef)) |declref| blk: { |
| 1114 | break :blk try self.emitDeclRef(inst.src, declref.decl); |
| 1115 | } else blk: { |
| 1116 | break :blk try self.emitTypedValue(inst.src, .{ .ty = inst.ty, .val = const_inst.val }); |
| 1117 | }; |
| 1118 | try inst_table.putNoClobber(inst, new_decl); |
| 1084 | 1119 | return new_decl; |
| 1085 | 1120 | } else { |
| 1086 | 1121 | return inst_table.getValue(inst).?; |
| 1087 | 1122 | } |
| 1088 | 1123 | } |
| 1089 | 1124 | |
| 1125 | fn emitDeclVal(self: *EmitZIR, src: usize, decl_name: []const u8) !*Inst { |
| 1126 | const declval = try self.arena.allocator.create(Inst.DeclVal); |
| 1127 | declval.* = .{ |
| 1128 | .base = .{ |
| 1129 | .name = try self.autoName(), |
| 1130 | .src = src, |
| 1131 | .tag = Inst.DeclVal.base_tag, |
| 1132 | }, |
| 1133 | .positionals = .{ .name = try self.arena.allocator.dupe(u8, decl_name) }, |
| 1134 | .kw_args = .{}, |
| 1135 | }; |
| 1136 | return &declval.base; |
| 1137 | } |
| 1138 | |
| 1090 | 1139 | fn emitComptimeIntVal(self: *EmitZIR, src: usize, val: Value) !*Inst { |
| 1091 | 1140 | const big_int_space = try self.arena.allocator.create(Value.BigIntSpace); |
| 1092 | 1141 | const int_inst = try self.arena.allocator.create(Inst.Int); |
| ... | ... | @@ -1105,8 +1154,31 @@ const EmitZIR = struct { |
| 1105 | 1154 | return &int_inst.base; |
| 1106 | 1155 | } |
| 1107 | 1156 | |
| 1157 | fn emitDeclRef(self: *EmitZIR, src: usize, decl: *IrModule.Decl) !*Inst { |
| 1158 | const declval = try self.emitDeclVal(src, mem.spanZ(decl.name)); |
| 1159 | const ref_inst = try self.arena.allocator.create(Inst.Ref); |
| 1160 | ref_inst.* = .{ |
| 1161 | .base = .{ |
| 1162 | .name = try self.autoName(), |
| 1163 | .src = src, |
| 1164 | .tag = Inst.Ref.base_tag, |
| 1165 | }, |
| 1166 | .positionals = .{ |
| 1167 | .operand = declval, |
| 1168 | }, |
| 1169 | .kw_args = .{}, |
| 1170 | }; |
| 1171 | try self.decls.append(self.allocator, &ref_inst.base); |
| 1172 | |
| 1173 | return &ref_inst.base; |
| 1174 | } |
| 1175 | |
| 1108 | 1176 | fn emitTypedValue(self: *EmitZIR, src: usize, typed_value: TypedValue) Allocator.Error!*Inst { |
| 1109 | 1177 | const allocator = &self.arena.allocator; |
| 1178 | if (typed_value.val.cast(Value.Payload.DeclRef)) |decl_ref| { |
| 1179 | const decl = decl_ref.decl; |
| 1180 | return self.emitDeclRef(src, decl); |
| 1181 | } |
| 1110 | 1182 | switch (typed_value.ty.zigTypeTag()) { |
| 1111 | 1183 | .Pointer => { |
| 1112 | 1184 | const ptr_elem_type = typed_value.ty.elemType(); |
| ... | ... | @@ -1142,7 +1214,6 @@ const EmitZIR = struct { |
| 1142 | 1214 | }, |
| 1143 | 1215 | .kw_args = .{}, |
| 1144 | 1216 | }; |
| 1145 | | try self.decls.append(self.allocator, &as_inst.base); |
| 1146 | 1217 | |
| 1147 | 1218 | return &as_inst.base; |
| 1148 | 1219 | }, |
| ... | ... | @@ -1182,6 +1253,33 @@ const EmitZIR = struct { |
| 1182 | 1253 | try self.decls.append(self.allocator, &fn_inst.base); |
| 1183 | 1254 | return &fn_inst.base; |
| 1184 | 1255 | }, |
| 1256 | .Array => { |
| 1257 | // TODO more checks to make sure this can be emitted as a string literal |
| 1258 | //const array_elem_type = ptr_elem_type.elemType(); |
| 1259 | //if (array_elem_type.eql(Type.initTag(.u8)) and |
| 1260 | // ptr_elem_type.hasSentinel(Value.initTag(.zero))) |
| 1261 | //{ |
| 1262 | //} |
| 1263 | const bytes = typed_value.val.toAllocatedBytes(allocator) catch |err| switch (err) { |
| 1264 | error.AnalysisFail => unreachable, |
| 1265 | else => |e| return e, |
| 1266 | }; |
| 1267 | const str_inst = try self.arena.allocator.create(Inst.Str); |
| 1268 | str_inst.* = .{ |
| 1269 | .base = .{ |
| 1270 | .name = try self.autoName(), |
| 1271 | .src = src, |
| 1272 | .tag = Inst.Str.base_tag, |
| 1273 | }, |
| 1274 | .positionals = .{ |
| 1275 | .bytes = bytes, |
| 1276 | }, |
| 1277 | .kw_args = .{}, |
| 1278 | }; |
| 1279 | try self.decls.append(self.allocator, &str_inst.base); |
| 1280 | return &str_inst.base; |
| 1281 | }, |
| 1282 | .Void => return self.emitPrimitive(src, .void_value), |
| 1185 | 1283 | else => |t| std.debug.panic("TODO implement emitTypedValue for {}", .{@tagName(t)}), |
| 1186 | 1284 | } |
| 1187 | 1285 | } |
| ... | ... | @@ -1395,30 +1493,30 @@ const EmitZIR = struct { |
| 1395 | 1493 | |
| 1396 | 1494 | fn emitType(self: *EmitZIR, src: usize, ty: Type) Allocator.Error!*Inst { |
| 1397 | 1495 | switch (ty.tag()) { |
| 1398 | | .isize => return self.emitPrimitiveType(src, .isize), |
| 1399 | | .usize => return self.emitPrimitiveType(src, .usize), |
| 1400 | | .c_short => return self.emitPrimitiveType(src, .c_short), |
| 1401 | | .c_ushort => return self.emitPrimitiveType(src, .c_ushort), |
| 1402 | | .c_int => return self.emitPrimitiveType(src, .c_int), |
| 1403 | | .c_uint => return self.emitPrimitiveType(src, .c_uint), |
| 1404 | | .c_long => return self.emitPrimitiveType(src, .c_long), |
| 1405 | | .c_ulong => return self.emitPrimitiveType(src, .c_ulong), |
| 1406 | | .c_longlong => return self.emitPrimitiveType(src, .c_longlong), |
| 1407 | | .c_ulonglong => return self.emitPrimitiveType(src, .c_ulonglong), |
| 1408 | | .c_longdouble => return self.emitPrimitiveType(src, .c_longdouble), |
| 1409 | | .c_void => return self.emitPrimitiveType(src, .c_void), |
| 1410 | | .f16 => return self.emitPrimitiveType(src, .f16), |
| 1411 | | .f32 => return self.emitPrimitiveType(src, .f32), |
| 1412 | | .f64 => return self.emitPrimitiveType(src, .f64), |
| 1413 | | .f128 => return self.emitPrimitiveType(src, .f128), |
| 1414 | | .anyerror => return self.emitPrimitiveType(src, .anyerror), |
| 1496 | .isize => return self.emitPrimitive(src, .isize), |
| 1497 | .usize => return self.emitPrimitive(src, .usize), |
| 1498 | .c_short => return self.emitPrimitive(src, .c_short), |
| 1499 | .c_ushort => return self.emitPrimitive(src, .c_ushort), |
| 1500 | .c_int => return self.emitPrimitive(src, .c_int), |
| 1501 | .c_uint => return self.emitPrimitive(src, .c_uint), |
| 1502 | .c_long => return self.emitPrimitive(src, .c_long), |
| 1503 | .c_ulong => return self.emitPrimitive(src, .c_ulong), |
| 1504 | .c_longlong => return self.emitPrimitive(src, .c_longlong), |
| 1505 | .c_ulonglong => return self.emitPrimitive(src, .c_ulonglong), |
| 1506 | .c_longdouble => return self.emitPrimitive(src, .c_longdouble), |
| 1507 | .c_void => return self.emitPrimitive(src, .c_void), |
| 1508 | .f16 => return self.emitPrimitive(src, .f16), |
| 1509 | .f32 => return self.emitPrimitive(src, .f32), |
| 1510 | .f64 => return self.emitPrimitive(src, .f64), |
| 1511 | .f128 => return self.emitPrimitive(src, .f128), |
| 1512 | .anyerror => return self.emitPrimitive(src, .anyerror), |
| 1415 | 1513 | else => switch (ty.zigTypeTag()) { |
| 1416 | | .Bool => return self.emitPrimitiveType(src, .bool), |
| 1417 | | .Void => return self.emitPrimitiveType(src, .void), |
| 1418 | | .NoReturn => return self.emitPrimitiveType(src, .noreturn), |
| 1419 | | .Type => return self.emitPrimitiveType(src, .type), |
| 1420 | | .ComptimeInt => return self.emitPrimitiveType(src, .comptime_int), |
| 1421 | | .ComptimeFloat => return self.emitPrimitiveType(src, .comptime_float), |
| 1514 | .Bool => return self.emitPrimitive(src, .bool), |
| 1515 | .Void => return self.emitPrimitive(src, .void), |
| 1516 | .NoReturn => return self.emitPrimitive(src, .noreturn), |
| 1517 | .Type => return self.emitPrimitive(src, .type), |
| 1518 | .ComptimeInt => return self.emitPrimitive(src, .comptime_int), |
| 1519 | .ComptimeFloat => return self.emitPrimitive(src, .comptime_float), |
| 1422 | 1520 | .Fn => { |
| 1423 | 1521 | const param_types = try self.allocator.alloc(Type, ty.fnParamLen()); |
| 1424 | 1522 | defer self.allocator.free(param_types); |
| ... | ... | @@ -1453,24 +1551,36 @@ const EmitZIR = struct { |
| 1453 | 1551 | } |
| 1454 | 1552 | |
| 1455 | 1553 | fn autoName(self: *EmitZIR) ![]u8 { |
| 1456 | | return std.fmt.allocPrint(&self.arena.allocator, "{}", .{self.decls.items.len}); |
| 1554 | while (true) { |
| 1555 | const proposed_name = try std.fmt.allocPrint(&self.arena.allocator, "unnamed${}", .{self.next_auto_name}); |
| 1556 | self.next_auto_name += 1; |
| 1557 | const gop = try self.names.getOrPut(proposed_name); |
| 1558 | if (!gop.found_existing) { |
| 1559 | gop.kv.value = {}; |
| 1560 | return proposed_name; |
| 1561 | } |
| 1562 | } |
| 1457 | 1563 | } |
| 1458 | 1564 | |
| 1459 | | fn emitPrimitiveType(self: *EmitZIR, src: usize, tag: Inst.Primitive.BuiltinType) !*Inst { |
| 1460 | | const primitive_inst = try self.arena.allocator.create(Inst.Primitive); |
| 1461 | | primitive_inst.* = .{ |
| 1462 | | .base = .{ |
| 1463 | | .name = try self.autoName(), |
| 1464 | | .src = src, |
| 1465 | | .tag = Inst.Primitive.base_tag, |
| 1466 | | }, |
| 1467 | | .positionals = .{ |
| 1468 | | .tag = tag, |
| 1469 | | }, |
| 1470 | | .kw_args = .{}, |
| 1471 | | }; |
| 1472 | | try self.decls.append(self.allocator, &primitive_inst.base); |
| 1473 | | return &primitive_inst.base; |
| 1565 | fn emitPrimitive(self: *EmitZIR, src: usize, tag: Inst.Primitive.Builtin) !*Inst { |
| 1566 | const gop = try self.primitive_table.getOrPut(tag); |
| 1567 | if (!gop.found_existing) { |
| 1568 | const primitive_inst = try self.arena.allocator.create(Inst.Primitive); |
| 1569 | primitive_inst.* = .{ |
| 1570 | .base = .{ |
| 1571 | .name = try self.autoName(), |
| 1572 | .src = src, |
| 1573 | .tag = Inst.Primitive.base_tag, |
| 1574 | }, |
| 1575 | .positionals = .{ |
| 1576 | .tag = tag, |
| 1577 | }, |
| 1578 | .kw_args = .{}, |
| 1579 | }; |
| 1580 | try self.decls.append(self.allocator, &primitive_inst.base); |
| 1581 | gop.kv.value = &primitive_inst.base; |
| 1582 | } |
| 1583 | return gop.kv.value; |
| 1474 | 1584 | } |
| 1475 | 1585 | |
| 1476 | 1586 | fn emitStringLiteral(self: *EmitZIR, src: usize, str: []const u8) !*Inst { |