summaryrefslogtreecommitdiffstats
path: root/tools/xml2h.py
blob: d259c5ba3cb102c3e531a6a492a6c6fc231287f9 (plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/python
#             __________               __   ___.
#   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
#   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
#   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
#   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
#                     \/            \/     \/    \/            \/
# $Id$
#
# Copyright (C) 2007 Catalin Patulea <cat@vv.carleton.ca>
#
#  All files in this archive are subject to the GNU General Public License.
#  See the file COPYING in the source tree root for full license agreement.
# 
#  This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
#  KIND, either express or implied.
#
import sys, os.path, array, re
from xml.dom import Node
from xml.dom.minidom import parse


C_IDENT_RE = re.compile('^[0-9a-zA-Z_]+$')


def getText(nodelist):
    rc = ""
    for node in nodelist:
        if node.nodeType == node.TEXT_NODE:
            rc = rc + node.data
    return rc


def descendAll(root, tagname):
    for child in root.childNodes:
        if child.nodeType == Node.ELEMENT_NODE and child.tagName == tagname:
            yield child


def descend(root, tagname):
    return descendAll(root, tagname).next()


def getTagText(root, tagname):
    try:
        tag = descend(root, tagname)
    except StopIteration:
        return None
    return getText(tag.childNodes)


def main():
    dom = parse(sys.stdin)
    
    ofd = descend(dom, "ofd")
    object_file = descend(ofd, "object_file")
    object_file_name = descend(object_file, "name")
    
    out_filepath = getText(object_file_name.childNodes)
    sys.stderr.write("*.out filename (input): %s\n" % out_filepath)
    
    out_file = open(out_filepath, "rb")
    h_file = sys.stdout
    
    h_file.write("""#ifndef DSP_IMAGE
#define DSP_IMAGE
/*
 * Automatically generated by xml2h.py from %s.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 *
 */
""" % out_filepath)

    # Section data and directory.
    h_directory = ["""
static const struct dsp_section dsp_image[] = {"""]
    
    ti_coff = descend(object_file, "ti_coff")
    for section in descendAll(ti_coff, "section"):
        page = int(getTagText(section, "page") or "0", 16)
        name = getTagText(section, "name")
        physical_addr = int(getTagText(section, "physical_addr"), 16)
        raw_data_size = int(getTagText(section, "raw_data_size"), 16)
        copy = getTagText(section, "copy")
        data = getTagText(section, "data")
        regular = getTagText(section, "regular")
        text = getTagText(section, "text")
        bss = getTagText(section, "bss")
        
        file_offsets = descend(section, "file_offsets")
        raw_data_ptr = int(getTagText(file_offsets, "raw_data_ptr"), 16)
        
        if copy:
            # Empirically, .debug* sections have this attribute set.
            sys.stderr.write(
                "%s: didn't copy debug section ('copy' attribute set)\n" %
                name)
            continue
        
        if raw_data_size == 0:
            sys.stderr.write("%s: not copying empty section\n" % name)
            continue

        if raw_data_size % 2 != 0:
            sys.stderr.write("%s: error, raw_data_size 0x%04x not a multiple "
                "of word size (2 bytes)\n" % (name, raw_data_size))
            break
        
        if data or regular or text:
            sys.stderr.write("%s: placing 0x%04x words at 0x%04x from offset "
                "0x%08x\n" % (
                name, raw_data_size >> 1, physical_addr, raw_data_ptr))

            sanitized_name = name.replace(".", "_")
            h_file.write(("static const unsigned short _section%s[] = {\n" %
                sanitized_name))

            out_file.seek(raw_data_ptr)
            data = array.array('H')
            data.fromfile(out_file, raw_data_size >> 1)
            h_file.write("\t")
            for word in data:
                h_file.write("0x%04x, " % word)
            h_file.write("""
};
""")
            
            h_directory.append("\t{_section%s, 0x%04x, 0x%04x}," % (
                sanitized_name, physical_addr, raw_data_size >> 1))
                
            continue
        
        if bss:
            sys.stderr.write("%s: bss section, 0x%04x words at 0x%04x\n" % (
                name, raw_data_size >> 1, physical_addr))
            
            h_directory.append("\t{NULL /* %s */, 0x%04x, 0x%04x}," % (
                name, physical_addr, raw_data_size >> 1))
            continue
        
        sys.stderr.write("%s: error, unprocessed section\n" % name)
    
    h_file.write("\n")

    h_directory.append("\t{NULL, 0, 0}")
    h_directory.append("};")
    
    h_file.write("\n".join(h_directory))
    h_file.write("\n")
    
    # Symbols.
    symbol_table = descend(ti_coff, "symbol_table")
    h_file.write("""
/* Symbol table, usable with the DSP_() macro (see dsp-target.h). */
""")
    for symbol in descendAll(symbol_table, "symbol"):
        name = getTagText(symbol, "name")
        kind = getTagText(symbol, "kind")
        value = int(getTagText(symbol, "value"), 16)
        
        if kind != "defined":
            continue
        
        if not C_IDENT_RE.match(name):
            continue
        
        h_file.write("#define %s 0x%04x\n" % (name, value))
    
    h_file.write("\n#endif\n")
    h_file.close()
    out_file.close()
    
    dom.unlink()


if __name__ == "__main__":
    main()