Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: da5b3a80debc5d2d09429586101531355024139a (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
/*******************************************************************************
 * 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 v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * 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 org.eclipse.etrice.core.genmodel.fsm.fsmgen.ExpandedModelComponent

@Singleton
class StateMachineGen extends GenericStateMachineGenerator {
	
	@Inject extension RoomExtensions
	
	def genHeaderConstants(ExpandedActorClass xpac) {
		val ac = xpac.actorClass
		/* TODO: can save one entry if NO_STATE=-1 but influences Java */
		val historySize = xpac.stateMachine.baseStateList.size - xpac.stateMachine.leafStateList.size + 2
		'''
			/* 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);
		'''
	}
	
	override public genExtra(ExpandedModelComponent xpmc) {
		val mc = xpmc.modelComponent
		val states = xpmc.stateMachine.baseStateList.getLeafStatesLast
		'''
			«IF Main::settings.generateMSCInstrumentation»
				/* state names */
				static char* stateStrings[] = {"<no state>","<top>",«FOR state : states SEPARATOR ","»"«state.genStatePathName»"
				«ENDFOR»};
			«ENDIF»
			
			«langExt.accessLevelPrivate»void 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.accessLevelPrivate»«stateType» getState(«mc.componentName»* self) {
				return self->state;
			}
		'''
	}
	
	override public stateType() {
		"etInt16"
	}
	
	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