crystal_lib icon indicating copy to clipboard operation
crystal_lib copied to clipboard

Bitfield support

Open kojix2 opened this issue 3 years ago • 2 comments

Hello. Thanks for the great tool. However, the tool seems to fail to parse structs with bit fields. Please provide support for bit fields.

kojix2 avatar Oct 25 '21 10:10 kojix2

struct BGZF {
    // Reserved bits should be written as 0; read as "don't care"
    unsigned errcode:16, reserved:1, is_write:1, no_eof_block:1, is_be:1;
    signed compress_level:9;
    unsigned last_block_eof:1, is_compressed:1, is_gzip:1;
    int cache_size;
    int block_length, block_clength, block_offset;
    int64_t block_address, uncompressed_address;
    void *uncompressed_block, *compressed_block;
    bgzf_cache_t *cache;
    struct hFILE *fp; // actual file handle
    struct bgzf_mtaux_t *mt; // only used for multi-threading
    bgzidx_t *idx;      // BGZF index
    int idx_build_otf;  // build index on the fly, set by bgzf_index_build_init()
    struct z_stream_s *gz_stream; // for gzip-compressed files
    int64_t seeked;     // virtual offset of last seek
};

Actual code generated by crystal_lib:

  struct Bgzf
    errcode : LibC::UInt
    reserved : LibC::UInt
    is_write : LibC::UInt
    no_eof_block : LibC::UInt
    is_be : LibC::UInt
    compress_level : LibC::Int
    last_block_eof : LibC::UInt
    is_compressed : LibC::UInt
    is_gzip : LibC::UInt
    cache_size : LibC::Int
    block_length : LibC::Int
    block_clength : LibC::Int
    block_offset : LibC::Int
    block_address : Int64T
    uncompressed_address : Int64T
    uncompressed_block : Void*
    compressed_block : Void*
    cache : BgzfCacheT
    fp : HFile*
    mt : BgzfMtauxT*
    idx : BgzidxT
    idx_build_otf : LibC::Int
    gz_stream : ZStreamS*
    seeked : Int64T
  end

Bad but somewhat better code:

    struct Bgzf
      bitfields : Uint32T # Fixme
      # errcode : LibC::UInt
      # reserved : LibC::UInt
      # is_write : LibC::UInt
      # no_eof_block : LibC::UInt
      # is_be : LibC::UInt
      # compress_level : LibC::Int
      # last_block_eof : LibC::UInt
      # is_compressed : LibC::UInt
      # is_gzip : LibC::UInt
      cache_size : LibC::Int
      block_length : LibC::Int
      block_clength : LibC::Int
      block_offset : LibC::Int
      block_address : Int64T
      uncompressed_address : Int64T
      uncompressed_block : Void*
      compressed_block : Void*
      cache : BgzfCacheT
      fp : HFile*
      mt : BgzfMtauxT*
      idx : BgzidxT
      idx_build_otf : LibC::Int
      gz_stream : ZStreamS*
      seeked : Int64T
    end

kojix2 avatar Oct 25 '21 10:10 kojix2

It would be better if we could link bindings created with crystal_lib with this. https://github.com/elorest/bitfields

kojix2 avatar Oct 31 '21 01:10 kojix2