aboutsummaryrefslogtreecommitdiff
path: root/src/types.c
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2024-05-22 14:04:36 +0200
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2024-06-12 13:38:05 +0200
commit4f9a2c7a2d8464b04cc08075a7762c6d457090df (patch)
treeae8fe229a3a5ba60d08b74299d0c1850685bda86 /src/types.c
parentf25b015e5b668028c34974bbb22faa4105c26690 (diff)
downloadnanowasm-sync-4f9a2c7a2d8464b04cc08075a7762c6d457090df.tar.gz
WIP
Diffstat (limited to 'src/types.c')
-rw-r--r--src/types.c96
1 files changed, 96 insertions, 0 deletions
diff --git a/src/types.c b/src/types.c
index 88c883e..56b2bc1 100644
--- a/src/types.c
+++ b/src/types.c
@@ -8,6 +8,7 @@
*/
#include <nw/leb128.h>
+#include <nw/log.h>
#include <nw/types.h>
#include <stdio.h>
@@ -87,3 +88,98 @@ int varint64_read(FILE *const f, varint64 *const out)
*out = value;
return 0;
}
+
+size_t get_type_size(const enum value_type type)
+{
+ static const size_t list[] =
+ {
+ [VALUE_TYPE_I32] = sizeof (int32_t),
+ [VALUE_TYPE_I64] = sizeof (int64_t),
+ [VALUE_TYPE_F32] = sizeof (float),
+ [VALUE_TYPE_F64] = sizeof (double)
+ };
+
+ return list[type];
+}
+
+enum
+{
+ I32 = 0x7f,
+ I64 = 0x7e,
+ F32 = 0x7d,
+ F64 = 0x7c,
+ ANYFUNC = 0x70,
+ FUNC = 0x60,
+ BLOCK_TYPE = 0x40
+};
+
+int get_value_type(const varint7 type, enum value_type *const vtype)
+{
+ static const struct size
+ {
+ varint7 type;
+ enum value_type vtype;
+ } sizes[] =
+ {
+ {.type = I32, .vtype = VALUE_TYPE_I32},
+ {.type = I64, .vtype = VALUE_TYPE_I64},
+ {.type = F32, .vtype = VALUE_TYPE_F32},
+ {.type = F64, .vtype = VALUE_TYPE_F64},
+ /* TODO: check this. */
+ {.type = ANYFUNC, .vtype = VALUE_TYPE_I32},
+ /* TODO: check this. */
+ {.type = FUNC, .vtype = VALUE_TYPE_I32},
+ /* TODO: check this. */
+ {.type = BLOCK_TYPE, .vtype = VALUE_TYPE_I32}
+ };
+
+ for (size_t i = 0; i < sizeof sizes / sizeof *sizes; i++)
+ {
+ const struct size *const s = &sizes[i];
+
+ if (type == s->type)
+ {
+ *vtype = s->vtype;
+ return 0;
+ }
+ }
+
+ LOG("%s: unknown type %#hhx\n", __func__, (char)type);
+ return -1;
+}
+
+static int32_t little_endian_i32(const int32_t in)
+{
+ const union
+ {
+ uint8_t a[4];
+ uint32_t b;
+ } v =
+ {
+ .b = in
+ };
+
+ return v.a[0] | (v.a[1] << 8l) | (v.a[2] << 16l) | (v.a[3] << 24l);
+}
+
+int32_t htoni32(const int32_t in)
+{
+ return little_endian_i32(in);
+}
+
+int32_t ntohi32(const int32_t in)
+{
+ return little_endian_i32(in);
+}
+
+const char *value_type_tostr(const enum value_type v)
+{
+ static const char *const s[] =
+ {
+#define X(x) [x] = #x,
+ VALUE_TYPES
+#undef X
+ };
+
+ return s[v];
+}