-
Notifications
You must be signed in to change notification settings - Fork 200
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
sizeof and lea operations #84
Comments
@GreyCat, when can I expect this landed? I'm implementing a ksy for a format, where a payload is in the middle of fixed-size fields, and the size of that payload is defined by container. Surely we cannot use "size: eos" here. |
Could you show the file format? I think this can be worked around by splitting the type into two different subtypes. |
Here is the spec for payload. It is a separate type, which size is defined by the field size it is placed in. It can be placed into different types, so I dislike to use the hacks like |
There are many different proposals here, so let's break it into some parts. First of all,
So, I can propose that we'd start from the basic
Given that we support addressing in both bytes and bits, probably we'd need two implementations of these, like I have an relatively easy syntax proposal for "using expression": do something like I have no idea right now on how to implement type-based sizeof operator, except for introducing yet another keyword, which is probably doable, but definitely not very pretty... |
Do we really need type-based syntax? I mean if the field of the type is never used, we don't need its size, if it is used, we can use expression-based syntax. Updated the proposal a bit with impl details. |
Well, yes and no. Sometimes it's just very inconvenient to go through all that seq:
- id: things
type: thing
repeat: eos
|
I've just committed preliminary implementation of precompile stage that calculated fixed size of all types and members, if that's possible. This already improved Graphviz output a lot: now it correctly prints out offsets/sizes table even for one fixed sized structure embedded into the other, i.e. stuff like BMP, Blender .blend, cramfs, etc. Also, it obviously opens the way for sizeof / lea implementation. |
If the type is not fixed size, could you add the possibility of getting the min and max size of the type |
@bsagal, it is a more complex derivation. Could you show use cases for it when the actual size used by a field is not enough? |
@KOLANICH I have non fixed size structs and would like to add logic to check that the size of the steam is valid before starting the parsing |
I guess it should be done by the code generated by KSC, not by a ksy dev. |
Implemented type-based sizeof in byte/bit flavors, as special keyword operator:
See expr_sizeof_type_1 for example. |
Also implemented value-based sizeof, using virtual |
Thank you. |
@KOLANICH I have a question regarding your initial example in #84 (comment). The first field Does it imply that you don't want the
But then it seems to me that you're contradicting yourself. I think
If the Querying All we'd have to do is to collect the fields that are used in public void _read() {
int _ofsStr = this._io.pos();
this.str = new String(this._io.readBytesTerm(0, false, true, true), Charset.forName("ASCII"));
this._sizeofStr = this._io.pos() - _ofsStr;
}
private Integer _sizeofStr;
public Integer _sizeofStr() { return _sizeofStr; } Actually, we can use this approach on all value-based And using |
@generalmimon, yes, it is meant to generate something that gives us byte size (not code point surely) (or maybe |
I'm taking this out of the 0.9 milestone. This feature currently works only for compile-time calculations, so it's not yet finished, but I'd say that's fine for purposes of 0.9 version. We can always improve it in the future. |
It'd be nice to have
sizeof
operators for getting amount of space occupied by some set of structures andlea
for getting offset of properties to have relative addressing modes.The specification is:
lea()
returns an offset of the struct making the scope. It is the stream position where the value of this type have been started parsing.lea(property_name)
returns an offset of the property which name is passed. It is either the stream position where the property have been started parsing, or predicted using known sizes of already parsed fields.sizeof()
returns size of the struct making the scope known by the moment. If the size is unknown - compile error. The size is known when any of the following is satisfied:size
is stored in some variable and can be accessed and modified (modification can have serialization implications like truncating of arrays)sizeof(property_name)
returns a size of the property which name has been passed.sizeof(property_name1, property_name2)
is equivalent tolea(property_name2) - lea(property_name1) + sizeof(property_name2)
andlea(property_name2) >= lea(property_name1)
must satisfyThe example is
It creates a struct of size
len
with variable-length string in the beginning. Then it places a 2superstruct
s indata
relative to beginning ofdata
. Variable-length string is taken as an example of a case where sizes and offsets are not defined on compile time.The text was updated successfully, but these errors were encountered: