CodeGeneration Options - Maple Help

CodeGeneration Options

 This help page describes the options used by the language translation commands in the CodeGeneration package.

Options

 coercetypes=value
 Specifies whether type coercion is to be performed.  Permissible values are true and false.  The default value is true.  When the option coercetypes=true (or simply coercetypes) is given, then an explicit coercion from one data type to another is generated wherever appropriate.  Constants are automatically converted to the required type.
 declare=[declaration(s)]
 Specifies the types of variables.  Each declaration has the form $\mathrm{varname}::\mathrm{vartype}$ where varname is a variable name and vartype is one of the Maple type names recognized by CodeGeneration, as described in TranslationDetails. Declarations specified using this option override any other type declarations within the input code.  The declare option is designed to be used with procedures and computation sequences only.
 deducereturn=value
 Specifies whether deduction of explicit returns should be attempted. Permissible values are true and false.  The default value is true. When the option deducereturn=true (or simply deducereturn) is given, the translator looks for an implicit return and if one is found, converts it to an explicit return.
 deducetypes=value
 Specifies whether automatic type deduction is to be performed.  Permissible values are true and false.  The default value is true. When the option deducetypes=true (or simply deducetypes) is given, the translator attempts to deduce the type of a variable based on the context in which it is found; otherwise, the variable is assigned a default type. If the option deducetypes=false is given, then all untyped variables are assigned the default type.
 defaulttype=value
 Specifies the default type. Any Maple type is accepted as a default type. It is mapped to the more limited set of types accepted by CodeGeneration, choosing the minimal type of which the given is a subtype if possible. Otherwise, if a type is not recognized by CodeGeneration, as described in TranslationDetails, then the default type is assumed. For example, float goes to numeric, posint goes to integer, and symbol goes to double.
 digits=value
 Specifies the number of digits used in the floating-point evaluation of known constants.  The value must be a positive integer and the default is the current setting of Digits. For some target languages, a symbolic constant is used in place of a literal float, and the digits option does not affect the accuracy of the symbols definition in that target.
 expandarrays=value
 Specifies how to translate the assignment of arrays.  (Here, array is used to mean a variety of Maple data structures, as described in TranslationDetails.) Permissible values are true and false.  The default value is true. This option applies to languages that do not have built-in support for array copying.  When the option expandarrays=true (or simply expandarrays) is given, element-wise assignments are generated from the input statement $x:=y\text{'}\text{'}$, where $x$ and $y$ are both arrays.  If the option expandarrays=false is given, then a simple assignment of y to x is generated, and the user must replace this statement with the appropriate code for array assignments in the target language, if necessary.
 functionprecision=value
 Specifies the numeric precision of expressions that appear as arguments and return values of functions in the input. Permissible values are single and double. The CodeGeneration[Fortran] function also accepts generic.  The default value is generic for Fortran and double for all other CodeGeneration functions. This option affects only the translation of function names and not the precision of variables.
 libraryorder=value
 Specifies how conflicts are to be resolved when the target language defines multiple function translations for a particular type signature.  The value for libraryorder is a list of strings, each string corresponding to a library.  If there are multiple available function translations available, CodeGeneration will choose the translation associated with the library appearing first in the libraryorder list.
 limitvariablelength=value
 Specifies whether variable names longer than 6 characters will be replaced. Permissible values are true and false.  The default value is true.  This option is available for the CodeGeneration[Fortran] command only.  The Fortran 77 standard requires that all variable names be no longer than 6 characters.  Use the limitvariablelength=false option to prevent the automatic replacement of long names.
 optimize=value
 Specifies whether optimization should be performed.  Permissible values are true,false, and tryhard.  The default value is false. When the option optimize=true (or simply optimize) is given, the Maple input is optimized using codegen[optimize] before it is translated. When the option optimize=tryhard is given, the Maple input is optimized using a stronger internal optimizer. For more information about how the optimization is performed, see TranslationDetails. The optimize option is designed to be used with procedures and computation sequences only.
 output=value
 Specifies the form of the output.  The value can be one of the symbols embed, string, terminal, or any Maple string.  The default value is terminal, which results in formatted output being printed to the terminal.  The option output=embed displays the formatted output in the document inside a text field.  If the option output=string is provided, then a string containing the result is returned.  This string may then be assigned and manipulated. If a string is given as the value, then the result is appended to a file having that name.
 precision=value
 Specifies the precision used for floating-point variables and constants. Permissible values are single and double. The default value is double.
 reduceanalysis=value
 Specifies whether a reduced form of analysis should be performed.  Permissible values are true and false.  The default value is false. This option is applicable only when the Maple code to be translated is a list of equations representing a computation sequence; the option cannot be used with modules and procedures. When the option reduceanalysis=true (or simply reduceanalysis) is given, the computation sequence is analyzed and translated one line at a time.  In contrast, the default behavior is to analyze the entire computation sequence as a whole, so that any type information collected during analysis of one assignment is applied to analysis of subsequent assignments.  Applying a full analysis means that the translated code is less likely to contain type mismatches; however, using the reduceanalysis=true option can significantly reduce the time and memory required for translation.
 resultname=value
 Specifies the name of the result (that is, the generated procedure or the left-hand-side variable in the generated assignment) when the input is unnamed.  The value can be any string.  When no result name is specified or deduced, an automatically generated name is used.
 returnvariablename=value
 Specifies the name of the return variable used in translated procedures. It is used only when it is necessary to generate a new variable to hold the return value.  The value can be any string.  When no return variable name is specified, an automatically generated name is used.
 strict=value
 Specifies the tolerance for errors encountered during translation. The default value of 0 specifies that when there is no translation available for a intermediate code expression or a named function, CodeGeneration should issue a warning and insert a placeholder in the generated code.  Setting strict=1 causes CodeGeneration to instead issue an error and abort the translation in this case.

