aboutsummaryrefslogtreecommitdiffstats
path: root/awlsim/awlcompiler/translator.py
blob: f0053550eaf63d382a7167d13ef37d4896d2b6da (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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
# -*- coding: utf-8 -*-
#
# AWL simulator - AWL translator and symbol resolver
#
# Copyright 2012-2018 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.cpuconfig import *
from awlsim.common.datatypehelpers import * #+cimport

from awlsim.core.instructions.all_insns import * #+cimport
from awlsim.core.datastructure import * #+cimport
from awlsim.core.util import *
from awlsim.core.offset import * #+cimport
from awlsim.core.operatortypes import * #+cimport
from awlsim.core.operators import * #+cimport
from awlsim.core.blocks import * #+cimport
from awlsim.core.blockinterface import *
from awlsim.core.datablocks import * #+cimport
from awlsim.core.identifier import *

from awlsim.awlcompiler.insntrans import *
from awlsim.awlcompiler.optrans import *
from awlsim.awlcompiler.tokenizer import *


__all__ = [
	"AwlTranslator",
	"AwlSymResolver",
]


_AwlDataType = None

def _getAwlDataTypeClass():
	global _AwlDataType
	AwlDataType = _AwlDataType
	if AwlDataType is None:
		from awlsim.core.datatypes import AwlDataType
		_AwlDataType = AwlDataType
	return AwlDataType

class AwlTranslator(object):
	"AWL instruction and operator translator."

	def __init__(self, cpu):
		self.cpu = cpu

	def __translateInsn(self, rawInsn, ip):
		ex = None
		try:
			insn = AwlInsnTranslator.fromRawInsn(self.cpu, rawInsn)
			insn.setIP(ip)
		except AwlSimError as e:
			if e.getRawInsn() is None:
				e.setRawInsn(rawInsn)
			raise e
		return insn

	def __translateInsns(self, rawInsns):
		insns = []
		# Translate raw instructions to simulator instructions
		for ip, rawInsn in enumerate(rawInsns):
			insns.append(self.__translateInsn(rawInsn, ip))
		# If the last instruction is not BE or BEA, add an implicit BE
		if not insns or insns[-1].insnType not in (AwlInsn.TYPE_BE,
							   AwlInsn.TYPE_BEA):
			insns.append(AwlInsn_BE(cpu = self.cpu, rawInsn = None))
		return insns

	def __translateInterfaceField(self, rawField):
		name, dataType, initBytes = self.rawFieldTranslate(rawField)
		if not dataType.allowedInInterface:
			raise AwlSimError("Data type '%s' not allowed in block interface" %\
				str(dataType))
		field = BlockInterfaceField(name = name,
					    dataType = dataType)
		return field

	def translateLibraryCodeBlock(self, block):
		# Switch mnemonics to DE for translation of library code.
		oldMnemonics = self.cpu.getConf().getConfiguredMnemonics()
		self.cpu.getConf().setConfiguredMnemonics(S7CPUConfig.MNEMONICS_DE)

		# Enable extended instructions for library code.
		oldExtEn = self.cpu.extendedInsnsEnabled()
		self.cpu.enableExtendedInsns(True)

		# Parse the library code
		p = AwlParser()
		p.parseText(block.getCode())
		tree = p.getParseTree()
		if block.isFC:
			assert(len(tree.fcs) == 1)
			rawBlock = getfirst(dictValues(tree.fcs))
		elif block.isFB:
			assert(len(tree.fbs) == 1)
			rawBlock = getfirst(dictValues(tree.fbs))
		else:
			assert(0)
		# Translate the library block instructions.
		block.insns = self.__translateInsns(rawBlock.insns)
		block.nrInsns = len(block.insns)
		block.resolveLabels()

		# Switch back to old extended-instructions state.
		self.cpu.enableExtendedInsns(oldExtEn)

		# Switch back to old mnemonics.
		self.cpu.getConf().setConfiguredMnemonics(oldMnemonics)

		return block

	def translateCodeBlock(self, rawBlock, blockClass):
		insns = self.__translateInsns(rawBlock.insns)
		block = blockClass(insns, rawBlock.index)
		block.setSourceRef(rawBlock.sourceRef, inheritRef = True)

		# Construct the block interface
		for rawVar in rawBlock.vars_in:
			block.interface.addField_IN(self.__translateInterfaceField(rawVar))
		for rawVar in rawBlock.vars_out:
			block.interface.addField_OUT(self.__translateInterfaceField(rawVar))
		if rawBlock.retTypeTokens:
			# ARRAY is not supported for RET_VAL. So make non-array dtype.
			AwlDataType = _getAwlDataTypeClass()
			dtype = AwlDataType.makeByName(rawBlock.retTypeTokens)
			if dtype.type != AwlDataType.TYPE_VOID:
				# Ok, we have a RET_VAL.
				field = BlockInterfaceField(name = "RET_VAL",
							    dataType = dtype)
				block.interface.addField_OUT(field)
		for rawVar in rawBlock.vars_inout:
			block.interface.addField_INOUT(self.__translateInterfaceField(rawVar))
		for rawVar in rawBlock.vars_static:
			block.interface.addField_STAT(self.__translateInterfaceField(rawVar))
		for rawVar in rawBlock.vars_temp:
			block.interface.addField_TEMP(self.__translateInterfaceField(rawVar))
		return block

	# Create an AwlStruct from a list of RawAwlDataFields.
	def __createStructFromRawFields(self, rawFields):
		struct = AwlStruct()
		for rawField in rawFields:
			name, dataType, initBytes = self.rawFieldTranslate(rawField)
			if not dataType.allowedInStruct:
				raise AwlSimError("Data type '%s' not allowed in STRUCT" %\
					str(dataType))
			name = rawField.getIdentString(fullChain = False)
			struct.addFieldNaturallyAligned(self.cpu, name,
							dataType, initBytes)
		return struct

	# Translate a RawAwlDataField to an AwlDataType.
	def __rawFieldToDataType(self, rawField):
		AwlDataType = _getAwlDataTypeClass()

		dtype = AwlDataType.makeByName(nameTokens = rawField.typeTokens,
					       arrayDimensions = rawField.dimensions)
		if dtype.type == AwlDataType.TYPE_STRUCT:
			# Make the AwlStruct that represents the STRUCT contents.
			if not rawField.children:
				raise AwlSimError("Data structure does not have "
					"any containing variables. (STRUCT is empty)")
			struct = self.__createStructFromRawFields(rawField.children)
			dtype.setStruct(struct)
		elif dtype.type == AwlDataType.TYPE_ARRAY and\
		     dtype.arrayElementType.type == AwlDataType.TYPE_STRUCT:
			# Make the AwlStruct that represents the
			# ARRAY-element (of type STRUCT) contents.
			if not rawField.children:
				raise AwlSimError("Data structure does not have "
					"any containing variables. (STRUCT is empty)")
			struct = self.__createStructFromRawFields(rawField.children)
			dtype.arrayElementType.setStruct(struct)
		else:
			if rawField.children:
				raise AwlSimError("Data type '%s' has children '%s', "
					"but is not a STRUCT." %\
					(str(dtype), str(rawField.children)))
		return dtype

	# Translate a RawAwlDataField to a (name, dataType, initBytes) tuple where:
	# name: The data field name string.
	# dataType: The AwlDataType for this field.
	# initBytes: The initialization bytes for this field, or None.
	def rawFieldTranslate(self, rawField):
#@cy		cdef AwlMemoryObject memObj

		# Get the field name
		name = rawField.getIdentString()
		# Get the field data type
		if len(rawField.typeTokens) == 1 and\
		   rawField.typeTokens[0].startswith('"') and\
		   rawField.typeTokens[0].endswith('"') and\
		   self.cpu:
			# The data type is symbolic.
			# (symbolic multi instance field)
			# Resolve it.
			assert(not rawField.dimensions)
			symStr = rawField.typeTokens[0][1:-1] # Strip quotes
			resolver = AwlSymResolver(self.cpu)
			AwlDataType = _getAwlDataTypeClass()
			fbNumber, sym = resolver.resolveBlockName((AwlDataType.TYPE_FB_X,
								   AwlDataType.TYPE_SFB_X),
								  symStr)
			# Get the data type from the symbol.
			dataType = sym.type
		else:
			# Parse the data type.
			dataType = self.__rawFieldToDataType(rawField)
		# Get the field inits
		if rawField.defaultInits:
			# Translate the initialization values and
			# put them into a bytearray.
			initMem = AwlMemory(intDivRoundUp(dataType.width, 8))
			AwlDataType = _getAwlDataTypeClass()
			if dataType.type == AwlDataType.TYPE_ARRAY:
				for rawDataInit in rawField.defaultInits:
					value = dataType.parseMatchingImmediate(rawDataInit.valueTokens)
					linArrayIndex = dataType.arrayIndicesCollapse(
						rawDataInit.identChain[-1].indices)
					offset = make_AwlOffset_fromLongBitOffset(
							linArrayIndex *
							dataType.arrayElementType.width)
					memObj = make_AwlMemoryObject_fromGeneric(
							value,
							dataType.arrayElementType.width)
					try:
						initMem.store(offset, memObj)
					except AwlSimError as e:
						raise AwlSimError("Data field '%s' initialization "
							"is out of range." % str(rawField))
			else:
				assert(len(rawField.defaultInits) == 1)
				value = dataType.parseMatchingImmediate(rawField.defaultInits[0].valueTokens)
				memObj = make_AwlMemoryObject_fromGeneric(
						value, dataType.width)
				try:
					initMem.store(make_AwlOffset(0, 0), memObj)
				except AwlSimError as e:
					raise AwlSimError("Data field '%s' initialization "
						"is out of range." % str(rawField))
			initBytes = initMem.getDataBytes()
		else:
			initBytes = None
		return name, dataType, initBytes

	# Initialize a DB (global or instance) data field from a raw data-init.
	def __initDBField(self, db, dataType, rawDataInit):
#@cy		cdef AwlStructInstance structInstance
#@cy		cdef AwlMemoryObject memObj

		fieldName = rawDataInit.getIdentString()
		value = dataType.parseMatchingImmediate(rawDataInit.valueTokens)
		if dataType.type == dataType.TYPE_ARRAY:
			# This is a single-array-element initializer.
			width = dataType.arrayElementType.width
		else:
			width = dataType.width
		memObj = make_AwlMemoryObject_fromGeneric(value, width)
		structInstance = db.structInstance
		structInstance.setFieldDataByName(fieldName, memObj)

	# Create a DB data field.
	def __createDBField(self, db, rawDataField):
		name, dataType, initBytes = self.rawFieldTranslate(rawDataField)
		if not dataType.allowedInStruct:
			raise AwlSimError("Data type '%s' not allowed in DB" %\
				str(dataType))
		db.struct.addFieldNaturallyAligned(self.cpu, name,
						   dataType, initBytes)

	def __translateGlobalDB(self, rawDB):
		db = DB(rawDB.index, None)
		db.setSourceRef(rawDB.sourceRef, inheritRef = True)
		# Create the data structure fields
		for rawField in rawDB.fields:
			self.__createDBField(db, rawField)
		# Allocate the data structure fields
		db.allocate()
		# Assign data structure initializations.
		for field, init in rawDB.allFieldInits():
			if not field:
				raise AwlSimError(
					"DB %d assigns field '%s', "
					"but does not declare it." %\
					(rawDB.index, init.getIdentString()))
			assert(field.getIdentChain() == init.identChain)
			AwlDataType = _getAwlDataTypeClass()
			dtype = self.__rawFieldToDataType(field)
			if dtype == AwlDataType.TYPE_STRUCT:
				raise AwlSimError(
					"Invalid assignment to STRUCT.")
			self.__initDBField(db, dtype, init)
		return db

	def __translateInstanceDB(self, rawDB):
#@cy		cdef S7CPU cpu

		if rawDB.fields:
			raise AwlSimError("DB %d is an "
				"instance DB, but it also "
				"declares a data structure." %\
				rawDB.index)

		if rawDB.fb.fbSymbol is None:
			# The FB name is absolute.
			fbNumber = rawDB.fb.fbNumber
			fbStr = "SFB" if rawDB.fb.isSFB else "FB"
			fbStr += " %d" % fbNumber
			isSFB = rawDB.fb.isSFB
		else:
			# The FB name is symbolic. Resolve it.
			AwlDataType = _getAwlDataTypeClass()
			resolver = AwlSymResolver(self.cpu)
			fbStr = '"%s"' % rawDB.fb.fbSymbol
			fbNumber, sym = resolver.resolveBlockName((AwlDataType.TYPE_FB_X,
								   AwlDataType.TYPE_SFB_X),
								  rawDB.fb.fbSymbol)
			isSFB = sym.type.type == AwlDataType.TYPE_SFB_X

		# Get the FB/SFB code block
		cpu = self.cpu
		if isSFB:
			fb = cpu.getSFB(fbNumber)
		else:
			fb = cpu.getFB(fbNumber)
		if not fb:
			raise AwlSimError("Instance DB %d references %s, "
				"but %s does not exist." %\
				(rawDB.index, fbStr, fbStr))

		# Create an instance data block
		db = DB(rawDB.index, fb)
		db.setSourceRef(rawDB.sourceRef, inheritRef = True)
		interface = fb.interface
		# Allocate the data structure fields
		db.allocate()
		# Initialize the data structure fields
		for init in rawDB.fieldInits:
			structField = db.struct.getField(init.getIdentString())
			self.__initDBField(db, structField.dataType, init)
		return db

	def translateDB(self, rawDB):
		if rawDB.index < 1 or rawDB.index > 0xFFFF:
			raise AwlSimError("DB number %d is invalid" % rawDB.index)
		if rawDB.isInstanceDB():
			return self.__translateInstanceDB(rawDB)
		return self.__translateGlobalDB(rawDB)

	# Translate implicit pointer immediates.
	# Implicit pointer immediates are assignments of non-pointer
	# operands to POINTER or ANY l-values.
	def __translateParamPointer(self, param):
#@cy		cdef Pointer pointer

		AwlDataType = _getAwlDataTypeClass()

		if param.lValueDataType.type == AwlDataType.TYPE_POINTER:
			# POINTER parameter.
			if param.rvalueOp.operType == AwlOperatorTypes.IMM_PTR:
				# Make sure this is a DB-pointer immediate (48 bit).
				if param.rvalueOp.width not in {32, 48}:
					raise AwlSimError("Invalid pointer immediate "
						"assignment to POINTER parameter.")
				pointer = param.rvalueOp.pointer
				pointer = param.rvalueOp.pointer = pointer.toDBPointer()
				param.rvalueOp.width = pointer.width
			else:
				# Translate the r-value to POINTER.
				try:
					pointer = param.rvalueOp.makeDBPointer()
				except (AwlSimBug, AwlSimError) as e:
					raise AwlSimError("Unable to transform "
						"operator '%s' to POINTER." %\
						str(param.rvalueOp))
				param.rvalueOp = make_AwlOperator(
					operType=AwlOperatorTypes.IMM_PTR,
					width=pointer.width,
					offset=None,
					insn=param.rvalueOp.insn)
				param.rvalueOp.pointer = pointer
			pointer = param.rvalueOp.pointer
			if pointer.getArea() == PointerConst.AREA_L:
				# L-stack access must be translated to VL.
				pointer.setArea(PointerConst.AREA_VL)
		elif param.lValueDataType.type == AwlDataType.TYPE_ANY:
			# ANY-pointer parameter.
			if param.rvalueOp.operType == AwlOperatorTypes.IMM_PTR:
				# Make sure this is an ANY-pointer immediate (80 bit).
				if param.rvalueOp.width not in {32, 48, 80}:
					raise AwlSimError("Invalid pointer immediate "
						"assignment to ANY parameter.")
				pointer = param.rvalueOp.pointer
				pointer = param.rvalueOp.pointer = pointer.toANYPointer()
				param.rvalueOp.width = pointer.width
			elif param.rvalueOp.operType == AwlOperatorTypes.MEM_L and\
			     param.rvalueOp.dataType is not None and\
			     param.rvalueOp.dataType.type == AwlDataType.TYPE_ANY:
				# r-value is an ANY pointer in L (as TEMP variable).
				# Forward it as-is to the FC.
				# The operator will be translated from MEM_L to MEM_VL
				# at call time.
				assert(param.rvalueOp.width == 80)
			else:
				# Translate the r-value to ANY.
				try:
					pointer = param.rvalueOp.makeANYPointer()
				except (AwlSimBug, AwlSimError) as e:
					raise AwlSimError("Unable to transform "
						"operator '%s' to ANY pointer." %\
						str(param.rvalueOp))
				param.rvalueOp = make_AwlOperator(
					operType=AwlOperatorTypes.IMM_PTR,
					width=pointer.width,
					offset=None,
					insn=param.rvalueOp.insn)
				param.rvalueOp.pointer = pointer
			pointer = param.rvalueOp.pointer
			if param.rvalueOp.operType == AwlOperatorTypes.IMM_PTR and\
			   pointer.getArea() == PointerConst.AREA_L:
				# L-stack access must be translated to VL.
				pointer.setArea(PointerConst.AREA_VL)

	# Translate STRING immediates.
	# Converts STRING to CHAR, if required, or expands string lengths.
	def __translateParamString(self, param):
		AwlDataType = _getAwlDataTypeClass()

		if param.lValueDataType.type == AwlDataType.TYPE_CHAR:
			# CHAR parameter.
			if param.rvalueOp.operType == AwlOperatorTypes.IMM_STR:
				# Translate single-character string immediates
				# to 8 bit integer immediates.
				if param.rvalueOp.width == (2 + 1) * 8:
					immediate = param.rvalueOp.immediateBytes[2]
					param.rvalueOp = make_AwlOperator(
						operType=AwlOperatorTypes.IMM,
						width=8,
						offset=None,
						insn=param.rvalueOp.insn)
					param.rvalueOp.immediate = immediate
		elif param.lValueDataType.type == AwlDataType.TYPE_STRING:
			# STRING parameter.
			if param.rvalueOp.operType == AwlOperatorTypes.IMM_STR:
				if param.rvalueOp.width < param.lValueDataType.width:
					# Expand the string immediate length.
					# This is an awlsim extension.
					curLen = param.rvalueOp.width // 8
					newLen = param.lValueDataType.width // 8
					assert(curLen >= 2 and newLen >= 2)
					data = bytearray(param.rvalueOp.immediateBytes)
					data[0] = newLen - 2
					data += b'\x00' * (newLen - curLen)
					param.rvalueOp.immediateBytes = data
					param.rvalueOp.width = newLen * 8

	# Final translation of AwlParamAssign r-value operands.
	# Overrides the rvalueOp in place, if required.
	def translateParamAssignOper(self, param):
		self.__translateParamPointer(param)
		self.__translateParamString(param)

class AwlSymResolver(object):
	"Global and local symbol resolver."

	def __init__(self, cpu):
		self.cpu = cpu

	# Resolve classic symbols ("abc")
	def __resolveClassicSym(self, block, insn, oper):
		if oper.operType == AwlOperatorTypes.SYMBOLIC:
			symbol = self.cpu.symbolTable.findByName(
				oper.offset.identChain.getString())
			if not symbol:
				raise AwlSimError("Symbol \"%s\" not found in "
					"symbol table." %\
					oper.offset.identChain.getString(),
					insn = insn)
			newOper = symbol.operator.dup()
			newOper.setInsn(oper.insn)
			return newOper
		return oper

	# Resolve symbolic OB/FB/FC/DB block name
	def resolveBlockName(self, blockTypeIds, blockName):
		if isString(blockName):
			symbol = self.cpu.symbolTable.findByName(blockName)
			if not symbol:
				raise AwlSimError("Symbolic block name \"%s\" "
					"not found in symbol table." % blockName)
			if symbol.type.type not in blockTypeIds:
				raise AwlSimError("Symbolic block name \"%s\" "
					"has an invalid type." % blockName)
			return symbol.operator.offset.byteOffset, symbol
		return blockName, None

	# Resolve local symbols (#abc or P##abc)
	# If pointer is false, try to resolve #abc.
	# If pointer is true, try to resolve P##abc.
	# If allowWholeArrayAccess is true, unsubscripted accesses
	# to array variables are supported.
	def resolveNamedLocal(self, block, insn, oper,
			      pointer=False, allowWholeArrayAccess=False):
#@cy		cdef S7CPU cpu

		# Check whether we need to do something.
		# Otherwise just return the source operator.
		if pointer:
			if oper.operType != AwlOperatorTypes.NAMED_LOCAL_PTR:
				return oper
		else:
			if oper.operType != AwlOperatorTypes.NAMED_LOCAL:
				return oper

		AwlDataType = _getAwlDataTypeClass()
		cpu = self.cpu

		# Walk the ident chain to accumulate the sub-offsets
		# for the ARRAY accesses.
		parentStruct = None
		subOffset = make_AwlOffset(0, 0)
		for i in range(len(oper.offset.identChain)):
			isFirstElement = (i == 0)
			isLastElement = (i == len(oper.offset.identChain) - 1)

			# Get the sub-chain and the interface field.
			chain = AwlDataIdentChain(oper.offset.identChain[:i+1])
			dataType = block.interface.getFieldDataType(chain)

			# Sanity checks
			if dataType.type in {AwlDataType.TYPE_ARRAY,
					     AwlDataType.TYPE_STRING}:
				if isLastElement and\
				   not chain[-1].indices and\
				   oper.operType != AwlOperatorTypes.NAMED_LOCAL_PTR and\
				   not allowWholeArrayAccess:
					raise AwlSimError("Cannot address array #%s "
						"without subscript list." %\
						chain.getString())
			else:
				if chain[-1].indices:
					raise AwlSimError("Trying to subscript array, "
						"but #%s is not an array." %\
						chain.getString())

			# Assign the struct to the UDT data type, if
			# not already done so.
			if dataType.type == AwlDataType.TYPE_UDT_X:
				udt = cpu.getUDT(dataType.index)
				if not udt:
					raise AwlSimError("UDT %d not found on CPU" %\
						dataType.index)
				assert(dataType._struct is None or
				       dataType._struct is udt._struct)
				dataType.setStruct(udt._struct)

			# Add the struct field offset of this field to the subOffset.
			# Need to look it up in the parent struct.
			if not isFirstElement:
				assert(parentStruct)
				structFieldName = chain[-1].dup(withIndices=False).getString()
				structField = parentStruct.getField(structFieldName)
				subOffset += structField.offset

			# Add array offset to subOffset,
			# if this is an ARRAY or STRING element access.
			if chain[-1].indices:
				if dataType.type == AwlDataType.TYPE_ARRAY:
					# Calculate the array offset.
					arrayIndex = dataType.arrayIndicesCollapse(chain[-1].indices)
					elemWidth = dataType.arrayElementType.width
					bitOffset = arrayIndex * elemWidth
					byteOffset = bitOffset // 8
					bitOffset %= 8
				elif dataType.type == AwlDataType.TYPE_STRING:
					# Calculate the string offset.
					if len(chain[-1].indices) != 1:
						raise AwlSimError("Only one index is "
							"allowed in STRING indexing.")
					index = chain[-1].indices[0]
					maxIdx = dataType.width // 8 - 2
					if index < 1 or index > maxIdx:
						raise AwlSimError("STRING index %d is "
							"out of range 1-%d." %\
							(index, maxIdx))
					byteOffset = 2 + index - 1
					bitOffset = 0
				else:
					assert(0)
				# Add it to the accumulated offset.
				subOffset += make_AwlOffset(byteOffset, bitOffset)

			parentStruct = dataType.itemStruct

		# 'dataType' now is the type of last field in the identChain.
		# (The field that we eventually address).

		isWholeArrayAccess = ((dataType.type == AwlDataType.TYPE_ARRAY or\
				       dataType.type == AwlDataType.TYPE_STRING) and\
				      not oper.offset.identChain[-1].indices)
		if dataType.type == AwlDataType.TYPE_ARRAY and\
		   not isWholeArrayAccess:
			# This is an array element access.
			accessDataType = dataType.arrayElementType
		elif dataType.type == AwlDataType.TYPE_STRING and\
		     not isWholeArrayAccess:
			# This is a string single character access.
			accessDataType = AwlDataType.makeByName("CHAR")
		else:
			# Non-array access or whole-array access.
			accessDataType = dataType
		# Store the access type and width in the operator.
		oper.dataType = accessDataType
		oper.width = accessDataType.width
		assert(oper.width > 0)

		# Store the sub-offset (might be zero).
		if subOffset.byteOffset or subOffset.bitOffset:
			oper.offset.subOffset = subOffset
		else:
			oper.offset.subOffset = None

		# If interface field is of compound data type access, mark
		# the operand as such.
		basicType = block.interface.getFieldDataType(chain, deep=False)
		oper.compound = basicType.compound

		fieldType = block.interface.getFieldType(oper.offset.identChain)
		if block.interface.hasInstanceDB or\
		   fieldType == BlockInterfaceField.FTYPE_TEMP:
			# This is an FB or a TEMP access. Translate the operator
			# to a DI/TEMP access.
			newOper = block.interface.getOperatorForField(oper.offset.identChain,
								      pointer)
			assert(newOper.width > 0)
			newOper.setInsn(oper.insn)
			newOper.compound = oper.compound
			newOper.dataType = oper.dataType
			return newOper
		else:
			# This is an FC. Accesses to local symbols
			# are resolved at runtime.
			# Just set interface index in the operator.
			# Pointer access (oper.operType == NAMED_LOCAL_PTR) is resolved
			# later at runtime.
			identChain = oper.offset.identChain.dup(withIndices = False)
			index = block.interface.getFieldByIdentChain(identChain).fieldIndex
			oper.interfaceIndex = index
		return oper

	# Resolve a symbolic DB name. Returns DB index number.
	def __resolveDBName(self, dbName):
		AwlDataType = _getAwlDataTypeClass()

		symbol = self.cpu.symbolTable.findByName(dbName)
		if not symbol:
			raise AwlSimError("Symbol \"%s\" specified as DB in "
				"fully qualified operator not found." %\
				dbName)
		if symbol.type.type != AwlDataType.TYPE_DB_X:
			raise AwlSimError("Symbol \"%s\" specified as DB in "
				"fully qualified operator is not a DB-symbol." %\
				dbName)
		return symbol.operator.offset.byteOffset

	# Get offset and width of a DB field.
	def __dbVarToOffset(self, dbNumber, identChain, allowWholeArrayAccess=True):
#@cy		cdef S7CPU cpu

		# Get the DB
		cpu = self.cpu
		db = cpu.getDB(dbNumber)
		if not db:
			raise AwlSimError("DB %d specified in fully qualified "
				"operator does not exist." % dbNumber)

		AwlDataType = _getAwlDataTypeClass()

		# Get the data structure base field descriptor.
		# For arrays, this is the base name of the array.
		tmpIdentChain = identChain.dup()
		tmpIdentChain[-1] = tmpIdentChain[-1].dup(withIndices = False)
		field = db.struct.getField(tmpIdentChain.getString())
		if identChain[-1].indices is None:
			# Access without array indices.
			if field.dataType.type == AwlDataType.TYPE_ARRAY:
				# This is a whole-array access.
				#  e.g.  DB1.ARRAYVAR
				if not allowWholeArrayAccess:
					raise AwlSimError("Variable '%s' in fully qualified "
						"DB access is an ARRAY." %\
						identChain.getString())
		else:
			# This is an array field access.
			# (The original last ident chain field has indices.)
			if field.dataType.type not in {AwlDataType.TYPE_ARRAY,
						       AwlDataType.TYPE_STRING}:
				raise AwlSimError("Indexed variable '%s' in fully qualified "
					"DB access is not an ARRAY or STRING." %\
					oper.offset.identChain.getString())
			# Get the actual data field.
			# (Don't remove indices from the last chain element.)
			field = db.struct.getField(identChain.getString())

		# Extract the offset data
		offset = field.offset.dup()
		width = field.bitSize
		dataType = field.dataType

		return offset, width, dataType

	# Resolve named fully qualified accesses (DBx.VARx)
	# If allowWholeArrayAccess is true, unsubscripted accesses
	# to array variables are supported.
	def __resolveNamedFullyQualified(self, block, insn, oper,
					 allowWholeArrayAccess=False):
		if oper.operType != AwlOperatorTypes.NAMED_DBVAR:
			return oper

		# Resolve the symbolic DB name, if needed
		assert(oper.offset.dbNumber >= 0 or\
		       oper.offset.dbName is not None)
		if oper.offset.dbNumber < 0:
			oper.offset.dbNumber = self.__resolveDBName(oper.offset.dbName)

		# Get the offset data and the width of the field.
		offset, width, fieldDataType = self.__dbVarToOffset(oper.offset.dbNumber,
								    oper.offset.identChain,
								    allowWholeArrayAccess)
		offset.dbNumber = oper.offset.dbNumber

		# Construct an absolute operator
		oper = make_AwlOperator(operType=AwlOperatorTypes.MEM_DB,
				   width=width,
				   offset=offset,
				   insn=oper.insn)
		# If this is a compound data type access, mark
		# the operand as such.
		oper.compound = fieldDataType.compound

		# Assign access data type.
		oper.dataType = fieldDataType

		return oper

	# Resolve named fully qualified pointers (P#DBx.VARx)
	# This is an awlsim extension.
	def __resolveNamedFQPointer(self, block, insn, oper, param=None):
#@cy		cdef Pointer pointer

		pointer = oper.pointer
		if oper.operType != AwlOperatorTypes.IMM_PTR:
			return oper
		if pointer.width > 0:
			return oper
		assert(isinstance(pointer, SymbolicDBPointer))

		# Resolve the symbolic DB name, if needed
		assert(pointer.dbNr or\
		       pointer.dbSymbol is not None)
		if pointer.dbSymbol is not None:
			pointer.dbNr = self.__resolveDBName(pointer.dbSymbol)

		# Get the offset data and the width of the field.
		offset, width, fieldDataType = self.__dbVarToOffset(pointer.dbNr,
								    pointer.identChain)

		# Write the pointer value.
		pointer.setDWord(offset.toPointerValue() |\
				 (PointerConst.AREA_DB << PointerConst.AREA_SHIFT))

		AwlDataType = _getAwlDataTypeClass()

		# Create a resolved Pointer.
		if param and\
		   param.lValueDataType.type == AwlDataType.TYPE_POINTER:
			# Create a resolved DB-Pointer (48 bit).
			pointer = oper.pointer = pointer.toDBPointer()
		elif param and\
		     param.lValueDataType.type == AwlDataType.TYPE_ANY:
			# Create a resolved ANY-Pointer (80 bit).
			pointer = oper.pointer = ANYPointer.makeByAutoType(
					dataType=fieldDataType,
					ptrValue=pointer.toPointerValue(),
					dbNr=pointer.toDBPointer().dbNr)
		else:
			# Create a resolved pointer (32 bit).
			pointer = oper.pointer = pointer.toPointer()
		oper.dataType = fieldDataType
		oper.width = pointer.width

		return oper

	# Resolve all symbols in the given code block
	def resolveSymbols_block(self, block):
		block.resolveSymbols()
		for insn in block.insns:
			try:
				for i, oper in enumerate(insn.ops):
					oper = self.__resolveClassicSym(block, insn, oper)
					oper = self.resolveNamedLocal(block, insn, oper, False)
					oper = self.__resolveNamedFullyQualified(block,
										 insn, oper, False)
					oper = self.__resolveNamedFQPointer(block, insn, oper)
					if oper.operType == AwlOperatorTypes.INDIRECT:
						oper.offsetOper = self.__resolveClassicSym(block,
									insn, oper.offsetOper)
						oper.offsetOper = self.resolveNamedLocal(block,
									insn, oper.offsetOper, False)
					oper = self.resolveNamedLocal(block, insn, oper, True)
					insn.ops[i] = oper
				for param in insn.params:
					oper = param.rvalueOp
					oper = self.__resolveClassicSym(block, insn, oper)
					oper = self.resolveNamedLocal(block, insn, oper, False, True)
					oper = self.__resolveNamedFullyQualified(block,
										 insn, oper, True)
					oper = self.__resolveNamedFQPointer(block, insn, oper, param)
					if oper.operType == AwlOperatorTypes.INDIRECT:
						oper.offsetOper = self.__resolveClassicSym(block,
									insn, oper.offsetOper)
						oper.offsetOper = self.resolveNamedLocal(block,
									insn, oper.offsetOper, False)
					oper = self.resolveNamedLocal(block, insn, oper, False, True)
					param.rvalueOp = oper
			except AwlSimError as e:
				if not e.getInsn():
					e.setInsn(insn)
				raise e
bues.ch cgit interface