C_CodeGen.jrag 51.2 KB
Newer Older
Anders Nilsson's avatar
Anders Nilsson committed
1
2
3
4
5
6
7
8
9
10
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 {

11
    final private static class C_printer {
Anders Nilsson's avatar
Anders Nilsson committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
      
      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;
      }
    }

36
    public final int version; //labcomm version (2006 or 2014)
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
37
    public final String verStr; // version suffix to append (currently _2006 and empty string)
38

Anders Nilsson's avatar
Anders Nilsson committed
39
40
41
42
43
44
45
    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;
46
47
48
    public final int nestedLevel;
    private boolean rootIsPointer;
    private int rootLevel;
Anders Nilsson's avatar
Anders Nilsson committed
49

50
51
52
53
    public boolean versionHasMetaData() {
      return version != 2006;
    }

Anders Nilsson's avatar
Anders Nilsson committed
54
    private C_env(String qualid, String lcName, String rawPrefix, 
Tommy Olofsson's avatar
Tommy Olofsson committed
55
56
57
		  int indent, int depth, C_printer printer,
                  int nestedLevel, int version)
    {
58
      this.version = version;
59
      this.verStr = LabCommVersion.versionString(version);
Anders Nilsson's avatar
Anders Nilsson committed
60
61
62
      this.qualid = qualid;
      this.lcName = lcName;
      this.rawPrefix = rawPrefix;
63
64
65
66
      if (rawPrefix == null) {
        System.err.println("WARNING: prefix==null");
        this.prefix = ""; 
      } else if (rawPrefix.equals("")) {
67
68
69
70
        this.prefix = rawPrefix;
      } else {
        this.prefix = rawPrefix + "_";
      }
Anders Nilsson's avatar
Anders Nilsson committed
71
72
73
      this.indent = indent;
      this.depth = depth;
      this.printer = printer;
74
      this.nestedLevel = nestedLevel;
Anders Nilsson's avatar
Anders Nilsson committed
75
76
    }

Tommy Olofsson's avatar
Tommy Olofsson committed
77
78
79
80
    public C_env(String qualid, String lcName, String rawPrefix,
		 PrintStream out, int version)
    {
      this(qualid, lcName, rawPrefix, 0, 0, new C_printer(out), 0, version);
81
82
83
    }

    public C_env(String qualid, String lcName, String rawPrefix,
Tommy Olofsson's avatar
Tommy Olofsson committed
84
85
		 int indent, int depth, C_printer printer, int version) {
            this(qualid, lcName, rawPrefix, indent, depth, printer, 0, version);
Anders Nilsson's avatar
Anders Nilsson committed
86
87
88
    }

    public C_env nestArray(String suffix) {
Tommy Olofsson's avatar
Tommy Olofsson committed
89
90
      return new C_env(qualid + suffix, lcName, rawPrefix,
		       indent, depth + 1, printer, nestedLevel + 1, version);
Anders Nilsson's avatar
Anders Nilsson committed
91
92
93
94
    }

    public C_env nestStruct(String suffix) {
      return new C_env(qualid + suffix, lcName, rawPrefix, 
Tommy Olofsson's avatar
Tommy Olofsson committed
95
		       indent, depth, printer, nestedLevel + 1, version);
Anders Nilsson's avatar
Anders Nilsson committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
    }

    public void indent() {
      indent++;
    }

    public void unindent() {
      indent--;
    }

    public String prefix() {
      return rawPrefix;
    }

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

114
115
116
117
    public void println() {
      printer.println(this, "");
    }

Anders Nilsson's avatar
Anders Nilsson committed
118
119
120
121
    public void println(String s) {
      printer.println(this, s);
    }

122
123
124
125
126
127
    public C_env setPointer() {
      rootIsPointer = true;
      rootLevel = nestedLevel;
      return this;
    }

Tommy Olofsson's avatar
Tommy Olofsson committed
128
    public String memberAccessor() {
129
130
      return (rootIsPointer && (nestedLevel == rootLevel)) ? "->" : ".";
    }
