aboutsummaryrefslogtreecommitdiffstats
path: root/awlsim/core/blockinterface.py
blob: b98c4d110ffeff05d98af3ec41202c7fea1c56ef (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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
# -*- coding: utf-8 -*-
#
# AWL simulator - block interface
#
# Copyright 2012-2017 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 __future__ import division, absolute_import, print_function, unicode_literals
#from awlsim.common.cython_support cimport * #@cy
from awlsim.common.compat import *

from awlsim.common.util import *
from awlsim.common.enumeration import *
from awlsim.common.exceptions import *

from awlsim.core.memory import * #+cimport
from awlsim.core.identifier import *
from awlsim.core.datastructure import * #+cimport
from awlsim.core.operatortypes import * #+cimport
from awlsim.core.operators import * #+cimport


__all__ = [
	"BlockInterfaceField",
	"VerboseBlockIntfField",
	"BlockInterface",
	"OBInterface",
	"FBInterface",
	"FCInterface",
]


class BlockInterfaceField(object):
	"""Block interface field descriptor."""

	EnumGen.start = -1
	FTYPE_UNKNOWN	= EnumGen.item
	FTYPE_IN	= EnumGen.item
	FTYPE_OUT	= EnumGen.item
	FTYPE_INOUT	= EnumGen.item
	FTYPE_STAT	= EnumGen.item
	FTYPE_TEMP	= EnumGen.item
	EnumGen.end

	__slots__ = (
		"name",
		"__dataType",
		"fieldType",
		"fieldIndex",
	)

	def __init__(self, name, dataType):
		# name -> The name string of the field, as defined
		#         in the block interface definition.
		# dataType -> AwlDataType instance or a type name string.
		self.name = name
		self.__dataType = dataType
		self.fieldType = self.FTYPE_UNKNOWN	# set later
		self.fieldIndex = None			# set later

	@property
	def dataType(self):
		from awlsim.core.datatypes import AwlDataType

		dataType = self.__dataType
		if not isinstance(dataType, AwlDataType):
			dataType = AwlDataType.makeByName(dataType)
			self.__dataType = dataType
		return dataType

	def varDeclString(self):
		return "%s : %s;" %\
			(self.name, str(self.dataType))

	def __repr__(self):
		ftype = {
			self.FTYPE_UNKNOWN	: "UNKNOWN",
			self.FTYPE_IN		: "IN",
			self.FTYPE_OUT		: "OUT",
			self.FTYPE_INOUT	: "IN_OUT",
			self.FTYPE_STAT		: "STAT",
			self.FTYPE_TEMP		: "TEMP",
		}[self.fieldType]
		return "(%s)  %s" %\
			(ftype, BlockInterfaceField.varDeclString(self))

class VerboseBlockIntfField(BlockInterfaceField):
	"""Block interface field descriptor,
	with verbose description string."""

	__slots__ = (
		"desc",
	)

	def __init__(self, name, dataType, desc=""):
		BlockInterfaceField.__init__(self, name, dataType)
		self.desc = desc

	def varDeclString(self):
		ret = [ BlockInterfaceField.varDeclString(self), ]
		if self.desc:
			ret.append(" // ")
			ret.append(self.desc)
		return "".join(ret)

class BlockInterface(object):
	"""Code block interface (IN/OUT/IN_OUT/STAT/TEMP parameters) base class."""

	# Structs (self._struct and self.tempStruct) build status.
	EnumGen.start
	STRUCT_NOT_BUILT	= EnumGen.item # Structs not built, yet.
	STRUCT_BUILDING		= EnumGen.item # Currently building structs.
	STRUCT_BUILT		= EnumGen.item # Structs are completely built.
	EnumGen.end

	# Specifies whether this interface has a DI assigned.
	# Set to true for FBs and SFBs
	hasInstanceDB = False

	# The number of allocated bytes on startup.
	# This is only non-zero for OBs
	startupTempAllocation = 0

	class StructRecursion(Exception): pass

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

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

		self.interfaceFieldCount = 0	# The number of interface fields
		self.staticFieldCount = 0	# The number of static fields
		self.tempFieldCount = 0		# The number of temp fields
		self.tempAllocation = 0		# The number of allocated TEMP bytes (interface only!)

	@property
	def fields_IN_OUT_INOUT(self):
		ret = self.fields_IN[:]
		ret.extend(self.fields_OUT)
		ret.extend(self.fields_INOUT)
		return ret

	@property
	def fields_IN_OUT_INOUT_STAT(self):
		ret = self.fields_IN_OUT_INOUT # don't copy
		ret.extend(self.fields_STAT)
		return ret

	def __addField(self, field):
		if field.name in self.fieldNameMap:
			raise AwlSimError("Data structure field name '%s' is ambiguous." %\
				field.name)

		if field.fieldType == BlockInterfaceField.FTYPE_TEMP:
			self.tempFieldCount += 1
		elif field.fieldType == BlockInterfaceField.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)

	# Set fieldIndex of each field.
	def __enumerateFields(self):
		for i, field in enumerate(self.fields_IN_OUT_INOUT):
			field.fieldIndex = i

	def __buildField(self, cpu, struct, field, isFirst):
		if isFirst:
			# Align to word boundary.
			structField = struct.addFieldAligned(cpu,
							field.name,
							field.dataType, 2)
		else:
			# Align naturally.
			structField = struct.addFieldNaturallyAligned(cpu,
							field.name,
							field.dataType)
		if self.hasInstanceDB and\
		   field.fieldType == field.FTYPE_INOUT and\
		   field.dataType.compound:
			# This is an FB IN_OUT compound data type parameter.
			# These are special. They are passed via DB-pointer.
			# The DB-pointer is stored in the instance DB.
			# Set the struct field override for this struct field
			# to DB-pointer type.
			structField.override = AwlStructField(
					structField.name, structField.offset,
					"POINTER")

	def __resolveMultiInstanceField(self, _cpu, field):
