Removing unused junk, strengthen instruciton Var type.

This commit is contained in:
Zellyn Hunter 2014-08-19 17:43:23 -07:00
parent 54fe4809c1
commit 457446772a
5 changed files with 30 additions and 42 deletions

View File

@ -57,10 +57,6 @@ func (a *Assembler) Load(filename string, prefix int) error {
return err
}
// if err := in.FixLabels(a.Flavor); err != nil {
// return err
// }
if _, err := a.passInst(&in, false); err != nil {
return err
}
@ -145,7 +141,7 @@ func (a *Assembler) AssembleWithPrefix(filename string, prefix int) error {
}
// Final pass.
if _, err := a.Pass(true); err != nil {
if err := a.Pass2(); err != nil {
return err
}
return nil
@ -194,7 +190,6 @@ func (a *Assembler) initPass() {
func (a *Assembler) passInst(in *inst.I, final bool) (isFinal bool, err error) {
// fmt.Printf("PLUGH: in.Compute(a.Flavor, true, true) on %s\n", in)
isFinal, err = in.Compute(a.Flavor, final)
// fmt.Printf("PLUGH: isFinal=%v, in.Final=%v, in.WidthKnown=%v, in.Width=%v\n", isFinal, in.Final, in.WidthKnown, in.Width)
if err != nil {
return false, err
}
@ -207,27 +202,22 @@ func (a *Assembler) passInst(in *inst.I, final bool) (isFinal bool, err error) {
return isFinal, nil
}
// Pass performs an assembly pass. 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(final bool) (isFinal bool, err error) {
// fmt.Printf("PLUGH: Pass(%v): %d instructions\n", final, len(a.Insts))
// Pass2 performs the second assembly pass. It returns an error for
// any instruction that cannot be finalized.
func (a *Assembler) Pass2() error {
a.initPass()
isFinal = true
for _, in := range a.Insts {
instFinal, err := a.passInst(in, final)
instFinal, err := a.passInst(in, true)
if err != nil {
return false, err
return err
}
if final && !instFinal {
return false, in.Errorf("cannot finalize instruction: %s", in)
if !instFinal {
return in.Errorf("cannot finalize instruction: %s", in)
}
// fmt.Printf("PLUGH: instFinal=%v, in.Final=%v, in.WidthKnown=%v, in.Width=%v\n", instFinal, in.Final, in.WidthKnown, in.Width)
isFinal = isFinal && instFinal
}
return isFinal, nil
return nil
}
// RawBytes returns the raw bytes, sequentially in the order of the

View File

@ -73,13 +73,12 @@ func DecodeOp(c context.Context, in inst.I, summary opcodes.OpSummary, indirect
in.WidthKnown = true
in.Width = 2
in.Mode = opcodes.MODE_RELATIVE
in.Mode = inst.VarRelative
if valKnown && xyzzy {
in.Var = inst.VarRelative
if valKnown {
b, err := RelativeAddr(c, in, val)
if err != nil {
return in, err
}
fmt.Printf("b=$%02x\n", b)
in.Data = []byte{in.Op, b}
in.Final = true
}
@ -175,7 +174,6 @@ func RelativeAddr(c context.Context, in inst.I, val uint16) (byte, error) {
if !ok {
return 0, in.Errorf("cannot determine current address for '%s'", in.Command)
}
fmt.Printf("RelativeAddr: curr=%04x, val=%04x\n", curr, val)
// Found both current and target addresses
offset := int32(val) - (int32(curr) + 2)
if offset > 127 {

View File

@ -26,7 +26,7 @@ const fileChars = Letters + Digits + "."
type DirectiveInfo struct {
Type inst.Type
Func func(inst.I, *lines.Parse) (inst.I, error)
Var int
Var inst.Variant
}
type Requiredness int

View File

@ -306,13 +306,10 @@ func TestMultiline(t *testing.T) {
if err := tt.a.Load("TESTFILE", 0); err != nil {
t.Fatalf(`%d("%s" - %s): tt.a.Load("TESTFILE") failed: %s`, i, tt.name, tt.a.Flavor, err)
}
isFinal, err := tt.a.Pass(true)
err := tt.a.Pass2()
if err != nil {
t.Fatalf(`%d("%s" - %s): tt.a.Pass(true) failed: %s`, i, tt.name, tt.a.Flavor, err)
}
if !isFinal {
t.Fatalf(`%d("%s" - %s): tt.a.Pass(true) couldn't finalize`, i, tt.name, tt.a.Flavor)
}
if tt.b != "" {
bb, err := tt.a.RawBytes()

View File

@ -14,7 +14,7 @@ import (
type Type int
const (
TypeUnknown Type = iota
TypeUnknown Type = Type(iota)
TypeNone // No type (eg. just a label, just a comment, empty, ignored directive)
TypeMacroStart // Start of macro definition
@ -36,20 +36,23 @@ const (
TypeSetting // An on/off setting toggle
)
type Variant int
// Variants for instructions. These tell the instruction how to
// interpret the raw data that comes in on the first or second pass.
const (
VarBytes = iota // Data: expressions, but forced to one byte per
VarMixed // Bytes or words (LE), depending on individual expression widths
VarWordsLe // Data: expressions, but forced to one word per, little-endian
VarWordsBe // Data: expressions, but forced to one word per, big-endian
VarAscii // Data: from ASCII strings, high bit clear
VarAsciiFlip // Data: from ASCII strings, high bit clear, except last char
VarAsciiHi // Data: from ASCII strings, high bit set
VarAsciiHiFlip // Data: from ASCII strings, high bit set, except last char
VarRelative // For branches: a one-byte relative address
VarEquNormal // Equ: a normal equate
VarEquPageZero // Equ: a page-zero equate
VarUnknown = Variant(iota)
VarBytes // Data: expressions, but forced to one byte per
VarMixed // Bytes or words (LE), depending on individual expression widths
VarWordsLe // Data: expressions, but forced to one word per, little-endian
VarWordsBe // Data: expressions, but forced to one word per, big-endian
VarAscii // Data: from ASCII strings, high bit clear
VarAsciiFlip // Data: from ASCII strings, high bit clear, except last char
VarAsciiHi // Data: from ASCII strings, high bit set
VarAsciiHiFlip // Data: from ASCII strings, high bit set, except last char
VarRelative // For branches: a one-byte relative address
VarEquNormal // Equ: a normal equate
VarEquPageZero // Equ: a page-zero equate
)
type I struct {
@ -71,7 +74,7 @@ type I struct {
DeclaredLine uint16 // Line number listed in file
Line *lines.Line // Line object for this line
Addr uint16 // Current memory address
Var int // Variant of instruction type
Var Variant // Variant of instruction type
}
func (i I) TypeString() string {
@ -215,7 +218,7 @@ func (i *I) Compute(c context.Context, final bool) (bool, error) {
return true, nil
}
// computeLabel attempts to compute equates and label values.
// computeLabel attempts to compute label values.
func (i *I) computeLabel(c context.Context, final bool) error {
if i.Label == "" {
return nil