汇编器增加一个hi52宏用于取更高的20bit

This commit is contained in:
chai2010
2025-12-05 07:46:52 +08:00
parent c84cc2437a
commit 9c03ddf840
6 changed files with 17 additions and 4 deletions

View File

@@ -57,11 +57,13 @@ type BuiltinFn int16
const (
BuiltinFn_HI = iota + 1 // %hi(symbol) # 绝对地址 HI20, 指令 lui
BuiltinFn_LO // %lo(symbol) # 绝对地址 LO12, 指令 load/store/add
BuiltinFn_HI52 // %hi52(symbol) # 绝对地址 32-51 bit 部分, 总地址宽度 52bit
BuiltinFn_PCREL_HI // %pcrel_hi(symbol) # PC相对地址 HI20, auipc
BuiltinFn_PCREL_LO // %pcrel_lo(label) # label 对应的指令中, 计算出的PC相对地址的 LO12 部分, 参数必须是 label
BuiltinFn_HI_zh
BuiltinFn_LO_zh
BuiltinFn_HI52_zh
BuiltinFn_PCREL_HI_zh
BuiltinFn_PCREL_LO_zh
)

View File

@@ -23,6 +23,8 @@ func (fn BuiltinFn) String() string {
return "%hi"
case BuiltinFn_LO:
return "%lo"
case BuiltinFn_HI52:
return "%hi52"
case BuiltinFn_PCREL_HI:
return "%pcrel_hi"
case BuiltinFn_PCREL_LO:
@@ -32,6 +34,8 @@ func (fn BuiltinFn) String() string {
return "%高位"
case BuiltinFn_LO_zh:
return "%低位"
case BuiltinFn_HI52_zh:
return "%高位五二"
case BuiltinFn_PCREL_HI_zh:
return "%相对高位"
case BuiltinFn_PCREL_LO_zh:

View File

@@ -259,6 +259,11 @@ func (p *_Assembler) asmFunc(fn *ast.Func) (err error) {
} else {
inst.Arg.Imm = x >> 12
}
case abi.BuiltinFn_HI52, abi.BuiltinFn_HI52_zh: // 高20bit
// 绝对地址不需要负数, 因为 LO 部分也不需要相对地址可能产生的负数
x := (uint64(addr) >> 32) & 0xFFFFF
inst.Arg.Imm = int32(x)
case abi.BuiltinFn_LO, abi.BuiltinFn_LO_zh: // 低12bit
x := int32(addr)
// 简单地取低 12 位

View File

@@ -13,7 +13,7 @@ func _start {
%begin:
# a0 = 字符串地址
lu12i.w a0, %hi($message) # 高20位
addi.w a0, a0, %lo($message) # 低12位
ori a0, a0, %lo($message) # 低12位(不能使用addi.w)
%print_loop:
ld.bu a1, 0(a0) # 取一个字节
@@ -21,7 +21,7 @@ func _start {
# t0 = UART0 地址
lu12i.w t0, %hi($UART0) # UART0 高20位
addi.w t0, t0, %lo($UART0) # UART0 低12位
ori t0, t0, %lo($UART0) # UART0 低12位
st.b a1, 0(t0) # 写到UART寄存器
addi.w a0, a0, 1 # 下一个字符

View File

@@ -263,13 +263,13 @@ func (p *parser) parseInst_loong(fn *ast.Func) (inst *ast.Instruction) {
inst.Arg.Symbol = sa3Symbol
return inst
case loong64.OpFormatType_code:
code, codeSymbol := p.parseInst_loong_imm_code_5bit()
code, codeSymbol := p.parseInst_loong_imm_code_15bit()
inst.Arg.Imm = code
inst.Arg.Symbol = codeSymbol
return inst
case loong64.OpFormatType_code_1R_si12:
code, codeSymbol := p.parseInst_loong_imm_code_15bit()
code, codeSymbol := p.parseInst_loong_imm_code_5bit()
p.acceptToken(token.COMMA)
rj := p.parseRegister()
p.checkRegI_loong(rj)

View File

@@ -86,6 +86,8 @@ func (p *CPU) execInst(bus *device.Bus, as abi.As, arg *abi.AsRawArgument) error
p.RegX[arg.Rd] = LAUInt(arg.Imm << 12)
case loong64.AADDI_W:
p.RegX[arg.Rd] = p.RegX[arg.Rs1] + LAUInt(arg.Imm)
case loong64.AORI:
p.RegX[arg.Rd] = p.RegX[arg.Rs1] | LAUInt(arg.Imm)
case loong64.ALD_BU:
addr := p.RegX[arg.Rs1] + LAUInt(arg.Imm)
value, err := bus.Read(uint64(addr), 1)