Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1a1e470905c95dc71b46b7db451104e36d909e17 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*******************************************************************************
 * Copyright (c) 2011 protos software gmbh (http://www.protos.de).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * CONTRIBUTORS:
 * 		Henrik Rentz-Reichert (initial contribution)
 *
 *******************************************************************************/

package org.eclipse.etrice.generator.c.gen

import com.google.inject.Inject
import com.google.inject.Singleton
import org.eclipse.etrice.core.genmodel.etricegen.ExpandedActorClass
import org.eclipse.etrice.generator.c.Main
import org.eclipse.etrice.generator.generic.GenericStateMachineGenerator
import org.eclipse.etrice.generator.generic.RoomExtensions

import static extension org.eclipse.etrice.core.genmodel.fsm.FsmGenExtensions.*
import org.eclipse.etrice.core.genmodel.fsm.fsmgen.GraphContainer
import org.eclipse.etrice.core.fsm.fSM.State
import org.eclipse.etrice.generator.c.setup.GeneratorOptionsHelper

@Singleton
class StateMachineGen extends GenericStateMachineGenerator {

	@Inject extension RoomExtensions
	@Inject protected extension GeneratorOptionsHelper

	def genHeaderConstants(ExpandedActorClass xpac) {
		val ac = xpac.actorClass
		
		// this is the C generator and thus we have all states and they are enumerated with non-leaf states first
		// starting with the NUM_PREDEF_STATE_CONSTANTS for TOP and NO_STATE
		val historySize = xpac.graphContainer.graph.allStateNodes.filter[!isLeaf].size + NUM_PREDEF_STATE_CONSTANTS
		'''
			/* constant for state machine data */
			#define ac.name.toUpperCase_HISTORY_SIZE historySize
		'''
	}

	def genDataMembers(ExpandedActorClass xpac) {
		val ac = xpac.actorClass
		'''
			/* state machine variables */
			etInt16 state;
			etInt16 history[ac.name.toUpperCase_HISTORY_SIZE];
		'''
	}

	def genInitialization(ExpandedActorClass xpac) {
		val ac = xpac.actorClass
		'''
			self->state = STATE_TOP;
			{
				int i;
				for (i=0; i<ac.name.toUpperCase_HISTORY_SIZE; ++i)
					self->history[i] = NO_STATE;
			}
			langExt.operationScope(ac.name, false)executeInitTransition(self);
		'''
	}

	/**
	 * @param generateImplementation NOT used
	 */
	override public genExtra(GraphContainer gc, boolean generateImplementation) {
		val mc = gc.component
		val allStates = gc.graph.allStateNodes.map[stateGraphNode].filter(typeof(State)).toList
		val states = allStates.getLeafStatesLast
		'''
			IF Main::settings.generateMSCInstrumentation
				/* state names */
				static char* stateStrings[] = {"<no state>","<top>",FOR state : states SEPARATOR ",""state.genStatePathName"
				ENDFOR};
			ENDIF

			langExt.accessLevelPrivatevoid setState(mc.componentName* self, stateType new_state) {
				self->state = new_state;
				IF Main::settings.generateMSCInstrumentation
					ET_MSC_LOGGER_CHANGE_STATE(self->constData->instName, stateStrings[new_state]);
				ENDIF
			}

			langExt.accessLevelPrivatestateType getState(mc.componentName* self) {
				return self->state;
			}
		'''
	}

	override public stateType() {
		"etInt16"
	}

	override boolType() {
		"etBool"
	}

	override markVariableUsed(String varname) {
		'''
			((void)trigger__et);	/* avoids unused warning */
		'''
	}
	override public unreachableReturn() {
		"/* return NO_STATE; // required by CDT but detected as unreachable by JDT because of while (true) */"
	}

}

Back to the top