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
|
# -*- coding: utf-8 -*-
#
# AWL simulator - FUP compiler - Block declaration
#
# Copyright 2016-2017 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.namevalidation import *
from awlsim.common.xmlfactory import *
from awlsim.common.project import *
from awlsim.common.version import *
from awlsim.common.cpuconfig import *
from awlsim.fupcompiler.base import *
class FupCompiler_BlockDeclFactory(XmlFactory):
def parser_open(self, tag=None):
self.inInstanceDBs = False
self.inDB = False
if tag:
self.decl.setBlockType(tag.getAttr("type", "FC"))
self.decl.setBlockName(tag.getAttr("name", "FC 1"))
XmlFactory.parser_open(self, tag)
def parser_beginTag(self, tag):
if self.inInstanceDBs:
if tag.name == "db":
self.decl.addInstanceDB(tag.getAttr("name", ""))
self.inDB = True
return
else:
if tag.name == "instance_dbs":
self.inInstanceDBs = True
return
XmlFactory.parser_beginTag(self, tag)
def parser_endTag(self, tag):
if self.inInstanceDBs:
if self.inDB:
if tag.name == "db":
self.inDB = False
return
else:
if tag.name == "instance_dbs":
self.inInstanceDBs = False
return
else:
if tag.name == "blockdecl":
self.parser_finish()
return
XmlFactory.parser_endTag(self, tag)
class FupCompiler_BlockDecl(FupCompiler_BaseObj):
factory = FupCompiler_BlockDeclFactory
noPreprocessing = True
def __init__(self, compiler):
FupCompiler_BaseObj.__init__(self)
self.compiler = compiler # FupCompiler
self.setBlockType("FC")
self.setBlockName("FC 1")
self.instanceDBs = []
def setBlockType(self, blockType):
self.blockType = blockType.upper().strip()
if self.blockType not in {"FC", "FB", "OB"}:
raise FupDeclError("Invalid block type: %s" % (
self.blockType),
self)
def setBlockName(self, blockName):
blockName = blockName.strip()
if not blockName:
return
self.blockName = blockName
def addInstanceDB(self, dbName):
dbName = dbName.strip()
if not dbName:
return
if len(dbName) > 3 and\
dbName.upper().startswith("DI") and\
dbName[2].isspace():
dbName = "DB" + dbName[2:]
self.instanceDBs.append(dbName)
def compile(self, interf):
"""Compile this FUP block declaration to AWL.
interf => FupCompiler_Interf
Returns a tuple: (list of AWL header lines,
list of AWL footer lines,
list of AWL lines for instance DBs).
"""
self.compileState = self.COMPILE_RUNNING
blockHeader = []
blockFooter = []
instDBs = []
mnemonics = self.compiler.mnemonics
srcName = AwlName.stripChars(self.compiler.fupSource.name,
replaceWith="_",
stripAlpha=False,
stripNum=False, stripSpace=False)
strAwl = "AWL" if mnemonics == S7CPUConfig.MNEMONICS_DE else "STL"
strFup = "FUP" if mnemonics == S7CPUConfig.MNEMONICS_DE else "FBD"
strMnemonics = "DE (German)" if mnemonics == S7CPUConfig.MNEMONICS_DE\
else "EN (international)"
def mkIntro(lines, blockTypeDecl):
optimizers = []
for i, grid in enumerate(self.compiler.grids):
if self.compiler.optimizerSettingsContainer is None:
optSettCont = grid.optimizerSettingsContainer
else:
optSettCont = self.compiler.optimizerSettingsContainer
optsStr = str(optSettCont)
if not optsStr:
optsStr = "disabled"
optimizers.append("grid-%d: %s" % (i + 1, optsStr))
lines.append("// ")
lines.append("// %s generated by Awlsim %s compiler" % (
blockTypeDecl, strFup))
lines.append("// ")
lines.append("// Source : %s" % srcName)
lines.append("// Compiler version : %s" % VERSION_STRING)
lines.append("// %s mnemonics : %s" % (strAwl, strMnemonics))
lines.append("// Optimizers : %s" % "; ".join(optimizers))
lines.append("// ")
def mkHdrInfo(lines):
lines.append("\tTITLE = %s" % srcName)
lines.append("\tFAMILY : %s" % strFup)
lines.append("\tVERSION : %s" % VERSION_STRING)
if self.blockType == "FC":
if not interf.retValField:
raise FupDeclError("RET_VAL is not defined for %s." % (
self.blockName),
self)
retVal = interf.retValField.typeStr
if not AwlName.mayBeValidType(retVal):
raise FupDeclError("RET_VAL data type contains "
"invalid characters.",
self)
mkIntro(blockHeader, "FUNCTION")
blockHeader.append("FUNCTION %s : %s" %(
self.blockName, retVal))
mkHdrInfo(blockHeader)
blockFooter.append("END_FUNCTION")
elif self.blockType == "FB":
mkIntro(blockHeader, "FUNCTION_BLOCK")
blockHeader.append("FUNCTION_BLOCK %s" %\
self.blockName)
mkHdrInfo(blockHeader)
blockFooter.append("END_FUNCTION_BLOCK")
elif self.blockType == "OB":
mkIntro(blockHeader, "ORGANIZATION_BLOCK")
blockHeader.append("ORGANIZATION_BLOCK %s" %\
self.blockName)
mkHdrInfo(blockHeader)
blockFooter.append("END_ORGANIZATION_BLOCK")
else:
raise FupDeclError("Unknown block type: %s" % (
self.blockType),
self)
for dbName in self.instanceDBs:
instDBs.append("")
instDBs.append("")
mkIntro(instDBs, "Instance DATA_BLOCK")
instDBs.append("DATA_BLOCK %s" % dbName)
mkHdrInfo(instDBs)
instDBs.append("\t%s" % self.blockName)
instDBs.append("BEGIN")
for field in interf.allFields:
fieldName = field.name
initValueStr = field.initValueStr.strip()
comment = field.comment
if not initValueStr:
continue
if not AwlName.isValidVarName(fieldName):
raise FupDeclError("Variable name "
"'%s' contains invalid characters." % (
fieldName),
self)
if not AwlName.mayBeValidValue(initValueStr):
raise FupDeclError("Variable value "
"'%s' contains invalid characters." % (
initValueStr),
self)
if not AwlName.isValidComment(comment):
raise FupDeclError("Comment "
"'%s' contains invalid characters." % (
comment),
self)
instDBs.append("\t%s := %s;%s" %(
fieldName, initValueStr,
(" // " + comment) if comment else ""))
instDBs.append("END_DATA_BLOCK")
self.compileState = self.COMPILE_DONE
return blockHeader, blockFooter, instDBs
def generateCallTemplate(self):
"""Generate template AWL code for a CALL operation
to this block.
Returns a list of AWL lines.
"""
awlLines = []
if self.blockType == "FC":
awlLines.append("\tCALL %s" % self.blockName)
elif self.blockType == "FB":
awlLines.append("\tCALL %s, DB ..." % self.blockName)
else:
raise FupDeclError("Cannot generate "
"CALL to %s block." % (
self.blockType),
self)
return awlLines
def __repr__(self):
return "FupCompiler_BlockDecl(compiler)"
def __str__(self):
return "FUP-block-declaration"
|