// File: /workspace/github_cpp_makefile_star2_O1/lhydave[P]SysYCompilerLab/back/riscv/emit_riscv.cpp
// Function: riscv::emit_load(std::shared_ptr<tigger::load_stmt> const&)
void emit_load(const shared_ptr<tigger::load_stmt> &stmt)
{
	if (isdigit(stmt->addr[0])) // stack
	{
		auto num = atoi(stmt->addr.c_str());
		if (in_int10(num))
			riscv_dst << "\tlw\t" << stmt->reg << ", " << num * 4 << "(sp)\n";
		else
		{
			riscv_dst << "\tli\tt0, " << num * 4 << "\n";
			riscv_dst << "\tadd\tt0, t0, sp\n";
			riscv_dst << "\tlw\t" << stmt->reg << ", 0(t0)\n";
		}
	}
	else // global_var
	{
		riscv_dst << "\tlui\t" << stmt->reg << ", %hi(" << stmt->addr << ")\n";
		riscv_dst << "\tlw\t" << stmt->reg << ", %lo(" << stmt->addr << ")("
				  << stmt->reg << ")\n";
	}
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
movq %rdi, %rbx
movq (%rdi), %rax
movq 0x8(%rax), %rdi
movsbl (%rdi), %eax
addl $-0x30, %eax
cmpl $0x9, %eax
ja 0x26156
callq 0x5410
movl %eax, %r14d
leal 0x200(%r14), %eax
leaq 0xf483(%rip), %r15      # 0x35570
cmpl $0x3ff, %eax            # imm = 0x3FF
ja 0x26250
leaq 0x36ae(%rip), %rsi      # 0x297ad
movl $0x4, %edx
movq %r15, %rdi
callq 0x52a0
movq (%rbx), %rax
movq 0x28(%rax), %rsi
movq 0x30(%rax), %rdx
movq %r15, %rdi
callq 0x52a0
movq %rax, %rbx
leaq 0x3746(%rip), %rsi      # 0x2986f
movl $0x2, %edx
movq %rax, %rdi
callq 0x52a0
shll $0x2, %r14d
movq %rbx, %rdi
movl %r14d, %esi
callq 0x54a0
leaq 0x373d(%rip), %rsi      # 0x29889
movl $0x5, %edx
jmp 0x262ce
leaq 0xf413(%rip), %r15      # 0x35570
leaq 0x373c(%rip), %rsi      # 0x298a0
movl $0x5, %edx
movq %r15, %rdi
callq 0x52a0
movq (%rbx), %rax
movq 0x28(%rax), %rsi
movq 0x30(%rax), %rdx
movq %r15, %rdi
callq 0x52a0
movq %rax, %r14
leaq 0x3718(%rip), %rsi      # 0x298a6
movl $0x6, %edx
movq %rax, %rdi
callq 0x52a0
movq (%rbx), %rax
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
movq %r14, %rdi
callq 0x52a0
leaq 0x3784(%rip), %r14      # 0x29939
movl $0x2, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x52a0
leaq 0x35e1(%rip), %rsi      # 0x297ad
movl $0x4, %edx
movq %r15, %rdi
callq 0x52a0
movq (%rbx), %rax
movq 0x28(%rax), %rsi
movq 0x30(%rax), %rdx
movq %r15, %rdi
callq 0x52a0
movq %rax, %r15
leaq 0x36b7(%rip), %rsi      # 0x298ad
movl $0x6, %edx
movq %rax, %rdi
callq 0x52a0
movq (%rbx), %rax
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
movq %r15, %rdi
callq 0x52a0
movq %rax, %r15
leaq 0x3694(%rip), %rsi      # 0x298b4
movl $0x2, %edx
movq %rax, %rdi
callq 0x52a0
movq (%rbx), %rax
movq 0x28(%rax), %rsi
movq 0x30(%rax), %rdx
movq %r15, %rdi
callq 0x52a0
movl $0x2, %edx
movq %rax, %rdi
movq %r14, %rsi
jmp 0x262d1
leaq 0x3612(%rip), %rsi      # 0x29869
movl $0x8, %edx
movq %r15, %rdi
callq 0x52a0
shll $0x2, %r14d
movq %r15, %rdi
movl %r14d, %esi
callq 0x54a0
leaq 0x2762(%rip), %rsi      # 0x289dc
movl $0x1, %edx
movq %rax, %rdi
callq 0x52a0
leaq 0x3601(%rip), %rsi      # 0x2988f
movl $0x10, %edx
movq %r15, %rdi
callq 0x52a0
leaq 0x350b(%rip), %rsi      # 0x297ad
movl $0x4, %edx
movq %r15, %rdi
callq 0x52a0
movq (%rbx), %rax
movq 0x28(%rax), %rsi
movq 0x30(%rax), %rdx
movq %r15, %rdi
callq 0x52a0
leaq 0x366a(%rip), %rsi      # 0x29933
movl $0x8, %edx
movq %rax, %rdi
popq %rbx
popq %r14
popq %r15
jmp 0x52a0