globalDefinitions_gcc.hpp revision 2294:e863062e521d
138032Speter/* 2261194Sgshapiro * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved. 364562Sgshapiro * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 438032Speter * 564562Sgshapiro * This code is free software; you can redistribute it and/or modify it 664562Sgshapiro * under the terms of the GNU General Public License version 2 only, as 738032Speter * published by the Free Software Foundation. 838032Speter * 938032Speter * This code is distributed in the hope that it will be useful, but WITHOUT 1038032Speter * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1190792Sgshapiro * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1238032Speter * version 2 for more details (a copy is included in the LICENSE file that 1338032Speter * accompanied this code). 1464562Sgshapiro * 1538032Speter * You should have received a copy of the GNU General Public License version 16266527Sgshapiro * 2 along with this work; if not, write to the Free Software Foundation, 1738032Speter * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1890792Sgshapiro * 1964562Sgshapiro * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2038032Speter * or visit www.oracle.com if you need additional information or have any 2164562Sgshapiro * questions. 2264562Sgshapiro * 2364562Sgshapiro */ 2490792Sgshapiro 2564562Sgshapiro#ifndef SHARE_VM_UTILITIES_GLOBALDEFINITIONS_GCC_HPP 2690792Sgshapiro#define SHARE_VM_UTILITIES_GLOBALDEFINITIONS_GCC_HPP 2738032Speter 2838032Speter#include "prims/jni.h" 2938032Speter 3038032Speter// This file holds compiler-dependent includes, 3138032Speter// globally used constants & types, class (forward) 3238032Speter// declarations and a few frequently used utility functions. 3338032Speter 3438032Speter#include <ctype.h> 3538032Speter#include <string.h> 3638032Speter#include <stdarg.h> 3738032Speter#include <stddef.h> 3838032Speter#include <stdio.h> 3938032Speter#include <stdlib.h> 4038032Speter#include <wchar.h> 4138032Speter 4238032Speter#ifdef SOLARIS 4338032Speter#include <ieeefp.h> 4438032Speter#endif // SOLARIS 4538032Speter 4638032Speter#include <math.h> 4738032Speter#ifndef FP_PZERO 4838032Speter// Linux doesn't have positive/negative zero 4938032Speter#define FP_PZERO FP_ZERO 5038032Speter#endif 5138032Speter#if (!defined fpclass) && ((!defined SPARC) || (!defined SOLARIS)) 5238032Speter#define fpclass fpclassify 5338032Speter#endif 5438032Speter 5538032Speter#include <time.h> 5638032Speter#include <fcntl.h> 5738032Speter#include <dlfcn.h> 5838032Speter#include <pthread.h> 5938032Speter 6064562Sgshapiro#ifdef SOLARIS 6138032Speter#include <thread.h> 6238032Speter#endif // SOLARIS 6338032Speter 6490792Sgshapiro#include <limits.h> 6538032Speter#include <errno.h> 6638032Speter 6764562Sgshapiro#ifdef SOLARIS 6838032Speter#include <sys/trap.h> 6938032Speter#include <sys/regset.h> 7038032Speter#include <sys/procset.h> 7138032Speter#include <ucontext.h> 7238032Speter#include <setjmp.h> 7338032Speter#endif // SOLARIS 7438032Speter 7538032Speter# ifdef SOLARIS_MUTATOR_LIBTHREAD 7638032Speter# include <sys/procfs.h> 7738032Speter# endif 7838032Speter 7938032Speter#ifdef LINUX 8038032Speter#ifndef __STDC_LIMIT_MACROS 8138032Speter#define __STDC_LIMIT_MACROS 8238032Speter#endif // __STDC_LIMIT_MACROS 8364562Sgshapiro#include <inttypes.h> 8464562Sgshapiro#include <signal.h> 8564562Sgshapiro#include <ucontext.h> 8664562Sgshapiro#include <sys/time.h> 8764562Sgshapiro#endif // LINUX 8864562Sgshapiro 8990792Sgshapiro// 4810578: varargs unsafe on 32-bit integer/64-bit pointer architectures 9064562Sgshapiro// When __cplusplus is defined, NULL is defined as 0 (32-bit constant) in 9138032Speter// system header files. On 32-bit architectures, there is no problem. 9264562Sgshapiro// On 64-bit architectures, defining NULL as a 32-bit constant can cause 93168515Sgshapiro// problems with varargs functions: C++ integral promotion rules say for 9490792Sgshapiro// varargs, we pass the argument 0 as an int. So, if NULL was passed to a 9564562Sgshapiro// varargs function it will remain 32-bits. Depending on the calling 9664562Sgshapiro// convention of the machine, if the argument is passed on the stack then 97120256Sgshapiro// only 32-bits of the "NULL" pointer may be initialized to zero. The 9864562Sgshapiro// other 32-bits will be garbage. If the varargs function is expecting a 9964562Sgshapiro// pointer when it extracts the argument, then we have a problem. 10090792Sgshapiro// 10164562Sgshapiro// Solution: For 64-bit architectures, redefine NULL as 64-bit constant 0. 10264562Sgshapiro// 10364562Sgshapiro// Note: this fix doesn't work well on Linux because NULL will be overwritten 10464562Sgshapiro// whenever a system header file is included. Linux handles NULL correctly 10590792Sgshapiro// through a special type '__null'. 10664562Sgshapiro#ifdef SOLARIS 10764562Sgshapiro #ifdef _LP64 10864562Sgshapiro #undef NULL 10938032Speter #define NULL 0L 110168515Sgshapiro #else 111168515Sgshapiro #ifndef NULL 11290792Sgshapiro #define NULL 0 11390792Sgshapiro #endif 11490792Sgshapiro #endif 11590792Sgshapiro#endif 11638032Speter 11738032Speter// NULL vs NULL_WORD: 11838032Speter// On Linux NULL is defined as a special type '__null'. Assigning __null to 11938032Speter// integer variable will cause gcc warning. Use NULL_WORD in places where a 12038032Speter// pointer is stored as integer value. On some platforms, sizeof(intptr_t) > 12138032Speter// sizeof(void*), so here we want something which is integer type, but has the 12238032Speter// same size as a pointer. 12338032Speter#ifdef LINUX 12438032Speter #ifdef _LP64 12538032Speter #define NULL_WORD 0L 12638032Speter #else 12790792Sgshapiro // Cast 0 to intptr_t rather than int32_t since they are not the same type 12890792Sgshapiro // on platforms such as Mac OS X. 12938032Speter #define NULL_WORD ((intptr_t)0) 13038032Speter #endif 13164562Sgshapiro#else 13238032Speter #define NULL_WORD NULL 13338032Speter#endif 13438032Speter 13564562Sgshapiro#ifndef LINUX 13638032Speter// Compiler-specific primitive types 13790792Sgshapirotypedef unsigned short uint16_t; 13890792Sgshapiro#ifndef _UINT32_T 13938032Speter#define _UINT32_T 14038032Spetertypedef unsigned int uint32_t; 14138032Speter#endif // _UINT32_T 14290792Sgshapiro 143132943Sgshapiro#if !defined(_SYS_INT_TYPES_H) 14438032Speter#ifndef _UINT64_T 14564562Sgshapiro#define _UINT64_T 14664562Sgshapirotypedef unsigned long long uint64_t; 14764562Sgshapiro#endif // _UINT64_T 14864562Sgshapiro// %%%% how to access definition of intptr_t portably in 5.5 onward? 14964562Sgshapirotypedef int intptr_t; 15064562Sgshapirotypedef unsigned int uintptr_t; 15164562Sgshapiro// If this gets an error, figure out a symbol XXX that implies the 15264562Sgshapiro// prior definition of intptr_t, and add "&& !defined(XXX)" above. 15364562Sgshapiro#endif // _SYS_INT_TYPES_H 15464562Sgshapiro 15564562Sgshapiro#endif // !LINUX 15664562Sgshapiro 15764562Sgshapiro// Additional Java basic types 15838032Speter 15990792Sgshapirotypedef uint8_t jubyte; 16064562Sgshapirotypedef uint16_t jushort; 16164562Sgshapirotypedef uint32_t juint; 16238032Spetertypedef uint64_t julong; 16338032Speter 16438032Speter//---------------------------------------------------------------------------------------------------- 16538032Speter// Special (possibly not-portable) casts 16638032Speter// Cast floats into same-size integers and vice-versa w/o changing bit-pattern 16738032Speter// %%%%%% These seem like standard C++ to me--how about factoring them out? - Ungar 168168515Sgshapiro 169168515Sgshapiroinline jint jint_cast (jfloat x) { return *(jint* )&x; } 17038032Speterinline jlong jlong_cast (jdouble x) { return *(jlong* )&x; } 171168515Sgshapiro 17290792Sgshapiroinline jfloat jfloat_cast (jint x) { return *(jfloat* )&x; } 17338032Speterinline jdouble jdouble_cast(jlong x) { return *(jdouble*)&x; } 17438032Speter 17538032Speter//---------------------------------------------------------------------------------------------------- 17638032Speter// Constant for jlong (specifying an long long canstant is C++ compiler specific) 17738032Speter 17838032Speter// Build a 64bit integer constant 17990792Sgshapiro#define CONST64(x) (x ## LL) 18038032Speter#define UCONST64(x) (x ## ULL) 18138032Speter 18238032Speterconst jlong min_jlong = CONST64(0x8000000000000000); 18390792Sgshapiroconst jlong max_jlong = CONST64(0x7fffffffffffffff); 18490792Sgshapiro 18590792Sgshapiro 18638032Speter#ifdef SOLARIS 18738032Speter//---------------------------------------------------------------------------------------------------- 18838032Speter// ANSI C++ fixes 18990792Sgshapiro// NOTE:In the ANSI committee's continuing attempt to make each version 19038032Speter// of C++ incompatible with the previous version, you can no longer cast 19138032Speter// pointers to functions without specifying linkage unless you want to get 19238032Speter// warnings. 19338032Speter// 19438032Speter// This also means that pointers to functions can no longer be "hidden" 19590792Sgshapiro// in opaque types like void * because at the invokation point warnings 19638032Speter// will be generated. While this makes perfect sense from a type safety 19738032Speter// point of view it causes a lot of warnings on old code using C header 19838032Speter// files. Here are some typedefs to make the job of silencing warnings 19938032Speter// a bit easier. 20038032Speter// 20138032Speter// The final kick in the teeth is that you can only have extern "C" linkage 20238032Speter// specified at file scope. So these typedefs are here rather than in the 20338032Speter// .hpp for the class (os:Solaris usually) that needs them. 20438032Speter 20538032Speterextern "C" { 20638032Speter typedef int (*int_fnP_thread_t_iP_uP_stack_tP_gregset_t)(thread_t, int*, unsigned *, stack_t*, gregset_t); 20738032Speter typedef int (*int_fnP_thread_t_i_gregset_t)(thread_t, int, gregset_t); 20864562Sgshapiro typedef int (*int_fnP_thread_t_i)(thread_t, int); 20990792Sgshapiro typedef int (*int_fnP_thread_t)(thread_t); 21038032Speter 21138032Speter typedef int (*int_fnP_cond_tP_mutex_tP_timestruc_tP)(cond_t *cv, mutex_t *mx, timestruc_t *abst); 21290792Sgshapiro typedef int (*int_fnP_cond_tP_mutex_tP)(cond_t *cv, mutex_t *mx); 21338032Speter 21438032Speter // typedef for missing API in libc 21590792Sgshapiro typedef int (*int_fnP_mutex_tP_i_vP)(mutex_t *, int, void *); 21690792Sgshapiro typedef int (*int_fnP_mutex_tP)(mutex_t *); 21790792Sgshapiro typedef int (*int_fnP_cond_tP_i_vP)(cond_t *cv, int scope, void *arg); 21890792Sgshapiro typedef int (*int_fnP_cond_tP)(cond_t *cv); 21938032Speter}; 22038032Speter#endif // SOLARIS 22138032Speter 22238032Speter//---------------------------------------------------------------------------------------------------- 22338032Speter// Debugging 22438032Speter 22538032Speter#define DEBUG_EXCEPTION ::abort(); 22638032Speter 22738032Speter#ifdef ARM 22864562Sgshapiro#ifdef SOLARIS 22938032Speter#define BREAKPOINT __asm__ volatile (".long 0xe1200070") 23038032Speter#else 23190792Sgshapiro#define BREAKPOINT __asm__ volatile (".long 0xe7f001f0") 23238032Speter#endif 23338032Speter#else 23490792Sgshapiroextern "C" void breakpoint(); 23590792Sgshapiro#define BREAKPOINT ::breakpoint() 23690792Sgshapiro#endif 23790792Sgshapiro 23890792Sgshapiro// checking for nanness 23990792Sgshapiro#ifdef SOLARIS 24090792Sgshapiro#ifdef SPARC 24190792Sgshapiroinline int g_isnan(float f) { return isnanf(f); } 24290792Sgshapiro#else 24390792Sgshapiro// isnanf() broken on Intel Solaris use isnand() 24490792Sgshapiroinline int g_isnan(float f) { return isnand(f); } 24538032Speter#endif 24690792Sgshapiroinline int g_isnan(double f) { return isnand(f); } 24738032Speter#elif LINUX 24890792Sgshapiroinline int g_isnan(float f) { return isnanf(f); } 24938032Speterinline int g_isnan(double f) { return isnan(f); } 25038032Speter#else 25138032Speter#error "missing platform-specific definition here" 25238032Speter#endif 25338032Speter 25438032Speter// GCC 4.3 does not allow 0.0/0.0 to produce a NAN value 25538032Speter#if (__GNUC__ == 4) && (__GNUC_MINOR__ > 2) 25638032Speter#define CAN_USE_NAN_DEFINE 1 25738032Speter#endif 25838032Speter 25938032Speter 26038032Speter// Checking for finiteness 26138032Speter 26238032Speterinline int g_isfinite(jfloat f) { return finite(f); } 26338032Speterinline int g_isfinite(jdouble f) { return finite(f); } 26438032Speter 26538032Speter 26638032Speter// Wide characters 26738032Speter 26838032Speterinline int wcslen(const jchar* x) { return wcslen((const wchar_t*)x); } 26938032Speter 27090792Sgshapiro 27138032Speter// Portability macros 27238032Speter#define PRAGMA_INTERFACE #pragma interface 27338032Speter#define PRAGMA_IMPLEMENTATION #pragma implementation 27438032Speter#define VALUE_OBJ_CLASS_SPEC 27538032Speter 27638032Speter#if (__GNUC__ == 2) && (__GNUC_MINOR__ < 95) 27738032Speter#define TEMPLATE_TABLE_BUG 27838032Speter#endif 27938032Speter#if (__GNUC__ == 2) && (__GNUC_MINOR__ >= 96) 28038032Speter#define CONST_SDM_BUG 28138032Speter#endif 28238032Speter 28338032Speter// Formatting. 28438032Speter#ifdef _LP64 28538032Speter#define FORMAT64_MODIFIER "l" 28638032Speter#else // !_LP64 28738032Speter#define FORMAT64_MODIFIER "ll" 28838032Speter#endif // _LP64 28938032Speter 29090792Sgshapiro// HACK: gcc warns about applying offsetof() to non-POD object or calculating 291168515Sgshapiro// offset directly when base address is NULL. Use 16 to get around the 29238032Speter// warning. gcc-3.4 has an option -Wno-invalid-offsetof to suppress 29338032Speter// this warning. 29438032Speter#define offset_of(klass,field) (size_t)((intx)&(((klass*)16)->field) - 16) 29538032Speter 29638032Speter#ifdef offsetof 29738032Speter# undef offsetof 29838032Speter#endif 299168515Sgshapiro#define offsetof(klass,field) offset_of(klass,field) 30038032Speter 30138032Speter#endif // SHARE_VM_UTILITIES_GLOBALDEFINITIONS_GCC_HPP 302168515Sgshapiro