Commit df9405e0 authored by Anders Blomdell's avatar Anders Blomdell
Browse files

20141009 passes first tests, lots of cleanup still to do.

parent 14e29fd7
...@@ -1188,12 +1188,12 @@ aspect C_Signature { ...@@ -1188,12 +1188,12 @@ aspect C_Signature {
} }
} }
env.println("};"); env.println("};");
C_emitSizeofValue(env);
env.println("struct labcomm"+env.verStr+"_signature labcomm"+env.verStr+"_signature_" + env.println("struct labcomm"+env.verStr+"_signature labcomm"+env.verStr+"_signature_" +
env.prefix + getName() + " = {"); env.prefix + getName() + " = {");
env.indent(); env.indent();
env.println("LABCOMM_SAMPLE, \"" + getName() + "\","); env.println("\"" + getName() + "\",");
env.println("(int (*)(struct labcomm"+env.verStr+"_signature *, void *))labcomm"+env.verStr+"_sizeof_" + env.println("sizeof_" + env.prefix + getName() + ",");
env.prefix + getName() + ",");
env.println("sizeof(signature_bytes_" + env.prefix + getName() + "),"); env.println("sizeof(signature_bytes_" + env.prefix + getName() + "),");
env.println("signature_bytes_" + env.prefix + getName() + ","); env.println("signature_bytes_" + env.prefix + getName() + ",");
env.println("0"); env.println("0");
...@@ -1260,6 +1260,22 @@ aspect C_Sizeof { ...@@ -1260,6 +1260,22 @@ aspect C_Sizeof {
return getType().C_fixedSizeof(); return getType().C_fixedSizeof();
} }
public void Decl.C_emitSizeof(C_env env) {
}
public void SampleDecl.C_emitSizeof(C_env env) {
env = env.nestStruct("(*v)");
env.println("int labcomm"+env.verStr+"_sizeof_" + env.prefix + getName() +
"(" + env.prefix + getName() + " *v)");
env.println("{");
env.indent();
env.println("return labcomm_internal_sizeof(" +
"&labcomm" + env.verStr+"_signature_" + env.prefix + getName() +
", v);");
env.unindent();
env.println("}");
}
public int Type.C_fixedSizeof() { public int Type.C_fixedSizeof() {
throw new Error(this.getClass().getName() + throw new Error(this.getClass().getName() +
".C_fixedSizeof()" + ".C_fixedSizeof()" +
...@@ -1308,18 +1324,17 @@ aspect C_Sizeof { ...@@ -1308,18 +1324,17 @@ aspect C_Sizeof {
return getType().C_fixedSizeof() * elements; return getType().C_fixedSizeof() * elements;
} }
public void Decl.C_emitSizeof(C_env env) { public void Decl.C_emitSizeofValue(C_env env) {
} }
public void SampleDecl.C_emitSizeof(C_env env) { public void SampleDecl.C_emitSizeofValue(C_env env) {
env = env.nestStruct("(*v)"); env = env.nestStruct("(*v)");
env.println("int labcomm"+env.verStr+"_sizeof_" + env.prefix + getName() + env.println("static int sizeof_" + env.prefix + getName() + "(void *vv)");
"(" + env.prefix + getName() + " *v)");
env.println("{"); env.println("{");
env.indent(); env.indent();
env.println("int result = labcomm"+env.verStr+"_size_packed32(labcomm"+env.verStr+"_signature_" + env.println("int result = 0;");
env.prefix + getName() +".index);");
if (C_isDynamic()) { if (C_isDynamic()) {
env.println(env.prefix + getName() + " *v = vv;");
getType().C_emitSizeof(env); getType().C_emitSizeof(env);
} else { } else {
env.println("result += " + C_fixedSizeof() + ";"); env.println("result += " + C_fixedSizeof() + ";");
...@@ -1338,7 +1353,8 @@ aspect C_Sizeof { ...@@ -1338,7 +1353,8 @@ aspect C_Sizeof {
public void PrimType.C_emitSizeof(C_env env) { public void PrimType.C_emitSizeof(C_env env) {
switch (getToken()) { switch (getToken()) {
case LABCOMM_STRING: { case LABCOMM_STRING: {
env.println("result += 0 + strlen(" + env.qualid + ");"); env.print("{ int length = strlen(" + env.qualid + "); ");
env.println("result += labcomm_size_packed32(length) + length; }");
} break; } break;
default: { default: {
throw new Error(this.getClass().getName() + throw new Error(this.getClass().getName() +
......
...@@ -83,6 +83,7 @@ aspect Python_CodeGen { ...@@ -83,6 +83,7 @@ aspect Python_CodeGen {
env.println("import labcomm"); env.println("import labcomm");
env.println(); env.println();
Python_genTypes(env); Python_genTypes(env);
/* Typedefs curenntly disabled
env.println("typedef = ["); env.println("typedef = [");
env.indent(); env.indent();
for (int i = 0 ; i < getNumDecl() ; i++) { for (int i = 0 ; i < getNumDecl() ; i++) {
...@@ -90,6 +91,7 @@ aspect Python_CodeGen { ...@@ -90,6 +91,7 @@ aspect Python_CodeGen {
} }
env.unindent(); env.unindent();
env.println("]"); env.println("]");
*/
env.println("sample = ["); env.println("sample = [");
env.indent(); env.indent();
for (int i = 0 ; i < getNumDecl() ; i++) { for (int i = 0 ; i < getNumDecl() ; i++) {
...@@ -105,11 +107,7 @@ aspect PythonTypes { ...@@ -105,11 +107,7 @@ aspect PythonTypes {
public void Program.Python_genTypes(Python_env env) { public void Program.Python_genTypes(Python_env env) {
for (int i = 0 ; i < getNumDecl() ; i++) { for (int i = 0 ; i < getNumDecl() ; i++) {
env.println("class " + getDecl(i).getName() + "(object):");
env.indent();
getDecl(i).Python_genSignature(env); getDecl(i).Python_genSignature(env);
env.unindent();
env.println();
} }
} }
...@@ -120,19 +118,29 @@ aspect PythonTypes { ...@@ -120,19 +118,29 @@ aspect PythonTypes {
} }
public void TypeDecl.Python_genSignature(Python_env env) { public void TypeDecl.Python_genSignature(Python_env env) {
/*
env.println("class " + getName() + "(object):");
env.indent();
env.println("signature = labcomm.typedef('" + getName() + "',"); env.println("signature = labcomm.typedef('" + getName() + "',");
env.indent(); env.indent();
getType().Python_genSignature(env); getType().Python_genSignature(env);
env.unindent(); env.unindent();
env.println(")"); env.println(")");
env.unindent();
env.println();
*/
} }
public void SampleDecl.Python_genSignature(Python_env env) { public void SampleDecl.Python_genSignature(Python_env env) {
env.println("class " + getName() + "(object):");
env.indent();
env.println("signature = labcomm.sample('" + getName() + "', "); env.println("signature = labcomm.sample('" + getName() + "', ");
env.indent(); env.indent();
getType().Python_genSignature(env); getType().Python_genSignature(env);
env.unindent(); env.unindent();
env.println(")"); env.println(")");
env.unindent();
env.println();
} }
public void UserType.Python_genSignature(Python_env env) { public void UserType.Python_genSignature(Python_env env) {
......
...@@ -260,3 +260,18 @@ int labcomm_get_local_index(struct labcomm_signature *signature) ...@@ -260,3 +260,18 @@ int labcomm_get_local_index(struct labcomm_signature *signature)
} }
return signature->index; return signature->index;
} }
int labcomm_internal_sizeof(struct labcomm_signature *signature,
void *v)
{
int length = signature->encoded_size(v);
fprintf(stderr, "SIZEOF(%s) = %d %d %d\n",
signature->name,
labcomm_size_packed32(signature->index),
labcomm_size_packed32(length),
length);
return (labcomm_size_packed32(signature->index) +
labcomm_size_packed32(length) +
length);
}
...@@ -43,9 +43,8 @@ struct labcomm_decoder; ...@@ -43,9 +43,8 @@ struct labcomm_decoder;
* Signature entry * Signature entry
*/ */
struct labcomm_signature { struct labcomm_signature {
int type;
char *name; char *name;
int (*encoded_size)(struct labcomm_signature *, void *); // void * == encoded_sample * int (*encoded_size)(void *); /* void* refers to sample_data */
int size; int size;
unsigned char *signature; unsigned char *signature;
int index; int index;
......
...@@ -187,7 +187,7 @@ static int decode_sample(struct labcomm_decoder *d, int kind) ...@@ -187,7 +187,7 @@ static int decode_sample(struct labcomm_decoder *d, int kind)
.error = 0, .error = 0,
}; };
struct labcomm_signature signature, *local_signature; struct labcomm_signature signature, *local_signature;
int remote_index, local_index, err; int remote_index, local_index, err, length;
local_signature = NULL; local_signature = NULL;
local_index = 0; local_index = 0;
...@@ -195,7 +195,8 @@ static int decode_sample(struct labcomm_decoder *d, int kind) ...@@ -195,7 +195,8 @@ static int decode_sample(struct labcomm_decoder *d, int kind)
labcomm_writer_start(&writer, writer.action_context, 0, NULL, NULL); labcomm_writer_start(&writer, writer.action_context, 0, NULL, NULL);
remote_index = labcomm_read_packed32(d->reader); remote_index = labcomm_read_packed32(d->reader);
signature.name = labcomm_read_string(d->reader); signature.name = labcomm_read_string(d->reader);
signature.type = kind; length = labcomm_read_packed32(d->reader);
fprintf(stderr, "SIGNATURE_LENGTH=%d\n", length);
collect_flat_signature(d, &writer); collect_flat_signature(d, &writer);
labcomm_writer_end(&writer, writer.action_context); labcomm_writer_end(&writer, writer.action_context);
err = writer_ioctl(&writer, err = writer_ioctl(&writer,
...@@ -228,7 +229,6 @@ static int decode_sample(struct labcomm_decoder *d, int kind) ...@@ -228,7 +229,6 @@ static int decode_sample(struct labcomm_decoder *d, int kind)
s = LABCOMM_SIGNATURE_ARRAY_REF(d->memory, s = LABCOMM_SIGNATURE_ARRAY_REF(d->memory,
d->local, struct sample_entry, i); d->local, struct sample_entry, i);
if (s->signature && if (s->signature &&
s->signature->type == signature.type &&
s->signature->size == signature.size && s->signature->size == signature.size &&
strcmp(s->signature->name, signature.name) == 0 && strcmp(s->signature->name, signature.name) == 0 &&
memcmp((void*)s->signature->signature, (void*)signature.signature, memcmp((void*)s->signature->signature, (void*)signature.signature,
...@@ -302,7 +302,7 @@ static void reader_alloc(struct labcomm_decoder *d) ...@@ -302,7 +302,7 @@ static void reader_alloc(struct labcomm_decoder *d)
int labcomm_decoder_decode_one(struct labcomm_decoder *d) int labcomm_decoder_decode_one(struct labcomm_decoder *d)
{ {
int result, remote_index; int result, remote_index, length;
reader_alloc(d); reader_alloc(d);
remote_index = labcomm_read_packed32(d->reader); remote_index = labcomm_read_packed32(d->reader);
...@@ -310,6 +310,12 @@ int labcomm_decoder_decode_one(struct labcomm_decoder *d) ...@@ -310,6 +310,12 @@ int labcomm_decoder_decode_one(struct labcomm_decoder *d)
result = d->reader->error; result = d->reader->error;
goto out; goto out;
} }
length = labcomm_read_packed32(d->reader);
if (d->reader->error < 0) {
result = d->reader->error;
goto out;
}
fprintf(stderr, "LENGTH=%d\n", length);
if (remote_index == LABCOMM_SAMPLE) { if (remote_index == LABCOMM_SAMPLE) {
result = decode_sample(d, remote_index); result = decode_sample(d, remote_index);
} else { } else {
......
...@@ -75,11 +75,10 @@ int labcomm_internal_encoder_register( ...@@ -75,11 +75,10 @@ int labcomm_internal_encoder_register(
labcomm_encoder_function encode) labcomm_encoder_function encode)
{ {
int result = -EINVAL; int result = -EINVAL;
int index, *done, err, i; int index, *done, err, i, length;
index = labcomm_get_local_index(signature); index = labcomm_get_local_index(signature);
labcomm_scheduler_writer_lock(e->scheduler); labcomm_scheduler_writer_lock(e->scheduler);
if (signature->type != LABCOMM_SAMPLE) { goto out; }
if (index <= 0) { goto out; } if (index <= 0) { goto out; }
done = LABCOMM_SIGNATURE_ARRAY_REF(e->memory, e->registered, int, index); done = LABCOMM_SIGNATURE_ARRAY_REF(e->memory, e->registered, int, index);
if (*done) { goto out; } if (*done) { goto out; }
...@@ -88,9 +87,26 @@ int labcomm_internal_encoder_register( ...@@ -88,9 +87,26 @@ int labcomm_internal_encoder_register(
index, signature, NULL); index, signature, NULL);
if (err == -EALREADY) { result = 0; goto out; } if (err == -EALREADY) { result = 0; goto out; }
if (err != 0) { result = err; goto out; } if (err != 0) { result = err; goto out; }
labcomm_write_packed32(e->writer, signature->type); labcomm_write_packed32(e->writer, LABCOMM_SAMPLE);
length = (labcomm_size_packed32(index) +
labcomm_size_string(signature->name) +
labcomm_size_packed32(signature->size) +
signature->size);
{
fprintf(stderr, "LENGTH(%s, %d)=%d %d %d %d -> %d\n",
signature->name, index,
labcomm_size_packed32(index),
labcomm_size_string(signature->name),
labcomm_size_packed32(signature->size),
signature->size,
length);
}
labcomm_write_packed32(e->writer, length);
labcomm_write_packed32(e->writer, index); labcomm_write_packed32(e->writer, index);
labcomm_write_string(e->writer, signature->name); labcomm_write_string(e->writer, signature->name);
labcomm_write_packed32(e->writer, signature->size);
for (i = 0 ; i < signature->size ; i++) { for (i = 0 ; i < signature->size ; i++) {
if (e->writer->pos >= e->writer->count) { if (e->writer->pos >= e->writer->count) {
labcomm_writer_flush(e->writer, e->writer->action_context); labcomm_writer_flush(e->writer, e->writer->action_context);
...@@ -111,16 +127,25 @@ int labcomm_internal_encode( ...@@ -111,16 +127,25 @@ int labcomm_internal_encode(
labcomm_encoder_function encode, labcomm_encoder_function encode,
void *value) void *value)
{ {
int result; int result, index, length;
int index;
index = labcomm_get_local_index(signature); index = labcomm_get_local_index(signature);
/* FIXME: try to gt rid of labcomm_size_packed32(index) in
labcomm_sizeof_* (since that calculation is currently
wrong [length field not accounted for]) */
length = (signature->encoded_size(value));
{
fprintf(stderr, "LENGTH(%s, %d)=%d\n",
signature->name, index,
length);
}
labcomm_scheduler_writer_lock(e->scheduler); labcomm_scheduler_writer_lock(e->scheduler);
result = labcomm_writer_start(e->writer, e->writer->action_context, result = labcomm_writer_start(e->writer, e->writer->action_context,
index, signature, value); index, signature, value);
if (result == -EALREADY) { result = 0; goto no_end; } if (result == -EALREADY) { result = 0; goto no_end; }
if (result != 0) { goto out; } if (result != 0) { goto out; }
result = labcomm_write_packed32(e->writer, index); result = labcomm_write_packed32(e->writer, index);
result = labcomm_write_packed32(e->writer, length);
if (result != 0) { goto out; } if (result != 0) { goto out; }
result = encode(e->writer, value); result = encode(e->writer, value);
out: out:
......
...@@ -377,6 +377,9 @@ int labcomm_internal_encoder_ioctl(struct labcomm_encoder *encoder, ...@@ -377,6 +377,9 @@ int labcomm_internal_encoder_ioctl(struct labcomm_encoder *encoder,
struct labcomm_signature *signature, struct labcomm_signature *signature,
uint32_t ioctl_action, va_list args); uint32_t ioctl_action, va_list args);
int labcomm_internal_sizeof(struct labcomm_signature *signature,
void *v);
#if __BYTE_ORDER == __LITTLE_ENDIAN #if __BYTE_ORDER == __LITTLE_ENDIAN
#define LABCOMM_ENCODE(name, type) \ #define LABCOMM_ENCODE(name, type) \
...@@ -445,7 +448,7 @@ static inline int labcomm_write_string(struct labcomm_writer *w, char *s) ...@@ -445,7 +448,7 @@ static inline int labcomm_write_string(struct labcomm_writer *w, char *s)
{ {
int length, i, err; int length, i, err;
length = strlen((char*)s); length = strlen(s);
err = labcomm_write_packed32(w, length); err = labcomm_write_packed32(w, length);
if (err != 0) { return err; } if (err != 0) { return err; }
for (i = 0 ; i < length ; i++) { for (i = 0 ; i < length ; i++) {
...@@ -473,6 +476,13 @@ static inline int labcomm_size_packed32(unsigned int data) ...@@ -473,6 +476,13 @@ static inline int labcomm_size_packed32(unsigned int data)
} }
static inline int labcomm_size_string(char *s)
{
int length = strlen(s);
return labcomm_size_packed32(length) + length;
}
/* /*
* Macros for handling arrays indexed by signature index * Macros for handling arrays indexed by signature index
*/ */
......
...@@ -193,19 +193,19 @@ int main(void) ...@@ -193,19 +193,19 @@ int main(void)
labcomm_encoder_register_test_sample_test_var(encoder); labcomm_encoder_register_test_sample_test_var(encoder);
err = test_decode_one(decoder); err = test_decode_one(decoder);
fprintf(stderr, "decode of register -> index %d\n", err); fprintf(stderr, "decode of register -> index %d\n", err);
test_encode_decode(encoder, decoder, 7, 1, 1); test_encode_decode(encoder, decoder, 8, 1, 1);
if (decoder_var.a[0] != encoder_var.a[0]) { if (decoder_var.a[0] != encoder_var.a[0]) {
fprintf(stderr, "Failed to decode correct value %d != %d\n", fprintf(stderr, "Failed to decode correct value %d != %d\n",
encoder_var.a[0], decoder_var.a[0]); encoder_var.a[0], decoder_var.a[0]);
exit(1); exit(1);
} }
test_encode_decode(encoder, decoder, 19, 2, 2); test_encode_decode(encoder, decoder, 20, 2, 2);
test_encode_decode(encoder, decoder, 3, 0, 0); test_encode_decode(encoder, decoder, 4, 0, 0);
for (i = 1 ; i <= 4 ; i++) { for (i = 1 ; i <= 4 ; i++) {
test_encode_decode(encoder, decoder, 2+i, 0, (1<<(7*i))-1); test_encode_decode(encoder, decoder, 3+i, 0, (1<<(7*i))-1);
test_encode_decode(encoder, decoder, 3+i, 0, (1<<(7*i))); test_encode_decode(encoder, decoder, 4+i, 0, (1<<(7*i)));
} }
test_encode_decode(encoder, decoder, 7, 0, 4294967295); test_encode_decode(encoder, decoder, 8, 0, 4294967295);
return 0; return 0;
} }
......
...@@ -190,23 +190,25 @@ int main(void) ...@@ -190,23 +190,25 @@ int main(void)
labcomm_pthread_scheduler_new(labcomm_default_memory)); labcomm_pthread_scheduler_new(labcomm_default_memory));
labcomm_encoder_ioctl(encoder, IOCTL_WRITER_RESET); labcomm_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
/* Register twice to make sure that only one registration gets encoded */
labcomm_encoder_register_generated_encoding_V(encoder); labcomm_encoder_register_generated_encoding_V(encoder);
labcomm_encoder_register_generated_encoding_V(encoder); labcomm_encoder_register_generated_encoding_V(encoder);
EXPECT({ 0x02, -1, 0x01, 'V', 0x11, 0x00 }); EXPECT({ 0x02, 0x06, -1, 0x01, 'V', 0x02, 0x11, 0x00 });
labcomm_encoder_ioctl(encoder, IOCTL_WRITER_RESET); labcomm_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
/* Register twice to make sure that only one registration gets encoded */
labcomm_encoder_register_generated_encoding_B(encoder); labcomm_encoder_register_generated_encoding_B(encoder);
labcomm_encoder_register_generated_encoding_B(encoder); labcomm_encoder_register_generated_encoding_B(encoder);
EXPECT({0x02, -1, 0x01, 'B', 0x21}); EXPECT({0x02, 0x05, -1, 0x01, 'B',0x01, 0x21});
labcomm_encoder_ioctl(encoder, IOCTL_WRITER_RESET); labcomm_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
// was: labcomm_encode_generated_encoding_V(encoder, &V); // was: labcomm_encode_generated_encoding_V(encoder, &V);
labcomm_encode_generated_encoding_V(encoder); labcomm_encode_generated_encoding_V(encoder);
EXPECT({-1}); EXPECT({-1, 0x00 });
labcomm_encoder_ioctl(encoder, IOCTL_WRITER_RESET); labcomm_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
labcomm_encode_generated_encoding_B(encoder, &B); labcomm_encode_generated_encoding_B(encoder, &B);
EXPECT({-1, 1}); EXPECT({-1, 0x01, 1});
return 0; return 0;
} }
......
...@@ -24,13 +24,22 @@ namespace se.lth.control.labcomm { ...@@ -24,13 +24,22 @@ namespace se.lth.control.labcomm {
bool done = false; bool done = false;
while (!done) { while (!done) {
int tag = decodePacked32(); int tag = decodePacked32();
int length = decodePacked32();
Console.Error.WriteLine(" tag=" + tag + "length=" + length);
switch (tag) { switch (tag) {
case LabComm.SAMPLE: { case LabComm.SAMPLE: {
int index = decodePacked32(); int index = decodePacked32();
String name = decodeString(); String name = decodeString();
MemoryStream signature = new MemoryStream(); int signature_length = decodePacked32();
collectFlatSignature(new LabCommEncoderChannel(signature, false)); // MemoryStream signature = new MemoryStream();
registry.add(index, name, signature.ToArray()); byte[] signature = new byte[signature_length];
// collectFlatSignature(new LabCommEncoderChannel(signature, false));
ReadBytes(signature, signature_length);
Console.Error.WriteLine("REMOTE:" + name + " " +
signature_length + " " +
signature);
// registry.add(index, name, signature.ToArray());
registry.add(index, name, signature);
} break; } break;
default: { default: {
LabCommDecoderRegistry.Entry e = registry.get(tag); LabCommDecoderRegistry.Entry e = registry.get(tag);
......
...@@ -11,11 +11,15 @@ namespace se.lth.control.labcomm { ...@@ -11,11 +11,15 @@ namespace se.lth.control.labcomm {
private MemoryStream bytes = new MemoryStream(); private MemoryStream bytes = new MemoryStream();
private LabCommEncoderRegistry registry = new LabCommEncoderRegistry(); private LabCommEncoderRegistry registry = new LabCommEncoderRegistry();
byte[] buf = new byte[8]; byte[] buf = new byte[8];
private int current_tag;
public LabCommEncoderChannel(Stream writer, bool emitVersion) { public LabCommEncoderChannel(Stream writer, bool emitVersion) {
this.writer = writer; this.writer = writer;
if (emitVersion) { if (emitVersion) {
encodeString(LabComm.VERSION); encodeString(LabComm.VERSION);
bytes.WriteTo(writer);
bytes.SetLength(0);
writer.Flush();
} }
} }
...@@ -24,26 +28,51 @@ namespace se.lth.control.labcomm { ...@@ -24,26 +28,51 @@ namespace se.lth.control.labcomm {
public void register(LabCommDispatcher dispatcher) { public void register(LabCommDispatcher dispatcher) {
int index = registry.add(dispatcher); int index = registry.add(dispatcher);
encodePacked32(LabComm.SAMPLE); begin(LabComm.SAMPLE);
encodePacked32(index); encodePacked32(index);
encodeString(dispatcher.getName()); encodeString(dispatcher.getName());
byte[] signature = dispatcher.getSignature(); byte[] signature = dispatcher.getSignature();
encodePacked32(signature.Length);
for (int i = 0 ; i < signature.Length ; i++) { for (int i = 0 ; i < signature.Length ; i++) {
encodeByte(signature[i]); encodeByte(signature[i]);
} }
end(null); end(null);
} }
private void begin(int tag) {
current_tag = tag;
bytes.SetLength(0);
Console.Error.WriteLine("BEGIN CURRENT=" + current_tag + " TAG=" + tag +
"LENGTH=" + bytes.Length);
}