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
|
# -*- coding: utf-8 -*-
#
# AWL simulator - FUP compiler - Element
#
# Copyright 2016 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.compat import *
from awlsim.common.xmlfactory import *
from awlsim.common.util import *
from awlsim.fupcompiler.fupcompiler_base import *
from awlsim.fupcompiler.fupcompiler_conn import *
#from awlsim.core.instructions.all_insns cimport * #@cy
from awlsim.core.instructions.all_insns import * #@nocy
from awlsim.core.optrans import *
class FupCompiler_ElemFactory(XmlFactory):
def parser_open(self, tag=None):
self.inElem = False
self.elem = None
XmlFactory.parser_open(self, tag)
def parser_beginTag(self, tag):
if self.inElem:
if self.elem and tag.name == "connections":
self.parser_switchTo(FupCompiler_Conn.factory(elem=self.elem))
return
else:
if tag.name == "element":
self.inElem = True
x = tag.getAttrInt("x")
y = tag.getAttrInt("y")
elemType = tag.getAttr("type")
subType = tag.getAttr("subtype", None)
content = tag.getAttr("content", None)
self.elem = FupCompiler_Elem.parse(self.grid,
x, y, elemType, subType, content)
if not self.elem:
raise self.Error("Failed to parse element")
return
XmlFactory.parser_beginTag(self, tag)
def parser_endTag(self, tag):
if self.inElem:
if tag.name == "element":
if self.elem:
self.grid.addElem(self.elem)
self.inElem = False
self.elem = None
return
else:
if tag.name == "elements":
self.parser_finish()
return
XmlFactory.parser_endTag(self, tag)
class FupCompiler_Elem(FupCompiler_BaseObj):
factory = FupCompiler_ElemFactory
EnumGen.start
TYPE_BOOLEAN = EnumGen.item
TYPE_OPERAND = EnumGen.item
EnumGen.end
EnumGen.start
SUBTYPE_AND = EnumGen.item
SUBTYPE_OR = EnumGen.item
SUBTYPE_XOR = EnumGen.item
SUBTYPE_LOAD = EnumGen.item
SUBTYPE_ASSIGN = EnumGen.item
EnumGen.end
str2type = {
"boolean" : TYPE_BOOLEAN,
"operand" : TYPE_OPERAND,
}
str2subtype = {
"and" : SUBTYPE_AND,
"or" : SUBTYPE_OR,
"xor" : SUBTYPE_XOR,
"load" : SUBTYPE_LOAD,
"assign" : SUBTYPE_ASSIGN,
}
@classmethod
def sorted(cls, elemList):
"""Sort all elements from elemList in ascending order by Y position.
The Y position in the diagram is the basic evaluation order.
Also sort by X position as a secondary key.
The sorted list is returned.
"""
if not elemList:
return []
yShift = max(e.x for e in elemList).bit_length()
return sorted(elemList,
key=lambda e: (e.y << yShift) + e.x)
@classmethod
def parse(cls, grid, x, y, elemType, subType, content):
try:
elemType = cls.str2type[elemType]
if subType:
subType = cls.str2subtype[subType]
else:
subType = None
content = content or None
except KeyError:
return None
return cls(grid, x, y, elemType, subType, content)
def __init__(self, grid, x, y, elemType, subType, content):
FupCompiler_BaseObj.__init__(self)
self.grid = grid # FupCompiler_Grid
self.x = x # X coordinate
self.y = y # Y coordinate
self.elemType = elemType # TYPE_...
self.subType = subType # SUBTYPE_... or None
self.content = content # content string or None
self.connections = set() # FupCompiler_Conn
def addConn(self, conn):
self.connections.add(conn)
return True
def __compile_OPERAND_ASSIGN(self):
opTrans = self.grid.compiler.opTrans
insns = []
# Only one connection allowed per ASSIGN.
if len(self.connections) != 1:
raise AwlSimError("FUP ASSIGN: Invalid number of "
"connections in '%s'." % (
str(self)))
# The connection must be input.
conn = getany(self.connections)
if not conn.dirIn or conn.dirOut or conn.pos != 0:
raise AwlSimError("FUP ASSIGN: Invalid connection "
"properties in '%s'." % (
str(self)))
# Compile the element connected to the input.
connOut = [ c for c in conn.getConnected() if c.dirOut ]
if len(connOut) != 1:
raise AwlSimError("FUP ASSIGN: Multiple outbound signals "
"connected to '%s'." % (
str(self)))
insns.extend(connOut[0].elem.compile())
# Create the ASSIGN instruction.
opDesc = opTrans.translateFromString(self.content)
insns.append(AwlInsn_ASSIGN(cpu=None, ops=[opDesc.operator]))
# Compile additional assign operators.
# This is an optimization to avoid additional compilations
# of the whole tree. We just assign the VKE once again.
otherElems = [ c.elem for c in conn.getConnected() if c.dirIn ]
for otherElem in self.sorted(otherElems):
if otherElem.elemType == self.TYPE_OPERAND and\
otherElem.subType == self.SUBTYPE_ASSIGN:
otherElem.compileState = self.COMPILE_RUNNING
opDesc = opTrans.translateFromString(otherElem.content)
insns.append(AwlInsn_ASSIGN(cpu=None, ops=[opDesc.operator]))
otherElem.compileState = self.COMPILE_DONE
return insns
__operandTable = {
SUBTYPE_ASSIGN : __compile_OPERAND_ASSIGN,
}
def __compile_OPERAND(self):
try:
handler = self.__operandTable[self.subType]
except KeyError:
raise AwlSimError("FUP compiler: Unknown element "
"subtype OPERAND/%d" % self.subType)
return handler(self)
def __compile_BOOLEAN_generic(self, insnClass, insnBranchClass):
opTrans = self.grid.compiler.opTrans
insns = []
for conn in self.connections:
if not conn.dirIn:
continue
for otherConn in conn.getConnected():
if not otherConn.dirOut:
continue
otherElem = otherConn.elem
if otherElem.elemType == self.TYPE_OPERAND and\
otherElem.subType == self.SUBTYPE_LOAD:
otherElem.compileState = self.COMPILE_RUNNING
opDesc = opTrans.translateFromString(otherElem.content)
insns.append(insnClass(cpu=None, ops=[opDesc.operator]))
otherElem.compileState = self.COMPILE_DONE
elif otherElem.elemType == self.TYPE_BOOLEAN:
insns.append(insnBranchClass(cpu=None))
insns.extend(otherElem.compile())
insns.append(AwlInsn_BEND(cpu=None))
else:
raise AwlSimError("FUP compiler: Invalid "
"element '%s' connected to '%s'." % (
str(otherElem), str(self)))
return insns
def __compile_BOOLEAN_AND(self):
return self.__compile_BOOLEAN_generic(AwlInsn_U, AwlInsn_UB)
def __compile_BOOLEAN_OR(self):
return self.__compile_BOOLEAN_generic(AwlInsn_O, AwlInsn_OB)
def __compile_BOOLEAN_XOR(self):
return self.__compile_BOOLEAN_generic(AwlInsn_X, AwlInsn_XB)
__booleanTable = {
SUBTYPE_AND : __compile_BOOLEAN_AND,
SUBTYPE_OR : __compile_BOOLEAN_OR,
SUBTYPE_XOR : __compile_BOOLEAN_XOR,
}
def __compile_BOOLEAN(self):
try:
handler = self.__booleanTable[self.subType]
except KeyError:
raise AwlSimError("FUP compiler: Unknown element "
"subtype BOOLEAN/%d" % self.subType)
return handler(self)
__typeTable = {
TYPE_OPERAND : __compile_OPERAND,
TYPE_BOOLEAN : __compile_BOOLEAN,
}
def compile(self):
if self.compileState == self.COMPILE_DONE:
return []
self.compileState = self.COMPILE_RUNNING
try:
handler = self.__typeTable[self.elemType]
except KeyError:
raise AwlSimError("FUP compiler: Unknown element "
"type %d" % self.elemType)
result = handler(self)
self.compileState = self.COMPILE_DONE
return result
def __repr__(self):
return "FupCompiler_Elem(grid, x=%d, y=%d, elemType=%d, "\
"subType=%d, content=%s)" % (
self.x, self.y, self.elemType, self.subType, self.content)
|