Skip to content
Snippets Groups Projects
C_CodeGen.jrag 29.2 KiB
Newer Older
Anders Nilsson's avatar
Anders Nilsson committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
import java.util.Vector;

aspect C_CodeGenEnv {

  // Environment wrapper for C-code generation
  // handles qualid nesting, indentation, file writing and
  // prefix propagation

  public class C_env {

    final private class C_printer {
      
      private boolean newline = true;
      private PrintStream out;

      public C_printer(PrintStream out) {
	this.out = out;
      }

      public void print(C_env env, String s) {
	if (newline) {
	  newline = false;
	  for (int i = 0 ; i < env.indent ; i++) {
	    out.print("  ");
	  }
	}
	out.print(s);
      }
      public void println(C_env env, String s) {
	print(env, s);
	out.println();
	newline = true;
      }
    }

    public final String qualid;
    public final String lcName;
    public final String rawPrefix;
    public final String prefix;
    private int indent;
    public final int depth;
    private C_printer printer;

    private C_env(String qualid, String lcName, String rawPrefix, 
		  int indent, int depth, C_printer printer) {
      this.qualid = qualid;
      this.lcName = lcName;
      this.rawPrefix = rawPrefix;
      this.prefix = rawPrefix + "_";
      this.indent = indent;
      this.depth = depth;
      this.printer = printer;
    }

    public C_env(String qualid, String lcName, String rawPrefix, 
		 PrintStream out) {
      this.qualid = qualid;
      this.lcName = lcName;
      this.rawPrefix = rawPrefix;
      this.prefix = rawPrefix + "_";
      this.depth = 0;
      this.indent = 0;
      this.printer = new C_printer(out);
    }

    public C_env nestArray(String suffix) {
      return new C_env(qualid + suffix, lcName, rawPrefix, 
		       indent, depth + 1, printer);
    }

    public C_env nestStruct(String suffix) {
      return new C_env(qualid + suffix, lcName, rawPrefix, 
		       indent, depth, printer);
    }

    public void indent() {
      indent++;
    }

    public void unindent() {
      indent--;
    }

    public String prefix() {
      return rawPrefix;
    }

    public void print(String s) {
      printer.print(this, s);
    }

    public void println(String s) {
      printer.println(this, s);
    }

  }

  public C_env ArrayType.C_Nest(C_env env) {
    throw new Error(this.getClass().getName() + 
		    ".C_Nest(C_env env)" + 
		    " not declared");
  }

  public C_env FixedArrayType.C_Nest(C_env env) {
    String index = ".a";
    for (int i = 0 ; i < getNumExp() ; i++) {
      index += "[i_" + env.depth + "_" + i + "]";
    }
    return env.nestArray(index);
  }

  public C_env VariableArrayType.C_Nest(C_env env) {
    return env.nestArray(".a[i_" + env.depth + "]");
  }


}

aspect C_IsDynamic {
  
  // Determine if a type has dynamically allocated data
  syn boolean Decl.C_isDynamic() = getType().C_isDynamic();
  syn boolean Type.C_isDynamic() = false;
  syn boolean PrimType.C_isDynamic() = getToken() == LABCOMM_STRING;
  syn boolean UserType.C_isDynamic() = 
    lookupType(getName()).getType().C_isDynamic();
  syn boolean StructType.C_isDynamic() {
    for (int i = 0 ; i < getNumField() ; i++) {
      if (getField(i).getType().C_isDynamic()) {
	return true;
      }
    }
    return false;
  }
  syn boolean FixedArrayType.C_isDynamic() = getType().C_isDynamic();
  syn boolean VariableArrayType.C_isDynamic() = true;
}

