lib/C/accessor.h
author Mario de Sousa <msousa@fe.up.pt>
Sun, 09 Apr 2017 23:43:04 +0100
changeset 1038 036f15e4041d
parent 1006 0668ba32de98
child 1051 e483512edbeb
permissions -rwxr-xr-x
Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
     1
#ifndef __ACCESSOR_H
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
     2
#define __ACCESSOR_H
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
     3
377
60b012b7793f Adding support for compiling direct array specification inside variable declaration
laurent
parents: 252
diff changeset
     4
#define __INITIAL_VALUE(...) __VA_ARGS__
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 219
diff changeset
     5
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
     6
// variable declaration macros
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
     7
#define __DECLARE_VAR(type, name)\
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 219
diff changeset
     8
	__IEC_##type##_t name;
392
9b88b8b6bccd Fixing generated code for global variables. Adding support for defining global variables with complex type
laurent
parents: 377
diff changeset
     9
#define __DECLARE_GLOBAL(type, domain, name)\
9b88b8b6bccd Fixing generated code for global variables. Adding support for defining global variables with complex type
laurent
parents: 377
diff changeset
    10
	__IEC_##type##_t domain##__##name;\
9b88b8b6bccd Fixing generated code for global variables. Adding support for defining global variables with complex type
laurent
parents: 377
diff changeset
    11
	static __IEC_##type##_t *GLOBAL__##name = &(domain##__##name);\
242
0036357a990f Bug with global located variables accessors fixed
laurent
parents: 235
diff changeset
    12
	void __INIT_GLOBAL_##name(type value) {\
0036357a990f Bug with global located variables accessors fixed
laurent
parents: 235
diff changeset
    13
		(*GLOBAL__##name).value = value;\
0036357a990f Bug with global located variables accessors fixed
laurent
parents: 235
diff changeset
    14
	}\
392
9b88b8b6bccd Fixing generated code for global variables. Adding support for defining global variables with complex type
laurent
parents: 377
diff changeset
    15
	IEC_BYTE __IS_GLOBAL_##name##_FORCED(void) {\
9b88b8b6bccd Fixing generated code for global variables. Adding support for defining global variables with complex type
laurent
parents: 377
diff changeset
    16
		return (*GLOBAL__##name).flags & __IEC_FORCE_FLAG;\
242
0036357a990f Bug with global located variables accessors fixed
laurent
parents: 235
diff changeset
    17
	}\
0036357a990f Bug with global located variables accessors fixed
laurent
parents: 235
diff changeset
    18
	type* __GET_GLOBAL_##name(void) {\
