Master the fundamental concepts of intermediate representation through this focused micro-challenge.
Three-address code (TAC) is the classic intermediate representation between AST and machine code. Each instruction has at most one operator and three operands: t1 = a + b. GCC lowers to GIMPLE; many textbooks use quadruples or triples. TAC flattens tree-shaped expressions into linear instruction sequences.
Binary ops: t = a op b. Copies: t = a. Jumps: goto L, if t goto L. Labels mark join points. Function calls use param and call instructions. Temporaries hold intermediate results.
cLoading…
Production compilers embed this step inside a longer pipeline. GCC flows through cpp, cc1, assembly, and ld; Clang uses the driver, Sema, LLVM IR passes, and a target backend. LLVM bitcode, JVM bytecode, and WASM are other familiar IRs at the same layer. The exercise isolates one pass so you can test it alone before chaining it to the next stage.
You will design a simple three-address code IR with instruction and operand types. This exercise asks you to define the data structures representing TAC before you write the generator that emits them from the AST.
Design an in-memory representation for three-address code (TAC), the IR the rest of this subtrack builds on, then prove it works by printing it in canonical form and executing it.
One instruction per line; tokens are separated by spaces; blank lines are ignored.
| Form | Meaning |
|---|---|
x = a | copy |
x = a OP b | binary operation, OP is one of + - * / % < == |
NAME: | label |
goto NAME | unconditional jump |
if a goto NAME | jump if a is non-zero |
ifFalse a goto NAME | jump if a is zero |
print a | output a value |
# text | comment (kept in the listing, does nothing) |
Operands a, b are integer literals (possibly negative, e.g. -3) or variable names. Every variable starts at 0. Arithmetic is on 64-bit signed integers; / and % truncate toward zero; < and == produce 1 or 0.
Canonical form is exactly the table's spelling with single spaces (a comment prints ascLoading…
# text).--- run ---.print outputs its value on its own line. Execution ends after the last instruction.steps=N: the number of instructions executed (labels and comments count).Runtime errors stop execution and print error: division by zero at K or, if 10000 steps are exceeded, error: step limit exceeded, instead of steps=N. A jump to a label that doesn't exist prints error: unknown label NAME at K.
Input:
cLoading…
Output:
cLoading…
Instr struct (opcode enum, result, two operands, label target) and a growable instruction list with add, print and free functions.Hidden tests cover nested loops driven by ifFalse, and a division by zero after some output has already been printed.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it works