diff --git a/nixos.just b/nixos.just index 09c7d8e..a2799ed 100644 --- a/nixos.just +++ b/nixos.just @@ -94,40 +94,50 @@ radare module: # Just do it # =================================================================================================================== # -[private] -build-common-pre module: - just clean {{ module }} - just create-build-dir {{ module }} - - just build-wasm-module {{ module }} - [private] build-common-post module target="fail": - just build-wasm-host {{ module }} {{ target }} - just build-system-startup {{ module }} {{ target }} - just build-system-syscalls {{ module }} {{ target }} - just link-system {{ module }} {{ target }} - just build-iso {{ module }} [doc("Perform all steps for a fail/linux/linux-bm build with aot/interp WASM")] [group("5: just do it")] build module target="fail" mode="aot": #!/usr/bin/env sh - just build-common-pre {{ module }} + just clean {{ module }} + just create-build-dir {{ module }} if [ "{{ mode }}" = "aot" ]; then + just build-wasm-module {{ module }} just build-wasm-aot {{ module }} just build-wasm-aot-array {{ module }} + just prepare-aot-host {{ module }} {{ target }} + just build-wasm-host {{ module }} {{ target }} + + just build-system-startup {{ module }} {{ target }} + just build-system-syscalls {{ module }} {{ target }} + just link-system {{ module }} {{ target }} elif [ "{{ mode }}" = "interp" ]; then + just build-wasm-module {{ module }} just build-wasm-interp-array {{ module }} + just prepare-interp-host {{ module }} {{ target }} + just build-wasm-host {{ module }} {{ target }} + + just build-system-startup {{ module }} {{ target }} + just build-system-syscalls {{ module }} {{ target }} + just link-system {{ module }} {{ target }} + elif [ "{{ mode }}" = "c" ]; then + just build-c-module {{ module }} {{ target }} + + just build-c-host {{ module }} {{ target }} + + just build-system-startup {{ module }} {{ target }} + just link-c-system {{ module }} {{ target }} else echo "unknown mode: {{ mode }}" >&2 exit 1 fi - just build-common-post {{ module }} {{ target }} + just build-iso {{ module }} [doc("Run binary")] [group("5: just do it")] diff --git a/targets/c-host/fail.c b/targets/c-host/fail.c new file mode 100644 index 0000000..f4a1a3b --- /dev/null +++ b/targets/c-host/fail.c @@ -0,0 +1,17 @@ +#include "lib.h" + +int wasm_module(void); + +MAIN() { + int result; + + MARKER(start_trace); + result = wasm_module(); + MARKER(stop_trace); + + if (result == 100) { + MARKER(ok_marker); + } else { + MARKER(fail_marker); + } +} diff --git a/targets/c-host/lib.h b/targets/c-host/lib.h new file mode 100644 index 0000000..daabf67 --- /dev/null +++ b/targets/c-host/lib.h @@ -0,0 +1,35 @@ +#pragma once + +#define INLINE __attribute__((always_inline)) inline +#define NOINLINE __attribute__((noinline)) + +#define __QUOTE(x) #x +#define QUOTE(x) __QUOTE(x) + +#ifndef ARCH_ASM_CLOBBER_ALL +#define ARCH_ASM_CLOBBER_ALL "eax", "ebx", "ecx", "edx", "esi", "edi", "ebp" +#endif + +#ifndef MARKER +#define MARKER(str) \ + __asm__ volatile(QUOTE(str) ":" \ + : /* no inputs */ \ + : /* no outputs */ \ + : "memory", ARCH_ASM_CLOBBER_ALL) +#endif + +#ifndef MAIN +#define MAIN() void os_main(void) +#endif + +#ifndef POSIX_PRINTF +#define POSIX_PRINTF(...) +#endif + +typedef __UINT8_TYPE__ uint8_t; +typedef __UINT16_TYPE__ uint16_t; +typedef __UINT32_TYPE__ uint32_t; + +typedef __INT8_TYPE__ int8_t; +typedef __INT16_TYPE__ int16_t; +typedef __INT32_TYPE__ int32_t; diff --git a/targets/c-host/linux.c b/targets/c-host/linux.c new file mode 100644 index 0000000..6dc3145 --- /dev/null +++ b/targets/c-host/linux.c @@ -0,0 +1,15 @@ +#include + +int wasm_module(void); + +int main(int argc, char *argv[]) { + int result = wasm_module(); + + if (result == 100) { + printf("OK Marker\n"); + return 0; + } else { + printf("FAIL Marker\n"); + return 1; + } +} diff --git a/wasm.just b/wasm.just index 49c4e34..47829df 100644 --- a/wasm.just +++ b/wasm.just @@ -16,6 +16,35 @@ WASI_CFLAGS := "\ -Wl,--export=__heap_base \ -Wl,--export=__data_end \ " +CROSS_CFLAGS_NOWASM := "\ +-m32 \ +-ffunction-sections \ +-fdata-sections \ +-ffreestanding \ +-fomit-frame-pointer \ +-ggdb \ +" +CROSS_LDFLAGS_NOWASM := f"\ +-Wl,--build-id=none \ +-static \ +-nostdlib \ +-m32 \ +-lc \ +-lgcc \ +-lm \ +" +LINUX_CFLAGS_NOWASM := "\ +-O0 \ +-m32 \ +-ffunction-sections \ +-fdata-sections \ +-ggdb \ +" +LINUX_LDFLAGS_NOWASM := f"\ +-Wl,--build-id=none \ +-m32 \ +-lm \ +" WAMRC := "wamrc" WAMRCFLAGS := "\ --target=i386 \ @@ -44,32 +73,38 @@ build-wasm-aot-array module: build-wasm-interp-array module: {{ XXD }} -i {{ BUILD_DIR }}-{{ module }}/wasm_module.wasm > {{ BUILD_DIR }}-{{ module }}/wasm_interp_array.c +[private] +build-c-module-fail module: + {{ CROSS_CC }} {{ CROSS_CFLAGS_NOWASM }} \ + -c targets/wasm-module/{{ module }}.c \ + -o {{ BUILD_DIR }}-{{ module }}/c_module.o + +[private] +build-c-module-linux module: + {{ LINUX_CC }} {{ LINUX_CFLAGS_NOWASM }} \ + -c targets/wasm-module/{{ module }}.c \ + -o {{ BUILD_DIR }}-{{ module }}/c_module.o + +[doc("C -> Object: Compile a C function (no WASM)")] +[group("1: build module")] +build-c-module module target="fail": + #!/usr/bin/env sh + if [ "{{ target }}" = "fail" ]; then + just build-c-module-fail "{{ module }}" + elif [ "{{ target }}" = "linux" ]; then + just build-c-module-linux "{{ module }}" + else + echo "unknown target: {{ target }}" >&2 + exit 1 + fi + # =================================================================================================================== # # Host program recipes # =================================================================================================================== # # FAIL* -CROSS_CFLAGS := "\ --I./targets/wasm-host \ --O2 \ --m32 \ --ffunction-sections \ --fdata-sections \ --ffreestanding \ --fomit-frame-pointer \ --ggdb \ -" -CROSS_LDFLAGS := f"\ --Wl,--build-id=none \ --static \ --nostdlib \ --m32 \ --L{{LIBIWASM_RELEASE}} \ --liwasm \ --lc \ --lgcc \ --lm \ -" +CROSS_CFLAGS := f"-I./targets/wasm-host {{CROSS_CFLAGS_NOWASM}} -O2" +CROSS_LDFLAGS := f"{{CROSS_LDFLAGS_NOWASM}} -L{{LIBIWASM_RELEASE}} -liwasm" CROSS_INCLUDES := f"\ -I{{WAMR_ROOT}}/core/iwasm/include \ -I{{WAMR_ROOT}}/core/shared/utils \ @@ -78,22 +113,8 @@ CROSS_INCLUDES := f"\ # LINUX-POSIX -LINUX_CFLAGS := "\ --I./targets/wasm-host \ --O0 \ --m32 \ --ffunction-sections \ --fdata-sections \ --ggdb \ -" -LINUX_LDFLAGS := f"\ --Wl,--build-id=none \ --m32 \ --Wl,-rpath,{{LIBIWASM_LINUX_DEBUG}} \ --L{{LIBIWASM_LINUX_DEBUG}} \ --liwasm \ --lm \ -" +LINUX_CFLAGS := f"-I./targets/wasm-host {{ LINUX_CFLAGS_NOWASM }}" +LINUX_LDFLAGS := f"{{LINUX_LDFLAGS_NOWASM}} -Wl,-rpath,{{LIBIWASM_LINUX_DEBUG}} -L{{LIBIWASM_LINUX_DEBUG}} -liwasm" LINUX_INCLUDES := f"\ -I{{WAMR_ROOT}}/core/iwasm/include \ -I{{WAMR_ROOT}}/core/shared/utils \ @@ -182,6 +203,32 @@ build-wasm-host module target="fail": exit 1 fi +[private] +build-c-host-fail module: + {{ CROSS_CC }} {{ CROSS_CFLAGS_NOWASM }} \ + -c targets/c-host/fail.c \ + -o {{ BUILD_DIR }}-{{ module }}/c_host.o + +[private] +build-c-host-linux module: + {{ LINUX_CC }} {{ LINUX_CFLAGS_NOWASM }} \ + -c targets/c-host/linux.c \ + -o {{ BUILD_DIR }}-{{ module }}/c_host.o + + +[doc("Insert the C function into the host program (no WASM)")] +[group("2: build host")] +build-c-host module target="fail": + #!/usr/bin/env sh + if [ "{{ target }}" = "fail" ]; then + just build-c-host-fail "{{ module }}" + elif [ "{{ target }}" = "linux" ]; then + just build-c-host-linux "{{ module }}" + else + echo "unknown target: {{ target }}" >&2 + exit 1 + fi + [private] build-system-startup-fail module: {{ CROSS_CC }} targets/startup.s {{ CROSS_CFLAGS }} -c -o {{ BUILD_DIR }}-{{ module }}/startup.o @@ -256,6 +303,37 @@ link-system module target="fail": exit 1 fi +[private] +link-c-system-fail module: + {{ CROSS_CC }} \ + -Wl,-T targets/linker.ld \ + {{ BUILD_DIR }}-{{ module }}/c_host.o \ + {{ BUILD_DIR }}-{{ module }}/startup.o \ + {{ BUILD_DIR }}-{{ module }}/c_module.o \ + {{ CROSS_LDFLAGS_NOWASM }} \ + -o {{ BUILD_DIR }}-{{ module }}/system.elf + +[private] +link-c-system-linux module: + {{ LINUX_CC }} \ + {{ BUILD_DIR }}-{{ module }}/c_host.o \ + {{ BUILD_DIR }}-{{ module }}/c_module.o \ + {{ LINUX_LDFLAGS_NOWASM }} \ + -o {{ BUILD_DIR }}-{{ module }}/system.elf + +[doc("Link C-Host, C-function and bootloader")] +[group("2: build host")] +link-c-system module target="fail": + #!/usr/bin/env sh + if [ "{{ target }}" = "fail" ]; then + just link-c-system-fail "{{ module }}" + elif [ "{{ target }}" = "linux" ]; then + just link-c-system-linux "{{ module }}" + else + echo "unknown target: {{ target }}" >&2 + exit 1 + fi + [doc("Create bootdisk")] [group("2: build host")] build-iso module: