Skip to content

Commit

Permalink
Merge pull request #107 from turbolent/bootstrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent authored Oct 15, 2024
2 parents cb1348a + 0d87745 commit d94227c
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 60 deletions.
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

0 comments on commit d94227c

Please sign in to comment.