C_CodeGen.jrag 50.1 KB
Newer Older
1001
1002
1003
1004
1005
1006
1007
    env.println("va_end(va);");
    env.println("return result;");
    env.unindent();
    env.println("}");
  }
}

Anders Nilsson's avatar
Anders Nilsson committed
1008
1009
1010
1011

aspect C_Encoder {

  public void Decl.C_emitEncoder(C_env env) {
1012
1013
1014
  //XXX  throw new Error(this.getClass().getName() + 
//		    ".C_emitEncoder()" + 
//		    " not declared");
Anders Nilsson's avatar
Anders Nilsson committed
1015
1016
  }

1017
1018
//  public void TypeDecl.C_emitEncoder(C_env env) {
//  }
Anders Nilsson's avatar
Anders Nilsson committed
1019
1020
1021

  public void SampleDecl.C_emitEncoder(C_env env) {
    env = env.nestStruct("(*v)");
1022
    env.println("static int encode_" + env.prefix + getName() + "(");
Anders Nilsson's avatar
Anders Nilsson committed
1023
    env.indent();
1024
1025
1026
1027
    env.println("struct labcomm"+env.verStr+"_writer *w");
    if(!isVoid() ) {
        env.println(", "+env.prefix + getName() + " *v");
    }
Anders Nilsson's avatar
Anders Nilsson committed
1028
1029
1030
1031
    env.unindent();
    env.println(")");
    env.println("{");
    env.indent();
1032
    env.println("int result = 0;");
Anders Nilsson's avatar
Anders Nilsson committed
1033
    getType().C_emitEncoder(env);
1034
    env.println("return result;");
Anders Nilsson's avatar
Anders Nilsson committed
1035
1036
1037
1038
    env.unindent();
    env.println("}");

    // Typesafe encode wrapper
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
1039
    env.println("int labcomm"+env.verStr+"_encode_" + env.prefix + getName() + "(");
1040
1041
1042
1043
    env.println("struct labcomm"+env.verStr+"_encoder *e");
    if(!isVoid() ) {
        env.println(", "+env.prefix + getName() + " *v");
    }
Anders Nilsson's avatar
Anders Nilsson committed
1044
1045
1046
1047
    env.unindent();
    env.println(")");
    env.println("{");
    env.indent();
1048
    env.println("return labcomm"+env.verStr+"_internal_encode(e, &signature_" + 
1049
		env.prefix + getName() + 
1050
1051
1052
		", (labcomm"+env.verStr+"_encoder_function)encode_" + 
                env.prefix + getName() +
		(!isVoid()?", v":", NULL")+");");
Anders Nilsson's avatar
Anders Nilsson committed
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
    env.unindent();
    env.println("}");
  }

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

  public void VoidType.C_emitEncoder(C_env env) {
1064
    env.println("result = 0;");
Anders Nilsson's avatar
Anders Nilsson committed
1065
1066
  }

1067
1068
1069
1070
1071
1072
1073
  public void SampleRefType.C_emitEncoder(C_env env) {
    env.println("result = labcomm"+env.verStr+"_write_int(w, " + 
                "labcomm_internal_encoder_signature_to_index(w->encoder, " +
                env.qualid + "));");
    env.println("if (result != 0) { return result; }");
  }

Anders Nilsson's avatar
Anders Nilsson committed
1074
  public void PrimType.C_emitEncoder(C_env env) {
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
1075
    env.println("result = labcomm"+env.verStr+"_write_" + getName() + 
1076
                "(w, " + env.qualid + ");");
1077
    env.println("if (result != 0) { return result; }");
Anders Nilsson's avatar
Anders Nilsson committed
1078
1079
1080
  }

  public void UserType.C_emitEncoder(C_env env) {
1081
    decl().getType().C_emitEncoder(env);
Anders Nilsson's avatar
Anders Nilsson committed
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
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
  }

  public void StructType.C_emitEncoder(C_env env) {
    for (int i = 0 ; i < getNumField() ; i++) {
      getField(i).C_emitEncoder(env);
    }
  }

  public void ArrayType.C_emitEncoder(C_env env) {
    C_emitEncoderEncodeLimit(env);
    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_emitEncoder(C_Nest(env));
    for (int i = getNumExp() - 1 ; i >= 0 ; i--) {
      env.unindent();
      env.println("}");
    }
    env.unindent();
    env.println("}");
  }

  public void Field.C_emitEncoder(C_env env) {
    getType().C_emitEncoder(env.nestStruct("." + getName()));
  }

  public void Exp.C_emitEncoderEncodeLimit(C_env env, int i) {
  }

  public void VariableSize.C_emitEncoderEncodeLimit(C_env env, int i) {
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
1122
    env.println("labcomm"+env.verStr+"_write_packed32(w, " + env.qualid + ".n_" + i + ");");
Anders Nilsson's avatar
Anders Nilsson committed
1123
1124
1125
1126
1127
1128
1129
1130
1131
  }

  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) {
1132
1133
1134
1135
1136
1137
1138
1139
1140
  //XXX
  //  throw new Error(this.getClass().getName() + 
  //		    ".C_emitEncoderRegisterHandler(C_env env)" + 
  //		    " not declared");
  }
  //
  //public void TypeDecl.C_emitEncoderRegisterHandler(C_env env) {
  //}
  //