Tommy Olofsson's avatar
Tommy Olofsson committed
131
132
133
134

    public String accessor() {
      return (rootIsPointer && (nestedLevel == rootLevel)) ? "*" : "";
    }
Anders Nilsson's avatar
Anders Nilsson committed
135
136
137
138
139
140
141
142
143
  }

  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) {
Tommy Olofsson's avatar
Tommy Olofsson committed
144
    String index = env.memberAccessor() + "a";
Anders Nilsson's avatar
Anders Nilsson committed
145
146
147
148
149
150
151
    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) {
Tommy Olofsson's avatar
Tommy Olofsson committed
152
    return env.nestArray(env.memberAccessor() + "a[i_" + env.depth + "]");
Anders Nilsson's avatar
Anders Nilsson committed
153
154
155
156
157
158
159
160
  }


}

aspect C_IsDynamic {
  
  // Determine if a type has dynamically allocated data
161
162
  syn boolean Decl.C_isDynamic() = getDataType().C_isDynamic();
  syn boolean DataType.C_isDynamic() = false;
Anders Nilsson's avatar
Anders Nilsson committed
163
164
  syn boolean PrimType.C_isDynamic() = getToken() == LABCOMM_STRING;
  syn boolean UserType.C_isDynamic() = 
165
    lookupType(getName()).getDataType().C_isDynamic();
Anders Nilsson's avatar
Anders Nilsson committed
166
167
  syn boolean StructType.C_isDynamic() {
    for (int i = 0 ; i < getNumField() ; i++) {
168
      if (getField(i).getDataType().C_isDynamic()) {
Anders Nilsson's avatar
Anders Nilsson committed
169
170
171
172
173
	return true;
      }
    }
    return false;
  }
174
  syn boolean FixedArrayType.C_isDynamic() = getDataType().C_isDynamic();
Anders Nilsson's avatar
Anders Nilsson committed
175
176
177
178
179
  syn boolean VariableArrayType.C_isDynamic() = true;
}