aspect C_CodeGen {

  public void Program.C_genH(PrintStream out, Vector includes, 
			     String lcName, String prefix) {
    C_env env = new C_env("", lcName, prefix, out);

    // Hackish prettyprint preamble
    out.println("/* LabComm declarations:");
    pp(out);
    out.println("*/");
    env.println("");
    env.println("");
    env.println("#ifndef __LABCOMM_" + env.lcName + "_H__"); 
    env.println("#define __LABCOMM_" + env.lcName + "_H__");
    env.println("");

    // Include
    env.println("#include \"labcomm.h\"");
    for (int i = 0 ; i < includes.size() ; i++) {
      env.println("#include \"" + includes.get(i) + "\"");
    }
    env.println("");

    C_emitH(env);

    env.println("#endif");
  }

  public void Program.C_genC(PrintStream out, Vector includes, 
			     String lcName, String prefix) {
    C_env env = new C_env("", lcName, prefix, out);

    // Include
    env.println("#include \"labcomm.h\"");
    env.println("#include \"labcomm_private.h\"");
    for (int i = 0 ; i < includes.size() ; i++) {
      env.println("#include \"" + includes.get(i) + "\"");
    }
    env.println("");
    
    // Method Implementations
    C_emitC(env);
  }

  public void Program.C_emitH(C_env env) {
    for (int i = 0; i < getNumDecl(); i++) {
      getDecl(i).C_emitType(env);
//      getDecl(i).C_emitSignatureDeclaration(env);
      getDecl(i).C_emitDecoderRegisterDeclaration(env);
      getDecl(i).C_emitEncoderDeclaration(env);
      getDecl(i).C_emitSizeofDeclaration(env);
      env.println("");
    }
    C_emitForAll(env);
  }

  public void Program.C_emitC(C_env env) {
    C_emitSignature(env);
    for (int i = 0; i < getNumDecl(); i++) {
      getDecl(i).C_emitDecoder(env);
      getDecl(i).C_emitDecoderRegisterHandler(env);
      getDecl(i).C_emitEncoder(env);
      getDecl(i).C_emitEncoderRegisterHandler(env);
      getDecl(i).C_emitSizeof(env);
    }
  }
 
}

aspect C_Common {

  public void ArrayType.C_emitLoopVariables(C_env env) {
    for (int i = 0 ; i < getNumExp() ; i++) {
      env.println("int i_" + env.depth + "_" + i + ";");
    }
  }

}

aspect C_Type {

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

  public void TypeDecl.C_emitType(C_env env) {
    env.println("#ifndef PREDEFINED_" + env.prefix + getName());
    env.print("typedef ");
    getType().C_emitType(env, env.prefix + getName());
    env.println(";");
    env.println("#endif");
  }

  public void SampleDecl.C_emitType(C_env env) {
    env.println("#ifndef PREDEFINED_" + env.prefix + getName());
    env.print("typedef ");
    getType().C_emitType(env, env.prefix + getName());
    env.println(";");
    env.println("#endif");
  }

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

  public void VoidType.C_emitType(C_env env, String name) {
    env.print("char " + name);
  }

  public void PrimType.C_emitType(C_env env, String name) {
    switch (getToken()) {
      case LABCOMM_BOOLEAN: { env.print("unsigned char"); } break;
      case LABCOMM_BYTE: { env.print("unsigned char"); } break;
      case LABCOMM_STRING: { env.print("char*"); } break;
      case LABCOMM_LONG: { env.print("long long"); } break;
      default: { env.print(getName()); } break;
    }
    env.print(" " + name);
  }

  public void UserType.C_emitType(C_env env, String name) {
    env.print(env.prefix + getName() + " " + name);
  }

  public void StructType.C_emitType(C_env env, String name) {
    env.println("struct {");
    env.indent();
    for (int i = 0 ; i < getNumField() ; i++) {
      getField(i).C_emitType(env);
      env.println(";");
    }
    env.unindent();
    env.print("} " + name);
  }

  public void Field.C_emitType(C_env env) {
    getType().C_emitType(env, getName());
  }

  public void FixedArrayType.C_emitType(C_env env, String name) {
    env.println("struct {");
    env.indent();
    StringBuffer index = new StringBuffer("a");
    for (int i = 0 ; i < getNumExp() ; i++) {
      index.append("[" + getExp(i).C_getValue() + "]");
    }
    getType().C_emitType(env, index.toString());
    env.println(";");
    env.unindent();
    env.print("} " + name);
  }

  public void VariableArrayType.C_emitType(C_env env, String name) {
    env.println("struct {");
    env.indent();
    for (int i = 0 ; i < getNumExp() ; i++) {
      if (getExp(i) instanceof VariableSize) {
	env.println("int n_" + i + ";");
      } else {
	env.println("// n_" + i + "=" + getExp(i).C_getValue());
      }
    }
    getType().C_emitType(env, "*a");
    env.println(";");
    env.unindent();
    env.print("} " + name);
  }

  public String Exp.C_getValue() {
   throw new Error(this.getClass().getName() + 
		    ".C_getValue()" + 
		    " not declared");
  }

  public String IntegerLiteral.C_getValue() {
    return getValue();
  }

}

