5
$\begingroup$

Is there a way to extract the types of the arguments and the return type from a CompiledFunction expression? I am writing a package for code generation (basically a wrapper around CCodeGenerate) where the user supplies CompiledFunction expressions, and it would be nice if I could extract argument and return types directly from these expressions. (Otherwise the user has to specify them correctly, and that's a source of errors.)

$\endgroup$
1
  • 1
    $\begingroup$ When cf = Compile[...]; you can get the types of the arguments with cf[[2]]. The index 2 can be determined by inspecting FullForm[cf] and might be version dependent, so be sure to check carefully. $\endgroup$ Sep 18, 2014 at 11:47

1 Answer 1

5
$\begingroup$

The argument pattern can be read directly from the CompiledFunction expression as DaveStrider commented:

cf = Compile[{{x, _Real}, {y, _Integer}}, Round[x/y]];

cf[[2]]
{_Real, _Integer}

The result information is printed by the CompiledFunctionTools package command CompilePrint:

Needs["CompiledFunctionTools`"]

CompilePrint[cf]
      2 arguments
      2 Integer registers
      3 Real registers
      Underflow checking off
      Overflow checking off
      Integer overflow checking on
      RuntimeAttributes -> {}

      R0 = A1
      I0 = A2
      Result = I1

1 R1 = I0
2 R2 = Reciprocal[ R1]
3 R1 = R0 * R2
4 I1 = Round[ R1]
5 Return

Note that A1 and A2 are the arguments, assigned to Real and Integer registers respectively. The output is an Integer (I1). This information can be more directly read from ToCompiledProcedure:

ToCompiledProcedure[cf][[4]]
CompiledResult[Register[Integer, 1]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.