IUCompilerCourse / public-student-support-code

Public helper code for p423/p523 students (Racket)
MIT License
155 stars 74 forks source link

interp-x86-3 errors when running code compiled from Lfun #24

Closed nicball closed 1 year ago

nicball commented 1 year ago

When compiling for tail calls, I emit code that modifies %rsp in functions' main body, which errors the interpretor:

R3/interp-x86-instr @ interp.rkt:916 (car ast): addq $0, %rsp

R4/interp-x86-exp @ interp.rkt:1272 ast: $0

interp-x86-exp @ interp.rkt:874 ast: $0

R2/interp-x86-exp @ interp.rkt:472 ast: $0

interp-x86-exp @ interp.rkt:261 ast: $0

R1/interp-x86-exp @ interp.rkt:271 (observe-value result): 0

R2/interp-x86-exp @ interp.rkt:501 (observe-value result): 0

R3/interp-x86-exp @ interp.rkt:883 (observe-value result): 0

R4/interp-x86-exp @ interp.rkt:1281 (observe-value result): 0

R4/interp-x86-exp @ interp.rkt:1272 ast: %rsp

interp-x86-exp @ interp.rkt:874 ast: %rsp

R2/interp-x86-exp @ interp.rkt:472 ast: %rsp

interp-x86-exp @ interp.rkt:261 ast: %rsp

encountered exception while testing 'fun`, case functions_test_1 lookup: didn't find rsp in (r15 rax rdi rcx r15) context...: /home/nicball/code/eoc/interp.rkt:260:6 /home/nicball/code/eoc/interp.rkt:471:6 /home/nicball/code/eoc/interp.rkt:873:6 /home/nicball/code/eoc/interp.rkt:1271:6 /nix/store/y4an9j6cm3gb2bzjrljb0vxrxrb188fa-racket-8.8/share/racket/collects/racket/match/compiler.rkt:559:40: f1310 /home/nicball/code/eoc/interp.rkt:1238:4: call-function method in interp-R4-class /home/nicball/code/eoc/interp.rkt:1362:6 /home/nicball/code/eoc/utilities.rkt:1975:2 /nix/store/y4an9j6cm3gb2bzjrljb0vxrxrb188fa-racket-8.8/share/racket/pkgs/rackunit-lib/rackunit/private/test-suite.rkt:86:13: the-tests /nix/store/y4an9j6cm3gb2bzjrljb0vxrxrb188fa-racket-8.8/share/racket/pkgs/rackunit-lib/rackunit/private/test-suite.rkt:61:0: apply-test-suite /nix/store/y4an9j6cm3gb2bzjrljb0vxrxrb188fa-racket-8.8/share/racket/pkgs/rackunit-lib/rackunit/private/test-suite.rkt:139:7 /nix/store/y4an9j6cm3gb2bzjrljb0vxrxrb188fa-racket-8.8/share/racket/pkgs/rackunit-lib/rackunit/private/test-suite.rkt:136:2 /nix/store/y4an9j6cm3gb2bzjrljb0vxrxrb188fa-racket-8.8/share/racket/pkgs/rackunit-lib/rackunit/private/test-suite.rkt:61:0: apply-test-suite /nix/store/y4an9j6cm3gb2bzjrljb0vxrxrb188fa-racket-8.8/share/racket/pkgs/rackunit-lib/rackunit/text-ui.rkt:91:0: run-tests /nix/store/y4an9j6cm3gb2bzjrljb0vxrxrb188fa-racket-8.8/share/racket/collects/racket/contract/private/arrow-val-first.rkt:489:18 body of "/home/nicball/code/eoc/run-tests.rkt"

Relevant part in my main function looks like this:

'mainstart

live-afters: (list (set fun-ref.44016) (set %rdi fun-ref.44016) (set))

leaq id(%rip), %rcx movq $42, %rdi movq %rcx, %rax subq $0, %r15 addq $0, %rsp popq %rbp jmp *%rax

It seems that the x86 interpretors treat preludes and conclusions specially without actually simulating %rsp and %rbp.

Am I implementing tail calls wrongly. Or this accompanying code is not sync with the book?

I am trying to understand interp.rkt and maybe come up with my PR. Your input on this issue would be much appreciated. Thanks in advance!

nicball commented 1 year ago

This issue duplicates https://github.com/IUCompilerCourse/public-student-support-code/issues/16.