Commit b621207a authored by Anders Blomdell's avatar Anders Blomdell
Browse files

Made sample references a primitive type.

C and python passes some tests...
parent 9b3847e0
...@@ -546,6 +546,7 @@ aspect CS_Class { ...@@ -546,6 +546,7 @@ aspect CS_Class {
case LABCOMM_FLOAT: { env.print("e.encodeFloat"); } break; case LABCOMM_FLOAT: { env.print("e.encodeFloat"); } break;
case LABCOMM_DOUBLE: { env.print("e.encodeDouble"); } break; case LABCOMM_DOUBLE: { env.print("e.encodeDouble"); } break;
case LABCOMM_STRING: { env.print("e.encodeString"); } break; case LABCOMM_STRING: { env.print("e.encodeString"); } break;
case LABCOMM_SAMPLE: { env.println("e.encodeSampleRef"); } break;
} }
env.println("(" + name + ");"); env.println("(" + name + ");");
} }
...@@ -640,6 +641,11 @@ aspect CS_Class { ...@@ -640,6 +641,11 @@ aspect CS_Class {
case LABCOMM_FLOAT: { env.println("d.decodeFloat();"); } break; case LABCOMM_FLOAT: { env.println("d.decodeFloat();"); } break;
case LABCOMM_DOUBLE: { env.println("d.decodeDouble();"); } break; case LABCOMM_DOUBLE: { env.println("d.decodeDouble();"); } break;
case LABCOMM_STRING: { env.println("d.decodeString();"); } break; case LABCOMM_STRING: { env.println("d.decodeString();"); } break;
case LABCOMM_SAMPLE: { env.println("d.decodeSampleRef();"); } break;
default: {
throw new Error("PrimType.CS_emitDecoder(CS_env env, String name)" +
" unknown token type");
}
} }
} }
...@@ -720,13 +726,10 @@ aspect CS_Class { ...@@ -720,13 +726,10 @@ aspect CS_Class {
" not declared"); " not declared");
} }
public void SampleRefType.CS_emitTypePrefix(CS_env env) {
env.print("Sample");
}
public void PrimType.CS_emitTypePrefix(CS_env env) { public void PrimType.CS_emitTypePrefix(CS_env env) {
switch (getToken()) { switch (getToken()) {
case LABCOMM_STRING: { env.print("String"); } break; case LABCOMM_STRING: { env.print("String"); } break;
case LABCOMM_SAMPLE: { env.print("Sample"); } break;
default: { env.print(getName()); } break; default: { env.print(getName()); } break;
} }
} }
...@@ -848,10 +851,6 @@ aspect CS_Class { ...@@ -848,10 +851,6 @@ aspect CS_Class {
" not declared"); " not declared");
} }
public void SampleRefType.CS_emitType(CS_env env) {
env.print("Sample");
}
public void VoidType.CS_emitType(CS_env env) { public void VoidType.CS_emitType(CS_env env) {
env.print("void"); env.print("void");
} }
...@@ -860,6 +859,7 @@ aspect CS_Class { ...@@ -860,6 +859,7 @@ aspect CS_Class {
switch (getToken()) { switch (getToken()) {
case LABCOMM_STRING: { env.print("String"); } break; case LABCOMM_STRING: { env.print("String"); } break;
case LABCOMM_BOOLEAN: { env.print("bool"); } break; case LABCOMM_BOOLEAN: { env.print("bool"); } break;
case LABCOMM_SAMPLE: { env.print("Sample"); } break;
default: { env.print(getName()); } break; default: { env.print(getName()); } break;
} }
} }
......
...@@ -298,10 +298,6 @@ aspect C_Type { ...@@ -298,10 +298,6 @@ aspect C_Type {
env.print("char " + name); env.print("char " + name);
} }
public void SampleRefType.C_emitType(C_env env, String name) {
env.print("const struct labcomm_signature *" + name);
}
public void PrimType.C_emitType(C_env env, String name) { public void PrimType.C_emitType(C_env env, String name) {
switch (getToken()) { switch (getToken()) {
case LABCOMM_BOOLEAN: { env.print("uint8_t"); } break; case LABCOMM_BOOLEAN: { env.print("uint8_t"); } break;
...@@ -312,6 +308,9 @@ aspect C_Type { ...@@ -312,6 +308,9 @@ aspect C_Type {
case LABCOMM_FLOAT: { env.print("float"); } break; case LABCOMM_FLOAT: { env.print("float"); } break;
case LABCOMM_DOUBLE: { env.print("double"); } break; case LABCOMM_DOUBLE: { env.print("double"); } break;
case LABCOMM_STRING: { env.print("char*"); } break; case LABCOMM_STRING: { env.print("char*"); } break;
case LABCOMM_SAMPLE: {
env.print("const struct labcomm_signature *");
} break;
} }
env.print(" " + name); env.print(" " + name);
} }
...@@ -523,12 +522,17 @@ aspect C_Decoder { ...@@ -523,12 +522,17 @@ aspect C_Decoder {
public void VoidType.C_emitDecoder(C_env env) { public void VoidType.C_emitDecoder(C_env env) {
} }
public void SampleRefType.C_emitDecoder(C_env env) {
env.println(env.qualid + " = labcomm_internal_decoder_index_to_signature(" +
"r->decoder, labcomm"+env.verStr+"_read_int(r));");
}
public void PrimType.C_emitDecoder(C_env env) { public void PrimType.C_emitDecoder(C_env env) {
env.println(env.qualid + " = labcomm"+env.verStr+"_read_" + getName() + "(r);"); env.print(env.qualid + " = ");
switch (getToken()) {
case LABCOMM_SAMPLE: {
env.println("labcomm_internal_decoder_index_to_signature(" +
"r->decoder, labcomm"+env.verStr+"_read_int(r));");
} break;
default: {
env.println("labcomm"+env.verStr+"_read_" + getName() + "(r);");
}; break;
}
} }
public void UserType.C_emitDecoder(C_env env) { public void UserType.C_emitDecoder(C_env env) {
...@@ -604,9 +608,6 @@ aspect C_Decoder { ...@@ -604,9 +608,6 @@ aspect C_Decoder {
" not declared"); " not declared");
} }
public void SampleRefType.C_emitDecoderDeallocation(C_env env) {
}
public void PrimType.C_emitDecoderDeallocation(C_env env) { public void PrimType.C_emitDecoderDeallocation(C_env env) {
if (C_isDynamic()) { if (C_isDynamic()) {
env.println("labcomm"+env.verStr+"_memory_free(r->memory, 1, " + env.println("labcomm"+env.verStr+"_memory_free(r->memory, 1, " +
...@@ -759,12 +760,6 @@ aspect C_copy { ...@@ -759,12 +760,6 @@ aspect C_copy {
public void VoidType.C_emitCopy(C_env env_src, C_env env_dst) { public void VoidType.C_emitCopy(C_env env_src, C_env env_dst) {
} }
public void SampleRefType.C_emitCopy(C_env env_src, C_env env_dst) {
env_src.println(env_dst.accessor() + env_dst.qualid + " = " +
env_src.accessor() + env_src.qualid + ";");
}
public void PrimType.C_emitCopy(C_env env_src, C_env env_dst) { public void PrimType.C_emitCopy(C_env env_src, C_env env_dst) {
if (C_isDynamic()) { if (C_isDynamic()) {
env_src.println(String.format( env_src.println(String.format(
...@@ -908,9 +903,6 @@ aspect C_copy { ...@@ -908,9 +903,6 @@ aspect C_copy {
public void VoidType.C_emitCopyDeallocation(C_env env) { public void VoidType.C_emitCopyDeallocation(C_env env) {
} }
public void SampleRefType.C_emitCopyDeallocation(C_env env) {
}
public void PrimType.C_emitCopyDeallocation(C_env env) { public void PrimType.C_emitCopyDeallocation(C_env env) {
if (C_isDynamic()) { if (C_isDynamic()) {
env.println("labcomm" + env.verStr + "_memory_free(mem, 1, " + env.println("labcomm" + env.verStr + "_memory_free(mem, 1, " +
...@@ -1065,16 +1057,19 @@ aspect C_Encoder { ...@@ -1065,16 +1057,19 @@ aspect C_Encoder {
env.println("result = 0;"); env.println("result = 0;");
} }
public void SampleRefType.C_emitEncoder(C_env env) {
env.println("result = labcomm"+env.verStr+"_write_int(w, " +
"labcomm_internal_encoder_signature_to_index(w->encoder, " +
env.qualid + "));");
env.println("if (result != 0) { return result; }");
}
public void PrimType.C_emitEncoder(C_env env) { public void PrimType.C_emitEncoder(C_env env) {
env.println("result = labcomm"+env.verStr+"_write_" + getName() + env.print("result = ");
"(w, " + env.qualid + ");"); switch (getToken()) {
case LABCOMM_SAMPLE: {
env.println("labcomm"+env.verStr+"_write_int(w, " +
"labcomm_internal_encoder_signature_to_index(w->encoder, " +
env.qualid + "));");
} break;
default: {
env.println("labcomm"+env.verStr+"_write_" + getName() +
"(w, " + env.qualid + ");");
} break;
}
env.println("if (result != 0) { return result; }"); env.println("if (result != 0) { return result; }");
} }
...@@ -1496,10 +1491,6 @@ aspect C_Sizeof { ...@@ -1496,10 +1491,6 @@ aspect C_Sizeof {
return 0; return 0;
} }
public int SampleRefType.C_fixedSizeof() {
return 4;
}
public int PrimType.C_fixedSizeof() { public int PrimType.C_fixedSizeof() {
switch (getToken()) { switch (getToken()) {
case LABCOMM_BOOLEAN: { return 1; } case LABCOMM_BOOLEAN: { return 1; }
...@@ -1509,6 +1500,7 @@ aspect C_Sizeof { ...@@ -1509,6 +1500,7 @@ aspect C_Sizeof {
case LABCOMM_LONG: { return 8; } case LABCOMM_LONG: { return 8; }
case LABCOMM_FLOAT: { return 4; } case LABCOMM_FLOAT: { return 4; }
case LABCOMM_DOUBLE: { return 8; } case LABCOMM_DOUBLE: { return 8; }
case LABCOMM_SAMPLE: { return 4; }
default: { default: {
throw new Error(this.getClass().getName() + throw new Error(this.getClass().getName() +
".C_fixedSizeof()" + ".C_fixedSizeof()" +
......
...@@ -645,6 +645,7 @@ aspect Java_Class { ...@@ -645,6 +645,7 @@ aspect Java_Class {
case LABCOMM_FLOAT: { env.print("e.encodeFloat"); } break; case LABCOMM_FLOAT: { env.print("e.encodeFloat"); } break;
case LABCOMM_DOUBLE: { env.print("e.encodeDouble"); } break; case LABCOMM_DOUBLE: { env.print("e.encodeDouble"); } break;
case LABCOMM_STRING: { env.print("e.encodeString"); } break; case LABCOMM_STRING: { env.print("e.encodeString"); } break;
case LABCOMM_SAMPLE: { env.print("e.encodeSampleRef"); } break;
} }
env.println("(" + name + ");"); env.println("(" + name + ");");
} }
...@@ -735,6 +736,7 @@ aspect Java_Class { ...@@ -735,6 +736,7 @@ aspect Java_Class {
case LABCOMM_FLOAT: { env.println("d.decodeFloat();"); } break; case LABCOMM_FLOAT: { env.println("d.decodeFloat();"); } break;
case LABCOMM_DOUBLE: { env.println("d.decodeDouble();"); } break; case LABCOMM_DOUBLE: { env.println("d.decodeDouble();"); } break;
case LABCOMM_STRING: { env.println("d.decodeString();"); } break; case LABCOMM_STRING: { env.println("d.decodeString();"); } break;
case LABCOMM_SAMPLE: { env.println("d.decodeSampleRef();"); } break;
} }
} }
...@@ -819,6 +821,7 @@ aspect Java_Class { ...@@ -819,6 +821,7 @@ aspect Java_Class {
public void PrimType.Java_emitTypePrefix(Java_env env) { public void PrimType.Java_emitTypePrefix(Java_env env) {
switch (getToken()) { switch (getToken()) {
case LABCOMM_STRING: { env.print("String"); } break; case LABCOMM_STRING: { env.print("String"); } break;
case LABCOMM_SAMPLE: { env.print("Sample"); } break;
default: { env.print(getName()); } break; default: { env.print(getName()); } break;
} }
} }
...@@ -938,10 +941,6 @@ aspect Java_Class { ...@@ -938,10 +941,6 @@ aspect Java_Class {
" not declared"); " not declared");
} }
public void SampleRefType.Java_emitType(Java_env env) {
env.print("Sample");
}
public void VoidType.Java_emitType(Java_env env) { public void VoidType.Java_emitType(Java_env env) {
env.print("void"); env.print("void");
} }
...@@ -949,6 +948,7 @@ aspect Java_Class { ...@@ -949,6 +948,7 @@ aspect Java_Class {
public void PrimType.Java_emitType(Java_env env) { public void PrimType.Java_emitType(Java_env env) {
switch (getToken()) { switch (getToken()) {
case LABCOMM_STRING: { env.print("String"); } break; case LABCOMM_STRING: { env.print("String"); } break;
case LABCOMM_SAMPLE: { env.print("Sample"); } break;
default: { env.print(getName()); } break; default: { env.print(getName()); } break;
} }
} }
......
...@@ -77,7 +77,6 @@ Type type = ...@@ -77,7 +77,6 @@ Type type =
| user_type.u {: return u; :} | user_type.u {: return u; :}
| struct_type.s {: return s; :} | struct_type.s {: return s; :}
| void_type.v {: return v; :} | void_type.v {: return v; :}
| sample_ref_type.s {: return s; :}
; ;
PrimType prim_type = PrimType prim_type =
...@@ -97,6 +96,8 @@ PrimType prim_type = ...@@ -97,6 +96,8 @@ PrimType prim_type =
{: return new PrimType(DOUBLE, ASTNode.LABCOMM_DOUBLE); :} {: return new PrimType(DOUBLE, ASTNode.LABCOMM_DOUBLE); :}
| STRING | STRING
{: return new PrimType(STRING, ASTNode.LABCOMM_STRING); :} {: return new PrimType(STRING, ASTNode.LABCOMM_STRING); :}
| SAMPLE
{: return new PrimType(SAMPLE, ASTNode.LABCOMM_SAMPLE); :}
; ;
UserType user_type = UserType user_type =
...@@ -111,10 +112,6 @@ VoidType void_type = ...@@ -111,10 +112,6 @@ VoidType void_type =
VOID {: return new VoidType(); :} VOID {: return new VoidType(); :}
; ;
SampleRefType sample_ref_type =
SAMPLE {: return new SampleRefType(); :}
;
List dim_list = List dim_list =
dim.d {: return new List().add(d); :} dim.d {: return new List().add(d); :}
| dim_list.l dim.d {: return l.add(d); :} | dim_list.l dim.d {: return l.add(d); :}
......
...@@ -16,5 +16,6 @@ aspect LabCommTokens { ...@@ -16,5 +16,6 @@ aspect LabCommTokens {
public static final int ASTNode.LABCOMM_FLOAT = 0x25; public static final int ASTNode.LABCOMM_FLOAT = 0x25;
public static final int ASTNode.LABCOMM_DOUBLE = 0x26; public static final int ASTNode.LABCOMM_DOUBLE = 0x26;
public static final int ASTNode.LABCOMM_STRING = 0x27; public static final int ASTNode.LABCOMM_STRING = 0x27;
public static final int ASTNode.LABCOMM_SAMPLE = 0x28;
} }
...@@ -152,10 +152,6 @@ aspect PythonTypes { ...@@ -152,10 +152,6 @@ aspect PythonTypes {
" not declared"); " not declared");
} }
public void SampleRefType.Python_genSignature(Python_env env) {
env.print("labcomm.SAMPLE_REF()");
}
public void PrimType.Python_genSignature(Python_env env) { public void PrimType.Python_genSignature(Python_env env) {
switch (getToken()) { switch (getToken()) {
case LABCOMM_BOOLEAN: { env.print("labcomm.BOOLEAN()"); } break; case LABCOMM_BOOLEAN: { env.print("labcomm.BOOLEAN()"); } break;
...@@ -166,6 +162,7 @@ aspect PythonTypes { ...@@ -166,6 +162,7 @@ aspect PythonTypes {
case LABCOMM_FLOAT: { env.print("labcomm.FLOAT()"); } break; case LABCOMM_FLOAT: { env.print("labcomm.FLOAT()"); } break;
case LABCOMM_DOUBLE: { env.print("labcomm.DOUBLE()"); } break; case LABCOMM_DOUBLE: { env.print("labcomm.DOUBLE()"); } break;
case LABCOMM_STRING: { env.print("labcomm.STRING()"); } break; case LABCOMM_STRING: { env.print("labcomm.STRING()"); } break;
case LABCOMM_SAMPLE: { env.print("labcomm.SAMPLE()"); } break;
} }
} }
......
...@@ -231,9 +231,12 @@ int labcomm_internal_encoder_signature_to_index( ...@@ -231,9 +231,12 @@ int labcomm_internal_encoder_signature_to_index(
struct labcomm_encoder *e, const struct labcomm_signature *signature) struct labcomm_encoder *e, const struct labcomm_signature *signature)
{ {
/* writer_lock should be held at this point */ /* writer_lock should be held at this point */
int index = labcomm_get_local_index(signature); int index = 0;
if (! LABCOMM_SIGNATURE_ARRAY_GET(e->sample_ref, int, index, 0)) { if (signature != NULL) {
index = 0; index = labcomm_get_local_index(signature);
if (! LABCOMM_SIGNATURE_ARRAY_GET(e->sample_ref, int, index, 0)) {
index = 0;
}
} }
return index; return index;
} }
......
...@@ -221,7 +221,7 @@ int main(void) ...@@ -221,7 +221,7 @@ int main(void)
labcomm_signature_generated_encoding_R); labcomm_signature_generated_encoding_R);
labcomm_encoder_sample_ref_register(encoder, labcomm_encoder_sample_ref_register(encoder,
labcomm_signature_generated_encoding_R); labcomm_signature_generated_encoding_R);
EXPECT({0x03, 0x08, -1, 0x01, 'R', 0x04, 0x10, 0x01, 0x04, 0x03}); EXPECT({0x03, 0x08, -1, 0x01, 'R', 0x04, 0x10, 0x01, 0x04, 0x28});
labcomm_encoder_ioctl(encoder, IOCTL_WRITER_RESET); labcomm_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
// was: labcomm_encode_generated_encoding_V(encoder, &V); // was: labcomm_encode_generated_encoding_V(encoder, &V);
......
...@@ -16,6 +16,7 @@ namespace se.lth.control.labcomm { ...@@ -16,6 +16,7 @@ namespace se.lth.control.labcomm {
double decodeDouble(); double decodeDouble();
String decodeString(); String decodeString();
int decodePacked32(); int decodePacked32();
Sample decodeSampleRef();
} }
......
...@@ -146,5 +146,9 @@ namespace se.lth.control.labcomm { ...@@ -146,5 +146,9 @@ namespace se.lth.control.labcomm {
return (int) (res & 0xffffffff); return (int) (res & 0xffffffff);
} }
public Sample decodeSampleRef() {
return null;
}
} }
} }
...@@ -17,6 +17,7 @@ namespace se.lth.control.labcomm { ...@@ -17,6 +17,7 @@ namespace se.lth.control.labcomm {
void encodeDouble(double value); void encodeDouble(double value);
void encodeString(String value); void encodeString(String value);
void encodePacked32(Int64 value); void encodePacked32(Int64 value);
void encodeSampleRef(Sample value);
} }
......
...@@ -117,5 +117,11 @@ namespace se.lth.control.labcomm { ...@@ -117,5 +117,11 @@ namespace se.lth.control.labcomm {
public void encodePacked32(Int64 value) { public void encodePacked32(Int64 value) {
WritePacked32(bytes, value); WritePacked32(bytes, value);
} }
public void encodeSampleRef(Sample value) {
WriteInt(0, 4);
throw new Exception("IMPLEMENT");
}
} }
} }
...@@ -15,5 +15,6 @@ public interface Decoder { ...@@ -15,5 +15,6 @@ public interface Decoder {
public double decodeDouble() throws IOException; public double decodeDouble() throws IOException;
public String decodeString() throws IOException; public String decodeString() throws IOException;
public int decodePacked32() throws IOException; public int decodePacked32() throws IOException;
public Sample decodeSampleRef() throws IOException;
} }
...@@ -172,5 +172,12 @@ public class DecoderChannel implements Decoder { ...@@ -172,5 +172,12 @@ public class DecoderChannel implements Decoder {
return (int) (res & 0xffffffff); return (int) (res & 0xffffffff);
} }
public Sample decodeSampleRef() throws IOException {
int index = in.readInt();
throw new IOException("IMPLEMENT");
// return null;
}
} }
...@@ -16,5 +16,6 @@ public interface Encoder { ...@@ -16,5 +16,6 @@ public interface Encoder {
public void encodeDouble(double value) throws IOException; public void encodeDouble(double value) throws IOException;
public void encodeString(String value) throws IOException; public void encodeString(String value) throws IOException;
public void encodePacked32(long value) throws IOException; public void encodePacked32(long value) throws IOException;
public void encodeSampleRef(Sample value) throws IOException;
} }
...@@ -131,5 +131,12 @@ public class EncoderChannel implements Encoder { ...@@ -131,5 +131,12 @@ public class EncoderChannel implements Encoder {
encodeByte((byte)(tmp[i] | (i!=0?0x80:0x00))); encodeByte((byte)(tmp[i] | (i!=0?0x80:0x00)));
} }
} }
public void encodeSampleRef(Sample value) throws IOException {
data.writeInt(0);
throw new IOException("IMPLEMENT");
}
} }
...@@ -156,8 +156,7 @@ DEFAULT_VERSION = "LabComm2014" ...@@ -156,8 +156,7 @@ DEFAULT_VERSION = "LabComm2014"
# Allowed packet tags # Allowed packet tags
i_VERSION = 0x01 i_VERSION = 0x01
i_SAMPLE_DEF = 0x02 i_SAMPLE_DEF = 0x02
i_TYPE_DEF = 0x03 i_SAMPLE_REF = 0x03
i_TYPE_BINDING= 0x04
i_PRAGMA = 0x3f i_PRAGMA = 0x3f
i_USER = 0x40 # ..0xffffffff i_USER = 0x40 # ..0xffffffff
...@@ -173,6 +172,7 @@ i_LONG = 0x24 ...@@ -173,6 +172,7 @@ i_LONG = 0x24
i_FLOAT = 0x25 i_FLOAT = 0x25
i_DOUBLE = 0x26 i_DOUBLE = 0x26
i_STRING = 0x27 i_STRING = 0x27
i_SAMPLE = 0x28
# Version testing # Version testing
...@@ -189,7 +189,7 @@ class length_encoder: ...@@ -189,7 +189,7 @@ class length_encoder:
self.data += data self.data += data
def __enter__(self): def __enter__(self):
return Encoder(self, None) return Encoder(writer=self, version=None, codec=self.encoder)
def __exit__(self, type, value, traceback): def __exit__(self, type, value, traceback):
if usePacketLength(self.version): if usePacketLength(self.version):
...@@ -334,18 +334,38 @@ class STRING(primitive): ...@@ -334,18 +334,38 @@ class STRING(primitive):
def __repr__(self): def __repr__(self):
return "labcomm.STRING()" return "labcomm.STRING()"
class SAMPLE(primitive):
def encode_decl(self, encoder):
return encoder.encode_type(i_SAMPLE)
def encode(self, encoder, value):
return encoder.encode_int(encoder.ref_to_index.get(value, 0))
def decode(self, decoder, obj=None):
return decoder.decode_ref()
def new_instance(self):
return ""
def __eq__(self, other):
return self.__class__ == other.__class__
def __repr__(self):
return "labcomm.SAMPLE()"
# #
# Aggregate types # Aggregate types
# #
class sample(object): class sample_def_or_ref(object):
def __init__(self, name, decl): def __init__(self, name=None, decl=None):
self.name = name self.name = name
self.decl = decl self.decl = decl
def encode_decl(self, encoder): def encode_decl(self, encoder):
encoder.encode_type(i_SAMPLE_DEF) encoder.encode_type(self.type_index)
with