aspect C_CodeGen {

180
  public void Specification.C_genH(PrintStream out, Vector includes, 
181
182
			     String lcName, String prefix, int version) {
    C_env env = new C_env("", lcName, prefix, out, version);
Anders Nilsson's avatar
Anders Nilsson committed
183
184
185
186
187
188
189
190
191
192
193
194

    // 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
195
    env.println("#include \"labcomm2014.h\"");
Anders Nilsson's avatar
Anders Nilsson committed
196
197
198
199
200
201
202
203
204
205
    for (int i = 0 ; i < includes.size() ; i++) {
      env.println("#include \"" + includes.get(i) + "\"");
    }
    env.println("");

    C_emitH(env);

    env.println("#endif");
  }

206
  public void Specification.C_genC(PrintStream out, Vector includes, 
207
208
			     String lcName, String prefix, int version) {
    C_env env = new C_env("", lcName, prefix, out, version);
Anders Nilsson's avatar
Anders Nilsson committed
209
210

    // Include
211
212
    env.println("#include \"labcomm2014.h\"");
    env.println("#include \"labcomm2014_private.h\"");
Anders Nilsson's avatar
Anders Nilsson committed
213
214
215
216
217
218
219
220
221
    for (int i = 0 ; i < includes.size() ; i++) {
      env.println("#include \"" + includes.get(i) + "\"");
    }
    env.println("");
    
    // Method Implementations
    C_emitC(env);
  }

222
  public void Specification.C_emitH(C_env env) {
Anders Nilsson's avatar
Anders Nilsson committed
223
224
    for (int i = 0; i < getNumDecl(); i++) {
      getDecl(i).C_emitType(env);
225
      getDecl(i).C_emitDecoderDeclaration(env);
Anders Nilsson's avatar
Anders Nilsson committed
226
227
      getDecl(i).C_emitEncoderDeclaration(env);
      getDecl(i).C_emitSizeofDeclaration(env);
Tommy Olofsson's avatar
Tommy Olofsson committed
228
      getDecl(i).C_emitCopyDeclaration(env);
229
      getDecl(i).C_emitCopyDeallocationDeclaration(env);
Anders Nilsson's avatar
Anders Nilsson committed
230
231
      env.println("");
    }
Anders Blomdell's avatar
Anders Blomdell committed
232
    C_emitConstructorDeclaration(env);
Anders Nilsson's avatar
Anders Nilsson committed
233
234
235
    C_emitForAll(env);
  }

236
  public void Specification.C_emitC(C_env env) {
Anders Nilsson's avatar
Anders Nilsson committed
237
    for (int i = 0; i < getNumDecl(); i++) {
238
      getDecl(i).C_emitSignature(env);
Anders Nilsson's avatar
Anders Nilsson committed
239
240
      getDecl(i).C_emitDecoder(env);
      getDecl(i).C_emitDecoderRegisterHandler(env);
241
      getDecl(i).C_emitDecoderIoctl(env);
Anders Nilsson's avatar
Anders Nilsson committed
242
243
      getDecl(i).C_emitEncoder(env);
      getDecl(i).C_emitEncoderRegisterHandler(env);
244
      getDecl(i).C_emitEncoderIoctl(env);
Anders Nilsson's avatar
Anders Nilsson committed
245
      getDecl(i).C_emitSizeof(env);
246
      getDecl(i).C_emitCopy(env);
247
      getDecl(i).C_emitCopyDeallocation(env);
Anders Nilsson's avatar
Anders Nilsson committed
248
    }
249
    C_emitConstructor(env);
Anders Nilsson's avatar
Anders Nilsson committed
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
  }
 
}

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) {
273
    env.println("#ifndef LABCOMM_DEFINED_" + env.prefix + getName());
Anders Nilsson's avatar
Anders Nilsson committed
274
    env.print("typedef ");
275
    getDataType().C_emitType(env, env.prefix + getName());
Anders Nilsson's avatar
Anders Nilsson committed
276
    env.println(";");
277
    env.println("#define LABCOMM_DEFINED_" + env.prefix + getName());
Anders Nilsson's avatar
Anders Nilsson committed
278
279
280
281
    env.println("#endif");
  }

  public void SampleDecl.C_emitType(C_env env) {
282
    env.println("#ifndef LABCOMM_DEFINED_" + env.prefix + getName());
Anders Nilsson's avatar
Anders Nilsson committed
283
    env.print("typedef ");
284
    getDataType().C_emitType(env, env.prefix + getName());
Anders Nilsson's avatar
Anders Nilsson committed
285
    env.println(";");
286
    env.println("#define LABCOMM_DEFINED_" + env.prefix + getName());
Anders Nilsson's avatar
Anders Nilsson committed
287
    env.println("#endif");
288
289
    env.println("extern const struct labcomm2014_signature " +
                "*labcomm2014_signature_" + env.prefix + getName() + 
290
                ";");
Anders Nilsson's avatar
Anders Nilsson committed
291
292
  }

293
  public void DataType.C_emitType(C_env env, String name) {
Anders Nilsson's avatar
Anders Nilsson committed
294
295
296
297
298
299
300
301
302
303
304
    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()) {
305
306
307
308
309
310
311
      case LABCOMM_BOOLEAN: { env.print("uint8_t"); } break;
      case LABCOMM_BYTE: { env.print("uint8_t"); } break;
      case LABCOMM_SHORT: { env.print("int16_t"); } break;
      case LABCOMM_INT: { env.print("int32_t"); } break;
      case LABCOMM_LONG: { env.print("int64_t"); } break;
      case LABCOMM_FLOAT: { env.print("float"); } break;
      case LABCOMM_DOUBLE: { env.print("double"); } break;
Anders Nilsson's avatar
Anders Nilsson committed
312
      case LABCOMM_STRING: { env.print("char*"); } break;
313
      case LABCOMM_SAMPLE: { 
314
        env.print("const struct labcomm2014_signature *"); 
315
      } break;
Anders Nilsson's avatar
Anders Nilsson committed
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
    }
    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) {
336
    getDataType().C_emitType(env, getName());
Anders Nilsson's avatar
Anders Nilsson committed
337
338
339
340
341
342
343
344
345
  }

  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() + "]");
    }
