aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2024-04-09 15:10:40 +0630
committerAryadev Chavali <aryadev@aryadevchavali.com>2024-04-09 15:11:42 +0630
commitafb48b65b9217fd0afd7d53cd89365d11a5a556b (patch)
tree62241e1197347ea8dea2490752446661e4654682
parent6df6dce1531a65b887963cb0fc19f2f976b21f8b (diff)
downloadovm-afb48b65b9217fd0afd7d53cd89365d11a5a556b.tar.gz
ovm-afb48b65b9217fd0afd7d53cd89365d11a5a556b.tar.bz2
ovm-afb48b65b9217fd0afd7d53cd89365d11a5a556b.zip
Completed TODO: Rigid Endian
Just used the endian.h functions to convert host endian to and from big endian.
-rw-r--r--lib/base.c17
-rw-r--r--todo.org36
2 files changed, 29 insertions, 24 deletions
diff --git a/lib/base.c b/lib/base.c
index 14a250e..b7a6ed5 100644
--- a/lib/base.c
+++ b/lib/base.c
@@ -10,30 +10,35 @@
* Description: Implementation of basic library functions
*/
+#include <endian.h>
#include <string.h>
#include "./base.h"
hword convert_bytes_to_hword(byte *bytes)
{
- hword h = 0;
- memcpy(&h, bytes, HWORD_SIZE);
+ hword be_h = 0;
+ memcpy(&be_h, bytes, HWORD_SIZE);
+ hword h = be32toh(be_h);
return h;
}
void convert_hword_to_bytes(hword w, byte *bytes)
{
- memcpy(bytes, &w, HWORD_SIZE);
+ hword be_h = htobe32(w);
+ memcpy(bytes, &be_h, HWORD_SIZE);
}
void convert_word_to_bytes(word w, byte *bytes)
{
- memcpy(bytes, &w, WORD_SIZE);
+ word be_w = htobe64(w);
+ memcpy(bytes, &be_w, WORD_SIZE);
}
word convert_bytes_to_word(byte *bytes)
{
- word w = 0;
- memcpy(&w, bytes, WORD_SIZE);
+ word be_w = 0;
+ memcpy(&be_w, bytes, WORD_SIZE);
+ word w = be64toh(be_w);
return w;
}
diff --git a/todo.org b/todo.org
index ba3f0f0..fd9cac4 100644
--- a/todo.org
+++ b/todo.org
@@ -15,24 +15,6 @@
*** TODO VM [0%]
**** TODO vm/runtime.h
** TODO Specification
-* TODO Rigid endian :LIB:
-Say a program is compiled on a little endian machine. The resultant
-bytecode file, as a result of using C's internal functions, will use
-little endian.
-
-This file, when distributed to other computers, will not work on those
-that use big endian.
-
-This is a massive problem; I would like bytecode compiled on one
-computer to work on any other one. Therefore we have to enforce big
-endian. This refactor is limited to only LIB as a result of only the
-~convert_*~ functions being used in the runtime to convert between
-byte buffers (usually read from the bytecode file directly or from
-memory to use in the stack).
-
-2024-04-09: Found the ~hto_e~ functions under =endian.h= that provide
-both way host to specific endian conversion of shorts, half words and
-words. This will make it super simple to just convert.
* TODO Preprocessing directives :ASM:
Like in FASM or NASM where we can give certain helpful instructions to
the assembler. I'd use the ~%~ symbol to designate preprocessor
@@ -272,3 +254,21 @@ constant potentially
#+end_src
which when referred to (by ~$print-1~) would insert the bytecode given
inline.
+** DONE Rigid endian :LIB:
+Say a program is compiled on a little endian machine. The resultant
+bytecode file, as a result of using C's internal functions, will use
+little endian.
+
+This file, when distributed to other computers, will not work on those
+that use big endian.
+
+This is a massive problem; I would like bytecode compiled on one
+computer to work on any other one. Therefore we have to enforce big
+endian. This refactor is limited to only LIB as a result of only the
+~convert_*~ functions being used in the runtime to convert between
+byte buffers (usually read from the bytecode file directly or from
+memory to use in the stack).
+
+2024-04-09: Found the ~hto_e~ functions under =endian.h= that provide
+both way host to specific endian conversion of shorts, half words and
+words. This will make it super simple to just convert.