This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | |||
tutorial_external_function [2017/04/19 13:25] waruna [Example] |
tutorial_external_function [2023/05/30 07:37] (current) lnarmour writeC and verification shouldn't be used together |
||
---|---|---|---|
Line 24: | Line 24: | ||
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: | <sxh alphabets; gutter: | ||
- | 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 45: | Line 45: | ||
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: | <sxh c; gutter: | ||
//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 '' | ||
Line 65: | Line 63: | ||
<sxh c; gutter: | <sxh c; gutter: | ||
//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! |