summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/symtab/classfile/Pickler.scala
blob: a60333f005fd7e052debf3ca8117ced25e182d93 (plain) (blame)
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
/* NSC -- new Scala compiler
 * Copyright 2005-2007 LAMP/EPFL
 * @author  Martin Odersky
 */
// $Id$

package scala.tools.nsc.symtab.classfile

import java.lang.{Float, Double}
import scala.tools.nsc.util.{Position, NoPosition, ShowPickled}
import Flags._
import PickleFormat._

/**
 * Serialize a top-level module and/or class.
 *
 * @see <code>EntryTags.scala</code> for symbol table attribute format.
 *
 * @author Martin Odersky
 * @version 1.0
 */
abstract class Pickler extends SubComponent {
  import global._

  val phaseName = "pickler"

  def newPhase(prev: Phase): StdPhase = new PicklePhase(prev)

  class PicklePhase(prev: Phase) extends StdPhase(prev) {
    def apply(unit: CompilationUnit) {
      def pickle(tree: Tree) {

        def add(sym: Symbol, pickle: Pickle) = {
          if (currentRun.compiles(sym) && !currentRun.symData.contains(sym)) {
            if (settings.debug.value) log("pickling " + sym)
            pickle.putSymbol(sym)
            currentRun.symData(sym) = pickle
          }
        }

        tree match {
          case PackageDef(_, stats) =>
            stats foreach pickle
          case ClassDef(_, _, _, _) | ModuleDef(_, _, _) =>
            val sym = tree.symbol
            val pickle = new Pickle(sym.name.toTermName, sym.owner)
            add(sym, pickle)
            add(sym.linkedSym, pickle)
            pickle.finish
            val doPickleHash = global.doPickleHash
            if (doPickleHash) {
              var i = 0
              while (i < pickle.writeIndex) {
                unit.pickleHash += pickle.bytes(i).toLong // toLong needed to work around bug
                i = i + 1
              }
            }
          case _ =>
        }
      }
      pickle(unit.body)
    }
  }

