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

cleanup, removing dead code and debug output

parent 35934d55
...@@ -26,6 +26,18 @@ ...@@ -26,6 +26,18 @@
#include "labcomm_ioctl.h" #include "labcomm_ioctl.h"
#include "labcomm_dynamic_buffer_writer.h" #include "labcomm_dynamic_buffer_writer.h"
#ifdef DEBUG
#define DEBUG_FPRINTF(str, ...) fprintf(str, ##__VA_ARGS__)
#else
#define DEBUG_FPRINTF(str, ...)
#endif
#ifdef DECODER_DEBUG
#define DECODER_DEBUG_FPRINTF(str, ...) fprintf(str, ##__VA_ARGS__)
#else
#define DECODER_DEBUG_FPRINTF(str, ...)
#endif
struct sample_entry { struct sample_entry {
int remote_index; int remote_index;
const struct labcomm_signature *signature; const struct labcomm_signature *signature;
...@@ -169,92 +181,19 @@ static int handle_sample_ref(struct labcomm_decoder *d, int remote_index, ...@@ -169,92 +181,19 @@ static int handle_sample_ref(struct labcomm_decoder *d, int remote_index,
static int decoder_skip(struct labcomm_decoder *d, int len, int tag) static int decoder_skip(struct labcomm_decoder *d, int len, int tag)
{ {
int i; int i;
#ifdef LABCOMM_DECODER_DEBUG DECODER_DEBUG_FPRINTF(stdout, "got tag 0x%x, skipping %d bytes\n", tag, len);
printf("got tag 0x%x, skipping %d bytes\n", tag, len);
#endif
for(i = 0; i <len; i++){ for(i = 0; i <len; i++){
#ifdef LABCOMM_DECODER_DEBUG DECODER_DEBUG_FPRINTF(stderr, ".");
fprintf(stderr,".");
#endif
labcomm_read_byte(d->reader); labcomm_read_byte(d->reader);
if (d->reader->error < 0) { if (d->reader->error < 0) {
#ifdef LABCOMM_DECODER_DEBUG DECODER_DEBUG_FPRINTF(stderr, "\nerror while skipping: %d\n", d->reader->error);
fprintf(stderr, "\nerror while skipping: %d\n", d->reader->error);
#endif
return d->reader->error; return d->reader->error;
} }
} }
#ifdef LABCOMM_DECODER_DEBUG DECODER_DEBUG_FPRINTF(stderr, "\n");
fprintf(stderr, "\n");
#endif
return tag; return tag;
} }
#ifdef OLD_type_def_DECODING_TEST_CODE
static int decode_type_binding(struct labcomm_decoder *d, int kind)
{
int result;
int sample_index = result = labcomm_read_packed32(d->reader);
if(d->reader->error < 0 ) {
result = d->reader->error;
goto out;
}
int type_def_index = labcomm_read_packed32(d->reader);
if(d->reader->error < 0 ) {
result = d->reader->error;
goto out;
}
#ifdef LABCOMM_DECODER_DEBUG
printf("type_binding: 0x%x -> 0x%x\n", sample_index, type_def_index);
#endif
out:
return result;
}
static int decode_type_def(struct labcomm_decoder *d, int kind){
int i, remote_index, result;
char *name;
int size;
remote_index = labcomm_read_packed32(d->reader);
result = remote_index;
if (d->reader->error < 0) {
result = d->reader->error;
goto out;
}
name = labcomm_read_string(d->reader);
if (d->reader->error < 0) {
result = d->reader->error;
goto out;
}
size = labcomm_read_packed32(d->reader);
if (d->reader->error < 0) {
result = d->reader->error;
goto free_signature_name;
}
//printf("got type_def 0x%x : %s, skipping %d signature bytes", remote_index, name, size);
#if 0
signature.signature = labcomm_memory_alloc(d->memory, 1, signature.size);
if (d->reader->error < 0) {
result = d->reader->error;
goto free_signature_name;
}
#endif
for (i = 0 ; i < size ; i++) {
/*signature.signature[i] =*/ labcomm_read_byte(d->reader);
if (d->reader->error < 0) {
result = d->reader->error;
goto free_signature_signature;
}
}
free_signature_signature:
// labcomm_memory_free(d->memory, 1, signature.signature);
free_signature_name:
labcomm_memory_free(d->memory, 0, name);
out:
return result;
}
#endif
static int decode_sample_def_or_ref(struct labcomm_decoder *d, int kind) static int decode_sample_def_or_ref(struct labcomm_decoder *d, int kind)
{ {
int result; int result;
...@@ -449,9 +388,7 @@ int labcomm_decoder_decode_one(struct labcomm_decoder *d) ...@@ -449,9 +388,7 @@ int labcomm_decoder_decode_one(struct labcomm_decoder *d)
} }
labcomm_memory_free(d->memory, 1, version); labcomm_memory_free(d->memory, 1, version);
} else if (! d->version_ok) { } else if (! d->version_ok) {
#ifdef LABCOMM_DECODER_DEBUG DEBUG_FPRINTF(stderr, "No VERSION %d %d\n", remote_index, length);
fprintf(stderr, "No VERSION %d %d\n", remote_index, length);
#endif
result = -ECONNRESET; result = -ECONNRESET;
} else if (remote_index == LABCOMM_SAMPLE_DEF) { } else if (remote_index == LABCOMM_SAMPLE_DEF) {
result = decode_sample_def_or_ref(d, LABCOMM_SAMPLE_DEF); result = decode_sample_def_or_ref(d, LABCOMM_SAMPLE_DEF);
...@@ -461,28 +398,18 @@ int labcomm_decoder_decode_one(struct labcomm_decoder *d) ...@@ -461,28 +398,18 @@ int labcomm_decoder_decode_one(struct labcomm_decoder *d)
result = decode_and_handle(d, d, remote_index); result = decode_and_handle(d, d, remote_index);
if(result == -ENOENT) { if(result == -ENOENT) {
//No handler for type_defs, skip //No handler for type_defs, skip
#ifdef OLD_type_def_DECODING_TEST_CODE
result = decode_type_def(d, LABCOMM_TYPE_DEF);
#else
result = decoder_skip(d, length, remote_index); result = decoder_skip(d, length, remote_index);
#endif
} }
} else if (remote_index == LABCOMM_TYPE_BINDING) { } else if (remote_index == LABCOMM_TYPE_BINDING) {
result = decode_and_handle(d, d, remote_index); result = decode_and_handle(d, d, remote_index);
if(result == -ENOENT) { if(result == -ENOENT) {
//No handler for type_bindings, skip //No handler for type_bindings, skip
#ifdef OLD_type_def_DECODING_TEST_CODE
result = decode_type_binding(d, LABCOMM_TYPE_BINDING);
#else
result = decoder_skip(d, length, remote_index); result = decoder_skip(d, length, remote_index);
#endif
} }
} else if (remote_index == LABCOMM_PRAGMA) { } else if (remote_index == LABCOMM_PRAGMA) {
result = decode_pragma(d, d, length); result = decode_pragma(d, d, length);
} else if (remote_index < LABCOMM_USER) { } else if (remote_index < LABCOMM_USER) {
#ifdef LABCOMM_DECODER_DEBUG DECODER_DEBUG_FPRINTF(stderr, "SKIP %d %d\n", remote_index, length);
fprintf(stderr, "SKIP %d %d\n", remote_index, length);
#endif
result = decoder_skip(d, length, remote_index); result = decoder_skip(d, length, remote_index);
} else { } else {
result = decode_and_handle(d, d, remote_index); result = decode_and_handle(d, d, remote_index);
......
...@@ -26,6 +26,9 @@ ...@@ -26,6 +26,9 @@
#include "labcomm_ioctl.h" #include "labcomm_ioctl.h"
#include "labcomm_dynamic_buffer_writer.h" #include "labcomm_dynamic_buffer_writer.h"
//define the following to disable encoding of typedefs
#undef LABCOMM_WITHOUT_TYPE_DEFS
struct labcomm_encoder { struct labcomm_encoder {
struct labcomm_writer *writer; struct labcomm_writer *writer;
struct labcomm_error_handler *error; struct labcomm_error_handler *error;
...@@ -97,134 +100,6 @@ void labcomm_encoder_free(struct labcomm_encoder* e) ...@@ -97,134 +100,6 @@ void labcomm_encoder_free(struct labcomm_encoder* e)
labcomm_memory_free(memory, 0, e); labcomm_memory_free(memory, 0, e);
} }
#ifndef LABCOMM_WITHOUT_TYPE_DEFS
static void write_sig_tree_byte(char b, const struct labcomm_signature *signature,
void *context)
{
struct labcomm_encoder *e = context;
if(signature) {
labcomm_write_packed32(e->writer, labcomm_get_local_index(signature));
}else {
if (e->writer->pos >= e->writer->count) {
labcomm_writer_flush(e->writer, e->writer->action_context);
}
e->writer->data[e->writer->pos] = b;
e->writer->pos++;
}
}
static void do_write_signature(struct labcomm_encoder * e,
const struct labcomm_signature *signature,
unsigned char flatten)
{
map_signature(write_sig_tree_byte, e, signature, flatten);
}
static void sig_size(char b, const struct labcomm_signature *signature,
void *context)
{
int *result = context;
int diff;
if(signature) {
int idx = labcomm_get_local_index(signature);
diff = labcomm_size_packed32(idx);
}else {
diff = 1;
}
(*result)+=diff;
}
static int calc_sig_encoded_size(struct labcomm_encoder *e,
const struct labcomm_signature *sig)
{
int result=0;
map_signature(sig_size, &result, sig, FALSE);
return result;
}
static int internal_reg_type(
struct labcomm_encoder *e,
const struct labcomm_signature *signature,
labcomm_bool flatten)
{
int result = -EINVAL;
int index, *done, err;
index = labcomm_get_local_index(signature);
labcomm_scheduler_writer_lock(e->scheduler);
if (index <= 0) { goto out; }
done = LABCOMM_SIGNATURE_ARRAY_REF(e->memory, e->typedefs, int, index);
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; }
int sig_size = calc_sig_encoded_size(e, signature);
int len = labcomm_size_packed32(index) +
labcomm_size_string(signature->name) +
labcomm_size_packed32(sig_size) +
sig_size;
labcomm_write_packed32(e->writer, LABCOMM_TYPE_DEF);
labcomm_write_packed32(e->writer, len);
labcomm_write_packed32(e->writer, index);
labcomm_write_string(e->writer, signature->name);
labcomm_write_packed32(e->writer, sig_size);
do_write_signature(e, signature, FALSE);
labcomm_writer_end(e->writer, e->writer->action_context);
result = e->writer->error;
out:
labcomm_scheduler_writer_unlock(e->scheduler);
return result;
}
#endif
int labcomm_internal_encoder_type_register(
struct labcomm_encoder *e,
const struct labcomm_signature *signature)
{
#ifndef WITHOUT_TYPE_DEFS
return internal_reg_type(e, signature, FALSE);
#else
return 0;
#endif
}
int labcomm_internal_encoder_type_bind(
struct labcomm_encoder *e,
const struct labcomm_signature *signature,
char has_deps)
{
#ifndef WITHOUT_TYPE_DEFS
int result = -EINVAL;
int err;
int sindex = labcomm_get_local_index(signature);
int tindex = has_deps ? labcomm_get_local_type_index(signature) : LABCOMM_BIND_SELF;
labcomm_scheduler_writer_lock(e->scheduler);
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_write_packed32(e->writer, LABCOMM_TYPE_BINDING);
labcomm_write_packed32(e->writer, length);
labcomm_write_packed32(e->writer, sindex);
labcomm_write_packed32(e->writer, tindex);
labcomm_writer_end(e->writer, e->writer->action_context);
result = e->writer->error;
out:
labcomm_scheduler_writer_unlock(e->scheduler);
return result;
#else
return 0;
#endif
}
int labcomm_internal_encoder_register( int labcomm_internal_encoder_register(
struct labcomm_encoder *e, struct labcomm_encoder *e,
const struct labcomm_signature *signature, const struct labcomm_signature *signature,
...@@ -388,3 +263,136 @@ int labcomm_internal_encoder_signature_to_index( ...@@ -388,3 +263,136 @@ int labcomm_internal_encoder_signature_to_index(
return index; return index;
} }
/**********************************************************
* Start of code related to sending (hierarchical)
* typedefs. Define LABCOMM_WITHOUT_TYPEDEFS to disable
**********************************************************/
#ifndef LABCOMM_WITHOUT_TYPE_DEFS
static void write_sig_tree_byte(char b, const struct labcomm_signature *signature,
void *context)
{
struct labcomm_encoder *e = context;
if(signature) {
labcomm_write_packed32(e->writer, labcomm_get_local_index(signature));
}else {
if (e->writer->pos >= e->writer->count) {
labcomm_writer_flush(e->writer, e->writer->action_context);
}
e->writer->data[e->writer->pos] = b;
e->writer->pos++;
}
}
static void do_write_signature(struct labcomm_encoder * e,
const struct labcomm_signature *signature,
unsigned char flatten)
{
map_signature(write_sig_tree_byte, e, signature, flatten);
}
static void sig_size(char b, const struct labcomm_signature *signature,
void *context)
{
int *result = context;
int diff;
if(signature) {
int idx = labcomm_get_local_index(signature);
diff = labcomm_size_packed32(idx);
}else {
diff = 1;
}
(*result)+=diff;
}
static int calc_sig_encoded_size(struct labcomm_encoder *e,
const struct labcomm_signature *sig)
{
int result=0;
map_signature(sig_size, &result, sig, FALSE);
return result;
}
static int internal_reg_type(
struct labcomm_encoder *e,
const struct labcomm_signature *signature,
labcomm_bool flatten)
{
int result = -EINVAL;
int index, *done, err;
index = labcomm_get_local_index(signature);
labcomm_scheduler_writer_lock(e->scheduler);
if (index <= 0) { goto out; }
done = LABCOMM_SIGNATURE_ARRAY_REF(e->memory, e->typedefs, int, index);
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; }
int sig_size = calc_sig_encoded_size(e, signature);
int len = labcomm_size_packed32(index) +
labcomm_size_string(signature->name) +
labcomm_size_packed32(sig_size) +
sig_size;
labcomm_write_packed32(e->writer, LABCOMM_TYPE_DEF);
labcomm_write_packed32(e->writer, len);
labcomm_write_packed32(e->writer, index);
labcomm_write_string(e->writer, signature->name);
labcomm_write_packed32(e->writer, sig_size);
do_write_signature(e, signature, FALSE);
labcomm_writer_end(e->writer, e->writer->action_context);
result = e->writer->error;
out:
labcomm_scheduler_writer_unlock(e->scheduler);
return result;
}
#endif
int labcomm_internal_encoder_type_register(
struct labcomm_encoder *e,
const struct labcomm_signature *signature)
{
#ifndef LABCOMM_WITHOUT_TYPE_DEFS
return internal_reg_type(e, signature, FALSE);
#else
return 0;
#endif
}
int labcomm_internal_encoder_type_bind(
struct labcomm_encoder *e,
const struct labcomm_signature *signature,
char has_deps)
{
#ifndef LABCOMM_WITHOUT_TYPE_DEFS
int result = -EINVAL;
int err;
int sindex = labcomm_get_local_index(signature);
int tindex = has_deps ? labcomm_get_local_type_index(signature) : LABCOMM_BIND_SELF;
labcomm_scheduler_writer_lock(e->scheduler);
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_write_packed32(e->writer, LABCOMM_TYPE_BINDING);
labcomm_write_packed32(e->writer, length);
labcomm_write_packed32(e->writer, sindex);
labcomm_write_packed32(e->writer, tindex);
labcomm_writer_end(e->writer, e->writer->action_context);
result = e->writer->error;
out:
labcomm_scheduler_writer_unlock(e->scheduler);
return result;
#else
return 0;
#endif
}
...@@ -23,7 +23,6 @@ import se.lth.control.labcomm2014.compiler.TypeDecl; ...@@ -23,7 +23,6 @@ import se.lth.control.labcomm2014.compiler.TypeDecl;
import se.lth.control.labcomm2014.compiler.SampleDecl; import se.lth.control.labcomm2014.compiler.SampleDecl;
import se.lth.control.labcomm2014.compiler.Type; import se.lth.control.labcomm2014.compiler.Type;
import se.lth.control.labcomm2014.compiler.VoidType; import se.lth.control.labcomm2014.compiler.VoidType;
//import se.lth.control.labcomm2014.compiler.SampleRefType;
import se.lth.control.labcomm2014.compiler.PrimType; import se.lth.control.labcomm2014.compiler.PrimType;
import se.lth.control.labcomm2014.compiler.UserType; import se.lth.control.labcomm2014.compiler.UserType;
import se.lth.control.labcomm2014.compiler.StructType; import se.lth.control.labcomm2014.compiler.StructType;
...@@ -42,9 +41,6 @@ import se.lth.control.labcomm2014.compiler.VariableSize; ...@@ -42,9 +41,6 @@ import se.lth.control.labcomm2014.compiler.VariableSize;
*/ */
public class ASTbuilder implements TypeDefParser.ParsedSymbolVisitor { public class ASTbuilder implements TypeDefParser.ParsedSymbolVisitor {
///// tree building
//
//
private LinkedList<Type> typeStack; private LinkedList<Type> typeStack;
private LinkedList<Field> fieldStack; private LinkedList<Field> fieldStack;
...@@ -78,6 +74,8 @@ public class ASTbuilder implements TypeDefParser.ParsedSymbolVisitor { ...@@ -78,6 +74,8 @@ public class ASTbuilder implements TypeDefParser.ParsedSymbolVisitor {
typeStack.push(new PrimType(t.getName(), t.getTag())); typeStack.push(new PrimType(t.getName(), t.getTag()));
} }
// SampleRefs are currently represented as primitive types,
// see comment in TypeDefParser
// public void visit(TypeDefParser.SampleRefType t){ // public void visit(TypeDefParser.SampleRefType t){
// typeStack.push(new SampleRefType()); // typeStack.push(new SampleRefType());
// } // }
...@@ -136,13 +134,14 @@ public class ASTbuilder implements TypeDefParser.ParsedSymbolVisitor { ...@@ -136,13 +134,14 @@ public class ASTbuilder implements TypeDefParser.ParsedSymbolVisitor {
if(errors.isEmpty()) { if(errors.isEmpty()) {
return p; return p;
} else { } else {
//XXX temporary debug printout // This should not happen
// get errors and throw exception
StringBuilder sb = new StringBuilder();
for (Iterator iter = errors.iterator(); iter.hasNext(); ) { for (Iterator iter = errors.iterator(); iter.hasNext(); ) {
String s = (String)iter.next(); String s = (String)iter.next();
System.err.println(s); sb.append(s);
} }
// This should not happen throw new RuntimeException("Internal error: parsed labcomm declaration has errors: "+sb.toString());
throw new RuntimeException("Internal error: parsed labcomm declaration has errors");
} }
} }
......
...@@ -38,19 +38,12 @@ public class DecoderChannel implements Decoder { ...@@ -38,19 +38,12 @@ public class DecoderChannel implements Decoder {
try { try {
processSample(Constant.TYPE_DEF); processSample(Constant.TYPE_DEF);
} catch(Exception ex) { } catch(Exception ex) {
System.out.println(ex.getMessage());
//System.err.println("Got TypeDef: skipping "+len+" bytes");
int idx = decodePacked32(); int idx = decodePacked32();
String name = decodeString(); String name = decodeString();
System.err.println("Ignoring (unhandled) TypeDef: "+idx+" "+name);
int siglen = decodePacked32(); int siglen = decodePacked32();
//System.err.println("siglen="+siglen);
for(int i=0; i<siglen; i++) { for(int i=0; i<siglen; i++) {
byte b = decodeByte(); byte b = decodeByte();
//System.out.print(b);
//System.out.print(" ");
} }
//System.out.println();
} }
} }
...@@ -58,8 +51,6 @@ public class DecoderChannel implements Decoder { ...@@ -58,8 +51,6 @@ public class DecoderChannel implements Decoder {
try { try {
processSample(Constant.TYPE_BINDING); processSample(Constant.TYPE_BINDING);
} catch(Exception ex) { } catch(Exception ex) {
System.out.println(ex.getMessage());
//System.err.println("Got TypeBinding: skipping "+len+" bytes");
for(int i=0; i<len; i++) { for(int i=0; i<len; i++) {
decodeByte(); decodeByte();
} }
...@@ -67,7 +58,6 @@ public class DecoderChannel implements Decoder { ...@@ -67,7 +58,6 @@ public class DecoderChannel implements Decoder {
} }
private void processPragma(int len) throws IOException { private void processPragma(int len) throws IOException {
System.err.println("Got Pragma: skipping "+len+" bytes");
for(int i=0; i<len; i++) { for(int i=0; i<len; i++) {
decodeByte(); decodeByte();
} }
...@@ -109,26 +99,26 @@ public class DecoderChannel implements Decoder { ...@@ -109,26 +99,26 @@ public class DecoderChannel implements Decoder {
version + " != " + Constant.CURRENT_VERSION); version + " != " + Constant.CURRENT_VERSION);
} }
} break;