Commit 34f41a65 authored by Sven Gestegård Robertz's avatar Sven Gestegård Robertz
Browse files

preliminary code for BIND_SELF in C lib

parent e016a25c
......@@ -1140,15 +1140,20 @@ aspect C_Encoder {
env.println("{");
env.indent();
env.println("//TODO: add error handling for dependencies");
C_emitUserTypeDeps(env, null, true); //XXX HERE BE DRAGONS
//currently set to false to turn off
//outputting of code
env.println("return labcomm"+env.verStr+"_internal_encoder_type_register(");
env.indent();
env.println("e,");
env.println("&signature_" + env.prefix + getName() + "");
env.unindent();
env.println(");");
C_emitUserTypeDeps(env, null, true);
if(!isSampleDecl() || hasDependencies()) {
env.println("return labcomm"+env.verStr+"_internal_encoder_type_register(");
env.indent();
env.println("e,");
env.println("&signature_" + env.prefix + getName() + "");
env.unindent();
env.println(");");
} else {
env.println("// the type has no dependencies, do nothing");
env.println("return 0;");
}
env.unindent();
env.println("}");
}
......@@ -1176,11 +1181,16 @@ aspect C_Encoder {
env.println("(labcomm"+env.verStr+"_encoder_function)encode_" + env.prefix + getName());
env.unindent();
env.println(");");
env.println("if(result >= 0) {\n");
env.indent();
env.println("labcomm"+env.verStr+"_encoder_type_register_" + env.prefix + getName()+"(e);");
env.println("labcomm"+env.verStr+"_internal_encoder_type_bind(");
env.indent();
env.println("e,");
env.println("&signature_" + env.prefix + getName() + ");");
env.println("&signature_" + env.prefix + getName() + ",");
env.println( (hasDependencies() ? "1" : "0") + ");");
env.unindent();
env.println("}");
env.unindent();
env.println("return result;");
env.unindent();
......
......@@ -7,7 +7,7 @@ using se.lth.control.labcomm;
namespace user_types
{
class Decoder : twoLines.Handler
class Decoder : twoLines.Handler, twoInts.Handler
{
DecoderChannel dec;
......@@ -15,6 +15,7 @@ namespace user_types
{
dec = new DecoderChannel(stream);
twoLines.register(dec, this);
twoInts.register(dec, this);
try
{
Console.WriteLine("Running decoder.");
......@@ -41,6 +42,13 @@ namespace user_types
Console.WriteLine("Line l2: " + genLine(d.l2));
}
public void handle(twoInts d)
{
Console.WriteLine("Got twoInts: ");
Console.WriteLine("a: "+d.a);
Console.WriteLine("b: "+d.b);
}
static void Main(string[] args)
{
new Decoder(new FileStream(args[0], FileMode.Open));
......
......@@ -197,7 +197,6 @@ static int decode_type_binding(struct labcomm_decoder *d, int kind)
}
printf("type_binding: 0x%x -> 0x%x\n", sample_index, typedef_index);
out:
fprintf(stderr,"[type_binding: %d]", result);
return result;
}
static int decode_type_def(struct labcomm_decoder *d, int kind){
......@@ -456,7 +455,6 @@ int labcomm_decoder_decode_one(struct labcomm_decoder *d)
result = decode_and_handle(d, d, remote_index);
}
out:
fprintf(stderr, "\n decode_one returns %d\n", result);
return result;
}
......
......@@ -65,11 +65,11 @@ static struct labcomm_encoder *internal_encoder_new(
labcomm_writer_alloc(result->writer,
result->writer->action_context);
if(outputVer) {
labcomm_writer_start(result->writer,
result->writer->action_context,
labcomm_writer_start(result->writer,
result->writer->action_context,
LABCOMM_VERSION, NULL, CURRENT_VERSION);
labcomm_write_packed32(result->writer, LABCOMM_VERSION);
#ifdef LENGTH_INCL_TAG
#ifdef LENGTH_INCL_TAG
length = (labcomm_size_packed32(LABCOMM_VERSION) +
labcomm_size_string(CURRENT_VERSION));
#else
......@@ -98,6 +98,7 @@ void labcomm_encoder_free(struct labcomm_encoder* e)
labcomm_writer_free(e->writer, e->writer->action_context);
LABCOMM_SIGNATURE_ARRAY_FREE(e->memory, e->registered, int);
LABCOMM_SIGNATURE_ARRAY_FREE(e->memory, e->sample_ref, int);
LABCOMM_SIGNATURE_ARRAY_FREE(e->memory, e->typedefs, int);
labcomm_memory_free(memory, 0, e);
}
//================
......@@ -149,9 +150,9 @@ int wrapped_end(struct labcomm_encoder *e, int tag, struct labcomm_encoder* wrap
&len);
if (err < 0) {
// HERE BE DRAGONS!
// HERE BE DRAGONS!
// What is the difference between error_handler (where is it defined?)
// and error_handler_callback. And why is the latter only in
// and error_handler_callback. And why is the latter only in
// the decoder struct?
//
// wrapped->on_error(LABCOMM_ERROR_BAD_WRITER, 2,
......@@ -170,9 +171,9 @@ int wrapped_end(struct labcomm_encoder *e, int tag, struct labcomm_encoder* wrap
err = -ENOENT;
goto free_encoder;
}
{
{
int i;
err = labcomm_writer_start(e->writer, e->writer->action_context,
err = labcomm_writer_start(e->writer, e->writer->action_context,
LABCOMM_TYPE_DEF, NULL, NULL);
if(err < 0) {
goto free_encoder;
......@@ -186,7 +187,7 @@ int wrapped_end(struct labcomm_encoder *e, int tag, struct labcomm_encoder* wrap
err = e->writer->error;
}
free_encoder:
//labcomm_memory_free(wrapped->memory, 1, ctx);
//labcomm_memory_free(wrapped->memory, 1, ctx);
labcomm_encoder_free(wrapped);
return err;
}
......@@ -319,28 +320,29 @@ int labcomm_internal_encoder_type_register(
#ifndef WITHOUT_TYPE_DEFS
struct labcomm_encoder *w = wrapped_begin(e);
internal_reg_type(w, signature, FALSE);
return wrapped_end(e, LABCOMM_TYPE_DEF, w);
return wrapped_end(e, LABCOMM_TYPE_DEF, w);
#else
return 0;
#endif
}
int labcomm_internal_encoder_type_bind(
struct labcomm_encoder *e,
const struct labcomm_signature *signature)
const struct labcomm_signature *signature,
char has_deps)
{
#ifndef WITHOUT_TYPE_DEFS
#ifndef WITHOUT_TYPE_DEFS
int result = -EINVAL;
int err;
int sindex = labcomm_get_local_index(signature);
int tindex = labcomm_get_local_type_index(signature);
int tindex = has_deps ? labcomm_get_local_type_index(signature) : LABCOMM_BIND_SELF;
labcomm_scheduler_writer_lock(e->scheduler);
if(sindex <= 0 || tindex <= 0) {goto out;}
err = labcomm_writer_start(e->writer, e->writer->action_context,
if(sindex <= 0 || (has_deps && tindex <= 0)) {goto out;}
err = labcomm_writer_start(e->writer, e->writer->action_context,
LABCOMM_TYPE_BINDING, signature, NULL);
if (err == -EALREADY) { result = 0; goto out; }
if (err != 0) { result = err; goto out; }
int length = (labcomm_size_packed32(sindex) +
labcomm_size_packed32(tindex));
labcomm_size_packed32(tindex));
labcomm_write_packed32(e->writer, LABCOMM_TYPE_BINDING);
labcomm_write_packed32(e->writer, length);
labcomm_write_packed32(e->writer, sindex);
......@@ -367,9 +369,10 @@ int labcomm_internal_encoder_register(
labcomm_scheduler_writer_lock(e->scheduler);
if (index <= 0) { goto out; }
done = LABCOMM_SIGNATURE_ARRAY_REF(e->memory, e->registered, int, index);
if (*done) { goto out; }
*done = 1;
err = labcomm_writer_start(e->writer, e->writer->action_context,
if (*done) {
goto out; }
*done = 1;
err = labcomm_writer_start(e->writer, e->writer->action_context,
index, signature, NULL);
if (err == -EALREADY) { result = 0; goto out; }
if (err != 0) { result = err; goto out; }
......@@ -407,7 +410,7 @@ int labcomm_internal_encode(
index = labcomm_get_local_index(signature);
length = (signature->encoded_size(value));
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);
if (result == -EALREADY) { result = 0; goto no_end; }
if (result != 0) { goto out; }
......@@ -435,8 +438,8 @@ int labcomm_encoder_sample_ref_register(
done = LABCOMM_SIGNATURE_ARRAY_REF(e->memory, e->sample_ref, int, index);
if (*done) { goto out; }
*done = 1;
err = labcomm_writer_start(e->writer, e->writer->action_context,
*done = 1;
err = labcomm_writer_start(e->writer, e->writer->action_context,
index, signature, NULL);
if (err == -EALREADY) { result = 0; goto out; }
if (err != 0) { result = err; goto out; }
......@@ -463,20 +466,20 @@ out:
return result;
}
int labcomm_encoder_ioctl(struct labcomm_encoder *encoder,
int labcomm_encoder_ioctl(struct labcomm_encoder *encoder,
uint32_t action,
...)
{
int result;
va_list va;
if (LABCOMM_IOC_SIG(action) != LABCOMM_IOC_NOSIG) {
result = -EINVAL;
goto out;
}
va_start(va, action);
result = labcomm_writer_ioctl(encoder->writer,
result = labcomm_writer_ioctl(encoder->writer,
encoder->writer->action_context,
0, NULL, action, va);
va_end(va);
......@@ -485,7 +488,7 @@ out:
return result;
}
int labcomm_internal_encoder_ioctl(struct labcomm_encoder *encoder,
int labcomm_internal_encoder_ioctl(struct labcomm_encoder *encoder,
const struct labcomm_signature *signature,
uint32_t action, va_list va)
{
......@@ -493,8 +496,8 @@ int labcomm_internal_encoder_ioctl(struct labcomm_encoder *encoder,
int index;
index = labcomm_get_local_index(signature);
result = labcomm_writer_ioctl(encoder->writer,
encoder->writer->action_context,
result = labcomm_writer_ioctl(encoder->writer,
encoder->writer->action_context,
index, signature, action, va);
return result;
}
......
......@@ -65,6 +65,11 @@
#define LABCOMM_DOUBLE 0x26
#define LABCOMM_STRING 0x27
/*
* other special values
*/
#define LABCOMM_BIND_SELF 0
/*
* Macro to automagically call constructors in modules compiled
......@@ -370,7 +375,8 @@ int labcomm_internal_encoder_type_register(
int labcomm_internal_encoder_type_bind(
struct labcomm_encoder *e,
const struct labcomm_signature *signature);
const struct labcomm_signature *signature,
char has_deps);
int labcomm_internal_encoder_register(
struct labcomm_encoder *encoder,
......
......@@ -252,7 +252,9 @@ int main(void)
NULL);
struct expect expect_registration[] = {
{ "Sampledef", -1, test_decode_one, decoder },
#ifdef SHOULD_THIS_BE_REMOVED
{ "Typedef", -1, test_decode_one, decoder },
#endif
{ "Binding", -1, test_decode_one, decoder },
{ 0, 0, 0 }
};
......
......@@ -214,24 +214,21 @@ int main(void)
labcomm_encoder_register_generated_encoding_V(encoder);
labcomm_encoder_register_generated_encoding_V(encoder);
EXPECT({ 0x02, 0x06, VARIABLE(0), 0x01, 'V', 0x02, 0x11, 0x00,
0x04, 0x06, VARIABLE(1), 0x01, 'V', 0x02, 0x11, 0x00,
0x05, 0x02, VARIABLE(0), VARIABLE(1) });
0x05, 0x02, VARIABLE(0), LABCOMM_BIND_SELF });
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);
EXPECT({ 0x02, 0x05, VARIABLE(2), 0x01, 'B', 0x01, 0x21,
0x04, 0x05, VARIABLE(3), 0x01, 'B', 0x01, 0x21,
0x05, 0x02, VARIABLE(2), VARIABLE(3) });
0x05, 0x02, VARIABLE(2), LABCOMM_BIND_SELF });
labcomm_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
/* Register twice to make sure that only one registration gets encoded */
labcomm_encoder_register_generated_encoding_R(encoder);
labcomm_encoder_register_generated_encoding_R(encoder);
EXPECT({ 0x02, 0x08, VARIABLE(4), 0x01, 'R', 0x04, 0x10, 0x01, 0x04, 0x28,
0x04, 0x08, VARIABLE(5), 0x01, 'R', 0x04, 0x10, 0x01, 0x04, 0x28,
0x05, 0x02, VARIABLE(4), VARIABLE(5) });
0x05, 0x02, VARIABLE(4), LABCOMM_BIND_SELF });
labcomm_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
/* Register twice to make sure that only one registration gets encoded */
......
Markdown is supported
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