346
    getDataType().C_emitType(env, index.toString());
Anders Nilsson's avatar
Anders Nilsson committed
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
    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());
      }
    }
362
    getDataType().C_emitType(env, "*a");
Anders Nilsson's avatar
Anders Nilsson committed
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
    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 {

382
  public void Decl.C_emitDecoderDeclaration(C_env env) {
Anders Nilsson's avatar
Anders Nilsson committed
383
384
  }

385
  public void SampleDecl.C_emitDecoderDeclaration(C_env env) {
386
    env.println("int labcomm2014_decoder_register_" + 
Anders Nilsson's avatar
Anders Nilsson committed
387
388
		env.prefix + getName() + "(");
    env.indent();
389
    env.println("struct labcomm2014_decoder *d,");
Anders Nilsson's avatar
Anders Nilsson committed
390
391
392
393
394
395
396
397
398
    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(");");
399

400
    env.println("int labcomm2014_decoder_ioctl_" + env.prefix + getName() + "(");
401
    env.indent();
402
    env.println("struct labcomm2014_decoder *d,");
403
404
405
406
    env.println("int ioctl_action,");
    env.println("...");
    env.unindent();
    env.println(");");
Anders Nilsson's avatar
Anders Nilsson committed
407
408
409
410
  }
  
  public void Decl.C_emitEncoderDeclaration(C_env env) {
  }
411

Anders Nilsson's avatar
Anders Nilsson committed
412
  public void SampleDecl.C_emitEncoderDeclaration(C_env env) {
413
    env.println("int labcomm2014_encoder_register_" + 
Anders Nilsson's avatar
Anders Nilsson committed
414
415
		env.prefix + getName() + "(");
    env.indent();
416
    env.println("struct labcomm2014_encoder *e);");
Anders Nilsson's avatar
Anders Nilsson committed
417
418
    env.unindent();

419
    env.println("int labcomm2014_encode_" + env.prefix + getName() + "(");
Anders Nilsson's avatar
Anders Nilsson committed
420
    env.indent();
421
    env.println("struct labcomm2014_encoder *e");
422
423
424
    if(!isVoid() ) {
        env.println(", "+env.prefix + getName() + " *v");
    }
Anders Nilsson's avatar
Anders Nilsson committed
425
426
    env.unindent();
    env.println(");");
427

428
    env.println("int labcomm2014_encoder_ioctl_" + env.prefix + getName() + "(");
429
    env.indent();
430
    env.println("struct labcomm2014_encoder *e,");
431
432
433
434
    env.println("int ioctl_action,");
    env.println("...");
    env.unindent();
    env.println(");");
Anders Nilsson's avatar
Anders Nilsson committed
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
  }

}

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) {
Tommy Olofsson's avatar
Tommy Olofsson committed
452
    return env.qualid + env.memberAccessor() + "n_" + i;
Anders Nilsson's avatar
Anders Nilsson committed
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
  }
  
}

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");
490
    env.println("static void decode_" + env.prefix + getName() + "(");
Anders Nilsson's avatar
Anders Nilsson committed
491
    env.indent();
492
    env.println("struct labcomm2014_reader *r,");
Anders Nilsson's avatar
Anders Nilsson committed
493
494
495
496
497
498
499
500
501
502
503
504
    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;");
505
    getDataType().C_emitDecoder(env);
Anders Nilsson's avatar
Anders Nilsson committed
506
507
508
509
    env.println("handle(&v, context);");
    if (C_isDynamic()) {
      env.println("{");
      env.indent();
510
      getDataType().C_emitDecoderDeallocation(env);
Anders Nilsson's avatar
Anders Nilsson committed
511
512
513
514
515
516
517
      env.unindent();
      env.println("}");
    }
    env.unindent();
    env.println("}");
  }

