mpers.awk: make fillers added to the output structure more predictable

* mpers.awk (array_seq): New function.
(what_is): Use it for printing filler names.  Tweak return types.
* mpers_test.sh: Use a more complicated sample type to test mpers
machinery.
This commit is contained in:
Дмитрий Левин 2015-12-15 12:58:42 +00:00
parent 54d18a279a
commit c4afd6dc7a
2 changed files with 98 additions and 22 deletions

View File

@ -15,9 +15,17 @@ function array_get(array_idx, array_member, array_return)
}
return array_return
}
function array_seq(array_idx)
{
if ("seq" in array[array_idx])
return array[array_idx]["seq"]
index_seq++
array[array_idx]["seq"] = index_seq
return index_seq
}
function enter(array_idx)
{
if (called[array_idx]) {
if (array_idx in called) {
printf("%s: index loop detected:", FILENAME) > "/dev/stderr"
for (item in called)
printf(" %s", item) > "/dev/stderr"
@ -40,12 +48,12 @@ function what_is(what_idx, type_idx, special, item, \
case "base_type":
switch (array_get(what_idx, "encoding")) {
case 5: # signed
printf("%s ", "int" \
8 * array_get(what_idx, "byte_size") "_t")
printf("int%s_t ",
8 * array_get(what_idx, "byte_size"))
break
case 7: # unsigned
printf("%s ", "uint" \
8 * array_get(what_idx, "byte_size") "_t")
printf("uint%s_t ",
8 * array_get(what_idx, "byte_size"))
break
default: # float, signed/unsigned char
printf("%s ", array_get(what_idx, "name"))
@ -56,19 +64,19 @@ function what_is(what_idx, type_idx, special, item, \
case "enumeration_type":
type_idx = array_get(what_idx, "type")
returned_size = array_get(what_idx, "byte_size")
printf("%s ", "uint" 8 * returned_size "_t")
printf("uint%s_t ", 8 * returned_size)
break
case "pointer_type":
printf("%s", "mpers_ptr_t ")
printf("mpers_ptr_t ")
returned_size = array_get(what_idx, "byte_size")
break
case "array_type":
type_idx = array_get(what_idx, "type")
what_is(type_idx)
to_return = array[what_idx]["upper_bound"]
returned_size = to_return * returned_size
if ("" == to_return)
to_return = "00"
to_return = 0
returned_size = to_return * returned_size
return leave(what_idx, to_return)
break
case "structure_type":
@ -84,15 +92,14 @@ function what_is(what_idx, type_idx, special, item, \
loc_diff = location - prev_location - \
prev_returned_size
if (loc_diff != 0) {
printf("%s", \
"unsigned char mpers_filler_" \
item "[" loc_diff "];\n")
printf("unsigned char mpers_%s_%s[%s];\n",
"filler", array_seq(item), loc_diff)
}
prev_location = location
returned = what_is(item)
prev_returned_size = returned_size
printf("%s", array[item]["name"])
if (returned) {
if ("" != returned) {
printf("[%s]", returned)
}
print ";"
@ -101,10 +108,10 @@ function what_is(what_idx, type_idx, special, item, \
returned_size = array_get(what_idx, "byte_size")
loc_diff = returned_size - prev_location - prev_returned_size
if (loc_diff != 0) {
printf("%s", "unsigned char mpers_end_filler_" \
item "[" loc_diff "];\n")
printf("unsigned char mpers_%s_%s[%s];\n",
"end_filler", array_seq(item), loc_diff)
}
printf("%s", "} ATTRIBUTE_PACKED ")
printf("} ATTRIBUTE_PACKED ")
break
case "union_type":
print "union {"
@ -113,13 +120,13 @@ function what_is(what_idx, type_idx, special, item, \
array_get(item, "parent") == what_idx) {
returned = what_is(item)
printf("%s", array_get(item, "name"))
if (returned) {
if ("" != returned) {
printf("[%s]", returned)
}
print ";"
}
}
printf("%s", "} ")
printf("} ")
returned_size = array_get(what_idx, "byte_size")
break
case "typedef":
@ -135,7 +142,7 @@ function what_is(what_idx, type_idx, special, item, \
what_is(type_idx)
break
}
return leave(what_idx, 0)
return leave(what_idx, "")
}
BEGIN {
print "#include <inttypes.h>"

View File

@ -1,6 +1,9 @@
#!/bin/sh -efu
mpers_name="$1"; shift
size="$(printf %s "$mpers_name" |tr -cd '[0-9]')"
[ "$size" -gt 0 ]
srcdir=${0%/*}
mpers_sh="${srcdir}/mpers.sh"
@ -11,17 +14,83 @@ sample="$mpers_dir/sample.c"
cat > "$sample" <<EOF
#include "mpers_type.h"
#include DEF_MPERS_TYPE(sample_struct)
typedef struct { int i; unsigned short s[0]; } sample_struct;
typedef struct {
struct {
void *p;
char sc;
/* unsigned char mpers_filler_1[1]; */
short ss;
unsigned char uc;
/* unsigned char mpers_filler_2[3]; */
int si;
unsigned ui;
long sl;
unsigned short us;
/* unsigned char mpers_filler_3[6]; */
long long sll __attribute__((__aligned__(8)));
unsigned long long ull;
unsigned long ul;
long asl[3];
char f;
/* unsigned char mpers_end_filler_4[7]; */
} s;
union {
long long sll;
unsigned long long ull;
void *p;
long sl;
unsigned long ul;
int si;
unsigned ui;
short ss;
unsigned short us;
char sc;
unsigned char uc;
} u[3];
short f[0];
} sample_struct;
#include MPERS_DEFS
EOF
expected="$mpers_dir/sample.expected"
cat > "$expected" <<EOF
#include <inttypes.h>
typedef uint${size}_t mpers_ptr_t;
typedef
struct {
int32_t i;
uint16_t s[00];
struct {
mpers_ptr_t p;
char sc;
unsigned char mpers_filler_1[1];
int16_t ss;
unsigned char uc;
unsigned char mpers_filler_2[3];
int32_t si;
uint32_t ui;
int${size}_t sl;
uint16_t us;
unsigned char mpers_filler_3[6];
int64_t sll;
uint64_t ull;
uint${size}_t ul;
int${size}_t asl[3];
char f;
unsigned char mpers_end_filler_4[7];
} ATTRIBUTE_PACKED s;
union {
int64_t sll;
uint64_t ull;
mpers_ptr_t p;
int${size}_t sl;
uint${size}_t ul;
int32_t si;
uint32_t ui;
int16_t ss;
uint16_t us;
char sc;
unsigned char uc;
} u[3];
int16_t f[0];
} ATTRIBUTE_PACKED ${mpers_name}_sample_struct;
#define MPERS_${mpers_name}_sample_struct ${mpers_name}_sample_struct
EOF