annotate python/ppci/layout.py @ 391:a139da1f44f6

Merge
author Windel Bouwman
date Fri, 16 May 2014 12:30:10 +0200
parents 2a970e7270e2
children
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):
385
d056b552d3f4 Made better use of layout
Windel Bouwman
parents: 383
diff changeset
8 self.memories = []
382
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):
385
d056b552d3f4 Made better use of layout
Windel Bouwman
parents: 383
diff changeset
11 self.memories.append(memory)
383
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):
385
d056b552d3f4 Made better use of layout
Windel Bouwman
parents: 383
diff changeset
14 return self.memories == other.memories
382
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):
385
d056b552d3f4 Made better use of layout
Windel Bouwman
parents: 383
diff changeset
17 return str(self.memories)
383
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:
385
d056b552d3f4 Made better use of layout
Windel Bouwman
parents: 383
diff changeset
21 """ Specification of how a memory may look like and what it contains. """
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
22 def __init__(self, name):
382
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
23 self.inputs = []
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
24 self.name = name
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
25 self.location = 0x0
382
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
26 self.size = 0x0
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
27
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
28 def add_input(self, inp):
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
29 assert isinstance(inp, Input)
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
30 self.inputs.append(inp)
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
31
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
32 def __repr__(self):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
33 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
34
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
35 def __eq__(self, other):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
36 return str(self) == str(other)
382
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
37
385
d056b552d3f4 Made better use of layout
Windel Bouwman
parents: 383
diff changeset
38
382
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
39 class Input:
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
40 pass
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
41
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
42
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
43 class Section(Input):
382
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
44 def __init__(self, section_name):
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
45 self.section_name = section_name
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
46
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
47 def __repr__(self):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
48 return 'Section({})'.format(self.section_name)
382
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
49
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
50
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
51 class Align(Input):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
52 def __init__(self, alignment):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
53 self.alignment = alignment
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
54
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
55 def __repr__(self):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
56 return 'Align({})'.format(self.alignment)
382
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
57
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
58
386
2a970e7270e2 Added repeat assembler macro
Windel Bouwman
parents: 385
diff changeset
59 class SymbolDefinition(Input):
2a970e7270e2 Added repeat assembler macro
Windel Bouwman
parents: 385
diff changeset
60 def __init__(self, symbol_name):
2a970e7270e2 Added repeat assembler macro
Windel Bouwman
parents: 385
diff changeset
61 self.symbol_name = symbol_name
2a970e7270e2 Added repeat assembler macro
Windel Bouwman
parents: 385
diff changeset
62
2a970e7270e2 Added repeat assembler macro
Windel Bouwman
parents: 385
diff changeset
63 def __repr__(self):
2a970e7270e2 Added repeat assembler macro
Windel Bouwman
parents: 385
diff changeset
64 return 'Symbol define: {}'.format(self.symbol_name)
2a970e7270e2 Added repeat assembler macro
Windel Bouwman
parents: 385
diff changeset
65
2a970e7270e2 Added repeat assembler macro
Windel Bouwman
parents: 385
diff changeset
66
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
67 class LayoutLexer(BaseLexer):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
68 def __init__(self):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
69 tok_spec = [
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
70 ('HEXNUMBER', r'0x[\da-fA-F]+', self.handle_number),
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
71 ('NUMBER', r'\d+', self.handle_number),
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
72 ('ID', r'[A-Za-z][A-Za-z\d_]*', self.handle_id),
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
73 ('SKIP', r'[ \t\r\n]', None),
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
74 ('LEESTEKEN', r':=|[\.,=:\-+*\[\]/\(\)]|>=|<=|<>|>|<|}|{', lambda typ, val: (val, val)),
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
75 ('STRING', r"'.*?'", lambda typ, val: (typ, val[1:-1])),
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
76 ]
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
77 super().__init__(tok_spec)
386
2a970e7270e2 Added repeat assembler macro
Windel Bouwman
parents: 385
diff changeset
78 self.kws = ['MEMORY', 'ALIGN', 'LOCATION','SECTION','SIZE', 'DEFINESYMBOL']
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
79
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
80 def handle_id(self, typ, val):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
81 if val in self.kws:
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
82 typ = val
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
83 return typ, val
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
84
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
85 def handle_number(self, typ, val):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
86 val = make_num(val)
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
87 typ = 'NUMBER'
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
88 return typ, val
382
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
89
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
90
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
91 class LayoutParser:
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
92 def __init__(self, kws):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
93 toks = ['ID', 'NUMBER', '{', '}', '.', ':', '=', '(', ')', pyyacc.EPS, pyyacc.EOF] + kws
382
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
94 g = pyyacc.Grammar(toks)
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
95 g.add_production('layout', ['mem_list'])
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
96 g.add_one_or_more('mem', 'mem_list')
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
97 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
98 g.add_one_or_more('input', 'input_list')
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
99 g.add_production('input', ['ALIGN', '(', 'NUMBER', ')'], self.handle_align)
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
100 g.add_production('input', ['SECTION', '(', 'ID', ')'], self.handle_section)
386
2a970e7270e2 Added repeat assembler macro
Windel Bouwman
parents: 385
diff changeset
101 g.add_production('input', ['DEFINESYMBOL', '(', 'ID', ')'], self.handle_defsym)
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
102
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
103 g.start_symbol = 'layout'
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
104 self.p = g.generate_parser()
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
105
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
106 def parse(self, lexer, layout):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
107 self.layout = layout
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
108 self.p.parse(lexer)
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_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
111 m = Memory(mem_name.val)
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
112 m.size = size.val
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
113 m.location = loc.val
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
114 for inp in inps:
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
115 m.add_input(inp)
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
116 self.layout.add_memory(m)
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
117
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
118 def handle_align(self, align_tag, lbrace, alignment, rbrace):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
119 return Align(alignment.val)
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
120
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
121 def handle_section(self, section_tag, lbrace, section_name, rbrace):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
122 return Section(section_name.val)
382
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
123
386
2a970e7270e2 Added repeat assembler macro
Windel Bouwman
parents: 385
diff changeset
124 def handle_defsym(self, section_tag, lbrace, name, rbrace):
2a970e7270e2 Added repeat assembler macro
Windel Bouwman
parents: 385
diff changeset
125 return SymbolDefinition(name.val)
2a970e7270e2 Added repeat assembler macro
Windel Bouwman
parents: 385
diff changeset
126
382
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
127
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
128 class LayoutLoader:
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
129 def __init__(self):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
130 self.lexer = LayoutLexer()
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
131 self.parser = LayoutParser(self.lexer.kws)
382
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents:
diff changeset
132
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
133 def load_layout(self, f):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
134 layout = Layout()
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
135 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
136 self.parser.parse(self.lexer, layout)
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
137 return layout
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
138
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
139 # Single definition:
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
140 _lloader = LayoutLoader()
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
141
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
142
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
143 def load_layout(f):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
144 return _lloader.load_layout(f)
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
145