// File: /workspace/github_cpp_makefile_star2_O3/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, 0x50(%rsp)
movq %rdi, %r13
movabsq $-0xfffffedf, %r15      # imm = 0xFFFFFFFF00000121
leaq 0x90(%rsp), %rbx
movq %r15, -0x8(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
movq $0x0, 0x10(%rbx)
leaq 0x5b818(%rip), %rsi     # 0x6e91c
movq %rsp, %rdi
leaq 0x88(%rsp), %rdx
callq 0x24e60
leaq 0x7c475(%rip), %rdi     # 0x8f590
movq %rsp, %rsi
callq 0x2617e
leaq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x25ee2
movq -0x28(%r14), %rdi
leaq 0x10(%rsp), %r12
cmpq %r12, %rdi
je 0x1314b
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x82a0
movq %rbx, %rdi
callq 0x25ee2
leaq 0x90(%rsp), %rbx
movq %r15, -0x8(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
movq $0x0, 0x10(%rbx)
leaq 0x5b7af(%rip), %rsi     # 0x6e923
movq %rsp, %rdi
leaq 0x88(%rsp), %rdx
callq 0x24efa
leaq 0x7c405(%rip), %rdi     # 0x8f590
movq %rsp, %rsi
callq 0x2617e
leaq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x25ee2
movq -0x28(%r14), %rdi
cmpq %r12, %rdi
je 0x131b6
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x82a0
movq %rbx, %rdi
callq 0x25ee2
leaq 0x90(%rsp), %rbx
movq %r15, -0x8(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
movq $0x0, 0x10(%rbx)
leaq 0x58(%rsp), %rsi
movl $0xffffffff, (%rsi)     # imm = 0xFFFFFFFF
movq %rsp, %rdi
leaq 0xa8(%rsp), %rcx
movl $0x1, %edx
callq 0x24f6c
movq %rsp, %rsi
movq %rbx, %rdi
callq 0x264fa
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x13219
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
leaq 0x5b709(%rip), %rsi     # 0x6e929
movq %rsp, %rdi
leaq 0x88(%rsp), %rdx
callq 0x24fb2
leaq 0x7c359(%rip), %rdi     # 0x8f590
movq %rsp, %rsi
callq 0x2617e
leaq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x25ee2
movq -0x28(%rbx), %rdi
cmpq %r12, %rdi
je 0x13262
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x82a0
movabsq $-0xfffffede, %r14      # imm = 0xFFFFFFFF00000122
leaq 0x60(%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 0x264fa
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x132aa
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
leaq 0x5b681(%rip), %rsi     # 0x6e932
movq %rsp, %rdi
leaq 0x58(%rsp), %rdx
callq 0x25024
leaq 0x7c2cb(%rip), %rdi     # 0x8f590
movq %rsp, %rsi
callq 0x2617e
leaq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x25ee2
movq -0x28(%rbx), %rdi
cmpq %r12, %rdi
je 0x132f0
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x82a0
leaq 0x5b642(%rip), %rsi     # 0x6e939
movq %rsp, %rdi
leaq 0x58(%rsp), %rdx
callq 0x25096
leaq 0x7c285(%rip), %rdi     # 0x8f590
movq %rsp, %rsi
callq 0x2617e
leaq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x25ee2
movq -0x28(%rbx), %rdi
cmpq %r12, %rdi
je 0x13336
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x82a0
leaq 0x5b60b(%rip), %rsi     # 0x6e948
movq %rsp, %rdi
leaq 0x58(%rsp), %rdx
callq 0x25108
leaq 0x7c23f(%rip), %rdi     # 0x8f590
movq %rsp, %rsi
callq 0x2617e
leaq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x25ee2
movq -0x28(%rbx), %rdi
cmpq %r12, %rdi
je 0x1337c
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x82a0
leaq 0x5b5d5(%rip), %rsi     # 0x6e958
movq %rsp, %rdi
leaq 0x58(%rsp), %rdx
callq 0x2517a
leaq 0x7c1f9(%rip), %rdi     # 0x8f590
movq %rsp, %rsi
callq 0x2617e
leaq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x25ee2
movq -0x28(%rbx), %rdi
cmpq %r12, %rdi
je 0x133c2
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x82a0
leaq 0xa8(%rsp), %rsi
movl $0xffffffff, (%rsi)     # imm = 0xFFFFFFFF
movq %rsp, %rdi
leaq 0x4c(%rsp), %rcx
movl $0x1, %edx
callq 0x24f6c
movq %rsp, %rsi
movq %r15, %rdi
callq 0x264fa
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x13403
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
leaq 0x5b535(%rip), %rsi     # 0x6e93f
movq %rsp, %rdi
leaq 0x58(%rsp), %rdx
callq 0x24fb2
leaq 0x7c172(%rip), %rdi     # 0x8f590
movq %rsp, %rsi
callq 0x2617e
leaq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x25ee2
movq -0x28(%rbx), %rdi
cmpq %r12, %rdi
je 0x13449
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x82a0
leaq 0xb0(%rsp), %rbp
movq %r14, -0x8(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, (%rbp)
movq $0x0, 0x10(%rbp)
leaq 0x4c(%rsp), %rsi
movl $0xffffffff, (%rsi)     # imm = 0xFFFFFFFF
movq %rsp, %rdi
leaq 0x4b(%rsp), %rcx
movl $0x1, %edx
callq 0x24f6c
movq %rsp, %rsi
movq %rbp, %rdi
callq 0x264fa
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x134a2
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
xorps %xmm0, %xmm0
movq %rsp, %rsi
movaps %xmm0, (%rsi)
movq $0x0, 0x10(%rsi)
movq %rbp, %rdi
callq 0x264fa
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x134d1
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
xorps %xmm0, %xmm0
movq %rsp, %rsi
movaps %xmm0, (%rsi)
movq $0x0, 0x10(%rsi)
movq %rbp, %rdi
callq 0x264fa
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x13500
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
leaq 0x5b460(%rip), %rsi     # 0x6e967
movq %rsp, %rdi
leaq 0xa8(%rsp), %rdx
callq 0x25024
leaq 0x7c072(%rip), %rdi     # 0x8f590
movq %rsp, %rsi
callq 0x2617e
leaq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x25ee2
movq -0x28(%rbx), %rdi
cmpq %r12, %rdi
je 0x13549
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 0x7bff7(%rip), %rdi     # 0x8f578
movq %rsp, %rsi
callq 0x26752
movq %rsp, %rdi
callq 0x25f36
movq 0x50(%rsp), %rax
movl $0x0, (%rax)
movb $0x0, 0x209(%rax)
movq 0x10(%r13), %r12
movq %r13, 0x78(%rsp)
movq 0x18(%r13), %r13
cmpq %r13, %r12
je 0x1362c
leaq 0x7a8d4(%rip), %rbx     # 0x8de90
leaq 0x7a695(%rip), %r14     # 0x8dc58
leaq 0x7bf36(%rip), %r15     # 0x8f500
movq (%r12), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x8320
testq %rax, %rax
je 0x13604
movq 0x10(%rax), %rax
movq 0x10(%rax), %rsi
movq %r15, %rdi
callq 0x269a2
testq %rax, %rax
leaq 0xb0(%rsp), %rbp
jne 0x13623
movq (%r12), %rbp
movq (%rbp), %rax
movq %rbp, %rdi
movq 0x50(%rsp), %rsi
movq 0x80(%rsp), %rdx
leaq 0xb0(%rsp), %rbp
callq *0x10(%rax)
addq $0x8, %r12
cmpq %r13, %r12
jne 0x135ca
movq %rsp, %rdi
leaq 0x10(%rsp), %r12
movq %r12, (%rdi)
leaq 0x5b522(%rip), %rsi     # 0x6eb60
leaq 0x5b51f(%rip), %rdx     # 0x6eb64
callq 0xc772
leaq 0x7bf3f(%rip), %rdi     # 0x8f590
movq %rsp, %rsi
leaq 0x90(%rsp), %r14
leaq 0x60(%rsp), %r15
callq 0x116e2
movq 0x78(%rsp), %r13
movq %rax, %rbx
movq (%rsp), %rdi
cmpq %r12, %rdi
je 0x13684
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x82a0
testq %rbx, %rbx
je 0x136bb
cmpl $0x122, 0x28(%rbx)      # imm = 0x122
jne 0x136f8
movl 0x8(%r13), %ebx
movq %rsp, %rdi
movq %r12, (%rdi)
leaq 0x5b4d5(%rip), %rsi     # 0x6eb78
leaq 0x5b4e8(%rip), %rdx     # 0x6eb92
callq 0xc772
movq %rsp, %rsi
movl %ebx, %edi
callq 0x2b6aa
jmp 0x136e2
movl 0x8(%r13), %ebx
movq %rsp, %rdi
movq %r12, (%rdi)
leaq 0x5b499(%rip), %rsi     # 0x6eb65
leaq 0x5b4a4(%rip), %rdx     # 0x6eb77
callq 0xc772
movq %rsp, %rsi
movl %ebx, %edi
callq 0x2b6aa
movq (%rsp), %rdi
cmpq %r12, %rdi
je 0x136f8
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x82a0
movq %rbp, %rdi
callq 0x25ee2
movq %r15, %rdi
callq 0x25ee2
movq %r14, %rdi
callq 0x25ee2
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x13730
jmp 0x1382a
jmp 0x13730
jmp 0x1382a
movq %rax, %r15
movq (%rsp), %rdi
cmpq %r12, %rdi
je 0x1382d
movq 0x10(%rsp), %rsi
incq %rsi
jmp 0x13790
jmp 0x1382a
movq %rax, %r15
movq %rsp, %rdi
callq 0x25f36
jmp 0x1382d
movq %rax, %r15
movq %rsp, %rdi
callq 0x24ed2
jmp 0x1382d
jmp 0x1382a
jmp 0x13778
jmp 0x13778
movq %rax, %r15
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x1382d
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
jmp 0x1382d
jmp 0x1382a
jmp 0x137b3
jmp 0x137c0
jmp 0x137c5
jmp 0x137c0
jmp 0x137b3
jmp 0x137c0
jmp 0x137b3
jmp 0x137c0
jmp 0x137b3
jmp 0x137c0
movq %rax, %r15
movq %rsp, %rdi
callq 0x24ed2
jmp 0x1383a
movq %rax, %r15
jmp 0x1383a
movq %rax, %r15
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x1383a
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
jmp 0x1383a
movq %rax, %r15
movq %rsp, %rdi
callq 0x24ed2
jmp 0x13844
jmp 0x1380a
movq %rax, %r15
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x13844
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
jmp 0x13844
movq %rax, %r15
jmp 0x13844
jmp 0x13813
jmp 0x13820
movq %rax, %r15
movq %rsp, %rdi
callq 0x24ed2
jmp 0x13823
movq %rax, %r15
movq %rbx, %rdi
jmp 0x1384c
jmp 0x1382a
movq %rax, %r15
leaq 0xb0(%rsp), %rdi
callq 0x25ee2
leaq 0x60(%rsp), %rdi
callq 0x25ee2
leaq 0x90(%rsp), %rdi
callq 0x25ee2
movq %r15, %rdi
callq 0x8540
nop