annotate python/ppci/layout.py @ 383:173e20a47fda

Added linker description loader
author Windel Bouwman
date Sun, 27 Apr 2014 17:40:39 +0200
parents 0c44e494ef58
children d056b552d3f4
rev   line source
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
1 from baselex import BaseLexer
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
2 import pyyacc
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
3 from . import make_num
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
4
382
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
5
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
6 class Layout:
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
7 def __init__(self):
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
8 self.mems = []
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
9
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
10 def add_memory(self, memory):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
11 self.mems.append(memory)
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
12
382
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
13 def __eq__(self, other):
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
14 return self.mems == other.mems
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
15
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
16 def __repr__(self):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
17 return str(self.mems)
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
18
382
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
19
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
20 class Memory:
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
21 def __init__(self, name):
382
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
22 self.inputs = []
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
23 self.name = name
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
24 self.location = 0x0
382
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
25 self.size = 0x0
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
26
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
27 def add_input(self, inp):
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
28 assert isinstance(inp, Input)
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
29 self.inputs.append(inp)
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
30
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
31 def __repr__(self):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
32 return 'MEM {} loc={:08X} size={:08X}'.format(self.name, self.location, self.size) + str(self.inputs)
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
33
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
34 def __eq__(self, other):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
35 return str(self) == str(other)
382
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
36
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
37 class Input:
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
38 pass
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
39
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
40
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
41 class Section(Input):
382
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
42 def __init__(self, section_name):
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
43 self.section_name = section_name
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
44
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
45 def __repr__(self):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
46 return 'Section({})'.format(self.section_name)
382
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
47
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
48
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
49 class Align(Input):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
50 def __init__(self, alignment):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
51 self.alignment = alignment
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
52
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
53 def __repr__(self):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
54 return 'Align({})'.format(self.alignment)
382
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
55
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
56
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
57 class LayoutLexer(BaseLexer):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
58 def __init__(self):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
59 tok_spec = [
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
60 ('HEXNUMBER', r'0x[\da-fA-F]+', self.handle_number),
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
61 ('NUMBER', r'\d+', self.handle_number),
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
62 ('ID', r'[A-Za-z][A-Za-z\d_]*', self.handle_id),
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
63 ('SKIP', r'[ \t\r\n]', None),
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
64 ('LEESTEKEN', r':=|[\.,=:\-+*\[\]/\(\)]|>=|<=|<>|>|<|}|{', lambda typ, val: (val, val)),
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
65 ('STRING', r"'.*?'", lambda typ, val: (typ, val[1:-1])),
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
66 ]
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
67 super().__init__(tok_spec)
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
68 self.kws = ['MEMORY', 'ALIGN', 'LOCATION','SECTION','SIZE']
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
69
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
70 def handle_id(self, typ, val):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
71 if val in self.kws:
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
72 typ = val
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
73 return typ, val
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
74
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
75 def handle_number(self, typ, val):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
76 val = make_num(val)
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
77 typ = 'NUMBER'
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
78 return typ, val
382
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
79
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
80
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
81 class LayoutParser:
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
82 def __init__(self, kws):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
83 toks = ['ID', 'NUMBER', '{', '}', '.', ':', '=', '(', ')', pyyacc.EPS, pyyacc.EOF] + kws
382
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
84 g = pyyacc.Grammar(toks)
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
85 g.add_production('layout', ['mem_list'])
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
86 g.add_one_or_more('mem', 'mem_list')
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
87 g.add_production('mem', ['MEMORY', 'ID', 'LOCATION', '=', 'NUMBER', 'SIZE', '=', 'NUMBER', '{', 'input_list', '}'], self.handle_mem)
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
88 g.add_one_or_more('input', 'input_list')
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
89 g.add_production('input', ['ALIGN', '(', 'NUMBER', ')'], self.handle_align)
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
90 g.add_production('input', ['SECTION', '(', 'ID', ')'], self.handle_section)
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
91
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
92 g.start_symbol = 'layout'
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
93 self.p = g.generate_parser()
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
94
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
95 def parse(self, lexer, layout):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
96 self.layout = layout
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
97 self.p.parse(lexer)
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
98
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
99 def handle_mem(self, mem_tag, mem_name, loc_tag, eq1, loc, size_tag, eq2, size, lbrace, inps, rbrace):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
100 m = Memory(mem_name.val)
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
101 m.size = size.val
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
102 m.location = loc.val
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
103 for inp in inps:
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
104 m.add_input(inp)
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
105 self.layout.add_memory(m)
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
106
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
107 def handle_align(self, align_tag, lbrace, alignment, rbrace):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
108 return Align(alignment.val)
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
109
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
110 def handle_section(self, section_tag, lbrace, section_name, rbrace):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
111 return Section(section_name.val)
382
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
112
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
113
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
114 class LayoutLoader:
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
115 def __init__(self):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
116 self.lexer = LayoutLexer()
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
117 self.parser = LayoutParser(self.lexer.kws)
382
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
118
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
119 def load_layout(self, f):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
120 layout = Layout()
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
121 self.lexer.feed(f.read()) # TODO: perhaps the read is better in the lexer?
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
122 self.parser.parse(self.lexer, layout)
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
123 return layout
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
124
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
125 # Single definition:
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
126 _lloader = LayoutLoader()
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
127
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
128
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
129 def load_layout(f):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
130 return _lloader.load_layout(f)
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
131