commit 6d04461575415ffd41bd2f9d47c37d80af01d827
parent a9771b645301e3290f208c3b3c354b714f6f3f3d
Author: Yersa Nordman <finwo@pm.me>
Date: Sat, 12 Oct 2019 00:12:30 +0200
Useful build structure
Diffstat:
11 files changed, 155 insertions(+), 6 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -1 +1,2 @@
*.o
+*.a
diff --git a/Makefile b/Makefile
@@ -0,0 +1,17 @@
+SRC=$(shell find src -type f -name *.c)
+OBJ=$(SRC:.c=.o)
+
+CC = clang
+LD = wasm-ld
+TARGET ?= wasm32
+CFLAGS ?= -O3
+
+matter.a: $(OBJ)
+ ar rcs matter.a $(OBJ)
+
+%.o: %.c
+ $(CC) $(CFLAGS) --target=$(TARGET) -nostdinc -fno-builtin -Iinclude -c $< -o $@
+
+.PHONY: clean
+clean:
+ rm -f $(OBJ)
diff --git a/TODO b/TODO
@@ -1,3 +0,0 @@
-MAIN
- copy build structure from musl
-
diff --git a/include/malloc.h b/include/malloc.h
@@ -0,0 +1,7 @@
+#ifndef _MALLOC_H_
+#define _MALLOC_H_
+
+void * malloc(int n);
+void free(void *p);
+
+#endif // _MALLOC_H_
diff --git a/include/stddef.h b/include/stddef.h
@@ -0,0 +1,10 @@
+#ifndef _STDDEF_H_
+#define _STDDEF_H_
+
+#ifdef __cplusplus
+#define NULL 0L
+#else
+#define NULL ((void*)0)
+#endif
+
+#endif // _STDDEF_H_
diff --git a/include/string.h b/include/string.h
@@ -0,0 +1,8 @@
+#ifndef _STRING_H_
+#define _STRING_H_
+
+int memcmp (const void *str1, const void *str2, int count);
+void * memcpy (void *dest, const void *src, int len);
+void * memset (void *dest, int val, int len);
+
+#endif // _STRING_H_
diff --git a/src/malloc/TODO b/src/malloc/TODO
@@ -1,3 +0,0 @@
-src/malloc
- copy heap malloc from supercop-js
-
diff --git a/src/wasm/malloc/malloc.c b/src/wasm/malloc/malloc.c
@@ -0,0 +1,66 @@
+#ifndef _MALLOC_C_
+#define _MALLOC_C_
+
+#include <malloc.h>
+#include <stddef.h>
+
+extern unsigned char __heap_base;
+
+unsigned char *bump_pointer = &__heap_base;
+
+unsigned char *heap_start = &__heap_base;
+unsigned char *heap_end = &__heap_base;
+unsigned int isize = sizeof(unsigned int);
+unsigned int hsize = (2 * sizeof(unsigned int));
+
+// TODO: realloc
+// TODO: free
+
+void * malloc(int n) {
+ unsigned char *r = heap_start;
+
+ unsigned int *size = NULL;
+ unsigned int *used = NULL;
+
+ // Loop through known blocks
+ while(r < heap_end) {
+ size = (void*)r;
+ used = (void*)(r+isize);
+
+ // In-use = skip
+ if (*used) {
+ r += *size;
+ continue;
+ }
+
+ // Too small = skip
+ if ((*size) < (n + hsize)) {
+ r += *size;
+ continue;
+ }
+
+ // Take this block
+ *used = n;
+ return (void*)(r + hsize);
+ }
+
+ // Something went wrong
+ if (r < heap_end) {
+ return NULL;
+ }
+
+ // Build a new block
+ size = (void*)r;
+ used = (void*)(r+isize);
+ *size = n + hsize;
+ *used = n;
+ heap_end = r + n + hsize;
+ return (void*)(r + hsize);
+}
+
+void free(void *p) {
+ unsigned int *used = p - isize;
+ *used = 0;
+}
+
+#endif // _MALLOC_C_
diff --git a/src/wasm/string/memcmp.c b/src/wasm/string/memcmp.c
@@ -0,0 +1,18 @@
+#ifndef _MEMCMP_C_
+#define _MEMCMP_C_
+
+#include <string.h>
+
+int memcmp (const void *str1, const void *str2, int count) {
+ const unsigned char *s1 = str1;
+ const unsigned char *s2 = str2;
+
+ while (count-- > 0)
+ {
+ if (*s1++ != *s2++)
+ return s1[-1] < s2[-1] ? -1 : 1;
+ }
+ return 0;
+}
+
+#endif // _MEMCMP_C_
diff --git a/src/wasm/string/memcpy.c b/src/wasm/string/memcpy.c
@@ -0,0 +1,15 @@
+#ifndef _MEMCPY_C_
+#define _MEMCPY_C_
+
+#include <string.h>
+
+void * memcpy (void *dest, const void *src, int len)
+{
+ char *d = dest;
+ const char *s = src;
+ while (len--)
+ *d++ = *s++;
+ return dest;
+}
+
+#endif // _MEMCPY_C_
diff --git a/src/wasm/string/memset.c b/src/wasm/string/memset.c
@@ -0,0 +1,13 @@
+#ifndef _MEMSET_C_
+#define _MEMSET_C_
+
+#include <string.h>
+
+void * memset (void *dest, int val, int len) {
+ unsigned char *ptr = dest;
+ while (len-- > 0)
+ *ptr++ = val;
+ return dest;
+}
+
+#endif // _MEMSET_C_