Hi all! looking for some advice on things to read or patterns to use related to an optimizing compiler. For some quick background I like several other people I’ve seen on this subreddit read through crafting interpreters book and caught the PL bug. I’ve continued to work on my language to what I would now call a close relative to Lox, [Laythe](https://github.com/Laythe-lang/Laythe).
So what my long term goal is to get a typescript like experience. Essentially I’d like for a program to be either run immediately without any type checking and very simple codegen to my vm’s bytecode or run the typechecker and maybe do some relatively simple optimization passes.
So far I’ve replaced my single pass compiler with an actual parsing step where the program transformed into an ast like format and then codegen is performed on that ast. Currently, the end result is much the same as before but with a clearer separation of parsing and codegen.
So just to elaborate a few of the types of optimizations I’d like to look into. As an example currently if I’m I’m going to access a property or a method, I’m currently doing a hash lookup. I’d emit an instruction like `GetProperty(u16)` which has a unsigned 16 bit argument. What currently happens is something like this
1. Lookup string constant in constant table at offset u16
2. Using said string do a hash lookup on and instance object to get the value of the property
Ideally, I think I should be able to instead have the compiler figure out the offset in the instance to avoid the hash lookup. I’m not sure what sort of passes or data structures would help me track that that some identifier `Foo` is a `Bar` and property `Baz` is therefore at offset 3.
Another relatively simple optimization that I could see would just be on my current bytecode itself. As an example have a `Drop` instruction and a `DropN(u8)` instruction for drop values off the stack. I’m not sure what would be the best approach to condensing something like
Would it make sense to have another intermediate presentation between the ast and the file vm bytecode. How are these sorts of transformations made?
Anyways I would appreciate any advice!