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.)
-
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$– DaveStriderSep 18, 2014 at 11:47
1 Answer
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]]