Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow bootstrapping #107

Merged
merged 6 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions breed/breed.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/bin/bash -ex

WASI_SDK_PATH=/opt/wasi-sdk
CC=${WASI_SDK_PATH}/bin/clang

cat >w2c2_main.c <<END
#include <stdio.h>
#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
2 changes: 1 addition & 1 deletion tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,4 @@ gen-%:
$(PYTHON3) gen.py $*

clean:
-rm -f main.o test_*.o
-rm -f main.o test_*.o test_*.h
2 changes: 1 addition & 1 deletion w2c2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions w2c2/c.c
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ typedef struct WasmCFunctionWriter {
const char* moduleName;
WasmFunction function;
Buffer* code;
U8* codeStart;
U32 indent;
bool ignore;
bool pretty;
Expand Down Expand Up @@ -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;
Expand Down
70 changes: 12 additions & 58 deletions w2c2/stringbuilder.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,85 +113,39 @@ 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
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
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
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
Expand Down
Loading