Commit 5b7e2b76 authored by Anders Blomdell's avatar Anders Blomdell
Browse files

Changed generated C code to use reader/writer interface and moved

labcomm_encoder/labcomm_decoder into labcomm.c
parent 88bfe766
......@@ -443,7 +443,7 @@ aspect C_Decoder {
env = env.nestStruct("v");
env.println("static void decode_" + getName() + "(");
env.indent();
env.println("struct labcomm_decoder *d,");
env.println("struct labcomm_reader *r,");
env.println("void (*handle)(");
env.indent();
env.println(env.prefix + getName() + " *v,");
......@@ -479,7 +479,7 @@ aspect C_Decoder {
}
public void PrimType.C_emitDecoder(C_env env) {
env.println(env.qualid + " = labcomm_decode_" + getName() + "(d);");
env.println(env.qualid + " = labcomm_read_" + getName() + "(r);");
}
public void UserType.C_emitDecoder(C_env env) {
......@@ -525,8 +525,7 @@ aspect C_Decoder {
}
public void VariableSize.C_emitDecoderDecodeLimit(C_env env, int i) {
//env.println(env.qualid + ".n_" + i + " = labcomm_decode_int(d);");
env.println(env.qualid + ".n_" + i + " = labcomm_decode_packed32(d);");
env.println(env.qualid + ".n_" + i + " = labcomm_read_packed32(r);");
}
public void ArrayType.C_emitDecoderDecodeLimit(C_env env) {
......@@ -700,7 +699,7 @@ aspect C_Encoder {
env = env.nestStruct("(*v)");
env.println("static int encode_" + getName() + "(");
env.indent();
env.println("struct labcomm_encoder *e,");
env.println("struct labcomm_writer *w,");
env.println(env.prefix + getName() + " *v");
env.unindent();
env.println(")");
......@@ -739,8 +738,8 @@ aspect C_Encoder {
}
public void PrimType.C_emitEncoder(C_env env) {
env.println("result = labcomm_encode_" + getName() +
"(e, " + env.qualid + ");");
env.println("result = labcomm_write_" + getName() +
"(w, " + env.qualid + ");");
env.println("if (result != 0) { return result; }");
}
......@@ -786,8 +785,7 @@ aspect C_Encoder {
}
public void VariableSize.C_emitEncoderEncodeLimit(C_env env, int i) {
//env.println("labcomm_encode_int(e, " + env.qualid + ".n_" + i + ");");
env.println("labcomm_encode_packed32(e, " + env.qualid + ".n_" + i + ");");
env.println("labcomm_write_packed32(w, " + env.qualid + ".n_" + i + ");");
}
public void ArrayType.C_emitEncoderEncodeLimit(C_env env) {
......
......@@ -36,6 +36,27 @@
#define LABCOMM_VERSION "LabComm2013"
struct labcomm_decoder {
void *context;
struct labcomm_reader reader;
struct {
void *context;
const struct labcomm_lock_action *action;
} lock;
labcomm_error_handler_callback on_error;
labcomm_handle_new_datatype_callback on_new_datatype;
};
struct labcomm_encoder {
void *context;
struct labcomm_writer writer;
struct {
void *context;
const struct labcomm_lock_action *action;
} lock;
labcomm_error_handler_callback on_error;
};
struct labcomm_sample_entry {
struct labcomm_sample_entry *next;
int index;
......@@ -408,7 +429,7 @@ int labcomm_internal_encode(
if (result != 0) { goto out; }
result = labcomm_write_packed32(&e->writer, index);
if (result != 0) { goto out; }
result = encode(e, value);
result = encode(&e->writer, value);
out:
e->writer.action.end(&e->writer);
no_end:
......@@ -633,7 +654,7 @@ int labcomm_decoder_decode_one(struct labcomm_decoder *d)
d->on_error(LABCOMM_ERROR_DEC_TYPE_NOT_FOUND, 3, "%s(): type not found (id=0x%x)\n", __FUNCTION__, result);
result = -ENOENT;
} else {
entry->decoder(d, entry->handler, entry->context);
entry->decoder(&d->reader, entry->handler, entry->context);
}
}
}
......
......@@ -50,21 +50,10 @@
typedef void (*labcomm_handler_function)(void *value, void *context);
typedef void (*labcomm_decoder_function)(
struct labcomm_decoder *d,
struct labcomm_reader *r,
labcomm_handler_function handler,
void *context);
struct labcomm_decoder {
void *context;
struct labcomm_reader reader;
struct {
void *context;
const struct labcomm_lock_action *action;
} lock;
labcomm_error_handler_callback on_error;
labcomm_handle_new_datatype_callback on_new_datatype;
};
/*
* Non typesafe registration function to be called from
* generated labcomm_decoder_register_* functions.
......@@ -94,9 +83,6 @@ int labcomm_internal_decoder_ioctl(struct labcomm_decoder *decoder,
r->pos++; \
} \
return result; \
} \
static inline type labcomm_decode_##name(struct labcomm_decoder *d) { \
return labcomm_read_##name(&d->reader); \
}
#else
......@@ -112,9 +98,6 @@ int labcomm_internal_decoder_ioctl(struct labcomm_decoder *decoder,
r->pos++; \
} \
return result; \
} \
static inline type labcomm_decode_##name(struct labcomm_decoder *d) { \
return labcomm_read_##name(&d->reader); \
}
#endif
......@@ -147,11 +130,6 @@ static inline unsigned int labcomm_read_packed32(struct labcomm_reader *r)
return result;
}
static inline unsigned int labcomm_decode_packed32(struct labcomm_decoder *d)
{
return labcomm_read_packed32(&d->reader);
}
static inline char *labcomm_read_string(struct labcomm_reader *r)
{
char *result;
......@@ -170,28 +148,13 @@ static inline char *labcomm_read_string(struct labcomm_reader *r)
return result;
}
static inline char *labcomm_decode_string(struct labcomm_decoder *d)
{
return labcomm_read_string(&d->reader);
}
/*
* Semi private encoder declarations
*/
typedef int (*labcomm_encoder_function)(
struct labcomm_encoder *,
struct labcomm_writer *,
void *value);
struct labcomm_encoder {
void *context;
struct labcomm_writer writer;
struct {
void *context;
const struct labcomm_lock_action *action;
} lock;
labcomm_error_handler_callback on_error;
};
void labcomm_internal_encoder_register(
struct labcomm_encoder *encoder,
struct labcomm_signature *signature,
......@@ -223,9 +186,6 @@ int labcomm_internal_encoder_ioctl(struct labcomm_encoder *encoder,
w->pos++; \
} \
return 0; \
} \
static inline int labcomm_encode_##name(struct labcomm_encoder *e, type data) { \
return labcomm_write_##name(&e->writer, data); \
}
#else
......@@ -243,9 +203,6 @@ int labcomm_internal_encoder_ioctl(struct labcomm_encoder *encoder,
w->pos++; \
} \
return 0; \
} \
static inline int labcomm_encode_##name(struct labcomm_encoder *e, type data) { \
return labcomm_write_##name(&e->writer, data); \
}
#endif
......@@ -278,14 +235,6 @@ static inline int labcomm_write_packed32(struct labcomm_writer *w,
return 0;
}
static inline int labcomm_encode_packed32(struct labcomm_encoder *e,
unsigned int data)
{
return labcomm_write_packed32(&e->writer, data);
}
static inline int labcomm_write_string(struct labcomm_writer *w, char *s)
{
int length, i, err;
......@@ -305,10 +254,4 @@ static inline int labcomm_write_string(struct labcomm_writer *w, char *s)
return 0;
}
static inline int labcomm_encode_string(struct labcomm_encoder *e,
char *s)
{
return labcomm_write_string(&e->writer, s);
}
#endif
......@@ -6,9 +6,8 @@
static int line;
static unsigned char buffer[128];
static struct labcomm_encoder encoder = {
.context = NULL,
.writer = {
static struct labcomm_writer writer = {
.context = NULL,
.data = buffer,
.data_size = sizeof(buffer),
......@@ -17,14 +16,9 @@ static struct labcomm_encoder encoder = {
.error = 0,
.action = { NULL, NULL, NULL, NULL, NULL, NULL },
.on_error = NULL,
},
.lock = { NULL, NULL },
.on_error = NULL,
};
static struct labcomm_decoder decoder = {
.context = NULL,
.reader = {
static struct labcomm_reader reader = {
.context = NULL,
.data = buffer,
.data_size = sizeof(buffer),
......@@ -32,9 +26,6 @@ static struct labcomm_decoder decoder = {
.pos = 0,
.action = { NULL, NULL, NULL, NULL, NULL, NULL },
.on_error = NULL,
},
.lock = { NULL, NULL },
.on_error = NULL,
};
typedef uint32_t packed32;
......@@ -43,15 +34,15 @@ typedef uint32_t packed32;
{ \
type decoded; \
line = __LINE__; \
encoder.writer.pos = 0; \
labcomm_encode_##ltype(&encoder, value); \
writer.pos = 0; \
labcomm_write_##ltype(&writer, value); \
writer_assert(#ltype, expect_count, (uint8_t*)expect_bytes); \
decoder.reader.count = encoder.writer.pos; \
decoder.reader.pos = 0; \
decoded = labcomm_decode_##ltype(&decoder); \
reader.count = writer.pos; \
reader.pos = 0; \
decoded = labcomm_read_##ltype(&reader); \
if (decoded != value) { \
fprintf(stderr, "Decode error" format " != " format " @%s:%d \n", value, decoded, \
__FILE__, __LINE__); \
fprintf(stderr, "Decode error" format " != " format " @%s:%d \n", \
value, decoded, __FILE__, __LINE__); \
exit(1); \
} \
}
......@@ -60,18 +51,18 @@ static void writer_assert(char *type,
int count,
uint8_t *bytes)
{
if (encoder.writer.pos != count) {
if (writer.pos != count) {
fprintf(stderr,
"Wrong number of bytes written for '%s' (%d != %d) @%s:%d\n",
type, encoder.writer.pos, count, __FILE__, line);
type, writer.pos, count, __FILE__, line);
exit(1);
}
if (memcmp(encoder.writer.data, bytes, count) != 0) {
if (memcmp(writer.data, bytes, count) != 0) {
int i;
fprintf(stderr, "Wrong bytes written for '%s' ( ", type);
for (i = 0 ; i < count ; i++) {
fprintf(stderr, "%2.2x ", encoder.writer.data[i]);
fprintf(stderr, "%2.2x ", writer.data[i]);
}
fprintf(stderr, "!= ");
for (i = 0 ; i < count ; i++) {
......
......@@ -9,9 +9,11 @@
#define IOCTL_WRITER_RESET 4097
static unsigned char buffer[128];
struct labcomm_writer *writer;
static int buf_writer_alloc(struct labcomm_writer *w, char *labcomm_version)
{
writer = w; /* Hack */
w->data_size = sizeof(buffer);
w->count = w->data_size;
w->data = buffer;
......@@ -112,8 +114,8 @@ void dump_encoder(struct labcomm_encoder *encoder)
{
int i;
for (i = 0 ; i < encoder->writer.pos ; i++) {
printf("%2.2x ", encoder->writer.data[i]);
for (i = 0 ; i < writer->pos ; i++) {
printf("%2.2x ", writer->data[i]);
}
printf("\n");
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment