How to forbid the riscv compressed codes? - compression

When I use the riscv64-unknown-elf-gcc, there is few differences between option -march=rv64g and -march=rv64gc.
-march=64g will use RVC codes in standard library functions, for example, the prinft, as much as possible but not in my own functions. While the -march=64gc, use the RVC codes in both types of functions.
I don't know whether this is default. But if I want to forbid the RVC codes so that even in the standard library functions without the RVC codes, what should I do?

Recompile the riscv-gnu-tool with option --with-arch=rv64g --disable-multilib

Related

How to check at compile time, whether the C++Standard Library(STL) is supported

I'm abstracting the interrupt handling on multiple microcontrollers.
The ARM Cortex M-3/4 ones do greatly support the STL, but the
ATMega328p (which are greatly used) do not support the C++ STL.
I'd like to still offer the possibility to register callbacks for specific interrupts utilizing the std::function additionally to the basic C-style function pointer.
I haven't tested it yet, but was told that the C++ lambdas can do a lot of stuff compared to the simple function pointers, especially when it comes to capturing variables and references.
I'd like to change the defining header class(if necessary) and the implementing classes(if necessary, might not be necessary, because the implementation will simply be missing) in order to remove the member function definition at compile time depending on whether the controller supports or doesn't support the STL.
I was wondering if C++ offers such a test or if it's somehow possible to do this by including an STL header and somehow test, whether the include failed or not?
...
#include <functional>
class InterruptVectorTable
{
bool setCallback(ValueType InterruptNumber, void (*Callback)(void));
bool setCallback(ValueType InterruptNumber, std::functional<void(void)> Callback);
}
If both definitions are not allowed at the same time, then I'd like to preferably use the std::function.
The new C++17 standard has the __has_include functionality you are looking for.
However, it is a bit of a chicken and the egg problem. If your platform does not support STLs then it is likely to be to out of date for c++17.
Instead you likely want to rely on a your build system. For instance, cmake can detect which C++ standard a compiler honors, or even which part of a standard is implemented.
In your case, it might be simpler to have the build define an ad-hoc pre-processor constant HAS_STD_CPP if the standard lib is available on that platform, and then use the c preprocessor to include the STL bits or not, as illustrated in https://en.wikipedia.org/wiki/C_preprocessor.

Two libraries which can expand each other

So I'm working on an Modular C++ library for me and my team. Here is my situation:
I have a library A which contains a complex storage control class a. I have also a library B that is something like a interface to an complex protocol which contains an special response. Now, I want to have a function in class a, which CAN use B. This can be helpful for Program X, which uses A and B. But there is also Program Y, which will only use A and not the complex library B.
How can I get this behavior in C++? Do I need macros, symbols or is there an other way to implement this easily so that I don't need to include an extra file in a Program? Which type of library is the better one?
This is pretty common in system libraries, where optional features can be chosen at (library) compile time. With this approach, you would have one or more #define preprocessor macros guarding the optional features. Thus, in library A:
#ifdef USE_LIBRARY_B
#include <b/foo.h>
int A_optional_feature_using_B(void) { ... }
#endif
// rest of library A follows
At compile time you would either define USE_LIBRARY_B or not (and add any necessary linker flags of course). For example, with gcc on a UNIX-like platform:
$ gcc ... -DUSE_LIBRARY_B ...
Most commonly, something like autoconf is used for UNIX environments to give the end-user an easy way to select the optional features. Thus, you might see:
me#pc:library_a$ ./configure --with-library-b
See the autoconf documentation for information on how to set something like that up.
On Windows, you could do something similar (e.g., in Visual Studio, use different Project/Solution configurations that define the appropriate macros).
The above gives you compile-time only control over the extra features. To allow the choice to be made at runtime so that the library can use B if present, you will need to use runtime dynamic linking. This is highly platform-specific. On Linux, use libdl. On Windows, use LoadLibrary() and friends. Here you will have to do a lot of extra coding to look for and load the optional library if present and to return appropriate error codes / exceptions if not present and functions requiring the optional library are called.
If all this sounds like a pain in the rear, you're right, it is. If there is any reasonable way to adjust your strategy so that you don't have to do this, you will be better off. But for some use cases this is necessary and appropriate and if yours is so, then I hope the above gives you some starting points, and good luck.

Define C++ function at runtime

I'm trying to adjust some mathematical code I've written to allow for arbitrary functions, but I only seem to be able to do so by pre-defining them at compile time, which seems clunky. I'm currently using function pointers, but as far as I can see the same problem would arise with functors. To provide a simplistic example, for forward-difference differentiation the code used is:
double xsquared(double x) {
return x*x;
}
double expx(double x) {
return exp(x);
}
double forward(double x, double h, double (*af)(double)) {
double answer = (af(x+h)-af(x))/h;
return answer;
}
Where either of the first two functions can be passed as the third argument. What I would like to do, however, is pass user input (in valid C++) rather than having to set up the functions beforehand. Any help would be greatly appreciated!
Historically the kind of functionality you're asking for has not been available in C++. The usual workaround is to embed an interpreter for a language other than C++ (Lua and Python for example are specifically designed for being integrated into C/C++ apps to allow scripting of them), or to create a new language specific to your application with your own parser, compiler, etc. However, that's changing.
Clang is a new open source compiler that's having its development by Apple that leverages LLVM. Clang is designed from the ground up to be usable not only as a compiler but also as a C++ library that you can embed into your applications. I haven't tried it myself, but you should be able to do what you want with Clang -- you'd link it as a library and ask it to compile code your users input into the application.
You might try checking out how the ClamAV team already did this, so that new virus definitions can be written in C.
As for other compilers, I know that GCC recently added support for plugins. It maybe possible to leverage that to bridge GCC and your app, but because GCC wasn't designed for being used as a library from the beginning it might be more difficult. I'm not aware of any other compilers that have a similar ability.
As C++ is a fully compiled language, you cannot really transform user input into code unless you write your own compiler or interpreter. But in this example, it can be possible to build a simple interpreter for a Domain Specific Language which would be mathematical formulae. All depends on what you want to do.
You could always take the user's input and run it through your compiler, then executing the resulting binary. This of course would have security risks as they could execute any arbitrary code.
Probably easier is to devise a minimalist language that lets users define simple functions, parsing them in C++ to execute the proper code.
The best solution is to use an embedded language like lua or python for this type of task. See e.g. Selecting An Embedded Language for suggestions.
You may use tiny C compiler as library (libtcc).
It allows you to compile arbitrary code in run-time and load it, but it is only works for C not C++.
Generally the only way is following:
Pass the code to compiler and create shared object or DLL
Load this Shared object or DLL
Use function from this shared object.
C++, unlike some other languages like Perl, isn't capable of doing runtime interpretation of itself.
Your only option here would be to allow the user to compile small shared libraries that could be dynamically-loaded by your application at runtime.
Well, there are two things you can do:
Take full advantage of boost/C++0x lambda's and to define functions at runtime.
If only mathematical formula's are needed, libraries like muParser are designed to turn a string into bytecode, which can be seen as defining a function at runtime.
While it seems like a blow off, there are a lot of people out there who have written equation parsers and interpreters for c++ and c, many commercial, many flawed, and all as different as faces in a crowd. One place to start is the college guys writing infix to postfix translators. Some of these systems use paranthetical grouping followed by putting the items on a stack like you would find in the old HP STL library. I spent 30 seconds and found this one:
http://www.speqmath.com/tutorials/expression_parser_cpp/index.html
possible search string:"gcc 'equation parser' infix to postfix"

Parsing c++ function headers from a file using GNU toolchain

I need to parse function headers from a .i file used by SWIG which contains all sorts of garbage beside the function headers. (final output would be a list of function declarations)
The best option for me would be using the GNU toolchain (GCC, Binutils, etc..) to do so, but i might be missing an easy way of doing it with SWIG. If I am please tell me!
Thanks :]
edit: I also don't know how to do that with GCC toolchain, if you have an idea it will be great.
I would try getting an XML dump of the abstract syntax tree either from clang or from gccxml. From there you can easily extract the function declarations you are interested in.
Our DMS Software Reengineering Toolkit provides general purpose program parsing, analysis, and transformation capability. It has front ends for a wide variety of languages, including C++.
It has been used to analyze and transforms very complex C++ programs and their header files.
You aren't clear as to what you will do after you "parse the function headers"; normally people want to extract some information or produce another artifact. DMS with its C++ front end can do the parsing; you can configure DMS to do the custom stuff.
As a practical matter, this isn't usually an afternoon's exercise; DMS is a complex beast, because it has to deal with complex beasts such as C++. And I'd expect you to face the same kind of complexity for any tool that can handle C++. The GCC toolchain can clearly handle C++, so you might be able to do it with that (at that same level of complexity) but GCC is designed to be a compiler, and IMHO you will find it a fight to get it do what you want.
Your "output function declarations" goal isn't clear. You want just the function names? You want a function signature? You want all the type declarations on which the function depends? You want all the type declarations on which the function depends, if they are not already present in an existing include file you intend to use?
The best way to extract function decls from the garbage which is C header files is to substitute out what constitutes the most smelly garbage: macros. You can do that with:
cpp - The C Preprocessor

tempnam equivalent in C++

I need to generate random names which I'll be using to create temporary files in a directory. Currently I am using C standard function tempnam() for this. My code is in C++ and would like to use C++ equivalent for doing the same task. The code needs to work on Solaris as well as on Windows.
Is anyone aware of such thing in C++? Any pointer on this would be highly appreciated.
Try std::tempnam in the cstdio header. ;)
The C standard library is still available in C++ code. For convenience, they provide C++ wrappers (in headers with the 'c' prefix, and no extension), and available in the std namespace.
You can also use the plain C version (stdio.h and tempnam in the global namespace, but you did ask for the C++ version ;))
The C++ standard library only provides new functions when there's actually room for improvement. It has a string class, because a string class is an improvement over char pointers as C has. It has a vector class, because, well, it's useful.
For something like tempnam, what would C++ be able to bring to the party, that we didn't already have from C? So they didn't do anything about it, other than making the old version available.
I know this doesn't answer your question but as a side note, according to the man page:
Although tempnam(3) generates names
that are difficult to guess, it is
nevertheless possible that between the
time that tempnam(3) returns a
pathname, and the time that the
program opens it, another program
might create that pathname using
open(2), or create it as a symbolic
link. This can lead to security
holes. To avoid such possibilities,
use the open(2) O_EXCL flag to open
the pathname. Or better yet, use
mkstemp(3) or tmpfile(3).
Why not just using the same function you are currently using in C? C++ is backward compatible with C.
What's wrong with tempnam()? You can use regular libc function right? tempnam is in stdio.h, which you're likely already including.
#include <cstdio>
using std::tmpnam;
using std::tmpfile;
You should also check this previous question on StackOverflow and avoid race conditions on creating the files using mkstemp, so I would recommend using std::tmpfile