518
  public void DataType.C_emitDecoder(C_env env) {
Anders Nilsson's avatar
Anders Nilsson committed
519
520
521
522
523
524
525
526
527
    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) {
528
529
530
    env.print(env.qualid + " = ");
    switch (getToken()) {
      case LABCOMM_SAMPLE: { 
531
532
        env.println("labcomm2014_internal_decoder_index_to_signature(" +
                    "r->decoder, labcomm2014_read_int(r));");
533
534
      } break;
      default: {
535
        env.println("labcomm2014_read_" + getName() + "(r);");
536
537
      }; break;
    }
Anders Nilsson's avatar
Anders Nilsson committed
538
539
540
  }

  public void UserType.C_emitDecoder(C_env env) {
541
    lookupType(getName()).getDataType().C_emitDecoder(env);
Anders Nilsson's avatar
Anders Nilsson committed
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
  }

  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);
566
    getDataType().C_emitDecoder(C_Nest(env));
Anders Nilsson's avatar
Anders Nilsson committed
567
568
569
570
571
572
573
574
575
    for (int i = getNumExp() - 1 ; i >= 0 ; i--) {
      env.unindent();
      env.println("}");
    }
    env.unindent();
    env.println("}");
  }

  public void Field.C_emitDecoder(C_env env) {
576
    getDataType().C_emitDecoder(env.nestStruct("." + getName()));
Anders Nilsson's avatar
Anders Nilsson committed
577
578
579
580
581
582
  }

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

  public void VariableSize.C_emitDecoderDecodeLimit(C_env env, int i) {
583
    env.println(env.qualid + ".n_" + i + " = labcomm2014_read_packed32(r);");
Anders Nilsson's avatar
Anders Nilsson committed
584
585
586
587
588
589
590
591
592
593
594
595
  }

  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) {
596
    env.print(env.qualid + 
597
              ".a = labcomm2014_memory_alloc(r->memory, 1, sizeof(" + 
598
	      env.qualid + ".a[0])");
Anders Nilsson's avatar
Anders Nilsson committed
599
600
601
602
603
604
605
606
    for (int i = 0 ; i < getNumExp() ; i++) {
      env.print(" * " + getExp(i).C_getLimit(env, i));
    }
    env.println(");");
  }

  // Code for deallocation of dynamically allocated data 

607
  public void DataType.C_emitDecoderDeallocation(C_env env) {
Anders Nilsson's avatar
Anders Nilsson committed
608
609
610
611
612
613
614
    throw new Error(this.getClass().getName() + 
		    ".C_emitDecoderDeallocation(C_env env)" + 
		    " not declared");
  }

  public void PrimType.C_emitDecoderDeallocation(C_env env) {
    if (C_isDynamic()) {
615
      env.println("labcomm2014_memory_free(r->memory, 1, " + 
616
                  env.qualid + ");");
Anders Nilsson's avatar
Anders Nilsson committed
617
618
619
620
621
    }
  }

  public void UserType.C_emitDecoderDeallocation(C_env env) {
    if (C_isDynamic()) {
622
      lookupType(getName()).getDataType().C_emitDecoderDeallocation(env);
Anders Nilsson's avatar
Anders Nilsson committed
623
624
625
626
627
628
629
630
631
632
633
634
    }
  }

  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) {
635
    if (getDataType().C_isDynamic()) {
Anders Nilsson's avatar
Anders Nilsson committed
636
637
638
639
640
641
642
643
644
645
646
647
648
      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);
649
      getDataType().C_emitDecoderDeallocation(C_Nest(env));
Anders Nilsson's avatar
Anders Nilsson committed
650
651
652
653
654
655
656
657
658
659
660
      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);
661
    env.println("labcomm2014_memory_free(r->memory, 1, " + 
662
                env.qualid + ".a);");
