My first compiler

6spill3

Source Code

// MyCC code example
// Gabriel Capella
class Test {
    int f;
    int h;
    public void main() {
        int x1;
        int x2;
        int x3;
        int x4;
        x1 = 49;
        x2 = x1 + 1;
        x3 = x2 + 1;
        x4 = x3 + 1;
        print(x4);
        print(x3);
        print(x2);
        print(x1);
    }
}

preview

preview

preview

preview

preview

preview

preview

preview

Results

Assembly

.globl main

main:
   pushq  %rbp                # Save old EBP
   movq    %rsp, %rbp         # Save old ESP

   movq    $16, %rax
   movq    %rax, %rdi
   callq   malloc@PLT
   testq   %rax, %rax
   jz      fail_exit
   pushq   %rax

   callq   __LABEL0
   popq   %rax

   movq %rbp, %rsp            # Restore ESP
   popq %rbp                  # Restore EBP
   retq

__LABEL0:
   pushq  %rbp                # Save old EBP
   movq    %rsp, %rbp         # Save old ESP
   addq    $-8, %rsp

   ##MovL %0 <- 49
   movq   $49, %rax
   ##MoMo %1 <- %0
   movq   %rax, %rcx
   ##MoMo %2 <- %1
   movq   %rcx, %rbx
   ##MovL %3 <- 1
   movq   $1, %rax
   ##MoOp %4 <- %2,%3
   addq   %rbx, %rax
   ##MoMo %5 <- %4
   movq   %rax, %rbx
   ##MoMo %6 <- %5
   movq   %rbx, %rdx
   ##MovL %7 <- 1
   movq   $1, %rax
   ##MoOp %8 <- %6,%7
   addq   %rdx, %rax
   ##MoMo %9 <- %8
   ##MoMo %10 <- %9
   movq   %rax, %r8
   ##MovL %11 <- 1
   movq   $1, %rdx
   ##MoOp %12 <- %10,%11
   addq   %r8, %rdx
   ##MoMo %13 <- %12
   ##MoMo %14 <- %13
   ##Prin %14
   pushq  %r8                   ## prepare print call
   pushq  %rax                   ## prepare print call
   pushq  %rbx                   ## prepare print call
   pushq  %rcx                   ## prepare print call
   pushq  %rdx                   ## prepare print call
   pushq  %rdx                  ## print
   movq   $1,%rax               ## sys_write
   movq   $1,%rdi               ## fd stdout
   movq   %rsp,%rsi             ## string pointer
   movq   $1,%rdx               ## size
   syscall                      ## syscall
   popq   %rsi                  ##
   popq   %rdx
   popq   %rcx
   popq   %rbx
   popq   %rax
   popq   %r8
   ##MoMo %15 <- %9
   ##Prin %15
   pushq  %r8                   ## prepare print call
   pushq  %rax                   ## prepare print call
   pushq  %rbx                   ## prepare print call
   pushq  %rcx                   ## prepare print call
   pushq  %rdx                   ## prepare print call
   pushq  %rax                  ## print
   movq   $1,%rax               ## sys_write
   movq   $1,%rdi               ## fd stdout
   movq   %rsp,%rsi             ## string pointer
   movq   $1,%rdx               ## size
   syscall                      ## syscall
   popq   %rsi                  ##
   popq   %rdx
   popq   %rcx
   popq   %rbx
   popq   %rax
   popq   %r8
   ##MoMo %16 <- %5
   movq   %rbx, %rax
   ##Prin %16
   pushq  %r8                   ## prepare print call
   pushq  %rax                   ## prepare print call
   pushq  %rbx                   ## prepare print call
   pushq  %rcx                   ## prepare print call
   pushq  %rdx                   ## prepare print call
   pushq  %rax                  ## print
   movq   $1,%rax               ## sys_write
   movq   $1,%rdi               ## fd stdout
   movq   %rsp,%rsi             ## string pointer
   movq   $1,%rdx               ## size
   syscall                      ## syscall
   popq   %rsi                  ##
   popq   %rdx
   popq   %rcx
   popq   %rbx
   popq   %rax
   popq   %r8
   ##MoMo %17 <- %1
   movq   %rcx, %rax
   ##Prin %17
   pushq  %r8                   ## prepare print call
   pushq  %rax                   ## prepare print call
   pushq  %rbx                   ## prepare print call
   pushq  %rcx                   ## prepare print call
   pushq  %rdx                   ## prepare print call
   pushq  %rax                  ## print
   movq   $1,%rax               ## sys_write
   movq   $1,%rdi               ## fd stdout
   movq   %rsp,%rsi             ## string pointer
   movq   $1,%rdx               ## size
   syscall                      ## syscall
   popq   %rsi                  ##
   popq   %rdx
   popq   %rcx
   popq   %rbx
   popq   %rax
   popq   %r8

   movq    %rbp, %rsp            # Restore ESP
   popq    %rbp                  # Restore EBP
   retq

fail_exit:
   movl   $1, %eax
   popq   %rbp
   retq
Expected
4321
Got
4321