Skip to content

Instantly share code, notes, and snippets.

@laytan
Last active November 14, 2023 22:15
Show Gist options
  • Save laytan/8dc137336d57b121c368a8f9ccb20a69 to your computer and use it in GitHub Desktop.
Save laytan/8dc137336d57b121c368a8f9ccb20a69 to your computer and use it in GitHub Desktop.
Orca in Odin
@echo off
setlocal enabledelayedexpansion
set ORCA_DIR=..\..\third-party\orca
set STDLIB_DIR=%ORCA_DIR%\src\libc-shim
:: common flags to build wasm modules
set wasmFlags=--target=wasm32^
--no-standard-libraries ^
-mbulk-memory ^
-g -O2 ^
-D__ORCA__ ^
-Wl,--no-entry ^
-Wl,--export-dynamic ^
-isystem %STDLIB_DIR%\include ^
-I%ORCA_DIR%\src ^
-I%ORCA_DIR%\src\ext
:: build orca core as wasm module
clang %wasmFlags% -Wl,--relocatable -o .\liborca.a %ORCA_DIR%\src\orca.c %ORCA_DIR%\src\libc-shim\src\*.c
IF %ERRORLEVEL% NEQ 0 EXIT /B %ERRORLEVEL%
:: build sample as wasm module and link it with the orca module
odin build . ^
-target:wasi_wasm32 ^
-o:speed ^
-no-entry-point ^
-no-crt ^
-show-system-calls ^
-out:module.wasm ^
-extra-linker-flags:"-L . -lorca --export-dynamic"
IF %ERRORLEVEL% NEQ 0 EXIT /B %ERRORLEVEL%
:: create app directory and copy files into it
orca bundle --orca-dir %ORCA_DIR% --name SampleOdin module.wasm
set -ex
ORCA_DIR=../../third-party/orca
STDLIB_DIR=$ORCA_DIR/src/libc-shim
# common flags to build wasm modules
wasmFlags="--target=wasm32 \
--no-standard-libraries \
-mbulk-memory \
-g -O2 \
-D__ORCA__ \
-Wl,--no-entry \
-Wl,--export-dynamic \
-isystem $STDLIB_DIR/include \
-I $ORCA_DIR/src \
-I $ORCA_DIR/src/ext"
# build orca core as wasm module
clang $wasmFlags -Wl,--relocatable -o ./liborca.a $ORCA_DIR/src/orca.c $STDLIB_DIR/src/*.c
# build the odin wasm module
#
# NOTE: would ideally do the --extra-linker-flags as a `@(require, extra_linker_flags="--export-dynamic") foreign import "orca"`
# But that doesn't seem to be implemented for wasm targets in the compiler.
#
odin build . \
-target:wasi_wasm32 \
-o:speed \
-no-entry-point \
-no-crt \
-show-system-calls \
-out:module.wasm \
-extra-linker-flags:"-L . -lorca --export-dynamic"
# create app directory and copy files into it
orca bundle --orca-dir $ORCA_DIR --name SampleOdin module.wasm
package main
@(default_calling_convention="c")
foreign {
oc_window_set_title :: proc(title: _OC_STR8) ---
}
_OC_STR8 :: struct {
data: [^]byte,
len: uint,
}
OC_STR8 :: proc "contextless" (s: string) -> _OC_STR8 {
return {raw_data(s), uint(len(s))}
}
@(export)
oc_on_init :: proc "c" () {
oc_window_set_title(OC_STR8("Hello"))
}
diff --git a/core/runtime/procs.odin b/core/runtime/procs.odin
index 1592c608b..ae982a45c 100644
--- a/core/runtime/procs.odin
+++ b/core/runtime/procs.odin
@@ -26,50 +26,50 @@ when ODIN_NO_CRT && ODIN_OS == .Windows {
return dst
}
} else when ODIN_NO_CRT || (ODIN_ARCH == .wasm32 || ODIN_ARCH == .wasm64p32) {
- @(link_name="memset", linkage="strong", require)
- memset :: proc "c" (ptr: rawptr, val: i32, len: int) -> rawptr {
- if ptr != nil && len != 0 {
- b := byte(val)
- p := ([^]byte)(ptr)
- for i := 0; i < len; i += 1 {
- p[i] = b
- }
- }
- return ptr
- }
-
- @(link_name="memmove", linkage="strong", require)
- memmove :: proc "c" (dst, src: rawptr, len: int) -> rawptr {
- d, s := ([^]byte)(dst), ([^]byte)(src)
- if d == s || len == 0 {
- return dst
- }
- if d > s && uintptr(d)-uintptr(s) < uintptr(len) {
- for i := len-1; i >= 0; i -= 1 {
- d[i] = s[i]
- }
- return dst
- }
-
- if s > d && uintptr(s)-uintptr(d) < uintptr(len) {
- for i := 0; i < len; i += 1 {
- d[i] = s[i]
- }
- return dst
- }
- return memcpy(dst, src, len)
- }
- @(link_name="memcpy", linkage="strong", require)
- memcpy :: proc "c" (dst, src: rawptr, len: int) -> rawptr {
- d, s := ([^]byte)(dst), ([^]byte)(src)
- if d != s {
- for i := 0; i < len; i += 1 {
- d[i] = s[i]
- }
- }
- return d
-
- }
} else {
memset :: proc "c" (ptr: rawptr, val: i32, len: int) -> rawptr {
if ptr != nil && len != 0 {
@@ -81,4 +81,4 @@ when ODIN_NO_CRT && ODIN_OS == .Windows {
}
return ptr
}
-}
\ No newline at end of file
+}
diff --git a/src/checker.cpp b/src/checker.cpp
index 9653116cf..d1febf5f2 100644
--- a/src/checker.cpp
+++ b/src/checker.cpp
@@ -2371,17 +2371,19 @@ gb_internal void generate_minimum_dependency_set(Checker *c, Entity *start) {
str_lit("cstring_to_string"),
str_lit("_cleanup_runtime"),
- // Pseudo-CRT required procedures
- str_lit("memset"),
- str_lit("memcpy"),
- str_lit("memmove"),
-
// Utility procedures
str_lit("memory_equal"),
str_lit("memory_compare"),
str_lit("memory_compare_zero"),
);
+ FORCE_ADD_RUNTIME_ENTITIES(false,
+ // Pseudo-CRT required procedures
+ str_lit("memset"),
+ str_lit("memcpy"),
+ str_lit("memmove"),
+ );
+
FORCE_ADD_RUNTIME_ENTITIES(!build_context.tilde_backend,
// Extended data type internal procedures
str_lit("umodti3"),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment