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 @@
#include "labcomm_ioctl.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 {
int remote_index;
const struct labcomm_signature *signature;
......@@ -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)
{
int i;
#ifdef LABCOMM_DECODER_DEBUG
printf("got tag 0x%x, skipping %d bytes\n", tag, len);
#endif
DECODER_DEBUG_FPRINTF(stdout, "got tag 0x%x, skipping %d bytes\n", tag, len);
for(i = 0; i <len; i++){
#ifdef LABCOMM_DECODER_DEBUG
fprintf(stderr,".");
#endif
DECODER_DEBUG_FPRINTF(stderr, ".");
labcomm_read_byte(d->reader);
if (d->reader->error < 0) {
#ifdef LABCOMM_DECODER_DEBUG
fprintf(stderr, "\nerror while skipping: %d\n", d->reader->error);
#endif
DECODER_DEBUG_FPRINTF(stderr, "\nerror while skipping: %d\n", d->reader->error);
return d->reader->error;
}
}
#ifdef LABCOMM_DECODER_DEBUG
fprintf(stderr, "\n");
#endif
DECODER_DEBUG_FPRINTF(stderr, "\n");
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)
{
int result;
......@@ -449,9 +388,7 @@ int labcomm_decoder_decode_one(struct labcomm_decoder *d)
}
labcomm_memory_free(d->memory, 1, version);
} else if (! d->version_ok) {
#ifdef LABCOMM_DECODER_DEBUG
fprintf(stderr, "No VERSION %d %d\n", remote_index, length);
#endif
DEBUG_FPRINTF(stderr, "No VERSION %d %d\n", remote_index, length);
result = -ECONNRESET;
} else if (remote_index == 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)
result = decode_and_handle(d, d, remote_index);
if(result == -ENOENT) {
//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);
#endif
}
} else if (remote_index == LABCOMM_TYPE_BINDING) {
result = decode_and_handle(d, d, remote_index);
if(result == -ENOENT) {
//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);
#endif
}
} else if (remote_index == LABCOMM_PRAGMA) {
result = decode_pragma(d, d, length);
} else if (remote_index < LABCOMM_USER) {
#ifdef LABCOMM_DECODER_DEBUG
fprintf(stderr, "SKIP %d %d\n", remote_index, length);
#endif
DECODER_DEBUG_FPRINTF(stderr, "SKIP %d %d\n", remote_index, length);
result = decoder_skip(d, length, remote_index);
} else {
result = decode_and_handle(d, d, remote_index);
......
......@@ -26,6 +26,9 @@
#include "labcomm_ioctl.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_writer *writer;
struct labcomm_error_handler *error;
......@@ -97,134 +100,6 @@ void labcomm_encoder_free(struct labcomm_encoder* 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(
struct labcomm_encoder *e,
const struct labcomm_signature *signature,
......@@ -388,3 +263,136 @@ int labcomm_internal_encoder_signature_to_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;
import se.lth.control.labcomm2014.compiler.SampleDecl;
import se.lth.control.labcomm2014.compiler.Type;
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.UserType;
import se.lth.control.labcomm2014.compiler.StructType;
......@@ -42,9 +41,6 @@ import se.lth.control.labcomm2014.compiler.VariableSize;
*/
public class ASTbuilder implements TypeDefParser.ParsedSymbolVisitor {
///// tree building
//
//
private LinkedList<Type> typeStack;
private LinkedList<Field> fieldStack;
......@@ -78,6 +74,8 @@ public class ASTbuilder implements TypeDefParser.ParsedSymbolVisitor {
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){
// typeStack.push(new SampleRefType());
// }
......@@ -136,13 +134,14 @@ public class ASTbuilder implements TypeDefParser.ParsedSymbolVisitor {
if(errors.isEmpty()) {
return p;
} 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(); ) {
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");
throw new RuntimeException("Internal error: parsed labcomm declaration has errors: "+sb.toString());
}
}
......
......@@ -38,19 +38,12 @@ public class DecoderChannel implements Decoder {
try {
processSample(Constant.TYPE_DEF);
} catch(Exception ex) {
System.out.println(ex.getMessage());
//System.err.println("Got TypeDef: skipping "+len+" bytes");
int idx = decodePacked32();
String name = decodeString();
System.err.println("Ignoring (unhandled) TypeDef: "+idx+" "+name);
int siglen = decodePacked32();
//System.err.println("siglen="+siglen);
for(int i=0; i<siglen; i++) {
byte b = decodeByte();
//System.out.print(b);
//System.out.print(" ");
}
//System.out.println();
}
}
......@@ -58,8 +51,6 @@ public class DecoderChannel implements Decoder {
try {
processSample(Constant.TYPE_BINDING);
} catch(Exception ex) {
System.out.println(ex.getMessage());
//System.err.println("Got TypeBinding: skipping "+len+" bytes");
for(int i=0; i<len; i++) {
decodeByte();
}
......@@ -67,7 +58,6 @@ public class DecoderChannel implements Decoder {
}
private void processPragma(int len) throws IOException {
System.err.println("Got Pragma: skipping "+len+" bytes");
for(int i=0; i<len; i++) {
decodeByte();
}
......@@ -109,26 +99,26 @@ public class DecoderChannel implements Decoder {
version + " != " + Constant.CURRENT_VERSION);
}
} break;
case Constant.SAMPLE_DEF: {
processSampleDef();
} break;
case Constant.SAMPLE_REF: {
processSampleRef();
} break;
case Constant.TYPE_DEF: {
processTypeDef(length);
} break;
case Constant.TYPE_BINDING: {
processTypeBinding(length);
} break;
case Constant.PRAGMA: {
processPragma(length);
} break;
default: {
processSample(tag);
done = true;
}
case Constant.SAMPLE_DEF: {
processSampleDef();
} break;
case Constant.SAMPLE_REF: {
processSampleRef();
} break;
case Constant.TYPE_DEF: {
processTypeDef(length);
} break;
case Constant.TYPE_BINDING: {
processTypeBinding(length);
} break;
case Constant.PRAGMA: {
processPragma(length);
} break;
default: {
processSample(tag);
done = true;
}
}
}
}
......
......@@ -77,14 +77,12 @@ public class EncoderChannel implements Encoder {
private int registerTypeDef(SampleDispatcher dispatcher) throws IOException {
//XXX A bit crude; maybe add boolean registry.contains(...) and check
// if already registered
// check if already registered
try {
return type_def_registry.getTag(dispatcher);
} catch (IOException e) {
//otherwise, add to the registry
int index = type_def_registry.add(dispatcher);
//System.out.println("registered "+dispatcher.getName());
//
//wrap encoder to get encoded length of signature
ByteArrayOutputStream baos = new ByteArrayOutputStream();
EncoderChannel wrapped = new WrappedEncoder(this, baos, false);
......@@ -143,7 +141,7 @@ public class EncoderChannel implements Encoder {
begin(sample_def_registry.getTag(c));
}
/* temporary(?) fix to allow nesting encoders to find encoded size */
/* aux. method used to allow nesting encoders to find encoded size */
private void flush() throws IOException{
data.flush();
writer.write(bytes.toByteArray());
......@@ -208,10 +206,6 @@ public class EncoderChannel implements Encoder {
}
public void encodeString(String value) throws IOException {
//data.writeShort(0); // HACK...
//data.writeUTF(value);
//kludge, to replace above hack with packed length
ByteArrayOutputStream tmpb = new ByteArrayOutputStream();
DataOutputStream tmps = new DataOutputStream(tmpb);
......@@ -219,6 +213,8 @@ public class EncoderChannel implements Encoder {
tmps.flush();
byte[] tmp = tmpb.toByteArray();
//the two first bytes written by writeUTF is the length of
//the string, so we skip those
encodePacked32(tmp.length-2