From 09f1dabbac3087041cd1cdcda9d3929554478d04 Mon Sep 17 00:00:00 2001
From: Sven Gestegard Robertz <sven.robertz@cs.lth.se>
Date: Thu, 29 Jan 2015 12:41:57 +0100
Subject: [PATCH] added TYPE_BIND_SELF; don't send type_decl for samples
 without dependencies

---
 compiler/2014/Java_CodeGen.jrag                | 18 +++++++++++-------
 doc/tech_report.tex                            |  5 +++++
 examples/user_types/Decoder.java               |  9 ++++++++-
 examples/user_types/Encoder.java               |  8 ++++++++
 examples/user_types/example_decoder.c          |  5 +++++
 examples/user_types/test.lc                    |  5 +++++
 lib/java/se/lth/control/labcomm/Constant.java  |  6 ++++++
 .../se/lth/control/labcomm/EncoderChannel.java |  7 ++++++-
 .../lth/control/labcomm/SampleDispatcher.java  |  4 ++++
 9 files changed, 58 insertions(+), 9 deletions(-)

diff --git a/compiler/2014/Java_CodeGen.jrag b/compiler/2014/Java_CodeGen.jrag
index e643aa4..62a59ef 100644
--- a/compiler/2014/Java_CodeGen.jrag
+++ b/compiler/2014/Java_CodeGen.jrag
@@ -465,7 +465,7 @@ aspect Java_Class {
     //the type_ids of dependent types. Therefore, flat sigs
     //are used for matching
     Java_emitFlatSignature(env);
-    if(isReferenced() || isSampleDecl()){
+    if(isReferenced() || (isSampleDecl() && hasDependencies() )){
       Signature signature = getSignature();
       signature.Java_emitSignature(env, !isSampleDecl());
     }
@@ -545,11 +545,11 @@ aspect Java_Class {
     env.println("return "+isSample+";");
     env.unindent();
     env.println("}");
-//    env.println("public boolean hasStaticSignature() {");
-//    env.indent();
-//    env.println("return "+!hasDependencies()+";");
-//    env.unindent();
-//    env.println("}");
+    env.println("public boolean hasDependencies() {");
+    env.indent();
+    env.println("return "+hasDependencies()+";");
+    env.unindent();
+    env.println("}");
     env.println();
     env.println("/** return the flat signature. */");
     env.println("public byte[] getSignature() {");
@@ -570,7 +570,11 @@ aspect Java_Class {
 //    env.println();
     env.println("public void encodeTypeDef(Encoder e, int index) throws IOException{");
     env.indent();
-    env.println("emitSignature(e);");
+    if(!isSample || hasDependencies()) {
+      env.println("emitSignature(e);");
+    } else {
+      env.println("// the type has no dependencies, do nothing");
+    }
     env.unindent();
     env.println("}");
     env.println();
diff --git a/doc/tech_report.tex b/doc/tech_report.tex
index 244cdd1..561d69a 100644
--- a/doc/tech_report.tex
+++ b/doc/tech_report.tex
@@ -382,6 +382,11 @@ come from two independent number series. To identify which
 \verb+TYPE_DECL+ a particular \verb+SAMPLE_DECL+ corresponds to, the
 \verb+TYPE_BINDING+ packet is used.
 
+For sample types that do not depend on any typedefs, no \verb+TYPE_DECL+
+is sent, and the \verb+TYPE_BINDING+ binds the sample id to the special
+value zero to indicate that the type definition is identical to the
+flattened signature.
+
 \subsection{Example}
 
 The labcomm declaration
diff --git a/examples/user_types/Decoder.java b/examples/user_types/Decoder.java
index bdb1737..558131b 100644
--- a/examples/user_types/Decoder.java
+++ b/examples/user_types/Decoder.java
@@ -5,7 +5,8 @@ import java.io.InputStream;
 import se.lth.control.labcomm.DecoderChannel;
 
 public class Decoder
-  implements twoLines.Handler
+  implements twoLines.Handler,
+             twoInts.Handler
 
 {
 
@@ -15,6 +16,7 @@ public class Decoder
     throws Exception 
   {
     decoder = new DecoderChannel(in);
+    twoInts.register(decoder, this);
     twoLines.register(decoder, this);
 
     try {
@@ -33,6 +35,11 @@ public class Decoder
     return "Line from "+genPoint(l.start)+" to "+genPoint(l.end);
   }
 
+  public void handle_twoInts(twoInts d) throws java.io.IOException {
+    System.out.print("Got twoInts: ");
+    System.out.println(d.a +", "+d.b);
+  }
+
   public void handle_twoLines(twoLines d) throws java.io.IOException {
     System.out.print("Got twoLines: ");
     System.out.println("Line l1: "+genLine(d.l1));
diff --git a/examples/user_types/Encoder.java b/examples/user_types/Encoder.java
index aa2f80e..acee889 100644
--- a/examples/user_types/Encoder.java
+++ b/examples/user_types/Encoder.java
@@ -16,10 +16,18 @@ public class Encoder
     throws Exception 
   {
     encoder = new EncoderChannel(out);
+    twoInts.register(encoder);
     twoLines.register(encoder);
   }
 
   public void doEncode() throws java.io.IOException {
+    twoInts ti = new twoInts();
+    ti.a = 12;
+    ti.b = 21;
+
+    System.out.println("Encoding twoInts");
+    twoInts.encode(encoder, ti);
+      
     twoLines x = new twoLines();
     line l1 = new line();
     point p11 = new point();
diff --git a/examples/user_types/example_decoder.c b/examples/user_types/example_decoder.c
index 1aebedc..ed33100 100644
--- a/examples/user_types/example_decoder.c
+++ b/examples/user_types/example_decoder.c
@@ -8,6 +8,10 @@
 #include "gen/test.h"
 #include <stdio.h>
 
+static void handle_test_twoInts(test_twoInts *v,void *context) {
+  printf("Got twoInts. (%d,%d) \n", v->a, v->b); 
+}
+
 static void handle_test_twoLines(test_twoLines *v,void *context) {
   printf("Got twoLines. (%d,%d) -> (%d,%d), (%d,%d) -> (%d,%d)\n", v->l1.start.x.val, v->l1.start.y.val, 
                                                                      v->l1.end.x.val, v->l1.end.y.val,    
@@ -33,6 +37,7 @@ int main(int argc, char *argv[]) {
     return 1;
   }
 
+  labcomm_decoder_register_test_twoInts(decoder, handle_test_twoInts, context);
   labcomm_decoder_register_test_twoLines(decoder, handle_test_twoLines, context);
 
   printf("Decoding:\n");
diff --git a/examples/user_types/test.lc b/examples/user_types/test.lc
index d222539..9df2ebc 100644
--- a/examples/user_types/test.lc
+++ b/examples/user_types/test.lc
@@ -23,3 +23,8 @@ sample struct {
   line l2;
   foo  f;
 } twoLines;
+
+sample struct {
+  int a;
+  int b;
+} twoInts;
diff --git a/lib/java/se/lth/control/labcomm/Constant.java b/lib/java/se/lth/control/labcomm/Constant.java
index fc52a28..1198172 100644
--- a/lib/java/se/lth/control/labcomm/Constant.java
+++ b/lib/java/se/lth/control/labcomm/Constant.java
@@ -36,4 +36,10 @@ public class Constant {
   public static final int DOUBLE           = 0x26;
   public static final int STRING           = 0x27;
 
+
+  /*
+   * Other predefined symbols
+   */
+
+  public static final int TYPE_BIND_SELF   = 0x00;
 }
diff --git a/lib/java/se/lth/control/labcomm/EncoderChannel.java b/lib/java/se/lth/control/labcomm/EncoderChannel.java
index a766293..221259a 100644
--- a/lib/java/se/lth/control/labcomm/EncoderChannel.java
+++ b/lib/java/se/lth/control/labcomm/EncoderChannel.java
@@ -53,7 +53,12 @@ public class EncoderChannel implements Encoder {
             encodeByte(signature[i]);
         }
         end(null);
-        int tindex = registerTypeDef(dispatcher);
+        int tindex;
+        if(dispatcher.hasDependencies()){
+            tindex = registerTypeDef(dispatcher);
+        } else {
+            tindex = Constant.TYPE_BIND_SELF;
+        }
         bindType(index, tindex);
 
   }
diff --git a/lib/java/se/lth/control/labcomm/SampleDispatcher.java b/lib/java/se/lth/control/labcomm/SampleDispatcher.java
index aeef84e..b2ed2fd 100644
--- a/lib/java/se/lth/control/labcomm/SampleDispatcher.java
+++ b/lib/java/se/lth/control/labcomm/SampleDispatcher.java
@@ -13,6 +13,10 @@ public interface SampleDispatcher <T extends SampleType>{
   public void decodeAndHandle(Decoder decoder,
 			      SampleHandler handler) throws Exception;
 
+  /** @return true if the type depends on one or more typedefs
+   */
+  public boolean hasDependencies();
+
   public void encodeTypeDef(Encoder e, int index) throws IOException;
 
   /** return the tag SAMPLE_DEF or TYPE_DEF, for use
-- 
GitLab