-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgen_from_tables.py
executable file
·80 lines (66 loc) · 2.27 KB
/
gen_from_tables.py
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
#! /usr/bin/env python2
# -*- coding: utf-8 -*-
"""
gen_from_tables.py
Generate a Python instruction table from the one provided in DSPTables.cpp
(part of the Dolphin project).
Copyright (C) 2011 Pierre Bourdon <[email protected]>
Licensed under the GPLv2 license, see the LICENSE file at the root of this
repository.
"""
import ast
import sys
class Modes: # enumeration
SKIP = 0
OPCODES = 1
OPCODES_EXT = 2
def full_strip(line):
"""Strips comments and trailing whitespaces from a line."""
comment_start = line.find('//')
if comment_start != -1:
line = line[:comment_start]
return line.strip()
def gen_from_text(text):
"""Generates the Python code from the file text."""
lines = [full_strip(l) for l in text.split('\n')]
mode = Modes.SKIP
opcodes = []
opcodes_ext = []
for line in lines:
if mode == Modes.SKIP:
if line == 'const DSPOPCTemplate opcodes[] =':
mode = Modes.OPCODES
elif line == 'const DSPOPCTemplate opcodes_ext[] =':
mode = Modes.OPCODES_EXT
else:
if line == '};':
mode = Modes.SKIP
elif line == '{' or not line:
continue
else:
fields = line.replace('{', '[').replace('}', ']').split(',')
fields = fields[0:3] + fields[5:-5] + [fields[-4]]
fields = map(str.strip, fields)
fields = ','.join(fields) + ']'
fields = fields.replace('true', 'True')
fields = fields.replace('false', 'False')
fields = fields.replace('P_', 'OpType.')
if mode == Modes.OPCODES:
opcodes.append(fields)
else:
opcodes_ext.append(fields)
print 'opcodes = ['
print ' ' + ',\n '.join(opcodes)
print ']'
print
print 'opcodes_ext = ['
print ' ' + ',\n '.join(opcodes_ext)
print ']'
if __name__ == '__main__':
if len(sys.argv) != 2:
print 'usage: %s /path/to/DSPTables.cpp' % sys.argv[0]
sys.exit(1)
text = open(sys.argv[1]).read()
print "#### THIS CODE WAS AUTO-GENERATED BY gen_from_tables.py ####"
gen_from_text(text)
print "#### END AUTO-GENERATED CODE ####"