(codeStartOffset uint16, instructions []Instruction)
| 40 | } |
| 41 | |
| 42 | func (a *Assembler) Process(codeStartOffset uint16, instructions []Instruction) ([]uint16, error) { |
| 43 | a.labels = make(map[string]uint16) |
| 44 | a.symbols = make(map[string]uint16) |
| 45 | position := uint16(0) |
| 46 | |
| 47 | //calculate labels and symbols |
| 48 | for _, ins := range instructions { |
| 49 | position += uint16(ins.Size()) |
| 50 | |
| 51 | if label, ok := ins.(DEFLABEL); ok { |
| 52 | if _, ok := a.labels[label.Name]; ok { |
| 53 | return nil, fmt.Errorf("label '%s' already exists, all labels should be unique", label.Name) |
| 54 | } |
| 55 | |
| 56 | a.labels[label.Name] = position + codeStartOffset |
| 57 | } |
| 58 | |
| 59 | if symbol, ok := ins.(DEFSYMBOL); ok { |
| 60 | if _, ok := a.symbols[symbol.Name]; ok { |
| 61 | return nil, fmt.Errorf("symbol '%s' already exists, all symbols should be unique", symbol.Name) |
| 62 | } |
| 63 | |
| 64 | if isReservedSymbol(symbol.Name) { |
| 65 | return nil, fmt.Errorf("symbol '%s' is reserved for internal use, please use another symbol name", symbol.Name) |
| 66 | } |
| 67 | |
| 68 | a.symbols[symbol.Name] = symbol.Value |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | emitted := []uint16{} |
| 73 | |
| 74 | position = 0 |
| 75 | for index, ins := range instructions { |
| 76 | if _, ok := ins.(DEFLABEL); ok { |
| 77 | continue |
| 78 | } |
| 79 | if _, ok := ins.(DEFSYMBOL); ok { |
| 80 | continue |
| 81 | } |
| 82 | |
| 83 | a.symbols[CURRENTINSTRUCTION] = position + codeStartOffset |
| 84 | a.symbols[NEXTINSTRUCTION] = getNextExecutableInstructionLoc(a.symbols[CURRENTINSTRUCTION], index, instructions) |
| 85 | emit, err := ins.Emit(a.ResolveLabel, a.ResolveSymbol) |
| 86 | if err != nil { |
| 87 | return nil, err |
| 88 | } |
| 89 | |
| 90 | emitted = append(emitted, emit...) |
| 91 | position += uint16(ins.Size()) |
| 92 | } |
| 93 | |
| 94 | return emitted, nil |
| 95 | } |
| 96 | |
| 97 | func (a *Assembler) ToString(codeStartOffset uint16, instructions []Instruction) (string, error) { |
| 98 | a.labels = make(map[string]uint16) |
no test coverage detected