// File: /workspace/github_cpp_makefile_star2_O1/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 0x25618
movl $0x58, %edi
callq 0x8250
movq %rax, %r15
leaq 0xa8(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0xae59(%rip), %rsi      # 0x2b6e6
leaq 0xae58(%rip), %rdx      # 0x2b6ec
leaq 0x98(%rsp), %rdi
callq 0xfc42
movb $0x1, %bpl
leaq 0x98(%rsp), %rsi
movq %r15, %rdi
movl $0xa, %edx
callq 0x20250
movq (%rbx), %rax
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r15, %rsi
callq *(%rax)
movq 0x98(%rsp), %rdi
cmpq %r12, %rdi
je 0x208e3
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x8260
movq %r14, %rdi
callq 0x25620
movl %eax, %ebp
testl %eax, %eax
jle 0x2091c
xorl %r15d, %r15d
movq %r14, %rdi
movl %r15d, %esi
callq 0x2560c
movq %rax, %rdi
callq 0x1a81a
cmpl $0x7, %eax
jne 0x20914
movq %rbx, %rdi
callq 0x245d8
incl %r15d
cmpl %r15d, %ebp
jne 0x208f4
testl %ebp, %ebp
jle 0x20c2d
xorl %r15d, %r15d
leaq 0xad8a(%rip), %r12      # 0x2b6b8
leaq 0x17393(%rip), %r13     # 0x37cc8
movl %ebp, 0x4(%rsp)
movq %r14, 0x90(%rsp)
movq %r14, %rdi
movl %r15d, %esi
callq 0x2560c
movq %rax, %rdi
callq 0x1a81a
cmpl $0x9, %eax
ja 0x20c21
movl %eax, %eax
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
movq %r14, %rdi
movl %r15d, %esi
callq 0x2560c
movq %rax, %rdi
movq %r13, %rsi
leaq 0x173d8(%rip), %rdx     # 0x37d58
xorl %ecx, %ecx
callq 0x82a0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x20d5e
jmp 0x20c21
movq %r14, %rdi
movl %r15d, %esi
callq 0x2560c
movq %rax, %rdi
movq %r13, %rsi
leaq 0x172b9(%rip), %rdx     # 0x37c68
xorl %ecx, %ecx
callq 0x82a0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x20ec6
jmp 0x20c21
movq %r14, %rdi
movl %r15d, %esi
callq 0x2560c
movq %rax, %rdi
movq %r13, %rsi
leaq 0x192ea(%rip), %rdx     # 0x39cc8
xorl %ecx, %ecx
callq 0x82a0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x20850
jmp 0x20c21
movq %rbx, %r13
movl $0x50, %edi
callq 0x8250
movq %rax, 0x8(%rsp)
leaq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0x19dd8(%rip), %rax     # 0x3a7f0
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x10(%rsp), %rdi
callq 0xabf6
leaq 0x80(%rsp), %rax
movq %rax, 0x70(%rsp)
leaq 0x19dd0(%rip), %rax     # 0x3a810
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
callq 0xabf6
movb $0x1, %r12b
movq 0x8(%rsp), %r14
movq %r14, %rdi
leaq 0x10(%rsp), %rsi
movq %rbx, %rdx
callq 0x1caf8
movq %r13, %rbx
addq $0x8, %r14
movq (%r13), %rax
xorl %r12d, %r12d
movq %r13, %rdi
movq %r14, %rsi
callq *(%rax)
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x20aa7
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x8260
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
leaq 0x50(%rsp), %r12
movq 0x90(%rsp), %r14
je 0x20ad0
movq 0x20(%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 0x962e(%rip), %rsi      # 0x2a11f
leaq 0x9628(%rip), %rdx      # 0x2a120
callq 0xfc42
movq %r12, %rsi
movb $0x1, %r12b
movq %r13, %rdi
callq 0x1e3b8
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 0x20b39
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 0x19e99(%rip), %rax     # 0x3a9f0
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x30(%rsp), %rbp
movq %rbp, %rdi
callq 0xabf6
movb $0x1, %r12b
movq %r13, %rdi
movq %rbp, %rsi
callq 0x1dcc0
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 0xab16(%rip), %r12      # 0x2b6b8
leaq 0x1711f(%rip), %r13     # 0x37cc8
movl 0x4(%rsp), %ebp
je 0x20c21
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x8260
jmp 0x20c21
movq %r14, %rdi
movl %r15d, %esi
callq 0x2560c
movq %rax, %rdi
movq %r13, %rsi
leaq 0x16f1a(%rip), %rdx     # 0x37af0
xorl %ecx, %ecx
callq 0x82a0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x20d94
jmp 0x20c21
movq %r14, %rdi
movl %r15d, %esi
callq 0x2560c
movq %rax, %rdi
movq %r13, %rsi
leaq 0x16ebe(%rip), %rdx     # 0x37ac0
xorl %ecx, %ecx
callq 0x82a0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x20e6e
jmp 0x20c21
movq %r14, %rdi
movl %r15d, %esi
callq 0x2560c
incl %r15d
cmpl %r15d, %ebp
jne 0x20941
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movb $0x1, %bpl
jmp 0x20c67
movq %rax, %rbx
movq 0x98(%rsp), %rdi
cmpq %r12, %rdi
je 0x20c67
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x8260
testb %bpl, %bpl
je 0x20d55
movl $0x58, %esi
movq %r15, %rdi
jmp 0x20d50
movq %rax, %rbx
movb $0x1, %r12b
jmp 0x20cc2
movq %rax, %rbx
movb $0x1, %r12b
jmp 0x20cf1
movq %rax, %rbx
movb $0x1, %r12b
jmp 0x20d25
movq %rax, %rbx
movb $0x1, %r12b
jmp 0x20d41
movq %rax, %rbx
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x20cc2
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x8260
testb %r12b, %r12b
je 0x20d55
movl $0x30, %esi
jmp 0x20cfb
movq %rax, %rbx
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x20cf1
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x8260
testb %r12b, %r12b
je 0x20d55
movl $0x38, %esi
movq %r13, %rdi
jmp 0x20d50
movq %rax, %rbx
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x20d25
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x8260
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x20d41
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x8260
testb %r12b, %r12b
je 0x20d55
movl $0x50, %esi
movq 0x8(%rsp), %rdi
callq 0x8260
movq %rbx, %rdi
callq 0x8420
nop