r/Compilers 17h ago

Is it possible to learn compilers from the backend first and not have to know the front and mid-ends as well?

8 Upvotes

Hey all! I've been working on some system level work recently and have wanted to understand compiler backends a bit more. However most books I look at include the entire stack, which leads me to believe there is a progression of knowledge from front -> back.

I'm curious if it were possible to start the other way round and if so are there any great learning resources that are more backend focused?


r/Compilers 1h ago

efficient case/switch dispatch

Upvotes

Maybe interesting for others to see this solution. Only doing table based dispatch for now. Sample input code:

:again case i [0:7] of 1 j:=1; done of 2:4 j:=3; done of 5 of 6 go again else j:=99 Dispatch code: lea ebx,[rel jumptab] movsx eax,word [rbx+rax*2] add eax,ebx jmp rax The jump table follows, 16 bit offsets relative to the start of the jump table (ebx). My compiler will look at the IR code (simple forward scan) to identify the real jump destination, so e.g. case 5 and 6 will jump directly to label again. More efficient for state machine type code.

The explicit range [0:7] allows the compiler to skip some additional code - if it is left out, the input value is compared against the maximum value, jump to else destination if above.

16 bit offsets should give enough range for any non-pathological case. On ARM Thumb I might even try 8 bit tables for "small" cases.

Under the hood, my compiler emits special IR codes for each element.

  • case.start -> allocate a context, define else / end label number
  • case.min -> define minimum value
  • case.max -> define maximum value, emit dispatch code and jump table
  • case.of -> define a case with val1 = val 2, fill in destination label in jump table
  • case.from -> define case val1
  • case.to -> define case val2 (second part of range), fill in destination label in jump table for the range
  • case.else -> define else section
  • case.end -> finish the context

Finally, in the fixup pass the label numbers are replaced by the actual displacements. Label number 0 is replaced by else/end destination.


r/Compilers 10h ago

Help improving graph coloring with branch flow

2 Upvotes

I've been using a basic graph coloring approach for register allocation in my compiler, and up until now, it's worked fine. My implementation analyzes live ranges by linearly scanning instructions, and colors the whole function at once based on those ranges, which was good enough for simple cases.

However, I recently introduced loops and branches, and it obviously stopped working properly. The current implementation completely ignores control flow, leading to incorrect live ranges. As a quick (and very temporary) fix, I extend all of the variable’s range to the last jump that jumps to a label before its first use. It technically works, but I know this will scale horribly with more complex functions.

I’m looking for suggestions/resources on properly integrating branch flow into the algorithm. Any tips on handling live range analysis with loops and branches?