summaryrefslogtreecommitdiffstats
path: root/awlsim/blocks.py
blob: 8edb4eb855c47cb468eb37df3993486463aad15b (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# -*- coding: utf-8 -*-
#
# AWL simulator - blocks
#
# Copyright 2012-2013 Michael Buesch <m@bues.ch>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

from awlsim.labels import *
from awlsim.datastructure import *
from awlsim.datatypes import *
from awlsim.operators import *
from awlsim.util import *


class BlockInterface(object):
	class Field(object):
		enum.start = -1
		FTYPE_UNKNOWN	= enum.item
		FTYPE_IN	= enum.item
		FTYPE_OUT	= enum.item
		FTYPE_INOUT	= enum.item
		FTYPE_STAT	= enum.item
		FTYPE_TEMP	= enum.item
		enum.end

		def __init__(self, name, dataType, initialValue=None):
			self.name = name
			self.dataType = dataType
			self.fieldType = self.FTYPE_UNKNOWN
			self.initialValue = initialValue

	# Data-types that must be passed "by-reference" to FCs/FBs.
	callByRef_Types = (
		AwlDataType.TYPE_TIMER,
		AwlDataType.TYPE_COUNTER,
		AwlDataType.TYPE_BLOCK_DB,
		AwlDataType.TYPE_BLOCK_FB,
		AwlDataType.TYPE_BLOCK_FC,
	)

	# Set to true for FBs and SFBs
	requiresInstanceDB = False

	def __init__(self):
		self.struct = None # Instance-DB structure (IN, OUT, INOUT, STAT)
		self.tempStruct = None # Local-stack structure (TEMP)

		self.fieldNameMap = {}
		self.fields_IN = []
		self.fields_OUT = []
		self.fields_INOUT = []
		self.fields_STAT = []
		self.fields_TEMP = []

		self.interfaceFieldCount = 0
		self.staticFieldCount = 0
		self.tempFieldCount = 0

	def __addField(self, field):
		if field.name in self.fieldNameMap:
			raise AwlSimError("Data structure field name '%s' is ambiguous." %\
				field.name)
		if field.fieldType == BlockInterface.Field.FTYPE_TEMP:
			self.tempFieldCount += 1
		elif field.fieldType == BlockInterface.Field.FTYPE_STAT:
			self.staticFieldCount += 1
		else:
			self.interfaceFieldCount += 1
		self.fieldNameMap[field.name] = field

	def addField_IN(self, field):
		field.fieldType = field.FTYPE_IN
		self.__addField(field)
		self.fields_IN.append(field)

	def addField_OUT(self, field):
		field.fieldType = field.FTYPE_OUT
		self.__addField(field)
		self.fields_OUT.append(field)

	def addField_INOUT(self, field):
		field.fieldType = field.FTYPE_INOUT
		self.__addField(field)
		self.fields_INOUT.append(field)

	def addField_STAT(self, field):
		field.fieldType = field.FTYPE_STAT
		self.__addField(field)
		self.fields_STAT.append(field)

	def addField_TEMP(self, field):
		field.fieldType = field.FTYPE_TEMP
		self.__addField(field)
		self.fields_TEMP.append(field)

	def __buildField(self, struct, field, isFirst):
		if isFirst:
			struct.addFieldAligned(field.name,
					       field.dataType, 2)
		else:
			struct.addFieldNaturallyAligned(field.name,
							field.dataType)

	def buildDataStructure(self):
		# Build interface-DB structure
		self.struct = AwlStruct()
		for i, field in enumerate(self.fields_IN):
			self.__buildField(self.struct, field, i==0)
		for i, field in enumerate(self.fields_OUT):
			self.__buildField(self.struct, field, i==0)
		for i, field in enumerate(self.fields_INOUT):
			self.__buildField(self.struct, field, i==0)
		for i, field in enumerate(self.fields_STAT):
			self.__buildField(self.struct, field, i==0)

		# Build local-stack structure
		self.tempStruct = AwlStruct()
		for i, field in enumerate(self.fields_TEMP):
			self.__buildField(self.tempStruct, field, i==0)

	def getFieldByName(self, name):
		try:
			return self.fieldNameMap[name]
		except KeyError:
			raise AwlSimError("Data structure field '%s' does not exist." %\
				name)

	def getOperatorForFieldName(self, name, wantPointer):
		return self.getOperatorForField(self.getFieldByName(name),
						wantPointer)

	# Get an AwlOperator that addresses the specified interfaceField.
	# If wantPointer is true, an IMM_PTR AwlOperator to the interfaceField
	# is returned.
	def getOperatorForField(self, interfaceField, wantPointer):
		if interfaceField.fieldType == interfaceField.FTYPE_TEMP:
			structField = self.tempStruct.getField(interfaceField.name)

			if wantPointer:
				ptrValue = structField.offset.toPointerValue()
				return AwlOperator(type = AwlOperator.IMM_PTR,
						   width = 32,
						   value = (AwlIndirectOp.AREA_L |\
							    ptrValue))

			# Translate to local-stack access
			operType = AwlOperator.MEM_L
		else: # IN/OUT/INOUT/STAT
			structField = self.struct.getField(interfaceField.name)

			if wantPointer:
				ptrValue = structField.offset.toPointerValue()
				return AwlOperator(type = AwlOperator.IMM_PTR,
						   width = 32,
						   value = (AwlIndirectOp.AREA_DI |\
							    ptrValue))

			# Translate to interface-DB access
			if structField.dataType.type in BlockInterface.callByRef_Types:
				# "call by reference"
				offsetOper = AwlOperator(type=AwlOperator.INTERF_DB,
							 width=structField.dataType.width,
							 value=structField.offset.dup())
				if structField.dataType.type == AwlDataType.TYPE_TIMER:
					area = AwlIndirectOp.EXT_AREA_T
					width = 16
				elif structField.dataType.type == AwlDataType.TYPE_COUNTER:
					area = AwlIndirectOp.EXT_AREA_Z
					width = 16
				elif structField.dataType.type == AwlDataType.TYPE_BLOCK_DB:
					area = AwlIndirectOp.EXT_AREA_BLKREF_DB
					width = 16
				elif structField.dataType.type == AwlDataType.TYPE_BLOCK_FB:
					area = AwlIndirectOp.EXT_AREA_BLKREF_FB
					width = 16
				elif structField.dataType.type == AwlDataType.TYPE_BLOCK_FC:
					area = AwlIndirectOp.EXT_AREA_BLKREF_FC
					width = 16
				else:
					assert(0)
				return AwlIndirectOp(
					area=area,
					width=width,
					addressRegister=AwlIndirectOp.AR_NONE,
					offsetOper=offsetOper)
			else:
				# "call by value"
				operType = AwlOperator.INTERF_DB
		return AwlOperator(type=operType,
				   width=structField.bitSize,
				   value=structField.offset.dup())

	def __repr__(self):
		ret = []
		for flist, fname in ((self.fields_IN, "VAR_IN"),
				     (self.fields_OUT, "VAR_OUT"),
				     (self.fields_INOUT, "VAR_IN_OUT")):
			if not flist:
				continue
			ret.append(fname)
			for field in flist:
				ret.append("  %s : %s;" %\
					   (field.name, str(field.dataType)))
			ret.append("END_VAR")
		if not ret:
			ret = [ "<None>" ]
		return '\n'.join(ret)

class Block(object):
	def __init__(self, insns, index, interface):
		self.insns = insns
		self.labels = None
		self.index = index
		if insns:
			self.labels = AwlLabel.resolveLabels(insns)
		self.interface = interface

	def __repr__(self):
		return "Block %d" % self.index

class OBInterface(BlockInterface):
	def addField_IN(self, field):
		raise AwlSimError("VAR_INPUT not possible in an OB")

	def addField_OUT(self, field):
		raise AwlSimError("VAR_OUTPUT not possible in an OB")

	def addField_INOUT(self, field):
		raise AwlSimError("VAR_IN_OUT not possible in an OB")

	def addField_STAT(self, field):
		raise AwlSimError("Static VAR not possible in an OB")

class OB(Block):
	def __init__(self, insns, index):
		Block.__init__(self, insns, index, OBInterface())

	def __repr__(self):
		return "OB %d" % self.index

class FBInterface(BlockInterface):
	requiresInstanceDB = True

class FB(Block):
	def __init__(self, insns, index):
		Block.__init__(self, insns, index, FBInterface())

	def __repr__(self):
		return "FB %d" % self.index

class FCInterface(BlockInterface):
	def addField_STAT(self, field):
		raise AwlSimError("Static VAR not possible in an FC")

class FC(Block):
	def __init__(self, insns, index):
		Block.__init__(self, insns, index, FCInterface())

	def __repr__(self):
		return "FC %d" % self.index
bues.ch cgit interface