aspect C_Declarations {

  public void Decl.C_emitDecoderRegisterDeclaration(C_env env) {
  }

  public void SampleDecl.C_emitDecoderRegisterDeclaration(C_env env) {
    env.println("void labcomm_decoder_register_" + 
		env.prefix + getName() + "(");
    env.indent();
    env.println("struct labcomm_decoder *d,");
    env.println("void (*handler)(");
    env.indent();
    env.println(env.prefix + getName() + " *v,");
    env.println("void *context");
    env.unindent();
    env.println("),");
    env.println("void *context");
    env.unindent();
    env.println(");");
  }
  
  public void Decl.C_emitEncoderDeclaration(C_env env) {
  }

  public void SampleDecl.C_emitEncoderDeclaration(C_env env) {
    env.println("void labcomm_encoder_register_" + 
		env.prefix + getName() + "(");
    env.indent();
    env.println("struct labcomm_encoder *e);");
    env.unindent();

    env.println("void labcomm_encode_" + env.prefix + getName() + "(");
    env.indent();
    env.println("struct labcomm_encoder *e,");
    env.println(env.prefix + getName() + " *v");
    env.unindent();
    env.println(");");
  }

}

aspect C_Limit {

  public String Exp.C_getLimit(C_env env, int i) {
    throw new Error(this.getClass().getName() + 
		    ".C_emitDecoderLimit(C_env env, int i)" + 
		    " not declared");
  }

  public String IntegerLiteral.C_getLimit(C_env env, int i) {
    return getValue();
  }

  public String VariableSize.C_getLimit(C_env env, int i) {
    return env.qualid + ".n_" + i;
  }
  
}

aspect C_Index {

  public void ArrayType.C_emitCalcIndex(C_env env) {
  }

  public void VariableArrayType.C_emitCalcIndex(C_env env) {
    env.print("int i_" + env.depth + " = ");

    String i_prefix = "i_" + env.depth + "_";
    String expr = i_prefix + "0";
    for (int i = 1 ; i < getNumExp() ; i++) {
      expr = "(" + expr + ") * " +
	getExp(i).C_getLimit(env, i) + " + " + 
	i_prefix + i;
    }
    env.println(expr + ";");
  }

}

