Ignore:
Timestamp:
Oct 10, 2019 2:41:07 PM (2 months ago)
Author:
Christian Andersson
Message:

Recommitted changeset:13700 to trunk with two bug fixes. Related to ticket:5837

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/Compiler/ModelicaCBackEnd/templates/ceval_external_template.c

    r13703 r13719  
    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"
     17
     18/* Must be defined here in order to override the methods from jmi_global.c */
     19void jmi_global_log(int warning, const char* name, const char* fmt, const char* value) {
     20    _jmi_global_log(warning, name, fmt, value);
     21}
     22void jmi_throw() {
     23    _jmi_throw();
     24}
     25void* jmi_global_calloc(size_t n, size_t s) {
     26    return _jmi_global_calloc(n,s);
     27}
    2328
    2429$ECE_external_includes$
    2530 
    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 
    3331/* Used record definitions */
    3432$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 }
    14833
    14934/* Main */
     
    19580        JMCEVAL_failed();
    19681    }
    197     jmi_dynamic_function_pool_destroy(dyn_fcn_mem);
     82    _jmi_dynamic_function_pool_destroy();
    19883    JMCEVAL_check("END");
    19984    return 0;
Note: See TracChangeset for help on using the changeset viewer.