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 {...@@ -150,7 +150,7 @@ pub fn parseRoot(ctx: *ParseContext) !void {
150 while (ctx.i < ctx.source.len) : (ctx.i += 1) switch (ctx.source[ctx.i]) {150 while (ctx.i < ctx.source.len) : (ctx.i += 1) switch (ctx.source[ctx.i]) {
151 ';' => _ = try skipToAndOver(ctx, '\n'),151 ';' => _ = try skipToAndOver(ctx, '\n'),
152 '@' => {152 '@' => {
153 const at_start = ctx.i;153 ctx.i += 1;
154 const ident = try skipToAndOver(ctx, ' ');154 const ident = try skipToAndOver(ctx, ' ');
155 const opt_type = try parseOptionalType(ctx);155 const opt_type = try parseOptionalType(ctx);
156 const inst = try parseInstruction(ctx, opt_type);156 const inst = try parseInstruction(ctx, opt_type);
...@@ -298,7 +298,24 @@ fn parseParameterGeneric(ctx: *ParseContext, comptime T: type) !T {...@@ -298,7 +298,24 @@ fn parseParameterGeneric(ctx: *ParseContext, comptime T: type) !T {
298 switch (T) {298 switch (T) {
299 Inst.Fn.Body => return parseBody(ctx),299 Inst.Fn.Body => return parseBody(ctx),
300 Inst.OtherInst => {300 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 };
302 },319 },
303 Value => return parseError(ctx, "TODO implement parseParameterGeneric for type Value", .{}),320 Value => return parseError(ctx, "TODO implement parseParameterGeneric for type Value", .{}),
304 else => @compileError("Unimplemented: ir parseParameterGeneric for type " ++ @typeName(T)),321 else => @compileError("Unimplemented: ir parseParameterGeneric for type " ++ @typeName(T)),
...@@ -319,7 +336,7 @@ fn parseBody(ctx: *ParseContext) !Inst.Fn.Body {...@@ -319,7 +336,7 @@ fn parseBody(ctx: *ParseContext) !Inst.Fn.Body {
319 while (ctx.i < ctx.source.len) : (ctx.i += 1) switch (ctx.source[ctx.i]) {336 while (ctx.i < ctx.source.len) : (ctx.i += 1) switch (ctx.source[ctx.i]) {
320 ';' => _ = try skipToAndOver(ctx, '\n'),337 ';' => _ = try skipToAndOver(ctx, '\n'),
321 '%' => {338 '%' => {
322 const at_start = ctx.i;339 ctx.i += 1;
323 const ident = try skipToAndOver(ctx, ' ');340 const ident = try skipToAndOver(ctx, ' ');
324 const opt_type = try parseOptionalType(ctx);341 const opt_type = try parseOptionalType(ctx);
325 const inst = try parseInstruction(ctx, opt_type);342 const inst = try parseInstruction(ctx, opt_type);
...@@ -452,3 +469,6 @@ fn findLineColumn(source: []const u8, byte_offset: usize) struct { line: usize,...@@ -452,3 +469,6 @@ fn findLineColumn(source: []const u8, byte_offset: usize) struct { line: usize,
452 }469 }
453 return .{ .line = line, .column = column };470 return .{ .line = line, .column = column };
454}471}
472
473// Performance optimization ideas:
474// * make the source code sentinel-terminated, so that all the checks against the length can be skipped