  private class Pickle(rootName: Name, rootOwner: Symbol)
        extends PickleBuffer(new Array[Byte](4096), -1, 0) {
    import scala.collection.jcl.LinkedHashMap
    private var entries = new Array[AnyRef](256)
    private var ep = 0
    private val index = new LinkedHashMap[AnyRef, int]

    /** Is root in symbol.owner*?
     *
     *  @param sym ...
     *  @return    ...
     */
    private def isLocal(sym: Symbol): Boolean = (
      sym.isRefinementClass ||
      sym.name.toTermName == rootName && sym.owner == rootOwner ||
      sym != NoSymbol && isLocal(sym.owner)
    )

    // Phase 1 methods: Populate entries/index ------------------------------------

    /** Store entry <code>e</code> in index at next available position unless
     *  it is already there.
     *
     *  @param entry ...
     *  @return      <code>true</code> iff entry is new.
     */
    private def putEntry(entry: AnyRef): Boolean = index.get(entry) match {
      case Some(_) => false
      case None =>
        if (ep == entries.length) {
          val entries1 = new Array[AnyRef](ep * 2)
          Array.copy(entries, 0, entries1, 0, ep)
          entries = entries1
        }
        entries(ep) = entry
        index(entry) = ep
        ep = ep + 1
        true
    }

    /** Store symbol in <code>index</code>. If symbol is local, also store
     * everything it refers to.
     *
     *  @param sym ...
     */
    def putSymbol(sym: Symbol) {
      if (putEntry(sym)) {
        if (isLocal(sym)) {
          putEntry(sym.name)
          putSymbol(sym.owner)
          putSymbol(sym.privateWithin)
          putType(sym.info)
          if (sym.thisSym.tpeHK != sym.tpeHK)
            putType(sym.typeOfThis);
          putSymbol(sym.alias)
          if (!sym.children.isEmpty) {
            val (locals, globals) = sym.children.toList.partition(_.isLocalClass)
            val children =
              if (locals.isEmpty) globals
              else {
                val localChildDummy = sym.newClass(sym.pos, nme.LOCALCHILD)
                localChildDummy.setInfo(ClassInfoType(List(sym.tpe), EmptyScope, localChildDummy))
                localChildDummy :: globals
              }
            putChildren(sym, children.sort((x, y) => x isLess y))
          }
          for (attr <- sym.attributes.reverse) {
            if (attr.atp.typeSymbol isNonBottomSubClass definitions.StaticAnnotationClass)
              putAnnotation(sym, attr)
          }
        } else if (sym != NoSymbol) {
          putEntry(if (sym.isModuleClass) sym.name.toTermName else sym.name)
          if (!sym.owner.isRoot) putSymbol(sym.owner)
        }
      }
    }

    private def putSymbols(syms: List[Symbol]) =
      syms foreach putSymbol

    /** Store type and everythig it refers to in map <code>index</code>.
     *
     *  @param tp ...
     */
    private def putType(tp: Type): Unit = if (putEntry(tp)) {
      tp match {
        case NoType | NoPrefix | DeBruijnIndex(_, _) =>
          ;
        case ThisType(sym) =>
          putSymbol(sym)
        case SingleType(pre, sym) =>
          putType(pre); putSymbol(sym)
        case ConstantType(value) =>
          putConstant(value)
        case TypeRef(pre, sym, args) =>
          putType(pre); putSymbol(sym); putTypes(args)
        case TypeBounds(lo, hi) =>
          putType(lo); putType(hi)
        case RefinedType(parents, decls) =>
          putSymbol(tp.typeSymbol); putTypes(parents); putSymbols(decls.toList)
        case ClassInfoType(parents, decls, clazz) =>
          putSymbol(clazz); putTypes(parents); putSymbols(decls.toList)
        case MethodType(formals, restpe) =>
          putType(restpe); putTypes(formals)
        case PolyType(tparams, restpe) =>
          putType(restpe); putSymbols(tparams)
        case ExistentialType(tparams, restpe) =>
          putType(restpe); putSymbols(tparams)
        case AnnotatedType(attribs, tp) =>
          putType(tp); putAnnotations(attribs)
        case _ =>
          throw new FatalError("bad type: " + tp + "(" + tp.getClass + ")")
      }
    }
    private def putTypes(tps: List[Type]) { tps foreach putType }

    private def putTree(tree: reflect.Tree): Unit = if (putEntry(tree)) {
      tree match {
        case reflect.Ident(sym) => putSymbol(sym)
        case reflect.Select(qual, sym) =>  putTree(qual); putSymbol(sym)
        case reflect.Literal(value) => putConstant(Constant(value))
        case reflect.Apply(fun, args) => putTree(fun); putRefTrees(args)
        case reflect.TypeApply(fun, args) =>  putTree(fun); putRefTypes(args)
        case reflect.Function(params, body) =>
          putRefSymbols(params); putTree(body)
        case reflect.This(sym) =>  putSymbol(sym)
        case reflect.Block(stats, expr) => putRefTrees(stats); putTree(expr)
        case reflect.New(clz) => putTree(clz)
        case reflect.If(condition, trueCase, falseCase) =>
          putTree(condition); putTree(trueCase); putTree(falseCase)
        case reflect.Assign(destination, source) =>
          putTree(destination); putTree(source)
        case reflect.Target(sym, body) => putSymbol(sym); putTree(body)
        case reflect.Goto(target) => putSymbol(target)
        case reflect.ValDef(sym, rhs)  => putSymbol(sym); putTree(rhs)
        case reflect.ClassDef(sym, tpe, impl) =>
          putSymbol(sym); putType(tpe); putTree(impl)
        case reflect.DefDef(sym, vparamss, ret, rhs) =>
          putSymbol(sym); putRefTreess(vparamss); putType(ret); putTree(rhs)
        case reflect.Super(psym) => putSymbol(psym)
        case reflect.Template(parents, body) =>
          putRefTypes(parents); putRefTrees(body)
        case _ =>
          throw new FatalError("bad tree: " + tree + "(" + tree.getClass + ")")
      }
    }
    private def putRefTrees(trees: List[reflect.Tree]) = trees foreach putTree
    private def putRefTreess(trees: List[List[reflect.Tree]]) =
      trees foreach putRefTrees

    private def putType(tpe: reflect.Type): Unit = if (putEntry(tpe)) {
      tpe match {
        case reflect.NoPrefix => ()
        case reflect.NoType => ()
        case reflect.NamedType(fullname) => putConstant(Constant(fullname))
        case reflect.PrefixedType(pre, sym) => putType(pre); putSymbol(sym)
        case reflect.SingleType(pre, sym) => putType(pre); putSymbol(sym)
        case reflect.ThisType(clazz) => putSymbol(clazz)
        case reflect.AppliedType(tpe, args) => putType(tpe); putRefTypes(args)
        case reflect.TypeBounds(lo, hi) => putType(lo); putType(hi)
        case reflect.MethodType(formals, restpe) => //can be implicit
          putRefTypes(formals); putType(restpe)
        case reflect.PolyType(typeParams, typeBounds, resultType) =>
          putRefSymbols(typeParams)
          for ((t1,t2) <- typeBounds) {
            putType(t1)
            putType(t2)
          }
          putType(resultType)
        case _ =>
          throw new FatalError("bad type: " + tpe + "(" + tpe.getClass + ")")

      }
    }
    private def putRefTypes(tpes: List[reflect.Type]) {
      tpes foreach putType
    }

    private def putSymbol(sym: reflect.Symbol): Unit = if(putEntry(sym)) {
      sym match {
        case reflect.Class(fullname) =>
          putConstant(Constant(fullname))
        case reflect.Method(fullname, tpe) =>
          putConstant(Constant(fullname))
          putType(tpe)
        case reflect.Field(fullname, tpe) =>
          putConstant(Constant(fullname))
          putType(tpe)
        case reflect.TypeField(fullname, tpe) =>
          putConstant(Constant(fullname))
          putType(tpe)
        case reflect.LocalValue(owner, name, tpe) =>
          putSymbol(owner)
          putConstant(Constant(name))
          putType(tpe)
        case reflect.LocalMethod(owner, name, tpe) =>
          putSymbol(owner)
          putConstant(Constant(name))
          putType(tpe)
        case reflect.NoSymbol => ()
        case reflect.RootSymbol => ()
        case reflect.LabelSymbol(name) =>
          putConstant(Constant(name))
      }
    }
    private def putRefSymbols(syms: List[reflect.Symbol]) =
      syms foreach putSymbol

    /** Store constant in map <code>index</code>.
     *
     *  @param c ...
     */
    private def putConstant(c: Constant) =
      if (putEntry(c)) {
        if (c.tag == StringTag) putEntry(newTermName(c.stringValue))
        else if (c.tag == ClassTag) putEntry(c.typeValue)
      }

    private def putChildren(sym: Symbol, children: List[Symbol]) {
      assert(putEntry((sym, children)))
      children foreach putSymbol
    }

    private def putAnnotation(sym: Symbol, annot: AnnotationInfo) {
      assert(putEntry((sym, annot)))
      val AnnotationInfo(atp, args, assocs) = annot
      putType(atp)
      args foreach putAnnotationArg
      for ((name, c) <- assocs) { putEntry(name); putAnnotationArg(c) }
    }

    private def putAnnotation(annot: AnnotationInfo) {
      if (putEntry(annot)) {
        val AnnotationInfo(tpe, args, assocs) = annot
        putType(tpe)
        args foreach putAnnotationArg
        for ((name, rhs) <- assocs) { putEntry(name); putAnnotationArg(rhs) }
      }
    }

    private def putAnnotationArg(arg: AnnotationArgument) {
      if (putEntry(arg)) {
        arg.constant match {
	  case Some(c) => putConstant(c)
	  case _ => putTree(arg.tree)
	}
      }
    }

    private def putAnnotations(annots: List[AnnotationInfo]) {
      annots foreach putAnnotation
    }

    // Phase 2 methods: Write all entries to byte array ------------------------------

    private val buf = new PickleBuffer(new Array[Byte](4096), -1, 0)

    /** Write a reference to object, i.e., the object's number in the map
     *  <code>index</code>.
     *
     *  @param ref ...
     */
    private def writeRef(ref: AnyRef) { writeNat(index(ref)) }
    private def writeRefs(refs: List[AnyRef]) { refs foreach writeRef }

    /** Write name, owner, flags, and info of a symbol.
     *
     *  @param sym ...
     *  @return    the position offset
     */
    private def writeSymInfo(sym: Symbol): Int = {
      var posOffset = 0
      writeRef(sym.name)
      writeRef(sym.owner)
      writeNat((sym.flags & PickledFlags).asInstanceOf[int])
      if (sym.privateWithin != NoSymbol) writeRef(sym.privateWithin)
      writeRef(sym.info)
      posOffset
    }

    /** Write a name in UTF8 format. */
    def writeName(name: Name) {
      ensureCapacity(name.length * 3)
      writeIndex = name.copyUTF8(bytes, writeIndex)
    }

    /** Write an entry */
    private def writeEntry(entry: AnyRef) {
      def writeBody(entry: AnyRef): Int = entry match {
        case name: Name =>
          writeName(name)
          if (name.isTermName) TERMname else TYPEname
        case NoSymbol =>
          NONEsym
        case sym: Symbol if !isLocal(sym) =>
          val tag =
            if (sym.isModuleClass) {
              writeRef(sym.name.toTermName); EXTMODCLASSref
            } else {
              writeRef(sym.name); EXTref
            }
          if (!sym.owner.isRoot) writeRef(sym.owner);
          tag
        case sym: ClassSymbol =>
          val posOffset = writeSymInfo(sym)
          if (sym.thisSym.tpe != sym.tpe) writeRef(sym.typeOfThis)
          CLASSsym + posOffset
        case sym: TypeSymbol =>
          val posOffset = writeSymInfo(sym)
          (if (sym.isAbstractType) TYPEsym else ALIASsym) + posOffset
        case sym: TermSymbol =>
          val posOffset = writeSymInfo(sym)
          if (sym.alias != NoSymbol) writeRef(sym.alias)
          (if (sym.isModule) MODULEsym else VALsym) + posOffset
        case NoType =>
          NOtpe
        case NoPrefix =>
          NOPREFIXtpe
        case ThisType(sym) =>
          writeRef(sym); THIStpe
        case SingleType(pre, sym) =>
          writeRef(pre); writeRef(sym); SINGLEtpe
        case ConstantType(value) =>
          writeRef(value); CONSTANTtpe
        case TypeRef(pre, sym, args) =>
          writeRef(pre); writeRef(sym); writeRefs(args); TYPEREFtpe
        case TypeBounds(lo, hi) =>
          writeRef(lo); writeRef(hi); TYPEBOUNDStpe
        case tp @ RefinedType(parents, decls) =>
          writeRef(tp.typeSymbol); writeRefs(parents); REFINEDtpe
        case ClassInfoType(parents, decls, clazz) =>
          writeRef(clazz); writeRefs(parents); CLASSINFOtpe
        case MethodType(formals, restpe) =>
          writeRef(restpe); writeRefs(formals)
          if (entry.isInstanceOf[ImplicitMethodType]) IMPLICITMETHODtpe
          else METHODtpe
        case PolyType(tparams, restpe) =>
          writeRef(restpe); writeRefs(tparams); POLYtpe
        case ExistentialType(tparams, restpe) =>
          writeRef(restpe); writeRefs(tparams); EXISTENTIALtpe
        case DeBruijnIndex(l, i) =>
          writeNat(l); writeNat(i); DEBRUIJNINDEXtpe
        case c @ Constant(_) =>
          if (c.tag == BooleanTag) writeLong(if (c.booleanValue) 1 else 0)
          else if (ByteTag <= c.tag && c.tag <= LongTag) writeLong(c.longValue)
          else if (c.tag == FloatTag) writeLong(Float.floatToIntBits(c.floatValue))
          else if (c.tag == DoubleTag) writeLong(Double.doubleToLongBits(c.doubleValue))
          else if (c.tag == StringTag) writeRef(newTermName(c.stringValue))
          else if (c.tag == ClassTag) writeRef(c.typeValue)
          LITERAL + c.tag
        case AnnotatedType(attribs, tp) =>
          writeRef(tp)
          writeRefs(attribs)
          ANNOTATEDtpe
        case (target: Symbol, attr @ AnnotationInfo(atp, args, assocs)) =>
          writeRef(target)
          writeRef(atp)
          for (c <- args) writeRef(c)
          for ((name, c) <- assocs) { writeRef(name); writeRef(c) }
          ATTRIBUTE
        case (target: Symbol, children: List[_]) =>
          writeRef(target)
          for (c <- children) writeRef(c.asInstanceOf[Symbol])
          CHILDREN
        case reflect.Ident(sym) =>
          writeNat(IDENTtree)
          writeRef(sym)
          REFLTREE
        case reflect.Select(qual, sym) =>
          writeNat(SELECTtree)
          writeRef(qual)
          writeRef(sym)
          REFLTREE
        case reflect.Literal(value) =>
          writeNat(LITERALtree)
          writeRef(Constant(value))
          REFLTREE
        case reflect.Apply(fun, args) =>
          writeNat(APPLYtree)
          writeRef(fun)
          writeRefs(args)
          REFLTREE
        case reflect.TypeApply(fun, args) =>
          writeNat(TYPEAPPLYtree)
          writeRef(fun)
          writeRefs(args)
          REFLTREE
        case reflect.Function(params, body) =>
          writeNat(FUNCTIONtree)
          writeRef(body)
          writeRefs(params)
          REFLTREE
        case reflect.This(sym) =>
          writeNat(THIStree)
          writeRef(sym)
          REFLTREE
        case reflect.Block(stats, expr) =>
          writeNat(BLOCKtree)
          writeRef(expr)
          writeRefs(stats)
          REFLTREE
        case reflect.New(clz) =>
          writeNat(NEWtree)
          writeRef(clz)
          REFLTREE
        case reflect.If(condition, trueCase, falseCase) =>
          writeNat(IFtree)
          writeRef(condition)
          writeRef(trueCase)
          writeRef(falseCase)
          REFLTREE
        case reflect.Assign(destination, source) =>
          writeNat(ASSIGNtree)
          writeRef(destination)
          writeRef(source)
          REFLTREE
        case reflect.Target(sym, body) =>
          writeNat(TARGETtree)
          writeRef(sym)
          writeRef(body)
          REFLTREE
        case reflect.Goto(target) =>
          writeNat(GOTOtree)
          writeRef(target)
          REFLTREE
        case reflect.ValDef(sym, rhs)  =>
          writeNat(VALDEFtree)
          writeRef(sym)
          writeRef(rhs)
          REFLTREE
        case reflect.ClassDef(sym, tpe, impl) =>
          writeNat(CLASSDEFtree)
          writeRef(sym)
          writeRef(tpe)
          writeRef(impl)
          REFLTREE
        case reflect.DefDef(sym, vparamss, ret, rhs) =>
          writeNat(DEFDEFtree)
          writeRef(sym)
          writeRef(ret)
          writeRef(rhs)
          for (vparams <- vparamss) {
            writeNat(vparams.length)
            writeRefs(vparams)
          }
          REFLTREE
        case reflect.Super(psym) =>
          writeNat(SUPERtree)
          writeRef(psym)
          REFLTREE
        case reflect.Template(parents, body) =>
          writeNat(TEMPLATEtree)
          writeNat(parents.length)
          writeRefs(parents)
          writeRefs(body)
          REFLTREE
        case reflect.NoPrefix =>
          writeNat(NOPREFIXrtpe)
          REFLTYPE
        case reflect.NoType =>
          writeNat(NOrtpe)
          REFLTYPE
        case reflect.NamedType(fullname) =>
          writeNat(NAMEDrtpe)
          writeRef(Constant(fullname))
          REFLTYPE
        case reflect.PrefixedType(pre, sym) =>
          writeNat(PREFIXEDrtpe)
          writeRef(pre)
          writeRef(sym)
          REFLTYPE
        case reflect.SingleType(pre, sym) =>
          writeNat(SINGLErtpe)
          writeRef(pre)
          writeRef(sym)
          REFLTYPE
        case reflect.ThisType(clazz) =>
          writeNat(THISrtpe)
          writeRef(clazz)
          REFLTYPE
        case reflect.AppliedType(tpe, args) =>
          writeNat(APPLIEDrtpe)
          writeRef(tpe)
          writeRefs(args)
          REFLTYPE
        case reflect.TypeBounds(lo, hi) =>
          writeNat(TYPEBOUNDSrtpe)
          writeRef(lo)
          writeRef(hi)
          REFLTYPE
        case entry@reflect.MethodType(formals, restpe) => //can be implicit
          if(entry.isInstanceOf[ImplicitMethodType])
            writeNat(IMPLICITMETHODrtpe)
          else
            writeNat(METHODrtpe)
          writeRef(restpe)
          writeRefs(formals)
          REFLTYPE
        case reflect.PolyType(typeParams, typeBounds, resultType) =>
          writeNat(POLYrtpe)
          writeRef(resultType)
          writeNat(typeBounds.length)
          for ((t1,t2) <- typeBounds) {
            writeRef(t1)
            writeRef(t2)
          }
          writeRefs(typeParams)
          REFLTYPE
        case reflect.Class(fullname) =>
          writeNat(CLASSrsym)
          writeRef(Constant(fullname))
          REFLSYM
        case reflect.Method(fullname, tpe) =>
          writeNat(METHODrsym)
          writeRef(Constant(fullname))
          writeRef(tpe)
          REFLSYM
        case reflect.Field(fullname, tpe) =>
          writeNat(FIELDrsym)
          writeRef(Constant(fullname))
          writeRef(tpe)
          REFLSYM
        case reflect.TypeField(fullname, tpe) =>
          writeNat(TYPEFIELDrsym)
          writeRef(Constant(fullname))
          writeRef(tpe)
          REFLSYM
        case reflect.LocalValue(owner, name, tpe) =>
          writeNat(LOCALVALUErsym)
          writeRef(owner)
          writeRef(Constant(name))
          writeRef(tpe)
          REFLSYM
        case reflect.LocalMethod(owner, name, tpe) =>
          writeNat(LOCALMETHODrsym)
          writeRef(owner)
          writeRef(Constant(name))
          writeRef(tpe)
          REFLSYM
        case reflect.NoSymbol =>
          writeNat(NOSYMBOLrsym)
          REFLSYM
        case reflect.RootSymbol =>
          writeNat(ROOTSYMBOLrsym)
          REFLSYM
        case reflect.LabelSymbol(name) =>
          writeNat(LABELSYMBOLrsym)
	  writeRef(name)
          REFLSYM
        case AnnotationInfo(target, args, assocs) =>
          writeRef(target)
          writeNat(args.length)
          for (arg <- args) writeRef(arg)
          for ((name, arg) <- assocs) {
            writeRef(name);
            writeRef(arg)
          }
          ATTRIBTREE

	case arg:AnnotationArgument =>
	  arg.constant match {
	    case Some(c) => writeBody(c)
	    case None => writeBody(arg.tree)
	  }

        case _ =>
          throw new FatalError("bad entry: " + entry + " " + entry.getClass)
      }
      val startpos = writeIndex
      writeByte(0); writeByte(0)
      patchNat(startpos, writeBody(entry))
      patchNat(startpos + 1, writeIndex - (startpos + 2))
    }

    /** Write byte array */
    def finish {
      assert(writeIndex == 0)
      writeNat(MajorVersion)
      writeNat(MinorVersion)
      writeNat(ep)
      if (settings.debug.value) log("" + ep + " entries")//debug
      for (i <- 0 until ep) writeEntry(entries(i))
      if (settings.Xshowcls.value == rootName.toString) {
        readIndex = 0
        ShowPickled.printFile(this, Console.out)
      }
    }

    override def toString() = "" + rootName + " in " + rootOwner
  }
}