IUCompilerCourse / python-student-support-code

Support for for students (Python)
MIT License
57 stars 38 forks source link

BugFix: interp_x86 'neg_a' case missing #3

Closed psolve closed 2 years ago

psolve commented 2 years ago

Interpreter was failing with negative immediate values.

Test Program Lvar: x = (30 - 2) y = (x - 5) print((x - y))

Test Program x86 Ast after select instructions: main: movq $28, %rax movq %rax, x movq $-5, %rax ---- interpreter was failing at this point - arg was Tree('neg_a', [Tree('inta', [5])]) addq x, %rax movq %rax, y movq y, %rax negq %rax
movq %rax, tmp
.0 movq x, %rax addq tmp.0, %rax movq %rax, tmp.1 movq tmp_.1, %rdi callq print_int

psolve commented 2 years ago

@jsiek @Fyrbll Please have a look.

Fyrbll commented 2 years ago

Thanks for making this pull request.

I could reproduce this bug by running

custom_program = \
    X86Program(
        [Instr('movq', [Immediate(-5), Reg('rdi')]),
         Instr('negq', [Reg('rdi')]), 
         Callq('_print_int', 1)])

interp_x86.eval_x86.interp_x86(custom_program)

I can also confirm that the code you added fixes the bug.