lua-users home
lua-l archive

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


Gabi <bugspynet <at> gmail.com> writes:
> I couldn't find any docs or references to help me with it.
> Does anybody even use 
> it (outside of LuaJIT of course) ?

I am using it as of about a month ago for protobuf decoding:
  http://blog.reverberate.org/2011/04/25/upb-status-and-preliminary-performance-
numbers/

I had to learn how to use it from reading the LuaJIT sources,
and having documentation would definitely have saved some time.
I was considering writing some docs, but haven't gotten around
to it.

> I only found a tiny example in github

Use the LuaJIT sources, they use most of its features.

But here's a quick rundown of a few tricky topics:

LABELS

There are three kinds of labels: global, local, and pclabel.

Global labels look like:
  ->mylabel:
      nop
      jmp  ->mylabel

The number of global labels is fixed at preprocessing time, and
each global label is assigned a static number from 0 - (count-1)

There are 9 local labels (with numbers 1-9) and they can be
redefined over and over.  When you jump to a local label, you
jump either forwards or backwards to the most recently defined
(or next defined) label with that number:

   0:
      nop
      jmp  <0   // jumps to last "0" label
      jmp  >0   // jumps to next "0" label
      nop
   0:

pclabel labels are flexible since you can decide at runtime how
many of them there are, but you have to allocate the numbers
yourself.

   =>my_labelnum:
     nop
     jmp  =>my_labelnum


TYPE MAPS

Type maps let you conveniently formulate effective addresses
based on a register that holds a pointer to a struct and a C
expression that indicates a member of that struct.

// This means that DECODER is associated with the C type
// "upb_decoder", and by default is assumed to be in register
// r15.
.type   DECODER,   upb_decoder, r15
   // This essentially does an offsetof(upb_decoder, foo) and
   // uses that to do offset-based addressing.  So this might
   // expand into an instruction like: mov [r15 + 8], 5
   mov  DECODER->foo, 5

   // Like the previous, but explicitly state that the decoder
   // is in a register other than r15.
   mov  DECODER:rax->foo, 5


Those were two of the hardest things for me to figure out.  The
other stuff you should pick up pretty easily by reading the
LuaJIT sources.  Good luck!

Josh