#@cy		cdef S7CPU cpu

		from awlsim.core.datatypes import AwlDataType
		cpu = _cpu

		if field.dataType.type != AwlDataType.TYPE_FB_X and\
		   field.dataType.type != AwlDataType.TYPE_SFB_X:
			# Not a multi-instance field.
			return
		if field.dataType.width >= 0:
			# Already resolved.
			return

		# This is a multi-instance element.
		# Get the FB that is embedded as multi-instance.
		if field.dataType.type == AwlDataType.TYPE_SFB_X:
			multiFB = cpu.getSFB(field.dataType.index)
		else:
			multiFB = cpu.getFB(field.dataType.index)
		if not multiFB:
			raise AwlSimError("The function block '%s' of the "
				"embedded multi-instance '%s' "
				"does not exist." %\
				(str(field.dataType), str(field.name)))

		# This embedded (S)FB might contain multi-instances, too.
		# Resolve them first. Do this by building its data structure.
		# Catch recursions.
		try:
			multiFB.interface.buildDataStructure(cpu)
		except self.StructRecursion:
			raise AwlSimError("Recursion detected while trying to "
				"resolve the multiinstance variables in %s." %\
				(str(multiFB)))

		# Assign the type width, in bits.
		field.dataType.width = multiFB.interface._struct.getSize() * 8
		if field.dataType.width == 0:
			# This is not supported by S7.
			# Awlsim _could_ support it, though.
			raise AwlSimError("Multiinstances with zero size "
				"are not supported. Please declare at least "
				"one static, input, output or in_out variable "
				"in %s" % str(multiFB))

	def buildDataStructure(self, cpu):
		if self.__structState == self.STRUCT_BUILT:
			# We already built this block's interface structure.
			return
		if self.__structState == self.STRUCT_BUILDING:
			# Whoops, we recursed! This is bad.
			raise self.StructRecursion()
		self.__structState = self.STRUCT_BUILDING

		self.__enumerateFields()

		# Build instance-DB structure, if any.
		if self.hasInstanceDB:
			# Resolve the sizes of all multi-instance
			# fields in our STAT area.
			for field in self.fields_STAT:
				self.__resolveMultiInstanceField(cpu, field)

			# Build instance-DB structure for the FB
			self._struct = AwlStruct()
			for i, field in enumerate(self.fields_IN):
				self.__buildField(cpu, self._struct, field, i==0)
			for i, field in enumerate(self.fields_OUT):
				self.__buildField(cpu, self._struct, field, i==0)
			for i, field in enumerate(self.fields_INOUT):
				self.__buildField(cpu, self._struct, field, i==0)
			for i, field in enumerate(self.fields_STAT):
				self.__buildField(cpu, self._struct, field, i==0)
		else:
			# An FC does not have an instance-DB
			assert(not self.fields_STAT) # No static data.

		# Build local-stack structure
		self.tempStruct = AwlStruct()
		for i, field in enumerate(self.fields_TEMP):
			self.__buildField(cpu, self.tempStruct, field, i==0)
		self.tempAllocation = self.tempStruct.getSize()
		# If the OB-interface did not specify all automatic TEMP-fields,
		# just force allocate them, so the lstack-allocator will not be confused.
		if self.tempAllocation < self.startupTempAllocation:
			self.tempAllocation = self.startupTempAllocation

		self.__structState = self.STRUCT_BUILT

	# Get an interface field by name string.
	def getFieldByName(self, name):
		try:
			return self.fieldNameMap[name]
		except KeyError:
			raise AwlSimError("Interface field '%s' does not exist." %\
				name)

	# Get the interface field (BlockInterfaceField) by AwlDataIdentChain.
	# Only the first element of the chain is honored.
	# (Other elements belong to sub-STRUCTs.)
	def getFieldByIdentChain(self, identChain):
		fieldName = identChain[0].name
		field = self.getFieldByName(fieldName)
		return field

	# Get the interface field type (FTYPE_xxx) for a
	# given identifier chain (AwlDataIdentChain).
	def getFieldType(self, identChain):
		return self.getFieldByIdentChain(identChain).fieldType

	# Get the data type (AwlDataType) of an interface field
	# identified by a given ident chain (AwlDataIdentChain).
	# If 'deep' is True, get the data type of the last element
	# in the chain. If 'deep' is False, get the type of the first
	# element (the interface element) only.
	def getFieldDataType(self, identChain, deep=True):
		# The interface field is determined by the first ident
		# chain element.
		fieldName = identChain[0].name
		field = self.getFieldByName(fieldName)
		if len(identChain) == 1 or not deep:
			return field.dataType
		identChain = AwlDataIdentChain(identChain[1:])
		# Walk the rest of the identifier chain to get to
		# the data type of the final element.
		structFieldName = identChain.dup(withIndices=False).getString()
		if field.dataType.itemStruct is None:
			raise AwlSimError("Data type '%s' does not have sub fields. "
				"Resolve of sub field '%s' failed." %\
				(str(field.dataType), structFieldName))
		structField = field.dataType.itemStruct.getField(structFieldName)
		return structField.dataType

	# Get an AwlOperator for TEMP access.
	def __getOperatorForField_TEMP(self, identChain, wantPointer):
		structField = self.tempStruct.getField(
			identChain.dup(withIndices=False).getString())
		if wantPointer:
			ptrValue = structField.offset.toPointerValue()
			ptrValue |= PointerConst.AREA_L_S
			oper = make_AwlOperator(operType=AwlOperatorTypes.IMM_PTR,
					   width=32,
					   offset=None,
					   insn=None)
			oper.pointer = Pointer(ptrValue)
			return oper
		oper = make_AwlOperator(operType=AwlOperatorTypes.MEM_L,
				   width=structField.bitSize,
				   offset=structField.offset.dup(),
				   insn=None)
		# If this is a compound data type access, mark
		# the operand as such.
		oper.compound = structField.dataType.compound
		return oper

	# Get an AwlOperator that addresses the specified interface field identified
	# by "identChain", which is an AwlDataIdentChain instance.
	# If wantPointer is true, an IMM_PTR AwlOperator to the interfaceField
	# is returned.
	def getOperatorForField(self, identChain, wantPointer):
		from awlsim.core.datatypes import AwlDataType

		interfaceFieldType = self.getFieldType(identChain)

		if interfaceFieldType == BlockInterfaceField.FTYPE_TEMP:
			# get TEMP interface field operator
			return self.__getOperatorForField_TEMP(identChain,
							       wantPointer)
		# otherwise get IN/OUT/INOUT/STAT interface field operator

		structField = self._struct.getField(identChain.getString())

		# FC-parameters cannot be resolved statically.
		assert(self.hasInstanceDB)

		if wantPointer:
			ptrValue = structField.offset.toPointerValue()
			ptrValue |= PointerConst.AREA_DI_S
			oper = make_AwlOperator(operType=AwlOperatorTypes.IMM_PTR,
					   width=32,
					   offset=None,
					   insn=None)
			oper.pointer = Pointer(ptrValue)
			return oper

		# Translate to instance-DB access

		if structField.dataType.type in AwlDataType.callByRefTypes:
			# "call by reference"
			offsetOper = make_AwlOperator(operType=AwlOperatorTypes.MEM_DI,
						 width=structField.dataType.width,
						 offset=structField.offset.dup(),
						 insn=None)
			if structField.dataType.type == AwlDataType.TYPE_TIMER:
				area = AwlIndirectOpConst.EXT_AREA_T_S
				width = 16
			elif structField.dataType.type == AwlDataType.TYPE_COUNTER:
				area = AwlIndirectOpConst.EXT_AREA_Z_S
				width = 16
			elif structField.dataType.type == AwlDataType.TYPE_BLOCK_DB:
				area = AwlIndirectOpConst.EXT_AREA_BLKREF_DB_S
				width = 16
			elif structField.dataType.type == AwlDataType.TYPE_BLOCK_FB:
				area = AwlIndirectOpConst.EXT_AREA_BLKREF_FB_S
				width = 16
			elif structField.dataType.type == AwlDataType.TYPE_BLOCK_FC:
				area = AwlIndirectOpConst.EXT_AREA_BLKREF_FC_S
				width = 16
			else:
				assert(0)
				return
			return make_AwlIndirectOp(
				area=area,
				width=width,
				addressRegister=AwlIndirectOpConst.AR_NONE,
				offsetOper=offsetOper,
				insn=None)

		if structField.dataType.type in (AwlDataType.TYPE_FB_X,
						 AwlDataType.TYPE_SFB_X):
			# Multi-instance operator (CALL)
			if structField.dataType.type == AwlDataType.TYPE_FB_X:
				operType = AwlOperatorTypes.MULTI_FB
			else:
				operType = AwlOperatorTypes.MULTI_SFB
			offset = structField.offset.dup()
			offset.fbNumber = structField.dataType.index
			return make_AwlOperator(operType=operType,
					   width=structField.bitSize,
					   offset=offset,
					   insn=None)

		# "call by value"
		oper = make_AwlOperator(operType=AwlOperatorTypes.MEM_DI,
				   width=structField.bitSize,
				   offset=structField.offset.dup(),
				   insn=None)
		# If this is a compound data type access, mark
		# the operand as such.
		oper.compound = structField.dataType.compound
		return oper

	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 OBInterface(BlockInterface):
	startupTempAllocation = 20

	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 FBInterface(BlockInterface):
	hasInstanceDB = True

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