// File: /workspace/github_cpp_makefile_star2_O3/martinda3[P]Compiler/src/JVM/JVMgenerateStatement.cpp
// Function: toyc::JVMgenerateStatement::BlockStatement(toyc::ASblockState*, toyc::JVMtargetCode*)
void JVMgenerateStatement::BlockStatement(ASblockState *ThisBlock,JVMtargetCode *tc) {
      int BlockVars = ThisBlock->getNumVarDef();
      tc->add(new limit("locals",10));  // Scooping is hard
      for (int Var = 0 ; Var <  BlockVars; Var++) // not sure if needed
      {
//          ASvarDef *VarInit = JVMgenerateStatement::ASvarDefinition(dynamic_cast<ASvarDef*>(ThisBlock->getVarDef(Var)), tc);
//          AStype *step = dynamic_cast<AStype*>(VarInit->getIdentifier(Var));
//          std::cout << "  "<< step->getExpr()->getLexeme() << std::endl;
//          JVMgenUtils::gen_ISTORE(*symTable->getSym(symTable->find(step->getExpr()->getLexeme())),tc);
      }
      ASreadState *read_set;
      ASexprState *expr_set; ASifState *If_set;
      ASwriteState *write_set; ASreturnState *return_set;
      ASblockState *FunctionBlock;
      int Blocks = ThisBlock->getNumStatement();
      for(int Block = 0 ; Block < Blocks ; Block++)
      {
          enum stateType ThisStatement = ThisBlock->getStatement(Block)->getType();

          switch (ThisStatement){
              case READstate:
                  JVMgenerateProgram::gen_input_stream_store(tc);
//                  tc->add(new line(LINE_COUNTER)); LINE_COUNTER++;    //  Bookmakr
                  break;
              case WRITEstate: // Untested
//                  tc->add(new GETSTATIC(OUTPUT_FIELD_SPEC,OUTPUT_DESCRIPTOR));
//                  JVMgenUtils::gen_ASTORE(*symTable->getSym(symTable->find("System.out")),tc);
//                  JVMgenerateProgram::gen_output_stream_store(tc);
//                  tc->add(new line(LINE_COUNTER)); LINE_COUNTER++;    //  Bookmakr
                  break;
              default: // Should not happen
                  break;
          }
      }
      for (int Block = 0 ; Block < Blocks ; Block++)
      {
          enum stateType ThisStatement = ThisBlock->getStatement(Block)->getType();
          switch (ThisStatement){
              case EXPRstate:
                  expr_set = dynamic_cast<ASexprState*>(ThisBlock->getStatement(Block));
                  JVMgenerateStatement::ExprStatement(expr_set, tc);
                  break;
              case BREAKstate: // not done
                  break;
              case BLOCKstate:
                  FunctionBlock = dynamic_cast<ASblockState*>(ThisBlock->getStatement(Block));
                  JVMgenerateStatement::BlockStatement(FunctionBlock, tc);
                  break;
              case IFstate:
                  If_set = dynamic_cast<ASifState*>(ThisBlock->getStatement(Block));
                  JVMgenerateStatement::IfStatement(If_set, tc);
                  break;
              case NULLstate:
                  break;
              case RETURNstate:
                  return_set = dynamic_cast<ASreturnState*>(ThisBlock->getStatement(Block));
                  JVMgenerateStatement::ReturnStatement(return_set, tc);
                  break;
              case WHILEstate:
                  break;
              case READstate:
                  read_set = dynamic_cast<ASreadState*>(ThisBlock->getStatement(Block));
                  JVMgenerateStatement::ReadStatement(read_set, tc);
                  break;
              case WRITEstate:
                  write_set = dynamic_cast<ASwriteState*>(ThisBlock->getStatement(Block));
                  JVMgenerateStatement::WriteStatement(write_set, tc);
                  break;
              case NEWLINEstate:
                  tc->add(new GETSTATIC(OUTPUT_FIELD_SPEC, OUTPUT_DESCRIPTOR));
                  tc->add(new LDC(" "));
                  tc->add(new INVOKEVIRTUAL(PRINT_STRING_METHOD_SPEC));
                  break;
              case EMPTYstate:
                  break;
              default:
                  break;
          }
      }
    }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rsi, %rbx
