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

WiP: intentions partially implemented in C, Java (typedefparser broken), and python

parent 467c8127
...@@ -48,6 +48,8 @@ aspect SigAnnotations { ...@@ -48,6 +48,8 @@ aspect SigAnnotations {
} }
}; };
syn int Decl.getNumIntentions() = getTypeInstance().sortedIntentions().getNumChild();
syn List<Intention> TypeInstance.sortedIntentions() { syn List<Intention> TypeInstance.sortedIntentions() {
List<Intention> res = new List<Intention>(); List<Intention> res = new List<Intention>();
......
...@@ -78,7 +78,7 @@ aspect FlatSignature { ...@@ -78,7 +78,7 @@ aspect FlatSignature {
public void Field.flatSignature(SignatureList list) { public void Field.flatSignature(SignatureList list) {
debugAnnotations(this.getName()+".Field.flatSignature"); debugAnnotations(this.getName()+".Field.flatSignature");
list.addIntentions(intentions(), "Field: "+getIntentionString()); list.addIntentions(intentions(), "Field: "+getIntentionString());
list.addString(getName(), signatureComment()); // list.addString(getName(), signatureComment());
getDataType().flatSignature(list); getDataType().flatSignature(list);
} }
......
...@@ -473,6 +473,7 @@ aspect Java_Class { ...@@ -473,6 +473,7 @@ aspect Java_Class {
} }
public void Decl.Java_emitIntentions(Java_env env){ public void Decl.Java_emitIntentions(Java_env env){
env.println("private static int numIntentions = " +getNumIntentions() +";");
env.println("private static byte[] intentions = new byte[] {"); env.println("private static byte[] intentions = new byte[] {");
env.indent(); env.indent();
byte[] data = getIntentionBytes(); byte[] data = getIntentionBytes();
...@@ -583,6 +584,28 @@ aspect Java_Class { ...@@ -583,6 +584,28 @@ aspect Java_Class {
env.unindent(); env.unindent();
env.println("}"); env.println("}");
env.println(); env.println();
env.println("/** return the outermost intentions. */");
env.println("public byte[] getIntentionBytes() {");
env.indent();
if(isSample) {
env.println("return intentions;");
} else {
env.println("return new byte[0];");
}
env.unindent();
env.println("}");
env.println();
env.println("/** return the number of intentions. */");
env.println("public int getNumIntentions() {");
env.indent();
if(isSample) {
env.println("return numIntentions;");
} else {
env.println("return 0;");
}
env.unindent();
env.println("}");
env.println();
// env.println("public void encodeSignature(Encoder e) throws IOException{"); // env.println("public void encodeSignature(Encoder e) throws IOException{");
// env.indent(); // env.indent();
// env.println("emitSignature(e);"); // env.println("emitSignature(e);");
......
...@@ -7,12 +7,12 @@ aspect Refactoring { ...@@ -7,12 +7,12 @@ aspect Refactoring {
public Annotations Annotations.addName(String n) { public Annotations Annotations.addName(String n) {
//XXX TODO: check if name already exists //XXX TODO: check if name already exists
addAnnotation(new Annotation("",n.getBytes())); addAnnotation(new Intention("",n.getBytes()));
return this; return this;
} }
public Field.Field(TypeInstance t) { public Field.Field(TypeInstance t) {
this(t.getDataType(), t.getAnnotations().addName(t.getName())); this(t.getDataType(), t.getAnnotations());
} }
public TypeInstance.TypeInstance(DataType t, String n, Annotations a) { public TypeInstance.TypeInstance(DataType t, String n, Annotations a) {
......
...@@ -289,6 +289,7 @@ aspect Signature { ...@@ -289,6 +289,7 @@ aspect Signature {
public void TypeInstance.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) { public void TypeInstance.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) {
// debugAnnotations(this.getName()); // debugAnnotations(this.getName());
// list.addString(inst.getIntentionString(), "intention string"); // list.addString(inst.getIntentionString(), "intention string");
list.addIntentions(intentionSet(), "intentions");
getDataType().genSigLineForDecl(list, decl, this); getDataType().genSigLineForDecl(list, decl, this);
} }
...@@ -351,12 +352,13 @@ aspect Signature { ...@@ -351,12 +352,13 @@ aspect Signature {
list.add(null, "}"); list.add(null, "}");
} }
public void Field.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) { // public void Field.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) {
list.addString(getName(), signatureComment()); // //XXX make intention
super.genSigLineForDecl(list, decl, inst); // list.addString(getName(), signatureComment());
//TODOintent // super.genSigLineForDecl(list, decl, inst);
//getDataType().genSigLineForDecl(list, decl, inst); // //TODOintent
} // //getDataType().genSigLineForDecl(list, decl, inst);
// }
public void IntegerLiteral.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) { public void IntegerLiteral.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) {
list.addInt(Integer.parseInt(getValue()), null); list.addInt(Integer.parseInt(getValue()), null);
......
...@@ -44,8 +44,11 @@ aspect AnnotationCheck { ...@@ -44,8 +44,11 @@ aspect AnnotationCheck {
public void ASTNode.annotationCheck() {} public void ASTNode.annotationCheck() {}
public void TypeDecl.annotationCheck() { public void TypeDecl.annotationCheck() {
if(getTypeInstance().hasIntentions()) { Iterator<Intention> it = getTypeInstance().intentions().iterator();;
error("TypeDecl " + getName() + " has intentions. (Not allowed for typedefs)"); while(it.hasNext()) {
if(!it.next().getKey().equals("")) {
error("TypeDecl " + getName() + " has intentions. (Not allowed for typedefs)");
}
} }
} }
} }
...@@ -161,7 +161,7 @@ static int decode_sample_def_or_ref(struct labcomm2014_decoder *d, int kind) ...@@ -161,7 +161,7 @@ static int decode_sample_def_or_ref(struct labcomm2014_decoder *d, int kind)
int numInts = labcomm2014_read_byte(d->reader); int numInts = labcomm2014_read_byte(d->reader);
if(numInts != 1) { if(numInts != 1) {
printf("WARNING! #intentions != 1, this will probably crash\n"); printf("WARNING! #intentions %d != 1, this will probably crash\n", numInts);
} }
//XXX temporary kludge for intentions //XXX temporary kludge for intentions
//assume only one intention: the name //assume only one intention: the name
...@@ -482,6 +482,16 @@ static void decode_raw_type_def( ...@@ -482,6 +482,16 @@ static void decode_raw_type_def(
struct labcomm2014_raw_type_def v; struct labcomm2014_raw_type_def v;
v.index = labcomm2014_read_packed32(r); v.index = labcomm2014_read_packed32(r);
if (r->error < 0) { goto out; } if (r->error < 0) { goto out; }
{
int numInts = labcomm2014_read_byte(r);
if(numInts != 1) {
printf("WARNING! #intentions %d != 1, this will probably crash\n", numInts);
}
//XXX temporary kludge for intentions
//assume only one intention: the name
labcomm2014_read_packed32(r); // assume the empty key (i.e., name)
}
v.name = labcomm2014_read_string(r); v.name = labcomm2014_read_string(r);
if (r->error < 0) { goto free_name; } if (r->error < 0) { goto free_name; }
v.length = labcomm2014_read_packed32(r); v.length = labcomm2014_read_packed32(r);
......
...@@ -302,15 +302,15 @@ static int internal_reg_type( ...@@ -302,15 +302,15 @@ static int internal_reg_type(
if (err != 0) { result = err; goto out; } if (err != 0) { result = err; goto out; }
int sig_size = calc_sig_encoded_size(e, signature); int sig_size = calc_sig_encoded_size(e, signature);
int len = labcomm2014_size_packed32(index) + int len = (labcomm2014_size_packed32(index) +
labcomm2014_size_string(signature->name) + TODO_sizeof_intentions(signature) +
labcomm2014_size_packed32(sig_size) + labcomm2014_size_packed32(signature->size) +
sig_size; sig_size);
labcomm2014_write_packed32(e->writer, LABCOMM_TYPE_DEF); labcomm2014_write_packed32(e->writer, LABCOMM_TYPE_DEF);
labcomm2014_write_packed32(e->writer, len); labcomm2014_write_packed32(e->writer, len);
labcomm2014_write_packed32(e->writer, index); labcomm2014_write_packed32(e->writer, index);
labcomm2014_write_string(e->writer, signature->name); TODO_encode_intentions(e, signature);
labcomm2014_write_packed32(e->writer, sig_size); labcomm2014_write_packed32(e->writer, sig_size);
do_write_signature(e, signature, LABCOMM2014_FALSE); do_write_signature(e, signature, LABCOMM2014_FALSE);
......
...@@ -214,8 +214,8 @@ int main(void) ...@@ -214,8 +214,8 @@ int main(void)
labcomm2014_encoder_register_generated_encoding_V(encoder); labcomm2014_encoder_register_generated_encoding_V(encoder);
labcomm2014_encoder_register_generated_encoding_V(encoder); labcomm2014_encoder_register_generated_encoding_V(encoder);
EXPECT({ 0x02, 0x08, VARIABLE(0), 0x01, 0x00, 0x01, 'V', 0x02, 0x11, 0x00, EXPECT({ 0x02, 0x08, VARIABLE(0), 0x01, 0x00, 0x01, 'V', 0x02, 0x11, 0x00,
0x04, 0x08, VARIABLE(1), 0x03, 'v', '_', 't', 0x02, 0x11, 0x00, 0x04, 0x0a, VARIABLE(1), 0x01, 0x00, 0x03, 'v', '_', 't', 0x02, 0x11, 0x00,
0x04, 0x05, VARIABLE(2), 0x01, 'V', 0x01, VARIABLE(1), 0x04, 0x07, VARIABLE(2), 0x01, 0x00, 0x01, 'V', 0x01, VARIABLE(1),
0x05, 0x02, VARIABLE(0), VARIABLE(2) }); 0x05, 0x02, VARIABLE(0), VARIABLE(2) });
labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_RESET); labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
......
...@@ -18,6 +18,14 @@ public class DecoderChannel implements Decoder { ...@@ -18,6 +18,14 @@ public class DecoderChannel implements Decoder {
private void processSampleDef() throws IOException { private void processSampleDef() throws IOException {
int index = decodePacked32(); int index = decodePacked32();
int numIntentions = decodePacked32();
if(numIntentions != 1) {
System.out.println("WARNING: #intentions == "+numIntentions);
}
int keylen = decodePacked32();
if(keylen != 0) {
System.out.println("WARNING: keylen == "+keylen);
}
String name = decodeString(); String name = decodeString();
int signature_length = decodePacked32(); int signature_length = decodePacked32();
byte[] signature = new byte[signature_length]; byte[] signature = new byte[signature_length];
...@@ -27,6 +35,14 @@ public class DecoderChannel implements Decoder { ...@@ -27,6 +35,14 @@ public class DecoderChannel implements Decoder {
private void processSampleRef() throws IOException { private void processSampleRef() throws IOException {
int index = decodePacked32(); int index = decodePacked32();
int numIntentions = decodePacked32();
if(numIntentions != 1) {
System.out.println("WARNING: #intentions == "+numIntentions);
}
int keylen = decodePacked32();
if(keylen != 0) {
System.out.println("WARNING: keylen == "+keylen);
}
String name = decodeString(); String name = decodeString();
int signature_length = decodePacked32(); int signature_length = decodePacked32();
byte[] signature = new byte[signature_length]; byte[] signature = new byte[signature_length];
...@@ -39,6 +55,14 @@ public class DecoderChannel implements Decoder { ...@@ -39,6 +55,14 @@ public class DecoderChannel implements Decoder {
processSample(Constant.TYPE_DEF); processSample(Constant.TYPE_DEF);
} catch(Exception ex) { } catch(Exception ex) {
int idx = decodePacked32(); int idx = decodePacked32();
int numIntentions = decodePacked32();
if(numIntentions != 1) {
System.out.println("WARNING: #intentions == "+numIntentions);
}
int keylen = decodePacked32();
if(keylen != 0) {
System.out.println("WARNING: keylen == "+keylen);
}
String name = decodeString(); String name = decodeString();
int siglen = decodePacked32(); int siglen = decodePacked32();
for(int i=0; i<siglen; i++) { for(int i=0; i<siglen; i++) {
......
...@@ -46,7 +46,15 @@ public class EncoderChannel implements Encoder { ...@@ -46,7 +46,15 @@ public class EncoderChannel implements Encoder {
int index = sample_def_registry.add(dispatcher); int index = sample_def_registry.add(dispatcher);
begin(dispatcher.getTypeDeclTag()); begin(dispatcher.getTypeDeclTag());
encodePacked32(index); encodePacked32(index);
//HERE BE DRAGONS! numintentions does not include name
// encodePacked32(dispatcher.getNumIntentions()+1);
encodePacked32(1);
encodePacked32(0); // the empty key == name
encodeString(dispatcher.getName()); encodeString(dispatcher.getName());
// byte[] intentions = dispatcher.getIntentionBytes();
// for (int k = 0 ; k < intentions.length ; k++) {
// encodeByte(intentions[k]);
// }
byte[] signature = dispatcher.getSignature(); byte[] signature = dispatcher.getSignature();
encodePacked32(signature.length); encodePacked32(signature.length);
for (int i = 0 ; i < signature.length ; i++) { for (int i = 0 ; i < signature.length ; i++) {
...@@ -92,6 +100,10 @@ public class EncoderChannel implements Encoder { ...@@ -92,6 +100,10 @@ public class EncoderChannel implements Encoder {
begin(Constant.TYPE_DEF); begin(Constant.TYPE_DEF);
encodePacked32(index); encodePacked32(index);
//HERE BE DRAGONS! numintentions does not include name
// encodePacked32(dispatcher.getNumIntentions()+1);
encodePacked32(1);
encodePacked32(0); // the empty key == name
encodeString(dispatcher.getName()); encodeString(dispatcher.getName());
encodePacked32(b.length); encodePacked32(b.length);
for(int i = 0; i<b.length; i++) { for(int i = 0; i<b.length; i++) {
...@@ -123,6 +135,10 @@ public class EncoderChannel implements Encoder { ...@@ -123,6 +135,10 @@ public class EncoderChannel implements Encoder {
int index = sample_ref_registry.add(dispatcher); int index = sample_ref_registry.add(dispatcher);
begin(Constant.SAMPLE_REF); begin(Constant.SAMPLE_REF);
encodePacked32(index); encodePacked32(index);
//HERE BE DRAGONS! numintentions does not include name
// encodePacked32(dispatcher.getNumIntentions()+1);
encodePacked32(1);
encodePacked32(0); // the empty key == name
encodeString(dispatcher.getName()); encodeString(dispatcher.getName());
byte[] signature = dispatcher.getSignature(); byte[] signature = dispatcher.getSignature();
encodePacked32(signature.length); encodePacked32(signature.length);
......
...@@ -3,13 +3,17 @@ package se.lth.control.labcomm2014; ...@@ -3,13 +3,17 @@ package se.lth.control.labcomm2014;
import java.io.IOException; import java.io.IOException;
public interface SampleDispatcher <T extends SampleType>{ public interface SampleDispatcher <T extends SampleType>{
public Class<T> getSampleClass(); public Class<T> getSampleClass();
public String getName(); public String getName();
public byte[] getSignature(); public byte[] getSignature();
public int getNumIntentions();
public byte[] getIntentionBytes();
public void decodeAndHandle(Decoder decoder, public void decodeAndHandle(Decoder decoder,
SampleHandler handler) throws Exception; SampleHandler handler) throws Exception;
...@@ -23,7 +27,7 @@ public interface SampleDispatcher <T extends SampleType>{ ...@@ -23,7 +27,7 @@ public interface SampleDispatcher <T extends SampleType>{
* by encoder.register. * by encoder.register.
* TODO: refactor types, moving this to a super-interface * TODO: refactor types, moving this to a super-interface
* applicable to both type and sample defs. * applicable to both type and sample defs.
*/ */
public byte getTypeDeclTag(); public byte getTypeDeclTag();
} }
...@@ -77,6 +77,14 @@ public class TypeBinding implements BuiltinType { ...@@ -77,6 +77,14 @@ public class TypeBinding implements BuiltinType {
return null; // not used for matching return null; // not used for matching
} }
public int getNumIntentions() {
return 0;
}
public byte[] getIntentionBytes() {
return null; // not used for matching
}
public void encodeTypeDef(Encoder e, int index) throws IOException{ public void encodeTypeDef(Encoder e, int index) throws IOException{
throw new Error("Should not be called"); throw new Error("Should not be called");
} }
......
...@@ -24,7 +24,7 @@ public class TypeDef implements BuiltinType { ...@@ -24,7 +24,7 @@ public class TypeDef implements BuiltinType {
public String toString() { public String toString() {
return getName(); return getName();
} }
public byte[] getSignature() { public byte[] getSignature() {
return signature; return signature;
} }
...@@ -39,32 +39,32 @@ public class TypeDef implements BuiltinType { ...@@ -39,32 +39,32 @@ public class TypeDef implements BuiltinType {
public interface Handler extends SampleHandler { public interface Handler extends SampleHandler {
public void handle_TypeDef(TypeDef value) throws Exception; public void handle_TypeDef(TypeDef value) throws Exception;
} }
public static void register(Decoder d, Handler h) throws IOException { public static void register(Decoder d, Handler h) throws IOException {
d.register(Dispatcher.singleton(), h); d.register(Dispatcher.singleton(), h);
} }
public static void register(Encoder e) throws IOException { public static void register(Encoder e) throws IOException {
register(e,false); register(e,false);
} }
public static void register(Encoder e, boolean sendMetaData) throws IOException { public static void register(Encoder e, boolean sendMetaData) throws IOException {
throw new IOException("cannot send TypeDefs"); throw new IOException("cannot send TypeDefs");
} }
static class Dispatcher implements SampleDispatcher<TypeDef> { static class Dispatcher implements SampleDispatcher<TypeDef> {
private static Dispatcher singleton; private static Dispatcher singleton;
public synchronized static Dispatcher singleton() { public synchronized static Dispatcher singleton() {
if(singleton==null) singleton=new Dispatcher(); if(singleton==null) singleton=new Dispatcher();
return singleton; return singleton;
} }
public Class<TypeDef> getSampleClass() { public Class<TypeDef> getSampleClass() {
return TypeDef.class; return TypeDef.class;
} }
public String getName() { public String getName() {
return "TypeDef"; return "TypeDef";
} }
...@@ -72,41 +72,49 @@ public class TypeDef implements BuiltinType { ...@@ -72,41 +72,49 @@ public class TypeDef implements BuiltinType {
public byte getTypeDeclTag() { public byte getTypeDeclTag() {
throw new Error("Should not be called"); throw new Error("Should not be called");
} }
public boolean isSample() { public boolean isSample() {
throw new Error("Should not be called"); throw new Error("Should not be called");
} }
public boolean hasStaticSignature() { public boolean hasStaticSignature() {
throw new Error("Should not be called"); throw new Error("Should not be called");
} }
/** return the flat signature. Intended use is on decoder side */ /** return the flat signature. Intended use is on decoder side */
public byte[] getSignature() { public byte[] getSignature() {
return null; // not used for matching return null; // not used for matching
} }
public int getNumIntentions() {
return 0;
}
public byte[] getIntentionBytes() {
return new byte[0];
}
public void encodeTypeDef(Encoder e, int index) throws IOException{ public void encodeTypeDef(Encoder e, int index) throws IOException{
throw new Error("Should not be called"); throw new Error("Should not be called");
} }
// public boolean canDecodeAndHandle() { // public boolean canDecodeAndHandle() {
// return true; // return true;
// } // }
public void decodeAndHandle(Decoder d, public void decodeAndHandle(Decoder d,
SampleHandler h) throws Exception { SampleHandler h) throws Exception {
((Handler)h).handle_TypeDef(TypeDef.decode(d)); ((Handler)h).handle_TypeDef(TypeDef.decode(d));
} }
public boolean hasDependencies() { public boolean hasDependencies() {
return false; return false;
} }
} }
public static void encode(Encoder e, TypeDef value) throws IOException { public static void encode(Encoder e, TypeDef value) throws IOException {
throw new Error("Should not be called"); throw new Error("Should not be called");
} }
protected TypeDef() { protected TypeDef() {
} }
...@@ -119,6 +127,14 @@ public class TypeDef implements BuiltinType { ...@@ -119,6 +127,14 @@ public class TypeDef implements BuiltinType {
public static TypeDef decode(Decoder d) throws IOException { public static TypeDef decode(Decoder d) throws IOException {
TypeDef result; TypeDef result;
int index = d.decodePacked32(); int index = d.decodePacked32();
int numIntentions = d.decodePacked32();
if(numIntentions != 1) {
System.out.println("WARNING: #intentions == "+numIntentions);
}
int keylen = d.decodePacked32();
if(keylen != 0) {
System.out.println("WARNING: keylen == "+keylen);
}
String name = d.decodeString(); String name = d.decodeString();
int siglen= d.decodePacked32(); int siglen= d.decodePacked32();
byte sig[] = new byte[siglen]; byte sig[] = new byte[siglen];
......
...@@ -547,7 +547,7 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler { ...@@ -547,7 +547,7 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
String getCurrentName() { String getCurrentName() {
return current.getName(); return current.getName();
} }
String decodeIntentions() throws IOException { String decodeIntentions() throws IOException {
int n = decodePacked32() & 0xffffffff; int n = decodePacked32() & 0xffffffff;
if(n==0) return ""; if(n==0) return ""<