Examples

Most of the following examples use the CodeGeneration[C] function, but the options are available with all the other CodeGeneration functions, unless it is stated otherwise in the Options section above.

 > $\mathrm{with}\left(\mathrm{CodeGeneration}\right):$

The 'coercetypes' Option

In the first call below, the variable y is coerced to a floating-point type.  This coercion is not generated in the second call.

 > $C\left(\left[z=x+y\right],\mathrm{declare}=\left[x::\mathrm{numeric},y::'\mathrm{integer}'\right]\right)$
 z = x + (double) y;
 > $C\left(\left[z=x+y\right],\mathrm{declare}=\left[x::\mathrm{numeric},y::'\mathrm{integer}'\right],\mathrm{coercetypes}=\mathrm{false}\right)$
 z = x + y;

The 'declare' Option

In the following example, $x$ is assigned a floating-point number because it is declared to have type float.  Otherwise, the translator would have deduced that it has type integer and generated an assignment to the integer 1.

 > $C\left(\left[x=1\right],\mathrm{declare}=\left[x::\mathrm{float}\right]\right)$
 x = 0.1e1;

The 'deducereturn' Option

The following Maple procedure returns the value 10 when invoked, but translation with the option deducereturn=false creates a C function with a void return type.

 > f := proc(x::Vector(4), n::integer) local i; for i to 4 do x[i] := 10 end do; end proc:
 > $C\left(f,\mathrm{deducereturn}=\mathrm{false}\right)$
 void f (int x[4], int n) {   int i;   for (i = 1; i <= 4; i++)     x[i - 1] = 10; }

The 'deducetypes' Option

In the following example, $x$ is given the default type, numeric, and so it is assigned a floating-point number.  Otherwise, the translator would have deduced that it has type integer and generated an assignment to the integer 1.

 > $C\left(\left[x=1\right],\mathrm{deducetypes}=\mathrm{false}\right)$
 x = 0.1e1;

The 'defaulttype' Option

In the following example, the untyped variables are given the type integer instead of numeric.  Thus the translation produces C declarations with type int rather than double.

 > f := proc(x) return x; end proc:
 > $C\left(f,\mathrm{defaulttype}=\mathrm{integer}\right)$
 int f (int x) {   return(x); }

The 'digits' Option

