first pass at DATA sections

This commit is contained in:
Michael McLoughlin
2018-12-27 11:57:46 -08:00
parent d29c6340d7
commit 9243d299e6
11 changed files with 250 additions and 15 deletions

34
examples/data/asm.go Normal file
View File

@@ -0,0 +1,34 @@
// +build ignore
package main
import (
"math"
. "github.com/mmcloughlin/avo/build"
. "github.com/mmcloughlin/avo/operand"
)
func main() {
bytes := GLOBL("bytes")
DATA(0, U64(0x0011223344556677))
DATA(8, String("strconst"))
DATA(16, F32(math.Pi))
DATA(24, F64(math.Pi))
DATA(32, U32(0x00112233))
DATA(36, U16(0x4455))
DATA(38, U8(0x66))
DATA(39, U8(0x77))
// TEXT ·DataAt(SB),0,$0-9
TEXT("DataAt", "func(i int) byte")
i := Load(Param("i"), GP64v()) // MOVQ i+0(FP), AX
ptr := Mem{Base: GP64v()}
LEAQ(bytes, ptr.Base) // LEAQ b<>+0x00(SB), BX
b := GP8v()
MOVB(ptr.Idx(i, 1), b) // MOVB 0(BX)(AX*1), CL
Store(b, ReturnIndex(0)) // MOVB CL, ret+8(FP)
RET() // RET
Generate()
}

21
examples/data/data.s Normal file
View File

@@ -0,0 +1,21 @@
// Code generated by command: go run asm.go -out data.s -stubs stub.go. DO NOT EDIT.
#include "textflag.h"
DATA bytes<>(SB)/8, $0x0011223344556677
DATA bytes<>+8(SB)/8, $"strconst"
DATA bytes<>+16(SB)/4, $(3.1415927)
DATA bytes<>+24(SB)/8, $(3.141592653589793)
DATA bytes<>+32(SB)/4, $0x00112233
DATA bytes<>+36(SB)/2, $0x4455
DATA bytes<>+38(SB)/1, $0x66
DATA bytes<>+39(SB)/1, $0x77
GLOBL ·bytes<>(SB), RODATA, $40
// func DataAt(i int) byte
TEXT ·DataAt(SB),0,$0-9
MOVQ i(FP), AX
LEAQ bytes<>(SB), CX
MOVB (CX)(AX*1), AL
MOVB AL, ret+8(FP)
RET

View File

@@ -0,0 +1,29 @@
package data
import (
"encoding/binary"
"math"
"testing"
)
//go:generate go run asm.go -out data.s -stubs stub.go
func TestDataAt(t *testing.T) {
order := binary.LittleEndian
expect := make([]byte, 40)
order.PutUint64(expect[0:], 0x0011223344556677) // DATA(0, U64(0x0011223344556677))
copy(expect[8:], []byte("strconst")) // DATA(8, String("strconst"))
order.PutUint32(expect[16:], math.Float32bits(math.Pi)) // DATA(16, F32(math.Pi))
order.PutUint64(expect[24:], math.Float64bits(math.Pi)) // DATA(24, F64(math.Pi))
order.PutUint32(expect[32:], 0x00112233) // DATA(32, U32(0x00112233))
order.PutUint16(expect[36:], 0x4455) // DATA(36, U16(0x4455))
expect[38] = 0x66 // DATA(38, U8(0x66))
expect[39] = 0x77 // DATA(39, U8(0x77))
for i, e := range expect {
b := DataAt(i)
if b != e {
t.Errorf("DataAt(%d) = %#02x; expected %#02x", i, b, e)
}
}
}

5
examples/data/stub.go Normal file
View File

@@ -0,0 +1,5 @@
// Code generated by command: go run asm.go -out data.s -stubs stub.go. DO NOT EDIT.
package data
func DataAt(i int) byte

View File

@@ -15,16 +15,16 @@ func main() {
// Store message values on the stack.
w := AllocLocal(64)
W := func(r int) Mem { return w.Idx((r % 16) * 4) }
W := func(r int) Mem { return w.Offset((r % 16) * 4) }
// Load initial hash.
h0, h1, h2, h3, h4 := GP32v(), GP32v(), GP32v(), GP32v(), GP32v()
MOVL(h.Idx(0), h0)
MOVL(h.Idx(4), h1)
MOVL(h.Idx(8), h2)
MOVL(h.Idx(12), h3)
MOVL(h.Idx(16), h4)
MOVL(h.Offset(0), h0)
MOVL(h.Offset(4), h1)
MOVL(h.Offset(8), h2)
MOVL(h.Offset(12), h3)
MOVL(h.Offset(16), h4)
// Initialize registers.
a, b, c, d, e := GP32v(), GP32v(), GP32v(), GP32v(), GP32v()
@@ -52,7 +52,7 @@ func main() {
// Load message value.
u := GP32v()
if r < 16 {
MOVL(m.Idx(4*r), u)
MOVL(m.Offset(4*r), u)
BSWAPL(u)
} else {
MOVL(W(r-3), u)
@@ -85,11 +85,11 @@ func main() {
ADDL(e, h4)
// Store results back.
MOVL(h0, h.Idx(0))
MOVL(h1, h.Idx(4))
MOVL(h2, h.Idx(8))
MOVL(h3, h.Idx(12))
MOVL(h4, h.Idx(16))
MOVL(h0, h.Offset(0))
MOVL(h1, h.Offset(4))
MOVL(h2, h.Offset(8))
MOVL(h3, h.Offset(12))
MOVL(h4, h.Offset(16))
RET()
Generate()