source: mainline/tools/autogen.py@ 476ac3b

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

Reduce the number of files that include <sys/types.h>

  • Property mode set to 100755
File size: 5.2 KB
RevLine 
[0b811da2]1#!/usr/bin/env python
[4be73ba]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#
[0b811da2]29
30import sys
[6ecc6ba6]31import yaml
[0b811da2]32import re
33
34def usage():
35 print("%s - Automated structure and offsets generator" % sys.argv[0])
[147a066]36 print("%s file.ag depend|probe|generate struct.ag" % sys.argv[0])
[0b811da2]37 sys.exit()
38
[147a066]39def depend(struct):
40 deps = ""
41 for include in struct['includes']:
42 if 'depends' in include.keys():
43 deps = deps + include['depends'] + "\n"
44 return deps.strip()
45
[0b811da2]46def generate_includes(struct):
47 code = ""
[713b8ce7]48 for include in struct['includes']:
49 if 'guard' in include.keys():
50 code = code + "#ifdef %s\n" % include['guard']
51 if 'negative-guard' in include.keys():
52 code = code + "#ifndef %s\n" % include['negative-guard']
53 code = code + "#include %s\n" % include['include']
54 if 'guard' in include.keys():
55 code = code + "#endif\n"
56 if 'negative-guard' in include.keys():
57 code = code + "#endif\n"
[0b811da2]58 return code.strip()
59
60def generate_struct(struct):
[fa9f8ad]61 packed = ""
62 if ('packed' in struct.keys() and struct['packed']):
63 packed = "__attribute__ ((packed)) "
[0b811da2]64 code = "typedef struct %s {\n" % struct['name']
65 for i in range(len(struct['members'])):
66 member = struct['members'][i]
[b8f433e]67 if 'elements' in member.keys():
68 code = code + "\t%s %s[%d];\n" % (member['type'], member['name'], member['elements'])
69 else:
70 code = code + "\t%s %s;\n" % (member['type'], member['name'])
[fa9f8ad]71 code = code + "} %s%s_t;" % (packed, struct['name'])
[0b811da2]72 return code
73
74def generate_probes(struct):
75 code = ""
76 for i in range(len(struct['members'])):
77 member = struct['members'][i]
[795e2bf]78 code = code + ("\temit_constant(%s_OFFSET_%s, offsetof(%s_t, %s));\n" %
[32e3cdf]79 (struct['name'].upper(), member['name'].upper(), struct['name'],
80 member['name']))
[795e2bf]81 code = code + ("\temit_constant(%s_SIZE_%s, sizeof(((%s_t *) 0)->%s));\n" %
[9827b5b]82 (struct['name'].upper(), member['name'].upper(), struct['name'],
83 member['name']))
84 if 'elements' in member.keys():
[795e2bf]85 code = code + ("\temit_constant(%s_%s_ITEM_SIZE, sizeof(%s));\n" %
[9827b5b]86 (struct['name'].upper(), member['name'].upper(), member['type']))
87
[0b811da2]88 return code
89
90def probe(struct):
91 name = struct['name']
92 typename = struct['name'] + "_t"
93
94 code = """
[795e2bf]95%s
[0b811da2]96
97#define str(s) #s
98#define emit_constant(n, v) \
99 asm volatile ("EMITTED_CONSTANT " str(n) \" = %%0\" :: \"i\" (v))
[8d2dd7f2]100#undef offsetof
[0b811da2]101#define offsetof(t, m) ((size_t) &(((t *) 0)->m))
102
103%s
104
[795e2bf]105extern int main(int, char *[]);
106
107int main(int argc, char *argv[])
[0b811da2]108{
109%s
110 emit_constant(%s_SIZE, sizeof(%s));
[81f0940]111 return 0;
[0b811da2]112}
113 """ % (generate_includes(struct), generate_struct(struct),
114 generate_probes(struct), name.upper(), typename)
[795e2bf]115
[0b811da2]116 return code
117
118def generate_defines(pairs):
119 code = ""
120 for pair in pairs:
121 code = code + "#define %s %s\n" % (pair[0], pair[1])
122 return code.strip()
123
124def generate(struct, lines):
125 code = """
126/*****************************************************************************
127 * AUTO-GENERATED FILE, DO NOT EDIT!!!
128 * Generated by: tools/autogen.py
129 * Generated from: %s
130 *****************************************************************************/
131
132#ifndef AUTOGEN_%s_H
133#define AUTOGEN_%s_H
134
135#ifndef __ASM__
136%s
137#endif
138
139%s
140
141#ifndef __ASM__
142%s
143#endif
144
145#endif
146 """ % (sys.argv[2], struct['name'].upper(), struct['name'].upper(),
147 generate_includes(struct), generate_defines(lines),
148 generate_struct(struct))
149
150 return code
151
152def filter_pairs(lines):
[da5ba3a]153 pattern = re.compile("^\tEMITTED_CONSTANT ([A-Z_][A-Z0-9_]*) = (\$|#)?([0-9]+)$");
[0b811da2]154 pairs = []
155 for line in lines:
156 res = pattern.match(line)
157 if res == None:
158 continue
[da5ba3a]159 pairs = pairs + [res.group(1, 3)]
[0b811da2]160 return pairs
161
162
163def run():
164 if len(sys.argv) != 3:
165 usage()
166
167 with open(sys.argv[2], "rb") as fp:
[6ecc6ba6]168 struct = yaml.load(fp)
[0b811da2]169
[147a066]170 if sys.argv[1] == "depend":
171 deps = depend(struct)
172 print(deps)
173 elif sys.argv[1] == "probe":
[0b811da2]174 code = probe(struct)
175 print(code)
176 elif sys.argv[1] == "generate":
177 lines = sys.stdin.readlines()
178 pairs = filter_pairs(lines)
179 code = generate(struct, pairs)
180 print(code)
181 else:
182 usage()
183
184run()
Note: See TracBrowser for help on using the repository browser.