1
0
mirror of https://github.com/zellyn/go6502.git synced 2024-10-05 15:56:13 +00:00
go6502/asm/asm.go

269 lines
7.4 KiB
Go
Raw Normal View History

2014-03-05 01:42:51 +00:00
package asm
import (
"fmt"
2014-05-20 15:23:20 +00:00
"path/filepath"
2014-03-05 01:42:51 +00:00
2014-05-08 00:44:03 +00:00
"github.com/zellyn/go6502/asm/flavors"
2014-03-05 01:42:51 +00:00
"github.com/zellyn/go6502/asm/inst"
"github.com/zellyn/go6502/asm/lines"
2014-05-08 00:44:03 +00:00
"github.com/zellyn/go6502/asm/macros"
2014-05-20 15:23:20 +00:00
"github.com/zellyn/go6502/asm/membuf"
2014-03-05 01:42:51 +00:00
)
type Assembler struct {
2014-05-08 00:44:03 +00:00
Flavor flavors.F
2014-03-05 01:42:51 +00:00
Opener lines.Opener
Insts []*inst.I
LastLabel string
2014-05-08 00:44:03 +00:00
Macros map[string]macros.M
2014-03-05 01:42:51 +00:00
}
2014-05-08 00:44:03 +00:00
func NewAssembler(flavor flavors.F, opener lines.Opener) *Assembler {
return &Assembler{
Flavor: flavor,
Opener: opener,
Macros: make(map[string]macros.M),
}
2014-03-05 01:42:51 +00:00
}
// Load loads a new assembler file, deleting any previous data.
func (a *Assembler) Load(filename string) error {
2014-05-08 00:44:03 +00:00
a.initPass()
2014-03-05 01:42:51 +00:00
context := lines.Context{Filename: filename}
ls, err := lines.NewFileLineSource(filename, context, a.Opener)
if err != nil {
return err
}
lineSources := []lines.LineSource{ls}
ifdefs := []bool{}
2014-05-08 00:44:03 +00:00
macroCall := 0
2014-03-05 01:42:51 +00:00
for len(lineSources) > 0 {
line, done, err := lineSources[0].Next()
if err != nil {
return err
}
if done {
lineSources = lineSources[1:]
continue
}
in, err := a.Flavor.ParseInstr(line)
if len(ifdefs) > 0 && !ifdefs[0] && in.Type != inst.TypeIfdefElse && in.Type != inst.TypeIfdefEnd {
// we're in an inactive ifdef branch
continue
}
2014-05-08 23:44:08 +00:00
if err := in.FixLabels(a.Flavor); err != nil {
2014-03-05 01:42:51 +00:00
return err
}
if _, err := a.passInst(&in, a.Flavor.SetWidthsOnFirstPass(), false); err != nil {
2014-03-05 01:42:51 +00:00
return err
}
switch in.Type {
case inst.TypeUnknown:
2014-05-16 00:11:05 +00:00
return line.Errorf("unknown instruction: %s", line.Parse.Text())
2014-03-05 01:42:51 +00:00
case inst.TypeMacroStart:
2014-05-08 00:44:03 +00:00
if err := a.readMacro(in, lineSources[0]); err != nil {
return err
}
continue // no need to append
return in.Errorf("macro start not (yet) implemented: %s", line)
2014-03-05 01:42:51 +00:00
case inst.TypeMacroCall:
2014-05-08 23:44:08 +00:00
macroCall++
2014-05-08 00:44:03 +00:00
m, ok := a.Macros[in.Command]
if !ok {
return in.Errorf(`unknown macro: "%s"`, in.Command)
}
2014-05-08 23:44:08 +00:00
subLs, err := m.LineSource(a.Flavor, in, macroCall)
2014-05-08 00:44:03 +00:00
if err != nil {
return in.Errorf(`error calling macro "%s": %v`, m.Name, err)
}
lineSources = append([]lines.LineSource{subLs}, lineSources...)
2014-03-05 01:42:51 +00:00
case inst.TypeIfdef:
if len(in.Exprs) == 0 {
panic(fmt.Sprintf("Ifdef got parsed with no expression: %s", line))
}
val, err := in.Exprs[0].Eval(a.Flavor, in.Line)
2014-03-05 01:42:51 +00:00
if err != nil {
return in.Errorf("cannot eval ifdef condition: %v", err)
2014-03-05 01:42:51 +00:00
}
ifdefs = append([]bool{val != 0}, ifdefs...)
case inst.TypeIfdefElse:
if len(ifdefs) == 0 {
return in.Errorf("ifdef else branch encountered outside ifdef: %s", line)
2014-03-05 01:42:51 +00:00
}
ifdefs[0] = !ifdefs[0]
case inst.TypeIfdefEnd:
if len(ifdefs) == 0 {
return in.Errorf("ifdef end encountered outside ifdef: %s", line)
2014-03-05 01:42:51 +00:00
}
ifdefs = ifdefs[1:]
case inst.TypeInclude:
2014-05-20 15:23:20 +00:00
filename = filepath.Join(filepath.Dir(in.Line.Context.Filename), in.TextArg)
subContext := lines.Context{Filename: filename, Parent: in.Line}
subLs, err := lines.NewFileLineSource(filename, subContext, a.Opener)
2014-03-05 01:42:51 +00:00
if err != nil {
return in.Errorf("error including file: %v", err)
2014-03-05 01:42:51 +00:00
}
lineSources = append([]lines.LineSource{subLs}, lineSources...)
case inst.TypeTarget:
return in.Errorf("target not (yet) implemented: %s", line)
2014-03-05 01:42:51 +00:00
case inst.TypeSegment:
return in.Errorf("segment not (yet) implemented: %s", line)
2014-03-05 01:42:51 +00:00
case inst.TypeEnd:
return nil
default:
}
a.Insts = append(a.Insts, &in)
}
return nil
}
2014-05-20 15:23:20 +00:00
func (a *Assembler) Assemble(filename string) error {
a.Reset()
if err := a.Load(filename); err != nil {
return err
}
// Setwidth pass if necessary.
if !a.Flavor.SetWidthsOnFirstPass() {
if _, err := a.Pass(true, false); err != nil {
return err
}
}
// Final pass.
if _, err := a.Pass(true, true); err != nil {
return err
}
return nil
}
2014-05-08 00:44:03 +00:00
func (a *Assembler) readMacro(in inst.I, ls lines.LineSource) error {
m := macros.M{
Name: in.TextArg,
Args: in.MacroArgs,
}
for {
line, done, err := ls.Next()
if err != nil {
return in.Errorf("error while reading macro %s: %v", m.Name)
}
if done {
return in.Errorf("end of file while reading macro %s", m.Name)
}
in2, err := a.Flavor.ParseInstr(line)
if err == nil && in2.Type == inst.TypeMacroEnd {
a.Macros[m.Name] = m
return nil
}
m.Lines = append(m.Lines, line.Parse.Text())
}
}
2014-03-05 01:42:51 +00:00
// Clear out stuff that may be hanging around from the previous pass, set origin to default, etc.
func (a *Assembler) initPass() {
a.Flavor.SetLastLabel("") // No last label (yet)
a.Flavor.RemoveChanged() // Remove any variables whose value ever changed.
if org, err := a.Flavor.DefaultOrigin(); err == nil {
a.Flavor.SetAddr(org)
} else {
a.Flavor.ClearAddr("beginning of assembly")
2014-03-05 01:42:51 +00:00
}
}
// passInst performs a pass on a single instruction. Depending on
// whether the instruction width can be determined, it updates or
// clears the current address. If setWidth is true, it forces the
// instruction to decide its final width. If final is true, and the
// instruction cannot be finalized, it returns an error.
func (a *Assembler) passInst(in *inst.I, setWidth, final bool) (isFinal bool, err error) {
// fmt.Printf("PLUGH: in.Compute(a.Flavor, true, true) on %s\n", in)
2014-03-05 01:42:51 +00:00
isFinal, err = in.Compute(a.Flavor, setWidth, final)
// fmt.Printf("PLUGH: isFinal=%v, in.Final=%v, in.WidthKnown=%v, in.MinWidth=%v\n", isFinal, in.Final, in.WidthKnown, in.MinWidth)
2014-03-05 01:42:51 +00:00
if err != nil {
return false, err
}
if in.WidthKnown && in.MinWidth != in.MaxWidth {
panic(fmt.Sprintf("inst.I %s: WidthKnown=true, but MinWidth=%d, MaxWidth=%d", in, in.MinWidth, in.MaxWidth))
}
2014-05-20 15:23:20 +00:00
// Update address
if a.Flavor.AddrKnown() {
2014-03-05 01:42:51 +00:00
addr, _ := a.Flavor.GetAddr()
2014-05-20 15:23:20 +00:00
in.Addr = addr
in.AddrKnown = true
if in.WidthKnown {
a.Flavor.SetAddr(addr + in.MinWidth)
} else {
a.Flavor.ClearAddr(in.Sprintf("lost known address"))
}
2014-03-05 01:42:51 +00:00
}
return isFinal, nil
}
// Pass performs an assembly pass. If setWidth is true, it causes all
// instructions to set their final width. If final is true, it returns
// an error for any instruction that cannot be finalized.
func (a *Assembler) Pass(setWidth, final bool) (isFinal bool, err error) {
// fmt.Printf("PLUGH: Pass(%v, %v): %d instructions\n", setWidth, final, len(a.Insts))
2014-03-05 01:42:51 +00:00
setWidth = setWidth || final // final ⊢ setWidth
a.initPass()
isFinal = true
for _, in := range a.Insts {
instFinal, err := a.passInst(in, setWidth, final)
if err != nil {
return false, err
}
if final && !instFinal {
return false, in.Errorf("cannot finalize instruction: %s", in)
2014-03-05 01:42:51 +00:00
}
// fmt.Printf("PLUGH: instFinal=%v, in.Final=%v, in.WidthKnown=%v, in.MinWidth=%v\n", instFinal, in.Final, in.WidthKnown, in.MinWidth)
2014-03-05 01:42:51 +00:00
isFinal = isFinal && instFinal
}
return isFinal, nil
}
2014-05-22 04:44:23 +00:00
// RawBytes returns the raw bytes, sequentially in the order of the
// lines of the file. Intended for testing, the return value gives no
// indication of address changes.
2014-03-05 01:42:51 +00:00
func (a *Assembler) RawBytes() ([]byte, error) {
result := []byte{}
for _, in := range a.Insts {
if !in.Final {
return []byte{}, in.Errorf("cannot finalize value: %s", in)
2014-03-05 01:42:51 +00:00
}
result = append(result, in.Data...)
}
return result, nil
}
func (a *Assembler) Reset() {
a.Insts = nil
a.LastLabel = ""
}
2014-05-20 15:23:20 +00:00
func (a *Assembler) Membuf() (*membuf.Membuf, error) {
m := &membuf.Membuf{}
for _, in := range a.Insts {
if !in.Final {
return nil, in.Errorf("cannot finalize value: %s", in)
}
if !in.AddrKnown {
return nil, in.Errorf("address unknown: %s", in)
}
if in.MinWidth > 0 {
m.Write(int(in.Addr), in.Data)
}
}
return m, nil
}