feat(riscv): add prologue, epilogue and frame pseudo-register support

Assisted-by: Kimi K3
This commit is contained in:
2026-08-02 00:18:00 +02:00
parent f860bf8ce6
commit a2acc9b5a3
5 changed files with 253 additions and 53 deletions
+77 -50
View File
@@ -12,10 +12,14 @@ import (
// assembleRISCV assembles a RISC-V TEXT function body into machine code.
// It handles the core RV64I/RV64M instruction set with fixed 32-bit encoding.
func assembleRISCV(t *ast.Text) ([]byte, map[string]int, error) {
fi := riscvComputeFrame(t)
prologue := riscvPrologue(fi)
// First pass: compute label offsets (all RISC-V instructions are 4 bytes).
// Labels are offset by the prologue length.
offsets := map[string]int{}
var instrs []*ast.Instr
pos := 0
pos := len(prologue)
for _, stmt := range t.Body {
switch s := stmt.(type) {
case *ast.Label:
@@ -27,9 +31,9 @@ func assembleRISCV(t *ast.Text) ([]byte, map[string]int, error) {
}
// Second pass: encode instructions.
out := make([]byte, 0, pos)
out := append([]byte(nil), prologue...)
for _, instr := range instrs {
code, err := encodeRISCVInstr(instr, pos, offsets)
code, err := encodeRISCVInstr(instr, pos, offsets, fi)
if err != nil {
return nil, nil, fmt.Errorf("%s: %w", instr.Mnemonic.Text, err)
}
@@ -40,50 +44,91 @@ func assembleRISCV(t *ast.Text) ([]byte, map[string]int, error) {
}
// encodeRISCVInstr encodes a single RISC-V instruction.
func encodeRISCVInstr(instr *ast.Instr, pc int, offsets map[string]int) ([]byte, error) {
func encodeRISCVInstr(instr *ast.Instr, pc int, offsets map[string]int, fi riscvFrameInfo) ([]byte, error) {
mnem := instr.Mnemonic.Text
ops := instr.Operands
var word uint32
// Handle pseudo-instructions and special cases first.
switch mnem {
case "RET":
// RET = JALR X0, 0(X1)
word = riscvIType(riscvEnc{0x67, 0x0, 0x00}, 0, 1, 0)
return []byte{byte(word), byte(word >> 8), byte(word >> 16), byte(word >> 24)}, nil
case "CALL":
// CALL is a pseudo-instruction; encode as NOP placeholder.
word = riscvIType(riscvEnc{0x13, 0x0, 0x00}, 0, 0, 0)
return []byte{byte(word), byte(word >> 8), byte(word >> 16), byte(word >> 24)}, nil
case "JMP":
// JMP = JAL X0, target
var target string
if len(ops) >= 1 {
target = labelFromOperand(ops[0])
}
targetOff, ok := offsets[target]
if !ok {
return nil, fmt.Errorf("undefined label %q", target)
}
offset := int32(targetOff - pc)
word = riscvJType(0, offset)
return []byte{byte(word), byte(word >> 8), byte(word >> 16), byte(word >> 24)}, nil
case "JAL":
rd := 0
var target string
if len(ops) >= 2 {
rd = regFromOperand(ops[0])
target = labelFromOperand(ops[1])
} else if len(ops) == 1 {
target = labelFromOperand(ops[0])
}
targetOff, ok := offsets[target]
if !ok {
return nil, fmt.Errorf("undefined label %q", target)
}
offset := int32(targetOff - pc)
word = riscvJType(rd, offset)
return []byte{byte(word), byte(word >> 8), byte(word >> 16), byte(word >> 24)}, nil
}
enc, ok := riscvInstrTable[mnem]
if !ok {
return nil, fmt.Errorf("unsupported RISC-V instruction %q", mnem)
}
ops := instr.Operands
var word uint32
switch {
// R-type: rd, rs1, rs2 (3 register operands).
// R-type: Plan 9 order is INSTR src1, src2, dst (destination last).
case len(ops) == 3 && isRTypeInstr(mnem):
rd := regFromOperand(ops[0])
rs1 := regFromOperand(ops[1])
rs2 := regFromOperand(ops[2])
rs1 := regFromOperand(ops[0]) // source 1 (first operand)
rs2 := regFromOperand(ops[1]) // source 2 (second operand)
rd := regFromOperand(ops[2]) // destination (last operand)
if rd < 0 || rs1 < 0 || rs2 < 0 {
return nil, fmt.Errorf("invalid register in %s", mnem)
}
word = riscvRType(enc, rd, rs1, rs2)
// I-type with immediate: rd, rs1, imm.
// I-type with immediate: Plan 9 order is INSTR src, imm, dst.
case len(ops) == 3 && isITypeInstr(mnem):
rd := regFromOperand(ops[0])
rs1 := regFromOperand(ops[1])
imm := immFromOperand(ops[2])
rs1 := regFromOperand(ops[0]) // source register
imm := immFromOperand(ops[1]) // immediate
rd := regFromOperand(ops[2]) // destination
if rd < 0 || rs1 < 0 {
return nil, fmt.Errorf("invalid register in %s", mnem)
}
word = riscvIType(enc, rd, rs1, imm)
// Loads: rd, offset(rs1) — parsed as rd, (rs1)+offset.
// Loads: rd, offset(rs1) — Plan 9 order is LD src, dst.
case len(ops) == 2 && isLoadInstr(mnem):
rd := regFromOperand(ops[0])
rs1, imm := memFromOperand(ops[1])
rd := regFromOperand(ops[1]) // destination (last operand)
rs1, imm := memFromOperandWithFrame(ops[0], fi) // memory source (first operand)
if rd < 0 || rs1 < 0 {
return nil, fmt.Errorf("invalid operand in %s", mnem)
}
word = riscvIType(enc, rd, rs1, imm)
// Stores: rs2, offset(rs1).
// Stores: Plan 9 order is SD src, dst (src=register, dst=memory).
case len(ops) == 2 && isStoreInstr(mnem):
rs2 := regFromOperand(ops[0])
rs1, imm := memFromOperand(ops[1])
rs2 := regFromOperand(ops[0]) // source register (first operand)
rs1, imm := memFromOperandWithFrame(ops[1], fi) // memory dest (last operand)
if rs2 < 0 || rs1 < 0 {
return nil, fmt.Errorf("invalid operand in %s", mnem)
}
@@ -115,35 +160,6 @@ func encodeRISCVInstr(instr *ast.Instr, pc int, offsets map[string]int) ([]byte,
}
word = riscvUType(enc, rd, imm)
// JAL: rd, label (or just label for JMP).
case mnem == "JAL" || mnem == "JMP":
rd := 0 // JMP uses rd=0 (X0)
if len(ops) >= 1 && mnem == "JAL" {
rd = regFromOperand(ops[0])
}
var target string
if len(ops) >= 2 {
target = labelFromOperand(ops[len(ops)-1])
} else if len(ops) == 1 && mnem == "JMP" {
target = labelFromOperand(ops[0])
}
targetOff, ok := offsets[target]
if !ok {
return nil, fmt.Errorf("undefined label %q", target)
}
offset := int32(targetOff - pc)
word = riscvJType(rd, offset)
// RET: JALR X0, 0(X1).
case mnem == "RET":
word = riscvIType(riscvEnc{0x67, 0x0, 0x00}, 0, 1, 0)
// CALL: AUIPC + JALR sequence (simplified: just encode as NOP placeholder).
case mnem == "CALL":
// CALL is a pseudo-instruction that expands to AUIPC + JALR.
// For now, encode as a NOP (ADDI X0, X0, 0).
word = riscvIType(riscvEnc{0x13, 0x0, 0x00}, 0, 0, 0)
default:
return nil, fmt.Errorf("cannot encode %s with %d operands", mnem, len(ops))
}
@@ -229,6 +245,17 @@ func memFromOperand(op *ast.Operand) (rs1 int, imm int32) {
return
}
// memFromOperandWithFrame resolves a memory operand, handling FP/SP
// pseudo-registers via the frame mapping.
func memFromOperandWithFrame(op *ast.Operand, fi riscvFrameInfo) (rs1 int, imm int32) {
// Check for a pseudo-register reference (name+offset(FP) or name+offset(SP)).
if op.Addr.Sym != nil && op.Addr.Sym.Pseudo != "" {
return riscvResolvePseudo(op.Addr.Sym, fi)
}
// Plain register+offset memory reference.
return memFromOperand(op)
}
func labelFromOperand(op *ast.Operand) string {
if op.Addr.Sym != nil {
return op.Addr.Sym.Name