// File: /workspace/github_cpp_makefile_star2_O3/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 0x260c4
callq 0x5410
movl %eax, %r14d
leal 0x200(%r14), %eax
leaq 0xf515(%rip), %r15      # 0x35570
cmpl $0x3ff, %eax            # imm = 0x3FF
ja 0x261be
leaq 0x3798(%rip), %rsi      # 0x29805
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 0x3830(%rip), %rsi      # 0x298c7
movl $0x2, %edx
movq %rax, %rdi
callq 0x52a0
shll $0x2, %r14d
movq %rbx, %rdi
movl %r14d, %esi
callq 0x54a0
leaq 0x3827(%rip), %rsi      # 0x298e1
movl $0x5, %edx
jmp 0x2623c
leaq 0xf4a5(%rip), %r15      # 0x35570
leaq 0x3826(%rip), %rsi      # 0x298f8
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 0x3802(%rip), %rsi      # 0x298fe
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 0x386e(%rip), %r14      # 0x29991
movl $0x2, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x52a0
leaq 0x36cb(%rip), %rsi      # 0x29805
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 0x37a1(%rip), %rsi      # 0x29905
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 0x377e(%rip), %rsi      # 0x2990c
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 0x2623f
leaq 0x36fc(%rip), %rsi      # 0x298c1
movl $0x8, %edx
movq %r15, %rdi
callq 0x52a0
shll $0x2, %r14d
movq %r15, %rdi
movl %r14d, %esi
callq 0x54a0
leaq 0x282b(%rip), %rsi      # 0x28a13
movl $0x1, %edx
movq %rax, %rdi
callq 0x52a0
leaq 0x36eb(%rip), %rsi      # 0x298e7
movl $0x10, %edx
movq %r15, %rdi
callq 0x52a0
leaq 0x35f5(%rip), %rsi      # 0x29805
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 0x3754(%rip), %rsi      # 0x2998b
movl $0x8, %edx
movq %rax, %rdi
popq %rbx
popq %r14
popq %r15
jmp 0x52a0