Live data from Hacker News

One-pass Compiler

keleshev.com

21–30 of 44 posts

Re: One-pass Compiler

#21
post #7

I'm actually working on a Pascal compiler right now. Register management has been a really tricky part for me, but I didn't even thing of treating an x86 processor as a stack based machine. Very clever, especially when you just want a simple compiler an not anything super optimized.

A simple optimization is to make a stack machine with the top of stack cached in a register. This can improve speed about 10% in general operation.

Re: One-pass Compiler

#23

I'm currently working on a 3-part blog series on writing a one-pass compiler in Python that emits C. It is quite a bit longer though because we make the lexer and parser from scratch. I started it because my students were having trouble with other tutorials because of all the jargon, theory, and libraries required. http://web.eecs.utk.edu/~azh/blog/teenytinycompiler1.html

That's very well done, thanks for sharing. I really like the straight forward writing style, I've never attempted to write a compiler, and have only done basic parsers for day to day tasks, so like the clean, simple approach.

Re: One-pass Compiler

#24
post #6

The author states: >"An optimizing compiler would constant-fold our expression into a single number ahead of time." Could someone say what is meant by constant-folding here?

As a concrete example I wrote a simple interpreter a while back, in golang. Operations would be compiled to a simple bytecode which would be executed at runtime. So: 3 + 4 Would get "compiled" to: PUSH_INT 3 PUSH_INT 4 OP_PLUS As you can imagine this was a virtual stack-machine. After a while I started folding these operations: * If there was a "push int" * And another "push int" * And a maths operation. * Then repla…

For anyone wondering, the NOPs are so that jumps don't get messed up. Assembly level GOTOs generally work on byte/word offsets, so if something intended to jump back before the calculation but was expecting two PUSH_INTs and an OP_PLUS (5 words: 3 opcodes, 2 arguments) but there was only a single PUSH_INT (2 words), it would jump back 3 words too far.

Re: One-pass Compiler

#26

I'm currently working on a 3-part blog series on writing a one-pass compiler in Python that emits C. It is quite a bit longer though because we make the lexer and parser from scratch. I started it because my students were having trouble with other tutorials because of all the jargon, theory, and libraries required. http://web.eecs.utk.edu/~azh/blog/teenytinycompiler1.html

I've done the same thing for the same reason but in JavaScript and compiling a lisp dialect. https://notes.eatonphil.com/compiler-basics-lisp-to-assembly...

Added to my list, keep 'em coming! :)

https://gist.github.com/cellularmitosis/1f55f9679f064bcff029...

Re: One-pass Compiler

#28
post #24

Earlier quoted context omitted.

As a concrete example I wrote a simple interpreter a while back, in golang. Operations would be compiled to a simple bytecode which would be executed at runtime. So: 3 + 4 Would get "compiled" to: PUSH_INT 3 PUSH_INT 4 OP_PLUS As you can imagine this was a virtual stack-machine. After a while I started folding these operations: * If there was a "push int" * And another "push int" * And a maths operation. * Then repla…

For anyone wondering, the NOPs are so that jumps don't get messed up. Assembly level GOTOs generally work on byte/word offsets, so if something intended to jump back before the calculation but was expecting two PUSH_INTs and an OP_PLUS (5 words: 3 opcodes, 2 arguments) but there was only a single PUSH_INT (2 words), it would jump back 3 words too far.

Good clarification, and exactly right!

Later I do walk back over the bytecode and remove the nops. But I have to update all JMP/CALL instructions to cope with the changed destination offsets. Not a hard job in my case, as there are only a couple of instructions which refer to byte-offsets.

(If I allowed "LD A,[BC]", or similar permuations I'd have a lot more work to do.)

Re: One-pass Compiler

#29

Tangential - in your work in progress compiler book https://keleshev.com/compiling-to-assembly-from-scratch-the-... are you going to write yourself the lexer and parser or use Flex and Bison like in the article ?

In the book, lexing and parsing is done from scratch. I think tools like Flex and Bison are very handy, but for the book, my focus is on learning value.

Re: One-pass Compiler

#30
post #23

I'm currently working on a 3-part blog series on writing a one-pass compiler in Python that emits C. It is quite a bit longer though because we make the lexer and parser from scratch. I started it because my students were having trouble with other tutorials because of all the jargon, theory, and libraries required. http://web.eecs.utk.edu/~azh/blog/teenytinycompiler1.html

That's very well done, thanks for sharing. I really like the straight forward writing style, I've never attempted to write a compiler, and have only done basic parsers for day to day tasks, so like the clean, simple approach.

Very glad to hear it! Part 2 will be out in a few days and part 3 the next week.
Post reply on HN