source: mainline/tools/autogen.py@ 4be73ba

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4be73ba was 4be73ba, checked in by Jakub Jermar <jakub@…>, 11 years ago

Add copyright header.

  • Property mode set to 100755
File size: 4.0 KB
Line 
1#!/usr/bin/env python
2#
3# Copyright (c) 2014 Jakub Jermar
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9#
10# - Redistributions of source code must retain the above copyright
11# notice, this list of conditions and the following disclaimer.
12# - Redistributions in binary form must reproduce the above copyright
13# notice, this list of conditions and the following disclaimer in the
14# documentation and/or other materials provided with the distribution.
15# - The name of the author may not be used to endorse or promote products
16# derived from this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28#
29
30import sys
31import json
32import re
33
34def usage():
35 print("%s - Automated structure and offsets generator" % sys.argv[0])
36 print("%s file.ag probe|generate struct.ag" % sys.argv[0])
37 sys.exit()
38
39def generate_includes(struct):
40 code = ""
41 for i in range(len(struct['includes'])):
42 code = code + "#include %s\n" % struct['includes'][i]
43 return code.strip()
44
45def generate_struct(struct):
46 code = "typedef struct %s {\n" % struct['name']
47 for i in range(len(struct['members'])):
48 member = struct['members'][i]
49 code = code + "\t%s %s;\n" % (member['type'], member['name'])
50 code = code + "} %s_t;" % struct['name']
51 return code
52
53def generate_probes(struct):
54 code = ""
55 for i in range(len(struct['members'])):
56 member = struct['members'][i]
57 code = code + ("\temit_constant(OFFSET_%s, offsetof(%s_t, %s));\n" %
58 (member['name'].upper(), struct['name'], member['name']))
59 return code
60
61def probe(struct):
62 name = struct['name']
63 typename = struct['name'] + "_t"
64
65 code = """
66%s
67
68#define str(s) #s
69#define emit_constant(n, v) \
70 asm volatile ("EMITTED_CONSTANT " str(n) \" = %%0\" :: \"i\" (v))
71#define offsetof(t, m) ((size_t) &(((t *) 0)->m))
72
73%s
74
75int main()
76{
77%s
78 emit_constant(%s_SIZE, sizeof(%s));
79 return 0;
80}
81 """ % (generate_includes(struct), generate_struct(struct),
82 generate_probes(struct), name.upper(), typename)
83
84 return code
85
86def generate_defines(pairs):
87 code = ""
88 for pair in pairs:
89 code = code + "#define %s %s\n" % (pair[0], pair[1])
90 return code.strip()
91
92def generate(struct, lines):
93 code = """
94/*****************************************************************************
95 * AUTO-GENERATED FILE, DO NOT EDIT!!!
96 * Generated by: tools/autogen.py
97 * Generated from: %s
98 *****************************************************************************/
99
100#ifndef AUTOGEN_%s_H
101#define AUTOGEN_%s_H
102
103#ifndef __ASM__
104%s
105#endif
106
107%s
108
109#ifndef __ASM__
110%s
111#endif
112
113#endif
114 """ % (sys.argv[2], struct['name'].upper(), struct['name'].upper(),
115 generate_includes(struct), generate_defines(lines),
116 generate_struct(struct))
117
118 return code
119
120def filter_pairs(lines):
121 pattern = re.compile("^\tEMITTED_CONSTANT ([A-Z_]*) = \$([0-9]*)$");
122 pairs = []
123 for line in lines:
124 res = pattern.match(line)
125 if res == None:
126 continue
127 pairs = pairs + [res.group(1, 2)]
128 return pairs
129
130
131def run():
132 if len(sys.argv) != 3:
133 usage()
134
135 with open(sys.argv[2], "rb") as fp:
136 struct = json.load(fp)
137
138 if sys.argv[1] == "probe":
139 code = probe(struct)
140 print(code)
141 elif sys.argv[1] == "generate":
142 lines = sys.stdin.readlines()
143 pairs = filter_pairs(lines)
144 code = generate(struct, pairs)
145 print(code)
146 else:
147 usage()
148
149run()
150
Note: See TracBrowser for help on using the repository browser.