From 43d8a6f89d5c110473ce70a15f0afc355d200024 Mon Sep 17 00:00:00 2001 From: Anders Blomdell <anders.blomdell@control.lth.se> Date: Mon, 1 Jun 2015 18:25:13 +0200 Subject: [PATCH] Continued work on removing typedef's from runtime library for java --- compiler/2014/Java_CodeGen.jrag | 8 +- .../lth/control/labcomm2014/DecoderChannel.cs | 6 - .../lth/control/labcomm2014/EncoderChannel.cs | 1 + .../se/lth/control/labcomm2014/Constant.java | 6 - .../se/lth/control/labcomm2014/Decoder.java | 2 + .../control/labcomm2014/DecoderChannel.java | 75 +++------- .../control/labcomm2014/DecoderRegistry.java | 10 +- .../se/lth/control/labcomm2014/Encoder.java | 7 +- .../control/labcomm2014/EncoderChannel.java | 128 +++--------------- 9 files changed, 52 insertions(+), 191 deletions(-) diff --git a/compiler/2014/Java_CodeGen.jrag b/compiler/2014/Java_CodeGen.jrag index f2d7441..d7f7dc5 100644 --- a/compiler/2014/Java_CodeGen.jrag +++ b/compiler/2014/Java_CodeGen.jrag @@ -631,9 +631,9 @@ aspect Java_Class { } env.println(") throws IOException {"); env.indent(); - env.println("e.begin(" + getName() + ".class);"); + env.println("e.begin(dispatcher);"); getDataType().Java_emitEncoder(env, "value"); - env.println("e.end(" + getName() + ".class);"); + env.println("e.end(dispatcher);"); env.unindent(); env.println("}"); env.println(); @@ -998,8 +998,8 @@ aspect Java_Signature { public abstract void SignatureLine.Java_emitSignature(Java_env env, boolean decl); public void TypeRefSignatureLine.Java_emitSignature(Java_env env, boolean isDecl){ - env.print(getIndentString()); - env.println("e.encodePacked32(e.getTypeId("+decl.getName()+".class));"); +// env.print(getIndentString()); +// env.println("e.encodePacked32(e.getTypeId("+decl.getName()+".class));"); } public void DataSignatureLine.Java_emitSignature(Java_env env, boolean decl){ diff --git a/lib/csharp/se/lth/control/labcomm2014/DecoderChannel.cs b/lib/csharp/se/lth/control/labcomm2014/DecoderChannel.cs index 95f2214..cf3426b 100644 --- a/lib/csharp/se/lth/control/labcomm2014/DecoderChannel.cs +++ b/lib/csharp/se/lth/control/labcomm2014/DecoderChannel.cs @@ -45,12 +45,6 @@ namespace se.lth.control.labcomm2014 { ReadBytes(signature, signature_length); ref_registry.add(index, name, signature); } break; - case Constant.TYPE_DEF: - case Constant.TYPE_BINDING: { - for(int i=0; i<length;i++){ - decodeByte(); - } - } break; default: { DecoderRegistry.Entry e = def_registry.get(tag); if (e == null) { diff --git a/lib/csharp/se/lth/control/labcomm2014/EncoderChannel.cs b/lib/csharp/se/lth/control/labcomm2014/EncoderChannel.cs index cba6072..7355905 100644 --- a/lib/csharp/se/lth/control/labcomm2014/EncoderChannel.cs +++ b/lib/csharp/se/lth/control/labcomm2014/EncoderChannel.cs @@ -137,6 +137,7 @@ namespace se.lth.control.labcomm2014 { try { index = ref_registry.getTag(value); } catch (NullReferenceException) { + //we want to return 0 for unregistered ref types } WriteInt(index, 4); } diff --git a/lib/java/se/lth/control/labcomm2014/Constant.java b/lib/java/se/lth/control/labcomm2014/Constant.java index 8aed416..8e41791 100644 --- a/lib/java/se/lth/control/labcomm2014/Constant.java +++ b/lib/java/se/lth/control/labcomm2014/Constant.java @@ -37,10 +37,4 @@ public class Constant { public static final int STRING = 0x27; public static final int SAMPLE = 0x28; - - /* - * Other predefined symbols - */ - - public static final int TYPE_BIND_SELF = 0x00; } diff --git a/lib/java/se/lth/control/labcomm2014/Decoder.java b/lib/java/se/lth/control/labcomm2014/Decoder.java index 52c6636..cb83979 100644 --- a/lib/java/se/lth/control/labcomm2014/Decoder.java +++ b/lib/java/se/lth/control/labcomm2014/Decoder.java @@ -4,6 +4,8 @@ import java.io.IOException; public interface Decoder { + public void runOne() throws Exception; + public void run() throws Exception; public void register(SampleDispatcher dispatcher, SampleHandler handler) throws IOException; public void registerSampleRef(SampleDispatcher dispatcher) throws IOException; diff --git a/lib/java/se/lth/control/labcomm2014/DecoderChannel.java b/lib/java/se/lth/control/labcomm2014/DecoderChannel.java index 0e3ac6e..25d04f2 100644 --- a/lib/java/se/lth/control/labcomm2014/DecoderChannel.java +++ b/lib/java/se/lth/control/labcomm2014/DecoderChannel.java @@ -34,56 +34,22 @@ public class DecoderChannel implements Decoder { ref_registry.add(index, name, signature); } - private void processTypeDef(int len) throws IOException { - try { - processSample(Constant.TYPE_DEF); - } catch(Exception ex) { - int idx = decodePacked32(); - String name = decodeString(); - int siglen = decodePacked32(); - for(int i=0; i<siglen; i++) { - byte b = decodeByte(); - } - } - } - - private void processTypeBinding(int len) throws IOException { - try { - processSample(Constant.TYPE_BINDING); - } catch(Exception ex) { - for(int i=0; i<len; i++) { - decodeByte(); - } - } - } - - private void processPragma(int len) throws IOException { - for(int i=0; i<len; i++) { - decodeByte(); - } - } - - private void processSample(int tag) throws IOException { - DecoderRegistry.Entry e = def_registry.get(tag); - if (e == null) { - throw new IOException("Unhandled tag " + tag); - } - SampleDispatcher d = e.getDispatcher(); - if (d == null) { - throw new IOException("No dispatcher for '" + e.getName() + "'"); - } - SampleHandler h = e.getHandler(); - if (h == null) { - throw new IOException("No handler for '" + e.getName() +"'"); - } - try { - //XXX why does decodeAndHandle throw Exception and not IOException? - d.decodeAndHandle(this, h); - } catch (IOException ex) { - throw ex; - } catch (Exception ex) { - ex.printStackTrace(); - } + private void processSample(int tag) throws Exception { + DecoderRegistry.Entry e = def_registry.get(tag); + if (e == null) { + throw new IOException("Unhandled tag " + tag); + } + SampleDispatcher d = e.getDispatcher(); + if (d == null) { + throw new IOException("No dispatcher for '" + e.getName() + "'"); + } + SampleHandler h = e.getHandler(); + if (h == null) { + throw new IOException("No handler for '" + e.getName() +"'"); + } + // decodeAndHandle throws Exception and not IOException because + // the user provided handler might throw anything + d.decodeAndHandle(this, h); } public void runOne() throws Exception { @@ -105,15 +71,6 @@ public class DecoderChannel implements Decoder { 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; diff --git a/lib/java/se/lth/control/labcomm2014/DecoderRegistry.java b/lib/java/se/lth/control/labcomm2014/DecoderRegistry.java index d6d2cf7..1131641 100644 --- a/lib/java/se/lth/control/labcomm2014/DecoderRegistry.java +++ b/lib/java/se/lth/control/labcomm2014/DecoderRegistry.java @@ -91,17 +91,17 @@ public class DecoderRegistry { } } - private HashMap<Class, Entry> byClass; + private HashMap<SampleDispatcher, Entry> byDispatcher; private HashMap<Integer, Entry> byIndex; public DecoderRegistry() { - byClass = new HashMap<Class, Entry>(); + byDispatcher = new HashMap<SampleDispatcher, Entry>(); byIndex = new HashMap<Integer, Entry>(); } public synchronized void add(SampleDispatcher dispatcher, SampleHandler handler) throws IOException{ - Entry e = byClass.get(dispatcher.getSampleClass()); + Entry e = byDispatcher.get(dispatcher); if (e != null) { e.check(dispatcher.getName(), dispatcher.getSignature()); e.setHandler(handler); @@ -116,7 +116,7 @@ public class DecoderRegistry { } if (e == null) { e = new Entry(dispatcher, handler); - byClass.put(dispatcher.getSampleClass(), e); + byDispatcher.put(dispatcher, e); } } } @@ -128,7 +128,7 @@ public class DecoderRegistry { if (e != null) { e.check(name, signature); } else { - for (Entry e2 : byClass.values()) { + for (Entry e2 : byDispatcher.values()) { if (e2.match(name, signature)) { e2.setIndex(index); e = e2; diff --git a/lib/java/se/lth/control/labcomm2014/Encoder.java b/lib/java/se/lth/control/labcomm2014/Encoder.java index 78c2e48..ff7951e 100644 --- a/lib/java/se/lth/control/labcomm2014/Encoder.java +++ b/lib/java/se/lth/control/labcomm2014/Encoder.java @@ -6,11 +6,8 @@ public interface Encoder { public void register(SampleDispatcher dispatcher) throws IOException; public void registerSampleRef(SampleDispatcher dispatcher) throws IOException; - public void begin(Class<? extends SampleType> c) throws IOException; - public void end(Class<? extends SampleType> c) throws IOException; - - public void begin(int t) throws IOException; - public int getTypeId(Class<? extends SampleType> c) throws IOException; + public void begin(SampleDispatcher dispatcher) throws IOException; + public void end(SampleDispatcher dispatcher) throws IOException; public void encodeBoolean(boolean value) throws IOException; public void encodeByte(byte value) throws IOException; diff --git a/lib/java/se/lth/control/labcomm2014/EncoderChannel.java b/lib/java/se/lth/control/labcomm2014/EncoderChannel.java index 4f07548..b12840c 100644 --- a/lib/java/se/lth/control/labcomm2014/EncoderChannel.java +++ b/lib/java/se/lth/control/labcomm2014/EncoderChannel.java @@ -10,109 +10,38 @@ public class EncoderChannel implements Encoder { private Writer writer; private ByteArrayOutputStream bytes = new ByteArrayOutputStream(); private DataOutputStream data = new DataOutputStream(bytes); - private EncoderRegistry sample_def_registry = new EncoderRegistry(); - private EncoderRegistry sample_ref_registry = new EncoderRegistry(); - private EncoderRegistry type_def_registry = new EncoderRegistry(); + private EncoderRegistry def_registry = new EncoderRegistry(); + private EncoderRegistry ref_registry = new EncoderRegistry(); private int current_tag; - private EncoderChannel(Writer writer, boolean emitVersion) throws IOException { + private EncoderChannel(Writer writer) throws IOException { this.writer = writer; - if(emitVersion){ - begin(Constant.VERSION); - encodeString(Constant.CURRENT_VERSION); - end(null); - } - } - public EncoderChannel(Writer writer) throws IOException { - this(writer, true); + begin(Constant.VERSION); + encodeString(Constant.CURRENT_VERSION); + end(null); } public EncoderChannel(OutputStream writer) throws IOException { - this(new WriterWrapper(writer), true); - } - - private EncoderChannel(OutputStream writer, boolean emitVersion) throws IOException { - this(new WriterWrapper(writer), emitVersion); - } - private void bindType(int sampleId, int typeId) throws IOException { - begin(Constant.TYPE_BINDING); - encodePacked32(sampleId); - encodePacked32(typeId); - end(null); - } - - private void registerSample(SampleDispatcher dispatcher) throws IOException { - int index = sample_def_registry.add(dispatcher); - begin(dispatcher.getTypeDeclTag()); - encodePacked32(index); - encodeString(dispatcher.getName()); - byte[] signature = dispatcher.getSignature(); - encodePacked32(signature.length); - for (int i = 0 ; i < signature.length ; i++) { - encodeByte(signature[i]); - } - end(null); - } - - private static class WrappedEncoder extends EncoderChannel{ - private Encoder wrapped; - - public WrappedEncoder(Encoder e, OutputStream s, boolean emitVersion) throws IOException { - super(s,emitVersion); - this.wrapped = e; - } - public int getTypeId(Class<? extends SampleType> c) throws IOException{ - return wrapped.getTypeId(c); - } - } - - - private int registerTypeDef(SampleDispatcher dispatcher) throws IOException { - // 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); - //wrap encoder to get encoded length of signature - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - EncoderChannel wrapped = new WrappedEncoder(this, baos, false); - dispatcher.encodeTypeDef(wrapped, index); - wrapped.flush(); - byte b[] = baos.toByteArray(); - - begin(Constant.TYPE_DEF); - encodePacked32(index); - encodeString(dispatcher.getName()); - encodePacked32(b.length); - for(int i = 0; i<b.length; i++) { - encodeByte(b[i]); - } - end(null); - - return index; - } + this(new WriterWrapper(writer)); } public void register(SampleDispatcher dispatcher) throws IOException { - switch (dispatcher.getTypeDeclTag()) { - case Constant.SAMPLE_DEF: { - registerSample(dispatcher); - break; - } - case Constant.TYPE_DEF: { - registerTypeDef(dispatcher); - break; - } - default: - throw new Error("Unknown typeDeclTag: "+dispatcher.getTypeDeclTag()); + int index = def_registry.add(dispatcher); + begin(Constant.SAMPLE_DEF); + encodePacked32(index); + encodeString(dispatcher.getName()); + byte[] signature = dispatcher.getSignature(); + encodePacked32(signature.length); + for (int i = 0 ; i < signature.length ; i++) { + encodeByte(signature[i]); } + end(null); } public void registerSampleRef(SampleDispatcher dispatcher) throws IOException { System.err.println(dispatcher); - int index = sample_ref_registry.add(dispatcher); + int index = ref_registry.add(dispatcher); begin(Constant.SAMPLE_REF); encodePacked32(index); encodeString(dispatcher.getName()); @@ -124,22 +53,16 @@ public class EncoderChannel implements Encoder { end(null); } - public void begin(int tag) { + private void begin(int tag) { current_tag = tag; bytes.reset(); } - public void begin(Class<? extends SampleType> c) throws IOException { - begin(sample_def_registry.getTag(c)); + public void begin(SampleDispatcher dispatcher) throws IOException { + begin(def_registry.getTag(dispatcher)); } - /* aux. method used to allow nesting encoders to find encoded size */ - private void flush() throws IOException{ - data.flush(); - writer.write(bytes.toByteArray()); - bytes.reset(); - } - public void end(Class<? extends SampleType> c) throws IOException { + public void end(SampleDispatcher dispatcher) throws IOException { data.flush(); WritePacked32(writer, current_tag); WritePacked32(writer, bytes.size()); @@ -147,13 +70,6 @@ public class EncoderChannel implements Encoder { bytes.reset(); } - /** - * @return the id of a TYPE_DEF - */ - public int getTypeId(Class<? extends SampleType> c) throws IOException { - return type_def_registry.getTag(c); - } - private void WritePacked32(Writer s, long value) throws IOException { byte[] tmp1 = new byte[5]; byte[] tmp2 = new byte[1]; @@ -229,7 +145,7 @@ public class EncoderChannel implements Encoder { public void encodeSampleRef(Class value) throws IOException { int index = 0; try { - index = sample_ref_registry.getTag(value); + index = ref_registry.getTag(value); } catch (NullPointerException e) { //we want to return 0 for unregistered ref types } -- GitLab