Changeset 13534


Ignore:
Timestamp:
Sep 18, 2019 8:50:22 PM (3 months ago)
Author:
Christian Andersson
Message:

Refactored runtime. Related to ticket:5837

Location:
branches/dev-cw-evaluator
Files:
2 added
6 edited

Legend:

Unmodified
Added
Removed
  • branches/dev-cw-evaluator/Compiler/ModelicaCBackEnd/templates/ceval_external_template.c

    r11746 r13534  
    1414*/
    1515
    16 #include <stdio.h>
    17 #include <stdlib.h>
    18 #include <string.h>
    19 #include "jmi.h"
    20 #include "jmi_dyn_mem.h"
    21 #include "ModelicaUtilities.h"
    22 #include <fcntl.h>
     16#include "jmi_evaluator_util.h"
    2317
    2418$ECE_external_includes$
    2519 
    26 /* Manual debugging */
    27 #define JMCEVAL_DEBUG 0
    28 #define JMCEVAL_DBGP(x) if (JMCEVAL_DEBUG) { printf(x); fflush(stdout);}
    29 
    30 /* Format specifier when printing jmi_real_t */
    31 #define JMCEVAL_realFormat "%.16f"
    32 
    3320/* Used record definitions */
    3421$ECE_record_definitions$
    35 
    36 /* Parses ND dimensions into dimension buffer d*/
    37 #define JMCEVAL_parseArrayDims(ND) \
    38     for (di = 0; di < ND; di++) { scanf("%d",&d[di]); }
    39 
    40 /* Parse/print basic types */
    41 double JMCEVAL_parseReal() {
    42     /* Char buffer when reading jmi_real_t. This is necessary
    43        since "%lf" is not allowed in c89. */
    44     char buff[32];
    45     JMCEVAL_DBGP("Parse number: ");
    46     scanf("%s",buff);
    47     return strtod(buff, 0);
    48 }
    49 
    50 void JMCEVAL_printReal(double x) {
    51     printf(JMCEVAL_realFormat, x); \
    52     printf("\n"); \
    53     fflush(stdout); \
    54 }
    55 
    56 char* JMCEVAL_parseString() {
    57     int d[1];
    58     char* str;
    59     size_t si,di;
    60     JMCEVAL_parseArrayDims(1);
    61     getchar();
    62     str = ModelicaAllocateString(d[0]);
    63     JMCEVAL_DBGP("Parse string: ");
    64     for (si = 0; si < d[0]; si++) str[si] = getchar();
    65     str[d[0]] = '\0';
    66     return str;
    67 }
    68 
    69 void JMCEVAL_printString(const char* str) {
    70     printf("%u\n%s\n", (unsigned)strlen(str), str);
    71     fflush(stdout);
    72 }
    73 
    74 #define JMCEVAL_parseInteger()  JMCEVAL_parseReal()
    75 #define JMCEVAL_parseBoolean()  JMCEVAL_parseInteger()
    76 #define JMCEVAL_parseEnum()     JMCEVAL_parseInteger()
    77 #define JMCEVAL_printInteger(X) JMCEVAL_printReal(X)
    78 #define JMCEVAL_printBoolean(X) JMCEVAL_printInteger(X)
    79 #define JMCEVAL_printEnum(X)    JMCEVAL_printInteger(X)
    80 #define JMCEVAL_parse(TYPE, X)  X = JMCEVAL_parse##TYPE()
    81 #define JMCEVAL_print(TYPE, X)  JMCEVAL_print##TYPE(X)
    82 
    83 /* Parse/print arrays */
    84 #define JMCEVAL_parseArray(TYPE,ARR) for (vi = 1; vi <= ARR->num_elems; vi++) { JMCEVAL_parse(TYPE, jmi_array_ref_1(ARR,vi)); }
    85 #define JMCEVAL_printArray(TYPE,ARR) for (vi = 1; vi <= ARR->num_elems; vi++) { JMCEVAL_print(TYPE, jmi_array_val_1(ARR,vi)); }
    86 
    87 /* Used by ModelicaUtilities */
    88 void jmi_global_log(int warning, const char* name, const char* fmt, const char* value)
    89 {
    90     printf("LOG\n");
    91     JMCEVAL_printInteger((double)warning);
    92     JMCEVAL_printString(name);
    93     JMCEVAL_printString(fmt);
    94     JMCEVAL_printString(value);
    95 }
    96 
    97 jmp_buf jmceval_try_location;
    98 
    99 #define JMCEVAL_try() (setjmp(jmceval_try_location) == 0)
    100 
    101 void jmi_throw()
    102 {
    103     longjmp(jmceval_try_location, 1);
    104 }
    105 
    106 jmi_dynamic_function_memory_t* dyn_fcn_mem = NULL;
    107 
    108 jmi_dynamic_function_memory_t* jmi_dynamic_function_memory() {
    109     if (dyn_fcn_mem == NULL) { dyn_fcn_mem = jmi_dynamic_function_pool_create(1024*1024); }
    110     return dyn_fcn_mem;
    111 }
    112 
    113 void* jmi_global_calloc(size_t n, size_t s)
    114 {
    115     return jmi_dynamic_function_pool_direct_alloc(dyn_fcn_mem, n*s, 1);
    116 }
    117 
    118 void JMCEVAL_setup() {
    119 #ifdef _WIN32
    120     /* Prevent win from translating \n to \r\n */
    121     _setmode(fileno(stdout), _O_BINARY);
    122 #endif
    123 }
    124 
    125 int JMCEVAL_cont(const char* word) {
    126     char l[10];
    127     char* s = fgets(l, 10, stdin);
    128     if (strlen(s) == 1) {
    129         s = fgets(l, 10, stdin); /* Extra call to fix stray newline */
    130     }
    131     if (s == NULL) {
    132         exit(2);
    133     }
    134     if (strlen(s) == strlen(word)) {
    135         return strncmp(l, word, strlen(word)) == 0;
    136     }
    137     return 0;
    138 }
    139 
    140 void JMCEVAL_check(const char* str) {
    141     printf("%s\n",str);
    142     fflush(stdout);
    143 }
    144 
    145 void JMCEVAL_failed() {
    146     JMCEVAL_check("ABORT");
    147 }
    14822
    14923/* Main */
     
    19569        JMCEVAL_failed();
    19670    }
    197     jmi_dynamic_function_pool_destroy(dyn_fcn_mem);
    19871    JMCEVAL_check("END");
    19972    return 0;
  • branches/dev-cw-evaluator/RuntimeLibrary/Makefiles/Makefile.linux

    r10938 r13534  
    126126LIBS_FMUCS10 = -lfmi1_cs -lfmi1_me $(LIBS_FMU_STD) $(LIB_COMMON) -l:libsundials_cvode.a
    127127LIBS_FMU20   = -lfmi2 $(LIBS_FMU_STD) $(LIB_COMMON) -l:libsundials_cvode.a
    128 LIBS_CEVAL   = $(LIBS_FMU_STD)
     128LIBS_CEVAL   = -ljmi_evaluator_util $(LIBS_FMU_STD)
    129129
    130130# Include paths for compilation
  • branches/dev-cw-evaluator/RuntimeLibrary/Makefiles/Makefile.macosx

    r10938 r13534  
    125125LIBS_FMUCS10 = -lfmi1_cs -lfmi1_me $(LIBS_FMU_STD) $(LIB_COMMON) $(SUNDIALS_HOME)/lib/libsundials_cvode.a
    126126LIBS_FMU20   = -lfmi2 $(LIBS_FMU_STD) $(LIB_COMMON) $(SUNDIALS_HOME)/lib/libsundials_cvode.a
    127 LIBS_CEVAL   = $(LIBS_FMU_STD)
     127LIBS_CEVAL   = -ljmi_evaluator_util $(LIBS_FMU_STD)
    128128
    129129# Include paths for compilation
  • branches/dev-cw-evaluator/RuntimeLibrary/Makefiles/Makefile.windows

    r11565 r13534  
    160160LIBS_FMUCS10 = -lfmi1_cs -lfmi1_me $(LIBS_FMU_ALL)
    161161LIBS_FMU20   = -lfmi2              $(LIBS_FMU_ALL)
    162 LIBS_CEVAL = $(LIBS_FMU_STD)
     162LIBS_CEVAL = -ljmi_evaluator_util $(LIBS_FMU_STD)
    163163
    164164# Include paths for compilation
  • branches/dev-cw-evaluator/RuntimeLibrary/src/evaluator/CMakeLists.txt

    r13440 r13534  
    2626endif()
    2727
    28 
    2928set(EvaluatorSources
    30 
    3129    jmi_evaluator.c
    3230)
     31
     32set(EvaluatorSourcesUtil
     33    jmi_evaluator_util.c
     34    jmi_evaluator_util.h
     35)
     36
     37#Build jmi_evaluator_util library
     38add_library(jmi_evaluator_util STATIC ${EvaluatorSourcesUtil})
     39if(NOT MSVC)
     40    set_target_properties(jmi_evaluator_util PROPERTIES COMPILE_FLAGS "-Wall -g -std=c89 -pedantic -O2")
     41endif()
     42
    3343#Build evaluator executable
    34 set (EVALUATOR_LIBS ${EVALUATOR_LIBS} jmi ModelicaExternalC)
     44set (EVALUATOR_LIBS ${EVALUATOR_LIBS} jmi_evaluator_util jmi ModelicaExternalC)
    3545
    3646include_directories(${STANDARD_HEADER_SRC})
     
    4656#Install the libraries
    4757install(TARGETS jmi_evaluator DESTINATION "${RTLIB_BIN_DIR}")
     58install(TARGETS jmi_evaluator_util DESTINATION "${RTLIB_LIB_DIR}")
     59
     60#Install header files
     61install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/"
     62        DESTINATION "${RTLIB_INCLUDE_DIR}"
     63        FILES_MATCHING PATTERN "*.h")
  • branches/dev-cw-evaluator/RuntimeLibrary/src/evaluator/jmi_evaluator.c

    r13465 r13534  
    1 #include <stdio.h>
    2 #include <stdlib.h>
    3 #include <string.h>
    4 #include "jmi.h"
    5 #include "jmi_dyn_mem.h"
    6 #include "ModelicaUtilities.h"
    7 #include <fcntl.h>
    8 
    9 #ifdef _WIN32
    10     #include <windows.h>
    11 #else
    12     #define _GNU_SOURCE
    13     #define __stdcall
    14     #include <dlfcn.h>
    15     #ifdef __APPLE__
    16         #include <limits.h>
    17     #else
    18         #include <linux/limits.h>
    19         #include <limits.h>
    20     #endif
    21 #endif
     1#include "jmi_evaluator_util.h"
    222
    233/* Builtins */
     
    255extern const char* ModelicaStrings_length(const char*);
    266extern const char* ModelicaStrings_skipWhiteSpace(const char*, int);
    27 
    28 #define ERROR_LOAD_DLL 3
    29 #define ERROR_LOAD_FUNCTION 4
    30 #define ERROR_OUTPUT_ARGS_NOT_SPECIFIED 5
    31 #define ERROR_INPUT_ARGS_NOT_SPECIFIED 6
    32 #define ERROR_NOT_SUPPORTED_INPUT_ARGS 7
    33 #define ERROR_NOT_SUPPORTED_OUTPUT_ARGS 8
    34 
    35 #define ERROR_LOAD_DLL_MSG "Error loading the DLL\n"
    36 #define ERROR_LOAD_FUNCTION_MSG "Error loading the function\n"
    37 #define ERROR_OUTPUT_ARGS_NOT_SPECIFIED_MSG "Error, the output arguments are not specified\n"
    38 #define ERROR_INPUT_ARGS_NOT_SPECIFIED_MSG "Error, the input arguments are not specified\n"
    39 #define ERROR_NOT_SUPPORTED_INPUT_ARGS_MSG "Error, the listed input arguments are not supported\n"
    40 #define ERROR_NOT_SUPPORTED_OUTPUT_ARGS_MSG "Error, the listed output arguments are not supported\n"
    41 
    42 
    43 /* Manual debugging */
    44 #define JMCEVAL_DEBUG 0
    45 #define JMCEVAL_DBGP(x) if (JMCEVAL_DEBUG) { printf(x); fflush(stdout);}
    46 
    47 /* Format specifier when printing jmi_real_t */
    48 #define JMCEVAL_realFormat "%.16f"
    497
    508/* Record definitions */
     
    6422};
    6523JMI_ARRAY_TYPE(R_ddddddddddd, R_ddddddddddd_a)
    66 
    67 
    68 /* Parses ND dimensions into dimension buffer d*/
    69 #define JMCEVAL_parseArrayDims(ND) \
    70     for (di = 0; di < ND; di++) { scanf("%d",&d[di]); }
    71 
    72 /* Parse/print basic types */
    73 double JMCEVAL_parseReal() {
    74     /* Char buffer when reading jmi_real_t. This is necessary
    75        since "%lf" is not allowed in c89. */
    76     char buff[32];
    77     JMCEVAL_DBGP("Parse number: ");
    78     scanf("%s",buff);
    79     return strtod(buff, 0);
    80 }
    81 
    82 void JMCEVAL_printReal(double x) {
    83     printf(JMCEVAL_realFormat, x); \
    84     printf("\n"); \
    85     fflush(stdout); \
    86 }
    87 
    88 char* JMCEVAL_parseString() {
    89     int d[1];
    90     char* str;
    91     size_t si,di;
    92     JMCEVAL_parseArrayDims(1);
    93     getchar();
    94     str = ModelicaAllocateString(d[0]);
    95     JMCEVAL_DBGP("Parse string: ");
    96     for (si = 0; si < d[0]; si++) str[si] = getchar();
    97     str[d[0]] = '\0';
    98     return str;
    99 }
    100 
    101 void JMCEVAL_printString(const char* str) {
    102     printf("%u\n%s\n", (unsigned)strlen(str), str);
    103     fflush(stdout);
    104 }
    105 
    106 #define JMCEVAL_parseInteger()  JMCEVAL_parseReal()
    107 #define JMCEVAL_parseBoolean()  JMCEVAL_parseInteger()
    108 #define JMCEVAL_parseEnum()     JMCEVAL_parseInteger()
    109 #define JMCEVAL_printInteger(X) JMCEVAL_printReal(X)
    110 #define JMCEVAL_printBoolean(X) JMCEVAL_printInteger(X)
    111 #define JMCEVAL_printEnum(X)    JMCEVAL_printInteger(X)
    112 #define JMCEVAL_parse(TYPE, X)  X = JMCEVAL_parse##TYPE()
    113 #define JMCEVAL_print(TYPE, X)  JMCEVAL_print##TYPE(X)
    114 
    115 /* Parse/print arrays */
    116 #define JMCEVAL_parseArray(TYPE,ARR) for (vi = 1; vi <= ARR->num_elems; vi++) { JMCEVAL_parse(TYPE, jmi_array_ref_1(ARR,vi)); }
    117 #define JMCEVAL_printArray(TYPE,ARR) for (vi = 1; vi <= ARR->num_elems; vi++) { JMCEVAL_print(TYPE, jmi_array_val_1(ARR,vi)); }
    118 
    119 /* Used by ModelicaUtilities */
    120 void jmi_global_log(int warning, const char* name, const char* fmt, const char* value)
    121 {
    122     printf("LOG\n");
    123     JMCEVAL_printInteger((double)warning);
    124     JMCEVAL_printString(name);
    125     JMCEVAL_printString(fmt);
    126     JMCEVAL_printString(value);
    127 }
    128 
    129 jmp_buf jmceval_try_location;
    130 
    131 #define JMCEVAL_try() (setjmp(jmceval_try_location) == 0)
    132 
    133 void jmi_throw()
    134 {
    135     longjmp(jmceval_try_location, 1);
    136 }
    137 
    138 jmi_dynamic_function_memory_t* dyn_fcn_mem = NULL;
    139 
    140 jmi_dynamic_function_memory_t* jmi_dynamic_function_memory() {
    141     if (dyn_fcn_mem == NULL) { dyn_fcn_mem = jmi_dynamic_function_pool_create(1024*1024); }
    142     return dyn_fcn_mem;
    143 }
    144 
    145 void* jmi_global_calloc(size_t n, size_t s)
    146 {
    147     return jmi_dynamic_function_pool_direct_alloc(dyn_fcn_mem, n*s, 1);
    148 }
    149 
    150 void JMCEVAL_setup() {
    151 #ifdef _WIN32
    152     /* Prevent win from translating \n to \r\n */
    153     _setmode(fileno(stdout), _O_BINARY);
    154 #endif
    155 }
    156 
    157 int JMCEVAL_cont(const char* word) {
    158     char l[10];
    159     char* s = fgets(l, 10, stdin);
    160     if (strlen(s) == 1) {
    161         s = fgets(l, 10, stdin); /* Extra call to fix stray newline */
    162     }
    163     if (s == NULL) {
    164         exit(2);
    165     }
    166     if (strlen(s) == strlen(word)) {
    167         return strncmp(l, word, strlen(word)) == 0;
    168     }
    169     return 0;
    170 }
    171 
    172 void JMCEVAL_check(const char* str) {
    173     printf("%s\n",str);
    174     fflush(stdout);
    175 }
    176 
    177 void JMCEVAL_failed() {
    178     JMCEVAL_check("ABORT");
    179 }
    18024
    18125typedef const char* (__stdcall *f_s_sii)(const char*, int, int);
     
    539383        JMCEVAL_failed();
    540384    }
    541     jmi_dynamic_function_pool_destroy(dyn_fcn_mem);
    542385    JMCEVAL_check("END");
    543386    return 0;
Note: See TracChangeset for help on using the changeset viewer.