import kernel
import math
+in "C" `{
+#ifdef __linux__
+ #include <endian.h>
+#endif
+#ifdef __APPLE__
+ #include <libkern/OSByteOrder.h>
+ #define be32toh(x) OSSwapBigToHostInt32(x)
+#endif
+
+#ifdef __pnacl__
+ #define be16toh(val) (((val) >> 8) | ((val) << 8))
+ #define be32toh(val) ((be16toh((val) << 16) | (be16toh((val) >> 16))))
+#endif
+#ifndef be32toh
+ #define be32toh(val) betoh32(val)
+#endif
+`}
+
redef class Byte
# Gives the length of the UTF-8 char starting with `self`
fun u8len: Int do
end
return ln
end
+
+ # Fetch 4 chars in `self` at `pos`
+ fun fetch_4_chars(pos: Int): Int is intern do return fetch_4_ffi(pos)
+
+ # Fetch 4 chars in `self` at `pos`
+ fun fetch_4_hchars(pos: Int): Int is intern do return fetch_4h_ffi(pos)
+
+ # FIXME: To remove when bootstrap supports PR #1898
+ private fun fetch_4_ffi(pos: Int): Int `{ return (long)*((uint32_t*)(self+pos)); `}
+ private fun fetch_4h_ffi(pos: Int): Int `{ return (long)be32toh(*((uint32_t*)(self+pos))); `}
end
self.header.add_decl("#include <sys/types.h>\n")
self.header.add_decl("#include <unistd.h>\n")
self.header.add_decl("#include <stdint.h>\n")
+ self.header.add_decl("#ifdef __linux__")
+ self.header.add_decl(" #include <endian.h>")
+ self.header.add_decl("#endif")
self.header.add_decl("#include <inttypes.h>\n")
self.header.add_decl("#include \"gc_chooser.h\"")
+ self.header.add_decl("#ifdef __APPLE__")
+ self.header.add_decl(" #include <libkern/OSByteOrder.h>")
+ self.header.add_decl(" #define be32toh(x) OSSwapBigToHostInt32(x)")
+ self.header.add_decl("#endif")
+ self.header.add_decl("#ifdef __pnacl__")
+ self.header.add_decl(" #define be16toh(val) (((val) >> 8) | ((val) << 8))")
+ self.header.add_decl(" #define be32toh(val) ((be16toh((val) << 16) | (be16toh((val) >> 16))))")
+ self.header.add_decl("#endif")
self.header.add_decl("#ifdef ANDROID")
+ self.header.add_decl(" #define be32toh(val) betoh32(val)")
self.header.add_decl(" #include <android/log.h>")
self.header.add_decl(" #define PRINT_ERROR(...) (void)__android_log_print(ANDROID_LOG_WARN, \"Nit\", __VA_ARGS__)")
self.header.add_decl("#else")
else if pname == "fast_cstring" then
v.ret(v.new_expr("{arguments[0]} + {arguments[1]}", ret.as(not null)))
return true
+ else if pname == "==" then
+ v.ret(v.equal_test(arguments[0], arguments[1]))
+ return true
+ else if pname == "!=" then
+ var res = v.equal_test(arguments[0], arguments[1])
+ v.ret(v.new_expr("!{res}", ret.as(not null)))
+ return true
else if pname == "new" then
v.ret(v.new_expr("(char*)nit_alloc({arguments[1]})", ret.as(not null)))
return true
+ else if pname == "fetch_4_chars" then
+ v.ret(v.new_expr("(long)*((uint32_t*)({arguments[0]} + {arguments[1]}))", ret.as(not null)))
+ return true
+ else if pname == "fetch_4_hchars" then
+ v.ret(v.new_expr("(long)be32toh(*((uint32_t*)({arguments[0]} + {arguments[1]})))", ret.as(not null)))
+ return true
end
else if cname == "NativeArray" then
return v.native_array_def(pname, ret, arguments)