lightning
[Top][All Lists]
Advanced

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

Re: Embedding data in the code stream


From: Paulo César Pereira de Andrade
Subject: Re: Embedding data in the code stream
Date: Thu, 11 Aug 2022 14:19:06 -0300

Em qui., 11 de ago. de 2022 às 08:00, Paul Cercueil
<paul@crapouillou.net> escreveu:
>
> Why don't you simply malloc your object, and tell Lightning to emit
> code at offset (e.g.) 0x100? Then your first 0x100 bytes would be for
> meta-data use.

  This should be the best approach, unless you want to embed code
in that chunk.

  There is already a jit_data() code, but it is internal, for literals. Backends
use different approaches with it. Usually to load immediates.
  Somewhat unrelated, but maybe you are already using, is the pattern
to disable a separate memory buffer for constants, or notes, see
check/lightning.c:
    if (flag_data == 0) {
        jit_realize();
        jit_set_data(NULL, 0, JIT_DISABLE_DATA | JIT_DISABLE_NOTE);
    }
to force generation of likely longer code to load immediates.

  If needing to embed code, can make a jit_jmpi to the start of the code,
then jit_align. But that would be somewhat ugly, and if it is not a entry
point to the vm, starting with a jump could just make it slower.

> Cheers,
> -Paul
>
> Le jeu., août 11 2022 at 12:44:08 +0200, Marc Nieper-Wißkirchen
> <marc.nieper+gnu@gmail.com> a écrit :
> > Am Do., 11. Aug. 2022 um 12:28 Uhr schrieb Paul Cercueil
> > <paul@crapouillou.net>:
> >> Hi Marc,
> >>
> >>  Obvious questions first: why would you do that?
> >
> > Say, I want to implement a VM using GNU lightning.  Code objects
> > would be first-class objects.  Some information (e.g. for the garbage
> > collector) would need to be attached to such code objects to remove
> > the need for an extra pointer indirection.
> >
> > An example:
> >
> > jit_align (16);
> > jit_data_ui (meta_information);
> > jit_align (16);
> > entry = label_indirect ();
> > /* the actual code */
> > ...
> > unsigned char *addr = jit_address (entry);
> > unsigned int info = * (unsigned int *) (addr - 16);

  You can attempt to optimize for very small code objects, but overall,
should assume it uses at least one page, so, can use mmap to get
the memory, and make a mprotect call later to make it executable
and readonly. See the jit_set_code example in the lightning info for
what can be done for what you need. Note that most likely you would
need to have that memory  readonly (must be executable), what might
not be a good idea, unless need a full page for metadata, and keep the
first page that way.



reply via email to

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