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