3.1.3.1 Expressions: lvalues and rvalues

The base class of expression is the gccjit;;rvalue, representing an expression that can be on the `right'-hand side of an assignment: a value that can be computed somehow, and assigned `to' a storage area (such as a variable). It has a specific gccjit;;type.

Anothe important class is gccjit;;lvalue. A gccjit;;lvalue. is something that can of the `left'-hand side of an assignment: a storage area (such as a variable).

In other words, every assignment can be thought of as:

LVALUE = RVALUE;

Note that gccjit;;lvalue is a subclass of gccjit;;rvalue, where in an assignment of the form:

LVALUE_A = LVALUE_B;

the LVALUE_B implies reading the current value of that storage area, assigning it into the LVALUE_A.

So far the only expressions we’ve seen are from the previous tutorial:

  1. the multiplication i * i:
gccjit::rvalue expr =
  ctxt.new_binary_op (
    GCC_JIT_BINARY_OP_MULT, int_type,
    param_i, param_i);

/* Alternatively, using operator-overloading: */
gccjit::rvalue expr = param_i * param_i;

which is a gccjit;;rvalue, and

  1. the various function parameters: param_i and param_n, instances of gccjit;;param, which is a subclass of gccjit;;lvalue (and, in turn, of gccjit;;rvalue): we can both read from and write to function parameters within the body of a function.

Our new example has a new kind of expression: we have two local variables. We create them by calling gccjit;;function;;new_local(), supplying a type and a name:

/* Build locals:  */
gccjit::lvalue i = func.new_local (the_type, "i");
gccjit::lvalue sum = func.new_local (the_type, "sum");

These are instances of gccjit;;lvalue - they can be read from and written to.

Note that there is no precanned way to create `and' initialize a variable like in C:

int i = 0;

Instead, having added the local to the function, we have to separately add an assignment of 0 to local_i at the beginning of the function.