| 1 | /** |
|---|
| 2 | * bb_backend.c -> Part of Crules Programming language |
|---|
| 3 | * |
|---|
| 4 | * Crules is the legal property of its developers. Please refer to the |
|---|
| 5 | * COPYRIGHT file distributed with this source distribution. |
|---|
| 6 | * |
|---|
| 7 | * This program is free software: you can redistribute it and/or modify |
|---|
| 8 | * it under the terms of the GNU General Public License as published by |
|---|
| 9 | * the Free Software Foundation, either version 3 of the License, or |
|---|
| 10 | * (at your option) any later version. |
|---|
| 11 | * |
|---|
| 12 | * This program is distributed in the hope that it will be useful, |
|---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 15 | * GNU General Public License for more details. |
|---|
| 16 | * |
|---|
| 17 | * You should have received a copy of the GNU General Public License |
|---|
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 19 | **/ |
|---|
| 20 | |
|---|
| 21 | #ifdef HAVE_CONFIG_H |
|---|
| 22 | # include "config.h" |
|---|
| 23 | #else |
|---|
| 24 | # define CMAKE 1 |
|---|
| 25 | #endif |
|---|
| 26 | |
|---|
| 27 | #include <stdio.h> |
|---|
| 28 | #include <stdlib.h> |
|---|
| 29 | #include <string.h> |
|---|
| 30 | |
|---|
| 31 | #include <stdarg.h> |
|---|
| 32 | #include <signal.h> |
|---|
| 33 | |
|---|
| 34 | #include <crules/crules.h> |
|---|
| 35 | #include <crules/opcodes.h> |
|---|
| 36 | #include <crules/symbols.h> |
|---|
| 37 | #include <crules/objects.h> |
|---|
| 38 | #include <crules/backend.h> |
|---|
| 39 | #include <crules/runtime.h> |
|---|
| 40 | #include <crules/garbage.h> |
|---|
| 41 | #include <crules/operators.h> |
|---|
| 42 | |
|---|
| 43 | // so as we can free the lexer on a fatal! |
|---|
| 44 | extern void yylex_destroy( void ); |
|---|
| 45 | |
|---|
| 46 | #ifdef DEBUG |
|---|
| 47 | static volatile int n_malloc = 0; |
|---|
| 48 | static volatile int n_calloc = 0; |
|---|
| 49 | static volatile int n_realloc = 0; |
|---|
| 50 | static volatile int n_free = 0; |
|---|
| 51 | #endif |
|---|
| 52 | |
|---|
| 53 | extern void crl_catch_signal( int signal ); |
|---|
| 54 | |
|---|
| 55 | inline |
|---|
| 56 | void crl_init_signals( void ) |
|---|
| 57 | { |
|---|
| 58 | void (*sig_catch)( int ); |
|---|
| 59 | sig_catch= &crl_catch_signal; |
|---|
| 60 | |
|---|
| 61 | signal( SIGINT, sig_catch ); |
|---|
| 62 | signal( SIGQUIT, sig_catch ); |
|---|
| 63 | signal( SIGTERM, sig_catch ); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | /** |
|---|
| 67 | *\return basic malloc but xmalloc, fail on exhaustion |
|---|
| 68 | **/ |
|---|
| 69 | inline |
|---|
| 70 | void * crl_malloc ( size_t size ) |
|---|
| 71 | __crl_malloc |
|---|
| 72 | { |
|---|
| 73 | register void* mm= malloc( size ); |
|---|
| 74 | #ifdef DEBUG |
|---|
| 75 | n_malloc++; |
|---|
| 76 | #endif |
|---|
| 77 | if ( !mm ) |
|---|
| 78 | crl_fatal("Heap size limit reached!\n"); |
|---|
| 79 | |
|---|
| 80 | return mm; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | /** |
|---|
| 84 | *\return basic calloc but fail on exhaustion |
|---|
| 85 | **/ |
|---|
| 86 | inline |
|---|
| 87 | void * crl_calloc( size_t num, size_t size ) |
|---|
| 88 | __crl_malloc |
|---|
| 89 | { |
|---|
| 90 | register void* mm= calloc( num, size ); |
|---|
| 91 | #ifdef DEBUG |
|---|
| 92 | n_calloc++; |
|---|
| 93 | #endif |
|---|
| 94 | if ( !mm ) |
|---|
| 95 | crl_fatal("Heap size limit reached!\n"); |
|---|
| 96 | |
|---|
| 97 | return mm; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | /** |
|---|
| 101 | *\return returns realloc of the pointer to that size |
|---|
| 102 | **/ |
|---|
| 103 | inline |
|---|
| 104 | void * crl_realloc ( void *ptr, size_t size ) |
|---|
| 105 | __crl_malloc |
|---|
| 106 | { |
|---|
| 107 | register void* mm= realloc( ptr, size ); |
|---|
| 108 | #ifdef DEBUG |
|---|
| 109 | n_realloc++; |
|---|
| 110 | #endif |
|---|
| 111 | if ( !mm ) |
|---|
| 112 | crl_fatal("Heap size limit reached!\n"); |
|---|
| 113 | |
|---|
| 114 | return mm; |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | #ifdef DEBUG |
|---|
| 118 | inline |
|---|
| 119 | void __crl_free__( size_t size ) |
|---|
| 120 | { |
|---|
| 121 | n_free++; |
|---|
| 122 | } |
|---|
| 123 | #endif |
|---|
| 124 | |
|---|
| 125 | /** |
|---|
| 126 | *\return returns the string dump of str |
|---|
| 127 | **/ |
|---|
| 128 | inline |
|---|
| 129 | char* crl_strdup( const char* str ) |
|---|
| 130 | __crl_pure |
|---|
| 131 | { |
|---|
| 132 | #ifdef HAVE_STRDUP |
|---|
| 133 | return ( strdup( str ) ); |
|---|
| 134 | #else |
|---|
| 135 | register size_t len= (crl_strlen( str )+1); |
|---|
| 136 | register char *ret= crl_malloc( len ); |
|---|
| 137 | memcpy(ret, str, len); |
|---|
| 138 | return ret; |
|---|
| 139 | #endif |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | /** |
|---|
| 143 | * \return the length of the string to the null terminator |
|---|
| 144 | * '\0' |
|---|
| 145 | **/ |
|---|
| 146 | inline |
|---|
| 147 | size_t crl_strlen( const char * str ) |
|---|
| 148 | __crl_pure |
|---|
| 149 | { |
|---|
| 150 | #ifdef HAVE_STRLEN |
|---|
| 151 | return ( strlen( str ) ); |
|---|
| 152 | #else |
|---|
| 153 | size_t i= 0; |
|---|
| 154 | while( *str != '\0' ) |
|---|
| 155 | { |
|---|
| 156 | i++; |
|---|
| 157 | *str++; |
|---|
| 158 | } |
|---|
| 159 | return i; |
|---|
| 160 | #endif |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | #ifdef DEBUG |
|---|
| 164 | void crl_print_stats( void ) |
|---|
| 165 | { |
|---|
| 166 | crl_printf("--- Crules Alloc Stats ---\n"); |
|---|
| 167 | crl_printf("\t* number malloc\t\t%i\n", n_malloc); |
|---|
| 168 | crl_printf("\t* number calloc\t\t%i\n", n_calloc); |
|---|
| 169 | crl_printf("\t* number realloc\t%i\n", n_realloc); |
|---|
| 170 | crl_printf("\t* number free\t\t%i\n", n_free); |
|---|
| 171 | crl_printf("--- --- --- --- ---\n"); |
|---|
| 172 | } |
|---|
| 173 | #endif |
|---|
| 174 | |
|---|
| 175 | inline |
|---|
| 176 | void __crl_printf__( const char* file, unsigned int line, |
|---|
| 177 | const char* functor, const char* fmt, ... ) |
|---|
| 178 | __crl_pure |
|---|
| 179 | { |
|---|
| 180 | fprintf( crlerr, "log: <%s:%s:%i> -> ", |
|---|
| 181 | file, functor, line); |
|---|
| 182 | va_list args; |
|---|
| 183 | va_start( args, fmt ); |
|---|
| 184 | vfprintf( crlerr, fmt, args ); |
|---|
| 185 | va_end( args ); |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | inline |
|---|
| 189 | void __crl_debug__( const char* file, unsigned int line, |
|---|
| 190 | const char* functor, const char* fmt, ... ) |
|---|
| 191 | __crl_pure |
|---|
| 192 | { |
|---|
| 193 | #ifdef DEBUG |
|---|
| 194 | fprintf( crlerr, "debug: <%s:%s:%i> -> ", |
|---|
| 195 | file, functor, line); |
|---|
| 196 | va_list args; |
|---|
| 197 | va_start( args, fmt ); |
|---|
| 198 | vfprintf( crlerr, fmt, args ); |
|---|
| 199 | va_end( args ); |
|---|
| 200 | #else |
|---|
| 201 | return; |
|---|
| 202 | #endif |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | inline |
|---|
| 206 | void __crl_error__( const char* file, unsigned int line, |
|---|
| 207 | const char* functor, const char* fmt, ... ) |
|---|
| 208 | __crl_pure |
|---|
| 209 | { |
|---|
| 210 | #ifdef DEBUG |
|---|
| 211 | fprintf( crlerr, "error: <%s:%s:%i> -> ", |
|---|
| 212 | file, functor, line); |
|---|
| 213 | #else |
|---|
| 214 | fprintf( crlerr, "error: -> "); |
|---|
| 215 | #endif |
|---|
| 216 | va_list args; |
|---|
| 217 | va_start( args, fmt ); |
|---|
| 218 | vfprintf( crlerr, fmt, args ); |
|---|
| 219 | va_end( args ); |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | inline |
|---|
| 223 | void __crl_warning__( const char* file, unsigned int line, |
|---|
| 224 | const char* functor, const char* fmt, ... ) |
|---|
| 225 | __crl_pure |
|---|
| 226 | { |
|---|
| 227 | #ifdef DEBUG |
|---|
| 228 | fprintf( crlerr, "warning: <%s:%s:%i> -> ", |
|---|
| 229 | file, functor, line); |
|---|
| 230 | #else |
|---|
| 231 | fprintf( crlerr, "warning: ->"); |
|---|
| 232 | #endif |
|---|
| 233 | va_list args; |
|---|
| 234 | va_start( args, fmt ); |
|---|
| 235 | vfprintf( crlerr, fmt, args ); |
|---|
| 236 | va_end( args ); |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | inline |
|---|
| 240 | void __crl_fatal__( const char* file, unsigned int line, |
|---|
| 241 | const char* functor, const char* fmt, ... ) |
|---|
| 242 | __crl_no_return |
|---|
| 243 | { |
|---|
| 244 | #ifdef DEBUG |
|---|
| 245 | fprintf( crlerr, "fatal: <%s:%s:%i> -> ", |
|---|
| 246 | file, functor, line); |
|---|
| 247 | #else |
|---|
| 248 | fprintf( crlerr, "error: -> "); |
|---|
| 249 | #endif |
|---|
| 250 | va_list args; |
|---|
| 251 | va_start( args, fmt ); |
|---|
| 252 | vfprintf( crlerr, fmt, args ); |
|---|
| 253 | va_end( args ); |
|---|
| 254 | |
|---|
| 255 | // cleanups before exit! |
|---|
| 256 | crl_cleanup( ); |
|---|
| 257 | exit( EXIT_FAILURE ); |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | /** |
|---|
| 261 | * a way of overiding assert.h to give a more |
|---|
| 262 | * specific assertion method.. crlassert |
|---|
| 263 | **/ |
|---|
| 264 | inline |
|---|
| 265 | void crl_assertion_failed( const char * assertion, unsigned int line, |
|---|
| 266 | const char * file, const char * functor ) |
|---|
| 267 | { |
|---|
| 268 | fprintf( crlout, "fatal: <%s:%s:%i> -> assertion '%s' failed exiting...\n", |
|---|
| 269 | file, functor, line, assertion ); |
|---|
| 270 | crl_cleanup( ); |
|---|
| 271 | exit( EXIT_FAILURE ); |
|---|
| 272 | } |
|---|