authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-04-19 00:38:56-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-04-19 19:31:50-04:00
logbd37c8d8ed24f7e8887986ffcae67af30b9ce23c
treeb41b16fe506f8f59f6028ddd868edbf5f3f8407b
parent49e2f3ca36edddc16d13959bc4b16745a04c6383

ir: parse ptrtoint


1 files changed, 23 insertions(+), 3 deletions(-)

src-self-hosted/ir.zig+23-3
......@@ -150,7 +150,7 @@ pub fn parseRoot(ctx: *ParseContext) !void {
150150 while (ctx.i < ctx.source.len) : (ctx.i += 1) switch (ctx.source[ctx.i]) {
151151 ';' => _ = try skipToAndOver(ctx, '\n'),
152152 '@' => {
153 const at_start = ctx.i;
153 ctx.i += 1;
154154 const ident = try skipToAndOver(ctx, ' ');
155155 const opt_type = try parseOptionalType(ctx);
156156 const inst = try parseInstruction(ctx, opt_type);
......@@ -298,7 +298,24 @@ fn parseParameterGeneric(ctx: *ParseContext, comptime T: type) !T {
298298 switch (T) {
299299 Inst.Fn.Body => return parseBody(ctx),
300300 Inst.OtherInst => {
301 return parseError(ctx, "TODO implement parseParameterGeneric for OtherInst", .{});
301 const map = switch (ctx.source[ctx.i]) {
302 '@' => ctx.global_name_map,
303 '%' => return parseError(ctx, "TODO implement OtherInst for %", .{}),
304 else => |byte| return parseError(ctx, "unexpected byte: '{c}'", .{byte}),
305 };
306 ctx.i += 1;
307 const name_start = ctx.i;
308 while (ctx.i < ctx.source.len) : (ctx.i += 1) switch (ctx.source[ctx.i]) {
309 ';', ' ', '\n', ',', ')' => break,
310 else => continue,
311 };
312 const ident = ctx.source[name_start..ctx.i];
313 const kv = map.get(ident) orelse {
314 const bad_name = ctx.source[name_start - 1 .. ctx.i];
315 ctx.i = name_start - 1;
316 return parseError(ctx, "unrecognized identifier: {}", .{bad_name});
317 };
318 return Inst.OtherInst{ .index = kv.value };
302319 },
303320 Value => return parseError(ctx, "TODO implement parseParameterGeneric for type Value", .{}),
304321 else => @compileError("Unimplemented: ir parseParameterGeneric for type " ++ @typeName(T)),
......@@ -319,7 +336,7 @@ fn parseBody(ctx: *ParseContext) !Inst.Fn.Body {
319336 while (ctx.i < ctx.source.len) : (ctx.i += 1) switch (ctx.source[ctx.i]) {
320337 ';' => _ = try skipToAndOver(ctx, '\n'),
321338 '%' => {
322 const at_start = ctx.i;
339 ctx.i += 1;
323340 const ident = try skipToAndOver(ctx, ' ');
324341 const opt_type = try parseOptionalType(ctx);
325342 const inst = try parseInstruction(ctx, opt_type);
......@@ -452,3 +469,6 @@ fn findLineColumn(source: []const u8, byte_offset: usize) struct { line: usize,
452469 }
453470 return .{ .line = line, .column = column };
454471}
472
473// Performance optimization ideas:
474// * make the source code sentinel-terminated, so that all the checks against the length can be skipped