Self-hosting the Epoch Compiler: Day Three

Published December 13, 2013
Advertisement
Sat down and figured out some of the annoying bugs that were left in the compiler, mostly surrounding higher order functions and templates. Rigged up a half-dozen more intrinsics and such, and fired off the compiler for yet another pass on itself.

At 8:02PM PST the compiler successfully completed its first semantic analysis on itself, and began attempting code generation. However, there are some bugs in code generation that cause assertion failures for the moment. That will have to be dealt with.

I pinned down the assertion failure to some kind of expression type inference bug in templated higher-order functions. Since I only use that idiom once in the compiler, I decided to punt. I tweaked the code to not use a templated function, and made a note to come back and fix the bug some other day.

The next hiccup was a mistake in the way parenthetical expressions were handled by the code generator; easy enough to fix.

At 8:22PM PST the compiler successfully emitted an executable binary of... something. The binary itself appears mangled and disappointingly does not actually run. But still, the compiler *did* do a complete pass on itself and emit something vaguely resembling a runnable .EXE, so that's pretty cool.

Turns out I cut some corners in the .exe generation code, and it doesn't accurately represent the size of code emitted onto disk. Since the compiler is an order of magnitude (or two!) larger than any other program that it has generated to date, the numbers are wonky and cause only a subset of the binary to be loaded by Windows, which then (correctly) complains that the file is borked.

This is fairly straight-forward to fix, so I went ahead and hacked in some stuff to try and emit a more sane binary.

At 8:33PM PST the compiler emitted its second attempt at a runnable binary. The .EXE runs and starts up, but then immediately crashes because the JIT layer finds some quirks in the emitted bytecode. The first quirk that shows up is that "nothing" typed parameters are being added to lexical scopes as if they had variables associated with them - which of course is patently silly since they have the type of "nothing." Another easy-enough fix.

8:37PM PST - attempt number three. This still crashes in the JITter, but at least it crashes later on.

It quickly becomes apparent that I'm going to need to come up with a way to view the bytecode generated by the compiler between when it is emitted and when the JIT gets ahold of it.

Once I have that sorted out, the problem looks pretty simple: global variables are stomping on each other and the JIT is getting confused. This is a code-gen bug so I rig up a test case and start trying to pin down a fix.

After fixing globals, I found another crash bug, this time to do with parenthetical expressions in code generation. Ironically, it turns out to be on my "to do" list of bugs to fix already, so now's as good a time as any!

9:54PM PST - I've lost track of what attempt this is, but we're getting closer - slowly but surely. Next bug on the list relates to "nothing" parameters again - this time because they're throwing off stack slot indexing in the bytecode layer. Not hard to fix, but annoying. Either way, though, we're plodding ever closer to the goal...

10:35PM PST - That's about enough for one night. We're still generating several bytecode miscompiles, particularly around anonymous temporary structures. Sadly, my brain is too mushy to figure out the exact problem at this point, so it'll have to wait until Day Four!
6 likes 1 comments

Comments

Liguria

Man, these 'captain's log book' style of entries are among the most interesting I have read so far.

Even though it seems you're not getting a whole lot of feedback/comments on the Day One/Two/Three/Etc... entries, please DO keep posting them.

Very, very entertaining!!

Oh, and good luck fixing and perfecting your compiler-compiler of course :)

December 13, 2013 09:23 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement