summaryrefslogtreecommitdiffstats
path: root/awlsim/callstack.py
blob: 8e333621c7e3880b675b343c43fe8ded3ce83a65 (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
# -*- coding: utf-8 -*-
#
# AWL simulator - CPU call stack
#
# 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.datatypes import *
from awlsim.blocks import *
from awlsim.parameters import *
from awlsim.objectcache import *
from awlsim.util import *


class CallStackElem(object):
	"Call stack element"

	localdataCache = ObjectCache(lambda cpu: ByteArray(cpu.specs.nrLocalbytes))

	@classmethod
	def resetCache(cls):
		cls.localdataCache.reset()

	def __init__(self, cpu, block, interfaceDB=None, parameters=()):
		(self.cpu,
		 self.parenStack,
		 self.ip,
		 self.localdata,
		 self.block,
		 self.insns,
		 self.labels,
		 self.interfaceDB,
		 self.prevDbRegister,
		 self.prevDiRegister) = (
			cpu,
			[],
			0,
			self.localdataCache.get(cpu),
			block,
			block.insns,
			block.labels,
			interfaceDB,
			cpu.dbRegister,
			cpu.diRegister,
		)

		# Handle parameters
		self.__outboundParams = []
		if parameters:
			blockInterface, interfaceDB, structInstance, callByRef_Types =\
				block.interface, \
				self.interfaceDB, \
				self.interfaceDB.structInstance, \
				BlockInterface.callByRef_Types
			for param in parameters:
				if param.isOutbound(blockInterface):
					# This is an outbound parameter.
					self.__outboundParams.append(param)
				if param.isInbound(blockInterface):
					# This is an inbound parameter.
					# Transfer data into DBI
					structField = param.getLvalueStructField(interfaceDB)
					if structField.dataType.type in callByRef_Types:
						data = param.rvalueOp.resolve().value.byteOffset
					else:
						data = cpu.fetch(param.rvalueOp)
					structInstance.setFieldData(structField, data)

	# Handle the exit from this code block.
	def handleBlockExit(self):
		# Transfer data out of DBI
		if self.__outboundParams:
			cpu, interfaceDB, structInstance =\
				self.cpu, \
				self.interfaceDB, \
				self.interfaceDB.structInstance
			for param in self.__outboundParams:
				cpu.store(
					param.rvalueOp,
					structInstance.getFieldData(param.getLvalueStructField(interfaceDB))
				)
		# Assign the DB/DI registers
		if self.block.interface.requiresInstanceDB:
			# We are returning from an FB
			self.cpu.dbRegister, self.cpu.diRegister = self.interfaceDB, self.prevDiRegister
		else:
			# We are returning from an FC
			self.cpu.dbRegister, self.cpu.diRegister = self.prevDbRegister, self.prevDiRegister

	def destroy(self):
		# Only put it back into the cache, if the size didn't change.
		if len(self.localdata) == self.cpu.specs.nrLocalbytes:
			self.localdataCache.put(self.localdata)

	def __repr__(self):
		return str(self.block)
bues.ch cgit interface