Skip to content
Snippets Groups Projects
C_CodeGen.jrag 37.4 KiB
Newer Older
Anders Nilsson's avatar
Anders Nilsson committed
  public void VariableSize.C_emitEncoderEncodeLimit(C_env env, int i) {
    env.println("labcomm_write_packed32(w, " + env.qualid + ".n_" + i + ");");
Anders Nilsson's avatar
Anders Nilsson committed
  }

  public void ArrayType.C_emitEncoderEncodeLimit(C_env env) {
    for (int i = 0 ; i < getNumExp() ; i++) {
      getExp(i).C_emitEncoderEncodeLimit(env, i);
    }
  }

  public void Decl.C_emitEncoderRegisterHandler(C_env env) {
    throw new Error(this.getClass().getName() + 
		    ".C_emitEncoderRegisterHandler(C_env env)" + 
		    " not declared");
  }

  public void TypeDecl.C_emitEncoderRegisterHandler(C_env env) {
  }

  public void SampleDecl.C_emitEncoderRegisterHandler(C_env env) {
    env.println("int labcomm_encoder_register_" + 
Anders Nilsson's avatar
Anders Nilsson committed
		env.prefix + getName() + "(");
    env.indent();
    env.println("struct labcomm_encoder *e");
    env.unindent();
    env.println(")");
    env.println("{");
    env.indent();
    env.println("return labcomm_internal_encoder_register(");
Anders Nilsson's avatar
Anders Nilsson committed
    env.indent();
    env.println("e,");
    env.println("&labcomm_signature_" + env.prefix + getName() + ",");
    env.println("(labcomm_encoder_function)encode_" + env.prefix + getName());
Anders Nilsson's avatar
Anders Nilsson committed
    env.unindent();
    env.println(");");
    env.unindent();
    env.println("}");
  }

}

aspect C_EncoderIoctl {

  public void Decl.C_emitEncoderIoctl(C_env env) {
    throw new Error(this.getClass().getName() + 
		    ".C_emitEncoderIoctl()" + 
		    " not declared");
  }

  public void TypeDecl.C_emitEncoderIoctl(C_env env) {
  }

  public void SampleDecl.C_emitEncoderIoctl(C_env env) {
    env.println("int labcomm_encoder_ioctl_" + env.prefix + getName() + "(");
    env.indent();
    env.println("struct labcomm_encoder *e,");
    env.println("int ioctl_action,");
    env.println("...");
    env.unindent();
    env.println(")");
    env.println("{");
    env.indent();
    env.println("int result;");
    env.println("va_list va;");
    env.println("va_start(va, ioctl_action);");
    env.println("result = labcomm_internal_encoder_ioctl(");
    env.indent();
    env.println("e, &labcomm_signature_" + env.prefix + getName() + ", ");
    env.println("ioctl_action, va);");
    env.unindent();
    env.println("va_end(va);");
    env.println("return result;");
    env.unindent();
    env.println("}");
  }
Anders Nilsson's avatar
Anders Nilsson committed

}

