Reduced memory allocations

This commit is contained in:
Eduard Urbach 2024-07-17 15:23:57 +02:00
parent f4e4e49fce
commit 8b2cdfa841
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
3 changed files with 11 additions and 3 deletions

View File

@ -13,3 +13,12 @@ func (a *Assembler) Merge(b Assembler) {
maps.Copy(a.Data, b.Data)
a.Instructions = append(a.Instructions, b.Instructions...)
}
// SetData sets the data for the given label.
func (a *Assembler) SetData(label string, data []byte) {
if a.Data == nil {
a.Data = map[string][]byte{}
}
a.Data[label] = data
}

View File

@ -27,7 +27,6 @@ func NewFunction(name string, file *fs.File, body token.List) *Function {
state: state{
assembler: asm.Assembler{
Instructions: make([]asm.Instruction, 0, 32),
Data: map[string][]byte{},
},
cpu: cpu.CPU{
All: x64.AllRegisters,

View File

@ -40,8 +40,8 @@ func (f *Function) TokenToRegister(t token.Token, register cpu.Register) error {
case token.String:
f.count.data++
label := fmt.Sprintf("%s_data_%d", f.Name, f.count.data)
value := t.Text()[1 : len(t.Bytes)-1]
f.assembler.Data[label] = []byte(value)
value := t.Bytes[1 : len(t.Bytes)-1]
f.assembler.SetData(label, value)
f.RegisterLabel(asm.MOVE, register, label)
return nil