aboutsummaryrefslogtreecommitdiffstats
path: root/awlsim/core/blocks.py
blob: 26bb5d6f5837d433a08fc14d2a1ce2f8950a9154 (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
# -*- coding: utf-8 -*-
#
# AWL simulator - blocks
#
# 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.refmanager import *
from awlsim.common.blockinfo import *
from awlsim.common.wordpacker import *
from awlsim.common.exceptions import *

from awlsim.core.blockinterface import *
from awlsim.core.labels import *
from awlsim.core.datatypes import *
from awlsim.core.memory import * #+cimport
from awlsim.core.operatortypes import * #+cimport
from awlsim.core.operators import * #+cimport
from awlsim.core.util import *
from awlsim.core.offset import * #+cimport
from awlsim.core.lstack import * #+cimport

import hashlib


__all__ = [
	"Block",
	"CodeBlock",
	"StaticCodeBlock",
	"OB",
	"FB",
	"FC",
]


class Block(object): #+cdef
	"""Base class for blocks (OBs, FCs, FBs, DBs, etc...)"""

	BLOCKTYPESTR	= "Block"
	IDENT_HASH	= hashlib.sha256

	def __init__(self, index):
		self.index = index
		self.sourceRef = None
		self.__identHash = None

	def setSourceRef(self, sourceManagerOrRef, inheritRef=False):
		if isinstance(sourceManagerOrRef, ObjRef):
			manager = None
			ref = sourceManagerOrRef
		else:
			manager = sourceManagerOrRef
			ref = None
		self.sourceRef = ObjRef.make(
			name=lambda ref: str(ref.obj),
			manager=manager,
			ref=ref,
			inheritRef=inheritRef,
			obj=self)
		self.__identHash = None

	def destroySourceRef(self):
		if self.sourceRef:
			self.sourceRef.destroy()
			self.sourceRef = None
		self.__identHash = None

	def getSource(self):
		"""Get this block's source (AwlSource() object), if any.
		"""
		sourceRef = self.sourceRef
		if sourceRef:
			manager = sourceRef.manager
			if manager:
				return manager.source
		return None

	@property
	def identHash(self):
		if not self.__identHash:
			# Calculate the ident hash
			h = self.IDENT_HASH(self.BLOCKTYPESTR.encode(
					"utf-8", "strict"))
			h.update(WordPacker.toBytes(bytearray(2), 16, 0, self.index))
			source = self.getSource()
			if source:
				h.update(source.identHash)
			self.__identHash = h.digest()
		return self.__identHash

	def getBlockInfo(self):
		"""Get a BlockInfo instance for this block.
		"""
		# By default there is no BlockInfo. Override this method.
		return None

	def __repr__(self):
		return "%s %d" % (self.BLOCKTYPESTR, self.index)

class CodeBlock(Block): #+cdef
	"""Base class for code blocks (OBs, (S)FCs, (S)FBs)"""

	BLOCKTYPESTR	= "CodeBlock"

	# Simple and fast tests for checking block identity.
	# These are partially overridden in the subclasses.
	_isOB		= False
	_isFC		= False
	_isFB		= False
	_isSystemBlock	= False
	_isLibraryBlock	= False

	def __init__(self, insns, index, interface):
		self.isOB = self._isOB
		self.isFC = self._isFC
		self.isFB = self._isFB
		self.isSystemBlock = self._isSystemBlock
		self.isLibraryBlock = self._isLibraryBlock
		Block.__init__(self, index)

		self.insns = insns
		self.nrInsns = len(insns)
		self.labels = None
		self.interface = interface
		self.tempAllocation = 0		# The number of allocated TEMP bytes
		self.resolveLabels()

	def resolveLabels(self):
		if self.insns:
			self.labels = AwlLabel.resolveLabels(self.insns)
		else:
			self.labels = None

	def resolveSymbols(self):
		pass

	# Account for interface TEMP allocations and
	# direct TEMP (L, LB, LW, LD) accesses.
	def accountTempAllocations(self):
		directAlloc = 0

		def accountDirect(currentAlloc, oper):
			if oper.operType != AwlOperatorTypes.MEM_L:
				return currentAlloc
			offset = oper.offset + make_AwlOffset(0, oper.width)
			return max(offset.roundUp(2).byteOffset,
				   currentAlloc)

		for insn in self.insns:
			for oper in insn.ops:
				directAlloc = accountDirect(directAlloc,
							    oper)
			for param in insn.params:
				directAlloc = accountDirect(directAlloc,
							    param.rvalueOp)

		self.tempAllocation = max(self.interface.tempAllocation,
					  directAlloc)

class StaticCodeBlock(CodeBlock): #+cdef
	"""Base class for static code blocks. (system and library blocks)."""

	BLOCKTYPESTR	= "StaticCodeBlock"

	# Static interface definition.
	# To be overridden by the subclass.
	interfaceFields = {
		BlockInterfaceField.FTYPE_IN	: (),
		BlockInterfaceField.FTYPE_OUT	: (),
		BlockInterfaceField.FTYPE_INOUT	: (),
		BlockInterfaceField.FTYPE_STAT	: (),
		BlockInterfaceField.FTYPE_TEMP	: (),
	}

	# Set to True by the subclass, if the implementation is incomplete.
	broken = False

	def __init__(self, insns, index, interface):
		CodeBlock.__init__(self, insns, index, interface)

		# Register the interface.
		for ftype in (BlockInterfaceField.FTYPE_IN,
			      BlockInterfaceField.FTYPE_OUT,
			      BlockInterfaceField.FTYPE_INOUT,
			      BlockInterfaceField.FTYPE_STAT,
			      BlockInterfaceField.FTYPE_TEMP):
			try:
				fields = self.interfaceFields[ftype]
			except KeyError:
				continue
			for field in fields:
				if ftype == BlockInterfaceField.FTYPE_IN:
					self.interface.addField_IN(field)
				elif ftype == BlockInterfaceField.FTYPE_OUT:
					self.interface.addField_OUT(field)
				elif ftype == BlockInterfaceField.FTYPE_INOUT:
					self.interface.addField_INOUT(field)
				elif ftype == BlockInterfaceField.FTYPE_STAT:
					self.interface.addField_STAT(field)
				elif ftype == BlockInterfaceField.FTYPE_TEMP:
					self.interface.addField_TEMP(field)
				else:
					assert(0)

class OB(CodeBlock): #+cdef

	BLOCKTYPESTR	= "OB"
	_isOB = True

	def __init__(self, insns, index):
		CodeBlock.__init__(self, insns, index, OBInterface())

		self.lstack = LStackAllocator(0)

	def getBlockInfo(self):
		"""Get a BlockInfo instance for this block.
		"""
		return BlockInfo(blockType = BlockInfo.TYPE_OB,
				 blockIndex = self.index,
				 identHash = self.identHash)

class FB(CodeBlock): #+cdef

	BLOCKTYPESTR	= "FB"
	_isFB = True

	def __init__(self, insns, index):
		CodeBlock.__init__(self, insns, index, FBInterface())

	def getBlockInfo(self):
		"""Get a BlockInfo instance for this block.
		"""
		return BlockInfo(blockType = BlockInfo.TYPE_FB,
				 blockIndex = self.index,
				 identHash = self.identHash)

class FC(CodeBlock): #+cdef

	BLOCKTYPESTR	= "FC"
	_isFC = True

	def __init__(self, insns, index):
		CodeBlock.__init__(self, insns, index, FCInterface())

	def getBlockInfo(self):
		"""Get a BlockInfo instance for this block.
		"""
		return BlockInfo(blockType = BlockInfo.TYPE_FC,
				 blockIndex = self.index,
				 identHash = self.identHash)
bues.ch cgit interface