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();;
while(it.hasNext()) {
if(!it.next().getKey().equals("")) {
error("TypeDecl " + getName() + " has intentions. (Not allowed for typedefs)"); 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);
......
...@@ -10,6 +10,10 @@ public interface SampleDispatcher <T extends SampleType>{ ...@@ -10,6 +10,10 @@ public interface SampleDispatcher <T extends SampleType>{
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;
......
...@@ -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");
} }
......
...@@ -85,6 +85,14 @@ public class TypeDef implements BuiltinType { ...@@ -85,6 +85,14 @@ public class TypeDef implements BuiltinType {
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");
} }
...@@ -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];
......
...@@ -401,9 +401,9 @@ class sampledef_or_sampleref_or_typedef(type_decl): ...@@ -401,9 +401,9 @@ class sampledef_or_sampleref_or_typedef(type_decl):
encoder.encode_type(self.type_index) encoder.encode_type(self.type_index)
with length_encoder(encoder) as e1: with length_encoder(encoder) as e1:
e1.encode_type(self.get_index(encoder)) e1.encode_type(self.get_index(encoder))
e1.encode_string(self.name) # XXX: temporary hack for intentions
e1.encode_empty_intentions(self.name)
with length_encoder(e1) as e2: with length_encoder(e1) as e2:
self.encode_empty_intentions(e2)
self.decl.encode_decl(e2) self.decl.encode_decl(e2)
def encode(self, encoder, value): def encode(self, encoder, value):
...@@ -411,10 +411,11 @@ class sampledef_or_sampleref_or_typedef(type_decl): ...@@ -411,10 +411,11 @@ class sampledef_or_sampleref_or_typedef(type_decl):
def decode_decl(self, decoder): def decode_decl(self, decoder):
index = decoder.decode_type_number() index = decoder.decode_type_number()
name = decoder.decode_string() # XXX: temporary hack for intentions
# assume the name is the only intention
name = decoder.decode_intentions()
if usePacketLength(decoder.version): if usePacketLength(decoder.version):
length = decoder.decode_packed32() length = decoder.decode_packed32()
self.decode_intentions(decoder)
decl = decoder.decode_decl() decl = decoder.decode_decl()
result = self.__class__.__new__(self.__class__) result = self.__class__.__new__(self.__class__)
result.__init__(name=name, decl=decl) result.__init__(name=name, decl=decl)
...@@ -456,12 +457,6 @@ class sample_def(sampledef_or_sampleref_or_typedef): ...@@ -456,12 +457,6 @@ class sample_def(sampledef_or_sampleref_or_typedef):
def rename(self, name): def rename(self, name):
return sample_def(name=name, decl=self.decl) return sample_def(name=name, decl=self.decl)
def decode_intentions(self, d):
return d.decode_string()
def encode_empty_intentions(self, e):
e.encode_empty_intentions()
class sample_ref(sampledef_or_sampleref_or_typedef): class sample_ref(sampledef_or_sampleref_or_typedef):
type_index = i_SAMPLE_REF type_index = i_SAMPLE_REF
type_name = 'sample_ref' type_name = 'sample_ref'
...@@ -474,12 +469,6 @@ class sample_ref(sampledef_or_sampleref_or_typedef): ...@@ -474,12 +469,6 @@ class sample_ref(sampledef_or_sampleref_or_typedef):
else: else:
self.sample = sample self.sample = sample
def decode_intentions(self, d):
return d.decode_string()
def encode_empty_intentions(self, e):
e.encode_empty_intentions()
def get_index(self, encoder): def get_index(self, encoder):
return encoder.ref_to_index[self.sample] return encoder.ref_to_index[self.sample]
...@@ -490,12 +479,6 @@ class typedef(sampledef_or_sampleref_or_typedef): ...@@ -490,12 +479,6 @@ class typedef(sampledef_or_sampleref_or_typedef):
type_index = i_TYPE_DEF type_index = i_TYPE_DEF
type_name = 'typedef' type_name = 'typedef'
def decode_intentions(self, d):
pass
def encode_empty_intentions(self, e):
pass
def encode_decl(self, encoder): def encode_decl(self, encoder):
self.decl.encode_decl(encoder) self.decl.encode_decl(encoder)
...@@ -647,8 +630,7 @@ class struct(type_decl): ...@@ -647,8 +630,7 @@ class struct(type_decl):
encoder.encode_type(i_STRUCT) encoder.encode_type(i_STRUCT)
encoder.encode_packed32(len(self.field)) encoder.encode_packed32(len(self.field))
for (name, decl) in self.field: for (name, decl) in self.field:
encoder.encode_empty_intentions() encoder.encode_empty_intentions(name)
encoder.encode_string(name)
encoder.encode_type_number(decl) encoder.encode_type_number(decl)
def encode(self, encoder, obj): def encode(self, encoder, obj):
...@@ -663,8 +645,7 @@ class struct(type_decl): ...@@ -663,8 +645,7 @@ class struct(type_decl):
n_field = decoder.decode_packed32() n_field = decoder.decode_packed32()
field = [] field = []
for i in range(n_field): for i in range(n_field):
intentions = decoder.decode_intentions() name = decoder.decode_intentions()
name = decoder.decode_string()
decl = decoder.decode_decl() decl = decoder.decode_decl()
field.append((name, decl)) field.append((name, decl))
return struct(field) return struct(field)
...@@ -891,9 +872,11 @@ class Encoder(Codec): ...@@ -891,9 +872,11 @@ class Encoder(Codec):
self.encode_packed32(len(s)); self.encode_packed32(len(s));
self.pack("%ds" % len(s),s) self.pack("%ds" % len(s),s)
def encode_empty_intentions(self): def encode_empty_intentions(self, name):
# pass # pass
self.encode_packed32(1)
self.encode_string("") self.encode_string("")
self.encode_string(name)
class Decoder(Codec): class Decoder(Codec):
def __init__(self, reader, version=DEFAULT_VERSION): def __init__(self, reader, version=DEFAULT_VERSION):
...@@ -1027,7 +1010,14 @@ class Decoder(Codec): ...@@ -1027,7 +1010,14 @@ class Decoder(Codec):
return self.index_to_ref.get(index, None) return self.index_to_ref.get(index, None)
def decode_intentions(self): def decode_intentions(self):
return self.decode_string() numIntentions = self.decode_packed32()
name = ""
for i in range(numIntentions):
key = self.decode_string()
val = self.decode_string()
if key=="":
name = val
return name
class signature_reader: class signature_reader:
def __init__(self, signature): def __init__(self, signature):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment