main.hh
changeset 625 c0bda77b37a0
parent 607 be9ba3531afb
child 645 e01ede1c9862
equal deleted inserted replaced
412:aad38592bdde 625:c0bda77b37a0
       
     1 /*
       
     2  *  matiec - a compiler for the programming languages defined in IEC 61131-3
       
     3  *  Copyright (C) 2003-2011  Mario de Sousa (msousa@fe.up.pt)
       
     4  *  Copyright (C) 2007-2011  Laurent Bessard and Edouard Tisserant
       
     5  *
       
     6  *  This program is free software: you can redistribute it and/or modify
       
     7  *  it under the terms of the GNU General Public License as published by
       
     8  *  the Free Software Foundation, either version 3 of the License, or
       
     9  *  (at your option) any later version.
       
    10  *
       
    11  *  This program is distributed in the hope that it will be useful,
       
    12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    14  *  GNU General Public License for more details.
       
    15  *
       
    16  *  You should have received a copy of the GNU General Public License
       
    17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
       
    18  *
       
    19  *
       
    20  * This code is made available on the understanding that it will not be
       
    21  * used in safety-critical situations without a full and competent review.
       
    22  */
       
    23 
       
    24 /*
       
    25  * An IEC 61131-3 compiler.
       
    26  *
       
    27  * Based on the
       
    28  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
       
    29  *
       
    30  */
       
    31 
       
    32 
       
    33 
       
    34 #ifndef _MAIN_HH
       
    35 #define _MAIN_HH
       
    36 
       
    37 
       
    38 
       
    39  /* Function used throughout the code --> used to report failed assertions (i.e. internal compiler errors)! */
       
    40 #include <stddef.h>  /* required for NULL */
       
    41  
       
    42 #define ERROR               error_exit(__FILE__,__LINE__)
       
    43 #define ERROR_MSG(msg, ...) error_exit(__FILE__,__LINE__, msg)
       
    44 // #define ERROR_MSG(msg, ...) error_exit(__FILE__,__LINE__, msg, __VA_ARGS__)
       
    45 
       
    46 extern void error_exit(const char *file_name, int line_no, const char *errmsg = NULL, ...);
       
    47 
       
    48 
       
    49 
       
    50 
       
    51 
       
    52 
       
    53  /* Get the definition of INT16_MAX, INT16_MIN, UINT64_MAX, INT64_MAX, INT64_MIN, ... */
       
    54 
       
    55 #define __STDC_LIMIT_MACROS /* required to have UINTxx_MAX defined when including stdint.h from C++ source code. */
       
    56 #include <stdint.h>         
       
    57 #include <limits>
       
    58 
       
    59 #ifndef   UINT64_MAX 
       
    60   #define UINT64_MAX (std::numeric_limits< uint64_t >::max())
       
    61 #endif
       
    62 #ifndef    INT64_MAX 
       
    63   #define  INT64_MAX (std::numeric_limits<  int64_t >::max())
       
    64 #endif
       
    65 #ifndef    INT64_MIN
       
    66   #define  INT64_MIN (std::numeric_limits<  int64_t >::min()) 
       
    67 #endif
       
    68 
       
    69 
       
    70 
       
    71 /* Determine, for the current platform, which datas types (float, double or long double) use 64 and 32 bits. */
       
    72 /* NOTE: We cant use sizeof() in pre-processor directives, so we have to do it another way... */
       
    73 /* CURIOSITY: We can use sizeof() and offsetof() inside static_assert() but:
       
    74  *          - this only allows us to make assertions, and not #define new macros
       
    75  *          - 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.
       
    76  *          https://www.securecoding.cert.org/confluence/display/seccode/DCL03-C.+Use+a+static+assertion+to+test+the+value+of+a+constant+expression
       
    77  *         struct {int a, b, c, d} header_t;
       
    78  *  e.g.:  static_assert(offsetof(struct header_t, c) == 8, "Compile time error message.");
       
    79  */
       
    80 
       
    81 #include <float.h>
       
    82 #if    (LDBL_MANT_DIG == 53) /* NOTE: 64 bit IEC559 real has 53 bits for mantissa! */
       
    83   #define long_double long double
       
    84   #define real64_t long_double /* so we can later use #if (real64_t == long_double) directives in the code! */
       
    85   #define REAL64_MAX  LDBL_MAX
       
    86 #elif  ( DBL_MANT_DIG == 53) /* NOTE: 64 bit IEC559 real has 53 bits for mantissa! */
       
    87   #define real64_t double
       
    88   #define REAL64_MAX  DBL_MAX
       
    89 #elif  ( FLT_MANT_DIG == 53) /* NOTE: 64 bit IEC559 real has 53 bits for mantissa! */
       
    90   #define real64_t float
       
    91   #define REAL64_MAX  FLT_MAX
       
    92 #else 
       
    93   #error Could not find a 64 bit floating point data type on this platform. Aborting...
       
    94 #endif
       
    95 
       
    96 
       
    97 #if    (LDBL_MANT_DIG == 24) /* NOTE: 32 bit IEC559 real has 24 bits for mantissa! */
       
    98   #ifndef long_double 
       
    99     #define long_double long double
       
   100   #endif  
       
   101   #define real32_t long_double /* so we can later use #if (real32_t == long_double) directives in the code! */
       
   102   #define REAL32_MAX  LDBL_MAX
       
   103 #elif  ( DBL_MANT_DIG == 24) /* NOTE: 32 bit IEC559 real has 24 bits for mantissa! */
       
   104   #define real32_t double
       
   105   #define REAL32_MAX  DBL_MAX
       
   106 #elif  ( FLT_MANT_DIG == 24) /* NOTE: 32 bit IEC559 real has 24 bits for mantissa! */
       
   107   #define real32_t float
       
   108   #define REAL32_MAX  FLT_MAX
       
   109 #else 
       
   110   #error Could not find a 32 bit floating point data type on this platform. Aborting...
       
   111 #endif
       
   112 
       
   113 
       
   114 
       
   115 #include <math.h>
       
   116 #ifndef INFINITY
       
   117   #error Could not find the macro that defines the value for INFINITY in the current platform.
       
   118 #endif
       
   119 #ifndef NAN
       
   120   #error Could not find the macro that defines the value for NAN in the current platform.
       
   121 #endif
       
   122 
       
   123 
       
   124 
       
   125 
       
   126 
       
   127 #endif // #ifndef _MAIN_HH