| 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 |
|
|---|
| 30 | import sys
|
|---|
| 31 | import yaml
|
|---|
| 32 | import re
|
|---|
| 33 |
|
|---|
| 34 | def 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 |
|
|---|
| 39 | def generate_includes(struct):
|
|---|
| 40 | code = ""
|
|---|
| 41 | for include in struct['includes']:
|
|---|
| 42 | if 'guard' in include.keys():
|
|---|
| 43 | code = code + "#ifdef %s\n" % include['guard']
|
|---|
| 44 | if 'negative-guard' in include.keys():
|
|---|
| 45 | code = code + "#ifndef %s\n" % include['negative-guard']
|
|---|
| 46 | code = code + "#include %s\n" % include['include']
|
|---|
| 47 | if 'guard' in include.keys():
|
|---|
| 48 | code = code + "#endif\n"
|
|---|
| 49 | if 'negative-guard' in include.keys():
|
|---|
| 50 | code = code + "#endif\n"
|
|---|
| 51 | return code.strip()
|
|---|
| 52 |
|
|---|
| 53 | def generate_struct(struct):
|
|---|
| 54 | code = "typedef struct %s {\n" % struct['name']
|
|---|
| 55 | for i in range(len(struct['members'])):
|
|---|
| 56 | member = struct['members'][i]
|
|---|
| 57 | if 'elements' in member.keys():
|
|---|
| 58 | code = code + "\t%s %s[%d];\n" % (member['type'], member['name'], member['elements'])
|
|---|
| 59 | else:
|
|---|
| 60 | code = code + "\t%s %s;\n" % (member['type'], member['name'])
|
|---|
| 61 | code = code + "} %s_t;" % struct['name']
|
|---|
| 62 | return code
|
|---|
| 63 |
|
|---|
| 64 | def generate_probes(struct):
|
|---|
| 65 | code = ""
|
|---|
| 66 | for i in range(len(struct['members'])):
|
|---|
| 67 | member = struct['members'][i]
|
|---|
| 68 | code = code + ("\temit_constant(%s_OFFSET_%s, offsetof(%s_t, %s));\n" %
|
|---|
| 69 | (struct['name'].upper(), member['name'].upper(), struct['name'],
|
|---|
| 70 | member['name']))
|
|---|
| 71 | code = code + ("\temit_constant(%s_SIZE_%s, sizeof(((%s_t *) 0)->%s));\n" %
|
|---|
| 72 | (struct['name'].upper(), member['name'].upper(), struct['name'],
|
|---|
| 73 | member['name']))
|
|---|
| 74 | if 'elements' in member.keys():
|
|---|
| 75 | code = code + ("\temit_constant(%s_%s_ITEM_SIZE, sizeof(%s));\n" %
|
|---|
| 76 | (struct['name'].upper(), member['name'].upper(), member['type']))
|
|---|
| 77 |
|
|---|
| 78 | return code
|
|---|
| 79 |
|
|---|
| 80 | def probe(struct):
|
|---|
| 81 | name = struct['name']
|
|---|
| 82 | typename = struct['name'] + "_t"
|
|---|
| 83 |
|
|---|
| 84 | code = """
|
|---|
| 85 | %s
|
|---|
| 86 |
|
|---|
| 87 | #define str(s) #s
|
|---|
| 88 | #define emit_constant(n, v) \
|
|---|
| 89 | asm volatile ("EMITTED_CONSTANT " str(n) \" = %%0\" :: \"i\" (v))
|
|---|
| 90 | #define offsetof(t, m) ((size_t) &(((t *) 0)->m))
|
|---|
| 91 |
|
|---|
| 92 | %s
|
|---|
| 93 |
|
|---|
| 94 | extern int main(int, char *[]);
|
|---|
| 95 |
|
|---|
| 96 | int main(int argc, char *argv[])
|
|---|
| 97 | {
|
|---|
| 98 | %s
|
|---|
| 99 | emit_constant(%s_SIZE, sizeof(%s));
|
|---|
| 100 | return 0;
|
|---|
| 101 | }
|
|---|
| 102 | """ % (generate_includes(struct), generate_struct(struct),
|
|---|
| 103 | generate_probes(struct), name.upper(), typename)
|
|---|
| 104 |
|
|---|
| 105 | return code
|
|---|
| 106 |
|
|---|
| 107 | def generate_defines(pairs):
|
|---|
| 108 | code = ""
|
|---|
| 109 | for pair in pairs:
|
|---|
| 110 | code = code + "#define %s %s\n" % (pair[0], pair[1])
|
|---|
| 111 | return code.strip()
|
|---|
| 112 |
|
|---|
| 113 | def generate(struct, lines):
|
|---|
| 114 | code = """
|
|---|
| 115 | /*****************************************************************************
|
|---|
| 116 | * AUTO-GENERATED FILE, DO NOT EDIT!!!
|
|---|
| 117 | * Generated by: tools/autogen.py
|
|---|
| 118 | * Generated from: %s
|
|---|
| 119 | *****************************************************************************/
|
|---|
| 120 |
|
|---|
| 121 | #ifndef AUTOGEN_%s_H
|
|---|
| 122 | #define AUTOGEN_%s_H
|
|---|
| 123 |
|
|---|
| 124 | #ifndef __ASM__
|
|---|
| 125 | %s
|
|---|
| 126 | #endif
|
|---|
| 127 |
|
|---|
| 128 | %s
|
|---|
| 129 |
|
|---|
| 130 | #ifndef __ASM__
|
|---|
| 131 | %s
|
|---|
| 132 | #endif
|
|---|
| 133 |
|
|---|
| 134 | #endif
|
|---|
| 135 | """ % (sys.argv[2], struct['name'].upper(), struct['name'].upper(),
|
|---|
| 136 | generate_includes(struct), generate_defines(lines),
|
|---|
| 137 | generate_struct(struct))
|
|---|
| 138 |
|
|---|
| 139 | return code
|
|---|
| 140 |
|
|---|
| 141 | def filter_pairs(lines):
|
|---|
| 142 | pattern = re.compile("^\tEMITTED_CONSTANT ([A-Z_][A-Z0-9_]*) = (\$|#)?([0-9]+)$");
|
|---|
| 143 | pairs = []
|
|---|
| 144 | for line in lines:
|
|---|
| 145 | res = pattern.match(line)
|
|---|
| 146 | if res == None:
|
|---|
| 147 | continue
|
|---|
| 148 | pairs = pairs + [res.group(1, 3)]
|
|---|
| 149 | return pairs
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 | def run():
|
|---|
| 153 | if len(sys.argv) != 3:
|
|---|
| 154 | usage()
|
|---|
| 155 |
|
|---|
| 156 | with open(sys.argv[2], "rb") as fp:
|
|---|
| 157 | struct = yaml.load(fp)
|
|---|
| 158 |
|
|---|
| 159 | if sys.argv[1] == "probe":
|
|---|
| 160 | code = probe(struct)
|
|---|
| 161 | print(code)
|
|---|
| 162 | elif sys.argv[1] == "generate":
|
|---|
| 163 | lines = sys.stdin.readlines()
|
|---|
| 164 | pairs = filter_pairs(lines)
|
|---|
| 165 | code = generate(struct, pairs)
|
|---|
| 166 | print(code)
|
|---|
| 167 | else:
|
|---|
| 168 | usage()
|
|---|
| 169 |
|
|---|
| 170 | run()
|
|---|