aboutsummaryrefslogtreecommitdiffstats
path: root/awlsim/core/lstack.py
blob: 20844d6695938acc2c52cb6fa8cee5f8453e5d81 (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
# -*- coding: utf-8 -*-
#
# AWL simulator - L-stack handling
#
# Copyright 2014-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.exceptions import *
from awlsim.common.datatypehelpers import * #+cimport
from awlsim.common.util import *

from awlsim.core.datatypes import *
from awlsim.core.memory import * #+cimport
from awlsim.core.offset import * #+cimport

#from libc.stdlib cimport abort #@cy
#from cpython.mem cimport PyMem_Malloc, PyMem_Free #@cy


class LStackFrame(object):		#@nocy
	offset = None			#@nocy
	allocBits = 0			#@nocy
	prevFrame = None		#@nocy

class LStackAllocator(object): #+cdef
	"""Memory allocator for the L-stack.
	This class manages the L-stack of one OB instance.
	"""

	__frameFreeSet = set() #@nocy

#@cy	def __dealloc__(self):
#@cy		cdef LStackFrame *frame
#@cy		cdef LStackFrame *oldFrame
#@cy		frame = self.topFrame
#@cy		while frame:
#@cy			oldFrame = frame
#@cy			frame = frame.prevFrame
#@cy			PyMem_Free(oldFrame)

	def __init__(self, maxSize): #@nocy
#@cy	def __init__(self, uint32_t maxSize):
		# maxSize -> max size of the L-stack, in bytes.

		self.topFrame = None #+NoneToNULL
		self.resize(maxSize)

	# Reset all allocations on the L-stack.
	# Sets the maximum possible allocation to maxAllocBytes.
	# maxAllocBytes must be smaller or equal to self.memory size.
	def resize(self, maxAllocBytes): #@nocy
#@cy	cdef resize(self, uint32_t maxAllocBytes):
		self.memory = AwlMemory(maxAllocBytes)
		self.maxAllocBits = maxAllocBytes * 8

	def reset(self): #@nocy
#@cy	cdef void reset(self):
#@cy		cdef LStackFrame *frame
#@cy		cdef LStackFrame *oldFrame

		self.globAllocBits = 0

		frame = self.topFrame
		while frame:
			oldFrame = frame
			self.topFrame = frame = frame.prevFrame

			oldFrame.prevFrame = None		#+NoneToNULL
			self.__frameFreeSet.add(oldFrame)	#@nocy
#@cy			PyMem_Free(oldFrame)

	def enterStackFrame(self): #@nocy
#@cy	cdef void enterStackFrame(self):
#@cy		cdef LStackFrame *frame
#@cy		cdef uint32_t globAllocBits
#@cy		cdef uint32_t prevAllocBits

		prevAllocBits = self.globAllocBits
		self.globAllocBits = globAllocBits = (((prevAllocBits + 7) >> 3) << 3)	#+suffix-u
		globAllocBytes = globAllocBits // 8					#+suffix-u

		# Allocate a new stack frame.
		try:						#@nocy
			frame = self.__frameFreeSet.pop()	#@nocy
		except KeyError:				#@nocy
			frame = LStackFrame()			#@nocy
#@cy		frame = <LStackFrame *>PyMem_Malloc(sizeof(LStackFrame))
#@cy		if frame == NULL:
#@cy			printError("enterStackFrame: Out of memory")
#@cy			abort()

		frame.byteOffset = globAllocBytes
		# Account the rounded-up bits to the new frame.
		frame.allocBits = globAllocBits - prevAllocBits

		frame.prevFrame = self.topFrame
		self.topFrame = frame
		self.topFrameOffset = make_AwlOffset(globAllocBytes, 0)

	def exitStackFrame(self): #@nocy
#@cy	cdef void exitStackFrame(self):
#@cy		cdef LStackFrame *frame
#@cy		cdef LStackFrame *topFrame
#@cy		cdef uint32_t globAllocBits

		frame = self.topFrame
		topFrame = self.topFrame = frame.prevFrame

		self.globAllocBits = globAllocBits = self.globAllocBits - frame.allocBits
		if topFrame:
			self.topFrameOffset = make_AwlOffset_fromLongBitOffset(
					globAllocBits - topFrame.allocBits)
		else:
			self.topFrameOffset = make_AwlOffset(0, 0)

		frame.prevFrame = None		#+NoneToNULL
		self.__frameFreeSet.add(frame)	#@nocy
#@cy		PyMem_Free(frame)

	# Allocate a number of bits on the L-stack.
	# Returns an AwlOffset as the offset the bits are allocated on.
	def alloc(self, nrBits): #@nocy
#@cy	cdef AwlOffset alloc(self, uint32_t nrBits):
#@cy		cdef LStackFrame *frame
#@cy		cdef uint32_t globAllocBits
#@cy		cdef uint32_t roundBits
#@cy		cdef AwlOffset offset

		frame = self.topFrame
		globAllocBits = self.globAllocBits

		# Calculate the number of bits to actually allocate and
		# calculate the AwlOffset to this allocation.
		roundBits = 0
		if nrBits == 1:
			# Bit-aligned allocation
			offset = make_AwlOffset(globAllocBits // 8 - frame.byteOffset,	#+suffix-u
						globAllocBits % 8)			#+suffix-u
		else:
			# Byte-aligned allocation
			if globAllocBits & 7:						#+suffix-u
				# Round up to the next byte boundary.
				roundBits = 8 - (globAllocBits & 7)			#+suffix-u
				globAllocBits += roundBits
			offset = make_AwlOffset(globAllocBits // 8 - frame.byteOffset,	#+suffix-u
						0)
		globAllocBits += nrBits

		if (((globAllocBits + 7) >> 3) << 3) >= self.maxAllocBits:		#+suffix-u
			raise AwlSimError(
				"Cannot allocate another %d+%d bits on the L-stack. "
				"The L-stack is exhausted. Maximum size = %d bits." % (
				nrBits, roundBits, self.maxAllocBits))

		# Actually allocate the bits.
		self.globAllocBits = globAllocBits
		frame.allocBits += nrBits + roundBits

		return offset
bues.ch cgit interface