Anders Nilsson's avatar
Anders Nilsson committed
1141
  public void SampleDecl.C_emitEncoderRegisterHandler(C_env env) {
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
1142
    env.println("int labcomm"+env.verStr+"_encoder_register_" + 
Anders Nilsson's avatar
Anders Nilsson committed
1143
1144
		env.prefix + getName() + "(");
    env.indent();
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
1145
    env.println("struct labcomm"+env.verStr+"_encoder *e");
Anders Nilsson's avatar
Anders Nilsson committed
1146
1147
1148
1149
    env.unindent();
    env.println(")");
    env.println("{");
    env.indent();
1150
1151
1152
    C_emitUserTypeDeps(env, null, false); //XXX HERE BE DRAGONS
                                          //set to false to turn off
                                          //outputting of code
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
1153
    env.println("return labcomm"+env.verStr+"_internal_encoder_register(");
Anders Nilsson's avatar
Anders Nilsson committed
1154
1155
    env.indent();
    env.println("e,");
1156
    env.println("&signature_" + env.prefix + getName() + ",");
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
1157
    env.println("(labcomm"+env.verStr+"_encoder_function)encode_" + env.prefix + getName());
Anders Nilsson's avatar
Anders Nilsson committed
1158
1159
1160
1161
1162
    env.unindent();
    env.println(");");
    env.unindent();
    env.println("}");
  }
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177

}

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) {
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
1178
    env.println("int labcomm"+env.verStr+"_encoder_ioctl_" + env.prefix + getName() + "(");
1179
    env.indent();
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
1180
    env.println("struct labcomm"+env.verStr+"_encoder *e,");
1181
1182
1183
1184
1185
1186
1187
1188
1189
    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);");
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
1190
    env.println("result = labcomm"+env.verStr+"_internal_encoder_ioctl(");
1191
    env.indent();
1192
    env.println("e, &signature_" + env.prefix + getName() + ", ");
1193
    env.println("ioctl_action, va);");
1194
1195
1196
1197
1198
1199
    env.unindent();
    env.println("va_end(va);");
    env.println("return result;");
    env.unindent();
    env.println("}");
  }
Anders Nilsson's avatar
Anders Nilsson committed
1200
1201
1202

}

1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
aspect C_TypeDependencies {
  public void Decl.C_emitUserTypeDeps(C_env env, String via, boolean outputCode) {
    if( hasDependencies() ) {
        Iterator<Decl> it = type_dependencies().iterator();
        while(it.hasNext()) {
            Decl t = it.next();

            t.C_emitUserTypeDeps(env, t.getName(), outputCode);
            if(outputCode) {
               System.out.println("Decl.C_emitUserTypeDeps registering "+t.getName());
               env.println("labcomm"+env.verStr+"_encoder_register_"+env.prefix + t.getName()+"(e);");
            } else {  // Just output a comment
	        String refpath = (via == null) ? "directly" : "indirectly via "+via;
	       //env.println(" //Depends ("+refpath+") on "+t.getName() + " (" + t +") " );
	       env.println(" //Depends ("+refpath+") on "+t.getName() );
            }
        }
    } 
  }
  public void Decl.C_emitUserTypeRefs(C_env env, String via, boolean outputCode) {
    if( isReferenced() ) {
        Iterator<Decl> it = type_references().iterator();
        while(it.hasNext()) {
            Decl t = it.next();

            t.C_emitUserTypeRefs(env, t.getName(), outputCode);
            if(outputCode) {
               env.println("labcomm"+env.verStr+"_encoder_register_"+env.prefix + t.getName()+"(e);");
            } else {  // Just output a comment
	        String refpath = (via == null) ? "directly" : "indirectly via "+via;
	       env.println(" //Is referenced ("+refpath+")  by "+t.getName() );
            }
        }
    } 
  }
}

