This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
tutorial_external_function [2014/05/30 11:49] 127.0.0.1 external edit |
tutorial_external_function [2023/05/30 07:37] (current) lnarmour writeC and verification shouldn't be used together |
||
---|---|---|---|
Line 23: | Line 23: | ||
====Example==== | ====Example==== | ||
Let's take a simple example with two external functions. One is a function that computes square root, and another is some user function that returns float given an integer. | Let's take a simple example with two external functions. One is a function that computes square root, and another is some user function that returns float given an integer. | ||
- | <sxh alphabets; gutter:false> | + | <sxh alphabets; gutter:true> |
- | float sqrt(float); | + | double |
- | float userfunc(int); | + | double |
affine exFuncExample {N|N>0} | affine exFuncExample {N|N>0} | ||
given | given | ||
- | float A {i|0< | + | double |
returns | returns | ||
- | float C {i|0< | + | double |
through | through | ||
- | C[i] = sqrt(A[i]) + userfunc([i]); | + | C[i] = sqrt(A[i]) + userfunc([i]); |
// must be an alphabets expression, and we are constructing one with | // must be an alphabets expression, and we are constructing one with | ||
// the IndexExpr rule of the grammar. | // the IndexExpr rule of the grammar. | ||
Line 40: | Line 40: | ||
Generate code with the following script. | Generate code with the following script. | ||
- | <sxh cs; gutter:false> | + | <sxh cs; gutter:true> |
prog = ReadAlphabets(" | prog = ReadAlphabets(" | ||
system = " | system = " | ||
- | generateWriteC(prog, system, " | + | generateScheduledCode(prog, system, " |
generateWrapper(prog, | generateWrapper(prog, | ||
generateMakefile(prog, | generateMakefile(prog, | ||
</ | </ | ||
- | Along with the two C codes for '' | + | Along with the two C codes for '' |
- | <sxh c; gutter:false> | + | <sxh c; gutter:true> |
//External functions | //External functions | ||
- | / | + | double |
- | float sqrt(float); | + | double |
- | float userfunc(int); | + | |
- | / | + | |
</ | </ | ||
- | Code surrounded by PROTECTED REGION | + | Code is generated when the file does not exist, but will be unchanged in subsequent generations.\\ |
\\ | \\ | ||
Now we need to provide implementations for these functions. The first function is actually in the C math library, which is linked by default. Thus we do not need to add body for this function. However, the declaration here will cause conflict with the other declaration of '' | Now we need to provide implementations for these functions. The first function is actually in the C math library, which is linked by default. Thus we do not need to add body for this function. However, the declaration here will cause conflict with the other declaration of '' | ||
\\ | \\ | ||
The other function needs a body, and it can be anything as long as it returns a floating point value. An example of the completed header file is shown below. | The other function needs a body, and it can be anything as long as it returns a floating point value. An example of the completed header file is shown below. | ||
- | <sxh c; gutter:false> | + | <sxh c; gutter:true> |
//External functions | //External functions | ||
- | / | + | //double |
- | //float sqrt(float); | + | double |
- | float userfunc(int in) { | + | |
| | ||
} | } | ||
- | / | ||
</ | </ | ||
Now try compiling and check if the code works! | Now try compiling and check if the code works! |