0036357a990f Bug with global located variables accessors fixed
laurent
parents: 235
diff changeset
    19
		return &((*GLOBAL__##name).value);\
231
b8527b0abe75 Adding support for forcing global without perturbation from setting external
laurent
parents: 227
diff changeset
    20
	}
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 403
diff changeset
    21
#define __DECLARE_GLOBAL_FB(type, domain, name)\
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 403
diff changeset
    22
	type domain##__##name;\
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 403
diff changeset
    23
	static type *GLOBAL__##name = &(domain##__##name);\
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 403
diff changeset
    24
	type* __GET_GLOBAL_##name(void) {\
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 403
diff changeset
    25
		return &(*GLOBAL__##name);\
708
6cc8255ecfe4 Fix in accessor undefined init methods for function block global instances
Laurent Bessard
parents: 706
diff changeset
    26
	}\
6cc8255ecfe4 Fix in accessor undefined init methods for function block global instances
Laurent Bessard
parents: 706
diff changeset
    27
	extern void type##_init__(type* data__, BOOL retain);
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
    28
#define __DECLARE_GLOBAL_LOCATION(type, location)\
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
    29
	extern type *location;
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
    30
#define __DECLARE_GLOBAL_LOCATED(type, resource, name)\
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 219
diff changeset
    31
	__IEC_##type##_p resource##__##name;\
392
9b88b8b6bccd Fixing generated code for global variables. Adding support for defining global variables with complex type
laurent
parents: 377
diff changeset
    32
	static __IEC_##type##_p *GLOBAL__##name = &(resource##__##name);\
242
0036357a990f Bug with global located variables accessors fixed
laurent
parents: 235
diff changeset
    33
	void __INIT_GLOBAL_##name(type value) {\
0036357a990f Bug with global located variables accessors fixed
laurent
parents: 235
diff changeset
    34
		*((*GLOBAL__##name).value) = value;\
0036357a990f Bug with global located variables accessors fixed
laurent
parents: 235
diff changeset
    35
	}\
392
9b88b8b6bccd Fixing generated code for global variables. Adding support for defining global variables with complex type
laurent
parents: 377
diff changeset
    36
	IEC_BYTE __IS_GLOBAL_##name##_FORCED(void) {\
9b88b8b6bccd Fixing generated code for global variables. Adding support for defining global variables with complex type
laurent
parents: 377
diff changeset
    37
		return (*GLOBAL__##name).flags & __IEC_FORCE_FLAG;\
242
0036357a990f Bug with global located variables accessors fixed
laurent
parents: 235
diff changeset
    38
	}\
0036357a990f Bug with global located variables accessors fixed
laurent
parents: 235
diff changeset
    39
	type* __GET_GLOBAL_##name(void) {\
0036357a990f Bug with global located variables accessors fixed
laurent
parents: 235
diff changeset
    40
		return (*GLOBAL__##name).value;\
0036357a990f Bug with global located variables accessors fixed
laurent
parents: 235
diff changeset
    41
	}
396
155560bfe837 Fixing bug with external variables refering to global variables defined in configurations
laurent
parents: 395
diff changeset
    42
#define __DECLARE_GLOBAL_PROTOTYPE(type, name)\
713
e3ca0f4d47b3 Fix warning message when declaring global variables in configuration
Laurent Bessard
parents: 708
diff changeset
    43
    extern type* __GET_GLOBAL_##name(void);
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
    44
#define __DECLARE_EXTERNAL(type, name)\
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 219
diff changeset
    45
	__IEC_##type##_p name;
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 403
diff changeset
    46
#define __DECLARE_EXTERNAL_FB(type, name)\
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 403
diff changeset
    47
	type* name;
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
    48
#define __DECLARE_LOCATED(type, name)\
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 219
diff changeset
    49
	__IEC_##type##_p name;
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
    50
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
    51
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
    52
// variable initialization macros
225
36d05588f91b Fix accessor code
laurent
parents: 222
diff changeset
    53
#define __INIT_RETAIN(name, retained)\
227
560c1231ad1f Changes merged
laurent
parents: 225 224
diff changeset
    54
    name.flags |= retained?__IEC_RETAIN_FLAG:0;
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 219
diff changeset
    55
#define __INIT_VAR(name, initial, retained)\
225
36d05588f91b Fix accessor code
laurent
parents: 222
diff changeset
    56
	name.value = initial;\
36d05588f91b Fix accessor code
laurent
parents: 222
diff changeset
    57
	__INIT_RETAIN(name, retained)
377
60b012b7793f Adding support for compiling direct array specification inside variable declaration
laurent
parents: 252
diff changeset
    58
#define __INIT_GLOBAL(type, name, initial, retained)\
60b012b7793f Adding support for compiling direct array specification inside variable declaration
laurent
parents: 252
diff changeset
    59
    {\
60b012b7793f Adding support for compiling direct array specification inside variable declaration
laurent
parents: 252
diff changeset
    60
	    static const type temp = initial;\
60b012b7793f Adding support for compiling direct array specification inside variable declaration
laurent
parents: 252
diff changeset
    61
	    __INIT_GLOBAL_##name(temp);\
60b012b7793f Adding support for compiling direct array specification inside variable declaration
laurent
parents: 252
diff changeset
    62
	    __INIT_RETAIN((*GLOBAL__##name), retained)\
60b012b7793f Adding support for compiling direct array specification inside variable declaration
laurent
parents: 252
diff changeset
    63
    }
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 403
diff changeset
    64
#define __INIT_GLOBAL_FB(type, name, retained)\
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 403
diff changeset
    65
	type##_init__(&(*GLOBAL__##name), retained);
392
9b88b8b6bccd Fixing generated code for global variables. Adding support for defining global variables with complex type
laurent
parents: 377
diff changeset
    66
#define __INIT_GLOBAL_LOCATED(domain, name, location, retained)\
9b88b8b6bccd Fixing generated code for global variables. Adding support for defining global variables with complex type
laurent
parents: 377
diff changeset
    67
	domain##__##name.value = location;\
9b88b8b6bccd Fixing generated code for global variables. Adding support for defining global variables with complex type
laurent
parents: 377
diff changeset
    68
	__INIT_RETAIN(domain##__##name, retained)
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 219
diff changeset
    69
#define __INIT_EXTERNAL(type, global, name, retained)\
392
9b88b8b6bccd Fixing generated code for global variables. Adding support for defining global variables with complex type
laurent
parents: 377
diff changeset
    70
    {\
9b88b8b6bccd Fixing generated code for global variables. Adding support for defining global variables with complex type
laurent
parents: 377
diff changeset
    71
		name.value = __GET_GLOBAL_##global();\
9b88b8b6bccd Fixing generated code for global variables. Adding support for defining global variables with complex type
laurent
parents: 377
diff changeset
    72
		__INIT_RETAIN(name, retained)\
9b88b8b6bccd Fixing generated code for global variables. Adding support for defining global variables with complex type
laurent
parents: 377
diff changeset
    73
    }
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 403
diff changeset
    74
#define __INIT_EXTERNAL_FB(type, global, name, retained)\
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 403
diff changeset
    75
	name = __GET_GLOBAL_##global();
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 219
diff changeset
    76
#define __INIT_LOCATED(type, location, name, retained)\
392
9b88b8b6bccd Fixing generated code for global variables. Adding support for defining global variables with complex type
laurent
parents: 377
diff changeset
    77
	{\
9b88b8b6bccd Fixing generated code for global variables. Adding support for defining global variables with complex type
laurent
parents: 377
diff changeset
    78
		extern type *location;\
9b88b8b6bccd Fixing generated code for global variables. Adding support for defining global variables with complex type
laurent
parents: 377
diff changeset
    79
		name.value = location;\
9b88b8b6bccd Fixing generated code for global variables. Adding support for defining global variables with complex type
laurent
parents: 377
diff changeset
    80
		__INIT_RETAIN(name, retained)\
9b88b8b6bccd Fixing generated code for global variables. Adding support for defining global variables with complex type
laurent
parents: 377
diff changeset
    81
    }
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
    82
#define __INIT_LOCATED_VALUE(name, initial)\
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 219
diff changeset
    83
	*(name.value) = initial;
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
    84
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
    85
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
    86
// variable getting macros
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 219
diff changeset
    87
#define __GET_VAR(name, ...)\
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 219
diff changeset
    88
	name.value __VA_ARGS__
225
36d05588f91b Fix accessor code
laurent
parents: 222
diff changeset
    89
#define __GET_EXTERNAL(name, ...)\
394
8a04e93f9ef2 Fixing bug in generated code for complex type external variable
laurent
parents: 392
diff changeset
    90
	((name.flags & __IEC_FORCE_FLAG) ? name.fvalue __VA_ARGS__ : (*(name.value)) __VA_ARGS__)
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 403
diff changeset
    91
#define __GET_EXTERNAL_FB(name, ...)\
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 403
diff changeset
    92
	__GET_VAR(((*name) __VA_ARGS__))
225
36d05588f91b Fix accessor code
laurent
parents: 222
diff changeset
    93
#define __GET_LOCATED(name, ...)\
394
8a04e93f9ef2 Fixing bug in generated code for complex type external variable
laurent
parents: 392
diff changeset
    94
	((name.flags & __IEC_FORCE_FLAG) ? name.fvalue __VA_ARGS__ : (*(name.value)) __VA_ARGS__)
873
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 819
diff changeset
    95
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 231
diff changeset
    96
#define __GET_VAR_BY_REF(name, ...)\
252
0bda13ec66b3 Bug with getter for pointed variables in accessors fixed
laurent
parents: 242
diff changeset
    97
	((name.flags & __IEC_FORCE_FLAG) ? &(name.fvalue __VA_ARGS__) : &(name.value __VA_ARGS__))
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 231
diff changeset
    98
#define __GET_EXTERNAL_BY_REF(name, ...)\
394
8a04e93f9ef2 Fixing bug in generated code for complex type external variable
laurent
parents: 392
diff changeset
    99
	((name.flags & __IEC_FORCE_FLAG) ? &(name.fvalue __VA_ARGS__) : &((*(name.value)) __VA_ARGS__))
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 403
diff changeset
   100
#define __GET_EXTERNAL_FB_BY_REF(name, ...)\
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 403
diff changeset
   101
	__GET_EXTERNAL_BY_REF(((*name) __VA_ARGS__))
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 231
diff changeset
   102
#define __GET_LOCATED_BY_REF(name, ...)\
394
8a04e93f9ef2 Fixing bug in generated code for complex type external variable
laurent
parents: 392
diff changeset
   103
	((name.flags & __IEC_FORCE_FLAG) ? &(name.fvalue __VA_ARGS__) : &((*(name.value)) __VA_ARGS__))
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   104
873
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 819
diff changeset
   105
#define __GET_VAR_REF(name, ...)\
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 819
diff changeset
   106
	(&(name.value __VA_ARGS__))
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 819
diff changeset
   107
#define __GET_EXTERNAL_REF(name, ...)\
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 819
diff changeset
   108
	(&((*(name.value)) __VA_ARGS__))
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 819
diff changeset
   109
#define __GET_EXTERNAL_FB_REF(name, ...)\
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 819
diff changeset
   110
	(&(__GET_VAR(((*name) __VA_ARGS__))))
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 819
diff changeset
   111
#define __GET_LOCATED_REF(name, ...)\
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 819
diff changeset
   112
	(&((*(name.value)) __VA_ARGS__))
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 819
diff changeset
   113
933
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 904
diff changeset
   114
#define __GET_VAR_DREF(name, ...)\
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 904
diff changeset
   115
	(*(name.value __VA_ARGS__))
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 904
diff changeset
   116
#define __GET_EXTERNAL_DREF(name, ...)\
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 904
diff changeset
   117
	(*((*(name.value)) __VA_ARGS__))
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 904
diff changeset
   118
#define __GET_EXTERNAL_FB_DREF(name, ...)\
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 904
diff changeset
   119
	(*(__GET_VAR(((*name) __VA_ARGS__))))
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 904
diff changeset
   120
#define __GET_LOCATED_DREF(name, ...)\
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 904
diff changeset
   121
	(*((*(name.value)) __VA_ARGS__))
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 904
diff changeset
   122
873
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 819
diff changeset
   123
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   124
// variable setting macros
885
b2604fc6d25c Change order of SET_xxx() macros. (this will allow me to simplify the print_setter() methods later on)
mjsousa
parents: 873
diff changeset
   125
#define __SET_VAR(prefix, name, suffix, new_value)\
b2604fc6d25c Change order of SET_xxx() macros. (this will allow me to simplify the print_setter() methods later on)
mjsousa
parents: 873
diff changeset
   126
	if (!(prefix name.flags & __IEC_FORCE_FLAG)) prefix name.value suffix = new_value
b2604fc6d25c Change order of SET_xxx() macros. (this will allow me to simplify the print_setter() methods later on)
mjsousa
parents: 873
diff changeset
   127
#define __SET_EXTERNAL(prefix, name, suffix, new_value)\
1006
0668ba32de98 fix C compilation warning about function prototype in __SET_EXTERNAL
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 933
diff changeset
   128
	{extern IEC_BYTE __IS_GLOBAL_##name##_FORCED(void);\
817
6a67fbc7bea7 Fixed warning in accessor __SET_EXTERNAL
Laurent Bessard
parents: 713
diff changeset
   129
    if (!(prefix name.flags & __IEC_FORCE_FLAG || __IS_GLOBAL_##name##_FORCED()))\
885
b2604fc6d25c Change order of SET_xxx() macros. (this will allow me to simplify the print_setter() methods later on)
mjsousa
parents: 873
diff changeset
   130
		(*(prefix name.value)) suffix = new_value;}
b2604fc6d25c Change order of SET_xxx() macros. (this will allow me to simplify the print_setter() methods later on)
mjsousa
parents: 873
diff changeset
   131
#define __SET_EXTERNAL_FB(prefix, name, suffix, new_value)\
b2604fc6d25c Change order of SET_xxx() macros. (this will allow me to simplify the print_setter() methods later on)
mjsousa
parents: 873
diff changeset
   132
	__SET_VAR((*(prefix name)), suffix, new_value)
b2604fc6d25c Change order of SET_xxx() macros. (this will allow me to simplify the print_setter() methods later on)
mjsousa
parents: 873
diff changeset
   133
#define __SET_LOCATED(prefix, name, suffix, new_value)\
b2604fc6d25c Change order of SET_xxx() macros. (this will allow me to simplify the print_setter() methods later on)
mjsousa
parents: 873
diff changeset
   134
	if (!(prefix name.flags & __IEC_FORCE_FLAG)) *(prefix name.value) suffix = new_value
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   135
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   136
#endif //__ACCESSOR_H