Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: native inline #615

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 2 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "tools/asm2asm"]
[submodule "cloudwego"]
path = tools/asm2asm
url = https://github.com/chenzhuoyu/asm2asm
url = https://github.com/cloudwego/asm2asm.git
116 changes: 0 additions & 116 deletions Makefile

This file was deleted.

2 changes: 1 addition & 1 deletion fuzz/ast_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func fuzzASTGetFromObject(t *testing.T, data []byte, m map[string]interface{}) {
v, err := node.Interface()
require.NoErrorf(t, err, "error in node convert\n%s", msg)
require.Equalf(t, v, expv, "error in node equal\n%sGot:\n%s\nExp:\n%s\n",
msg, spew.Sdump(v), spew.Sdump(expv))
msg, spew.Sdump(v), spew.Sdump(expv))
}
}

Expand Down
3 changes: 2 additions & 1 deletion internal/decoder/assembler_regabi_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ var (
_R9 = jit.Reg("R9")
_X0 = jit.Reg("X0")
_X1 = jit.Reg("X1")
_X15 = jit.Reg("X15")
)

var (
Expand Down Expand Up @@ -421,6 +422,7 @@ func (self *_Assembler) call_go(fn obj.Addr) {
func (self *_Assembler) callc(fn obj.Addr) {
self.save(_IP)
self.call(fn)
self.Emit("XORPS", _X15, _X15)
self.load(_IP)
}

Expand Down Expand Up @@ -605,7 +607,6 @@ func (self *_Assembler) skip_one() {
self.Emit("TESTQ", _AX, _AX) // TESTQ AX, AX
self.Sjmp("JS" , _LB_parsing_error_v) // JS _parse_error_v
self.Emit("MOVQ" , _VAR_pc, _R9) // MOVQ pc, R9
// self.Byte(0xcc)
self.Rjmp("JMP" , _R9) // JMP (R9)
}

Expand Down
58 changes: 37 additions & 21 deletions internal/decoder/assembler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
`encoding/base64`
`encoding/json`
`reflect`
`runtime`
`testing`
`unsafe`

Expand All @@ -31,6 +32,28 @@ import (
`github.com/stretchr/testify/require`
)

func TestAssembler_DecodeStruct_SinglePrivateField(t *testing.T) {
var v Tx
s := `{"x": 1}`
p, err := newCompiler().compile(reflect.TypeOf(v))
require.NoError(t, err)
k := new(_Stack)
a := newAssembler(p)
f := a.Load()
pos, err := f(s, 0, unsafe.Pointer(&v), k, 0, "", nil)
if len(s) != pos {
panic(pos)
}
pc, file, line, ok := runtime.Caller(0)
if !ok {
println(pc, file, line)
panic("runtime.Caller failed")
}
require.NoError(t, err)
// assert.Equal(t, len(s), pos)
// assert.Equal(t, Tx{}, v)
}

var utextVar []byte
type UtextValue int

Expand Down Expand Up @@ -175,7 +198,8 @@ func TestAssembler_OpCode(t *testing.T) {
src: `hello\ud800world"`,
exp: "hello\ufffdworld",
val: new(string),
}, {
},
{
key: "_OP_str/error_eof",
ins: []_Instr{newInsOp(_OP_str)},
src: `12345`,
Expand All @@ -187,7 +211,8 @@ func TestAssembler_OpCode(t *testing.T) {
src: `12\g345"`,
err: SyntaxError{Src: `12\g345"`, Pos: 3, Code: types.ERR_INVALID_ESCAPE},
val: new(string),
}, {
},
{
key: "_OP_str/error_invalid_unicode",
ins: []_Instr{newInsOp(_OP_str)},
src: `hello\ud800world"`,
Expand Down Expand Up @@ -218,7 +243,8 @@ func TestAssembler_OpCode(t *testing.T) {
src: `aGVsbG8!sIHdvcmxk"`,
err: base64.CorruptInputError(7),
val: new([]byte),
}, {
},
{
key: "_OP_bool/true",
ins: []_Instr{newInsOp(_OP_bool)},
src: "true",
Expand Down Expand Up @@ -288,7 +314,8 @@ func TestAssembler_OpCode(t *testing.T) {
src: "-",
err: SyntaxError{Src: `-`, Pos: 1, Code: types.ERR_INVALID_CHAR},
val: new(json.Number),
}, {
},
{
key: "_OP_num/error_invalid_char",
ins: []_Instr{newInsOp(_OP_num)},
src: "xxx",
Expand Down Expand Up @@ -521,20 +548,23 @@ func TestAssembler_OpCode(t *testing.T) {
assert.Equal(t, _MinSlice, cap(v.([]int)))
},
val: new([]int),
}, {
},
{
key: "_OP_slice_append",
ins: []_Instr{newInsVt(_OP_slice_append, reflect.TypeOf(0)), newInsOp(_OP_nil_1)},
src: "",
exp: []int{123, 0},
val: &[]int{123},
}, {
},
{
key: "_OP_object_skip",
ins: []_Instr{newInsOp(_OP_object_skip)},
src: `{"zxcv":[1,2.0],"asdf":[true,false,null,"asdf",{"qwer":345}]}`,
pos: 1,
vfn: func(i int, _ interface{}) { assert.Equal(t, 61, i) },
val: nil,
}, {
},
{
key: "_OP_object_next",
ins: []_Instr{newInsOp(_OP_object_next)},
src: `{"asdf":[1,2.0,true,false,null,"asdf",{"qwer":345}]}`,
Expand Down Expand Up @@ -711,20 +741,6 @@ type Tx struct {
x int
}

func TestAssembler_DecodeStruct_SinglePrivateField(t *testing.T) {
var v Tx
s := `{"x": 1}`
p, err := newCompiler().compile(reflect.TypeOf(v))
require.NoError(t, err)
k := new(_Stack)
a := newAssembler(p)
f := a.Load()
pos, err := f(s, 0, unsafe.Pointer(&v), k, 0, "", nil)
require.NoError(t, err)
assert.Equal(t, len(s), pos)
assert.Equal(t, Tx{}, v)
}

func TestAssembler_DecodeByteSlice_Bin(t *testing.T) {
var v []byte
s := `"aGVsbG8sIHdvcmxk"`
Expand Down
2 changes: 0 additions & 2 deletions internal/decoder/primitives.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ func decodeTypedPointer(s string, i int, vt *rt.GoType, vp unsafe.Pointer, sb *_
return 0, err
} else {
rt.MoreStack(_FP_size + _VD_size + native.MaxFrameSize)
rt.StopProf()
ret, err := fn(s, i, vp, sb, fv, "", nil)
rt.StartProf()
return ret, err
}
}
Expand Down
2 changes: 2 additions & 0 deletions internal/encoder/assembler_regabi_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ var (

var (
_X0 = jit.Reg("X0")
_X15 = jit.Reg("X15")
_Y0 = jit.Reg("Y0")
)

Expand Down Expand Up @@ -512,6 +513,7 @@ func (self *_Assembler) call_c(pc obj.Addr) {
self.call(pc) // CALL $pc
self.xload(_REG_ffi...) // LOAD $REG_ffi
self.Emit("XCHGQ", _SP_p, _BX)
self.Emit("XORPS", _X15, _X15)
}

func (self *_Assembler) call_go(pc obj.Addr) {
Expand Down
5 changes: 5 additions & 0 deletions internal/encoder/assembler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ import (
`github.com/stretchr/testify/assert`
)

func TestEncodeNative(t *testing.T) {
var obj = float64(1.11)
Encode(obj, 0)
}

func TestEncoderMemoryCorruption(t *testing.T) {
println("TestEncoderMemoryCorruption")
runtime.GC()
Expand Down
9 changes: 3 additions & 6 deletions internal/encoder/primitives.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

`github.com/bytedance/sonic/internal/jit`
`github.com/bytedance/sonic/internal/native`
`github.com/bytedance/sonic/internal/native/types`
`github.com/bytedance/sonic/internal/rt`
)

Expand Down Expand Up @@ -73,15 +74,11 @@ func encodeTypedPointer(buf *[]byte, vt *rt.GoType, vp *unsafe.Pointer, sb *_Sta
return err
} else if vt.Indirect() {
rt.MoreStack(_FP_size + native.MaxFrameSize)
rt.StopProf()
err := fn(buf, *vp, sb, fv)
rt.StartProf()
return err
} else {
rt.MoreStack(_FP_size + native.MaxFrameSize)
rt.StopProf()
err := fn(buf, unsafe.Pointer(vp), sb, fv)
rt.StartProf()
return err
}
}
Expand Down Expand Up @@ -123,8 +120,8 @@ func htmlEscape(dst []byte, src []byte) []byte {
dbuf := (*rt.GoSlice)(unsafe.Pointer(&dst))

/* grow dst if it is shorter */
if cap(dst) - len(dst) < len(src) + native.BufPaddingSize {
cap := len(src) * 3 / 2 + native.BufPaddingSize
if cap(dst) - len(dst) < len(src) + types.BufPaddingSize {
cap := len(src) * 3 / 2 + types.BufPaddingSize
*dbuf = growslice(typeByte, *dbuf, cap)
}

Expand Down
36 changes: 36 additions & 0 deletions internal/native/avx/f32toa.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading