operand: doc for exported symbols (#9)

This commit is contained in:
Michael McLoughlin
2019-01-04 21:38:23 -08:00
parent 52a501c7be
commit 2e250a6f4c
2 changed files with 19 additions and 3 deletions

View File

@@ -2,6 +2,7 @@ package operand
import "fmt" import "fmt"
// Constant represents a constant literal.
type Constant interface { type Constant interface {
Op Op
Bytes() int Bytes() int
@@ -13,9 +14,13 @@ type Constant interface {
// String is a string constant. // String is a string constant.
type String string type String string
// Asm returns an assembly syntax representation of the string s.
func (s String) Asm() string { return fmt.Sprintf("$%q", s) } func (s String) Asm() string { return fmt.Sprintf("$%q", s) }
func (s String) Bytes() int { return len(s) }
func (s String) constant() {} // Bytes returns the length of s.
func (s String) Bytes() int { return len(s) }
func (s String) constant() {}
// Imm returns an unsigned integer constant with size guessed from x. // Imm returns an unsigned integer constant with size guessed from x.
func Imm(x uint64) Constant { func Imm(x uint64) Constant {

View File

@@ -6,15 +6,18 @@ import (
"github.com/mmcloughlin/avo/reg" "github.com/mmcloughlin/avo/reg"
) )
// Op is an operand.
type Op interface { type Op interface {
Asm() string Asm() string
} }
// Symbol represents a symbol name.
type Symbol struct { type Symbol struct {
Name string Name string
Static bool Static bool // only visible in current source file
} }
// NewStaticSymbol builds a static Symbol. Static symbols are only visible in the current source file.
func NewStaticSymbol(name string) Symbol { func NewStaticSymbol(name string) Symbol {
return Symbol{Name: name, Static: true} return Symbol{Name: name, Static: true}
} }
@@ -27,6 +30,7 @@ func (s Symbol) String() string {
return n return n
} }
// Mem represents a memory reference.
type Mem struct { type Mem struct {
Symbol Symbol Symbol Symbol
Disp int Disp int
@@ -48,6 +52,7 @@ func NewParamAddr(name string, offset int) Mem {
} }
} }
// NewStackAddr returns a memory reference relative to the stack pointer.
func NewStackAddr(offset int) Mem { func NewStackAddr(offset int) Mem {
return Mem{ return Mem{
Disp: offset, Disp: offset,
@@ -55,6 +60,7 @@ func NewStackAddr(offset int) Mem {
} }
} }
// NewDataAddr returns a memory reference relative to the named data symbol.
func NewDataAddr(sym Symbol, offset int) Mem { func NewDataAddr(sym Symbol, offset int) Mem {
return Mem{ return Mem{
Symbol: sym, Symbol: sym,
@@ -63,12 +69,14 @@ func NewDataAddr(sym Symbol, offset int) Mem {
} }
} }
// Offset returns a reference to m plus idx bytes.
func (m Mem) Offset(idx int) Mem { func (m Mem) Offset(idx int) Mem {
a := m a := m
a.Disp += idx a.Disp += idx
return a return a
} }
// Idx returns a new memory reference with (Index, Scale) set to (r, s).
func (m Mem) Idx(r reg.Register, s uint8) Mem { func (m Mem) Idx(r reg.Register, s uint8) Mem {
a := m a := m
a.Index = r a.Index = r
@@ -76,6 +84,7 @@ func (m Mem) Idx(r reg.Register, s uint8) Mem {
return a return a
} }
// Asm returns an assembly syntax representation of m.
func (m Mem) Asm() string { func (m Mem) Asm() string {
a := m.Symbol.String() a := m.Symbol.String()
if m.Disp != 0 { if m.Disp != 0 {
@@ -97,6 +106,7 @@ func (m Mem) Asm() string {
// Rel is an offset relative to the instruction pointer. // Rel is an offset relative to the instruction pointer.
type Rel int32 type Rel int32
// Asm returns an assembly syntax representation of r.
func (r Rel) Asm() string { func (r Rel) Asm() string {
return fmt.Sprintf(".%+d", r) return fmt.Sprintf(".%+d", r)
} }
@@ -104,6 +114,7 @@ func (r Rel) Asm() string {
// LabelRef is a reference to a label. // LabelRef is a reference to a label.
type LabelRef string type LabelRef string
// Asm returns an assembly syntax representation of l.
func (l LabelRef) Asm() string { func (l LabelRef) Asm() string {
return string(l) return string(l)
} }