// File: 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 %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
callq 0x49100
movq %rax, %rdi
addq $0x8, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x56a0
movsbl (%rax), %edi
callq 0x52c0
cmpl $0x0, %eax
je 0x47b3c
movq -0x8(%rbp), %rdi
callq 0x49100
movq %rax, %rdi
addq $0x8, %rdi
callq 0x5160
movq %rax, %rdi
callq 0x5540
movl %eax, -0xc(%rbp)
movl -0xc(%rbp), %edi
callq 0x490b0
testb $0x1, %al
jne 0x47a54
jmp 0x47ab5
leaq 0x2abd5(%rip), %rdi     # 0x72630
leaq 0x70d2(%rip), %rsi      # 0x4eb34
callq 0x52f0
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
callq 0x49100
movq -0x18(%rbp), %rdi
movq %rax, %rsi
addq $0x28, %rsi
callq 0x52b0
movq %rax, %rdi
leaq 0x7168(%rip), %rsi      # 0x4ebf6
callq 0x52f0
movq %rax, %rdi
movl -0xc(%rbp), %esi
shll $0x2, %esi
callq 0x55f0
movq %rax, %rdi
leaq 0x7165(%rip), %rsi      # 0x4ec10
callq 0x52f0
jmp 0x47b37
leaq 0x2ab74(%rip), %rdi     # 0x72630
leaq 0x712d(%rip), %rsi      # 0x4ebf0
callq 0x52f0
movq %rax, %rdi
movl -0xc(%rbp), %esi
shll $0x2, %esi
callq 0x55f0
movq %rax, %rdi
leaq 0x6160(%rip), %rsi      # 0x4dc40
callq 0x52f0
leaq 0x2ab44(%rip), %rdi     # 0x72630
leaq 0x7123(%rip), %rsi      # 0x4ec16
callq 0x52f0
leaq 0x2ab31(%rip), %rdi     # 0x72630
leaq 0x702e(%rip), %rsi      # 0x4eb34
callq 0x52f0
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
callq 0x49100
movq -0x20(%rbp), %rdi
movq %rax, %rsi
addq $0x28, %rsi
callq 0x52b0
movq %rax, %rdi
leaq 0x7188(%rip), %rsi      # 0x4ecba
callq 0x52f0
jmp 0x47c3e
leaq 0x2aaed(%rip), %rdi     # 0x72630
leaq 0x70dd(%rip), %rsi      # 0x4ec27
callq 0x52f0
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rdi
callq 0x49100
movq -0x48(%rbp), %rdi
movq %rax, %rsi
addq $0x28, %rsi
callq 0x52b0
movq %rax, %rdi
leaq 0x70b7(%rip), %rsi      # 0x4ec2d
callq 0x52f0
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
callq 0x49100
movq -0x40(%rbp), %rdi
movq %rax, %rsi
addq $0x8, %rsi
callq 0x52b0
movq %rax, %rdi
leaq 0x711e(%rip), %rsi      # 0x4ecc0
callq 0x52f0
leaq 0x2aa82(%rip), %rdi     # 0x72630
leaq 0x6f7f(%rip), %rsi      # 0x4eb34
callq 0x52f0
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x49100
movq -0x38(%rbp), %rdi
movq %rax, %rsi
addq $0x28, %rsi
callq 0x52b0
movq %rax, %rdi
leaq 0x7053(%rip), %rsi      # 0x4ec34
callq 0x52f0
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
callq 0x49100
movq -0x30(%rbp), %rdi
movq %rax, %rsi
addq $0x8, %rsi
callq 0x52b0
movq %rax, %rdi
leaq 0x702e(%rip), %rsi      # 0x4ec3b
callq 0x52f0
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
callq 0x49100
movq -0x28(%rbp), %rdi
movq %rax, %rsi
addq $0x28, %rsi
callq 0x52b0
movq %rax, %rdi
leaq 0x7087(%rip), %rsi      # 0x4ecc0
callq 0x52f0
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)