-
Notifications
You must be signed in to change notification settings - Fork 1
/
addresses.py
66 lines (59 loc) · 2.01 KB
/
addresses.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
# Disassembler 8051/8052
#
# Symbols and associated addresses
#
# Copyright (c) 2022 Aleksander Mazur
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys, re
import utils
class Addresses:
pat = re.compile(';?(?P<label>[a-zA-Z0-9_]+)\s+(?P<scope>[A-Z]+)\s+(?P<addr>[0-9A-F]+)(?P<hex>h|H)?')
def __init__(self):
self.addr = {}
self.addr['CODE'] = {} # offsets in code segment used for emitting ORG statements
self.addr['DATA'] = {} # symbols in data segment (direct addressing)
self.addr['BIT'] = {} # symbols in bit segment
self.addr['LABEL'] = {} # symbols in code segment used for emitting labels
def __str__(self):
s = ''
for k, v in self.addr.items():
s += ('\n%s: ' % k) + str(v)
return s[1:]
def include(self, f):
starts = {}
for line in f.readlines():
line = line[:-1]
m = Addresses.pat.match(line)
if m:
label = m.group('label')
scope = m.group('scope').upper()
base = m.group('hex')
if base:
base = 16
else:
base = 10
addr = int(m.group('addr'), base)
if scope in self.addr:
if addr in self.addr[scope]:
print('warning: overriding %s %s from %s to %s' % (scope, utils.int2hex(addr), self.addr[scope][addr], label), file=sys.stderr)
self.addr[scope][addr] = label
if scope == 'CODE':
starts[label] = addr
elif len(line) > 0 and line[0] != ';':
print('warning: unrecognized definition: %s' % line, file=sys.stderr)
return starts
def __getitem__(self, scope):
return self.addr[scope]
def __iter__(self):
return iter(self.addr)