// File: /workspace/github_cpp_makefile_star2_O2/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 $0xd8, %rsp
movq %rdx, 0x90(%rsp)
movq %rsi, 0x60(%rsp)
movq %rdi, %r13
movabsq $-0xfffffedf, %r14      # imm = 0xFFFFFFFF00000121
leaq 0xa0(%rsp), %rbx
movq %r14, -0x8(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
andq $0x0, 0x10(%rbx)
leaq 0x46cf3(%rip), %rsi     # 0x568b8
leaq 0x10(%rsp), %rdi
leaq 0x98(%rsp), %rdx
callq 0x1a61a
leaq 0x71982(%rip), %rdi     # 0x81560
leaq 0x10(%rsp), %rsi
callq 0x1b65c
leaq 0x10(%rsp), %rdi
callq 0x1a656
movq %rbx, %rdi
callq 0x1b336
leaq 0xa0(%rsp), %rbx
movq %r14, -0x8(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
andq $0x0, 0x10(%rbx)
leaq 0x46ca7(%rip), %rsi     # 0x568bf
leaq 0x10(%rsp), %rdi
leaq 0x98(%rsp), %rdx
callq 0x1a66c
leaq 0x7192f(%rip), %rdi     # 0x81560
leaq 0x10(%rsp), %rsi
callq 0x1b65c
leaq 0x10(%rsp), %rdi
callq 0x1a656
movq %rbx, %rdi
callq 0x1b336
leaq 0xa0(%rsp), %rbx
movq %r14, -0x8(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
andq $0x0, 0x10(%rbx)
leaq 0x68(%rsp), %rsi
orl $-0x1, (%rsi)
leaq 0x10(%rsp), %rdi
pushq $0x1
popq %rdx
leaq 0xb8(%rsp), %rcx
callq 0x1a6a8
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x1ba18
leaq 0x10(%rsp), %rdi
callq 0x1b2b6
leaq 0x46c26(%rip), %rsi     # 0x568c5
leaq 0x10(%rsp), %rdi
leaq 0x98(%rsp), %rdx
callq 0x1a6de
leaq 0x718a8(%rip), %rdi     # 0x81560
leaq 0x10(%rsp), %rsi
callq 0x1b65c
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x1a656
movabsq $-0xfffffede, %r14      # imm = 0xFFFFFFFF00000122
leaq 0x70(%rsp), %r15
movq %r14, -0x8(%r15)
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
andq $0x0, 0x10(%r15)
movaps %xmm0, (%rbx)
andq $0x0, 0x10(%rbx)
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x1ba18
leaq 0x10(%rsp), %rdi
callq 0x1b2b6
leaq 0x46bba(%rip), %rsi     # 0x568ce
leaq 0x10(%rsp), %rdi
leaq 0x68(%rsp), %rdx
callq 0x1a71a
leaq 0x71836(%rip), %rdi     # 0x81560
leaq 0x10(%rsp), %rsi
callq 0x1b65c
leaq 0x10(%rsp), %rdi
callq 0x1a656
leaq 0x46b90(%rip), %rsi     # 0x568d5
leaq 0x10(%rsp), %rdi
leaq 0x68(%rsp), %rdx
callq 0x1a756
leaq 0x71805(%rip), %rdi     # 0x81560
leaq 0x10(%rsp), %rsi
callq 0x1b65c
leaq 0x10(%rsp), %rdi
callq 0x1a656
leaq 0x46b6e(%rip), %rsi     # 0x568e4
leaq 0x10(%rsp), %rdi
leaq 0x68(%rsp), %rdx
callq 0x1a792
leaq 0x717d4(%rip), %rdi     # 0x81560
leaq 0x10(%rsp), %rsi
callq 0x1b65c
leaq 0x10(%rsp), %rdi
callq 0x1a656
leaq 0x46b4d(%rip), %rsi     # 0x568f4
leaq 0x10(%rsp), %rdi
leaq 0x68(%rsp), %rdx
callq 0x1a7ce
leaq 0x717a3(%rip), %rdi     # 0x81560
leaq 0x10(%rsp), %rsi
callq 0x1b65c
leaq 0x10(%rsp), %rdi
callq 0x1a656
leaq 0xb8(%rsp), %rsi
orl $-0x1, (%rsi)
leaq 0x10(%rsp), %rdi
pushq $0x1
popq %rdx
leaq 0xc(%rsp), %rcx
callq 0x1a6a8
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x1ba18
leaq 0x10(%rsp), %rdi
callq 0x1b2b6
leaq 0x46acf(%rip), %rsi     # 0x568db
leaq 0x10(%rsp), %rdi
leaq 0x68(%rsp), %rdx
callq 0x1a6de
leaq 0x7173e(%rip), %rdi     # 0x81560
leaq 0x10(%rsp), %rsi
callq 0x1b65c
leaq 0x10(%rsp), %rdi
callq 0x1a656
leaq 0xc0(%rsp), %r15
movq %r14, -0x8(%r15)
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
andq $0x0, 0x10(%r15)
leaq 0xc(%rsp), %rsi
orl $-0x1, (%rsi)
leaq 0x10(%rsp), %rdi
pushq $0x1
popq %rdx
leaq 0x5f(%rsp), %rcx
callq 0x1a6a8
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x1ba18
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x1b2b6
xorps %xmm0, %xmm0
movaps %xmm0, (%rbx)
andq $0x0, 0x10(%rbx)
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x1ba18
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x1b2b6
xorps %xmm0, %xmm0
movaps %xmm0, (%rbx)
andq $0x0, 0x10(%rbx)
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x1ba18
leaq 0x10(%rsp), %rdi
callq 0x1b2b6
leaq 0x46a33(%rip), %rsi     # 0x56903
leaq 0x10(%rsp), %rdi
leaq 0xb8(%rsp), %rdx
callq 0x1a71a
leaq 0x71677(%rip), %rdi     # 0x81560
leaq 0x10(%rsp), %rsi
callq 0x1b65c
leaq 0x10(%rsp), %rdi
callq 0x1a656
leaq 0x40(%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 0x71613(%rip), %rdi     # 0x81548
leaq 0x10(%rsp), %rsi
callq 0x1bc58
leaq 0x10(%rsp), %rdi
callq 0x1b358
movq 0x60(%rsp), %rax
andl $0x0, (%rax)
movb $0x0, 0x209(%rax)
movq 0x10(%r13), %r12
movq %r13, 0x88(%rsp)
movq 0x18(%r13), %r13
leaq 0x6ff21(%rip), %rbx     # 0x7fe90
leaq 0x6fce2(%rip), %r14     # 0x7fc58
leaq 0x71553(%rip), %r15     # 0x814d0
cmpq %r13, %r12
je 0xffd1
movq (%r12), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x82c0
testq %rax, %rax
je 0xffb4
movq 0x10(%rax), %rax
movq 0x10(%rax), %rsi
movq %r15, %rdi
callq 0x1beee
testq %rax, %rax
jne 0xffcb
movq (%r12), %rbp
movq (%rbp), %rax
movq %rbp, %rdi
movq 0x60(%rsp), %rsi
movq 0x90(%rsp), %rdx
callq *0x10(%rax)
addq $0x8, %r12
jmp 0xff7d
leaq 0x46ae8(%rip), %rsi     # 0x56ac0
leaq 0x10(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0xa292
leaq 0x71572(%rip), %rdi     # 0x81560
leaq 0x10(%rsp), %rsi
leaq 0xa0(%rsp), %r14
leaq 0x70(%rsp), %r15
callq 0xe8da
movq 0x88(%rsp), %r12
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x8578
testq %rbx, %rbx
je 0x10051
cmpl $0x122, 0x28(%rbx)      # imm = 0x122
jne 0x10082
movl 0x8(%r12), %ebx
leaq 0x46aa4(%rip), %rsi     # 0x56ad8
leaq 0x10(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0xa292
leaq 0x10(%rsp), %rsi
movl %ebx, %edi
callq 0x1f699
jmp 0x10078
movl 0x8(%r12), %ebx
leaq 0x46a68(%rip), %rsi     # 0x56ac5
leaq 0x10(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0xa292
leaq 0x10(%rsp), %rsi
movl %ebx, %edi
callq 0x1f699
leaq 0x10(%rsp), %rdi
callq 0x8578
leaq 0xc0(%rsp), %rdi
callq 0x1b336
movq %r15, %rdi
callq 0x1b336
movq %r14, %rdi
callq 0x1b336
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x100bf
jmp 0x1018d
jmp 0x100bf
jmp 0x1018d
movq %rax, %r15
leaq 0x10(%rsp), %rdi
callq 0x8578
jmp 0x10190
jmp 0x1018d
movq %rax, %r15
leaq 0x10(%rsp), %rdi
callq 0x1b358
jmp 0x10190
movq %rax, %r15
leaq 0x10(%rsp), %rdi
callq 0x1a656
jmp 0x10190
jmp 0x1018d
jmp 0x10103
jmp 0x10103
movq %rax, %r15
leaq 0x10(%rsp), %rdi
callq 0x1b2b6
jmp 0x10190
jmp 0x1018d
jmp 0x10128
jmp 0x10137
jmp 0x1013c
jmp 0x10137
jmp 0x10128
jmp 0x10137
jmp 0x10128
jmp 0x10137
jmp 0x10128
jmp 0x10137
movq %rax, %r15
leaq 0x10(%rsp), %rdi
callq 0x1a656
jmp 0x1019d
movq %rax, %r15
jmp 0x1019d
movq %rax, %r15
leaq 0x10(%rsp), %rdi
callq 0x1b2b6
jmp 0x1019d
movq %rax, %r15
leaq 0x10(%rsp), %rdi
callq 0x1a656
jmp 0x101a7
jmp 0x1016b
movq %rax, %r15
leaq 0x10(%rsp), %rdi
callq 0x1b2b6
jmp 0x101a7
movq %rax, %r15
jmp 0x101a7
jmp 0x10174
jmp 0x10183
movq %rax, %r15
leaq 0x10(%rsp), %rdi
callq 0x1a656
jmp 0x10186
movq %rax, %r15
movq %rbx, %rdi
jmp 0x101af
jmp 0x1018d
movq %rax, %r15
leaq 0xc0(%rsp), %rdi
callq 0x1b336
leaq 0x70(%rsp), %rdi
callq 0x1b336
leaq 0xa0(%rsp), %rdi
callq 0x1b336
movq %r15, %rdi
callq 0x84d0