poke-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Offset of a value's element by name


From: Jose E. Marchesi
Subject: Offset of a value's element by name
Date: Tue, 08 Mar 2022 12:47:23 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

Hi people.

During one of the demos at the binary tools summit we felt the need of,
given a Poke struct value, get the offset of one of its fields by name.

We already support the following value attributes:

  VAL'length
    Evals to the number of elements (array elements or struct fields) of
    the given composite value.

  VAL'eoffset (IDX)
    Evals to the offset of the element (array element or struct field)
    at the given position IDX in the given composite value.
  
  VAL'ename (IDX)
    Evals to the name of the element (array element or struct field)
    at the given position IDX in the given composite value.  This is
    "[IDX]" for array elements, and the empty string "" for unnammed
    struct fields.

Now, we could add a new attribute:

  VAL'eoffset (STRING)
    Evals to the offset of the element (array element or struct field)
    having the given name.

But, it is way easier to do it in Poke instead of adding a new
language-level construction:

  fun eoffset = (any v, string n) offset<uint<64>,b>:
  {
    for (var i = 0UL; i < v'length; ++i)
      if (n == v'ename (i))
        return v'eoffset (i);
    raise E_inval;
  }

Example usage:

(poke) type Foo = struct { int i; int; int j; }
(poke) var f = Foo {}
(poke) eoffset_by_name (f, "j")
0x40UL#b
(poke) eoffset_by_name (f, "i")
0x0UL#b

Now the question is: should we add the above function to the standard
library?  With what name?

Opinions?



reply via email to

[Prev in Thread] Current Thread [Next in Thread]