annotate python/ppci/layout.py @ 382:0c44e494ef58

Made lexer more generic
author Windel Bouwman
date Sun, 27 Apr 2014 12:24:21 +0200
parents
children 173e20a47fda
rev   line source
382
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
1
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
2 class Layout:
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
3 def __init__(self):
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
4 self.mems = []
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
5
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
6 def __eq__(self, other):
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
7 return self.mems == other.mems
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
8
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
9
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
10 class Memory:
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
11 def __init__(self, address=0x0):
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
12 self.inputs = []
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
13 self.address = address
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
14 self.size = 0x0
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
15
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
16 def add_input(self, inp):
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
17 assert isinstance(inp, Input)
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
18 self.inputs.append(inp)
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
19
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
20
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
21 class Input:
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
22 pass
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
23
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
24
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
25 class SectionInput(Input):
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
26 def __init__(self, section_name):
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
27 self.section_name = section_name
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
28
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
29
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
30 def load_layout(f):
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
31 return deserialize(json.load(f))
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
32
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
33
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
34 def make_int(txt):
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
35 if txt.startswith('0x'):
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
36 return int(txt[2:], 16)
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
37 else:
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
38 return int(txt)
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
39
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
40
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
41 class LayoutParser:
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
42 def __init__(self):
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
43 toks = ['ID', '{', '}', 'MEMORY', 'ALIGN', '.', pyyacc.EPS, pyyacc.EOF]
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
44 g = pyyacc.Grammar(toks)
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
45 g.add_production('layout', ['MEMORY', '{', 'input_list', '}'])
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
46 g.add_production('input_list', ['MEMORY', '{', 'input_list', '}'])
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
47
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
48
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
49 def deserialize(d):
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
50 layout = Layout()
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
51 for mem_node in d['memories']:
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
52 m = Memory()
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
53 m.address = make_int(mem_node['address'])
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
54 m.size = make_int(mem_node['size'])
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
55 for input_node in mem_node['inputs']:
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
56 pass
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
57 return layout
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
58