Repository navigation
Expand file tree
/
Copy pathprogram_builder.go
More file actions
178 lines (152 loc) · 4.7 KB
/
Copy pathprogram_builder.go
File metadata and controls
178 lines (152 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package m68kasm
import (
"fmt"
"sort"
"strings"
)
// ProgramBuilder is a lightweight helper for test-oriented source generation.
type ProgramBuilder struct {
lines []string
}
// NewProgramBuilder creates a new assembly source builder.
func NewProgramBuilder() *ProgramBuilder {
return &ProgramBuilder{}
}
// Line appends a raw source line.
func (b *ProgramBuilder) Line(line string) *ProgramBuilder {
b.lines = append(b.lines, line)
return b
}
// Instruction appends an instruction line.
func (b *ProgramBuilder) Instruction(line string) *ProgramBuilder {
return b.Line(line)
}
// Label appends a global label definition.
func (b *ProgramBuilder) Label(name string) *ProgramBuilder {
return b.Line(name + ":")
}
// Origin appends an .org directive.
func (b *ProgramBuilder) Origin(addr uint32) *ProgramBuilder {
return b.Line(".org " + formatUint32Hex(addr, 0))
}
// Text switches subsequent output to the text section.
func (b *ProgramBuilder) Text() *ProgramBuilder {
return b.Line(".text")
}
// Data switches subsequent output to the data section.
func (b *ProgramBuilder) Data() *ProgramBuilder {
return b.Line(".data")
}
// BSS switches subsequent output to the bss section.
func (b *ProgramBuilder) BSS() *ProgramBuilder {
return b.Line(".bss")
}
// Section appends a .section directive.
func (b *ProgramBuilder) Section(name string) *ProgramBuilder {
if strings.HasPrefix(name, ".") {
return b.Line(".section " + name)
}
return b.Line(`.section "` + name + `"`)
}
// Byte appends a .byte directive with numeric values.
func (b *ProgramBuilder) Byte(values ...uint8) *ProgramBuilder {
exprs := make([]string, len(values))
for i, value := range values {
exprs[i] = formatUint64Hex(uint64(value), 2)
}
return b.ByteExpr(exprs...)
}
// ByteExpr appends a .byte directive with arbitrary expressions.
func (b *ProgramBuilder) ByteExpr(exprs ...string) *ProgramBuilder {
return b.directive(".byte", exprs...)
}
// Word appends a .word directive with numeric values.
func (b *ProgramBuilder) Word(values ...uint16) *ProgramBuilder {
exprs := make([]string, len(values))
for i, value := range values {
exprs[i] = formatUint64Hex(uint64(value), 4)
}
return b.WordExpr(exprs...)
}
// WordExpr appends a .word directive with arbitrary expressions.
func (b *ProgramBuilder) WordExpr(exprs ...string) *ProgramBuilder {
return b.directive(".word", exprs...)
}
// Long appends a .long directive with numeric values.
func (b *ProgramBuilder) Long(values ...uint32) *ProgramBuilder {
exprs := make([]string, len(values))
for i, value := range values {
exprs[i] = formatUint32Hex(value, 8)
}
return b.LongExpr(exprs...)
}
// LongExpr appends a .long directive with arbitrary expressions.
func (b *ProgramBuilder) LongExpr(exprs ...string) *ProgramBuilder {
return b.directive(".long", exprs...)
}
// Align appends an .align directive with optional fill byte.
func (b *ProgramBuilder) Align(boundary uint32, fill ...byte) *ProgramBuilder {
line := ".align " + fmt.Sprintf("%d", boundary)
if len(fill) > 0 {
line += "," + formatUint64Hex(uint64(fill[0]), 2)
}
return b.Line(line)
}
// Even appends an .even directive.
func (b *ProgramBuilder) Even() *ProgramBuilder {
return b.Line(".even")
}
// VectorTable emits a zero-filled vector table up to the highest supplied vector index.
func (b *ProgramBuilder) VectorTable(entries map[uint32]string) *ProgramBuilder {
if len(entries) == 0 {
return b
}
keys := make([]int, 0, len(entries))
for idx := range entries {
keys = append(keys, int(idx))
}
sort.Ints(keys)
values := make([]string, keys[len(keys)-1]+1)
for i := range values {
values[i] = "0"
}
for _, idx := range keys {
values[idx] = strings.TrimSpace(entries[uint32(idx)])
if values[idx] == "" {
values[idx] = "0"
}
}
return b.LongExpr(values...)
}
// String returns the assembled source text.
func (b *ProgramBuilder) String() string {
if len(b.lines) == 0 {
return ""
}
return strings.Join(b.lines, "\n") + "\n"
}
// Assemble assembles the generated source and returns detailed metadata.
func (b *ProgramBuilder) Assemble() (*AssemblyResult, error) {
return AssembleStringDetailed(b.String())
}
// AssembleWithOptions assembles the generated source with parser options.
func (b *ProgramBuilder) AssembleWithOptions(opts ParseOptions) (*AssemblyResult, error) {
return AssembleStringDetailedWithOptions(b.String(), opts)
}
func (b *ProgramBuilder) directive(name string, exprs ...string) *ProgramBuilder {
if len(exprs) == 0 {
return b
}
values := make([]string, 0, len(exprs))
for _, expr := range exprs {
expr = strings.TrimSpace(expr)
if expr == "" {
continue
}
values = append(values, expr)
}
if len(values) == 0 {
return b
}
return b.Line(name + " " + strings.Join(values, ","))
}