Anders Nilsson's avatar
Anders Nilsson committed
1240
1241
aspect C_Signature {

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

1248
1249
1250
1251
  syn String Decl.C_DeclTypeString();
  eq SampleDecl.C_DeclTypeString() = "LABCOMM_SAMPLE";
  eq TypeDecl.C_DeclTypeString() = "LABCOMM_TYPEDEF";

1252
  public void Decl.C_emitSignature(C_env env) {
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
    //always emit the flat signature, as it is needed
    //for matching at the decoder side (which cannot know
    //the type_ids of dependent types. Therefore, flat sigs
    //are used for matching
    C_emitFlatSignature(env);
/*
    if( false && (isReferenced() || isSampleDecl())){ 
      Signature signature = getSignature();
      signature.C_emitSignature(env, !isSampleDecl());
    } else {
      env.println("// not emitting signature for "+getName()+isReferenced()+isSampleDecl());  
    }
    if(env.versionHasMetaData()) { 
      if(isReferenced() || isSampleDecl()){
          env.println("(int (*)(void *))labcomm"+env.verStr+"_signature_" + 
  	 	env.prefix + getName() + "_emit_signature");
      } else {
          env.println("NULL");  // HERE BE DRAGONS! Is it worth the size saving to skip emitting the emit_signature function for unused types?
                                //                  The code won't likely end up in a target system anyway?
      }
    }
    env.unindent();
    env.println(" };");
*/
  }

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

  public void Decl.C_emitFlatSignature(C_env env) {
Anders Nilsson's avatar
Anders Nilsson committed
1286
1287
  }

1288
  public void SampleDecl.C_emitFlatSignature(C_env env){
Anders Nilsson's avatar
Anders Nilsson committed
1289
    env.println("static unsigned char signature_bytes_" + 
1290
1291
  	        env.prefix + getName() + "[] = {");
    SignatureList signature = flatSignature(env.version);
Anders Nilsson's avatar
Anders Nilsson committed
1292
1293
1294
1295
1296
    for (int i = 0 ; i < signature.size() ; i++) {
      String comment = signature.getComment(i);
      if (comment != null) {
        env.println(signature.getIndent(i) + "// " + comment);
      }
1297
      byte[] data = signature.getData(i, env.version);
Anders Nilsson's avatar
Anders Nilsson committed
1298
1299
1300
1301
1302
1303
1304
1305
1306
      if (data != null) {
        env.print(signature.getIndent(i));
        for (int j = 0 ; j < data.length ; j++) {
          env.print(data[j] + ", ");
        }
        env.println("");
      }
    }
    env.println("};");
1307

1308
    C_emitSizeofValue(env);
1309
1310
    env.println("static struct labcomm"+env.verStr+"_signature " +
                "signature_" + env.prefix + getName() + " = {");
1311
    env.indent();
1312
1313
    env.println("\"" + getName() + "\",");
    env.println("sizeof_" + env.prefix + getName() + ",");
1314
1315
1316
1317
1318
    env.println("sizeof(signature_bytes_" + env.prefix + getName() + "),");
    env.println("signature_bytes_" + env.prefix + getName() + ",");
    env.println("0");
    env.unindent();
    env.println(" };");
1319
1320
1321
    env.println("const struct labcomm"+env.verStr+"_signature " +
                "*labcomm"+env.verStr+"_signature_" + env.prefix + getName() + 
                " = &signature_" + env.prefix + getName() + ";");
Anders Nilsson's avatar
Anders Nilsson committed
1322
1323
  }

1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
    public void Signature.C_emitSignature(C_env env, boolean decl){
      getSignatureList().C_emitSignature(env, decl);
    }

    public abstract void SignatureLine.C_emitSignature(C_env env, boolean decl);

    public void TypeRefSignatureLine.C_emitSignature(C_env env, boolean isDecl){ 
      //env.print(getIndentString());
      //env.println("LABCOMM_SIGDEF_SIGNATURE(labcomm"+env.verStr+"_signature_" + env.prefix + decl.getName() +"),");
    }

    public void DataSignatureLine.C_emitSignature(C_env env, boolean decl){ 
       //   String comment = getComment();
       //   if (comment != null && comment.length() > 0) {
       //     env.println(getIndentString() + "// " + comment);
       //   }
       //   byte[] data = getData(env.version);
       //   if (data != null && data.length > 0) {
       //     env.print(getIndentString());
       //     env.print("LABCOMM_SIGDEF_BYTES("+data.length+", \"");
       //     for (int j = 0 ; j < data.length ; j++) {
       //       byte d = data[j];
       //       //if(d>='a'&&d<='z' || d>='A'&&d<='Z'|| d>='0'&&d<='9'  )
       //       //  env.print(""+(char)d);
       //       //else
       //       env.print("\\x"+Integer.toHexString(d));
       //     }
       //     env.println("\"),");
       // }
    }
/*

        byte[] data = getData(env.version);
          if (data != null) {
              for (int j = 0 ; j < data.length ; j++) {
                  env.print(getIndentString());
                  //env.print("printf(\"labcomm"+env.verStr+"_write_byte( w, (unsigned char)"+ String.format("0x%02X ", data[j]) +")\\n\"); ");
                  env.print("labcomm"+env.verStr+"_write_byte( w, (unsigned char)"+ String.format("0x%02X ", data[j]) +"); ");
                  env.println("if (result != 0) { return result; }");
              }
              env.println();
          }
*/
  //}
/**
static int encode_test_twoLines(
struct labcomm_writer *w
, test_twoLines *v
)
{
result = labcomm_write_int(w, (*v).l1.start.x.val);
**/
  public void SignatureList.C_emitSignature(C_env env, boolean decl) { 
    env.println("static struct labcomm_signature_data signature_tree_" + 
		 env.prefix + parentDecl().getName() + "[] = {");
    env.indent();
    for (int i = 0 ; i < size() ; i++) {
      SignatureLine l = getSignatureLine(i);
      l.C_emitSignature(env, decl);
    }
    
    env.println("LABCOMM_SIGDEF_END");
    env.println("};");
    env.unindent();
    env.println();
  }



//  public void SampleDecl.C_emitSignature(C_env env) {
//    env.println("static unsigned char signature_bytes_" + 
//		       env.prefix + getName() + "[] = {");
//    SignatureList signature = signature(env.version);
//    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"+env.verStr+"_signature labcomm"+env.verStr+"_signature_" + 
//		env.prefix + getName() + " = {");
//    env.indent();
//    env.println("LABCOMM_SAMPLE, \"" + getName() + "\",");
//    env.println("(int (*)(struct labcomm"+env.verStr+"_signature *, void *))labcomm"+env.verStr+"_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(" };");
//  }

}
aspect C_Constructor {
1427
  public void ASTNode.C_emitConstructor(C_env env) {
Anders Nilsson's avatar
Anders Nilsson committed
1428
    throw new Error(this.getClass().getName() + 
1429
		    ".C_emitConstructor(C_env env)" + 
Anders Nilsson's avatar
Anders Nilsson committed
1430
1431
1432
		    " not declared");
  }

1433
1434
1435
1436
  public void Program.C_emitConstructor(C_env env) {
    env.println("LABCOMM_CONSTRUCTOR void init_" +
		env.prefix + "_signatures(void)");
    env.println("{");
1437
    env.indent();
1438
1439
1440
1441
1442
1443
1444
    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);
    }
