12 #include <stdlib.h> |
12 #include <stdlib.h> |
13 #include <string.h> |
13 #include <string.h> |
14 #include <stdarg.h> |
14 #include <stdarg.h> |
15 |
15 |
16 #include "iec_std_lib_generated.h" |
16 #include "iec_std_lib_generated.h" |
|
17 |
|
18 #define __print_BOOL(name) printf(" %s = (BOOL) %s\n",#name, name?"TRUE":"FALSE"); |
|
19 #define __print_SINT(name) printf(" %s = (SINT) %d\n",#name, name); |
|
20 #define __print_INT(name) printf(" %s = (INT) %d\n",#name, name); |
|
21 #define __print_DINT(name) printf(" %s = (DINT) %d\n",#name, name); |
|
22 #define __print_LINT(name) printf(" %s = (LINT) %d\n",#name, name); |
|
23 #define __print_USINT(name) printf(" %s = (USINT) %u\n",#name, name); |
|
24 #define __print_UINT(name) printf(" %s = (UINT) %u\n",#name, name); |
|
25 #define __print_UDINT(name) printf(" %s = (UDINT) %u\n",#name, name); |
|
26 #define __print_ULINT(name) printf(" %s = (ULINT) %lu\n",#name, name); |
|
27 #define __print_REAL(name) printf(" %s = (REAL) %f\n",#name, (double)name); |
|
28 #define __print_LREAL(name) printf(" %s = (LREAL) %f\n",#name, (double)name); |
|
29 #define __print_TIME(name) {STRING __tmp = __time_to_string(name);__tmp.body[__tmp.len] = 0; printf(" %s = (TIME) %*s\n",#name, __tmp.len, &__tmp.body);} |
|
30 #define __print_DATE(name) {STRING __tmp = __date_to_string(name);__tmp.body[__tmp.len] = 0; printf(" %s = (DATE) %*s\n",#name, __tmp.len, &__tmp.body);} |
|
31 #define __print_TOD(name) {STRING __tmp = __tod_to_string(name);__tmp.body[__tmp.len] = 0; printf(" %s = (TOD) %*s\n",#name, __tmp.len, &__tmp.body);} |
|
32 #define __print_DT(name) {STRING __tmp = __dt_to_string(name);__tmp.body[__tmp.len] = 0; printf(" %s = (DT) %*s\n",#name, __tmp.len, &__tmp.body);} |
|
33 #define __print_STRING(name) printf(" %s = (STRING) {%d, \"%*s\"}\n",#name, name.len, name.len, &name.body); |
|
34 #define __print_BYTE(name) printf(" %s = (BYTE) 0x%2.2x\n",#name, name); |
|
35 #define __print_WORD(name) printf(" %s = (WORD) 0x%4.4x\n",#name, name); |
|
36 #define __print_DWORD(name) printf(" %s = (DWORD) 0x%8.8x\n",#name, name); |
|
37 #define __print_LWORD(name) printf(" %s = (LWORD) 0x%16.16lx\n",#name, name); |
|
38 |
|
39 #ifdef DEBUG_IEC |
|
40 #define DBG(...) printf(__VA_ARGS__); |
|
41 #define DBG_TYPE(TYPENAME, name) __print_##TYPENAME(name); |
|
42 #else |
|
43 #define DBG(...) |
|
44 #define DBG_TYPE(TYPENAME, name) |
|
45 #endif |
17 |
46 |
18 /*****************/ |
47 /*****************/ |
19 /* Types defs */ |
48 /* Types defs */ |
20 /*****************/ |
49 /*****************/ |
21 |
50 |
401 if(res.len > STR_MAX_LEN) res.len = STR_MAX_LEN; |
430 if(res.len > STR_MAX_LEN) res.len = STR_MAX_LEN; |
402 return res; |
431 return res; |
403 } |
432 } |
404 static inline STRING __real_to_string(LREAL IN){ |
433 static inline STRING __real_to_string(LREAL IN){ |
405 STRING res = __INIT_STRING; |
434 STRING res = __INIT_STRING; |
406 res.len = snprintf(res.body, STR_MAX_LEN, "%g", IN); |
435 res.len = snprintf(res.body, STR_MAX_LEN, "%.10g", IN); |
407 if(res.len > STR_MAX_LEN) res.len = STR_MAX_LEN; |
436 if(res.len > STR_MAX_LEN) res.len = STR_MAX_LEN; |
408 return res; |
437 return res; |
409 } |
438 } |
410 static inline STRING __sint_to_string(LINT IN){ |
439 static inline STRING __sint_to_string(LINT IN){ |
411 STRING res = __INIT_STRING; |
440 STRING res = __INIT_STRING; |
451 if( c >= '0' && c <= '7'){ |
480 if( c >= '0' && c <= '7'){ |
452 res |= ( c - '0') << shift; |
481 res |= ( c - '0') << shift; |
453 shift += 3; |
482 shift += 3; |
454 } |
483 } |
455 } |
484 } |
456 }else if(IN->body[0]=='1' && IN->body[1]=='6' && IN->body[1]=='#'){ |
485 }else if(IN->body[0]=='1' && IN->body[1]=='6' && IN->body[2]=='#'){ |
457 /* 16#1234_5678_9abc_DEFG */ |
486 /* 16#1234_5678_9abc_DEFG */ |
458 for(l = IN->len - 1; l >= 3 && shift < 64; l--) |
487 for(l = IN->len - 1; l >= 3 && shift < 64; l--) |
459 { |
488 { |
460 char c = IN->body[l]; |
489 char c = IN->body[l]; |
461 if( c >= '0' && c <= '9'){ |
490 if( c >= '0' && c <= '9'){ |
462 res |= ( c - '0') << shift; |
491 res |= (LWORD)( c - '0') << shift; |
463 shift += 4; |
492 shift += 4; |
464 }else if( c >= 'a' && c <= 'f'){ |
493 }else if( c >= 'a' && c <= 'f'){ |
465 res |= ( c - 'a' + 10 ) << shift; |
494 res |= (LWORD)( c - 'a' + 10 ) << shift; |
466 shift += 4; |
495 shift += 4; |
467 }else if( c >= 'A' && c <= 'F'){ |
496 }else if( c >= 'A' && c <= 'F'){ |
468 res |= ( c - 'A' + 10 ) << shift; |
497 res |= (LWORD)( c - 'A' + 10 ) << shift; |
469 shift += 4; |
498 shift += 4; |
470 } |
499 } |
471 } |
500 } |
472 }else{ |
501 }else{ |
473 /* -123456789 */ |
502 /* -123456789 */ |
838 static inline BOOL fname##TYPENAME( UINT param_count, TYPENAME op1, ...){\ |
867 static inline BOOL fname##TYPENAME( UINT param_count, TYPENAME op1, ...){\ |
839 va_list ap;\ |
868 va_list ap;\ |
840 UINT i;\ |
869 UINT i;\ |
841 \ |
870 \ |
842 va_start (ap, op1); /* Initialize the argument list. */\ |
871 va_start (ap, op1); /* Initialize the argument list. */\ |
|
872 DBG(#fname #TYPENAME "\n")\ |
|
873 DBG_TYPE(TYPENAME, op1)\ |
843 \ |
874 \ |
844 for (i = 0; i < param_count; i++){\ |
875 for (i = 0; i < param_count - 1; i++){\ |
845 TYPENAME tmp = va_arg (ap, VA_ARGS_##TYPENAME);\ |
876 TYPENAME tmp = va_arg (ap, VA_ARGS_##TYPENAME);\ |
|
877 DBG_TYPE(TYPENAME, tmp)\ |
846 if(COND){\ |
878 if(COND){\ |
847 op1 = tmp;\ |
879 op1 = tmp;\ |
848 }else{\ |
880 }else{\ |
849 va_end (ap); /* Clean up. */\ |
881 va_end (ap); /* Clean up. */\ |
850 return 0;\ |
882 return 0;\ |