vmfilegenerator.java

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

JAVA
648
字号
// $Id: VMFileGenerator.java,v 1.4 2004/03/23 22:49:31 scipio Exp $/*									tab:4 * "Copyright (c) 2000-2003 The Regents of the University  of California.   * All rights reserved. * * Permission to use, copy, modify, and distribute this software and its * documentation for any purpose, without fee, and without written agreement is * hereby granted, provided that the above copyright notice, the following * two paragraphs and the author appear in all copies of this software. *  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS." * * Copyright (c) 2002-2003 Intel Corporation * All rights reserved. * * This file is distributed under the terms in the attached INTEL-LICENSE      * file. If you do not find these files, copies can be found by writing to * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA,  * 94704.  Attention:  Intel License Inquiry. *//* Authors:	Phil Levis <pal@cs.berkeley.edu> * Date:        Jan 6 2004 * Desc:        Generates VM files from opcodes, contexts, and options. * *//** * @author Phil Levis <pal@cs.berkeley.edu> */package net.tinyos.script;import java.awt.*;import java.awt.event.*;import java.awt.font.*;import java.io.*;import java.net.*;import java.util.*;import javax.swing.*;import javax.swing.border.*;import net.tinyos.message.*;import net.tinyos.packet.*;import net.tinyos.script.tree.*;import net.tinyos.util.*;import java.io.*;import java.util.regex.*;public class VMFileGenerator {  private File directory;  private File languageFile;  private Vector contexts;  private Vector primitives;  private String vmName;  private String vmDesc;  private VMOptions options;    public VMFileGenerator(File directory, String name, String desc, File languageFile, Enumeration contexts,  Enumeration primitives, VMOptions options) {    this.directory = directory;    this.languageFile = languageFile;    this.options = options;    vmName = name;    vmDesc = desc;        this.contexts = new Vector();    while (contexts.hasMoreElements()) {      Object context = contexts.nextElement();      //System.err.println("adding " + context);      this.contexts.addElement(context);    }    this.primitives = new Vector();    while (primitives.hasMoreElements()) {      this.primitives.addElement(primitives.nextElement());    }      }  public String getName() {return vmName;}  public String getDesc() {return vmDesc;}  public String getJavaConstantsName() {return getName() + "Constants";}  /**   * Return the set of selected contexts.   */    public Enumeration getContexts() {    return contexts.elements();  }  /**   * Return the set of explicitly selected primitives.   */    public Enumeration getSelectedPrimitives() {    return primitives.elements();  }  /**   * Return the set of capsules determined by contexts and primitives.   */    public Enumeration getCapsules() {     Vector vector = new Vector();     Enumeration enum = getContexts();     while (enum.hasMoreElements()) {       BuilderContext context = (BuilderContext)enum.nextElement();       vector.addElement(context.capsule());     }     enum = getAllPrimitives();     while (enum.hasMoreElements()) {       Primitive primitive = (Primitive)enum.nextElement();       Enumeration capsules = primitive.capsules();       while (capsules.hasMoreElements()) {	 vector.addElement(capsules.nextElement());       }     }     return vector.elements();  }  /**   * Return the full set of primitives, those explicitly selected and   * those implicitly selected by including contexts.   *   */   public Enumeration getAllPrimitives() {    Vector instrList = new Vector();    // First, get the selected primitives    Enumeration current = getSelectedPrimitives();    while (current.hasMoreElements()) {      instrList.addElement(current.nextElement());    }    // Next, get those that contexts implicitly select    current = getContexts();    while (current.hasMoreElements()) {      BuilderContext context = (BuilderContext)current.nextElement();      if (context.hasPrimitives()) {	Enumeration prims = context.primitives();	while (prims.hasMoreElements()) {	  Primitive p = (Primitive)prims.nextElement();	  //System.err.println(p);	  instrList.addElement(p);	}      }    }    return instrList.elements();  }  /**   * Generate an array of vectors of all instructions, sorted by how   * many bits of opcode space they consume. For example, instructions   * in element 0 in the array consume 0 bits, that is, have a single   * opcode value, while instructions in element 2 of the array   * consume 2 bits, that is, have 4 opcode values.   */    private Vector[] generateSortedInstructions() throws IOException {    Vector[] sortedInstrs = new Vector[32];    Vector v = new Vector();    System.err.println("Generating language-based instructions.");    DFTokenizer tokenizer = new DFTokenizer(new FileReader(languageFile));    while (tokenizer.hasMoreStatements()) {      DFStatement stmt = tokenizer.nextStatement();      if (stmt != null) {	v.add(new Primitive(stmt));      }    }        organizeInstrs(v.elements(), sortedInstrs);    System.err.println("Organizing function primitives.");    organizeInstrs(getAllPrimitives(), sortedInstrs);    return sortedInstrs;  }    public void createFiles() throws IOException {     System.err.println("Creating files");    Vector[] sortedInstrs = generateSortedInstructions();        System.err.println("Making Config file");    File configFile = new File(directory, "vm.vmdf");    PrintWriter configWriter = new PrintWriter(new FileWriter(configFile), true);    createConfigFile(configWriter);        System.err.println("Making constants file");    File constantsFile = new File(directory, "MateConstants.h");    PrintWriter constantsWriter = new PrintWriter(new FileWriter(constantsFile), true);    createConstantsFile(constantsWriter, sortedInstrs);        System.err.println("Making component file");    File componentsFile = new File(directory, "MateTopLevel.nc");    PrintWriter componentsWriter = new PrintWriter(new FileWriter(componentsFile), true);    createComponentFile(componentsWriter, sortedInstrs);        System.err.println("Making makefile (haha!)");    File makefileFile = new File(directory, "Makefile");    PrintWriter makefileWriter = new PrintWriter(new FileWriter(makefileFile), true);    createMakefile(makefileWriter);        }  private void createMakefile(PrintWriter writer) {    String vmJavaName = getJavaConstantsName();        writer.println("COMPONENT=MateTopLevel");    writer.print("CFLAGS=");    Enumeration enum = options.getSearchPaths();    while (enum.hasMoreElements()) {      writer.print("-I" + enum.nextElement() + " ");    }    writer.println(" -I%T/lib/VM/components -I%T/lib/VM/opcodes -I%T/lib/VM/contexts -I%T/lib/VM/types -I%T/lib/VM/interfaces -I%T/lib/Queue -I%T/lib/MintRoute -I.");    writer.println("MSG_SIZE=36");    writer.println("BUILD_EXTRA_DEPS=" + vmJavaName + ".class CapsuleChunkMsg.class CapsuleStatusMsg.class CapsuleMsg.class");    writer.println();    writer.println("include ../Makerules");    writer.println();    writer.println(vmJavaName + ".java:");    writer.println("\tmkdir -p vm_specific");    writer.println("\tncg java $(CFLAGS) -java-classname=vm_specific." + vmJavaName + " MateTopLevel.nc MateConstants.h -o vm_specific/$@ -DTOSH_DATA_LENGTH=36");    writer.println();    writer.println("CapsuleMsg.java:");    writer.println("\tmkdir -p vm_specific");    writer.println("\tmig java $(CFLAGS) -java-classname=vm_specific.CapsuleMsg ../../tos/lib/VM/types/Mate.h MateCapsuleMsg -o vm_specific/$@ -DTOSH_DATA_LENGTH=36");    writer.println();    writer.println("CapsuleChunkMsg.java:");    writer.println("\tmkdir -p vm_specific");    writer.println("\tmig java $(CFLAGS) -java-classname=vm_specific.CapsuleChunkMsg ../../tos/lib/VM/types/Mate.h MateCapsuleChunkMsg -o vm_specific/$@ -DTOSH_DATA_LENGTH=36");    writer.println();    writer.println("CapsuleStatusMsg.java:");    writer.println("\tmkdir -p vm_specific");    writer.println("\tmig java $(CFLAGS) -java-classname=vm_specific.CapsuleStatusMsg ../../tos/lib/VM/types/Mate.h MateCapsuleStatusMsg -o vm_specific/$@ -DTOSH_DATA_LENGTH=36");    writer.println();    writer.println("%.class: %.java");    writer.println("\tjavac $<");    writer.println();    writer.println("" + vmJavaName + ".class: " + vmJavaName + ".java");    writer.println("\tjavac vm_specific/$<");    writer.println();    writer.println("CapsuleMsg.class: CapsuleMsg.java");    writer.println("\tjavac vm_specific/$<");    writer.println();    writer.println("CapsuleChunkMsg.class: CapsuleChunkMsg.java");    writer.println("\tjavac vm_specific/$<");    writer.println();    writer.println("CapsuleStatusMsg.class: CapsuleStatusMsg.java");    writer.println("\tjavac vm_specific/$<");    writer.println();    writer.println("cleanmig:");    writer.println("\trm -f " + vmJavaName + ".*");    writer.println();    writer.println("clean: cleanmig");    writer.println("\trm -rf build/ vm_specific/");    writer.println("\trm -f core.* *.class *.java");    writer.println("\trm -f *~");    writer.flush();  }	  private void createConfigFile(PrintWriter writer) {    Enumeration instrs = getAllPrimitives();    writer.println("<VM name=\"" + getName() + "\" desc=\"" + getDesc() + "\" className=\"vm_specific." + getJavaConstantsName() + "\">");    while (instrs.hasMoreElements()) {      Primitive p = (Primitive) instrs.nextElement();      writer.println(p);    }    Enumeration contexts = getContexts();    while (contexts.hasMoreElements()) {      BuilderContext bc = (BuilderContext) contexts.nextElement();      writer.println(bc);    }        writer.flush();  }  private void createConstantsFile(PrintWriter writer, Vector[] sortedInstrs) {    int counter = 0;    String opcode;    Vector v;    Primitive p;    BuilderContext c;    Enumeration e;    Capsule capsule;        writer.println("#ifndef BOMBILLA_CONSTANTS_H_INCLUDED");    writer.println("#define BOMBILLA_CONSTANTS_H_INCLUDED\n");    writer.println("typedef enum {");    writer.println("  MATE_OPTION_FORWARD     = 0x80,");    writer.println("  MATE_OPTION_FORCE       = 0x40,");    writer.println("  MATE_OPTION_MASK        = 0x3f,");    writer.println("} MateCapsuleOption;\n");        writer.println("typedef enum {");    e = getContexts();    while (e.hasMoreElements()) {      c = (BuilderContext)e.nextElement();      String context = c.name();      context = context.toUpperCase();      writer.println("  MATE_CONTEXT_" + context + "\t = unique(\"MateContextConstant\"),");    }    writer.println("  MATE_CONTEXT_NUM\t = unique(\"MateContextConstant\"),");    writer.println("  MATE_CONTEXT_INVALID = 255");    writer.println("} MateContextType;");        writer.println("typedef enum {");    e = getCapsules();

⌨️ 快捷键说明

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