aspect C_Decoder {

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

  public void TypeDecl.C_emitDecoder(C_env env) {
  }

  public void SampleDecl.C_emitDecoder(C_env env) {
    env = env.nestStruct("v");
    env.println("static void decode_" + getName() + "(");
    env.indent();
    env.println("labcomm_decoder_t *d,");
    env.println("void (*handle)(");
    env.indent();
    env.println(env.prefix + getName() + " *v,");
    env.println("void *context");
    env.unindent();
    env.println("),");
    env.println("void *context");
    env.unindent();
    env.println(")");
    env.println("{");
    env.indent();
    env.println(env.prefix + getName() + " v;");
    getType().C_emitDecoder(env);
    env.println("handle(&v, context);");
    if (C_isDynamic()) {
      env.println("{");
      env.indent();
      getType().C_emitDecoderDeallocation(env);
      env.unindent();
      env.println("}");
    }
    env.unindent();
    env.println("}");
  }

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

  public void VoidType.C_emitDecoder(C_env env) {
  }

  public void PrimType.C_emitDecoder(C_env env) {
    env.println(env.qualid + " = labcomm_decode_" + getName() + "(d);");
  }

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

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

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

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

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

  public void VariableSize.C_emitDecoderDecodeLimit(C_env env, int i) {
    //env.println(env.qualid + ".n_" + i + " = labcomm_decode_int(d);");
    env.println(env.qualid + ".n_" + i + " = labcomm_decode_packed32(d);");
Anders Nilsson's avatar
Anders Nilsson committed
  }

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

  public void ArrayType.C_emitDecoderArrayAllocate(C_env env) {
  }

  public void VariableArrayType.C_emitDecoderArrayAllocate(C_env env) {
    env.print(env.qualid + ".a = malloc(sizeof(" + env.qualid + ".a[0])");
    for (int i = 0 ; i < getNumExp() ; i++) {
      env.print(" * " + getExp(i).C_getLimit(env, i));
    }
    env.println(");");
  }

  // Code for deallocation of dynamically allocated data 

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

  public void PrimType.C_emitDecoderDeallocation(C_env env) {
    if (C_isDynamic()) {
      env.println("free(" + env.qualid + ");");
    }
  }

  public void UserType.C_emitDecoderDeallocation(C_env env) {
    if (C_isDynamic()) {
      lookupType(getName()).getType().C_emitDecoderDeallocation(env);
    }
  }

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

  public void ArrayType.C_emitDecoderDeallocation(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_emitDecoderDeallocation(C_Nest(env));
      for (int i = 0 ; i < getNumExp() ; i++) {
	env.unindent();
	env.println("}");
      }
      env.unindent();
      env.println("}");
    }
  }

  public void VariableArrayType.C_emitDecoderDeallocation(C_env env) {
    super.C_emitDecoderDeallocation(env);
    env.println("free(" + env.qualid + ".a);");
  }

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

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

  public void TypeDecl.C_emitDecoderRegisterHandler(C_env env) {
  }

  public void SampleDecl.C_emitDecoderRegisterHandler(C_env env) {
    env.println("void labcomm_decoder_register_" + 
		env.prefix + getName() + "(");
    env.indent();
    env.println("struct labcomm_decoder *d,");
    env.println("void (*handler)(");
    env.indent();
    env.println(env.prefix + getName() + " *v,");
    env.println("void *context");
    env.unindent();
    env.println("),");
    env.println("void *context");
    env.unindent();
    env.println(")");
    env.println("{");
    env.indent();
    env.println("labcomm_internal_decoder_register(");
    env.indent();
    env.println("d,");
    env.println("&labcomm_signature_" + env.prefix + getName() + ",");
    env.println("(labcomm_decoder_typecast_t)decode_" + getName() + ",");
    env.println("(labcomm_handler_typecast_t)handler,");
    env.println("context");
    env.unindent();
    env.println(");");
    env.unindent();
    env.println("}");
  }

}


aspect C_Encoder {

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

  public void TypeDecl.C_emitEncoder(C_env env) {
  }

  public void SampleDecl.C_emitEncoder(C_env env) {
    env = env.nestStruct("(*v)");
    env.println("static void encode_" + getName() + "(");
    env.indent();
    env.println("labcomm_encoder_t *e,");
    env.println(env.prefix + getName() + " *v");
    env.unindent();
    env.println(")");
    env.println("{");
    env.indent();
Sven Robertz's avatar
Sven Robertz committed
    env.println("labcomm_encoder_start(e, &labcomm_signature_" + 
		env.prefix + getName() + ");");
Anders Nilsson's avatar
Anders Nilsson committed
    env.println("labcomm_encode_type_index(e, &labcomm_signature_" + 
		env.prefix + getName() + ");");
    env.println("{");
    env.indent();
    getType().C_emitEncoder(env);
    env.unindent();
    env.println("}");
Sven Robertz's avatar
Sven Robertz committed
    env.println("labcomm_encoder_end(e, &labcomm_signature_" + 
		env.prefix + getName() + ");");
Anders Nilsson's avatar
Anders Nilsson committed
    env.unindent();
    env.println("}");

    // Typesafe encode wrapper
    env.println("void labcomm_encode_" + env.prefix + getName() + "(");
    env.println("labcomm_encoder_t *e,");
    env.println(env.prefix + getName() + " *v");
    env.unindent();
    env.println(")");
    env.println("{");
    env.indent();
    env.println("labcomm_internal_encode(e, &labcomm_signature_" + 
		env.prefix + getName() + ", v);");
    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) {
  }

  public void PrimType.C_emitEncoder(C_env env) {
    env.println("labcomm_encode_" + getName() + "(e, " + env.qualid + ");");
  }

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

  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) {
    //env.println("labcomm_encode_int(e, " + env.qualid + ".n_" + i + ");");
    env.println("labcomm_encode_packed32(e, " + 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("void labcomm_encoder_register_" + 
		env.prefix + getName() + "(");
    env.indent();
    env.println("struct labcomm_encoder *e");
    env.unindent();
    env.println(")");
    env.println("{");
    env.indent();
    env.println("labcomm_internal_encoder_register(");
    env.indent();
    env.println("e,");
    env.println("&labcomm_signature_" + env.prefix + getName() + ",");
    env.println("(labcomm_encode_typecast_t)encode_" + getName());
    env.unindent();
    env.println(");");
    env.unindent();
    env.println("}");
  }
 

}

