rvasm

package module
v0.0.0-...-4d95029 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2021 License: MIT Imports: 7 Imported by: 0

README

rvasm

A simple RISC-V assembler. Supports the RV32i standard plus CSR and other privileged instructions (uret, sret, mret, wfi). Also supports all pseudoinstructions associated with the supported base instructions.

This is a toy assembler. It may be useful for small RISC-V simulator projects.

Caveats:

  • Does not support the fence instruction.
  • Does not support data sections/symbol lookups for load/store instructions.
  • Not extensively tested (see testdata for tests).
  • li pseudo-instruction does not support full 32-bit immediates.

rvasm also supports full RISC-V RV32gc disassembly via the deadsy/rvda package. Use the -disas flag to disassemble.

Usage

rvasm will print the instructions in hex format when given an assembly program.

$ rvasm prog.s
00500113
00c00193
ff718393
0023e233
0041f2b3
004282b3
...

Use the -raw flag to dump the bytes directly instead of using the hex representation.

rvasm assumes the base address of the machine code to be 0, but you can change this with the -base flag.

rvasm will also assemble input passed via stdin if there are no input files. This can be quite useful for quickly generating machine code for individual instructions:

$ echo "li t0, 42" | rvasm
02a00293

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Csr = map[string]uint32{
	"ustatus":  0x000,
	"uie":      0x004,
	"utvec":    0x005,
	"uscratch": 0x040,
	"uepc":     0x041,
	"ucause":   0x042,
	"utval":    0x043,
	"uip":      0x044,

	"cycle":    0xC00,
	"time":     0xC01,
	"instret":  0xC02,
	"cycleh":   0xC80,
	"timeh":    0xC81,
	"instreth": 0xC82,
}
View Source
var Insns = map[string]Insn{
	"lui":   Insn{"lui", ULayout, UType},
	"auipc": Insn{"auipc", ULayout, UType},
	"jal":   Insn{"jal", JLayout, JType},
	"jalr":  Insn{"jalr", ILayout, IType},
	"beq":   Insn{"beq", BLayout, BType},
	"bne":   Insn{"bne", BLayout, BType},
	"blt":   Insn{"blt", BLayout, BType},
	"bge":   Insn{"bge", BLayout, BType},
	"bltu":  Insn{"bltu", BLayout, BType},
	"bgeu":  Insn{"bgeu", BLayout, BType},

	"lb":  Insn{"lb", ILayout, LType},
	"lh":  Insn{"lh", ILayout, LType},
	"lw":  Insn{"lw", ILayout, LType},
	"lbu": Insn{"lbu", ILayout, LType},
	"lhu": Insn{"lhu", ILayout, LType},

	"sb": Insn{"sb", SLayout, SType},
	"sh": Insn{"sh", SLayout, SType},
	"sw": Insn{"sw", SLayout, SType},

	"addi":  Insn{"addi", ILayout, IType},
	"slti":  Insn{"slti", ILayout, IType},
	"sltiu": Insn{"sltiu", ILayout, IType},
	"xori":  Insn{"xori", ILayout, IType},
	"ori":   Insn{"ori", ILayout, IType},
	"andi":  Insn{"andi", ILayout, IType},

	"slli": Insn{"slli", RLayout, IType},
	"srli": Insn{"srli", RLayout, IType},
	"srai": Insn{"srai", RLayout, IType},

	"add":  Insn{"add", RLayout, RType},
	"sub":  Insn{"sub", RLayout, RType},
	"sll":  Insn{"sll", RLayout, RType},
	"slt":  Insn{"slt", RLayout, RType},
	"sltu": Insn{"sltu", RLayout, RType},
	"xor":  Insn{"xor", RLayout, RType},
	"srl":  Insn{"srl", RLayout, RType},
	"sra":  Insn{"sra", RLayout, RType},
	"or":   Insn{"or", RLayout, RType},
	"and":  Insn{"and", RLayout, RType},

	"fence": Insn{"fence", FLayout, FType},

	"ecall":  Insn{"ecall", ILayout, EType},
	"ebreak": Insn{"ebreak", ILayout, EType},

	"csrrw":  Insn{"csrrw", ILayout, CType},
	"csrrs":  Insn{"csrrs", ILayout, CType},
	"csrrc":  Insn{"csrrc", ILayout, CType},
	"csrrwi": Insn{"csrrwi", ILayout, CType},
	"csrrsi": Insn{"csrrsi", ILayout, CType},
	"csrrci": Insn{"csrrci", ILayout, CType},

	"uret": Insn{"uret", RLayout, PType},
	"sret": Insn{"sret", RLayout, PType},
	"mret": Insn{"mret", RLayout, PType},
	"wfi":  Insn{"wfi", RLayout, PType},
}
View Source
var Reg = map[string]uint32{
	"x0":  0,
	"x1":  1,
	"x2":  2,
	"x3":  3,
	"x4":  4,
	"x5":  5,
	"x6":  6,
	"x7":  7,
	"x8":  8,
	"x9":  9,
	"x10": 10,
	"x11": 11,
	"x12": 12,
	"x13": 13,
	"x14": 14,
	"x15": 15,
	"x16": 16,
	"x17": 17,
	"x18": 18,
	"x19": 19,
	"x20": 20,
	"x21": 21,
	"x22": 22,
	"x23": 23,
	"x24": 24,
	"x25": 25,
	"x26": 26,
	"x27": 27,
	"x28": 28,
	"x29": 29,
	"x30": 30,
	"x31": 31,

	"zero": 0,
	"ra":   1,
	"sp":   2,
	"gp":   3,
	"tp":   4,
	"t0":   5,
	"t1":   6,
	"t2":   7,
	"s0":   8,
	"s1":   9,
	"a0":   10,
	"a1":   11,
	"a2":   12,
	"a3":   13,
	"a4":   14,
	"a5":   15,
	"a6":   16,
	"a7":   17,
	"s2":   18,
	"s3":   19,
	"s4":   20,
	"s5":   21,
	"s6":   22,
	"s7":   23,
	"s8":   24,
	"s9":   25,
	"s10":  26,
	"s11":  27,
	"t3":   28,
	"t4":   29,
	"t5":   30,
	"t6":   31,

	"fp": 8,
}