Anders Nilsson's avatar
Anders Nilsson committed
663
664
665
  }

  public void Field.C_emitDecoderDeallocation(C_env env) {
666
    getDataType().C_emitDecoderDeallocation(env.nestStruct("." + getName()));
Anders Nilsson's avatar
Anders Nilsson committed
667
668
669
670
671
672
673
674
675
676
677
678
  }

  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) {
679
    env.println("int labcomm2014_decoder_register_" + 
Anders Nilsson's avatar
Anders Nilsson committed
680
681
		env.prefix + getName() + "(");
    env.indent();
682
    env.println("struct labcomm2014_decoder *d,");
Anders Nilsson's avatar
Anders Nilsson committed
683
684
685
686
687
688
689
690
691
692
693
    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();
694
    env.println("return labcomm2014_internal_decoder_register(");
Anders Nilsson's avatar
Anders Nilsson committed
695
696
    env.indent();
    env.println("d,");
697
    env.println("&signature_" + env.prefix + getName() + ",");
698
699
    env.println("(labcomm2014_decoder_function)decode_" + env.prefix + getName() + ",");
    env.println("(labcomm2014_handler_function)handler,");
Anders Nilsson's avatar
Anders Nilsson committed
700
701
702
703
704
705
706
707
708
    env.println("context");
    env.unindent();
    env.println(");");
    env.unindent();
    env.println("}");
  }

}

