Manipulating Compiled Code
If you use compiled code created by Compile
only within Mathematica
itself, then you should never need to know the details of its internal form. Nevertheless, the compiled code can be represented by an ordinary Mathematica
expression, and it is sometimes useful to manipulate it.
For example, you can take compiled code generated by Compile
, and feed it to external programs or devices. You can also create CompiledFunction
objects yourself, then execute them in Mathematica
In all of these cases, you need to know the internal form of CompiledFunction
objects. The first element of a CompiledFunction
object is always a list of patterns which specifies the types of arguments accepted by the object. The fifth element of a CompiledFunction
object is a Mathematica
pure function that is used if the compiled code instruction stream fails for any reason to give a result.
|compiled code taking arguments of type argi and executing the instruction stream instr using nk registers of type k|
The structure of a compiled code object.
This shows the explicit form of the compiled code generated by Compile
The instruction stream in a CompiledFunction
object consists of a list of instructions for a simple idealized computer. The computer is assumed to have numbered "registers", on which operations can be performed. There are five basic types of registers: logical, integer, real, complex and tensor. For each of these basic types it is then possible to have either a single scalar register or an array of registers of any rank. A list of the total number of registers of each type required to evaluate a particular CompiledFunction
object is given as the second element of the object.
The actual instructions in the compiled code object are given as lists. The first element is an integer "opcode" which specifies what operation should be performed. Subsequent elements are either the numbers of registers of particular types, or literal constants. Typically the last element of the list is the number of a "destination register", into which the result of the operation should be put.