Domain: Programming Languages, Compilers & Formal Methods

You are given a linear scan register allocation problem with 3 physical registers: R0, R1, R2.

Live intervals for virtual registers:

V0: [0,7]
V1: [1,5]
V2: [2,9]
V3: [3,6]
V4: [4,11]
V5: [6,10]
V6: [8,14]
V7: [10,13]
V8: [12,15]
V9: [1,3]

Use the following deterministic rules:

1. Process intervals in ascending order of start point.
2. If two intervals have the same start point, process the one with the smaller end point first.
3. Before processing an interval with start point `s`, expire every active interval whose end point is less than or equal to `s`.
4. If one or more registers are free, assign the lowest-numbered free register.
5. If all registers are occupied:
   - Consider the active intervals plus the new interval.
   - Spill the interval with the largest end point.
   - If multiple intervals tie for the largest end point, spill the new interval.
   - If the new interval is spilled, it receives no register.
   - Otherwise, remove the spilled active interval and give its register to the new interval.
6. Record spill events in the exact order they occur.
7. For part C, evaluate the active-set size only at the distinct start indices that appear in the input: `0, 1, 2, 3, 4, 6, 8, 10, 12`.
   - At each such index, first finish processing all intervals that start there.
   - Then measure the number of active intervals.

Tasks:

A. List the intervals in the exact processing order.

B. List the spill events in order.

C. List all start indices from `0, 1, 2, 3, 4, 6, 8, 10, 12` where the number of active intervals is exactly 3 after all intervals starting at that index have been processed.

D. Provide the final register assignment for every interval that is not spilled.

Use this exact output format:

Processing Order: comma-separated interval names
Spill Events: comma-separated interval names, or `None` if there are no spills
Active Count 3 At: comma-separated start indices
Final Register Assignments:
one `interval -> register` entry per line, ordered by interval name


Grading rubric:
criterion: Lists the processing order exactly as V0, V9, V1, V2, V3, V4, V5, V6, V7, V8.
weight: 15
criterion: States that the total number of spill events is exactly 2.
weight: 10
criterion: Identifies the first spill event as V2.
weight: 10
criterion: Identifies the second spill event as V4.
weight: 10
criterion: Lists the part C indices exactly as 1, 2, 3, 4, 12 with no missing or extra indices.
weight: 20
criterion: Gives the R0 final assignments exactly as V0 -> R0 and V6 -> R0.
weight: 10
criterion: Gives the R1 final assignments exactly as V3 -> R1, V5 -> R1, V7 -> R1, and V9 -> R1.
weight: 15
criterion: Gives the R2 final assignments exactly as V1 -> R2 and V8 -> R2.
weight: 10

Answer:
Processing Order: V0, V9, V1, V2, V3, V4, V5, V6, V7, V8
Spill Events: V2, V4
Active Count 3 At: 1, 2, 3, 4, 12
Final Register Assignments:
V0 -> R0
V1 -> R2
V3 -> R1
V5 -> R1
V6 -> R0
V7 -> R1
V8 -> R2
V9 -> R1

Explanation

Step 1: Determine processing order

Intervals are sorted by increasing start point, and ties are broken by smaller end point first.

- Start 0: V0
- Start 1: V9 before V1 because 3 < 5
- Start 2: V2
- Start 3: V3
- Start 4: V4
- Start 6: V5
- Start 8: V6
- Start 10: V7
- Start 12: V8

So the exact processing order is:
V0, V9, V1, V2, V3, V4, V5, V6, V7, V8

Step 2: Simulate the allocator

Start with no active intervals and all registers free.

1. Process V0 = [0,7]
- Expire intervals with end <= 0: none
- Lowest free register is R0
- Assign V0 -> R0
- Active set: V0(R0)

2. Process V9 = [1,3]
- Expire intervals with end <= 1: none
- Lowest free register is R1
- Assign V9 -> R1
- Active set: V0(R0), V9(R1)

3. Process V1 = [1,5]
- Expire intervals with end <= 1: none
- Lowest free register is R2
- Assign V1 -> R2
- Active set: V0(R0), V9(R1), V1(R2)

4. Process V2 = [2,9]
- Expire intervals with end <= 2: none
- No free register exists
- Compare end points of V0(7), V9(3), V1(5), V2(9)
- Largest end point is 9, so spill V2
- V2 gets no register
- Active set stays: V0(R0), V9(R1), V1(R2)

5. Process V3 = [3,6]
- Expire intervals with end <= 3: V9 expires, so R1 becomes free
- Lowest free register is R1
- Assign V3 -> R1
- Active set: V0(R0), V1(R2), V3(R1)

6. Process V4 = [4,11]
- Expire intervals with end <= 4: none
- No free register exists
- Compare end points of V0(7), V1(5), V3(6), V4(11)
- Largest end point is 11, so spill V4
- V4 gets no register
- Active set stays: V0(R0), V1(R2), V3(R1)

7. Process V5 = [6,10]
- Expire intervals with end <= 6: V1 expires and V3 expires
- R2 and R1 are now free
- Lowest free register is R1
- Assign V5 -> R1
- Active set: V0(R0), V5(R1)

8. Process V6 = [8,14]
- Expire intervals with end <= 8: V0 expires
- R0 becomes free
- Lowest free register is R0
- Assign V6 -> R0
- Active set: V5(R1), V6(R0)

9. Process V7 = [10,13]
- Expire intervals with end <= 10: V5 expires
- R1 becomes free
- Lowest free register is R1
- Assign V7 -> R1
- Active set: V6(R0), V7(R1)

10. Process V8 = [12,15]
- Expire intervals with end <= 12: none
- Lowest free register is R2
- Assign V8 -> R2
- Active set: V6(R0), V7(R1), V8(R2)

From the full trace, the spill events occur in this exact order:
V2, V4

Step 3: Compute part C exactly

Part C asks for the distinct start indices from the prompt, after all intervals beginning at that index have been processed.

- At start 0: active set is {V0} -> size 1
- At start 1: after processing both V9 and V1, active set is {V0, V9, V1} -> size 3
- At start 2: after processing V2 and spilling it, active set is still {V0, V9, V1} -> size 3
- At start 3: after expiring V9 and assigning V3, active set is {V0, V1, V3} -> size 3
- At start 4: after processing V4 and spilling it, active set is still {V0, V1, V3} -> size 3
- At start 6: active set is {V0, V5} -> size 2
- At start 8: active set is {V5, V6} -> size 2
- At start 10: active set is {V6, V7} -> size 2
- At start 12: active set is {V6, V7, V8} -> size 3

Therefore the exact indices for part C are:
1, 2, 3, 4, 12

Step 4: Final register assignments

Only non-spilled intervals appear in the final assignment list. Reading them from the simulation:

- V0 -> R0
- V1 -> R2
- V3 -> R1
- V5 -> R1
- V6 -> R0
- V7 -> R1
- V8 -> R2
- V9 -> R1

Spilled intervals:
V2, V4