Reducing Duplicate Code with Quoting

“Quoting” is a method of decreasing code size by avoiding duplication of code. It is similar to using function calls: your program contains only one instance of the code for a function, even though it may be used many times. The program calls the function instead of repeating the function code.

Quoting extends this technique to code that isn't defined as a function. The compiler examines the code that it generates, looking for places where a sequence of instructions is repeated. If it finds such repetitions, it replaces all but one of the occurrences with the equivalent of a function call, and makes the remaining occurrence the equivalent of the function body. Quoting differs from function calls in that there are no arguments and no return value; quoting changes only the path of execution. The low space overhead for quoting makes it worth using for sequences only a few bytes long.

Quoting is implemented by two instructions: the QUOTE instruction indicates the function call, and the EQUOTE instruction indicates the end of the function body. No label marks the beginning of the function body.

A QUOTE instruction takes a one- or two-byte offset as an argument. When a QUOTE is executed, it saves the address of the next instruction as a return address and performs a jump to the specified offset. The return address is not pushed onto the stack; it is stored in the PQ register instead. When an EQUOTE instruction is executed, it checks whether PQ contains an address. If not, EQUOTE does nothing; if it does, EQUOTE performs a jump back to that address. This allows the quoted section of code to be executed in two ways: in sequence with the preceding and following code or as a quote call.

The compiler does not nest quotes; that is, a quoted section of code itself cannot contain quote instructions. However, a quoted section may contain a procedure call, and the procedure call may execute a different section of quoted code. This is possible because the stack frame belonging to each p-code procedure has its own PQ register.

Quoting enabled (/Of) is the compiler default. However, quoting makes compiled p-code difficult to read and debug. Therefore, you should use the disable-quoting option (/Of–) during program development. See “Fine-Tuning Your P-Code Program”.