Is there a recipe for calling an inline Chapel routine which itself calls an inline C routine (which will actually have an __asm__ statement in it). I assume it will only work with clang which may give me grief when testing for floating point exceptions but we will cross that bridge when I come to it.
I agree with Vass — If the external C routine is defined as a static inline routine in its header file, for example, I believe it should be fully inlined using the pattern above.
Thanks. I did not RTFM properly. Once I used static in the C code, it worked. I do not quite understand what a staticinline piece of C is, but it delivers results and I assume it is inline'd and has no subroutine jump overhead. I still cannot figure out how to read the assembler produced by the Chapel compiler. Before I go playing around a
c_float
a C type, the equivalent of a real(32), for use in a C program or is it a Chapel type that matches a float for use in a Chapel program. The manual infers the former but I get errors when I try and use them. Is there some include file I am missing in the chunk of C code?
c_float is available for use in Chapel code. The static in the C code just means it's only for use within a C file (which is admittedly a bit weird since you are using it within Chapel code) but it's relatively normal in C code for static inline to go together.
I still cannot figure out how to read the assembler produced by the Chapel compiler.
I have been using inline routines for 35 years in C++ (albeit far less in C) but I have never combined that with static. Anyway, let's discuss Chapel and not my seemingly poor C knowledge. Why wont it compile until I put static in there? Just curious? My code is
(This post is my understanding -- I didn't try to compile these)
You'll get an error about needing a prototype if you don't have static. You can add static like this:
static int qfred(int j) { return turkey; };
or you can add a prototype like this:
int qfred(int j);
int qfred(int j) { return turkey; };
but the first of these communicates to the C compiler that it need not consider that the function will be called outside of the compilation unit. I think our interop documentation probably could be improved to clarify that it works as if these extern blocks end up in the same compilation unit as the Chapel module that they are contained within.
Thanks. Now Vass's email makes more sense. But I cannot understand why you need a prototype as well as the definition. That seems redundant. But I am sure I will learn why soon.
Anyway, I might ask a few more dumb questions later in the week and once I get my head around it, I might make some suggestions/enhancements regarding the said documentation. Having Discourse+Github means that the odd hole in the documentation gets plugged pretty quickly. The only hiccup is the weekend. And of course, for some us who have a separate day job, that is when we work on our Chapel research.
I cannot understand why you need a prototype as well as the definition.
Or maybe it is called "declaration" in addition to the definition. Frankly, I do not understand either and am not hoping to learn any time soon—this warning/error has been around for a long time so it is more of "a fact of life" to me.
My best guess is that you are supposed to use a given function in one of two ways. (A) locally to the current file, in which case you should mark it "static". (B) across compilation units, in which case you should put its declaration in a header file that will be used across multiple compilations. This declaration would be in addition to the definition that goes into a .c file. The warning shows up when your function does not fit into (A) or (B).
Thanks. All a lot clearer now. What was great was that once I got the definition/declaration/whatever right, it all just worked as it was designed to do. Thanks for the help. I will look at the description and see it I can make some suggestions on changes to wording that would have made things clearer to me.
So we have a C interoperability section in the Primer and a more comprehensive one in the Tech Notes that need to be addressed. Anywhere else? Where is the master of those pages. Is it chpldoc and if so, where do I find the latest documentation on that tool? Can I correct the English please while I am there?
I cannot understand why you need a prototype as well as the definition.
They're both needed for non-static functions because of the clang flag -Wmissing-prototypes, which comes from compiler/llvm/clangUtil.cpprunClang()clang_warn[]. So clang is warning about it because it's been asked to, and turning that warning into an error because -Werror tells it to.
With the -Wmissing-prototypes removed from clang_warn[] the example turkey code above doesn't hit that warning/error (though it does hit others).
Since it hasn't been spelled out here, the warnings/errors I'm talking about are the ones of the form
In file included from <built-in>:2:
foo.chpl:8:13: error: no previous prototype for function 'qfred' [-Werror,-Wmissing-prototypes]
int qfred(int j) { return turkey; };
^
foo.chpl:8:9: note: declare 'static' if the function is not intended to be used outside of this translation unit
int qfred(int j) { return turkey; };
^
static
The problem is worse than that. Without static, you get the error, i.e. not a warning,
/tmp/chpl-damianm.deleteme-qlPYgQ/chpl__module.o: In function `chpl_user_main':
root:(.text+0x7f70f): undefined reference to `roundabovef'
clang-11: error: linker command failed with exit code 1 (use -v to see invocation)
error: Make Binary - Linking
To generate online documentation, go to $CHPL_HOME and run make docs. This will run chpldoc as well. As it will tell you at the end, it will place the webpages in $CHPL_HOME/doc/html.
Yes, feel free to correct the English and whatever other glitches you come across.