// File: 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
movq %rsp, %rbp
subq $0x210, %rsp            # imm = 0x210
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
callq 0x2dc80
movl %eax, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x150(%rbp)
movl $0x58, %edi
callq 0x9350
movq %rax, -0x148(%rbp)
movb $0x1, -0x4d(%rbp)
leaq -0x39(%rbp), %rdi
movq %rdi, -0x140(%rbp)
callq 0x95c0
movq -0x140(%rbp), %rdx
leaq 0xe019(%rip), %rsi      # 0x347a6
leaq -0x38(%rbp), %rdi
callq 0x13550
jmp 0x26798
movq -0x148(%rbp), %rdi
leaq -0x38(%rbp), %rsi
movl $0xa, %edx
callq 0x26100
jmp 0x267af
movq -0x148(%rbp), %rsi
movq -0x150(%rbp), %rdi
movb $0x0, -0x4d(%rbp)
movq (%rdi), %rax
movq (%rax), %rax
callq *%rax
jmp 0x267cb
leaq -0x38(%rbp), %rdi
callq 0x9658
leaq -0x39(%rbp), %rdi
callq 0x93b0
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x26843
jmp 0x267ee
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x267e4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x2681c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x9658
leaq -0x39(%rbp), %rdi
callq 0x93b0
testb $0x1, -0x4d(%rbp)
jne 0x2682d
jmp 0x2683e
movq -0x148(%rbp), %rdi
movl $0x58, %esi
callq 0x9360
jmp 0x26f12
movq -0x8(%rbp), %rdi
callq 0x2dca0
movl %eax, -0x8c(%rbp)
movl $0x0, -0x90(%rbp)
movl -0x90(%rbp), %eax
cmpl -0x8c(%rbp), %eax
jge 0x268c9
movq -0x8(%rbp), %rdi
movl -0x90(%rbp), %esi
callq 0x2dc60
movq %rax, %rdi
callq 0x1ee10
movl %eax, -0x94(%rbp)
movl -0x94(%rbp), %eax
movl %eax, -0x154(%rbp)
subl $0x7, %eax
je 0x268a7
jmp 0x2689a
movl -0x154(%rbp), %eax
subl $0x8, %eax
je 0x268b2
jmp 0x268b4
movq -0x10(%rbp), %rdi
callq 0x2cd50
jmp 0x268b6
jmp 0x268b6
jmp 0x268b6
jmp 0x268b8
movl -0x90(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
jmp 0x2685c
movl $0x0, -0x98(%rbp)
movl -0x98(%rbp), %eax
cmpl -0x8c(%rbp), %eax
jge 0x26f09
movq -0x8(%rbp), %rdi
movl -0x98(%rbp), %esi
callq 0x2dc60
movq %rax, %rdi
callq 0x1ee10
movl %eax, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
movq %rax, -0x160(%rbp)
subq $0xa, %rax
ja 0x26ef1
movq -0x160(%rbp), %rax
leaq 0xde4d(%rip), %rcx      # 0x34774
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x8(%rbp), %rdi
movl -0x98(%rbp), %esi
callq 0x2dc60
movq %rax, -0x168(%rbp)
cmpq $0x0, %rax
je 0x26973
movq -0x168(%rbp), %rdi
leaq 0x1c2ee(%rip), %rsi     # 0x42c48
leaq 0x1c377(%rip), %rdx     # 0x42cd8
xorl %eax, %eax
movl %eax, %ecx
callq 0x93e0
movq %rax, -0x170(%rbp)
jmp 0x2697e
xorl %eax, %eax
movq %rax, -0x170(%rbp)
jmp 0x2697e
movq -0x170(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x26f20
jmp 0x26ef3
jmp 0x26ef3
movq -0x8(%rbp), %rdi
movl -0x98(%rbp), %esi
callq 0x2dc60
movq %rax, -0x178(%rbp)
cmpq $0x0, %rax
je 0x269e3
movq -0x178(%rbp), %rdi
leaq 0x1c27e(%rip), %rsi     # 0x42c48
leaq 0x1e2df(%rip), %rdx     # 0x44cb0
xorl %eax, %eax
movl %eax, %ecx
callq 0x93e0
movq %rax, -0x180(%rbp)
jmp 0x269ee
xorl %eax, %eax
movq %rax, -0x180(%rbp)
jmp 0x269ee
movq -0x180(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x26730
jmp 0x26ef3
movq -0x8(%rbp), %rdi
movl -0x98(%rbp), %esi
callq 0x2dc60
movq %rax, -0x188(%rbp)
cmpq $0x0, %rax
je 0x26a54
movq -0x188(%rbp), %rdi
leaq 0x1c20d(%rip), %rsi     # 0x42c48
leaq 0x1bffe(%rip), %rdx     # 0x42a40
xorl %eax, %eax
movl %eax, %ecx
callq 0x93e0
movq %rax, -0x190(%rbp)
jmp 0x26a5f
xorl %eax, %eax
movq %rax, -0x190(%rbp)
jmp 0x26a5f
movq -0x190(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x26f90
jmp 0x26ef3
jmp 0x26ef3
movq -0x8(%rbp), %rdi
movl -0x98(%rbp), %esi
callq 0x2dc60
movq %rax, -0x198(%rbp)
cmpq $0x0, %rax
je 0x26ac4
movq -0x198(%rbp), %rdi
leaq 0x1c19d(%rip), %rsi     # 0x42c48
leaq 0x1bf5e(%rip), %rdx     # 0x42a10
xorl %eax, %eax
movl %eax, %ecx
callq 0x93e0
movq %rax, -0x1a0(%rbp)
jmp 0x26acf
xorl %eax, %eax
movq %rax, -0x1a0(%rbp)
jmp 0x26acf
movq -0x1a0(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x27170
jmp 0x26ef3
jmp 0x26ef3
movq -0x8(%rbp), %rdi
movl -0x98(%rbp), %esi
callq 0x2dc60
movq %rax, -0x1a8(%rbp)
cmpq $0x0, %rax
je 0x26b34
movq -0x1a8(%rbp), %rdi
leaq 0x1c12d(%rip), %rsi     # 0x42c48
leaq 0x1c036(%rip), %rdx     # 0x42b58
xorl %eax, %eax
movl %eax, %ecx
callq 0x93e0
movq %rax, -0x1b0(%rbp)
jmp 0x26b3f
xorl %eax, %eax
movq %rax, -0x1b0(%rbp)
jmp 0x26b3f
movq -0x1b0(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x271f0
jmp 0x26ef3
movq -0x8(%rbp), %rdi
movl -0x98(%rbp), %esi
callq 0x2dc60
movq %rax, -0x1b8(%rbp)
cmpq $0x0, %rax
je 0x26b9f
movq -0x1b8(%rbp), %rdi
leaq 0x1c0c2(%rip), %rsi     # 0x42c48
leaq 0x1c05b(%rip), %rdx     # 0x42be8
xorl %eax, %eax
movl %eax, %ecx
callq 0x93e0
movq %rax, -0x1c0(%rbp)
jmp 0x26baa
xorl %eax, %eax
movq %rax, -0x1c0(%rbp)
jmp 0x26baa
movq -0x1c0(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x27200
jmp 0x26ef3
movq -0x10(%rbp), %rax
movq %rax, -0x1d0(%rbp)
movl $0x50, %edi
callq 0x9350
movq %rax, -0x1c8(%rbp)
movb $0x1, -0xe1(%rbp)
leaq 0x1ecef(%rip), %rsi     # 0x458e0
leaq -0xc0(%rbp), %rdi
callq 0x9170
jmp 0x26bff
leaq 0x1ecfa(%rip), %rsi     # 0x45900
leaq -0xe0(%rbp), %rdi
callq 0x9170
jmp 0x26c14
movq -0x1c8(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
leaq -0xe0(%rbp), %rdx
callq 0x217d0
jmp 0x26c30
movq -0x1c8(%rbp), %rcx
movb $0x0, -0xe1(%rbp)
xorl %eax, %eax
cmpq $0x0, %rcx
movq %rax, -0x1d8(%rbp)
je 0x26c5f
movq -0x1c8(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x1d8(%rbp)
movq -0x1d0(%rbp), %rdi
movq -0x1d8(%rbp), %rsi
movq (%rdi), %rax
movq (%rax), %rax
callq *%rax
jmp 0x26c77
leaq -0xe0(%rbp), %rdi
callq 0x9658
leaq -0xc0(%rbp), %rdi
callq 0x9658
movq -0x10(%rbp), %rax
movq %rax, -0x1f0(%rbp)
movl $0x38, %edi
callq 0x9350
movq %rax, -0x1e8(%rbp)
movb $0x1, -0x10a(%rbp)
leaq -0x109(%rbp), %rdi
movq %rdi, -0x1e0(%rbp)
callq 0x95c0
movq -0x1e0(%rbp), %rdx
leaq 0xc480(%rip), %rsi      # 0x33153
leaq -0x108(%rbp), %rdi
callq 0x13550
jmp 0x26ce1
movq -0x1e8(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x23b10
jmp 0x26cf6
movq -0x1e8(%rbp), %rcx
movb $0x0, -0x10a(%rbp)
xorl %eax, %eax
cmpq $0x0, %rcx
movq %rax, -0x1f8(%rbp)
je 0x26d25
movq -0x1e8(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x1f8(%rbp)
movq -0x1f0(%rbp), %rdi
movq -0x1f8(%rbp), %rsi
movq (%rdi), %rax
movq (%rax), %rax
callq *%rax
jmp 0x26d3d
leaq -0x108(%rbp), %rdi
callq 0x9658
leaq -0x109(%rbp), %rdi
callq 0x93b0
movq -0x10(%rbp), %rax
movq %rax, -0x208(%rbp)
movl $0x30, %edi
callq 0x9350
movq %rax, -0x200(%rbp)
movb $0x1, -0x131(%rbp)
leaq 0x1ed61(%rip), %rsi     # 0x45ae0
leaq -0x130(%rbp), %rdi
callq 0x9170
jmp 0x26d8d
movq -0x200(%rbp), %rdi
leaq -0x130(%rbp), %rsi
callq 0x22fe0
jmp 0x26da2
movq -0x200(%rbp), %rcx
movb $0x0, -0x131(%rbp)
xorl %eax, %eax
cmpq $0x0, %rcx
movq %rax, -0x210(%rbp)
je 0x26dd1
movq -0x200(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x210(%rbp)
movq -0x208(%rbp), %rdi
movq -0x210(%rbp), %rsi
movq (%rdi), %rax
movq (%rax), %rax
callq *%rax
jmp 0x26de9
leaq -0x130(%rbp), %rdi
callq 0x9658
jmp 0x26ef3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x26e3a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x26e2e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x9658
leaq -0xc0(%rbp), %rdi
callq 0x9658
testb $0x1, -0xe1(%rbp)
jne 0x26e45
jmp 0x26e56
movq -0x1c8(%rbp), %rdi
movl $0x50, %esi
callq 0x9360
jmp 0x26f12
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x26e81
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x9658
leaq -0x109(%rbp), %rdi
callq 0x93b0
testb $0x1, -0x10a(%rbp)
jne 0x26e98
jmp 0x26ea9
movq -0x1e8(%rbp), %rdi
movl $0x38, %esi
callq 0x9360
jmp 0x26f12
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x26ed1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x130(%rbp), %rdi
callq 0x9658
testb $0x1, -0x131(%rbp)
jne 0x26edc
jmp 0x26eed
movq -0x200(%rbp), %rdi
movl $0x30, %esi
callq 0x9360
jmp 0x26f12
jmp 0x26ef3
jmp 0x26ef3
jmp 0x26ef5
movl -0x98(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x98(%rbp)
jmp 0x268d3
addq $0x210, %rsp            # imm = 0x210
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x95b0
nopl (%rax,%rax)