1 /* Jitter: VM-independent library. 2 3 Copyright (C) 2016, 2017, 2019, 2020, 2021 Luca Saiu 4 Written by Luca Saiu 5 6 This file is part of Jitter. 7 8 Jitter is free software: you can redistribute it and/or modify 9 it under the terms of the GNU General Public License as published by 10 the Free Software Foundation, either version 3 of the License, or 11 (at your option) any later version. 12 13 Jitter is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with Jitter. If not, see <http://www.gnu.org/licenses/>. */ 20 21 22 #ifndef JITTER_H_ 23 #define JITTER_H_ 24 25 #include <stdlib.h> // for size_t . 26 #include <limits.h> // for CHAR_BIT . 27 #include <stdint.h> 28 #include <inttypes.h> // format strings for standard integer types. 29 #include <string.h> // JITTER_ARCHITECTURE_IS relies on strcmp . 30 31 /* Include the host-dependent header, and make sure that it actually contains 32 some definitions for defensiveness's sake. */ 33 #include <jitter/jitter-config.h> 34 #ifndef JITTER_SIZEOF_VOID_P 35 # error "jitter/jitter-config.h is probably incorrect" 36 #endif // #ifndef JITTER_SIZEOF_VOID_P 37 38 /* Include macros emulating missing GNU C features. */ 39 #include <jitter/jitter-missing.h> 40 41 /* We need some CPP machinery here, for conveninient stringification and token 42 concatenation. */ 43 #include <jitter/jitter-cpp.h> 44 45 46 47 48 /* Fundamental type definitions. 49 * ************************************************************************** */ 50 51 /* Configurations where a char is not 8 bits have never been tested. */ 52 #if CHAR_BIT != 8 53 # warning "The char type of a size different from 8 bits: untested." 54 #endif // #if CHAR_BIT != 8 55 56 /* In Jitter a "word" has, by definition, the same size as a pointer. The 57 following macros define, respectively, how many chars and bits fit in a 58 word. */ 59 #define JITTER_WORD_CHAR_NO \ 60 JITTER_SIZEOF_VOID_P 61 #define JITTER_WORD_BIT_NO \ 62 /* FIXME: redefine as a literal if I need that somewhere, \ 63 for example in inline assembly. */ \ 64 (JITTER_WORD_CHAR_NO * CHAR_BIT) 65 66 /* Define jitter_int and jitter_uint as word-sized integer type, respectively 67 signed and unsigned; also define the format strings, JITTER_PRIi , 68 JITTER_PRIu , JITTER_PRIo and JITTER_PRIx to be used with printf and scanf 69 for those types, in the style of PRIi64 , PRIu64 , PRIo64 and PRIx64 . */ 70 #if JITTER_WORD_BIT_NO == 16 71 /* This will not happen on GNU, but one check costs almost nothing. */ 72 typedef int16_t jitter_int; 73 typedef uint16_t jitter_uint; 74 # define JITTER_PRIi PRIi16 75 # define JITTER_PRIu PRIu16 76 # define JITTER_PRIo PRIo16 77 # define JITTER_PRIx PRIx16 78 #elif JITTER_WORD_BIT_NO == 32 79 typedef int32_t jitter_int; 80 typedef uint32_t jitter_uint; 81 # define JITTER_PRIi PRIi32 82 # define JITTER_PRIu PRIu32 83 # define JITTER_PRIo PRIo32 84 # define JITTER_PRIx PRIx32 85 #elif JITTER_WORD_BIT_NO == 64 86 typedef int64_t jitter_int; 87 typedef uint64_t jitter_uint; 88 # define JITTER_PRIi PRIi64 89 # define JITTER_PRIu PRIu64 90 # define JITTER_PRIo PRIo64 91 # define JITTER_PRIx PRIx64 92 #else 93 # error "can't find a word-sized integer type." 94 #endif // #if (JITTER_SIZEOF_VOID_P == ...) 95 96 /* Define two more format strings for convenience, JITTER_INT_FORMAT and 97 JITTER_UINT_FORMAT; they can be used like "%li" and "%lu" . */ 98 #define JITTER_INT_FORMAT "%" JITTER_PRIi 99 #define JITTER_UINT_FORMAT "%" JITTER_PRIu 100 101 /* Define a word-sized floating-point type. */ 102 #if (JITTER_SIZEOF_VOID_P == JITTER_SIZEOF_FLOAT) 103 typedef float jitter_float; 104 #elif (JITTER_SIZEOF_VOID_P == JITTER_SIZEOF_DOUBLE) 105 typedef double jitter_float; 106 #elif (JITTER_SIZEOF_VOID_P == JITTER_SIZEOF_LONG_DOUBLE) 107 /* This should not happen anywhere, I guess -- on PowerPC long double is 64-bit 108 but so is double which is checked before. Anyway, it costs nothing. */ 109 typedef long double jitter_float; 110 #else 111 # error "can't find a word-sized floating-point type" 112 #endif // #if (JITTER_SIZEOF_VOID_P == ...) 113 114 115 /* A thread, which is to say a label-as-value using the GNU C extension. From 116 the executor point of view the memory is constant, so restrict here is 117 correct and might possibly enable some optimization. */ 118 typedef const void * restrict jitter_thread; 119 120 /* A register index as occurring in an instruction. A pair <class, index> 121 uniquely identifies a register. */ 122 typedef jitter_int jitter_register_index; 123 124 /* The index of an unspecialized instruction, 0-based. */ 125 typedef jitter_int jitter_label_as_index; 126 127 /* Define a word-sized union holding an integer (signed or unsigned) or a 128 pointer value. This fits in a general register, by design; in order to allow 129 this we prefer not to include a floating-point case as well in the same 130 union. 131 The integer fields are word-sized, and therefore they can also hold any enum 132 value without loss of information, given the appropriate signedness; in this 133 project enum values are always non-negative. 134 135 FIXME: is this design a problem on m68k, which has different registers for 136 integers and addresses? Not very high-priority, but I'm curious. */ 137 union jitter_word 138 { 139 /* A signed word-sized integer. */ 140 jitter_int fixnum; 141 142 /* An unsigned word-sized integer. */ 143 jitter_uint ufixnum; 144 145 /* A word pointer. The restrict qualifier may enable some optimization and is 146 correct here: when memory is changed thru the pointer the program being 147 interpreted will explicitly reload when needed; it's useless to let GCC be 148 pessimistic here and assume that everything may be changed by a store thru 149 this. */ 150 union jitter_word * restrict pointer; 151 152 /* A label is in practice a pointer, with the difference that the pointed 153 memory is constant. */ 154 const void *label; 155 156 /* FIXME: are these useful? Pointers to different types, just as a 157 convenience. */ 158 void * restrict pointer_to_void; 159 char * restrict pointer_to_char; 160 161 /* A thread. */ 162 jitter_thread thread; 163 }; 164 165 166 167 168 /* The long long type or some approximation of it. 169 * ************************************************************************** */ 170 171 /* Jitter uses integer types as wide as pointers in most cases, but in a few 172 circumstances, particularly for textual I/O, it is convenient to use the 173 widest integer type available. */ 174 175 /* Provide some replacement for long long if it's not available. In either 176 case define: 177 - the type jitter_long_long , defined as either long long or long ; 178 - the type jitter_ulong_long , defined as the unsigned version of 179 jitter_long_long ; 180 - the format strings JITTER_PRIill and JITTER_PRIull (not including the "%" 181 prefix, for signed and unsigned types, printed only in radix 10), expanding 182 to either "lli" and "llu" or "li" and "lu"; 183 - the macros jitter_strtoll and jitter_strtoull, expanding to either strtoll 184 and strtoull or strtol and strtoul . */ 185 #ifdef JITTER_HAVE_LONG_LONG_INT 186 /* We have a real long long type. Define the type and macros above as trivial 187 wrappers. */ 188 typedef long long jitter_long_long; 189 typedef unsigned long long jitter_ulong_long; 190 # define JITTER_PRIill "lli" 191 # define JITTER_PRIull "llu" 192 # define jitter_strtoll strtoll 193 # define jitter_strtoull strtoull 194 #else 195 /* The type long long is not available in this configuration. Use long in its 196 place. */ 197 typedef long jitter_long_long; 198 typedef unsigned long jitter_ulong_long; 199 # define JITTER_PRIill "li" 200 # define JITTER_PRIull "lu" 201 # define jitter_strtoll strtol 202 # define jitter_strtoull strtoul 203 #endif // #ifdef JITTER_HAVE_LONG_LONG_INT 204 205 206 207 208 /* Word size in bytes and its binary logarithm. 209 * ************************************************************************** */ 210 211 /* Word size in bytes (actually in chars). This expands to an integer literal 212 and therefore is suitable for use in CPP conditionals. */ 213 #define JITTER_BYTES_PER_WORD \ 214 JITTER_SIZEOF_VOID_P 215 216 /* Define the binary logarithm of the word size in bits (a common parameter to 217 use for shifting operations), and the name for a Gas pseudo-op generating a 218 word-size object. These definitions need a dispatch on the word size. */ 219 #if JITTER_BYTES_PER_WORD == 8 220 # define JITTER_LG_BYTES_PER_WORD 3 221 # define JITTER_ASM_WORD ".quad" 222 #elif JITTER_BYTES_PER_WORD == 4 223 # define JITTER_LG_BYTES_PER_WORD 2 224 # define JITTER_ASM_WORD ".long" 225 #elif JITTER_BYTES_PER_WORD == 2 226 # define JITTER_LG_BYTES_PER_WORD 1 227 # define JITTER_ASM_WORD ".word" 228 # warning "Weird: running Jitter on a 16-bit machine; this is untested." 229 #else 230 # error "Weird: this machine's word size is not 8, 4 or 2 bytes" 231 #endif // #if JITTER_BYTES_PER_WORD == ... 232 233 234 235 236 /* Definitions depending on the dispatch model. 237 * ************************************************************************** */ 238 239 /* Sanity check: make sure that the user passed the correct set of CPP flags. 240 It is mandatory to either: 241 - specify flags for one dispatch mode by defining a feature macro on the 242 command line; 243 or: 244 - defining the feature macro JITTER_INTERNAL on the command line, in case 245 this compilation is part of the Jitter utility library or the Jitter C code 246 generator, which are independent from the dispatch. 247 248 Of course a user trying hard to shoot herself in the foot will be able to 249 circumvent this check, but the intent here is to protect her from mistakes 250 which would have subtle consequences. In particular, JITTER_CPPFLAGS and its 251 dispatch-specific variants are defined to contain suitable -I options in 252 sub-package mode, which give priority to the Jitter source and build 253 directories over installed headers. By requiring that the flags are always 254 passed we can reliably prevent conflicts between two different versions of 255 Jitter, one installed and another used in sub-package mode. 256 257 The user does not need to see any of this complexity, as long as she supplies 258 JITTER_CFLAGS or its appropriate dispatch-specific variant. */ 259 #if ! defined (JITTER_DISPATCH_SWITCH) \ 260 && ! defined (JITTER_DISPATCH_DIRECT_THREADING) \ 261 && ! defined (JITTER_DISPATCH_MINIMAL_THREADING) \ 262 && ! defined (JITTER_DISPATCH_NO_THREADING) \ 263 && ! defined (JITTER_INTERNAL) 264 # error "You are using a Jitter header, but forgot to supply the preprocessing \ 265 flags in JITTER_CPPFLAGS or some dispatch-specific variant of it. \ 266 This is very easy to do if you are using the GNU Autotools, and should \ 267 still be easy with other build systems as well. \ 268 Please see \"Building preliminaries\" and the appropriate section of \ 269 \"Building a Jittery program\" in the Jitter manual. \ 270 \ 271 Please do not work around this problem by manually supplying just a \ 272 command-line option to enable a particular dispatch: using \ 273 JITTER_CPPFLAGS or one of its variants as intended will prevent subtle \ 274 problems. See the source code for more information." 275 #endif // no JITTER_CPPFLAGS or -DJITTER_INTERNAL. 276 277 /* Check that one dispatching model is defined with a CPP macro, and define 278 JITTER_REPLICATE if needed. Also define the JITTER_DISPATCH_NAME and 279 JITTER_DISPATCH_NAME_STRING macros as the dispatching model name, to be 280 respectively used as a C identifier and as text for user messages. */ 281 #if defined(JITTER_DISPATCH_SWITCH) 282 # define JITTER_DISPATCH_NAME switch 283 # define JITTER_DISPATCH_NAME_STRING "switch" 284 #elif defined(JITTER_DISPATCH_DIRECT_THREADING) 285 # define JITTER_DISPATCH_NAME direct_threading 286 # define JITTER_DISPATCH_NAME_STRING "direct-threading" 287 #elif defined(JITTER_DISPATCH_MINIMAL_THREADING) 288 # define JITTER_DISPATCH_NAME minimal_threading 289 # define JITTER_DISPATCH_NAME_STRING "minimal-threading" 290 /* Minimal threading requires code replication. */ 291 # define JITTER_REPLICATE 1 292 #elif defined(JITTER_DISPATCH_NO_THREADING) 293 # define JITTER_DISPATCH_NAME no_threading 294 # define JITTER_DISPATCH_NAME_STRING "no-threading" 295 /* No-threading requires code replication. */ 296 # define JITTER_REPLICATE 1 297 #elif ! defined (JITTER_INTERNAL) 298 # error "unknown dispatching model. This should never happen." 299 #endif // #if defined(JITTER_DISPATCH_...) 300 301 /* Sanity check: do not use advanced dispatching models on a system missing 302 prerequisites. It is friendlier to fail here than with some mysterious 303 compilation error later. 304 It is more helpful to have separate checks, so that the user knows what 305 the exact missing requirement is. */ 306 #if (defined (JITTER_DISPATCH_MINIMAL_THREADING) \ 307 || defined (JITTER_DISPATCH_NO_THREADING)) \ 308 && ! defined (JITTER_ENABLE_ASSEMBLY) 309 # error "Invalid configuration: you cannot use minimal-threading or" 310 # error "no-threading when assembly is unimplemented or disabled." 311 #endif // advanced dispatch && ! supported-binary-format 312 #if (defined (JITTER_DISPATCH_MINIMAL_THREADING) \ 313 || defined (JITTER_DISPATCH_NO_THREADING)) \ 314 && ! defined (JITTER_HAVE_KNOWN_BINARY_FORMAT) 315 # error "Invalid configuration: you cannot use minimal-threading or" 316 # error "no-threading when the binary format is unsupported." 317 #endif // advanced dispatch && ! assembly 318 319 320 321 322 /* Compiler version check. 323 * ************************************************************************** */ 324 325 /* Perform a compiler version check, to make sure the compiler being use is the 326 same as the one Jitter was configured for, when the issue matters. */ 327 #include <jitter/jitter-c-compiler-version.h> 328 329 330 331 332 /* The selected dispatching model name as a C identifier. 333 * ************************************************************************** */ 334 335 /* Compute the name of a C global whose name depends on the dispatching model. 336 Only the one for the selected model is defined, and this serves to prevent 337 mistakes when linking jitterc-generated code to a runtime library; the 338 definition is in jitter.c and the code using the global is in vm1.c . */ 339 #define JITTER_DISPATCH_DEPENDENT_GLOBAL_NAME \ 340 JITTER_CONCATENATE_THREE(jitter_this_is_the_runtime_for_, \ 341 JITTER_DISPATCH_NAME, \ 342 _dispatch) 343 344 345 346 347 /* Feature macros derived on other macros. 348 * ************************************************************************** */ 349 350 /* The following macros are useful from many places in C code, in particular in 351 order to know whether some feature should be enabled, depending on the 352 dispatching mode or the architecture. 353 It is convenient to define them here in a centralized way, rather than in 354 individual headers which the user may forget to include before testing 355 whether some feature macro is defined. 356 357 Naming convention: 358 - Feature macros defining whether a functionality could be used have names 359 starting with "JITTER_HAVE_" . 360 - Feature macros defining whether a functionality is actually used (in the 361 current dispatching mode, for the program which is being compiled) have 362 names starting with "JITTER_USE_" . 363 Notice that the feature macros defined in jitter/jitter-config.h are also 364 visible from here. */ 365 366 367 /* This is not a feature macro in the sense of the other macros defined in 368 this section, but it still belongs here because of its practical role. 369 Expand to a (currently non-constant) Boolean expression evaluating to 370 non-false iff the architecture name, as per JITTER_ASSEMBLY_SUBDIRECTORY, 371 is the given one. 372 If the architecture name is unknown then the expansion evaluates to false 373 with any argument. 374 The macro argument must evaluate to a C string. */ 375 #ifdef JITTER_ASSEMBLY_SUBDIRECTORY 376 #define JITTER_ARCHITECTURE_IS(jitter_architecture_name_as_string) \ 377 (! strcmp ((jitter_architecture_name_as_string), \ 378 JITTER_ASSEMBLY_SUBDIRECTORY)) 379 #else // unknown architecture 380 #define JITTER_ARCHITECTURE_IS(jitter_architecture_name_as_string) \ 381 false 382 #endif // #ifdef JITTER_ASSEMBLY_SUBDIRECTORY 383 384 /* Some machine-specific headers, included below, use jitter/jitter-arithmetic.h 385 . Include it now, afte we have defined our integer types. */ 386 #include <jitter/jitter-arithmetic.h> 387 388 /* Include the machine-specific header, if we are actually using assembly. */ 389 #ifdef JITTER_ENABLE_ASSEMBLY 390 # include <jitter/machine/jitter-machine.h> 391 #endif // #ifdef JITTER_ENABLE_ASSEMBLY 392 393 #endif // #ifndef JITTER_CONFIG_H_ 394