first pass at DATA sections
This commit is contained in:
34
examples/data/asm.go
Normal file
34
examples/data/asm.go
Normal 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
21
examples/data/data.s
Normal 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
|
||||
29
examples/data/data_test.go
Normal file
29
examples/data/data_test.go
Normal 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
5
examples/data/stub.go
Normal 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
|
||||
Reference in New Issue
Block a user