00001 /* 00002 FALCON - The Falcon Programming Language. 00003 FILE: flc_common.h 00004 00005 Definition for falcon common library. 00006 ------------------------------------------------------------------- 00007 Author: Giancarlo Niccolai 00008 Begin: dom giu 20 2004 00009 00010 ------------------------------------------------------------------- 00011 (C) Copyright 2004: the FALCON developers (see list in AUTHORS file) 00012 00013 See LICENSE file for licensing details. 00014 */ 00015 00016 00017 #ifndef flc_COMMON_H 00018 #define flc_COMMON_H 00019 00020 #include <falcon/types.h> 00021 00022 namespace Falcon 00023 { 00024 00025 class String; 00026 00027 #define flc_CURRENT_VER 1 00028 #define flc_CURRENT_SUB 0 00029 00030 00031 #define flcC_SYM_VAR 0 00032 #define flcC_SYM_FUNC 1 00033 #define flcC_SYM_EXT 2 00034 #define flcC_SYM_CLASS 3 00035 #define flcC_SYM_VARPROP 4 00036 #define flcC_EXPORT_BIT 0x80 00037 00038 #define flcC_VAL_NIL 0 00039 #define flcC_VAL_INT 1 00040 #define flcC_VAL_NUM 2 00041 #define flcC_VAL_STRID 3 00042 #define flcC_VAL_SIMID 4 00043 00045 #define flc_HASH_SEED 0xC2AF3DE4 00046 00047 00055 class CharPtrCmp 00056 { 00057 public: 00058 bool operator() ( const char *s1, const char *s2 ) const 00059 { 00060 while( *s1 && *s2 && *s1 == *s2 ) { 00061 s1 ++; 00062 s2 ++; 00063 } 00064 return (*s1 < *s2); 00065 } 00066 }; 00067 00068 #define flc_ASM_GLOBAL 0 00069 #define flc_ASM_LOCAL 1 00070 #define flc_ASM_PARAM 2 00071 00072 00073 #if FALCON_LITTLE_ENDIAN == 1 00074 00075 inline uint64 grabInt64( void* data ) { return *(uint64*)data; } 00076 inline int64 loadInt64( void* data ) { return *(int64*)data; } 00077 inline numeric grabNum( void* data ) { return *(numeric*)data; } 00078 inline numeric loadNum( void* data ) { return *(numeric*)data; } 00079 00080 inline uint64 endianInt64( const uint64 param ) { return param; } 00081 inline uint32 endianInt32( const uint32 param ) { return param; } 00082 inline uint16 endianInt16( const uint16 param ) { return param; } 00083 inline numeric endianNum( const numeric param ) { return param; } 00084 00085 #else 00086 00087 inline uint64 endianInt64( const uint64 param ) { 00088 byte *chars = (byte *) ¶m; 00089 return ((uint64)chars[7]) << 56 | ((uint64)chars[6]) << 48 | ((uint64)chars[5]) << 40 | 00090 ((uint64)chars[4]) << 32 | ((uint64)chars[3]) << 24 | ((uint64)chars[2]) << 16 | 00091 ((uint64)chars[1]) << 8 | ((uint64)chars[0]); 00092 } 00093 00094 inline uint64 grabInt64( void* data ) { 00095 byte *chars = (byte *) data; 00096 return ((uint64)chars[7]) << 56 | ((uint64)chars[6]) << 48 | ((uint64)chars[5]) << 40 | 00097 ((uint64)chars[4]) << 32 | ((uint64)chars[3]) << 24 | ((uint64)chars[2]) << 16 | 00098 ((uint64)chars[1]) << 8 | ((uint64)chars[0]); 00099 } 00100 00101 00102 inline numeric grabNum( void* numMemory ) 00103 { 00104 const byte* data = (const byte*) numMemory; 00105 00106 union t_unumeric { 00107 byte buffer[ sizeof(numeric) ]; 00108 numeric number; 00109 } unumeric; 00110 00111 uint32 i; 00112 for ( i = 0; i < sizeof( numeric ); i++ ) { 00113 unumeric.buffer[i] = data[(sizeof( numeric )-1) - i]; 00114 } 00115 00116 return unumeric.number; 00117 } 00118 00119 inline numeric endianNum( const numeric ¶m ) 00120 { 00121 return grabNum( (void*) ¶m ); 00122 } 00123 00124 00125 inline numeric loadNum( void* data ) 00126 { 00127 byte* bdata = (byte*) data; 00128 00129 union t_unumeric { 00130 struct t_integer { 00131 uint32 high; 00132 uint32 low; 00133 } integer; 00134 numeric number; 00135 } unumeric; 00136 00137 unumeric.integer.high = *reinterpret_cast<uint32*>(bdata); 00138 unumeric.integer.low = *reinterpret_cast<uint32*>(bdata+sizeof(uint32)); 00139 00140 return unumeric.number; 00141 } 00142 00143 00144 inline int64 loadInt64( void* data ) 00145 { 00146 byte* bdata = (byte*) data; 00147 00148 uint64 res = *reinterpret_cast<uint32*>(bdata); 00149 res <<= 32; 00150 res |= *reinterpret_cast<uint32*>(bdata+sizeof(uint32)); 00151 return (int64) res; 00152 } 00153 00154 00155 inline uint32 endianInt32( const uint32 param ) { 00156 byte *chars = (byte *) ¶m; 00157 return ((uint32)chars[3]) << 24 | ((uint32)chars[2]) << 16 | ((uint32)chars[1]) << 8 | ((uint32)chars[0]); 00158 } 00159 00160 inline uint16 endianInt16( const uint16 param ) { 00161 byte *chars = (byte *) ¶m; 00162 return ((uint32)chars[1]) << 8 | ((uint32)chars[0]); 00163 } 00164 00165 #endif /* FALCON_LITTLE_ENDIAN */ 00166 00167 inline int charToHex( const char elem ) 00168 { 00169 if( elem >= '0' && elem <= '9' ) 00170 return elem - '0'; 00171 else if( elem >= 'A' && elem <= 'F' ) 00172 return elem - 'A'; 00173 else if( elem >= 'a' && elem <= 'f' ) 00174 return elem - 'a'; 00175 00176 return -1; 00177 } 00178 00179 FALCON_DYN_SYM uint32 calcMemHash( const char *memory, uint32 size ); 00180 FALCON_DYN_SYM uint32 calcCstrHash( const char *cstring ); 00181 FALCON_DYN_SYM uint32 calcStringHash( const String &string ); 00182 inline uint32 calcIntHash( const int32 number ) { return flc_HASH_SEED * number; } 00183 00184 } 00185 00186 #endif 00187 00188 /* end of flc_common.h */