diff --git a/runtime/src/main/cpp/Exceptions.cpp b/runtime/src/main/cpp/Exceptions.cpp index 3d901d6d8c2..2765d0f92e5 100644 --- a/runtime/src/main/cpp/Exceptions.cpp +++ b/runtime/src/main/cpp/Exceptions.cpp @@ -1,4 +1,6 @@ #include +#include +#include #include "Assert.h" #include "Exceptions.h" @@ -10,7 +12,7 @@ void ThrowNullPointerException() { void* pc = __builtin_return_address(0); char message[100]; snprintf(message, sizeof(message), "NullPointerException at %p", pc); - fprintf(stderr, "%s\n", message); + write(STDOUT_FILENO, message, strlen(message)); // TODO: throw it for real. RuntimeAssert(false, "Throwing is unsupported"); } @@ -19,11 +21,21 @@ void ThrowArrayIndexOutOfBoundsException() { void* pc = __builtin_return_address(0); char message[100]; snprintf(message, sizeof(message), "ArrayIndexOutOfBoundsException at %p", pc); - fprintf(stderr, "%s\n", message); + write(STDOUT_FILENO, message, strlen(message)); // TODO: throw it for real. RuntimeAssert(false, "Throwing is unsupported"); } +void ThrowClassCastException() { + void* pc = __builtin_return_address(0); + char message[100]; + snprintf(message, sizeof(message), "ClassCastException at %p", pc); + write(STDOUT_FILENO, message, strlen(message)); + // TODO: throw it for real. + RuntimeAssert(false, "Throwing is unsupported"); +} + + #ifdef __cplusplus } // extern "C" #endif diff --git a/runtime/src/main/cpp/Exceptions.h b/runtime/src/main/cpp/Exceptions.h index 2b4cff40828..185843331d7 100644 --- a/runtime/src/main/cpp/Exceptions.h +++ b/runtime/src/main/cpp/Exceptions.h @@ -1,7 +1,7 @@ #ifndef RUNTIME_EXCEPTIONS_H #define RUNTIME_EXCEPTIONS_H -#include +#include "Types.h" #ifdef __cplusplus extern "C" { @@ -11,6 +11,8 @@ void ThrowNullPointerException(); // Throws array index out of bounds exception. // Context is evaluated from caller's address. void ThrowArrayIndexOutOfBoundsException(); +// Throws class cast exception. +void ThrowClassCastException(); #ifdef __cplusplus } // extern "C" diff --git a/runtime/src/main/cpp/Memory.h b/runtime/src/main/cpp/Memory.h index 5ef2b88552c..fce850e9499 100644 --- a/runtime/src/main/cpp/Memory.h +++ b/runtime/src/main/cpp/Memory.h @@ -85,6 +85,12 @@ inline void* AddressOfElementAt(ArrayHeader* obj, int32_t index) { obj->type_info()->instanceSize_ * index; } +inline const void* AddressOfElementAt(const ArrayHeader* obj, int32_t index) { + // Instance size is negative. + return reinterpret_cast(obj + 1) - + obj->type_info()->instanceSize_ * index; +} + inline uint32_t ArrayDataSizeBytes(const ArrayHeader* obj) { // Instance size is negative. return -obj->type_info()->instanceSize_ * obj->count_; diff --git a/runtime/src/main/cpp/Natives.h b/runtime/src/main/cpp/Natives.h index 2c39b8d5ca1..5f678554b9a 100644 --- a/runtime/src/main/cpp/Natives.h +++ b/runtime/src/main/cpp/Natives.h @@ -1,23 +1,8 @@ #ifndef RUNTIME_NATIVES_H #define RUNTIME_NATIVES_H -#include "Memory.h" #include "Types.h" -typedef uint8_t KBoolean; -typedef int8_t KByte; -typedef uint16_t KChar; -// Note that it is signed. -typedef int16_t KShort; -typedef int32_t KInt; -typedef int64_t KLong; -typedef float KFloat; -typedef double KDouble; - -typedef ObjHeader* KRef; -typedef const ObjHeader* KConstRef; -typedef const ArrayHeader* KString; - // Optimized versions not accessing type info. inline KByte* ByteArrayAddressOfElementAt(ArrayHeader* obj, KInt index) { return reinterpret_cast(obj + 1) + index; diff --git a/runtime/src/main/cpp/Types.h b/runtime/src/main/cpp/Types.h index a712fad2bcb..f3e0cdea7a1 100644 --- a/runtime/src/main/cpp/Types.h +++ b/runtime/src/main/cpp/Types.h @@ -1,8 +1,23 @@ #ifndef RUNTIME_TYPES_H #define RUNTIME_TYPES_H +#include "Memory.h" #include "TypeInfo.h" +// Note that almost all types are signed. +typedef uint8_t KBoolean; +typedef int8_t KByte; +typedef uint16_t KChar; +typedef int16_t KShort; +typedef int32_t KInt; +typedef int64_t KLong; +typedef float KFloat; +typedef double KDouble; + +typedef ObjHeader* KRef; +typedef const ObjHeader* KConstRef; +typedef const ArrayHeader* KString; + #ifdef __cplusplus extern "C" { #endif