Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • anders.blomdell
  • compiler-refactoring
  • labcomm2006
  • labcomm2013
  • labcomm2014
  • master
  • pragma
  • python_sig_hash
  • typedefs
  • typeref
  • v2006.0
  • v2013.0
  • v2014.0
  • v2014.1
  • v2014.2
  • v2014.3
  • v2014.4
  • v2014.5
  • v2014.6
  • v2015.0
20 results

Target

Select target project
  • anders_blomdell/labcomm
  • klaren/labcomm
  • tommyo/labcomm
  • erikj/labcomm
  • sven/labcomm
5 results
Select Git revision
  • anders.blomdell
  • compiler-refactoring
  • labcomm2006
  • labcomm2013
  • master
  • pragma
  • typedefs
  • typeref
  • v2006.0
  • v2013.0
  • v2014.0
  • v2014.1
  • v2014.2
  • v2014.3
  • v2014.4
15 results
Show changes
Showing
with 844 additions and 477 deletions
package se.lth.control.labcomm2014;
public class ParsedTypeDef extends SigTypeDef{
private int idx;
ParsedTypeDef(int idx, String name){
super(name);
this.idx = idx;
}
ParsedTypeDef(int idx, String name, DataType type) {
super(name, type);
this.idx = idx;
}
ParsedTypeDef(ParsedTypeDef td) {
super(td);
this.idx = td.getIndex();
}
int getIndex() {
return idx;
}
}
package se.lth.control.labcomm2014;
import java.util.Iterator;
import java.io.IOException;
public interface SampleDispatcher <T extends SampleType>{
......@@ -10,6 +10,10 @@ public interface SampleDispatcher <T extends SampleType>{
public byte[] getSignature();
public int getNumIntentions();
public byte[] getIntentionBytes();
public void decodeAndHandle(Decoder decoder,
SampleHandler handler) throws Exception;
......@@ -25,5 +29,11 @@ public interface SampleDispatcher <T extends SampleType>{
* applicable to both type and sample defs.
*/
public byte getTypeDeclTag();
public void registerTypeDeps(Encoder e) throws IOException;
public Iterator<SampleDispatcher> getDependencyIterator();
public DataType getDataType();
}
package se.lth.control.labcomm2014;
public class SampleSymbol implements SignatureSymbol {
public String toString() {
return "sample ";
}
public void accept(SignatureSymbolVisitor v){
v.visit(this);
}
}
package se.lth.control.labcomm2014;
import java.io.PrintStream;
public class SigArrayType extends DataType {
private int idx[];
private DataType type;
public SigArrayType(DataType elementType, int idx[]) {
super("array", Constant.ARRAY);
this.idx = idx;
this.type = elementType;
}
public DataType getType() {
return type;
}
public int[] getIdx() {
return idx;
}
public boolean isArray() {
return true;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(type.toString());
for(int i : idx) {
sb.append("["+(i>0 ? i : "_")+"]");
}
return sb.toString();
}
public void accept(SignatureSymbolVisitor v) {
v.visit(this);
}
public void print(PrintStream out, String indent) {
type.print(out,indent);
out.print("[");
for(int i =0; i<idx.length;i++) {
int d = idx[i];
out.print( (d==-1?"-":d));
if(i<idx.length-1) {
out.print(", ");
}
}
out.print("]");
}
}
package se.lth.control.labcomm2014;
import java.io.PrintStream;
public class SigField implements SignatureSymbol{
private DataType type;
private String name;
SigField(String name, DataType type) {
this.name = name;
this.type = type;
}
public DataType getType() {
return type;
}
public String getName() {
return name;
}
public String toString() {
return type.toString() + " " + name;
}
public void accept(SignatureSymbolVisitor v) {
v.visit(this);
}
public void print(PrintStream out, String indent) {
type.print(out, indent);
out.println(" "+name+";");
}
}
package se.lth.control.labcomm2014;
public class SigPrimitiveType extends DataType {
public SigPrimitiveType(int tag) {
super(tagName(tag), tag);
}
public void accept(SignatureSymbolVisitor v) {
v.visit(this);
}
public String toString() {
return getName();}
}
package se.lth.control.labcomm2014;
public class SigSampleDef extends SigTypeDef{
SigSampleDef(SigTypeDef td) {
super(td.getName(), td.getType());
}
@Override
public boolean isSampleDef() {
return true;
}
public String defType() {
return "sample";
}
}
package se.lth.control.labcomm2014;
import java.io.PrintStream;
import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;
public class SigStructType extends DataType {
private List<SigField> fields;
SigStructType(int nSigFields) {
super("struct", Constant.STRUCT);
this.fields = new LinkedList<SigField>();
}
public SigStructType() {
this("struct");
}
protected SigStructType(String name) {
super(name, Constant.STRUCT);
this.fields = new LinkedList<SigField>();
}
public SigField[] getFields() {
return fields.toArray(new SigField[0]);
}
public Iterator<SigField> getFieldIterator() {
return fields.iterator();
}
public void addField(String name, DataType type) {
fields.add(new SigField(name, type));
}
public void addField(SigField f) {
fields.add(f);
}
public boolean isStruct() {
return true;
}
public boolean isVoid() {
return fields.size() == 0;
}
public String toString() {
if(isVoid()) { //void type is empty struct
return "void";
} else {
StringBuilder sb = new StringBuilder();
sb.append("struct {\n");
for(SigField f : fields) {
sb.append(f.toString());
sb.append(";\n");
}
sb.append("}");
return sb.toString();
}
}
public void accept(SignatureSymbolVisitor v) {
v.visit(this);
}
public void print(PrintStream out, String indent) {
out.println("struct {");
String newIndent=indent+" ";
out.print(newIndent);
Iterator<SigField> it = getFieldIterator();
while(it.hasNext()) {
it.next().print(out, newIndent);
if(it.hasNext()) {
out.print(newIndent);
}
}
out.print(indent+"}");
}
}
package se.lth.control.labcomm2014;
import java.util.HashSet;
import java.util.Iterator;
public class SigTypeDef{
private String name;
private DataType type;
protected HashSet<SigTypeDef> deps;
SigTypeDef(SigTypeDef td) {
this(td.getName(), td.getType());
this.deps = new HashSet<SigTypeDef>(td.deps);
}
SigTypeDef( String name){
this.name = name;
this.deps = new HashSet<SigTypeDef>();
}
SigTypeDef(String name, DataType type) {
this(name);
this.type = type;
}
void addDependency(SigTypeDef d) {
deps.add(d);
}
HashSet<SigTypeDef> getDependencies() {
return deps;
}
Iterator<SigTypeDef> getDepIterator() {
return deps.iterator();
}
/** To be overridden in SigSampleDef
*/
public boolean isSampleDef() {
return false;
}
public String defType() {
return "typedef";
}
void setType(DataType type) {
this.type = type;
}
public DataType getType() {
if(type==null) {
System.out.println("******** WARNING! SigTypeDef.getType returning null");
}
return type;
}
int getIndex() {
return 0;
}
public String getName() {
return name;
}
public String toString() {
return type.toString();
}
public int hashCode() {
return name.hashCode();
}
public boolean equals(Object o) {
if(! (o instanceof SigTypeDef)){
return false;
} else {
SigTypeDef other = (SigTypeDef) o;
return other.getIndex() == getIndex() && other.name.equals(name);
}
}
public void accept(SignatureSymbolVisitor v) {
type.accept(v);
}
}
package se.lth.control.labcomm2014;
public class SigUserType extends DataType {
public String toString() {
return getName();
}
public SigUserType(String name) {
super(name, 0);
}
public boolean isUserType() {
return true;
}
public void accept(SignatureSymbolVisitor v) {
v.visit(this);
}
}
package se.lth.control.labcomm2014;
public interface SignatureSymbol{
public void accept(SignatureSymbolVisitor v);
}
package se.lth.control.labcomm2014;
/* An interface for using Visitor pattern to traverse
* the type tree
*/
public interface SignatureSymbolVisitor {
void visit(TypeSymbol s);
void visit(SampleSymbol s);
void visit(NameSymbol s);
void visit(SigPrimitiveType t);
//sampleRefs are sent as primitive types
//Put this back if that is changed to SampleRefType
//void visit(SampleRefType t);
void visit(SigStructType t);
void visit(SigField t);
void visit(SigArrayType t);
void visit(SigUserType t);
}
......@@ -3,6 +3,8 @@ package se.lth.control.labcomm2014;
import java.io.IOException;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.util.Iterator;
import java.util.NoSuchElementException;
import se.lth.control.labcomm2014.Decoder;
import se.lth.control.labcomm2014.DecoderChannel;
import se.lth.control.labcomm2014.SampleDispatcher;
......@@ -77,10 +79,22 @@ public class TypeBinding implements BuiltinType {
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{
throw new Error("Should not be called");
}
public void registerTypeDeps(Encoder e) throws IOException{
throw new Error("Should not be called");
}
public void decodeAndHandle(Decoder d,
SampleHandler h) throws Exception {
((Handler)h).handle_TypeBinding(TypeBinding.decode(d));
......@@ -89,6 +103,25 @@ public class TypeBinding implements BuiltinType {
public boolean hasDependencies() {
return false;
}
public Iterator<SampleDispatcher> getDependencyIterator() {
return new Iterator<SampleDispatcher>() {
public boolean hasNext() {
return false;
}
public SampleDispatcher next() throws NoSuchElementException {
throw new NoSuchElementException();
}
public void remove() throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
};
}
public DataType getDataType() {
throw new Error("not implemented");
}
}
public static void encode(Encoder e, TypeBinding value) throws IOException {
......
......@@ -3,6 +3,8 @@ package se.lth.control.labcomm2014;
import java.io.IOException;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.util.Iterator;
import java.util.NoSuchElementException;
import se.lth.control.labcomm2014.Decoder;
import se.lth.control.labcomm2014.DecoderChannel;
import se.lth.control.labcomm2014.SampleDispatcher;
......@@ -85,10 +87,22 @@ public class TypeDef implements BuiltinType {
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{
throw new Error("Should not be called");
}
public void registerTypeDeps(Encoder e) throws IOException{
throw new Error("Should not be called");
}
// public boolean canDecodeAndHandle() {
// return true;
// }
......@@ -101,6 +115,25 @@ public class TypeDef implements BuiltinType {
public boolean hasDependencies() {
return false;
}
public Iterator<SampleDispatcher> getDependencyIterator() {
return new Iterator<SampleDispatcher>() {
public boolean hasNext() {
return false;
}
public SampleDispatcher next() throws NoSuchElementException {
throw new NoSuchElementException();
}
public void remove() throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
};
}
public DataType getDataType() {
throw new Error("not implemented");
}
}
public static void encode(Encoder e, TypeDef value) throws IOException {
......@@ -119,6 +152,14 @@ public class TypeDef implements BuiltinType {
public static TypeDef decode(Decoder d) throws IOException {
TypeDef result;
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();
int siglen= d.decodePacked32();
byte sig[] = new byte[siglen];
......
......@@ -49,13 +49,14 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
}
public interface TypeDefListener {
void onTypeDef(ParsedTypeDef d);
void onTypeDef(SigTypeDef d);
}
private HashMap<Integer,TypeDef> typeDefs;
private HashMap<Integer,Integer> typeBindings;
private HashSet<TypeDefListener> listeners;
private LinkedList<ParsedSampleDef> sampleDefs;
private HashMap<Integer,ParsedTypeDef> pts;
private Decoder decoder;
protected TypeDefParser(Decoder d) {
......@@ -64,6 +65,7 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
typeBindings = new HashMap<Integer,Integer>();
listeners = new HashSet<TypeDefListener>();
sampleDefs = new LinkedList<ParsedSampleDef>();
pts = new HashMap<Integer,ParsedTypeDef>();
}
public void addListener(TypeDefListener l) {
......@@ -76,7 +78,10 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
}
public void handle_TypeDef(TypeDef d) throws java.io.IOException {
System.out.println("handle_TypeDef: "+d.getIndex());
typeDefs.put(d.getIndex(), d);
ParsedTypeDef td = parseSignatureTD(d);
pts.put(d.getIndex(), td);
}
public void handle_TypeBinding(TypeBinding d) throws java.io.IOException {
......@@ -87,6 +92,7 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
typeBindings.put(d.getSampleIndex(), d.getTypeIndex());
td = getTypeDefForIndex(d.getSampleIndex());
}
System.out.println("handle_TypeBinding: "+d.getSampleIndex());
ParsedSampleDef result = parseSignature(td);
sampleDefs.add(result);
......@@ -100,7 +106,7 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
private void notifyListener(TypeDefListener l, ParsedTypeDef d) {
l.onTypeDef(d);
if(d instanceof ParsedSampleDef) {
for(ParsedTypeDef dep : ((ParsedSampleDef)d).getDependencies()) {
for(SigTypeDef dep : ((ParsedSampleDef)d).getDependencies()) {
//do we want to change ParseTypeDef to have dependencies,
//and do recursion here?
//if so, do notifyListener(l, dep);
......@@ -130,21 +136,21 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
return res;
}
public LinkedList<ParsedSymbol> symbolify() {
public LinkedList<SignatureSymbol> symbolify() {
LinkedList<ParsedSymbol> result = new LinkedList<ParsedSymbol>();
LinkedList<SignatureSymbol> result = new LinkedList<SignatureSymbol>();
Iterator<ParsedSampleDef> sdi = sampleDefIterator();
while(sdi.hasNext()) {
ParsedSampleDef sd = sdi.next();
SigTypeDef sd = sdi.next();
result.add(new SampleSymbol());
result.add(sd.getType());
result.add(new NameSymbol(sd.getName()));
Iterator<ParsedTypeDef> di = sd.getDepIterator();
Iterator<SigTypeDef> di = sd.getDepIterator();
while(di.hasNext()) {
ParsedTypeDef d = di.next();
SigTypeDef d = di.next();
result.add(new TypeSymbol());
result.add(d.getType());
result.add(new NameSymbol(d.getName()));
......@@ -154,7 +160,7 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
}
public String symbolString() {
Iterator<ParsedSymbol> i = symbolify().iterator();
Iterator<SignatureSymbol> i = symbolify().iterator();
StringBuilder sb = new StringBuilder();
......@@ -164,65 +170,9 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
return sb.toString();
}
/* An interface for using Visitor pattern to traverse
* ParsedTypeDefs
*/
public interface ParsedSymbolVisitor {
void visit(TypeSymbol s);
void visit(SampleSymbol s);
void visit(NameSymbol s);
void visit(PrimitiveType t);
//sampleRefs are sent as primitive types
//Put this back if that is changed to SampleRefType
//void visit(SampleRefType t);
void visit(ParsedStructType t);
void visit(ParsedField t);
void visit(ArrayType t);
void visit(ParsedUserType t);
}
public abstract class ParsedSymbol{
public abstract void accept(ParsedSymbolVisitor v);
}
public class TypeSymbol extends ParsedSymbol {
public String toString() {
return "typedef ";
}
public void accept(ParsedSymbolVisitor v){
v.visit(this);
}
}
public class SampleSymbol extends ParsedSymbol {
public String toString() {
return "sample ";
}
public void accept(ParsedSymbolVisitor v){
v.visit(this);
}
}
public class NameSymbol extends ParsedSymbol {
private String name;
public NameSymbol(String name) {
this.name = name;
}
public String toString() {
return name;
}
public void accept(ParsedSymbolVisitor v){
v.visit(this);
}
}
public abstract class ParsedType extends ParsedSymbol{
}
// SampleRefType currently not sent, se above
// public class SampleRefType extends ParsedType {
// public void accept(ParsedSymbolVisitor v) {
// public class SampleRefType extends DataType {
// public void accept(SignatureSymbolVisitor v) {
// v.visit(this);
// }
//
......@@ -230,259 +180,6 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
// return "sample";}
// }
public class PrimitiveType extends ParsedType {
private final String name;
private int tag;
String getName() {
return name;
}
int getTag() {
return tag;
}
PrimitiveType(int tag) {
this.tag = tag;
switch(tag) {
case Constant.BOOLEAN:
this.name = "boolean";
break;
case Constant.BYTE:
this.name = "byte";
break;
case Constant.SHORT:
this.name = "short";
break;
case Constant.INT:
this.name = "int";
break;
case Constant.LONG:
this.name = "long";
break;
case Constant.FLOAT:
this.name = "float";
break;
case Constant.DOUBLE:
this.name = "double";
break;
case Constant.STRING:
this.name = "string";
break;
case Constant.SAMPLE:
this.name = "sample";
break;
default:
this.name = "??? unknown tag 0x"+Integer.toHexString(tag);
}
}
public void accept(ParsedSymbolVisitor v) {
v.visit(this);
}
public String toString() {
return name;}
}
public class ParsedStructType extends ParsedType {
private ParsedField fields[];
ParsedStructType(int nParsedFields) {
this.fields = new ParsedField[nParsedFields];
}
public ParsedField[] getFields() {
return fields;
}
void setParsedField(int idx, ParsedField f) {
fields[idx] = f;
}
public boolean isVoid() {
return fields.length == 0;
}
public String toString() {
if(isVoid()) { //void type is empty struct
return "void";
} else {
StringBuilder sb = new StringBuilder();
sb.append("struct {\n");
for(ParsedField f : fields) {
sb.append(f.toString());
sb.append(";\n");
}
sb.append("}");
return sb.toString();
}
}
public void accept(ParsedSymbolVisitor v) {
v.visit(this);
}
}
public class ParsedField extends ParsedSymbol{
private ParsedType type;
private String name;
ParsedField(String name, ParsedType type) {
this.name = name;
this.type = type;
}
public ParsedType getType() {
return type;
}
public String getName() {
return name;
}
public String toString() {
return type.toString() + " " + name;
}
public void accept(ParsedSymbolVisitor v) {
v.visit(this);
}
}
public class ArrayType extends ParsedType {
private int idx[];
private ParsedType type;
ArrayType(int idx[], ParsedType elementType) {
this.idx = idx;
this.type = elementType;
}
public ParsedType getType() {
return type;
}
public int[] getIdx() {
return idx;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(type.toString());
for(int i : idx) {
sb.append("["+(i>0 ? i : "_")+"]");
}
return sb.toString();
}
public void accept(ParsedSymbolVisitor v) {
v.visit(this);
}
}
public class ParsedUserType extends ParsedType {
private String name;
public String getName() {
return name;
}
public String toString() {
return name;
}
ParsedUserType(String name) {
this.name = name;
}
public void accept(ParsedSymbolVisitor v) {
v.visit(this);
}
}
public class ParsedTypeDef{
private int idx;
private String name;
private ParsedType type;
ParsedTypeDef(int idx, String name){
this.idx = idx;
this.name = name;
}
ParsedTypeDef(int idx, String name, ParsedType type) {
this(idx, name);
this.type = type;
}
/** To be overridden in ParsedSampleDef
*/
public boolean isSampleDef() {
return false;
}
void setType(ParsedType type) {
this.type = type;
}
ParsedType getType() {
return type;
}
int getIndex() {
return idx;
}
public String getName() {
return name;
}
public String toString() {
return type.toString();
}
public int hashCode() {
return name.hashCode();
}
public boolean equals(Object o) {
if(! (o instanceof ParsedTypeDef)){
return false;
} else {
ParsedTypeDef other = (ParsedTypeDef) o;
return other.idx == idx && other.name.equals(name);
}
}
public void accept(ParsedSymbolVisitor v) {
type.accept(v);
}
}
public class ParsedSampleDef extends ParsedTypeDef{
private HashSet<ParsedTypeDef> deps;
ParsedSampleDef(ParsedTypeDef td) {
super(td.getIndex(), td.getName(), td.getType());
this.deps = new HashSet<ParsedTypeDef>();
}
void addDependency(ParsedTypeDef d) {
deps.add(d);
}
@Override
public boolean isSampleDef() {
return true;
}
private HashSet<ParsedTypeDef> getDependencies() {
return deps;
}
Iterator<ParsedTypeDef> getDepIterator() {
return deps.iterator();
}
}
private class ParserState {
private ByteArrayInputStream bis;
private DataInputStream in;
......@@ -546,6 +243,40 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
return current.getName();
}
void addTypeUse(int tag) {
SigTypeDef td = pts.get(tag);
if(td != null) {
currentParsed.addDependency(td);
} else {
System.out.println("******* WARNING: TypeDefParser:addTypeUse ("+tag+"): null???");
}
}
/** return name, (if any, or "") for now */
String decodeIntentions() throws IOException {
int n = decodePacked32() & 0xffffffff;
if(n==0) return "";
String name = "";
for(int i=0; i<n;i++) {
int klen = decodePacked32() & 0xffffffff;
byte[] kchars = new byte[klen];
for(int k=0; k<klen; k++) {
kchars[k] = in.readByte();
}
int vlen = decodePacked32() & 0xffffffff;
byte[] vchars = new byte[vlen];
for(int j=0; j<vlen; j++) {
vchars[j] = in.readByte();
}
if(klen==0) {
name = new String(vchars);
}
}
return name;
}
String decodeString() throws IOException {
int len = decodePacked32() & 0xffffffff;
byte[] chars = new byte[len];
......@@ -569,6 +300,24 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
return (int) (res & 0xffffffff);
}
void skipBytes(int len) throws IOException {
for(int i=0; i<len; i++) {
in.readByte();
}
}
}
private ParsedTypeDef parseSignatureTD(TypeDef td) throws IOException{
return parseSignatureTD(td, new ParserState(td));
}
private ParsedTypeDef parseSignatureTD(TypeDef td, ParserState s) throws IOException{
ParsedTypeDef result=null;
s.popType();
result = parseTypeDef(s);
return result;
}
public ParsedSampleDef parseSignature(TypeDef td) throws IOException{
......@@ -576,74 +325,99 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
ParsedSampleDef result=null;
try {
s.popType();
result = parseSampleTypeDef(s);
while(s.moreTypes()) {
s.popType();
result.addDependency(parseTypeDef(s));
}
result = new ParsedSampleDef(parseSignatureTD(td,s));
// while(s.moreTypes()) {
// s.popType();
// result.addDependency(parseTypeDef(s));
// }
} catch(java.io.EOFException ex) {
System.out.println("EOF: self_binding");
}
return result;
}
private ArrayType parseArray(ParserState in) throws IOException {
private SigArrayType parseArray(ParserState in) throws IOException {
int numIdx = in.decodePacked32();
int idx[] = new int[numIdx];
for(int i=0; i<numIdx; i++){
idx[i] = in.decodePacked32();
}
int type = in.decodePacked32();
ParsedType elementType = lookupType(type, in);
ArrayType result = new ArrayType(idx, elementType);
DataType elementType = lookupType(type, in);
SigArrayType result = new SigArrayType(elementType, idx);
for(int i=0; i<numIdx; i++){
idx[i] = in.decodePacked32();
}
return result;
}
private ParsedStructType parseStruct(ParserState in) throws IOException {
int numParsedFields = in.decodePacked32();
ParsedStructType result = new ParsedStructType(numParsedFields);
for(int i=0; i<numParsedFields; i++) {
result.setParsedField(i, parseParsedField(in));
private SigStructType parseStruct(ParserState in) throws IOException {
int numSigFields = in.decodePacked32();
SigStructType result = new SigStructType(numSigFields);
for(int i=0; i<numSigFields; i++) {
result.addField(parseSigField(in));
}
return result;
}
private ParsedField parseParsedField(ParserState in) throws IOException {
String name = in.decodeString();
return new ParsedField(name, parseType(in));
private SigField parseSigField(ParserState in) throws IOException {
String name = in.decodeIntentions();
return new SigField(name, parseType(in, false));
}
private ParsedType lookupType(int tag, ParserState in) {
ParsedType result;
private DataType lookupType(int tag, ParserState in) {
DataType result;
if(tag >= Constant.FIRST_USER_INDEX) {
TypeDef td = typeDefs.get(tag);
result = new ParsedUserType(td.getName());
result = new SigUserType(td.getName());
in.addTypeUse(tag);
in.pushType(tag);
// sampleRefs are sent as primitive types, see above
// } else if(tag == Constant.SAMPLE) {
// result = new SampleRefType();
} else {
result = new PrimitiveType(tag);
result = new SigPrimitiveType(tag);
}
return result;
}
private ParsedSampleDef parseSampleTypeDef(ParserState in) throws IOException {
ParsedTypeDef td = parseTypeDef(in);
ParsedTypeDef td = parseTypeDef(in, true);
return new ParsedSampleDef(td);
}
private ParsedTypeDef parseTypeDef(ParserState in) throws IOException {
return parseTypeDef(in, false);
}
private void addParsedTypeDef(ParsedTypeDef td) {
int idx = td.getIndex();
if(idx>=0x40) {
pts.put(idx, td);
}
}
private ParsedTypeDef parseTypeDef(ParserState in, boolean parseIntentions) throws IOException {
ParsedTypeDef result = in.newTypeDef();
result.setType(parseType(in));
result.setType(parseType(in, false));
addParsedTypeDef(result);
return result;
}
private ParsedType parseType(ParserState in) throws IOException {
private DataType parseType(ParserState in, boolean parseIntentions) throws IOException {
if(parseIntentions) {
String intentions = in.decodeIntentions();
if(intentions.length()>0) {
System.out.println("parseType intentions ("+intentions);
} else {
System.out.println("no intentions");
}
} else {
// System.out.println("not parsing intentions");
}
int tag = in.decodePacked32();
ParsedType result = null;
DataType result = null;
switch(tag) {
case 0:
System.out.println("SELF");
......
package se.lth.control.labcomm2014;
public class TypeSymbol implements SignatureSymbol {
public String toString() {
return "typedef ";
}
public void accept(SignatureSymbolVisitor v){
v.visit(this);
}
}
package se.lth.control.labcomm2014;
import java.io.PrintStream;
public class VoidType extends SigStructType{
public VoidType() {
super("void");
}
public void print(PrintStream out, String indent) {
out.print("void");
}
}
......@@ -389,19 +389,29 @@ class SAMPLE(primitive):
def __repr__(self):
return "labcomm.SAMPLE()"
# helper function
def dict_to_sorted_tuple(d):
tmpL = zip(d.keys(), d.values())
tmpL.sort()
return tuple(tmpL)
#
# Aggregate types
#
class sampledef_or_sampleref_or_typedef(type_decl):
def __init__(self, name=None, decl=None):
self.name = name
def __init__(self, intentions={}, decl=None):
self.intentionDict = dict(intentions)
self.name = self.intentionDict.get('', None)
self.intentions = tuple(sorted(self.intentionDict.iteritems()))
self.decl = decl
def encode_decl(self, encoder):
encoder.encode_type(self.type_index)
with length_encoder(encoder) as e1:
e1.encode_type(self.get_index(encoder))
e1.encode_string(self.name)
# XXX: temporary hack for intentions
e1.encode_intentions(self.intentions)
with length_encoder(e1) as e2:
self.decl.encode_decl(e2)
......@@ -410,12 +420,14 @@ class sampledef_or_sampleref_or_typedef(type_decl):
def decode_decl(self, decoder):
index = decoder.decode_type_number()
name = decoder.decode_string()
# XXX: temporary hack for intentions
# assume the name is the only intention
ints = decoder.decode_intentions()
if usePacketLength(decoder.version):
length = decoder.decode_packed32()
decl = decoder.decode_decl()
result = self.__class__.__new__(self.__class__)
result.__init__(name=name, decl=decl)
result.__init__(intentions=ints, decl=decl)
self.add_index(decoder, index, result)
return result
......@@ -451,15 +463,22 @@ class sample_def(sampledef_or_sampleref_or_typedef):
def add_index(self, decoder, index, decl):
decoder.add_decl(decl, index)
def rename(self, name):
newIntentions = dict(self.intentionDict)
newIntentions['']=name
return sample_def(newIntentions, decl=self.decl)
class sample_ref(sampledef_or_sampleref_or_typedef):
type_index = i_SAMPLE_REF
type_name = 'sample_ref'
def __init__(self, name=None, decl=None, sample=None):
self.name = name
def __init__(self, intentions={}, decl=None, sample=None):
self.intentionDict = dict(intentions)
self.name = self.intentionDict.get('', None) # XXX should we allow nameless?
self.decl = decl
if sample == None and name != None and decl != None:
self.sample = sample_def(name, decl)
self.intentions=tuple(sorted(self.intentionDict.iteritems()))
if sample == None and self.name != None and decl != None:
self.sample = sample_def(intentions, decl)
else:
self.sample = sample
......@@ -623,44 +642,59 @@ class struct(type_decl):
def encode_decl(self, encoder):
encoder.encode_type(i_STRUCT)
encoder.encode_packed32(len(self.field))
for (name, decl) in self.field:
encoder.encode_string(name)
for (intentions, decl) in self.field:
encoder.encode_intentions(intentions)
encoder.encode_type_number(decl)
def encode(self, encoder, obj):
if isinstance(obj, dict):
for (name, decl) in self.field:
decl.encode(encoder, obj[name])
else:
for (name, decl) in self.field:
try:
# hack to get names as keys in the obj:
tmp_foo = zip (map(lambda x:dict(x)[''],obj.keys()), obj.values())
tmp_obj = dict(tmp_foo)
for (intentions, decl) in self.field:
tmp = dict(intentions)
name = tmp['']
decl.encode(encoder, tmp_obj[name])
except AttributeError:
print "HERE BE DRAGONS! hack to get duck-typing example to work"
for (intentions, decl) in self.field:
tmp = dict(intentions)
name = tmp['']
print "trying to encode [%s] " % (name)
decl.encode(encoder, getattr(obj, name))
def decode_decl(self, decoder):
n_field = decoder.decode_packed32()
field = []
for i in range(n_field):
name = decoder.decode_string()
ints = decoder.decode_intentions()
decl = decoder.decode_decl()
field.append((name, decl))
field.append((ints, decl))
return struct(field)
def decode(self, decoder, obj=None):
if obj == None:
obj = decoder.create_object()
for (name, decl) in self.field:
obj.__setattr__(name, decl.decode(decoder))
for (intentions, decl) in self.field:
#name = dict(intentions)['']
obj.__setattr__(intentions, decl.decode(decoder))
return obj
def new_instance(self):
result = anonymous_object()
for (name, decl) in self.field:
for (intentions, decl) in self.field:
name = dict(intentions)['']
result.__setattr__(name, decl.new_instance())
return result
def __repr__(self):
delim = ""
result = "labcomm.struct(["
for (name, decl) in self.field:
for (intentions, decl) in self.field:
try:
name = dict(intentions)['']
except:
name = '(no name)'
result += "%s\n ('%s', %s)" % (delim, name, decl)
delim = ","
result += "\n])"
......@@ -674,7 +708,8 @@ STRUCT = struct([])
class anonymous_object(dict):
def __setattr__(self, name, value):
if name.startswith("_"):
# XXX HERE BE DRAGONS! Is this OK:
if (str(name)).startswith("_"):
super(anonymous_object, self).__setattr__(name, value)
else:
self[name] = value
......@@ -793,7 +828,7 @@ class Encoder(Codec):
if not isinstance(ref, type_decl):
# Trying to register a sample class
ref = ref.signature
decl = sample_ref(name=ref.name, decl=ref.decl, sample=ref)
decl = sample_ref(intentions=ref.intentions, decl=ref.decl, sample=ref)
if index == 0:
self.writer.mark_begin(ref, None)
if super(Encoder, self).add_ref(decl, index):
......@@ -866,11 +901,24 @@ class Encoder(Codec):
self.encode_packed32(len(s));
self.pack("%ds" % len(s),s)
def encode_intentions(self, intentions):
self.encode_packed32(len(intentions))
try:
for (k,v) in intentions:
self.encode_string(k)
self.encode_string(v)
except:
print "WARNING! encode_intentions: don't know what to do with %s" % intentions
class Decoder(Codec):
def __init__(self, reader, version=DEFAULT_VERSION):
super(Decoder, self).__init__()
self.reader = reader
self.version = version
self.handlers = {}
def register_handler(self, decl, handler):
self.handlers[decl] = handler
def unpack(self, format):
size = packer.calcsize(format)
......@@ -900,6 +948,24 @@ class Decoder(Codec):
else:
raise Exception("Invalid type index %d" % index)
def runOne(self):
data,decl = self.decode()
# decode any signatures until next sample
while data == None:
data,decl = self.decode()
if decl:
if data != None:
if decl in self.handlers:
handler = self.handlers[decl]
handler(data)
else:
print ("No handler for %s" % decl.name )
for key, value in self.handlers.iteritems():
if key == decl:
print "but value %s == decl %s" % (key,decl)
print "hashes %d : %d" % (hash(key),hash(decl))
raise Exception()
def decode(self):
while True:
index = self.decode_type_number()
......@@ -997,6 +1063,16 @@ class Decoder(Codec):
index = self.decode_int()
return self.index_to_ref.get(index, None)
def decode_intentions(self):
numIntentions = self.decode_packed32()
res = {}
for i in range(numIntentions):
key = self.decode_string()
val = self.decode_string()
res[key] = val
result = dict_to_sorted_tuple(res)
return result
class signature_reader:
def __init__(self, signature):
self.signature = packer.pack("!%db" % len(signature), *signature)
......
labcomm-*.src.rpm
labcomm2014-*.src.rpm
rpmbuild
......@@ -8,7 +8,7 @@ include ../lib/c/os_compat.mk
all:
test: $(TESTS:%=test_%) compiler_errors
test: $(TESTS:%=test_%) $(TESTS:%=test_renaming_%) compiler_errors
# PYTHONPATH=../lib/python \
# ./test_encoder_decoder.py --labcomm="$(LABCOMM)" basic.lc
......@@ -26,10 +26,29 @@ test_%: gen/%/signatures.py \
./test_encoder_decoder.py \
--signatures=gen/$*/signatures.py \
--test tee gen/$*/testdata \
--test gen/$*/c_relay /dev/stdin /dev/stdout \
--test $(shell echo $(VALGRIND) | sed -e 's/[-][-]/\\\\--/g') \
gen/$*/c_relay /dev/stdin /dev/stdout \
--test mono gen/$*/cs_relay.exe /dev/stdin /dev/stdout \
--test java \\-cp gen/$*:../lib/java/labcomm2014.jar java_relay \
--test java \\-cp gen/$*:../lib/java/labcomm2014.jar \
java_relay /dev/stdin /dev/stdout
.PHONY: test_renaming_%
test_renaming_%: gen/%/signatures.py \
gen/%/c_renaming_relay \
gen/%/cs_renaming_relay.exe \
gen/%/java_relay.class \
gen/%/java_code
PYTHONPATH=$(PYTHONPATH) MONO_PATH=$(MONO_PATH) \
./test_renaming_encoder_decoder.py \
--signatures=gen/$*/signatures.py \
--test tee gen/$*/testdata.renamed \
--test $(shell echo $(VALGRIND) | sed -e 's/[-][-]/\\\\--/g') \
gen/$*/c_renaming_relay /dev/stdin /dev/stdout \
--test mono gen/$*/cs_renaming_relay.exe \
/dev/stdin /dev/stdout
echo \
--test java \\-cp gen/$*:../lib/java/labcomm2014.jar \
java_relay /dev/stdin /dev/stdout
# test cases for compiler error checking
.PHONY: compiler_errors testErrorsOK testErrorsNOK
......@@ -71,6 +90,15 @@ gen/%/c_relay: gen/%/c_relay.c gen/%/c_code.c Makefile
$(CC) $(CFLAGS) -o $@ $< -I../lib/c/2014 -I. -L../lib/c \
gen/$*/c_code.c -llabcomm2014
.PRECIOUS: gen/%/c_renaming_relay.c
gen/%/c_renaming_relay.c: gen/%/typeinfo relay_gen_c.py Makefile
./relay_gen_c.py --renaming $< > $@
.PRECIOUS: gen/%/c_renaming_relay
gen/%/c_renaming_relay: gen/%/c_renaming_relay.c gen/%/c_code.c Makefile
$(CC) $(CFLAGS) -o $@ $< -I../lib/c/2014 -I. -L../lib/c \
gen/$*/c_code.c -llabcomm2014
# C# relay test rules
.PRECIOUS: gen/%/cs_code.cs
gen/%/cs_code.cs: %.lc Makefile | gen/%/.dir
......@@ -84,6 +112,15 @@ gen/%/cs_relay.cs: gen/%/typeinfo relay_gen_cs.py Makefile
gen/%/cs_relay.exe: gen/%/cs_relay.cs gen/%/cs_code.cs Makefile
mcs -out:$@ $(filter %.cs, $^) -lib:../lib/csharp/ -r:labcomm2014
.PRECIOUS: gen/%/cs_renaming_relay.cs
gen/%/cs_renaming_relay.cs: gen/%/typeinfo relay_gen_cs.py Makefile
./relay_gen_cs.py --renaming $< > $@
.PRECIOUS: gen/%/cs_renaming_relay.exe
gen/%/cs_renaming_relay.exe: gen/%/cs_renaming_relay.cs \
gen/%/cs_code.cs Makefile
mcs -out:$@ $(filter %.cs, $^) -lib:../lib/csharp/ -r:labcomm2014
# Java relay test rules
.PRECIOUS: gen/%/java_code
gen/%/java_code: %.lc | gen/%/.dir
......