movq %rdi, %r14
callq 0x25c40
movl $0x58, %edi
callq 0x8250
movq %rax, %r15
leaq 0xa8(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0xa83f(%rip), %rsi      # 0x2b6e6
leaq 0xa83e(%rip), %rdx      # 0x2b6ec
leaq 0x98(%rsp), %rdi
callq 0xfc2a
movb $0x1, %bpl
leaq 0x98(%rsp), %rsi
movq %r15, %rdi
movl $0xa, %edx
callq 0x20870
movq (%rbx), %rax
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r15, %rsi
callq *(%rax)
movq 0x98(%rsp), %rdi
cmpq %r12, %rdi
je 0x20efd
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x8260
movq %r14, %rdi
callq 0x25c48
testl %eax, %eax
jle 0x2123c
movl %eax, %ebp
xorl %r15d, %r15d
movq %r14, %rdi
movl %r15d, %esi
callq 0x25c34
movq %rax, %rdi
callq 0x1ae52
cmpl $0x7, %eax
jne 0x20f32
movq %rbx, %rdi
callq 0x24c04
incl %r15d
cmpl %r15d, %ebp
jne 0x20f12
testl %ebp, %ebp
jle 0x2123c
xorl %r15d, %r15d
leaq 0xa76c(%rip), %r12      # 0x2b6b8
leaq 0x16ddd(%rip), %r13     # 0x37d30
movq %rbx, 0x90(%rsp)
movq %r14, %rdi
movl %r15d, %esi
callq 0x25c34
movq %rax, %rdi
callq 0x1ae52
cmpl $0x9, %eax
ja 0x21230
movl %eax, %eax
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
movq %r14, %rdi
movl %r15d, %esi
callq 0x25c34
movq %rax, %rdi
movq %r13, %rsi
leaq 0x16e26(%rip), %rdx     # 0x37dc0
xorl %ecx, %ecx
callq 0x82a0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x21384
jmp 0x21230
movq %r14, %rdi
movl %r15d, %esi
callq 0x25c34
movq %rax, %rdi
movq %r13, %rsi
leaq 0x16d07(%rip), %rdx     # 0x37cd0
xorl %ecx, %ecx
callq 0x82a0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x214ec
jmp 0x21230
movq %r14, %rdi
movl %r15d, %esi
callq 0x25c34
movq %rax, %rdi
movq %r13, %rsi
leaq 0x18cd0(%rip), %rdx     # 0x39cc8
xorl %ecx, %ecx
callq 0x82a0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x20e6a
jmp 0x21230
movl $0x50, %edi
callq 0x8250
movq %rax, 0x28(%rsp)
leaq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
leaq 0x197c1(%rip), %rax     # 0x3a7f0
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x8(%rsp), %rdi
callq 0xabdc
leaq 0x80(%rsp), %rax
movq %rax, 0x70(%rsp)
leaq 0x197b9(%rip), %rax     # 0x3a810
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x70(%rsp), %r13
movq %r13, %rdi
callq 0xabdc
movb $0x1, %r12b
movq 0x28(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movq %r13, %rdx
movq %rdi, %r13
callq 0x1d114
movq %r13, %rsi
addq $0x8, %rsi
movq (%rbx), %rax
xorl %r12d, %r12d
movq %rbx, %rdi
callq *(%rax)
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x210ba
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x8260
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
leaq 0x50(%rsp), %r12
je 0x210db
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x8260
movl $0x38, %edi
callq 0x8250
movq %rax, %r13
leaq 0x60(%rsp), %rax
movq %rax, 0x50(%rsp)
movq %r12, %rdi
leaq 0x9023(%rip), %rsi      # 0x2a11f
leaq 0x901d(%rip), %rdx      # 0x2a120
callq 0xfc2a
movq %r12, %rsi
movb $0x1, %r12b
movq %r13, %rdi
callq 0x1e9d4
movq %r13, %rsi
addq $0x8, %rsi
movq (%rbx), %rax
xorl %r12d, %r12d
movq %rbx, %rdi
callq *(%rax)
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x21144
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x8260
movl $0x30, %edi
callq 0x8250
movq %rax, %r13
leaq 0x40(%rsp), %rax
movq %rax, 0x30(%rsp)
leaq 0x1988e(%rip), %rax     # 0x3a9f0
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x30(%rsp), %rbx
movq %rbx, %rdi
callq 0xabdc
movb $0x1, %r12b
movq %r13, %rdi
movq %rbx, %rsi
callq 0x1e2dc
movq 0x90(%rsp), %rbx
movq %r13, %rsi
addq $0x8, %rsi
movq (%rbx), %rax
xorl %r12d, %r12d
movq %rbx, %rdi
callq *(%rax)
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
leaq 0xa503(%rip), %r12      # 0x2b6b8
leaq 0x16b74(%rip), %r13     # 0x37d30
je 0x21230
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x8260
jmp 0x21230
movq %r14, %rdi
movl %r15d, %esi
callq 0x25c34
movq %rax, %rdi
movq %r13, %rsi
leaq 0x16973(%rip), %rdx     # 0x37b58
xorl %ecx, %ecx
callq 0x82a0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x213ba
jmp 0x21230
movq %r14, %rdi
movl %r15d, %esi
callq 0x25c34
movq %rax, %rdi
movq %r13, %rsi
leaq 0x16917(%rip), %rdx     # 0x37b28
xorl %ecx, %ecx
callq 0x82a0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x21494
jmp 0x21230
movq %r14, %rdi
movl %r15d, %esi
callq 0x25c34
incl %r15d
cmpl %r15d, %ebp
jne 0x20f5b
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x212bc
movq %rax, %rbx
jmp 0x212f1
movq %rax, %rbx
jmp 0x2131c
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x2136c
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x8260
jmp 0x2136c
movq %rax, %rbx
jmp 0x2136c
movq %rax, %rbx
movq 0x98(%rsp), %rdi
cmpq %r12, %rdi
je 0x212b3
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x8260
testb %bpl, %bpl
je 0x2137b
movl $0x58, %esi
movq %r15, %rdi
jmp 0x21376
movq %rax, %rbx
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x212e8
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x8260
testb %r12b, %r12b
je 0x2137b
movl $0x30, %esi
jmp 0x21321
movq %rax, %rbx
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x21317
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x8260
testb %r12b, %r12b
je 0x2137b
movl $0x38, %esi
movq %r13, %rdi
jmp 0x21376
movq %rax, %rbx
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x2134b
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x8260
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x21367
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x8260
testb %r12b, %r12b
je 0x2137b
movl $0x50, %esi
movq 0x28(%rsp), %rdi
callq 0x8260
movq %rbx, %rdi
callq 0x8420
nop