35 cpp_header =
"""/* src/vm/jit/compiler2/{filename} - {file_desc}
38 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
40 This file is part of CACAO.
42 This program is free software; you can redistribute it and/or
43 modify it under the terms of the GNU General Public License as
44 published by the Free Software Foundation; either version 2, or (at
45 your option) any later version.
47 This program is distributed in the hope that it will be useful, but
48 WITHOUT ANY WARRANTY; without even the implied warranty of
49 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
50 General Public License for more details.
52 You should have received a copy of the GNU General Public License
53 along with this program; if not, write to the Free Software
54 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
61 cpp_generation_disclaimer =
"""/*
62 WARNING: THIS FILE IS AUTO-GENERATED! DO NOT ALTER!
63 Instead have a look at the generator ({generator})
64 and the input file ({input_file}).
71 * These are local overrides for various environment variables in Emacs.
72 * Please do not remove this and leave it at the end of the file, where
73 * Emacs will automagically detect them.
74 * ---------------------------------------------------------------------
81 * vim:noexpandtab:sw=4:ts=4:
89 elif 'l' == conversion:
92 return super(Formatter, self).
convert_field(value, conversion)
95 def __init__(self,filename,file_desc, line, header='',footer=''):
96 self.
file = open(filename,
'w')
105 except KeyError
as e:
106 print 'Unknown template key: ', e
112 except KeyError
as e:
113 print 'Unknown template key: ', e
119 except KeyError
as e:
120 print 'Unknown template key: ', e
124 default_file =
'./instruction_table.csv'
125 comment_pattern = re.compile(
r'^[^#]*')
127 entry_pattern = re.compile(
r'(?P<name>\w+)')
130 parser = argparse.ArgumentParser(description=
'Generate instruction table includes.')
131 parser.add_argument(
'table', nargs=
'?', type=file,
132 help=
'instruction table file [default='+default_file+
']', default=default_file)
134 args = parser.parse_args()
140 'generator' : sys.argv[0],
141 'input_file': args.table.name,
149 for line
in args.table:
152 sline = comment_pattern.match(line).group(0)
153 sline = sline.strip()
156 entry = entry_pattern.match(sline)
158 print 'Error in line: ' + str(line_nr) +
' :' + line.strip()
161 entry = entry.groupdict()
164 t.print_line(dict(keys.items() + entry.items()))
171 default_file =
'./instruction_table.csv'
172 comment_pattern = re.compile(
r'^[^#]*')
174 entry_pattern = re.compile(
r'(?P<name>\w+)(,(?P<ops>\d+))?')
177 parser = argparse.ArgumentParser(description=
'Generate instruction table includes.')
178 parser.add_argument(
'table', nargs=
'?', type=file,
179 help=
'instruction table file [default='+default_file+
']', default=default_file)
181 args = parser.parse_args()
187 'generator' : sys.argv[0],
188 'input_file': args.table.name
196 ex_header = cpp_header + cpp_generation_disclaimer
198 ex_header = ex_header.replace(
"{filename}",
"GrammarExcludedNodes.inc")
199 ex_header = ex_header.replace(
"{file_desc}",
"Instructions that are excluded from pattern matching")
200 ex_header = ex_header.replace(
"{generator}",
"src/vm/jit/compiler2/instruction_gen.py and contrib/patternmatching/")
201 ex_header = ex_header.replace(
"{input_file}",
"instruction_table.csv")
203 excludednodes = open(
'GrammarExcludedNodes.inc',
'w')
205 excludednodes.write(ex_header)
209 for line
in args.table:
212 sline = comment_pattern.match(line).group(0)
213 sline = sline.strip()
216 entry = entry_pattern.match(sline)
218 print 'Error in line: ' + str(line_nr) +
' :' + line.strip()
221 entry = entry.groupdict()
223 entry[
'enumid'] = enumid
225 bigdict[enumid] = entry
227 excludes.append(entry)
231 for i
in range(0, len(excludes)):
233 excludednodes.write(
'Instruction::' + e[
'name'] +
'ID')
234 if len(excludes)-1 > i:
235 excludednodes.write(
',')
236 excludednodes.write(
'\n')
238 excludednodes.write(cpp_footer)
241 noinstentry = {
'enumid' : enumid,
'ops' : 0,
'name' :
'NoInst'}
242 bigdict[enumid] = noinstentry
245 brgfile = open(
'GrammarBase.brg',
'w')
247 gram_header = cpp_header + cpp_generation_disclaimer
249 gram_header = gram_header.replace(
"{filename}",
"GrammarBase.brg")
250 gram_header = gram_header.replace(
"{file_desc}",
"Generated Basic Grammar")
251 gram_header = gram_header.replace(
"{generator}",
"src/vm/jit/compiler2/instruction_gen.py and contrib/patternmatching/")
252 gram_header = gram_header.replace(
"{input_file}",
"instruction_table.csv")
254 brgfile.write(
'%{\n' + gram_header +
'%}\n%start stm\n')
256 for op
in bigdict.itervalues():
257 brgfile.write(
'%term ' + op[
'name'] +
'ID = ' + str(op[
'enumid']) +
'\n')
259 brgfile.write(
'%%\n');
261 for op
in bigdict.itervalues():
262 brgfile.write(
'stm: ' + op[
'name'] +
'ID')
263 if int(op[
'ops']) > 0:
265 for numops
in range (0, int(op[
'ops'])):
267 if (numops < int(op[
'ops']) - 1):
271 brgfile.write(
' "' + op[
'name'] +
'ID" 1 \n')
274 if __name__ ==
'__main__':
276 Template(
'InstructionDeclGen.inc',
'Instruction Declarations',
'class {name};\n',
277 cpp_header+cpp_generation_disclaimer,cpp_footer),
278 Template(
'InstructionIDGen.inc',
'Instruction IDs',
'{name}ID,\n',
279 cpp_header+cpp_generation_disclaimer,cpp_footer),
280 Template(
'InstructionToInstGen.inc',
'Instruction conversion methods',
281 'virtual {name}* to_{name}() {{ return NULL; }}\n',
282 cpp_header+cpp_generation_disclaimer,cpp_footer),
283 Template(
'InstructionNameSwitchGen.inc',
'Instruction name switch',
284 'case {name}ID: return "{name}";\n',
285 cpp_header+cpp_generation_disclaimer,cpp_footer),
286 Template(
'InstructionVisitorGen.inc',
'Instruction Visitor',
287 'virtual void visit({name}* I, bool copyOperands);\n',
288 cpp_header+cpp_generation_disclaimer,cpp_footer),
289 Template(
'InstructionVisitorImplGen.inc',
'Instruction Visitor',
290 'void InstructionVisitor::visit({name}* I, bool copyOperands) {{visit_default(I);}}\n',
291 cpp_header+cpp_generation_disclaimer,cpp_footer),