sointu/compiler/wasm_macros.go
Veikko Sariola e4490faa2e feat(compiler): Add support for targeting WebAssembly.
The working principle is similar as before with x86, but instead of outputting .asm, it outputs .wat. This can be compiled into .wasm by using the wat2wasm assembler.
2020-12-26 23:16:18 +02:00

51 lines
911 B
Go

package compiler
import (
"bytes"
"encoding/binary"
)
type WasmMacros struct {
data *bytes.Buffer
Labels map[string]int
}
func NewWasmMacros() *WasmMacros {
return &WasmMacros{
data: new(bytes.Buffer),
Labels: map[string]int{},
}
}
func (wm *WasmMacros) SetLabel(label string) string {
wm.Labels[label] = wm.data.Len()
return ""
}
func (wm *WasmMacros) GetLabel(label string) int {
return wm.Labels[label]
}
func (wm *WasmMacros) DataB(value byte) string {
binary.Write(wm.data, binary.LittleEndian, value)
return ""
}
func (wm *WasmMacros) DataW(value uint16) string {
binary.Write(wm.data, binary.LittleEndian, value)
return ""
}
func (wm *WasmMacros) DataD(value uint32) string {
binary.Write(wm.data, binary.LittleEndian, value)
return ""
}
func (wm *WasmMacros) ToByte(value int) byte {
return byte(value)
}
func (wm *WasmMacros) Data() []byte {
return wm.data.Bytes()
}