|
| 1 | +Documentation for the RPN expression engine in OverLua |
| 2 | +====================================================== |
| 3 | + |
| 4 | +For some graphics processing operationg, OverLua provides an RPN (Reverse |
| 5 | +Polish Notation) to do fast calculations. This expression engine is designed |
| 6 | +primarily to have very fast execution and be multithreading-safe in the |
| 7 | +runtime. |
| 8 | + |
| 9 | +The language is meant to support multi-variable calculations, so apart from |
| 10 | +the usual arithmetic operators there is also an assignment operator. Finally |
| 11 | +there is a limited number of temporary registers. |
| 12 | + |
| 13 | +There is no explicit Lua interface to the expression engine, rather it is |
| 14 | +implicitly used by some image processing functions. In those cases, a |
| 15 | +program is passed as a string to the image processing function. |
| 16 | + |
| 17 | + |
| 18 | +Stack machine execution model |
| 19 | +----------------------------- |
| 20 | + |
| 21 | +The expression evaluator is implemented as a stack machine with a number |
| 22 | +of registers. Some of these registers are used for input and/or output |
| 23 | +values and some are freely usable for temporary storage. |
| 24 | + |
| 25 | +The stack holds numbers in 'double' precision, this is the only data type |
| 26 | +supported by the machine. There are no practical limits on stack depth. |
| 27 | + |
| 28 | +The registers also hold numbers in 'double' precision. Depending on the |
| 29 | +function using the expression evaluator, different input/output registers |
| 30 | +will be available. The input/output registers will usually be single-letter |
| 31 | +names using uppercase letters. There are always exactly ten temporary |
| 32 | +registers available, named "t0" to "t9", ie. lowercase "t" followed by |
| 33 | +a digit. The contents of the temporary registers are undefined at the start |
| 34 | +of program execution. |
| 35 | + |
| 36 | + |
| 37 | +The program consists of a number of instructions executed sequentially. There |
| 38 | +are three basic types of instructions: |
| 39 | + - push |
| 40 | + - call |
| 41 | + - store |
| 42 | + |
| 43 | +A 'push' instruction pushes a single number to the top of the stack. The |
| 44 | +source of the number can either be a constant or a register. |
| 45 | + |
| 46 | +A 'call' instruction pops some numbers from the stack, performs an operation |
| 47 | +on them and pushes a single number back onto the stack. This includes basic |
| 48 | +arithmetic operations such as addition, mulitiplication, but also functions |
| 49 | +such as sinus, rounding and logarithms. How many numbers are popped from the |
| 50 | +stack depends on the function. |
| 51 | + |
| 52 | +The 'store' instruction pops one number from the stack and stores it into |
| 53 | +a register, overwriting the previous number in the register. |
| 54 | + |
| 55 | + |
| 56 | +After every instruction in the program has been executed, any remaining numbers |
| 57 | +on the stack are discarded, and the numbers in the output registers are passed |
| 58 | +to the function using the expression evaluator. |
| 59 | + |
| 60 | + |
| 61 | +If a program operation attempt to pop a number from the stack when the stack |
| 62 | +is empty, the machine halts with an error state, and the result of the program |
| 63 | +is undefined. |
| 64 | + |
| 65 | + |
| 66 | +Expression syntax |
| 67 | +----------------- |
| 68 | + |
| 69 | +The syntax used to specify programs is very straightforward. |
| 70 | + |
| 71 | +The program consists of a number of tokens separated by whitespace. Each token |
| 72 | +translates into one instruction. |
| 73 | + |
| 74 | + |
| 75 | +A token can be a number. This translates into a 'push' instruction, pushing |
| 76 | +that number as a constant. The format for numbers is the same as in C. |
| 77 | + |
| 78 | +Examples: "1", "-5", "3.14", ".5" "2.998e8" |
| 79 | + |
| 80 | + |
| 81 | +A token can be a register name. This translates into a 'push' instruction, |
| 82 | +pushing the number in the register. |
| 83 | + |
| 84 | +Examples: "X", "Y", "t0", "t6" |
| 85 | + |
| 86 | + |
| 87 | +A token can be a basic arithmetic operator. These translate into 'call' |
| 88 | +instructions. Each of these represent a function that pops two numbers, |
| 89 | +performs the calculation and pushes the result. |
| 90 | + |
| 91 | +The basic arithmetic operators are: + - * / ^ |
| 92 | + |
| 93 | +Examples: |
| 94 | +Suppose the two previous instructions were "A B" where A and B are registers. |
| 95 | +This means that now the stack contains B at the top and A just below. |
| 96 | +Operator "+" will then calculate "A + B". Operator "-" will calculate "A - B". |
| 97 | +Operator "*" will calculate "A * B". Operator "/" will calculate "A / B". |
| 98 | +Operator "^" will calculate "A ^ B", ie. A raised to the power of B. |
| 99 | + |
| 100 | +The result of illegal operations is undefined. It will most likely result in |
| 101 | +an invalid number being pushed onto the stack, and operating on that number |
| 102 | +will cause the error to propagate. |
| 103 | + |
| 104 | + |
| 105 | +A token matching the name of a defined function will translate into a 'call' |
| 106 | +instruction for the given function. A number of standard functions are always |
| 107 | +available, see below for details. |
| 108 | + |
| 109 | + |
| 110 | +An equal sign followed by the name of a register translated into a 'store' |
| 111 | +instruction, that will pop the number at the top of the stack and store it |
| 112 | +into the named register. There must not be any whitespace between the equal |
| 113 | +sign and the register name. |
| 114 | + |
| 115 | +Examples: "=X", "=t0" |
| 116 | + |
| 117 | + |
| 118 | +Standard function library |
| 119 | +------------------------- |
| 120 | + |
| 121 | +The following functions are always available in the machine. |
| 122 | + |
| 123 | + |
| 124 | +~ (tilde character) |
| 125 | +Takes one argument, produces one result. |
| 126 | +Unary minus. (Negate the argument.) |
| 127 | + |
| 128 | +abs |
| 129 | +Takes one argument, produces one result. |
| 130 | +Return the absolute value. |
| 131 | + |
| 132 | +floor |
| 133 | +Takes one argument, produces one result. |
| 134 | +Round towards negative infinity. |
| 135 | + |
| 136 | +ceil |
| 137 | +Takes one argument, produces one result. |
| 138 | +Round towards positive infinity. |
| 139 | + |
| 140 | +trunc |
| 141 | +Takes one argument, produces one result. |
| 142 | +Round towards zero. |
| 143 | + |
| 144 | + |
| 145 | +log |
| 146 | +Takes one argument, produces one result. |
| 147 | +Natural (base e) logarithm. |
| 148 | + |
| 149 | +exp |
| 150 | +Takes one argument, produces one result. |
| 151 | +Natural exponentiation. (e to the power of x.) |
| 152 | + |
| 153 | +sqrt |
| 154 | +Takes one argument, produces one result. |
| 155 | +Square root. |
| 156 | + |
| 157 | +e |
| 158 | +Takes no arguments, produces one result. |
| 159 | +Push the value of e. |
| 160 | + |
| 161 | + |
| 162 | +min |
| 163 | +Takes two arguments, produces one result. |
| 164 | +Return the smallest of the arguments. |
| 165 | + |
| 166 | +max |
| 167 | +Takes two arguments, produces one result. |
| 168 | +Return the largest of the arguments. |
| 169 | + |
| 170 | + |
| 171 | +pi |
| 172 | +Takes no arguments, produces one result. |
| 173 | +Push the value of pi. |
| 174 | + |
| 175 | +sin |
| 176 | +Takes one argument, produces one result. |
| 177 | +Sinus function. Argument in radians. |
| 178 | + |
| 179 | +cos |
| 180 | +Takes one argument, produces one result. |
| 181 | +Cosine function. Argument in radians. |
| 182 | + |
| 183 | +tan |
| 184 | +Takes one argument, produces one result. |
| 185 | +Tangent function. Argument in radians. |
| 186 | + |
| 187 | +asin |
| 188 | +Takes one argument, produces one result. |
| 189 | +Arc sinus function. |
| 190 | + |
| 191 | +acos |
| 192 | +Takes one argument, produces one result. |
| 193 | +Arc cosine function. |
| 194 | + |
| 195 | +atan |
| 196 | +Takes one argument, produces one result. |
| 197 | +Arc tangent function. |
| 198 | + |
| 199 | + |
| 200 | +mod |
| 201 | +Takes two arguments, produces one result. |
| 202 | +Modulo operation. |
| 203 | + |
| 204 | + |
| 205 | +rand |
| 206 | +Takes no arguments, produces one result. |
| 207 | +Return a random value in range 0..1. |
| 208 | + |
| 209 | + |
| 210 | +ifgtz |
| 211 | +Takes three arguments, produces one result. |
| 212 | +If the first argument is greater than zero, return the second argument, |
| 213 | +else return the third argument. |
| 214 | + |
| 215 | +ifeqz |
| 216 | +Takes three arguments, produces one result. |
| 217 | +If the first argument is equal to zero, return the second argument, |
| 218 | +else return the third argument. |
| 219 | + |
| 220 | + |
| 221 | +Sample programs |
| 222 | +--------------- |
| 223 | + |
| 224 | +To be written. No functions in OverLua use the expression evaluator yet. |
0 commit comments