globalDefinitions_gcc.hpp revision 0:a61af66fc99e
12896Saeremeev/* 22896Saeremeev * Copyright 1998-2007 Sun Microsystems, Inc. All Rights Reserved. 32896Saeremeev * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 42896Saeremeev * 52896Saeremeev * This code is free software; you can redistribute it and/or modify it 62896Saeremeev * under the terms of the GNU General Public License version 2 only, as 72896Saeremeev * published by the Free Software Foundation. 82896Saeremeev * 92896Saeremeev * This code is distributed in the hope that it will be useful, but WITHOUT 102896Saeremeev * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 112896Saeremeev * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 122896Saeremeev * version 2 for more details (a copy is included in the LICENSE file that 132896Saeremeev * accompanied this code). 142896Saeremeev * 152896Saeremeev * You should have received a copy of the GNU General Public License version 162896Saeremeev * 2 along with this work; if not, write to the Free Software Foundation, 172896Saeremeev * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 182896Saeremeev * 192896Saeremeev * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 202896Saeremeev * CA 95054 USA or visit www.sun.com if you need additional information or 212896Saeremeev * have any questions. 222896Saeremeev * 232896Saeremeev */ 242896Saeremeev 252896Saeremeev// This file holds compiler-dependent includes, 262896Saeremeev// globally used constants & types, class (forward) 272896Saeremeev// declarations and a few frequently used utility functions. 282896Saeremeev 292896Saeremeev#include <ctype.h> 302896Saeremeev#include <string.h> 31#include <stdarg.h> 32#include <stddef.h> 33#include <stdio.h> 34#include <stdlib.h> 35#include <wchar.h> 36 37#ifdef SOLARIS 38#include <ieeefp.h> 39#endif // SOLARIS 40 41#include <math.h> 42#ifndef FP_PZERO 43// Linux doesn't have positive/negative zero 44#define FP_PZERO FP_ZERO 45#endif 46#if (!defined fpclass) && ((!defined SPARC) || (!defined SOLARIS)) 47#define fpclass fpclassify 48#endif 49 50#include <time.h> 51#include <fcntl.h> 52#include <dlfcn.h> 53#include <pthread.h> 54 55#ifdef SOLARIS 56#include <thread.h> 57#endif // SOLARIS 58 59#include <limits.h> 60#include <errno.h> 61 62#ifdef SOLARIS 63#include <sys/trap.h> 64#include <sys/regset.h> 65#include <sys/procset.h> 66#include <ucontext.h> 67#include <setjmp.h> 68#endif // SOLARIS 69 70# ifdef SOLARIS_MUTATOR_LIBTHREAD 71# include <sys/procfs.h> 72# endif 73 74#ifdef LINUX 75#include <inttypes.h> 76#include <signal.h> 77#include <ucontext.h> 78#include <sys/time.h> 79#endif // LINUX 80 81// 4810578: varargs unsafe on 32-bit integer/64-bit pointer architectures 82// When __cplusplus is defined, NULL is defined as 0 (32-bit constant) in 83// system header files. On 32-bit architectures, there is no problem. 84// On 64-bit architectures, defining NULL as a 32-bit constant can cause 85// problems with varargs functions: C++ integral promotion rules say for 86// varargs, we pass the argument 0 as an int. So, if NULL was passed to a 87// varargs function it will remain 32-bits. Depending on the calling 88// convention of the machine, if the argument is passed on the stack then 89// only 32-bits of the "NULL" pointer may be initialized to zero. The 90// other 32-bits will be garbage. If the varargs function is expecting a 91// pointer when it extracts the argument, then we have a problem. 92// 93// Solution: For 64-bit architectures, redefine NULL as 64-bit constant 0. 94// 95// Note: this fix doesn't work well on Linux because NULL will be overwritten 96// whenever a system header file is included. Linux handles NULL correctly 97// through a special type '__null'. 98#ifdef SOLARIS 99 #ifdef _LP64 100 #undef NULL 101 #define NULL 0L 102 #else 103 #ifndef NULL 104 #define NULL 0 105 #endif 106 #endif 107#endif 108 109// NULL vs NULL_WORD: 110// On Linux NULL is defined as a special type '__null'. Assigning __null to 111// integer variable will cause gcc warning. Use NULL_WORD in places where a 112// pointer is stored as integer value. On some platforms, sizeof(intptr_t) > 113// sizeof(void*), so here we want something which is integer type, but has the 114// same size as a pointer. 115#ifdef LINUX 116 #ifdef _LP64 117 #define NULL_WORD 0L 118 #else 119 #define NULL_WORD 0 120 #endif 121#else 122 #define NULL_WORD NULL 123#endif 124 125#ifndef LINUX 126// Compiler-specific primitive types 127typedef unsigned short uint16_t; 128#ifndef _UINT32_T 129#define _UINT32_T 130typedef unsigned int uint32_t; 131#endif // _UINT32_T 132 133#if !defined(_SYS_INT_TYPES_H) 134#ifndef _UINT64_T 135#define _UINT64_T 136typedef unsigned long long uint64_t; 137#endif // _UINT64_T 138// %%%% how to access definition of intptr_t portably in 5.5 onward? 139typedef int intptr_t; 140typedef unsigned int uintptr_t; 141// If this gets an error, figure out a symbol XXX that implies the 142// prior definition of intptr_t, and add "&& !defined(XXX)" above. 143#endif // _SYS_INT_TYPES_H 144 145#endif // !LINUX 146 147// Additional Java basic types 148 149typedef uint8_t jubyte; 150typedef uint16_t jushort; 151typedef uint32_t juint; 152typedef uint64_t julong; 153 154//---------------------------------------------------------------------------------------------------- 155// Special (possibly not-portable) casts 156// Cast floats into same-size integers and vice-versa w/o changing bit-pattern 157// %%%%%% These seem like standard C++ to me--how about factoring them out? - Ungar 158 159inline jint jint_cast (jfloat x) { return *(jint* )&x; } 160inline jlong jlong_cast (jdouble x) { return *(jlong* )&x; } 161 162inline jfloat jfloat_cast (jint x) { return *(jfloat* )&x; } 163inline jdouble jdouble_cast(jlong x) { return *(jdouble*)&x; } 164 165//---------------------------------------------------------------------------------------------------- 166// Constant for jlong (specifying an long long canstant is C++ compiler specific) 167 168// Build a 64bit integer constant 169#define CONST64(x) (x ## LL) 170#define UCONST64(x) (x ## ULL) 171 172const jlong min_jlong = CONST64(0x8000000000000000); 173const jlong max_jlong = CONST64(0x7fffffffffffffff); 174 175 176#ifdef SOLARIS 177//---------------------------------------------------------------------------------------------------- 178// ANSI C++ fixes 179// NOTE:In the ANSI committee's continuing attempt to make each version 180// of C++ incompatible with the previous version, you can no longer cast 181// pointers to functions without specifying linkage unless you want to get 182// warnings. 183// 184// This also means that pointers to functions can no longer be "hidden" 185// in opaque types like void * because at the invokation point warnings 186// will be generated. While this makes perfect sense from a type safety 187// point of view it causes a lot of warnings on old code using C header 188// files. Here are some typedefs to make the job of silencing warnings 189// a bit easier. 190// 191// The final kick in the teeth is that you can only have extern "C" linkage 192// specified at file scope. So these typedefs are here rather than in the 193// .hpp for the class (os:Solaris usually) that needs them. 194 195extern "C" { 196 typedef int (*int_fnP_thread_t_iP_uP_stack_tP_gregset_t)(thread_t, int*, unsigned *, stack_t*, gregset_t); 197 typedef int (*int_fnP_thread_t_i_gregset_t)(thread_t, int, gregset_t); 198 typedef int (*int_fnP_thread_t_i)(thread_t, int); 199 typedef int (*int_fnP_thread_t)(thread_t); 200 201 typedef int (*int_fnP_cond_tP_mutex_tP_timestruc_tP)(cond_t *cv, mutex_t *mx, timestruc_t *abst); 202 typedef int (*int_fnP_cond_tP_mutex_tP)(cond_t *cv, mutex_t *mx); 203 204 // typedef for missing API in libc 205 typedef int (*int_fnP_mutex_tP_i_vP)(mutex_t *, int, void *); 206 typedef int (*int_fnP_mutex_tP)(mutex_t *); 207 typedef int (*int_fnP_cond_tP_i_vP)(cond_t *cv, int scope, void *arg); 208 typedef int (*int_fnP_cond_tP)(cond_t *cv); 209}; 210#endif // SOLARIS 211 212//---------------------------------------------------------------------------------------------------- 213// Debugging 214 215#define DEBUG_EXCEPTION ::abort(); 216 217extern "C" void breakpoint(); 218#define BREAKPOINT ::breakpoint() 219 220// checking for nanness 221#ifdef SOLARIS 222#ifdef SPARC 223inline int g_isnan(float f) { return isnanf(f); } 224#else 225// isnanf() broken on Intel Solaris use isnand() 226inline int g_isnan(float f) { return isnand(f); } 227#endif 228inline int g_isnan(double f) { return isnand(f); } 229#elif LINUX 230inline int g_isnan(float f) { return isnanf(f); } 231inline int g_isnan(double f) { return isnan(f); } 232#else 233#error "missing platform-specific definition here" 234#endif 235 236// Checking for finiteness 237 238inline int g_isfinite(jfloat f) { return finite(f); } 239inline int g_isfinite(jdouble f) { return finite(f); } 240 241 242// Wide characters 243 244inline int wcslen(const jchar* x) { return wcslen((const wchar_t*)x); } 245 246 247// Portability macros 248#define PRAGMA_INTERFACE #pragma interface 249#define PRAGMA_IMPLEMENTATION #pragma implementation 250#define VALUE_OBJ_CLASS_SPEC 251 252#if (__GNUC__ == 2) && (__GNUC_MINOR__ < 95) 253#define TEMPLATE_TABLE_BUG 254#endif 255#if (__GNUC__ == 2) && (__GNUC_MINOR__ >= 96) 256#define CONST_SDM_BUG 257#endif 258 259// Formatting. 260#ifdef _LP64 261#define FORMAT64_MODIFIER "l" 262#else // !_LP64 263#define FORMAT64_MODIFIER "ll" 264#endif // _LP64 265 266// HACK: gcc warns about applying offsetof() to non-POD object or calculating 267// offset directly when base address is NULL. Use 16 to get around the 268// warning. gcc-3.4 has an option -Wno-invalid-offsetof to suppress 269// this warning. 270#define offset_of(klass,field) (size_t)((intx)&(((klass*)16)->field) - 16) 271 272#ifdef offsetof 273# undef offsetof 274#endif 275#define offsetof(klass,field) offset_of(klass,field) 276