This is documentation for Mathematica 5, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)

Documentation / Mathematica / The Mathematica Book / Principles of Mathematica / Evaluation of Expressions /

2.6.16 Advanced Topic: 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.

The structure of a compiled code object.

This shows the explicit form of the compiled code generated by Compile.

In[1]:= Compile[{x}, x^2] // InputForm

Out[1]//InputForm= CompiledFunction[{_Real}, {{3, 0, 0}, {3, 0, 1}}, {0, 0, 2, 0, 0}, {{1, 5}, {29, 0, 0, 1}, {2}}, Function[{x}, x^2], Evaluate]

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.