The following call shows how the digits option affects the translation of known constants.

 > $C\left(\mathrm{\pi },\mathrm{digits}=20\right)$
 cg = 0.31415926535897932385e1;

The 'expandarrays' Option

In the following example, the variable $\mathrm{cgret}$ is automatically created to hold the return values.  In the first call, statements to perform element-wise copying are generated.  In the second call, the assignment involves only the array names.  In this case, you must replace the assignments $\mathrm{cgret}=x\text{'}\text{'}$ and $\mathrm{cgret}=y\text{'}\text{'}$ in the output with appropriate C statements (for example, calls to a user-defined C function).

 > f := proc(x::Vector(2), y::Vector(2), z) if z > 0 then x else y end if; end proc:
 > $C\left(f\right)$
 void f (   double x[2],   double y[2],   int z,   double cgret[2]) {   if (0 < z)   {     cgret[0] = x[0];     cgret[1] = x[1];   }   else   {     cgret[0] = y[0];     cgret[1] = y[1];   } }
 > $C\left(f,\mathrm{expandarrays}=\mathrm{false}\right)$
 void f (   double x[2],   double y[2],   int z,   double cgret[2]) {   if (0 < z)     cgret = x;   else     cgret = y; }

The 'functionprecision' Option

The following example specifies that double precision Fortran function names are to be used instead of generic function names.

 > $\mathrm{Fortran}\left(\mathrm{sin}\left(x\right),\mathrm{functionprecision}=\mathrm{double},\mathrm{resultname}=w\right)$
 w = dsin(x)

The 'optimize' Option

In the following example, the computation sequence is optimized before it is translated.

 > $C\left(\left[x=yz,w=5.0yz\right],\mathrm{optimize}\right)$
 x = y * z; w = 0.50e1 * x;

The 'output' Option

In the following example, the result is put into a string and returned.

 > $s≔C\left(\left[y=\mathrm{sin}\left(x\right)\right],\mathrm{output}=\mathrm{string}\right)$
 ${s}{≔}{"y = sin\left(x\right);"}$ (1)

The 'precision' Option

The following example specifies that single precision variables and constants are to be generated.

 > f := proc(x) return x+2.0; end proc:
 > $C\left(f,\mathrm{precision}=\mathrm{single}\right)$
 float f (float x) {   return(x + 0.20e1f); }

The 'reduceanalysis' Option

In the first call below, full analysis is applied. The translator recognizes, in analyzing the second assignment, that the variable $x$ has been given an integer type and includes the necessary type coercion. In the second call, each assignment is analyzed and translated on its own, with no knowledge retained from analysis of previous assignments.

 > $s≔\left[x=1,y=x+2.0\right]:$
 > $\mathrm{CodeGeneration}\left[C\right]\left(s\right)$
 x = 1; y = (double) x + 0.20e1;
 > $\mathrm{CodeGeneration}\left[C\right]\left(s,\mathrm{reduceanalysis}\right)$
 x = 1; y = x + 0.20e1;

The 'resultname' Option

In the following example, the result is assigned to a variable with the specified name instead of an automatically generated name.

 > $C\left({x}^{y},\mathrm{resultname}="myresult"\right)$
 myresult = pow(x, y);

The 'returnvariablename' Option

In the following example, the return variable is given the specified name instead of an automatically generated name.

 > f := proc(n) local i; for i to n do i*n end do end proc:
 > $C\left(f,\mathrm{returnvariablename}="myreturn"\right)$
 int f (int n) {   int i;   int myreturn;   for (i = 1; i <= n; i++)     myreturn = i * n;   return(myreturn); }

The 'strict' Option

The following example illustrates the use of the strict setting.

 > $\mathrm{CodeGeneration}\left[C\right]\left(\mathrm{unknownFunction}\left(x\right)\right)$
 cg0 = unknownFunction(x);
 > $\mathrm{CodeGeneration}\left[C\right]\left(\mathrm{unknownFunction}\left(x\right),\mathrm{strict}=1\right)$

Compatibility

 • The optimize option was updated in Maple 15.
 • The libraryorder option was introduced in Maple 18.