encodeValue encodes the proto value to buf.
(s *compiler.S, ptr, buf *codegen.Value, ty semantic.Type)
| 416 | |
| 417 | // encodeValue encodes the proto value to buf. |
| 418 | func (e *encoder) encodeValue(s *compiler.S, ptr, buf *codegen.Value, ty semantic.Type) { |
| 419 | ty = semantic.Underlying(ty) |
| 420 | switch ty := ty.(type) { |
| 421 | case *semantic.Builtin: |
| 422 | switch ty { |
| 423 | case semantic.Int8Type, |
| 424 | semantic.Int16Type, |
| 425 | semantic.Int32Type, |
| 426 | semantic.Int64Type, |
| 427 | semantic.IntType, |
| 428 | semantic.CharType: |
| 429 | e.writeZigzag(s, buf, ptr.Load().Cast(e.T.Int64)) // Sign-ext |
| 430 | return |
| 431 | case semantic.Uint8Type, |
| 432 | semantic.Uint16Type, |
| 433 | semantic.Uint32Type, |
| 434 | semantic.Uint64Type, |
| 435 | semantic.UintType, |
| 436 | semantic.SizeType, |
| 437 | semantic.BoolType: |
| 438 | e.writeZigzag(s, buf, ptr.Load().Cast(e.T.Uint64)) // No sign-ext |
| 439 | return |
| 440 | case semantic.Float32Type: |
| 441 | e.writeFixed32(s, buf, ptr.Load()) |
| 442 | return |
| 443 | case semantic.Float64Type: |
| 444 | e.writeFixed64(s, buf, ptr.Load()) |
| 445 | return |
| 446 | case semantic.StringType: |
| 447 | strPtr := ptr.Load() |
| 448 | size := strPtr.Index(0, compiler.StringLength).Load().Cast(e.T.Uint32) |
| 449 | bytes := strPtr.Index(0, compiler.StringData, 0) |
| 450 | e.debug(s, "encoding string at %p: size: %d, refcount: %d, str: %s", |
| 451 | strPtr, size, strPtr.Index(0, compiler.StringRefCount).Load(), bytes) |
| 452 | e.writeBytes(s, buf, size, bytes) |
| 453 | return |
| 454 | } |
| 455 | case *semantic.Enum: |
| 456 | e.encodeValue(s, ptr, buf, ty.NumberType) |
| 457 | return |
| 458 | case *semantic.Pointer: |
| 459 | e.encodeValue(s, ptr, buf, semantic.Uint64Type) |
| 460 | return |
| 461 | case *semantic.StaticArray: |
| 462 | panic("Must be handled in encodeField") |
| 463 | case *semantic.Class: |
| 464 | e.writeBlob(s, buf, func(buf *codegen.Value) { |
| 465 | s.Call(e.funcs.encodeToBuf[ty], ptr, s.Ctx, buf) |
| 466 | }) |
| 467 | return |
| 468 | case *semantic.Reference: |
| 469 | refPtr := ptr.Load() |
| 470 | s.If(s.NotEqual(refPtr, s.Zero(refPtr.Type())), func(s *compiler.S) { |
| 471 | signedRefID := s.Call(e.callbacks.encodeBackref, s.Ctx, refPtr.Cast(e.T.VoidPtr)) |
| 472 | newRef := s.GreaterOrEqualTo(signedRefID, s.Scalar(int64(0))) |
| 473 | refID := s.Select(newRef, signedRefID, s.Negate(signedRefID)) |
| 474 | |
| 475 | e.writeBlob(s, buf, func(buf *codegen.Value) { |
no test coverage detected