msousa@597: /* msousa@597: * matiec - a compiler for the programming languages defined in IEC 61131-3 msousa@597: * Copyright (C) 2003-2011 Mario de Sousa (msousa@fe.up.pt) msousa@597: * Copyright (C) 2007-2011 Laurent Bessard and Edouard Tisserant msousa@597: * msousa@597: * This program is free software: you can redistribute it and/or modify msousa@597: * it under the terms of the GNU General Public License as published by msousa@597: * the Free Software Foundation, either version 3 of the License, or msousa@597: * (at your option) any later version. msousa@597: * msousa@597: * This program is distributed in the hope that it will be useful, msousa@597: * but WITHOUT ANY WARRANTY; without even the implied warranty of msousa@597: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the msousa@597: * GNU General Public License for more details. msousa@597: * msousa@597: * You should have received a copy of the GNU General Public License msousa@597: * along with this program. If not, see . msousa@597: * msousa@597: * msousa@597: * This code is made available on the understanding that it will not be msousa@597: * used in safety-critical situations without a full and competent review. msousa@597: */ msousa@597: msousa@597: /* msousa@597: * An IEC 61131-3 compiler. msousa@597: * msousa@597: * Based on the msousa@597: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) msousa@597: * msousa@597: */ msousa@597: msousa@597: msousa@597: msousa@597: #ifndef _MAIN_HH msousa@597: #define _MAIN_HH msousa@597: msousa@604: msousa@604: mjsousa@946: /* Compiler options, specified at runtime on the command line */ mjsousa@946: mjsousa@946: typedef struct { mjsousa@946: /* options specific to stage1_2 */ mjsousa@956: bool pre_parsing; /* Support forward references (Run a pre-parsing phase before the defintive parsing phase that builds the AST) */ mjsousa@946: bool safe_extensions; /* support SAFE_* datatypes defined in PLCOpen TC5 "Safety Software Technical Specification - Part 1" v1.0 */ mjsousa@946: bool full_token_loc; /* error messages specify full token location */ mjsousa@946: bool conversion_functions; /* Create a conversion function for derived datatype */ mjsousa@946: bool nested_comments; /* Allow the use of nested comments. */ mjsousa@946: bool ref_standard_extensions; /* Allow the use of REFerences (keywords REF_TO, REF, DREF, ^, NULL). */ mjsousa@946: bool ref_nonstand_extensions; /* Allow the use of non-standard extensions to REF_TO datatypes: REF_TO ANY, and REF_TO in struct elements! */ mjsousa@980: bool nonliteral_in_array_size; /* Allow the use of constant non-literals when specifying size of arrays (ARRAY [1..max] OF INT) */ mjsousa@946: const char *includedir; /* Include directory, where included files will be searched for... */ mjsousa@946: mjsousa@946: /* options specific to stage3 */ mjsousa@946: bool relaxed_datatype_model; /* Use the relaxed datatype equivalence model, instead of the default strict equivalence model */ mjsousa@946: } runtime_options_t; mjsousa@946: mjsousa@946: extern runtime_options_t runtime_options; mjsousa@946: mjsousa@946: mjsousa@946: mjsousa@946: mjsousa@946: mjsousa@946: msousa@597: /* Function used throughout the code --> used to report failed assertions (i.e. internal compiler errors)! */ msousa@604: #include /* required for NULL */ msousa@604: msousa@597: #define ERROR error_exit(__FILE__,__LINE__) agraeper@654: #define ERROR_MSG(msg, ...) error_exit(__FILE__,__LINE__, msg, ## __VA_ARGS__) msousa@597: msousa@597: extern void error_exit(const char *file_name, int line_no, const char *errmsg = NULL, ...); msousa@597: msousa@597: msousa@597: msousa@597: msousa@607: /* Get the definition of INT16_MAX, INT16_MIN, UINT64_MAX, INT64_MAX, INT64_MIN, ... */ mjsousa@760: #ifndef __STDC_LIMIT_MACROS msousa@607: #define __STDC_LIMIT_MACROS /* required to have UINTxx_MAX defined when including stdint.h from C++ source code. */ mjsousa@760: #endif msousa@607: #include msousa@607: #include msousa@607: msousa@607: #ifndef UINT64_MAX msousa@607: #define UINT64_MAX (std::numeric_limits< uint64_t >::max()) msousa@607: #endif msousa@607: #ifndef INT64_MAX msousa@607: #define INT64_MAX (std::numeric_limits< int64_t >::max()) msousa@607: #endif msousa@607: #ifndef INT64_MIN msousa@607: #define INT64_MIN (std::numeric_limits< int64_t >::min()) msousa@607: #endif msousa@607: msousa@607: msousa@607: msousa@607: /* Determine, for the current platform, which datas types (float, double or long double) use 64 and 32 bits. */ msousa@607: /* NOTE: We cant use sizeof() in pre-processor directives, so we have to do it another way... */ msousa@607: /* CURIOSITY: We can use sizeof() and offsetof() inside static_assert() but: msousa@607: * - this only allows us to make assertions, and not #define new macros msousa@607: * - is only available in the C standard [ISO/IEC 9899:2011] and the C++ 0X draft standard [Becker 2008]. It is not available in C99. msousa@607: * https://www.securecoding.cert.org/confluence/display/seccode/DCL03-C.+Use+a+static+assertion+to+test+the+value+of+a+constant+expression msousa@607: * struct {int a, b, c, d} header_t; msousa@607: * e.g.: static_assert(offsetof(struct header_t, c) == 8, "Compile time error message."); msousa@607: */ msousa@607: msousa@607: #include msousa@607: #if (LDBL_MANT_DIG == 53) /* NOTE: 64 bit IEC559 real has 53 bits for mantissa! */ mjsousa@890: #define real64_tX long_double /* so we can later use #if (real64_t == long_double) directives in the code! */ mjsousa@890: #define real64_t long double /* NOTE: no underscore '_' between 'long' and 'double' */ msousa@607: #define REAL64_MAX LDBL_MAX msousa@607: #elif ( DBL_MANT_DIG == 53) /* NOTE: 64 bit IEC559 real has 53 bits for mantissa! */ mjsousa@890: #define real64_tX double mjsousa@890: #define real64_t double msousa@607: #define REAL64_MAX DBL_MAX msousa@607: #elif ( FLT_MANT_DIG == 53) /* NOTE: 64 bit IEC559 real has 53 bits for mantissa! */ mjsousa@890: #define real64_tX float mjsousa@890: #define real64_t float msousa@607: #define REAL64_MAX FLT_MAX msousa@607: #else msousa@607: #error Could not find a 64 bit floating point data type on this platform. Aborting... msousa@607: #endif msousa@607: msousa@607: msousa@607: #if (LDBL_MANT_DIG == 24) /* NOTE: 32 bit IEC559 real has 24 bits for mantissa! */ mjsousa@890: #define real32_tX long_double /* so we can later use #if (real32_t == long_double) directives in the code! */ mjsousa@890: #define real32_t long double /* NOTE: no underscore '_' between 'long' and 'double' */ msousa@607: #define REAL32_MAX LDBL_MAX msousa@607: #elif ( DBL_MANT_DIG == 24) /* NOTE: 32 bit IEC559 real has 24 bits for mantissa! */ mjsousa@890: #define real32_tX double mjsousa@890: #define real32_t double msousa@607: #define REAL32_MAX DBL_MAX msousa@607: #elif ( FLT_MANT_DIG == 24) /* NOTE: 32 bit IEC559 real has 24 bits for mantissa! */ mjsousa@890: #define real32_tX float mjsousa@890: #define real32_t float msousa@607: #define REAL32_MAX FLT_MAX msousa@607: #else msousa@607: #error Could not find a 32 bit floating point data type on this platform. Aborting... msousa@607: #endif msousa@607: msousa@607: msousa@607: msousa@607: #include msousa@607: #ifndef INFINITY msousa@607: #error Could not find the macro that defines the value for INFINITY in the current platform. msousa@607: #endif msousa@607: #ifndef NAN msousa@607: #error Could not find the macro that defines the value for NAN in the current platform. msousa@607: #endif msousa@607: msousa@607: msousa@607: msousa@645: /* get the printf format macros for printing variables of fixed data size msousa@645: * e.g. int64_t v; printf("value=%"PRId64" !!\n", v); msousa@645: * e.g. uint64_t v; printf("value=%"PRIu64" !!\n", v); msousa@645: * e.g. uint64_t v; printf("value=%"PRIx64" !!\n", v); // hexadecimal format msousa@645: */ mjsousa@760: #ifndef __STDC_FORMAT_MACROS msousa@645: #define __STDC_FORMAT_MACROS mjsousa@760: #endif msousa@645: #include msousa@607: msousa@607: agraeper@654: #endif // #ifndef _MAIN_HH