1445
    env.unindent();
1446
1447
1448
1449
1450
1451
1452
    env.println("}"); 
    env.unindent();
    env.println("}"); 
  }

  public void Decl.C_emitConstructor(C_env env) {
  }
1453
//XXX
1454
  public void SampleDecl.C_emitConstructor(C_env env) {
1455
    env.println("labcomm"+env.verStr+"_set_local_index(&signature_" + 
1456
		env.prefix + getName() + ");");
Anders Nilsson's avatar
Anders Nilsson committed
1457
1458
1459
  }


Anders Blomdell's avatar
Anders Blomdell committed
1460
1461
1462
1463
1464
1465
1466
  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) {
1467
    env.println("void init_" + env.prefix + "_signatures(void);");
Anders Blomdell's avatar
Anders Blomdell committed
1468
1469
  }

Anders Nilsson's avatar
Anders Nilsson committed
1470
1471
1472
}

aspect C_Sizeof {
1473
 public void Decl.C_emitSizeofDeclaration(C_env env) {
Anders Nilsson's avatar
Anders Nilsson committed
1474
1475
1476
  }

  public void SampleDecl.C_emitSizeofDeclaration(C_env env) {
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
1477
    env.println("extern int labcomm"+env.verStr+"_sizeof_" + env.prefix + getName() +
1478
        "(" + env.prefix + getName() + " *v);");
Anders Nilsson's avatar
Anders Nilsson committed
1479
1480
1481
1482
1483
1484
  }

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

1485
1486
1487
1488
1489
1490
  public void Decl.C_emitSizeof(C_env env) {
  }

  public void SampleDecl.C_emitSizeof(C_env env) {
    env = env.nestStruct("(*v)");
    env.println("int labcomm"+env.verStr+"_sizeof_" + env.prefix + getName() +
1491
        "(" + env.prefix + getName() + " *v)");
1492
1493
    env.println("{");
    env.indent();
Anders Blomdell's avatar
Anders Blomdell committed
1494
    env.println("return labcomm"+env.verStr+"_internal_sizeof(" +
1495
                "&signature_" + env.prefix + getName() +
1496
1497
1498
1499
1500
                ", v);");
    env.unindent();
    env.println("}");
  }

Anders Nilsson's avatar
Anders Nilsson committed
1501
1502
  public int Type.C_fixedSizeof() {
    throw new Error(this.getClass().getName() + 
1503
1504
            ".C_fixedSizeof()" + 
            " not declared");
Anders Nilsson's avatar
Anders Nilsson committed
1505
1506
1507
1508
1509
1510
  }

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

1511
1512
1513
1514
  public int SampleRefType.C_fixedSizeof() {
    return 4;
  }

Anders Nilsson's avatar
Anders Nilsson committed
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
  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: { 
1525
1526
1527
    throw new Error(this.getClass().getName() + 
            ".C_fixedSizeof()" + 
            " unknown size (" + getName() + ")"); 
Anders Nilsson's avatar
Anders Nilsson committed
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
      } 
    }
  }

  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;
  }

