diff --git a/breed/breed.sh b/breed/breed.sh new file mode 100755 index 0000000..7f1b524 --- /dev/null +++ b/breed/breed.sh @@ -0,0 +1,85 @@ +#!/bin/bash -ex + +WASI_SDK_PATH=/opt/wasi-sdk +CC=${WASI_SDK_PATH}/bin/clang + +cat >w2c2_main.c < +#include "w2c2_base.h" +#include "wasi.h" +#include "w2c2.h" + +void +trap( + Trap trap +) { + fprintf(stderr, "TRAP: %s\n", trapDescription(trap)); + abort(); +} + +wasmMemory* +wasiMemory( + void* instance +) { + return w2c2_memory((w2c2Instance*)instance); +} + +extern char** environ; + +/* Main */ + +int main(int argc, char* argv[]) { + /* Initialize WASI */ + if (!wasiInit(argc, argv, environ)) { + fprintf(stderr, "failed to init WASI\n"); + return 1; + } + + if (!wasiFileDescriptorAdd(-1, "/", NULL)) { + fprintf(stderr, "failed to add preopen\n"); + return 1; + } + + { + w2c2Instance instance; + w2c2Instantiate(&instance, NULL); + w2c2__start(&instance); + w2c2FreeInstance(&instance); + } + + return 0; +} +END + +if ! [ -d build ]; then + mkdir build + cd build + mkdir w2c2 + cd w2c2 + CFLAGS="-Wl,--stack-first -Wl,-z,stack-size=1048576" cmake ../../../w2c2 -DCMAKE_TOOLCHAIN_FILE=${WASI_SDK_PATH}/share/cmake/wasi-sdk.cmake -DCMAKE_BUILD_TYPE=Debug + make + cd .. + mkdir wasi + cd wasi + CFLAGS="-Wl,--stack-first -Wl,-z,stack-size=1048576" cmake ../../../wasi -DCMAKE_TOOLCHAIN_FILE=${WASI_SDK_PATH}/share/cmake/wasi-sdk.cmake -DCMAKE_BUILD_TYPE=Debug + make + cd ../.. +fi + +rm -rf silly +mkdir -p silly/gen1 +cp build/w2c2/w2c2 silly/gen1/w2c2.wasm + +function breed() { + genX=gen$1 + genY=gen$(($1 + 1)) + echo "=======> breeding ${genX} into ${genY}" >&2 + + mkdir -p silly/${genY} + wasmtime run --dir . "silly/${genX}/w2c2.wasm" "silly/${genX}/w2c2.wasm" "silly/${genY}/w2c2.c" + ${CC} -I../w2c2 -I../wasi -Isilly/${genY} build/wasi/libw2c2wasi.a silly/${genY}/w2c2.c w2c2_main.c -o silly/${genY}/w2c2.wasm +} + +for gen in $(seq 1 10); do + breed "$gen" +done diff --git a/tests/Makefile b/tests/Makefile index 52750f5..714d211 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -69,4 +69,4 @@ gen-%: $(PYTHON3) gen.py $* clean: - -rm -f main.o test_*.o + -rm -f main.o test_*.o test_*.h diff --git a/w2c2/CMakeLists.txt b/w2c2/CMakeLists.txt index c5f27fa..dafb342 100644 --- a/w2c2/CMakeLists.txt +++ b/w2c2/CMakeLists.txt @@ -11,7 +11,7 @@ set(CMAKE_C_STANDARD 90) set(THREADS_PREFER_PTHREAD_FLAG ON) find_package(Threads) -if(NOT DWARF_FOUND) +if((NOT DWARF_FOUND) AND (NOT CMAKE_CROSSCOMPILING)) find_package(PkgConfig) if(PKG_CONFIG_FOUND) pkg_check_modules(DWARF libdwarf) diff --git a/w2c2/c.c b/w2c2/c.c index 5961f78..bbaae5e 100644 --- a/w2c2/c.c +++ b/w2c2/c.c @@ -556,6 +556,7 @@ typedef struct WasmCFunctionWriter { const char* moduleName; WasmFunction function; Buffer* code; + U8* codeStart; U32 indent; bool ignore; bool pretty; @@ -4425,6 +4426,7 @@ wasmCWriteFunctionBody( writer.moduleName = moduleName; writer.function = function; writer.code = &code; + writer.codeStart = code.data; writer.indent = 0; writer.ignore = false; writer.pretty = pretty; diff --git a/w2c2/stringbuilder.c b/w2c2/stringbuilder.c index bd4eb40..c7cd227 100644 --- a/w2c2/stringbuilder.c +++ b/w2c2/stringbuilder.c @@ -113,26 +113,9 @@ stringBuilderAppendU32( StringBuilder* stringBuilder, U32 value ) { - char temp[10]; - char* tempPointer = temp; - char* buffer = NULL; - - MUST (stringBuilderEnsureCapacity(stringBuilder, stringBuilder->length + 11)) - buffer = stringBuilder->string + stringBuilder->length; - - do { - *tempPointer++ = (char)(value % 10) + '0'; - value /= 10; - } while (value > 0); - - do { - *buffer++ = *--tempPointer; - stringBuilder->length++; - } while (tempPointer != temp); - - *buffer = '\0'; - - return true; + char buffer[11]; + const int length = sprintf(buffer, "%u", value); + return stringBuilderAppendSized(stringBuilder, buffer, (size_t) length); } bool @@ -140,15 +123,9 @@ stringBuilderAppendI32( StringBuilder* stringBuilder, const I32 value ) { - U32 unsignedValue = (U32)value; - if (value < 0) { - MUST (stringBuilderEnsureCapacity(stringBuilder, stringBuilder->length + 1)) - stringBuilder->string[stringBuilder->length] = '-'; - stringBuilder->length++; - - unsignedValue = ~unsignedValue + 1; - } - return stringBuilderAppendU32(stringBuilder, unsignedValue); + char buffer[12]; + const int length = sprintf(buffer, "%i", value); + return stringBuilderAppendSized(stringBuilder, buffer, (size_t) length); } bool @@ -156,26 +133,9 @@ stringBuilderAppendU64( StringBuilder* stringBuilder, U64 value ) { - char temp[20]; - char* tempPointer = temp; - char* buffer = NULL; - - MUST (stringBuilderEnsureCapacity(stringBuilder, stringBuilder->length + 21)) - buffer = stringBuilder->string + stringBuilder->length; - - do { - *tempPointer++ = (char)(value % 10) + '0'; - value /= 10; - } while (value > 0); - - do { - *buffer++ = *--tempPointer; - stringBuilder->length++; - } while (tempPointer != temp); - - *buffer = '\0'; - - return true; + char buffer[21]; + const int length = sprintf(buffer, "%llu", value); + return stringBuilderAppendSized(stringBuilder, buffer, (size_t) length); } bool @@ -183,15 +143,9 @@ stringBuilderAppendI64( StringBuilder* stringBuilder, const I64 value ) { - U64 unsignedValue = (U64)value; - if (value < 0) { - MUST (stringBuilderEnsureCapacity(stringBuilder, stringBuilder->length + 1)) - stringBuilder->string[stringBuilder->length] = '-'; - stringBuilder->length++; - - unsignedValue = ~unsignedValue + 1; - } - return stringBuilderAppendU64(stringBuilder, unsignedValue); + char buffer[22]; + const int length = sprintf(buffer, "%lli", value); + return stringBuilderAppendSized(stringBuilder, buffer, (size_t) length); } bool