authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-04-21 17:11:42-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-04-21 17:11:42-04:00
log25679b63eb01b7f4b1fb031afb9e355b90b2ea86
treef16c7263e4732c4bc6c4cac66f67332ab8804035
parent8671e8d6d4e570b104014859eaa66d70a0991217

ir: analyze primitive instruction


2 files changed, 34 insertions(+), 5 deletions(-)

src-self-hosted/ir.zig+5-1
......@@ -280,7 +280,7 @@ const Analyze = struct {
280280 .@"unreachable" => return self.fail(old_inst.src, "TODO implement analyzing {}", .{@tagName(old_inst.tag)}),
281281 .@"fn" => return self.analyzeInstFn(func, old_inst.cast(text.Inst.Fn).?),
282282 .@"export" => return self.fail(old_inst.src, "TODO implement analyzing {}", .{@tagName(old_inst.tag)}),
283 .primitive => return self.fail(old_inst.src, "TODO implement analyzing {}", .{@tagName(old_inst.tag)}),
283 .primitive => return self.analyzeInstPrimitive(func, old_inst.cast(text.Inst.Primitive).?),
284284 .fntype => return self.analyzeInstFnType(func, old_inst.cast(text.Inst.FnType).?),
285285 }
286286 }
......@@ -337,6 +337,10 @@ const Analyze = struct {
337337 return self.fail(fntype.base.src, "TODO implement fntype instruction more", .{});
338338 }
339339
340 fn analyzeInstPrimitive(self: *Analyze, opt_func: ?*Fn, primitive: *text.Inst.Primitive) InnerError!*Inst {
341 return self.constType(primitive.base.src, primitive.positionals.tag.toType());
342 }
343
340344 fn coerce(self: *Analyze, dest_type: Type, inst: *Inst) !*Inst {
341345 const in_memory_result = coerceInMemoryAllowed(dest_type, inst.ty);
342346 if (in_memory_result == .ok) {
src-self-hosted/ir/text.zig+29-4
......@@ -3,10 +3,9 @@
33const std = @import("std");
44const mem = std.mem;
55const Allocator = std.mem.Allocator;
6const Value = @import("../value.zig").Value;
76const assert = std.debug.assert;
8const ir = @import("../ir.zig");
97const BigInt = std.math.big.Int;
8const Type = @import("../type.zig").Type;
109
1110/// These are instructions that correspond to the ZIR text format. See `ir.Inst` for
1211/// in-memory, analyzed instructions with types and values.
......@@ -201,6 +200,34 @@ pub const Inst = struct {
201200 @"anyerror",
202201 @"comptime_int",
203202 @"comptime_float",
203
204 fn toType(self: BuiltinType) Type {
205 return switch (self) {
206 .@"isize" => Type.initTag(.@"isize"),
207 .@"usize" => Type.initTag(.@"usize"),
208 .@"c_short" => Type.initTag(.@"c_short"),
209 .@"c_ushort" => Type.initTag(.@"c_ushort"),
210 .@"c_int" => Type.initTag(.@"c_int"),
211 .@"c_uint" => Type.initTag(.@"c_uint"),
212 .@"c_long" => Type.initTag(.@"c_long"),
213 .@"c_ulong" => Type.initTag(.@"c_ulong"),
214 .@"c_longlong" => Type.initTag(.@"c_longlong"),
215 .@"c_ulonglong" => Type.initTag(.@"c_ulonglong"),
216 .@"c_longdouble" => Type.initTag(.@"c_longdouble"),
217 .@"c_void" => Type.initTag(.@"c_void"),
218 .@"f16" => Type.initTag(.@"f16"),
219 .@"f32" => Type.initTag(.@"f32"),
220 .@"f64" => Type.initTag(.@"f64"),
221 .@"f128" => Type.initTag(.@"f128"),
222 .@"bool" => Type.initTag(.@"bool"),
223 .@"void" => Type.initTag(.@"void"),
224 .@"noreturn" => Type.initTag(.@"noreturn"),
225 .@"type" => Type.initTag(.@"type"),
226 .@"anyerror" => Type.initTag(.@"anyerror"),
227 .@"comptime_int" => Type.initTag(.@"comptime_int"),
228 .@"comptime_float" => Type.initTag(.@"comptime_float"),
229 };
230 }
204231 };
205232 };
206233
......@@ -337,7 +364,6 @@ pub const Module = struct {
337364 return stream.writeAll(@tagName(param));
338365 }
339366 switch (@TypeOf(param)) {
340 Value => return stream.print("{}", .{param}),
341367 *Inst => return self.writeInstParamToStream(stream, param, inst_table),
342368 []*Inst => {
343369 try stream.writeByte('[');
......@@ -693,7 +719,6 @@ const Parser = struct {
693719 return instructions.toOwnedSlice();
694720 },
695721 *Inst => return parseParameterInst(self, body_ctx),
696 Value => return self.fail("TODO implement parseParameterGeneric for type Value", .{}),
697722 []u8 => return self.parseStringLiteral(),
698723 BigInt => return self.parseIntegerLiteral(),
699724 else => @compileError("Unimplemented: ir parseParameterGeneric for type " ++ @typeName(T)),