1553
  public void Decl.C_emitSizeofValue(C_env env) {
Anders Nilsson's avatar
Anders Nilsson committed
1554
1555
  }

1556
  public void SampleDecl.C_emitSizeofValue(C_env env) {
Anders Nilsson's avatar
Anders Nilsson committed
1557
    env = env.nestStruct("(*v)");
1558
    env.println("static int sizeof_" + env.prefix + getName() + "(void *vv)");
Anders Nilsson's avatar
Anders Nilsson committed
1559
1560
    env.println("{");
    env.indent();
1561
    env.println("int result = 0;");
Anders Nilsson's avatar
Anders Nilsson committed
1562
    if (C_isDynamic()) {
1563
      env.println(env.prefix + getName() + " *v = vv;");
Anders Nilsson's avatar
Anders Nilsson committed
1564
1565
      getType().C_emitSizeof(env);
    } else {
Anders Blomdell's avatar
Anders Blomdell committed
1566
      env.println("result += " + C_fixedSizeof() + ";");
Anders Nilsson's avatar
Anders Nilsson committed
1567
    }    
Anders Blomdell's avatar
Anders Blomdell committed
1568
    env.println("return result;");
Anders Nilsson's avatar
Anders Nilsson committed
1569
1570
1571
1572
1573
1574
    env.unindent();
    env.println("}");
  }

  public void Type.C_emitSizeof(C_env env) {
    throw new Error(this.getClass().getName() + 
1575
1576
            ".C_emitSizeof(C_env env)" + 
            " not declared");
Anders Nilsson's avatar
Anders Nilsson committed
1577
1578
1579
1580
1581
  }

  public void PrimType.C_emitSizeof(C_env env) {
    switch (getToken()) {
      case LABCOMM_STRING: { 
Anders Blomdell's avatar
Anders Blomdell committed
1582
        env.print("{ int l = strlen(" + env.qualid + "); ");
1583
    env.println("result += labcomm"+env.verStr+"_size_packed32(l) + l; }"); 
Anders Nilsson's avatar
Anders Nilsson committed
1584
1585
      } break;
      default: { 
1586
1587
1588
    throw new Error(this.getClass().getName() + 
            ".C_emitSizeof(C_env env)" + 
            " known size (" + getName() + ")"); 
Anders Nilsson's avatar
Anders Nilsson committed
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
      } 
    }
  }

  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()) {
1601
1602
    getField(i).getType().C_emitSizeof(
      env.nestStruct("." + getField(i).getName()));
Anders Nilsson's avatar
Anders Nilsson committed
1603
      } else {
1604
    fixed += getField(i).getType().C_fixedSizeof();
Anders Nilsson's avatar
Anders Nilsson committed
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
      }
    }
    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++) {
1618
1619
1620
1621
1622
1623
1624
    String iterator = "i_" + env.depth + "_" + i;
    env.println("for (" + iterator + " = 0" +
            " ; " +
            iterator + " < " + getExp(i).C_getLimit(env, i) +
            " ; " +
            iterator + "++) {");
    env.indent();
Anders Nilsson's avatar
Anders Nilsson committed
1625
1626
1627
1628
      }
      C_emitCalcIndex(env);
      getType().C_emitSizeof(C_Nest(env));
      for (int i = 0 ; i < getNumExp() ; i++) {
1629
1630
    env.unindent();
    env.println("}");
Anders Nilsson's avatar
Anders Nilsson committed
1631
1632
1633
1634
      }
      env.unindent();
      env.println("}");
    } else {
Anders Blomdell's avatar
Anders Blomdell committed
1635
      for (int i = 0 ; i < getNumExp() ; i++) {
1636
1637
        env.println("result += labcomm"+env.verStr+"_size_packed32(" + 
                getExp(i).C_getLimit(env, i) + ");");
Anders Blomdell's avatar
Anders Blomdell committed
1638
      }
Anders Nilsson's avatar
Anders Nilsson committed
1639
1640
      env.print("result += " + getType().C_fixedSizeof());
      for (int i = 0 ; i < getNumExp() ; i++) {
1641
    env.print(" * " + getExp(i).C_getLimit(env, i));
Anders Nilsson's avatar
Anders Nilsson committed
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
      }
      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 {

1680
1681
  public void Program.C_info(PrintStream out, String prefix, int version) {
    C_env env = new C_env("", "", prefix, out, version);
Anders Nilsson's avatar
Anders Nilsson committed
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
    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) {
Anders Blomdell's avatar
Anders Blomdell committed
1694
    env.println(",C,typedef," + env.prefix + getName() + "," + 
1695
1696
                 env.prefix + getName() + "," +
                 C_info_type_or_void(env.prefix));
Anders Nilsson's avatar
Anders Nilsson committed
1697
1698
1699
  }

  public void SampleDecl.C_info(C_env env) {
Anders Blomdell's avatar
Anders Blomdell committed
1700
    env.println(",C,sample," + env.prefix + getName() + "," + 
1701
1702
                 env.prefix + getName() + "," +
                 C_info_type_or_void(env.prefix));
Anders Nilsson's avatar
Anders Nilsson committed
1703
1704
  }

1705
1706
1707
1708
1709
1710
1711
1712
1713
  // make void types explicitly as they require special treatment
  // in encoder/decoder calls
  protected String Decl.C_info_type_or_void(String prefix) {
    if(isVoid() ) {
      return "void";
    } else {
      return prefix + getName() ;
    }
  }
Anders Nilsson's avatar
Anders Nilsson committed
1714
}