C_CodeGen.jrag 29.2 KB
Newer Older
Anders Nilsson's avatar
Anders Nilsson committed
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107

  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 {
      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," + getName() + "," + getName());
  }

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

}