vmfilegenerator.java

来自「tinyos最新版」· Java 代码 · 共 648 行 · 第 1/2 页

JAVA
648
字号
    while (e.hasMoreElements()) {      capsule = (Capsule)e.nextElement();      String cName = capsule.name();      cName = cName.toUpperCase();      writer.println("  MATE_CAPSULE_" + cName + "\t = unique(\"MateCapsuleConstant\"),");    }    writer.println("  MATE_CAPSULE_NUM\t = unique(\"MateCapsuleConstant\"),");    writer.println("  MATE_CAPSULE_INVALID = 255");    writer.println("} MateCapsuleType;\n");    writer.println("enum {");    writer.println("  MATE_CALLDEPTH    = " + options.getCallDepth() + ",");    writer.println("  MATE_OPDEPTH      = " + options.getOpDepth() + ",");    writer.println("  MATE_HEAPSIZE     = uniqueCount(\"MateLock\"),");    writer.println("  MATE_MAX_PARALLEL = 4,");    writer.println("  MATE_NUM_YIELDS   = 4,");    writer.println("  MATE_HEADERSIZES  = 3,");    writer.println("  MATE_HEADERSIZE   = 6,");    writer.println("  MATE_BUF_LEN      = " + options.getBufLen() + ",");    writer.println("  MATE_PGMSIZE      = " + options.getProgramSize() + ",");    writer.println("  MATE_BUF_NUM      = 8");    writer.println("} MateSizeConstants;\n");    writer.println("typedef enum {");    writer.println("  MATE_DATA_NONE    = unique(\"MateSensorType\"),");    writer.println("  MATE_DATA_VALUE   = unique(\"MateSensorType\"),");    writer.println("  MATE_DATA_PHOTO   = unique(\"MateSensorType\"),");    writer.println("  MATE_DATA_TEMP    = unique(\"MateSensorType\"),");    writer.println("  MATE_DATA_MIC     = unique(\"MateSensorType\"),");    writer.println("  MATE_DATA_MAGX    = unique(\"MateSensorType\"),");    writer.println("  MATE_DATA_MAGY    = unique(\"MateSensorType\"),");    writer.println("  MATE_DATA_ACCELX  = unique(\"MateSensorType\"),");    writer.println("  MATE_DATA_ACCELY  = unique(\"MateSensorType\"),");    writer.println("  MATE_DATA_END     = unique(\"MateSensorType\")");    writer.println("} MateSensorType;\n");    writer.println("typedef enum {");    writer.println("  MATE_TYPE_INVALID = 0,");    writer.println("  MATE_TYPE_VALUE   = (1 << unique(\"MateDataType\")),");    writer.println("  MATE_TYPE_BUFFER  = (1 << unique(\"MateDataType\")),");    writer.println("  MATE_TYPE_SENSE   = (1 << unique(\"MateDataType\"))");    writer.println("} MateDataType;\n");    writer.println("typedef enum {");    writer.println("  MATE_VAR_V = MATE_TYPE_VALUE,");    writer.println("  MATE_VAR_B = MATE_TYPE_BUFFER,");    writer.println("  MATE_VAR_S = MATE_TYPE_SENSE,");    writer.println("  MATE_VAR_VB = MATE_VAR_V | MATE_VAR_B,");    writer.println("  MATE_VAR_VS = MATE_VAR_V | MATE_VAR_S,");    writer.println("  MATE_VAR_SB = MATE_VAR_B | MATE_VAR_S,");    writer.println("  MATE_VAR_VSB = MATE_VAR_B | MATE_VAR_S | MATE_VAR_V,");    writer.println("  MATE_VAR_ALL = MATE_VAR_B | MATE_VAR_S | MATE_VAR_V");    writer.println("} MateDataCondensed;\n");    writer.println("typedef enum {");    writer.println("  MATE_STATE_HALT        = unique(\"MateState\"),");    writer.println("  MATE_STATE_SENDING     = unique(\"MateState\"),");    writer.println("  MATE_STATE_LOG         = unique(\"MateState\"),");    writer.println("  MATE_STATE_SENSE       = unique(\"MateState\"),");    writer.println("  MATE_STATE_SEND_WAIT   = unique(\"MateState\"),");    writer.println("  MATE_STATE_LOG_WAIT    = unique(\"MateState\"),");    writer.println("  MATE_STATE_SENSE_WAIT  = unique(\"MateState\"),");    writer.println("  MATE_STATE_LOCK_WAIT   = unique(\"MateState\"),");    writer.println("  MATE_STATE_RESUMING    = unique(\"MateState\"),");    writer.println("  MATE_STATE_RUN         = unique(\"MateState\")");    writer.println("} MateContextState;\n");    writer.println("typedef enum {");    writer.println("  MATE_ERROR_TRIGGERED                =  unique(\"MateError\"),");    writer.println("  MATE_ERROR_INVALID_RUNNABLE         =  unique(\"MateError\"),");    writer.println("  MATE_ERROR_STACK_OVERFLOW           =  unique(\"MateError\"),");    writer.println("  MATE_ERROR_STACK_UNDERFLOW          =  unique(\"MateError\"),");    writer.println("  MATE_ERROR_BUFFER_OVERFLOW          =  unique(\"MateError\"),");    writer.println("  MATE_ERROR_BUFFER_UNDERFLOW         =  unique(\"MateError\"),");    writer.println("  MATE_ERROR_INDEX_OUT_OF_BOUNDS      =  unique(\"MateError\"),");    writer.println("  MATE_ERROR_INSTRUCTION_RUNOFF       =  unique(\"MateError\"),");    writer.println("  MATE_ERROR_LOCK_INVALID             =  unique(\"MateError\"),");    writer.println("  MATE_ERROR_LOCK_STEAL               =  unique(\"MateError\"),");    writer.println("  MATE_ERROR_UNLOCK_INVALID           = unique(\"MateError\"),");    writer.println("  MATE_ERROR_QUEUE_ENQUEUE            = unique(\"MateError\"),");    writer.println("  MATE_ERROR_QUEUE_DEQUEUE            = unique(\"MateError\"),");    writer.println("  MATE_ERROR_QUEUE_REMOVE             = unique(\"MateError\"),");    writer.println("  MATE_ERROR_QUEUE_INVALID            = unique(\"MateError\"),");    writer.println("  MATE_ERROR_RSTACK_OVERFLOW          = unique(\"MateError\"),");    writer.println("  MATE_ERROR_RSTACK_UNDERFLOW         = unique(\"MateError\"),");    writer.println("  MATE_ERROR_INVALID_ACCESS           = unique(\"MateError\"),");    writer.println("  MATE_ERROR_TYPE_CHECK               = unique(\"MateError\"),");    writer.println("  MATE_ERROR_INVALID_TYPE             = unique(\"MateError\"),");    writer.println("  MATE_ERROR_INVALID_LOCK             = unique(\"MateError\"),");    writer.println("  MATE_ERROR_INVALID_INSTRUCTION      = unique(\"MateError\"),");    writer.println("  MATE_ERROR_INVALID_SENSOR           = unique(\"MateError\")");    writer.println("} MateErrorCode;\n");      writer.println("typedef enum {");    writer.println("  MATE_MAX_NET_ACTIVITY  = 64,");    writer.println("  MATE_PROPAGATE_TIMER   = 737,");    writer.println("  MATE_PROPAGATE_FACTOR  = 0x7f   // 127");    writer.println("} MateCapsulePropagateConstants;\n");      writer.println("enum {");    writer.println("  AM_MATEROUTEMSG         = 0x1b,");    writer.println("  AM_MATEVERSIONMSG       = 0x1c,");    writer.println("  AM_MATEERRORMSG         = 0x1d,");    writer.println("  AM_MATECAPSULEMSG       = 0x1e,");    writer.println("  AM_MATEPACKETMSG        = 0x1f,");    writer.println("  AM_MATECAPSULECHUNKMSG  = 0x20,");    writer.println("  AM_MATECAPSULESTATUSMSG = 0x21,");    writer.println("};\n");    writer.println("typedef enum {");    writer.print("// instruction set");    for (int i = 0; i < 32; i++) {      v = sortedInstrs[i];      if (v != null) {        counter = (int)Math.ceil((double)counter/(1 << i)) * (1 << i);        e = v.elements();        while (e.hasMoreElements()) {          opcode = Integer.toHexString(counter);          counter += (1 << i);          p = (Primitive) e.nextElement();          writer.println(",");          writer.print("  OP" + p.get("opcode") + "\t= 0x" + opcode);        }      }    }        writer.println();    writer.println("} MateInstruction;\n");    writer.println();    writer.println("/*");    writer.println(" * MVirus uses the Trickle algorithm for code propagation and maintenance.");    writer.println(" * A full description and evaluation of the algorithm can be found in");    writer.println(" *");    writer.println(" * Philip Levis, Neil Patel, David Culler, and Scott Shenker.");    writer.println(" * \"Trickle: A Self-Regulating Algorithm for Code Propagation and Maintenance");    writer.println(" * in Wireless Sensor Networks.\" In Proceedings of the First USENIX/ACM");    writer.println(" * Symposium on Networked Systems Design and Implementation (NSDI 2004).");    writer.println(" *");    writer.println(" * A copy of the paper can be downloaded from Phil Levis' web site:");    writer.println(" *        http://www.cs.berkeley.edu/~pal/");    writer.println(" *");    writer.println(" * A brief description of the algorithm can be found in the comments");    writer.println(" * at the head of MVirus.nc.");    writer.println(" *");    writer.println(" */");    writer.println();    writer.println("typedef enum {");    writer.println("  /* These first two constants define the granularity at which t values");    writer.println("     are calculated (in ms). Version vectors and capsules have separate");    writer.println("     timers, as version timers decay (lengthen) while capsules timers");    writer.println("     are constant, as they are not a continuous process.*/");    writer.println("  MVIRUS_VERSION_TIMER = 100,           // The units of time (ms)");    writer.println("  MVIRUS_CAPSULE_TIMER = 100,           // The units of time (ms)");    writer.println("");    writer.println("  /* These constants define how many times a capsule is transmitted,");    writer.println("     the timer interval for Trickle suppression, and the redundancy constant");    writer.println("     k. Due to inherent loss, having a repeat > 1 is preferrable, although");    writer.println("     it should be small. It's better to broadcast the data twice rather");    writer.println("     than require another metadata announcement to trigger another");    writer.println("     transmission. It's not clear whether REDUNDANCY should be > or = to");    writer.println("     REPEAT. In either case, both constants should be small (e.g, 2-4). */");    writer.println("  ");    writer.println("  MVIRUS_CAPSULE_REPEAT = 2,            // How many times to repeat a capsule");    writer.println("  MVIRUS_CAPSULE_TAU = 10,              // Capsules have a fixed tau");    writer.println("  MVIRUS_CAPSULE_REDUNDANCY = 2,        // Capsule redundancy (suppression pt.)");    writer.println("");    writer.println("  /* These constants define the minimum and maximum tau values for");    writer.println("     version vector exchange, as well as the version vector redundancy");    writer.println("     constant k. Note that the tau values are in terms of multiples");    writer.println("     of the TIMER value above (e.g., a MIN of 10 and a TIMER of 100");    writer.println("     means a MIN of 1000 ms, or one second). */");    writer.println("  MVIRUS_VERSION_TAU_MIN = 10,          // Version scaling tau minimum");    writer.println("  MVIRUS_VERSION_TAU_MAX = 600,         // Version scaling tau maximum");    writer.println("  MVIRUS_VERSION_REDUNDANCY = 1,        // Version redundancy (suppression pt.)");    writer.println("  ");    writer.println("  /* These constants are all for sending data larger than a single");    writer.println("     packet; they define the size of a program chunk, bitmasks, etc.*/");    writer.println("  MVIRUS_CHUNK_HEADER_SIZE = 8,");    writer.println("  MVIRUS_CHUNK_SIZE = TOSH_DATA_LENGTH - MVIRUS_CHUNK_HEADER_SIZE,");    writer.println("  MVIRUS_BITMASK_ENTRIES = ((MATE_PGMSIZE + MVIRUS_CHUNK_SIZE - 1) / MVIRUS_CHUNK_SIZE),");    writer.println("  MVIRUS_BITMASK_SIZE = (MVIRUS_BITMASK_ENTRIES + 7) / 8,");    writer.println("} MVirusConstants;");    writer.println();    writer.println("#endif");    writer.flush();  }  private void createComponentFile(PrintWriter writer, Vector[] sortedInstrs) {    BuilderContext c;    Vector v;    Primitive p;    Enumeration e;        writer.println("includes Mate;");    writer.println("includes MateConstants;\n");    writer.println("configuration MateTopLevel {}");    writer.println("implementation");    writer.println("{");    writer.println("\tcomponents MateEngine as VM, Main, MContextSynchProxy as ContextSynch;");    for (int i = 0; i < 32; i++) {      v = sortedInstrs[i];      if (v != null) {        e = v.elements();        while (e.hasMoreElements()) {          p = (Primitive)e.nextElement();	  writer.println("\tcomponents OP" + p.get("opcode") + ";");        }      }    }        e = getContexts();    while (e.hasMoreElements()) {      c = (BuilderContext)e.nextElement();      writer.println("\tcomponents " + c.name() + "Context;");    }    writer.println();    writer.println("\tMain.StdControl -> VM;");    int opCodesUsed = 0;    for (int i = 0; i < 32; i++) {      v = sortedInstrs[i];      if (v != null) {        e = v.elements();        while (e.hasMoreElements()) {          p = (Primitive)e.nextElement();	  for (int j = 0; j < Math.pow(2,i); j++) {	    writer.println("\tVM.Bytecode[OP" + p.get("opcode") + "+" + j + "] -> OP" + p.get("opcode") + ";");	    opCodesUsed++;	  }	}	writer.println();      }    }    System.err.println("" + opCodesUsed + " of 256 opcodes used.\n");    for (int i = 0; i < 32; i++) {      v = sortedInstrs[i];      if (v != null) {        e = v.elements();        while (e.hasMoreElements()) {          p = (Primitive)e.nextElement();	  if (p.hasLocks()) {	    for (int j = 0; j < Math.pow(2,i); j++)	      writer.println("\tContextSynch.CodeLocks[OP" + p.get("opcode") + "+" + j + "] -> OP" + p.get("opcode") + ";");	  }	}	writer.println();      }    }        writer.println("}");    writer.flush();  }    private void organizeInstrs(Enumeration e, Vector[] sortedInstrs) {    Primitive p;    Vector v;    String operandSizeStr, instrLenStr;    Integer operandSizeInt, instrLenInt;    Pattern re = Pattern.compile("(\\d*)\\D+(\\d+)");     int opcodesUsed = 0;        while (e.hasMoreElements()) {      operandSizeStr = "0";      instrLenStr = "1";      operandSizeInt = new Integer(0);;      instrLenInt = new Integer(1);	      p = (Primitive) e.nextElement();      //System.err.println("Adding " + p);      Matcher m = re.matcher((String)p.get("opcode"));      if (m.matches()) {	instrLenStr = m.group(1);	if (!instrLenStr.equals("")) {          instrLenInt = new Integer(instrLenStr);	}	operandSizeStr = m.group(2);        operandSizeInt = new Integer(operandSizeStr);      }      else {	      }      // If an instruction is wider than a single byte,      // then embedded operand bits are in the additional bytes.      // E.g., a 2-byte wide instruction with 10 bits of embedded operand      // only requires 4 instruction slots      if (instrLenInt.intValue() > 1) {        operandSizeInt = new Integer(operandSizeInt.intValue() - (8 * (instrLenInt.intValue() -1 )));        //System.out.println("changing " + p.get("opcode") + " width from " + operandSizeStr + " to " + operandSizeInt);      }            v = sortedInstrs[operandSizeInt.intValue()];      if (v == null) {	v = new Vector();      }            v.add(p);      //System.out.println("Added " + p + " to " + operandSizeInt + " instruction set.");      sortedInstrs[operandSizeInt.intValue()] = v;    }  }    public String arrayToStr(int[] num) {    String numStr = "";        if (num == null)      return numStr;        for (int i = 0; i < num.length; i++) {      numStr += num[i];    }        return numStr;  }  public static void main(String[] args) {      }    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?