Functions

func IsPseudo

func IsPseudo(inst *Inst) bool

func Reg2Str

func Reg2Str(r uint32) string

Types

type Arg

type Arg struct {
	Ma *MemArg    `  @@`
	Na *NormalArg `| @@`
}

func (*Arg) String

func (a *Arg) String() string

type Bits

type Bits struct {
	// contains filtered or unexported fields
}

func CatBits

func CatBits(bs ...Bits) Bits

func GetBits

func GetBits(i uint32, top, bot int) Bits

func (Bits) Cat

func (b1 Bits) Cat(b2 Bits) Bits

func (Bits) Uint32

func (b Bits) Uint32() uint32

type Insn

type Insn struct {
	Name   string
	Layout InsnLayout
	Type   InsnType
}

func (Insn) Encode

func (i Insn) Encode(rd, rs1, rs2, imm uint32) uint32

func (Insn) Op

func (i Insn) Op() uint32

type InsnLayout

type InsnLayout int
const (
	RLayout InsnLayout = iota
	JLayout
	ULayout
	BLayout
	SLayout
	ILayout
	FLayout // fence instructions
)

type InsnType

type InsnType int
const (
	RType InsnType = iota
	JType
	IType
	SType
	LType
	EType
	FType
	CType
	BType
	UType
	PType
)

type Inst

type Inst struct {
	Name string `@Ident`
	Args []*Arg `( @@ ( "," @@ )* )? (Newline+|EOF)`
}

func (*Inst) String

func (i *Inst) String() string

type MemArg

type MemArg struct {
	Imm int    `@Number`
	Reg string `"(" @Ident ")"`
}

func (*MemArg) String

func (m *MemArg) String() string

type NormalArg

type NormalArg struct {
	Val string `@(Ident | Number)`
}

func (*NormalArg) Imm

func (n *NormalArg) Imm(locs map[string]uint32, rel uint32) uint32

func (*NormalArg) IsReg

func (n *NormalArg) IsReg() bool

func (*NormalArg) Reg

func (n *NormalArg) Reg() uint32

func (*NormalArg) String

func (n *NormalArg) String() string

type Operation

type Operation struct {
	Label string `  @Ident ":" Newline*`
	Inst  *Inst  `| @@`
}

func FromPseudo

func FromPseudo(inst *Inst, labels map[string]uint32, base uint32) []*Operation

func (*Operation) String

func (o *Operation) String() string

type Program

type Program []uint32

func Assemble

func Assemble(fname string, r io.Reader, base uint32) (Program, error)

func AssembleAST

func AssembleAST(ast *RV32i, base uint32) (Program, error)

func (Program) EncodeToBin

func (p Program) EncodeToBin() []byte

func (Program) EncodeToHex

func (p Program) EncodeToHex() []string

type RV32i

type RV32i struct {
	Pos lexer.Position

	Ops []*Operation `@@*`
}

func Parse

func Parse(name string, r io.Reader) (*RV32i, error)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL