// File: /workspace/github_cpp_makefile_star2_O1/luooofan[P]hblzbd_compiler/src/generate_ir.cpp
// Function: ast::Root::GenerateIR(ir::ContextInfo&, std::vector<ir::IR*, std::allocator<ir::IR*>>&)
void Root::GenerateIR(ir::ContextInfo &ctx, std::vector<ir::IR *> &gIRList) {
  // 把系统库中的函数添加到函数表中
  // NOTE: 库函数size为0
  ir::gFuncTable.insert({"getint", {INT, -1}});
  ir::gFuncTable.insert({"getch", {INT, -1}});
  ir::FuncTableItem func_item = {INT, -1};
  func_item.shape_list_.push_back({-1});
  ir::gFuncTable.insert({"getarray", func_item});
  ir::FuncTableItem func_item_void = {VOID, -1};
  func_item_void.shape_list_.push_back({});
  ir::gFuncTable.insert({"putint", func_item_void});
  ir::gFuncTable.insert({"putch", func_item_void});
  ir::gFuncTable.insert({"_sysy_starttime", func_item_void});
  ir::gFuncTable.insert({"_sysy_stoptime", func_item_void});
  func_item_void.shape_list_.push_back({-1});
  ir::gFuncTable.insert({"putarray", func_item_void});
  ir::FuncTableItem memset_func_item = {VOID, -1};
  memset_func_item.shape_list_.push_back({-1});
  memset_func_item.shape_list_.push_back({});
  memset_func_item.shape_list_.push_back({});
  ir::gFuncTable.insert({"memset", memset_func_item});
  // TODO: string type and putf function
  // ir::gFuncTable.insert({"putf", {VOID}});

  // 创建一张全局符号表 全局作用域id为0 父作用域id为-1
  ir::gScopes.push_back({0, -1});
  // auto &extern_glo_symtab = ir::gScopes[0].symbol_table_;
  // extern_glo_symtab.insert({"_sysy_idx", {false, false, 0}});
  // auto general_extern_array = ir::SymbolTableItem(true, false, 0);
  // general_extern_array.shape_.push_back(1024);
  // general_extern_array.width_.push_back(1024 * 4);
  // general_extern_array.width_.push_back(4);
  // general_extern_array.initval_ = std::vector<int>(1024, 0);
  // extern_glo_symtab.insert({"_sysy_l1", general_extern_array});
  // extern_glo_symtab.insert({"_sysy_l2", general_extern_array});
  // extern_glo_symtab.insert({"_sysy_h", general_extern_array});
  // extern_glo_symtab.insert({"_sysy_m", general_extern_array});
  // extern_glo_symtab.insert({"_sysy_s", general_extern_array});
  // extern_glo_symtab.insert({"_sysy_us", general_extern_array});

  // 初始化上下文信息中的当前作用域id
  ctx.scope_id_ = 0;
  ctx.is_assigned_ = false;

#ifdef GENIR_TIME_CONTROL
  clock_t begin, end;
  double cost;
  begin = clock();
#endif

  // 对每个compunit语义分析并生成IR
  int i = 1;
  for (const auto &ele : this->compunit_list_) {
    if (auto func_def = dynamic_cast<FunctionDefine *>(ele)) {
      if (dead_func_set.count(func_def->name_.name_)) continue;
    }
    ele->GenerateIR(ctx, gIRList);
  }

#ifdef GENIR_TIME_CONTROL
  end = clock();
  cost = (double)(end - begin) / CLOCKS_PER_SEC;
  std::cout << "GenIrRoot cost: " << cost << "s" << std::endl;
  if (cost > 3) exit(233);
#endif

  // 检查是否有main函数 main函数的返回值是否是int
  auto func_iter = ir::gFuncTable.find("main");
  if (func_iter == ir::gFuncTable.end()) {
    ir::SemanticError(this->line_no_, "no main() function");
  } else if ((*func_iter).second.ret_type_ == VOID) {
    ir::SemanticError(this->line_no_, "main() return type not int");
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rdx, 0x80(%rsp)
movq %rsi, %r13
movq %rdi, 0x50(%rsp)
movabsq $-0xfffffedf, %r15      # imm = 0xFFFFFFFF00000121
leaq 0x90(%rsp), %rbx
movq %r15, -0x8(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
movq $0x0, 0x10(%rbx)
leaq 0x5c4d8(%rip), %rsi     # 0x6f91c
movq %rsp, %rdi
leaq 0x88(%rsp), %rdx
callq 0x247a2
leaq 0x7d135(%rip), %rdi     # 0x90590
movq %rsp, %rsi
callq 0x25a96
leaq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x25816
movq -0x28(%r14), %rdi
leaq 0x10(%rsp), %r12
cmpq %r12, %rdi
je 0x1348b
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x82a0
movq %rbx, %rdi
callq 0x25816
leaq 0x90(%rsp), %rbx
movq %r15, -0x8(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
movq $0x0, 0x10(%rbx)
leaq 0x5c46f(%rip), %rsi     # 0x6f923
movq %rsp, %rdi
leaq 0x88(%rsp), %rdx
callq 0x2483c
leaq 0x7d0c5(%rip), %rdi     # 0x90590
movq %rsp, %rsi
callq 0x25a96
leaq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x25816
movq -0x28(%r14), %rdi
cmpq %r12, %rdi
je 0x134f6
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x82a0
movq %rbx, %rdi
callq 0x25816
leaq 0x90(%rsp), %rbx
movq %r15, -0x8(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
movq $0x0, 0x10(%rbx)
leaq 0x60(%rsp), %rsi
movl $0xffffffff, (%rsi)     # imm = 0xFFFFFFFF
movq %rsp, %rdi
leaq 0xa8(%rsp), %rcx
movl $0x1, %edx
callq 0x248ae
movq %rsp, %rsi
movq %rbx, %rdi
callq 0x25e16
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x13559
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
leaq 0x5c3c9(%rip), %rsi     # 0x6f929
movq %rsp, %rdi
leaq 0x88(%rsp), %rdx
callq 0x248f4
leaq 0x7d019(%rip), %rdi     # 0x90590
movq %rsp, %rsi
callq 0x25a96
leaq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x25816
movq -0x28(%rbx), %rdi
cmpq %r12, %rdi
je 0x135a2
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x82a0
movabsq $-0xfffffede, %r14      # imm = 0xFFFFFFFF00000122
leaq 0x68(%rsp), %r15
movq %r14, -0x8(%r15)
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
xorl %eax, %eax
movq %rax, 0x10(%r15)
movq %rsp, %rsi
movq %rax, 0x10(%rsi)
movaps %xmm0, (%rsi)
movq %r15, %rdi
callq 0x25e16
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x135ea
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
leaq 0x5c341(%rip), %rsi     # 0x6f932
movq %rsp, %rdi
leaq 0x60(%rsp), %rdx
callq 0x24966
leaq 0x7cf8b(%rip), %rdi     # 0x90590
movq %rsp, %rsi
callq 0x25a96
leaq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x25816
movq -0x28(%rbx), %rdi
cmpq %r12, %rdi
je 0x13630
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x82a0
leaq 0x5c302(%rip), %rsi     # 0x6f939
movq %rsp, %rdi
leaq 0x60(%rsp), %rdx
callq 0x249d8
leaq 0x7cf45(%rip), %rdi     # 0x90590
movq %rsp, %rsi
callq 0x25a96
leaq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x25816
movq -0x28(%rbx), %rdi
cmpq %r12, %rdi
je 0x13676
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x82a0
leaq 0x5c2cb(%rip), %rsi     # 0x6f948
movq %rsp, %rdi
leaq 0x60(%rsp), %rdx
callq 0x24a4a
leaq 0x7ceff(%rip), %rdi     # 0x90590
movq %rsp, %rsi
callq 0x25a96
leaq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x25816
movq -0x28(%rbx), %rdi
cmpq %r12, %rdi
je 0x136bc
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x82a0
leaq 0x5c295(%rip), %rsi     # 0x6f958
movq %rsp, %rdi
leaq 0x60(%rsp), %rdx
callq 0x24abc
leaq 0x7ceb9(%rip), %rdi     # 0x90590
movq %rsp, %rsi
callq 0x25a96
leaq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x25816
movq -0x28(%rbx), %rdi
cmpq %r12, %rdi
je 0x13702
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x82a0
leaq 0xa8(%rsp), %rsi
movl $0xffffffff, (%rsi)     # imm = 0xFFFFFFFF
movq %rsp, %rdi
leaq 0x5c(%rsp), %rcx
movl $0x1, %edx
callq 0x248ae
movq %rsp, %rsi
movq %r15, %rdi
callq 0x25e16
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x13743
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
leaq 0x5c1f5(%rip), %rsi     # 0x6f93f
movq %rsp, %rdi
leaq 0x60(%rsp), %rdx
callq 0x248f4
leaq 0x7ce32(%rip), %rdi     # 0x90590
movq %rsp, %rsi
callq 0x25a96
leaq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x25816
movq -0x28(%rbx), %rdi
cmpq %r12, %rdi
je 0x13789
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x82a0
leaq 0xb0(%rsp), %rbx
movq %r14, -0x8(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
movq $0x0, 0x10(%rbx)
leaq 0x5c(%rsp), %rsi
movl $0xffffffff, (%rsi)     # imm = 0xFFFFFFFF
movq %rsp, %rdi
leaq 0x4f(%rsp), %rcx
movl $0x1, %edx
callq 0x248ae
movq %rsp, %rsi
movq %rbx, %rdi
callq 0x25e16
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x137e1
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
xorps %xmm0, %xmm0
movq %rsp, %rsi
movaps %xmm0, (%rsi)
movq $0x0, 0x10(%rsi)
movq %rbx, %rdi
callq 0x25e16
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x13810
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
xorps %xmm0, %xmm0
movq %rsp, %rsi
movaps %xmm0, (%rsi)
movq $0x0, 0x10(%rsi)
movq %rbx, %rdi
callq 0x25e16
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x1383f
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
leaq 0x5c121(%rip), %rsi     # 0x6f967
movq %rsp, %rdi
leaq 0xa8(%rsp), %rdx
callq 0x24966
leaq 0x7cd33(%rip), %rdi     # 0x90590
movq %rsp, %rsi
callq 0x25a96
leaq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x25816
movq -0x28(%rbx), %rdi
cmpq %r12, %rdi
je 0x13888
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x82a0
leaq 0x30(%rsp), %rax
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%rax)
movl $0x3f800000, -0x10(%rax) # imm = 0x3F800000
movups %xmm0, -0x8(%rax)
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
movq %rcx, 0x8(%rax)
leaq 0x7ccb8(%rip), %rdi     # 0x90578
movq %rsp, %rsi
callq 0x26072
movq %rsp, %rdi
callq 0x25864
movl $0x0, (%r13)
movb $0x0, 0x209(%r13)
movq 0x50(%rsp), %rax
movq 0x10(%rax), %rbp
movq 0x18(%rax), %r12
cmpq %r12, %rbp
je 0x13959
leaq 0x7cc07(%rip), %rbx     # 0x90500
leaq 0x7b560(%rip), %r14     # 0x8ee60
leaq 0x7b321(%rip), %r15     # 0x8ec28
movq (%rbp), %rdi
testq %rdi, %rdi
je 0x1391f
movq %r14, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x8320
jmp 0x13921
xorl %eax, %eax
testq %rax, %rax
je 0x1393b
movq 0x10(%rax), %rax
movq 0x10(%rax), %rsi
movq %rbx, %rdi
callq 0x262be
testq %rax, %rax
jne 0x13950
movq (%rbp), %rdi
movq (%rdi), %rax
movq %r13, %rsi
movq 0x80(%rsp), %rdx
callq *0x10(%rax)
addq $0x8, %rbp
cmpq %r12, %rbp
jne 0x13907
movq %rsp, %rdi
leaq 0x10(%rsp), %r12
movq %r12, (%rdi)
leaq 0x5c1f5(%rip), %rsi     # 0x6fb60
leaq 0x5c1f2(%rip), %rdx     # 0x6fb64
callq 0xcaa0
leaq 0x7cc12(%rip), %rdi     # 0x90590
movq %rsp, %rsi
leaq 0x90(%rsp), %r14
leaq 0x68(%rsp), %r15
leaq 0xb0(%rsp), %r13
callq 0x11a14
movq %rax, %rbx
movq (%rsp), %rdi
cmpq %r12, %rdi
je 0x139b4
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x82a0
testq %rbx, %rbx
je 0x139ef
cmpl $0x122, 0x28(%rbx)      # imm = 0x122
jne 0x13a30
movq 0x50(%rsp), %rax
movl 0x8(%rax), %ebx
movq %rsp, %rdi
movq %r12, (%rdi)
leaq 0x5c1a1(%rip), %rsi     # 0x6fb78
leaq 0x5c1b4(%rip), %rdx     # 0x6fb92
callq 0xcaa0
movq %rsp, %rsi
movl %ebx, %edi
callq 0x2b12a
jmp 0x13a1a
movq 0x50(%rsp), %rax
movl 0x8(%rax), %ebx
movq %rsp, %rdi
movq %r12, (%rdi)
leaq 0x5c161(%rip), %rsi     # 0x6fb65
leaq 0x5c16c(%rip), %rdx     # 0x6fb77
callq 0xcaa0
movq %rsp, %rsi
movl %ebx, %edi
callq 0x2b12a
movq (%rsp), %rdi
cmpq %r12, %rdi
je 0x13a30
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x82a0
movq %r13, %rdi
callq 0x25816
movq %r15, %rdi
callq 0x25816
movq %r14, %rdi
callq 0x25816
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x13a68
jmp 0x13b62
jmp 0x13a68
jmp 0x13b62
movq %rax, %r15
movq (%rsp), %rdi
cmpq %r12, %rdi
je 0x13b65
movq 0x10(%rsp), %rsi
incq %rsi
jmp 0x13ac8
jmp 0x13b62
movq %rax, %r15
movq %rsp, %rdi
callq 0x25864
jmp 0x13b65
movq %rax, %r15
movq %rsp, %rdi
callq 0x24814
jmp 0x13b65
jmp 0x13b62
jmp 0x13ab0
jmp 0x13ab0
movq %rax, %r15
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x13b65
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
jmp 0x13b65
jmp 0x13b62
jmp 0x13aeb
jmp 0x13af8
jmp 0x13afd
jmp 0x13af8
jmp 0x13aeb
jmp 0x13af8
jmp 0x13aeb
jmp 0x13af8
jmp 0x13aeb
jmp 0x13af8
movq %rax, %r15
movq %rsp, %rdi
callq 0x24814
jmp 0x13b72
movq %rax, %r15
jmp 0x13b72
movq %rax, %r15
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x13b72
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
jmp 0x13b72
movq %rax, %r15
movq %rsp, %rdi
callq 0x24814
jmp 0x13b7c
jmp 0x13b42
movq %rax, %r15
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x13b7c
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
jmp 0x13b7c
movq %rax, %r15
jmp 0x13b7c
jmp 0x13b4b
jmp 0x13b58
movq %rax, %r15
movq %rsp, %rdi
callq 0x24814
jmp 0x13b5b
movq %rax, %r15
movq %rbx, %rdi
jmp 0x13b84
jmp 0x13b62
movq %rax, %r15
leaq 0xb0(%rsp), %rdi
callq 0x25816
leaq 0x68(%rsp), %rdi
callq 0x25816
leaq 0x90(%rsp), %rdi
callq 0x25816
movq %r15, %rdi
callq 0x8540
nop