Converting stack pushing loop to three address code basic block

233 views Asked by At

I've been working on a small compiler (Using llvm, but shoulden't matter) for a super simple stack based language.

I've been reading a lot of papers about translating an evaluation stack based language to TAC or SSA, and the basic procedure outlined generally is to divide the code in basic blocks, then analyze the height of the stack at every instruction and with that allocate registers for each position in the stack using the heights as name. Then, analyze the net pushes and pops of the basic blocks, and forward the registries between them as needed.

However, with this information there is a simple case I cannot understand. For example the following pseudo-code:

1: ...
2: READ
3: IF 0 GO TO 4 ELSE GOTO 2
4: ...

Assuming that READ is a function that get values from somewhere and pushes into the stack, this function will create a simple basic block with the READ instruction, maybe a subtraction and a branch at the end (Assuming whatever the if does not pop the value from the stack).

The problem is, if I just analyze stack usage there are no pops, just one push, so there is only one value that I need to pass along. However, as the loop goes, because there is only one registry for storage, all the reads will be lost.

I haven't found any text about this case. Is one supposed to keep a real stack along with the converted program? If this is the case, finding out when values MUST be pushed or popped out of the real stack doesn't seem trivial, as it depends on the relations between all the basic blocks.

Is there any article or paper to read about this? Any help is appreciated, thanks.

0

There are 0 answers