Skip to content

Instantly share code, notes, and snippets.

@matu3ba
Last active Aug 1, 2022
Embed
What would you like to do?
Examples for discussion on compiler meeting on 2022-08-04
Idea
1. parsing the AST to have a simplification for faster frequent access
* create another tree storing what things are active
* create another tree to store what things reference what other things for deletion strategy
(idea: no unused variable errors)
2. starting with remove stuff from end of execution to beginning and fixup unused var errors,
later graph based deletion based on where things are used (!global side effects may not be captured!)
3. rendering the used parts
* fixups will be likely dirty hacks at first
Options for zig-reduce to emit reduced programs are then:
- 1. hack up AstGen and using the slow path for getSourceLocation for the to be removd AST nodes (and tokens) and prune them from the rendered program
- 2. "emitSAST.zig" and "reduceSAST.zig" to convert from and to AST and the corresponding routines to fix spaces of render.zig (each removal must result in a valid program)
- 3. make render.zig iterative
- 4. provide render.zig a comptime-chosen structure to check against what to render and what not
- 5. rewrite parser to be iterative for having simple source<->token<->ast forward and backward traversal interface.
Further options (ie how source location lookup works)
- Module.zig has astGenFile, which calls AstGen.zig generate
to return Zir
- astGenFile is invoked in workerAstGenFile as part of
performAllTheWork in Compilation.zig
before the Job queue for Sema etc is filled
- 1. AstGen to use Zir (unused variables etc)
* using GenZir
* advanceSourceCursorToNode, advanceSourceCursor
=> trace with debugger with watchpoints on
source_offset, source_line, source_column
=> checkUsed invoked alot in AstGen
* Is there any way to only run that and get the source nodes?
Hint: called alot together with *GenZir and traverses
up to outer scope. (not global one though)
* On error: token_src: Ast.TokenIndex looked up from LocalVal
as tmp struct of AST
- 2. Sema to use Air
* LazySrcLoc with toSrcLoc based on tagged union
- SrcLoc = filescope, parent_decl_node, LazySrcLoc
- LazySrcLoc relative to parent_decl_node
* Though it would be nice to have Air<->SrcLocc
TODO: Look for the stuff after Air to see understand
source locations are reconstructed.
// Plan: Single file removal of blocks, expresisons and lines.
// Therefore, keep tack of what has to be printed and.
// Start with top level decls, then pick last lines
// and work to start of execution.
// Now, all variables should be used/alive and we need
// to identify unused vars or make them unused. :)
const std = @import("std");
const builtin = @import("builtin");
pub fn main() !void {
{
if (true) {
@panic("failure");
} else {
unreachable;
}
}
}
fn testme() !void {
const stdout = std.io.getStdOut().writer();
try stdout.writeAll("test\n");
}
test "works also" {
try testme();
@panic("failure2");
}
test "works also" {
try testme();
@panic("failure2");
}
// What will not work
// only remove blocks and lines with brackets, not
// 1. rewrite
// 2. not delete single tokens etc
// 3. evaluate comptime things
// 4. fixup removed things
const use_42 = true;
const number = switch (builtin.cpu.arch) {
.arm, .armeb, .thumb, .thumbeb => impl: {
if (use_42) {
break :impl 42;
} else break :impl 12;
},
else => 12,
};
test "does not work" {
std.debug.assert(number == 12);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment