While Alchemy supports compiling C++, it seems that using the using the STL is trouble, mostly due to a problem with std::string. What's strange is that Alchemy seems to be using GNU libstd++ v3.4.6. It's hard to believe that std::string is broken in GNU's STL.
Has anyone figured out any workarounds for this problem? C++ without the STL is like a fish without water.
The problem isn't in the STL as such. The GNU implementation of
std::string
is reference-counted using the thread-safe functions__gnu_cxx::__exchange_and_add
and__gnu_cxx::__atomic_add
. The problem is that__exchange_and_add
/__atomic_add
are broken.The solution is to rebuild the STL with good implementations of these functions.
Fortunately, the Alchemy distro leaves some breadcrumbs for us. See
$ALCHEMY_HOME/avm2-libc/README
, which tells us how to do it:Normally I'd expect to find the implementations of __exchange_and_add/__atomic_add in libstd++ (
$ALCHEMY_HOME/avm2-libc/lib/avm2-libstdc++.l.bc
), but for some reason they're defined in libc ($ALCHEMY_HOME/avm2-libc/lib/avm2-libc.l.bc
).I'm not sure why this is the case, but we can work around it by hacking atomicity.h, where the prototypes are kept. Note that after unpacking alchemy_gnucpp3-4library_121008.zip, you'll need to edit two atomicity.h files:
$ALCHEMY_HOME/avm2-libc/include/c++/3.4/bits/atomicity.h
$ALCHEMY_HOME/avm2-libc/libstdc++/include/bits/atomicity.h
Here is the code:
Here is some test code to run to make sure the rebuilt STL works:
Note that the rebuilt STL appears to fix some related issues with map and ifstream.