aspect C_Signature {

  public void ASTNode.C_emitSignature(C_env env) {
Anders Nilsson's avatar
Anders Nilsson committed
    throw new Error(this.getClass().getName() + 
Anders Nilsson's avatar
Anders Nilsson committed
		    " not declared");
  }

  public void Decl.C_emitSignature(C_env env) {
  public void SampleDecl.C_emitSignature(C_env env) {
Anders Nilsson's avatar
Anders Nilsson committed
    env.println("static unsigned char signature_bytes_" + 
		       env.prefix + getName() + "[] = {");
Anders Nilsson's avatar
Anders Nilsson committed
    SignatureList signature = signature();
    for (int i = 0 ; i < signature.size() ; i++) {
      String comment = signature.getComment(i);
      if (comment != null) {
        env.println(signature.getIndent(i) + "// " + comment);
      }
      byte[] data = signature.getData(i);
      if (data != null) {
        env.print(signature.getIndent(i));
        for (int j = 0 ; j < data.length ; j++) {
          env.print(data[j] + ", ");
        }
        env.println("");
      }
    }
    env.println("};");
    env.println("struct labcomm_signature labcomm_signature_" + 
		env.prefix + getName() + " = {");
    env.indent();
    env.println("LABCOMM_SAMPLE, \"" + getName() + "\",");
    env.println("(int (*)(struct labcomm_signature *, void *))labcomm_sizeof_" + 
		env.prefix + getName() + ",");
    env.println("sizeof(signature_bytes_" + env.prefix + getName() + "),");
    env.println("signature_bytes_" + env.prefix + getName() + ",");
    env.println("0");
    env.unindent();
    env.println(" };");
  public void ASTNode.C_emitConstructor(C_env env) {
Anders Nilsson's avatar
Anders Nilsson committed
    throw new Error(this.getClass().getName() + 
Anders Nilsson's avatar
Anders Nilsson committed
		    " not declared");
  }

  public void Program.C_emitConstructor(C_env env) {
    env.println("LABCOMM_CONSTRUCTOR void init_" +
		env.prefix + "_signatures(void)");
    env.println("{");
    env.println("static int initialized = 0;");
    env.println("if (initialized == 0) {");
    env.indent();
    env.println("initialized = 1;");
    for (int i = 0; i < getNumDecl(); i++) {
      getDecl(i).C_emitConstructor(env);
    }
    env.println("}"); 
    env.unindent();
    env.println("}"); 
  }

  public void Decl.C_emitConstructor(C_env env) {
  }

  public void SampleDecl.C_emitConstructor(C_env env) {
    env.println("labcomm_set_local_index(&labcomm_signature_" + 
		env.prefix + getName() + ");");
Anders Blomdell's avatar
Anders Blomdell committed
  public void ASTNode.C_emitConstructorDeclaration(C_env env) {
    throw new Error(this.getClass().getName() + 
		    ".C_emitConstructorDeclaration(C_env env)" + 
		    " not declared");
  }

  public void Program.C_emitConstructorDeclaration(C_env env) {
    env.println("void init_" + env.prefix + "_signatures(void);");
Anders Nilsson's avatar
Anders Nilsson committed
}

aspect C_Sizeof {

  public void Decl.C_emitSizeofDeclaration(C_env env) {
  }

  public void SampleDecl.C_emitSizeofDeclaration(C_env env) {
    env.println("extern int labcomm_sizeof_" + env.prefix + getName() +
Anders Blomdell's avatar
Anders Blomdell committed
		"(" + env.prefix + getName() + " *v);");
Anders Nilsson's avatar
Anders Nilsson committed
  }

  public int Decl.C_fixedSizeof() {
    return getType().C_fixedSizeof();
  }

  public int Type.C_fixedSizeof() {
    throw new Error(this.getClass().getName() + 
		    ".C_fixedSizeof()" + 
		    " not declared");
  }

  public int VoidType.C_fixedSizeof() {
    return 0;
  }

  public int PrimType.C_fixedSizeof() {
    switch (getToken()) {
      case LABCOMM_BOOLEAN: { return 1; } 
      case LABCOMM_BYTE: { return 1; } 
      case LABCOMM_SHORT: { return 2; } 
      case LABCOMM_INT: { return 4; } 
      case LABCOMM_LONG: { return 8; }
      case LABCOMM_FLOAT: { return 4; }
      case LABCOMM_DOUBLE: { return 8; }
      default: { 
	throw new Error(this.getClass().getName() + 
			".C_fixedSizeof()" + 
			" unknown size (" + getName() + ")"); 
      } 
    }
  }

  public int UserType.C_fixedSizeof() {
    return lookupType(getName()).getType().C_fixedSizeof();
  }

  public int StructType.C_fixedSizeof() {
    int result = 0;
    for (int i = 0 ; i < getNumField() ; i++) {
      result += getField(i).getType().C_fixedSizeof();
    }
    return result;
  }

  public int ArrayType.C_fixedSizeof() {
    int elements = 1;
    for (int i = 0 ; i < getNumExp() ; i++) {
      int n = Integer.parseInt(((IntegerLiteral)getExp(i)).getValue());
      elements = elements * n;
    }
    return getType().C_fixedSizeof() * elements;
  }

  public void Decl.C_emitSizeof(C_env env) {
  }

  public void SampleDecl.C_emitSizeof(C_env env) {
    env = env.nestStruct("(*v)");
    env.println("int labcomm_sizeof_" + env.prefix + getName() +
Anders Blomdell's avatar
Anders Blomdell committed
		"(" + env.prefix + getName() + " *v)");
Anders Nilsson's avatar
Anders Nilsson committed
    env.println("{");
    env.indent();
Anders Blomdell's avatar
Anders Blomdell committed
    env.println("int result = labcomm_size_packed32(labcomm_signature_" + 
                env.prefix + getName() +".index);");
Anders Nilsson's avatar
Anders Nilsson committed
    if (C_isDynamic()) {
      getType().C_emitSizeof(env);
    } else {
Anders Blomdell's avatar
Anders Blomdell committed
      env.println("result += " + C_fixedSizeof() + ";");
Anders Nilsson's avatar
Anders Nilsson committed
    }    
Anders Blomdell's avatar
Anders Blomdell committed
    env.println("return result;");
Anders Nilsson's avatar
Anders Nilsson committed
    env.unindent();
    env.println("}");
  }

  public void Type.C_emitSizeof(C_env env) {
    throw new Error(this.getClass().getName() + 
		    ".C_emitSizeof(C_env env)" + 
		    " not declared");
  }

  public void PrimType.C_emitSizeof(C_env env) {
    switch (getToken()) {
      case LABCOMM_STRING: { 
	env.println("result += 0 + strlen(" + env.qualid + ");"); 
Anders Nilsson's avatar
Anders Nilsson committed
      } break;
      default: { 
	throw new Error(this.getClass().getName() + 
			".C_emitSizeof(C_env env)" + 
			" known size (" + getName() + ")"); 
      } 
    }
  }

  public void UserType.C_emitSizeof(C_env env) {
    lookupType(getName()).getType().C_emitSizeof(env);
  }

  public void StructType.C_emitSizeof(C_env env) {
    int fixed = 0;
    for (int i = 0 ; i < getNumField() ; i++) {
      if (getField(i).getType().C_isDynamic()) {
	getField(i).getType().C_emitSizeof(
	  env.nestStruct("." + getField(i).getName()));
      } else {
	fixed += getField(i).getType().C_fixedSizeof();
      }
    }
    if (fixed > 0) {
      env.println("result += " + fixed + ";");
    }
  }

  public void ArrayType.C_emitSizeof(C_env env) {
    if (getType().C_isDynamic()) {
      env.println("{");
      env.indent();
      C_emitLoopVariables(env);
      for (int i = 0 ; i < getNumExp() ; i++) {
	String iterator = "i_" + env.depth + "_" + i;
	env.println("for (" + iterator + " = 0" +
		    " ; " +
		    iterator + " < " + getExp(i).C_getLimit(env, i) +
		    " ; " +
		    iterator + "++) {");
	env.indent();
      }
      C_emitCalcIndex(env);
      getType().C_emitSizeof(C_Nest(env));
      for (int i = 0 ; i < getNumExp() ; i++) {
	env.unindent();
	env.println("}");
      }
      env.unindent();
      env.println("}");
    } else {
Anders Blomdell's avatar
Anders Blomdell committed
      for (int i = 0 ; i < getNumExp() ; i++) {
      	env.println("result += labcomm_size_packed32(" + 
	            getExp(i).C_getLimit(env, i) + ");");
      }
Anders Nilsson's avatar
Anders Nilsson committed
      env.print("result += " + getType().C_fixedSizeof());
      for (int i = 0 ; i < getNumExp() ; i++) {
	env.print(" * " + getExp(i).C_getLimit(env, i));
      }
      env.println(";");      
    }
  }

}

aspect C_forAll {

  public void Program.C_emitForAll(C_env env) {
    env.print("#define LABCOMM_FORALL_SAMPLES_" + env.lcName + 
		"(func, sep)");
    env.indent();
    boolean needSeparator = false;
    for (int i = 0; i < getNumDecl(); i++) {
      String s = getDecl(i).C_forAll(env);
      if (s != null) {
	if (needSeparator) { env.print(" sep"); }
	env.println(" \\");
	env.print(s);
	needSeparator = true;
      }
    }
    env.println("");
    env.unindent();
  }
  
  public String Decl.C_forAll(C_env env) {
    return null;
  }

  public String SampleDecl.C_forAll(C_env env) {
    return "func(" + getName() + ", " + env.prefix + getName() + ")";
  }

}

aspect C_Info {

  public void Program.C_info(PrintStream out, String prefix) {
    C_env env = new C_env("", "", prefix, out);
    for (int i = 0; i < getNumDecl(); i++) {
      getDecl(i).C_info(env);
    }
  }

  public void Decl.C_info(C_env env) {
    throw new Error(this.getClass().getName() + 
		    ".C_info((C_env env)" + 
		    " not declared");
  }

  public void TypeDecl.C_info(C_env env) {
    env.println(",C,typedef," + env.prefix + getName() + "," + 
Anders Nilsson's avatar
Anders Nilsson committed
  }

  public void SampleDecl.C_info(C_env env) {
    env.println(",C,sample," + env.prefix + getName() + "," +