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: 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, ... */ msousa@607: msousa@607: #define __STDC_LIMIT_MACROS /* required to have UINTxx_MAX defined when including stdint.h from C++ source code. */ 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! */ msousa@607: #define long_double long double msousa@607: #define real64_t long_double /* so we can later use #if (real64_t == long_double) directives in the code! */ msousa@607: #define REAL64_MAX LDBL_MAX msousa@607: #elif ( DBL_MANT_DIG == 53) /* NOTE: 64 bit IEC559 real has 53 bits for mantissa! */ msousa@607: #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! */ msousa@607: #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! */ msousa@607: #ifndef long_double msousa@607: #define long_double long double msousa@607: #endif msousa@607: #define real32_t long_double /* so we can later use #if (real32_t == long_double) directives in the code! */ msousa@607: #define REAL32_MAX LDBL_MAX msousa@607: #elif ( DBL_MANT_DIG == 24) /* NOTE: 32 bit IEC559 real has 24 bits for mantissa! */ msousa@607: #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! */ msousa@607: #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: */ msousa@645: #define __STDC_FORMAT_MACROS msousa@645: #include msousa@607: msousa@607: agraeper@654: #endif // #ifndef _MAIN_HH