709
710
aspect C_copy {

Tommy Olofsson's avatar
Tommy Olofsson committed
711
  private void SampleDecl.C_emitCopyFunctionParam(C_env env, String src,
Tommy Olofsson's avatar
Tommy Olofsson committed
712
713
						  String dst)
  {
714
    env.println("void labcomm2014_copy_" +
Tommy Olofsson's avatar
Tommy Olofsson committed
715
716
                env.prefix + getName() + "(");
    env.indent();
717
    env.println("struct labcomm2014_memory *mem,");
Tommy Olofsson's avatar
Tommy Olofsson committed
718
719
720
721
    env.println(env.prefix + getName() + " *" + dst + ",");
    env.println(env.prefix + getName() + " *" + src);
    env.unindent();
    env.print(")");
Tommy Olofsson's avatar
Tommy Olofsson committed
722
723
724
725
726
727
728
729
730
731
  }

  public void Decl.C_emitCopyDeclaration(C_env env) {
  }

  public void SampleDecl.C_emitCopyDeclaration(C_env env) {
    C_emitCopyFunctionParam(env, "src", "dst");
    env.println(";");
  }

732
733
734
735
736
737
738
739
740
741
742
743
744
745
  public void Decl.C_emitCopy(C_env env) {
    throw new Error(this.getClass().getName() + 
		    ".C_emitCopy(C_env env)" + 
		    " not declared");
  }

  public void TypeDecl.C_emitCopy(C_env env) {
  }

  public void SampleDecl.C_emitCopy(C_env env) {
    final String dst = "dst";
    final String src = "src";
    C_env env_src = env.nestStruct(src).setPointer();
    C_env env_dst = env.nestStruct(dst).setPointer();
Tommy Olofsson's avatar
Tommy Olofsson committed
746
747
748

    C_emitCopyFunctionParam(env_src, src, dst);
    env_src.println("");
749
750
    env_src.println("{");
    env_src.indent();
751
    getDataType().C_emitCopy(env_src, env_dst);
752
753
754
755
    env_src.unindent();
    env_src.println("}");
  }

756
  public void DataType.C_emitCopy(C_env env_src, C_env env_dst) {
757
758
759
760
761
762
763
764
765
    throw new Error(this.getClass().getName() + 
		    ".C_emitCopy(C_env env)" + 
		    " not declared");
  }

  public void VoidType.C_emitCopy(C_env env_src, C_env env_dst) {
  }

  public void PrimType.C_emitCopy(C_env env_src, C_env env_dst) {
766
767
    if (C_isDynamic()) {
      env_src.println(String.format(
768
	  "%s%s = labcomm2014_memory_alloc(mem, 1, strlen(%s%s)+1);",
769
770
771
772
773
774
775
776
777
778
779
	  env_dst.accessor(), env_dst.qualid,
	  env_src.accessor(), env_src.qualid));
      env_src.println(String.format(
	  "memcpy(%s%s, %s%s, strlen(%s%s)+1);",
	  env_dst.accessor(), env_dst.qualid,
	  env_src.accessor(), env_src.qualid,
	  env_src.accessor(), env_src.qualid));
    } else {
      env_src.println(env_dst.accessor() + env_dst.qualid + " = " +
		      env_src.accessor() + env_src.qualid + ";");
    }
780
781
782
  }

  public void UserType.C_emitCopy(C_env env_src, C_env env_dst) {
783
    lookupType(getName()).getDataType().C_emitCopy(env_src, env_dst);
784
785
786
787
788
789
790
791
792
  }

  public void StructType.C_emitCopy(C_env env_src, C_env env_dst) {
    for (int i = 0 ; i < getNumField() ; i++) {
      getField(i).C_emitCopy(env_src, env_dst);
    }
  }

  public void ArrayType.C_emitCopy(C_env env_src, C_env env_dst) {
Tommy Olofsson's avatar
Tommy Olofsson committed
793
    C_emitCopyDecodeLimit(env_src, env_dst);
794
795
796
797
798
799
800
801
802
803
804
805
806
807
    C_emitCopyArrayAllocate(env_src, env_dst);
    env_src.println("{");
    env_src.indent();
    C_emitLoopVariables(env_src);
    for (int i = 0 ; i < getNumExp() ; i++) {
      String iterator = "i_" + env_src.depth + "_" + i;
      env_src.println("for (" + iterator + " = 0" +
		  " ; " +
		  iterator + " < " + getExp(i).C_getLimit(env_src, i) +
		  " ; " +
		  iterator + "++) {");
      env_src.indent();
    }
    C_emitCalcIndex(env_src);
808
    getDataType().C_emitCopy(C_Nest(env_src), C_Nest(env_dst));
809
810
811
812
813
814
815
816
817
    for (int i = getNumExp() - 1 ; i >= 0 ; i--) {
      env_src.unindent();
      env_src.println("}");
    }
    env_src.unindent();
    env_src.println("}");
  }

  public void Field.C_emitCopy(C_env env_src, C_env env_dst) {
Tommy Olofsson's avatar
Tommy Olofsson committed
818
    String fnam = env_src.memberAccessor() + getName();
819
    getDataType().C_emitCopy(env_src.nestStruct(fnam), env_dst.nestStruct(fnam));
820
821
  }

Tommy Olofsson's avatar
Tommy Olofsson committed
822
823
  public void Exp.C_emitCopyDecodeLimit(C_env env_src, C_env env_dst, int i) {
    // Ordinary array has no length-member.
824
825
  }

Tommy Olofsson's avatar
Tommy Olofsson committed
826
827
828
829
  public void VariableSize.C_emitCopyDecodeLimit(C_env env_src, C_env env_dst, int i) {
    String src = env_src.qualid + env_src.memberAccessor() + "n_" + i;
    String dst = env_dst.qualid + env_dst.memberAccessor() + "n_" + i;
    env_src.println(dst + " = " + src + ";");
830
831
  }

Tommy Olofsson's avatar
Tommy Olofsson committed
832
  public void ArrayType.C_emitCopyDecodeLimit(C_env env_src, C_env env_dst) {
833
    for (int i = 0 ; i < getNumExp() ; i++) {
Tommy Olofsson's avatar
Tommy Olofsson committed
834
      getExp(i).C_emitCopyDecodeLimit(env_src, env_dst, i);
835
836
837
838
839
840
841
842
843
    }
  }

  public void ArrayType.C_emitCopyArrayAllocate(C_env env_src, C_env env_dst) {
  }

  public void VariableArrayType.C_emitCopyArrayAllocate(C_env env_src,
							C_env env_dst)
  {
Tommy Olofsson's avatar
Tommy Olofsson committed
844
    env_src.print(env_dst.qualid + env_dst.memberAccessor() +
845
                  "a = labcomm2014_memory_alloc(mem, 1, sizeof(" +
Tommy Olofsson's avatar
Tommy Olofsson committed
846
		  env_src.qualid + env_src.memberAccessor() + "a[0])");
847
848
849
850
851
852
    for (int i = 0 ; i < getNumExp() ; i++) {
      env_src.print(" * " + getExp(i).C_getLimit(env_src, i));
    }
    env_dst.println(");");
  }

853
854
855
856
857
  // Code for deallocation of dynamically allocated data in a copy.

  private void SampleDecl.C_emitCopyDeallocationFunctionParam(C_env env,
							      String par)
  {
858
    env.println("void labcomm2014_copy_free_" +
Tommy Olofsson's avatar
Tommy Olofsson committed
859
                env.prefix + getName() + "(");
860
    env.indent();
861
    env.println("struct labcomm2014_memory *mem,");
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
    env.println(env.prefix + getName() + " *" + par);
    env.unindent();
    env.print(")");
  }

  public void Decl.C_emitCopyDeallocationDeclaration(C_env env) {
  }

  public void SampleDecl.C_emitCopyDeallocationDeclaration(C_env env) {
    C_emitCopyDeallocationFunctionParam(env, "c");
    env.println(";");
  }

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

  public void TypeDecl.C_emitCopyDeallocation(C_env env) {
  }

  public void SampleDecl.C_emitCopyDeallocation(C_env env) {
    String par = "par";
    env = env.nestStruct(par).setPointer();

    C_emitCopyDeallocationFunctionParam(env, par);
    env.println("");
    env.println("{");
    env.indent();
892
    getDataType().C_emitCopyDeallocation(env);
893
894
895
    env.unindent();
    env.println("}");
  }
896

897
  public void DataType.C_emitCopyDeallocation(C_env env) {
898
899
    throw new Error(this.getClass().getName() +
  		    ".C_emitCopyDeallocation(C_env env)" +
900
901
902
  		    " not declared");
  }

903
904
905
  public void VoidType.C_emitCopyDeallocation(C_env env) {
  }

906
907
  public void PrimType.C_emitCopyDeallocation(C_env env) {
    if (C_isDynamic()) {
908
      env.println("labcomm2014_memory_free(mem, 1, " +
909
                  env.accessor() + env.qualid + ");");
910
911
912
913
914
    }
  }

  public void UserType.C_emitCopyDeallocation(C_env env) {
    if (C_isDynamic()) {
915
      lookupType(getName()).getDataType().C_emitCopyDeallocation(env);
916
917
918
919
920
921
922
923
924
925
926
927
    }
  }

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

  public void ArrayType.C_emitCopyDeallocation(C_env env) {
928
    if (getDataType().C_isDynamic()) {
929
930
931
932
933
934
935
936
937
938
939
940
941
      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);
942
      getDataType().C_emitCopyDeallocation(C_Nest(env));
943
944
945
946
947
948
949
950
951
952
953
      for (int i = 0 ; i < getNumExp() ; i++) {
  	env.unindent();
  	env.println("}");
      }
      env.unindent();
      env.println("}");
    }
  }

  public void VariableArrayType.C_emitCopyDeallocation(C_env env) {
    super.C_emitCopyDeallocation(env);
954
    env.println("labcomm2014_memory_free(mem, 1, " +
955
                env.qualid + env.memberAccessor() + "a);");
956
957
958
  }

  public void Field.C_emitCopyDeallocation(C_env env) {
959
    getDataType().C_emitCopyDeallocation(env.nestStruct(env.memberAccessor()
960
						    + getName()));
961
962
963
  }
}

964
965
966
967
968
969
970
971
972
973
974
975
aspect C_DecoderIoctl {

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

  public void TypeDecl.C_emitDecoderIoctl(C_env env) {
  }

  public void SampleDecl.C_emitDecoderIoctl(C_env env) {
976
    env.println("int labcomm2014_decoder_ioctl_" + env.prefix + getName() + "(");
977
    env.indent();
978
    env.println("struct labcomm2014_decoder *d,");
979
980
981
982
983
984
985
986
987
    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);");
988
    env.println("result = labcomm2014_internal_decoder_ioctl(");
989
    env.indent();
990
    env.println("d, &signature_" + env.prefix + getName() + ", ");
991
    env.println("ioctl_action, va);");
992
993
994
995
996
997
998
999
    env.unindent();
    env.println("va_end(va);");
    env.println("return result;");
    env.unindent();
    env.println("}");
  }
}

Anders Nilsson's avatar
Anders Nilsson committed
1000

For faster browsing, not all history is shown. View entire blame