News for hackers. Stuff that matters.
Re:Mugo, a toy compiler for a subset of Go that can compile ... by Someone on Monday April 12, 2021 @11:59AM (#26780928)
You ‘have’ to go back to the start of your function to fill in the correct size of the stack frame (An alternative is to make a stack frame for every variable or basic block. That’s a waste of cycles and code, especially on early machines)

Again, the solution to that shouldn’t stop you, as it is similar to how you handle filling in the branch offsets in forward branches in if/then/else, and that’s something you have to do, anyways (yes, you can stream out your object code without going back and filling in branch offsets but that leads to slow, ugly code)

I think the reason this _was_ done in old languages is that parsing and compiling were black arts when those languages were created. COBOL and FORTRAN didn’t use stack frames, Algol invented per-function stack frames and later loosened that to per-block ones. That’s what K&R C did, too.

I don’t know which language invented the idea that you could lossen that further, but it might have been C++.

Re:Mugo, a toy compiler for a subset of Go that can compile ... by a1369209993 on Monday April 12, 2021 @07:04PM (#26786259)
> You 'have' to go back to the start of your function to fill in the correct size of the stack frame

  foo:
    push ebp
    mov ebp esp
    sub esp .stacksz
    ; ...
    .stacksz = 0x14
    leave
    ret
> similar to how you handle filling in the branch offsets in forward branches in if/then/else, and that’s something you have to do, anyways

Pretty much this. I'm with Joker_vD on this one; I don't see a problem here.

Re:Mugo, a toy compiler for a subset of Go that can compile ... by benhoyt on Tuesday April 13, 2021 @06:06PM (#26799954)
Thanks, that's a great idea -- not sure why I didn't think of that, as I am using the assembler to figure this out for if/else forward branches already (as the parent comment pointed out). I've added a paragraph to the article mentioning this comment. Thanks!

Another approach I thought of is, instead of printing the output right away, append to a list of strings you'll output later, and patch it up yourself. But if we're using an assembler, we might as well get it to work for us. (The patching technique is commonly used for patching binary machine code.)

Re:Mugo, a toy compiler for a subset of Go that can compile ... by Someone on Tuesday April 13, 2021 @02:21AM (#26788713)
Neither do I. That’s why I wrote “the solution to that shouldn’t stop you, as it is similar to how you handle filling in the branch offsets in forward branches in if/then/else”.

The above doesn’t solve the problem, though. It moves it to the assembler.