Related Technologies

Code generation from the Wolfram Language involves converting programs written in the Wolfram Language into other languages and then supporting them so that they can be executed. The Wolfram System compiler provides a system for code generation into the C language.

This section describes some technologies that are related to code generation.

The Wolfram System Compiler

The Wolfram System compiler is an important way both to speed up and also to work with Wolfram Language computations. It does this by taking assumptions about the computations and rewriting them in more efficient ways.

A key way to use the Wolfram System compiler is with the function (RefLink[Compile, paclet:ref/Compile]).

cFun=Compile[ {x},(x^2)+Sin[(x^2)]]

You can use the compiled function like any other Wolfram Language function, passing it real number input.

cFun[ 10.5]

However, if you pass the compiled function an input that is not a real number, an error message is returned. The Wolfram Language still returns a result, but has not used the compiler.

cFun[ sym]

More detailed information can be found in the compiler documentation.

SymbolicC

SymbolicC is a key part of the Wolfram Language's C code generation system. It is used automatically, when (RefLink[Export, paclet:ref/Export]) or (RefLink[ExportString, paclet:ref/ExportString]) to C code is used. It provides a hierarchical view of C code as Wolfram Language expressions. This makes it well suited to creating, manipulating, and optimizing C code.

You can use SymbolicC for your own code-generation purposes. It is also used extensively for the Wolfram Language's code generation tools.

To use SymbolicC, you first need to load the package.

Needs["SymbolicC`"]

Now you can start to build up elements of a C program. The following represents a variable declaration.

dec=CDeclare[ double,"a"]

An important feature of SymbolicC expressions is that they are inert; they evaluate to themselves, staying in an unevaluated form.

Here is an assignment and an entire function.

asgn = CAssign["a","x"];
fun=CFunction[void, "fun",{{double,x}},CBlock[{dec,asgn}]];

You can keep the function as an expression, or you can convert it into a string that shows the C code.

ToCCodeString[fun]

More detailed information can be found in the SymbolicC documentation.

CCompilerDriver

CCompilerDriver is a Wolfram Language package that gives an interface to C compilers installed on your computer. It can build shared libraries, executables, and object files. Since it is integrated with the Wolfram Language, it is easy to use it for creating C code and linking this back in with the Wolfram Language, for example, giving a convenient way to test the code.

Of course, you may choose to set up your own project or makefiles and not use the C Compiler Driver tools.

To use the package, it must first be loaded.

Needs["CCompilerDriver`"]

Now you can create a DLL from a file of C code.

This is a sample source file.

demoFile= FileNameJoin[ {$CCompilerDirectory,"SystemFiles","CSource","createDLL_demo.c"}];

This creates a shared library, returning the full path.

lib=CreateLibrary[{demoFile},"testDLL"]

If you do not have any suitable C compiler, the compilation will fail and a message will be generated.

The sample source file can work as a Wolfram Library and can be loaded with DLLFunctionLoad.

libFun=Compile`DLLFunctionLoad[lib,"incrementInteger",{{_Integer}},_Integer]

You can call the library function from the Wolfram Language.

libFun[10]

More detailed information can be found in the CCompilerDriver documentation.