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
|
#!/usr/bin/env python
import sys
import yaml
def emit_func_bit(name, field, params):
bit_params = dict(name=name, field=field, word=int(params[0]), bit=int(params[1]))
print("""static inline bool ata_get_%(name)s_%(field)s(const unsigned char *buf) {
ata_word_t val = ata_get_word(buf, %(word)d);
return val & (1 << %(bit)d);
}
""" % bit_params)
def emit_func_bits(name, field, params):
bit_params = dict(name=name, field=field, word=int(params[0]), start_bit=int(params[1]), end_bit=int(params[2]))
print("""static inline unsigned ata_get_%(name)s_%(field)s(const unsigned char *buf) {
ata_word_t val = ata_get_word(buf, %(word)d);
return (val >> %(start_bit)d) & ((1<<(%(end_bit)d - %(start_bit)d + 1)) - 1);
}
""" % bit_params)
def emit_func_string(name, field, params):
bit_params = dict(name=name, field=field, word_start=int(params[0]), word_end=int(params[1]))
print("""static inline void ata_get_%(name)s_%(field)s(const unsigned char *buf, char *out) {
ata_get_string(buf, %(word_start)d, %(word_end)d, out);
}
""" % bit_params)
def emit_func_longword(name, field, params):
bit_params = dict(name=name, field=field, word_start=int(params))
print("""static inline ata_longword_t ata_get_%(name)s_%(field)s(const unsigned char *buf) {
return ata_get_longword(buf, %(word_start)d);
}
""" % bit_params)
def emit_func_qword(name, field, params):
bit_params = dict(name=name, field=field, word_start=int(params))
print("""static inline ata_qword_t ata_get_%(name)s_%(field)s(const unsigned char *buf) {
return ata_get_qword(buf, %(word_start)d);
}
""" % bit_params)
kinds = {
'bit': emit_func_bit,
'bits': emit_func_bits,
'string': emit_func_string,
'longword': emit_func_longword,
'qword': emit_func_qword,
}
def emit_header_single(name, struct):
for field, info in list(struct.items()):
keys = list(info.keys())
assert(len(keys) == 1)
kind = keys[0]
params = info[kind]
kinds[kind](name, field, params)
def emit_header(structs):
for name, struct in list(structs.items()):
emit_header_single(name, struct)
def emit_prefix():
print('/* Generated file, do not edit */')
print('#ifndef ATA_PARSE_H')
print('#define ATA_PARSE_H')
print('#include "ata.h"')
def emit_suffix():
print('#endif')
def convert_def(filename):
f = open(filename)
structs = yaml.load(f)
f.close()
emit_header(structs)
if __name__ == '__main__':
emit_prefix()
filenames = sys.argv[1:]
for filename in filenames:
convert_def(filename)
emit_suffix()
|