Skip to content

Commit

Permalink
gapil encoder: Un-inline write_zigzag
Browse files Browse the repository at this point in the history
It's used everywhere and produces masses of code.
  • Loading branch information
ben-clayton committed Mar 7, 2018
1 parent 9d1c854 commit 0ef40ad
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions gapil/compiler/plugins/encoder/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type funcs struct {
termBuf codegen.Function // void term_buf(buffer*)
appendBuf codegen.Function // void append_buf(buffer*, u32 size, void* data)
writeVarint codegen.Function // void write_varint(buffer*, u64 val)
writeZigzag codegen.Function // void write_zigzag(buffer*, u64 val)
encodeToBuf map[*semantic.Class]codegen.Function // void(context*, buffer*)
}

Expand Down Expand Up @@ -146,7 +147,7 @@ func (e *encoder) buildBufferFuncs() {
})
})

e.funcs.writeVarint = e.M.Function(e.T.VoidPtr, "write_varint", e.T.CtxPtr, e.bufPtrTy, e.T.Uint64).
e.funcs.writeVarint = e.M.Function(e.T.Void, "write_varint", e.T.CtxPtr, e.bufPtrTy, e.T.Uint64).
LinkOnceODR()
e.C.Build(e.funcs.writeVarint, func(s *compiler.S) {
buf, val := s.Parameter(1), s.Parameter(2)
Expand All @@ -170,6 +171,18 @@ func (e *encoder) buildBufferFuncs() {
bytes.Index(0, length.Load()).Store(i.Load().Cast(e.T.Uint8))
s.Call(e.funcs.appendBuf, s.Ctx, buf, s.Add(length.Load(), s.Scalar(uint32(1))), bytes.Index(0, 0))
})

e.funcs.writeZigzag = e.M.Function(e.T.Void, "write_zigzag", e.T.CtxPtr, e.bufPtrTy, e.T.Uint64).
LinkOnceODR()
e.C.Build(e.funcs.writeZigzag, func(s *compiler.S) {
buf, val := s.Parameter(1), s.Parameter(2)
// (n << 1) ^ (n >> 63)
val = val.Cast(e.T.Int64)
lhs := s.ShiftLeft(val, s.Scalar(int64(1)))
rhs := s.ShiftRight(val, s.Scalar(int64(63)))
zigzag := s.Xor(lhs, rhs)
e.writeVarint(s, buf, zigzag)
})
}

// mgEncode returns the mangling function for an encode method on the given
Expand Down Expand Up @@ -649,21 +662,16 @@ func (e *encoder) writeFixed64(s *compiler.S, buf, val *codegen.Value) {
s.Call(e.funcs.appendBuf, s.Ctx, buf, s.Scalar(uint32(8)), i.Cast(e.T.VoidPtr))
}

// writeZigzag writes a zigzag encoded, variable length integer to buf.
func (e *encoder) writeZigzag(s *compiler.S, buf, val *codegen.Value) {
// (n << 1) ^ (n >> 63)
val = val.Cast(e.T.Int64)
lhs := s.ShiftLeft(val, s.Scalar(int64(1)))
rhs := s.ShiftRight(val, s.Scalar(int64(63)))
zigzag := s.Xor(lhs, rhs)
e.writeVarint(s, buf, zigzag)
}

// writeZigzag writes a variable length integer to buf.
func (e *encoder) writeVarint(s *compiler.S, buf, val *codegen.Value) {
s.Call(e.funcs.writeVarint, s.Ctx, buf, val.Cast(e.T.Uint64))
}

// writeZigzag writes a zigzag encoded, variable length integer to buf.
func (e *encoder) writeZigzag(s *compiler.S, buf, val *codegen.Value) {
s.Call(e.funcs.writeZigzag, s.Ctx, buf, val.Cast(e.T.Uint64))
}

// writeBlob writes calls inner with a new buffer. Once inner returns the buffer
// size is encoded as a varint. followed by the buffer itself.
func (e *encoder) writeBlob(s *compiler.S, buf *codegen.Value, inner func(*codegen.Value)) {
Expand Down

0 comments on commit 0ef40ad

Please sign in to comment.