cpsc411 / cpsc411-book

The WIP book for CPSC 411
Other
59 stars 7 forks source link

Zero displacements for frame variables considered a bug #122

Open skeuchel opened 6 months ago

skeuchel commented 6 months ago

The book uses the frame variable abstraction with nonnegative indices, i.e. fv0, fv1, ... Using the reference implementation, the first frame variable fv0 is compiled to a zero displacement

(parameterize ([current-assignable-registers '()])
  (compile '(module (let ([x 0]) x))))
start:
  mov rbp, rsp
  mov r15, done
  mov QWORD [rbp - 0], 0
  mov rax, QWORD [rbp - 0]

In the System V AMD64 ABI, local variables are accessed through negative displacements of the base pointer only, and normally only negative displacements from the initial stack pointer should be used. In fact, the above assembly code will overwrite the argument count (argc) passed by the operating system. See Figure 3.9: Initial Process Stack in the ABI specification. Am I missing something or is this sort of an off-by-one bug?

wilbowma commented 6 months ago

Thanks Steven!

I'll try to look into this over the summer (busy with deadlines right now), but I think you're reading the spec right. I ignore the spec a little in places, but this one might be worth adhering to because I want to add input.