// File: /workspace/github_cpp_makefile_star2_O2/tsoding[P]wcc/src/./wcc_wat_compiler.cpp
// Function: Wat_Compiler::compile_statement(Statement)
S_Expr *Wat_Compiler::compile_statement(Statement statement)
{
    switch (statement.kind) {
    case Statement_Kind::Local_Var_Def:
        reporter.fail(statement.offset, "TODO: Local variable definitions are only allowed at the beginning of the function");
        break;
    case Statement_Kind::If:
        if (statement.iph.then == nullptr && statement.iph.elze == nullptr) {
            return list(atom("drop"_sv), compile_expression(statement.iph.condition));
        } else {
            //  (if (result <type>)
            //    (then <then-branch>)
            //    (else (else-branch)))
            if (statement.type == Type::U0) {
                return list(
                    atom("if"_sv),
                    compile_expression(statement.iph.condition),
                    append(list(atom("then"_sv)), compile_block(statement.iph.then)),
                    append(list(atom("else"_sv)), compile_block(statement.iph.elze)));
            } else {
                return list(
                    atom("if"_sv), list(atom("result"_sv), wat_name_of_type(statement.type)),
                    compile_expression(statement.iph.condition),
                    append(list(atom("then"_sv)), compile_block(statement.iph.then)),
                    append(list(atom("else"_sv)), compile_block(statement.iph.elze)));
            }
        }
    case Statement_Kind::While:
        // (block
        //  (loop
        //   (br_if 1 (i32.eqz <condition>))
        //   <body>
        //   (br 0)))
        return list(
            atom("block"_sv),
            append(
                list(atom("loop"_sv),
                     list(atom("br_if"_sv), atom(1),
                          list(atom("i32.eqz"_sv), compile_expression(statement.hwile.condition)))),
                compile_block(statement.hwile.body),
                list(list(atom("br"_sv), atom(0)))));

    case Statement_Kind::Assignment:
        return list(atom("set_local"_sv),
                    wat_ident(statement.assignment.var_name),
                    compile_expression(statement.assignment.value));

    case Statement_Kind::Expression:
        return compile_expression(statement.expression);

    case Statement_Kind::Return:
        return list(atom("return"_sv), compile_expression(statement.reeturn.value));
    }

    assert(0 && "Memory corruption?");
    return nullptr;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl 0x10(%rbp), %eax
cmpq $0x5, %rax
ja 0x52ce
movq %rdi, %rbx
leaq 0x10(%rbp), %r12
leaq 0x335c(%rip), %rcx      # 0x8274
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x4a72(%rip), %rdx      # 0x999a
pushq $0x5
popq %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6716
movq %rax, -0x38(%rbp)
leaq 0x4a5e(%rip), %rdx      # 0x99a0
pushq $0x4
popq %rsi
movq %rbx, %rdi
callq 0x6716
movq %rax, -0x30(%rbp)
leaq 0x4a4d(%rip), %rdx      # 0x99a5
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6716
movq %rax, %r15
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x6824
movq %rax, %r13
leaq 0x4a30(%rip), %rdx      # 0x99ab
pushq $0x7
popq %rsi
movq %rbx, %rdi
callq 0x6716
movq %rax, %r14
movq 0x10(%r12), %rsi
movq %rbx, %rdi
callq 0x4380
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x67fc
movq %rbx, %rdi
movq %r15, %rsi
movq %r13, %rdx
movq %rax, %rcx
callq 0x67aa
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
movq %rax, %rdx
callq 0x67fc
movq %rax, %r15
movq 0x18(%r12), %rsi
movq %rbx, %rdi
callq 0x52ee
movq %rax, %r12
leaq 0x49d5(%rip), %rdx      # 0x99b3
pushq $0x2
popq %rsi
movq %rbx, %rdi
callq 0x6716
movq %rax, %r14
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6824
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x67fc
movq %rbx, %rdi
movq %rax, %rsi
callq 0x6920
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %rax, %rcx
callq 0x695c
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
jmp 0x5072
leaq 0x390d(%rip), %rdx      # 0x893d
pushq $0x6
jmp 0x5053
movq 0x18(%r12), %r15
movq 0x20(%r12), %rcx
movq %r15, %rax
orq %rcx, %rax
jne 0x50f9
leaq 0x48a4(%rip), %rdx      # 0x98f5
pushq $0x4
popq %rsi
movq %rbx, %rdi
callq 0x6716
movq %rax, %r14
movq 0x10(%r12), %rsi
movq %rbx, %rdi
callq 0x4380
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x67fc
movq 0x10(%r12), %rsi
movq %rbx, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4380
leaq 0x490c(%rip), %rdx      # 0x99b6
pushq $0x9
popq %rsi
movq %rbx, %rdi
callq 0x6716
movq %rax, %r14
movq 0x10(%r12), %rsi
movq 0x18(%r12), %rdx
movq %rbx, %rdi
callq 0x3fbe
movq %rax, %r15
movq 0x20(%r12), %rsi
movq %rbx, %rdi
callq 0x4380
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %rax, %rcx
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x67aa
movq %rcx, -0x30(%rbp)
movl 0x4(%r12), %r14d
leaq 0x489f(%rip), %rdx      # 0x99a8
pushq $0x2
popq %rsi
movq %rbx, %rdi
callq 0x6716
movq %rax, -0x38(%rbp)
cmpl $0x1, %r14d
jne 0x51d1
movq 0x10(%r12), %rsi
movq %rbx, %rdi
callq 0x4380
movq %rax, -0x40(%rbp)
leaq 0x485b(%rip), %rdx      # 0x9995
pushq $0x4
popq %r13
movq %rbx, %rdi
movq %r13, %rsi
callq 0x6716
movq %rbx, %rdi
movq %rax, %rsi
callq 0x6920
movq %rax, %r14
movq %rbx, %rdi
movq %r15, %rsi
callq 0x52ee
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x68c6
movq %rax, %r12
leaq 0x37be(%rip), %rdx      # 0x8938
movq %rbx, %rdi
movq %r13, %rsi
callq 0x6716
movq %rbx, %rdi
movq %rax, %rsi
callq 0x6920
movq %rax, %r14
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
callq 0x52ee
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x68c6
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq %r12, %rcx
movq %rax, %r8
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6898
leaq 0x3aa2(%rip), %rdx      # 0x8c7a
pushq $0x6
popq %rsi
movq %rbx, %rdi
callq 0x6716
movq %rax, %r13
movq %rbx, %rdi
movl %r14d, %esi
callq 0x3fee
movq %rbx, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x67fc
movq %rax, -0x40(%rbp)
movq 0x10(%r12), %rsi
movq %rbx, %rdi
callq 0x4380
movq %rax, -0x48(%rbp)
leaq 0x477a(%rip), %rdx      # 0x9995
pushq $0x4
popq %r13
movq %rbx, %rdi
movq %r13, %rsi
callq 0x6716
movq %rbx, %rdi
movq %rax, %rsi
callq 0x6920
movq %rax, %r14
movq %rbx, %rdi
movq %r15, %rsi
callq 0x52ee
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x68c6
movq %rax, %r12
leaq 0x36dd(%rip), %rdx      # 0x8938
movq %rbx, %rdi
movq %r13, %rsi
callq 0x6716
movq %rbx, %rdi
movq %rax, %rsi
callq 0x6920
movq %rax, %r14
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
callq 0x52ee
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x68c6
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x48(%rbp), %rcx
movq %r12, %r8
movq %rax, %r9
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x692c
addq $0x8, %rbx
movq 0x8(%r12), %rsi
leaq 0x467c(%rip), %rdx      # 0x9942
movq %rbx, %rdi
callq 0x687c
leaq 0x3b36(%rip), %rdi      # 0x8e0b
leaq 0x38f2(%rip), %rsi      # 0x8bce
leaq 0x46dd(%rip), %rcx      # 0x99c0
movl $0x1ed, %edx            # imm = 0x1ED
callq 0x1090
nop