aspect C_Signature {

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

  public void Program.C_emitSignature(C_env env) {
    for (int i = 0; i < getNumDecl(); i++) {
      getDecl(i).C_emitSignature(env);
    }
  }

  public void Decl.C_emitSignature(C_env env) {
  }

  public void SampleDecl.C_emitSignature(C_env env) {
    env.println("static unsigned char signature_bytes_" + 
		       getName() + "[] = {");
//    C_genFlatSignature(env);
    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("LABCOMM_DECLARE_SIGNATURE(labcomm_signature_" + 
		env.prefix + getName() + ") = {");
Anders Nilsson's avatar
Anders Nilsson committed
    env.indent();
    env.println("LABCOMM_SAMPLE, \"" + getName() + "\",");
    env.println("(int (*)(labcomm_signature_t *, void *))labcomm_sizeof_" + 
Anders Nilsson's avatar
Anders Nilsson committed
		env.prefix + getName() + ",");
    env.println("sizeof(signature_bytes_" + getName() + "),");
    env.println("signature_bytes_"+ getName());
    env.unindent();
    env.println(" };");
  }

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

  public void TypeDecl.C_genFlatSignature(C_env env) {
    getType().C_genFlatSignature(env);
  }

  public void SampleDecl.C_genFlatSignature(C_env env) {
    getType().C_genFlatSignature(env);
  }

  public void PrimType.C_genFlatSignature(C_env env) {
    C_genFlatSignature(env, getToken());
    env.println("// " + getName());
  }

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

  public void ArrayType.C_genFlatSignature(C_env env) {
    C_genFlatSignature(env, LABCOMM_ARRAY);
    env.println("// LABCOMM_ARRAY");
    C_genFlatSignature(env, getNumExp());
    env.println("// # of dimensions");
    for (int i = 0 ; i < getNumExp() ; i++) {
      getExp(i).C_genFlatSignature(env);
      env.println("");
    }
    getType().C_genFlatSignature(env);
  }

  public void StructType.C_genFlatSignature(C_env env) {
    C_genFlatSignature(env, LABCOMM_STRUCT);
    env.println("// LABCOMM_STRUCT");
    C_genFlatSignature(env, getNumField());
    env.println("// # of fields");
    for (int i = 0 ; i < getNumField() ; i++) {
      getField(i).C_genFlatSignature(env);
    }
  }

  public void Field.C_genFlatSignature(C_env env) {
    C_genFlatSignature(env, getName());
    env.println("");
    getType().C_genFlatSignature(env);
  }

  public void IntegerLiteral.C_genFlatSignature(C_env env) {
    C_genFlatSignature(env, Integer.parseInt(getValue()));
    env.print("// " + getValue());
  }

  public void VariableSize.C_genFlatSignature(C_env env) {
    C_genFlatSignature(env, 0);
    env.print("// _");
  }

  public void ASTNode.C_genFlatSignature(C_env env, int value) {
    env.print("  ");
    for (int i = 24 ; i >= 0 ; i -= 8) {
      env.print("0x");
      String hex = Integer.toHexString((value >> i) & 0xff);
      if (hex.length() == 1) { env.print("0"); }
      env.print(hex);
      env.print(", ");
    }
  }

  public void ASTNode.C_genFlatSignature(C_env env, String value) {
    C_genFlatSignature(env, value.length());
    env.println("");
    env.print("  ");
    for (int i = 0 ; i < value.length() ; i++) {
      env.print("'" + value.charAt(i) +"', ");
    }
  }

}

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() +
		"(labcomm_signature_t *sig, " + 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() +
		"(labcomm_signature_t *sig, " + env.prefix + getName() + " *v)");
Anders Nilsson's avatar
Anders Nilsson committed
    env.println("{");
    env.indent();
    if (C_isDynamic()) {
Anders Nilsson's avatar
Anders Nilsson committed
      getType().C_emitSizeof(env);
      env.println("return result;");
    } else {
      env.println("return " + (0 + C_fixedSizeof()) + ";");
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() + ")"); 
      } 
    }
  }