| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| package gcprog |
|
|
| import ( |
| "fmt" |
| "io" |
| ) |
|
|
| const progMaxLiteral = 127 |
|
|
| |
| |
| |
| |
| |
| type Writer struct { |
| writeByte func(byte) |
| index int64 |
| b [progMaxLiteral]byte |
| nb int |
| debug io.Writer |
| debugBuf []byte |
| } |
|
|
| |
| |
| func (w *Writer) Init(writeByte func(byte)) { |
| w.writeByte = writeByte |
| } |
|
|
| |
| |
| |
| func (w *Writer) Debug(out io.Writer) { |
| w.debug = out |
| } |
|
|
| |
| func (w *Writer) byte(x byte) { |
| if w.debug != nil { |
| w.debugBuf = append(w.debugBuf, x) |
| } |
| w.writeByte(x) |
| } |
|
|
| |
| func (w *Writer) End() { |
| w.flushlit() |
| w.byte(0) |
| if w.debug != nil { |
| index := progbits(w.debugBuf) |
| if index != w.index { |
| println("gcprog: End wrote program for", index, "bits, but current index is", w.index) |
| panic("gcprog: out of sync") |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| func (w *Writer) Ptr(index int64) { |
| if index < w.index { |
| println("gcprog: Ptr at index", index, "but current index is", w.index) |
| panic("gcprog: invalid Ptr index") |
| } |
| w.ZeroUntil(index) |
| if w.debug != nil { |
| fmt.Fprintf(w.debug, "gcprog: ptr at %d\n", index) |
| } |
| w.lit(1) |
| } |
|
|
| |
| |
| func (w *Writer) Repeat(n, c int64) { |
| if n == 0 || c == 0 { |
| return |
| } |
| w.flushlit() |
| if w.debug != nil { |
| fmt.Fprintf(w.debug, "gcprog: repeat %d × %d\n", n, c) |
| } |
| if n < 128 { |
| w.byte(0x80 | byte(n)) |
| } else { |
| w.byte(0x80) |
| w.varint(n) |
| } |
| w.varint(c) |
| w.index += n * c |
| } |
|
|
| |
| |
| |
| |
| func (w *Writer) ZeroUntil(index int64) { |
| if index < w.index { |
| println("gcprog: Advance", index, "but index is", w.index) |
| panic("gcprog: invalid Advance index") |
| } |
| skip := (index - w.index) |
| if skip == 0 { |
| return |
| } |
| if skip < 4*8 { |
| if w.debug != nil { |
| fmt.Fprintf(w.debug, "gcprog: advance to %d by literals\n", index) |
| } |
| for i := int64(0); i < skip; i++ { |
| w.lit(0) |
| } |
| return |
| } |
|
|
| if w.debug != nil { |
| fmt.Fprintf(w.debug, "gcprog: advance to %d by repeat\n", index) |
| } |
| w.lit(0) |
| w.flushlit() |
| w.Repeat(1, skip-1) |
| } |
|
|
| |
| func progbits(p []byte) int64 { |
| var n int64 |
| for len(p) > 0 { |
| x := p[0] |
| p = p[1:] |
| if x == 0 { |
| break |
| } |
| if x&0x80 == 0 { |
| count := x &^ 0x80 |
| n += int64(count) |
| p = p[(count+7)/8:] |
| continue |
| } |
| nbit := int64(x &^ 0x80) |
| if nbit == 0 { |
| nbit, p = readvarint(p) |
| } |
| var count int64 |
| count, p = readvarint(p) |
| n += nbit * count |
| } |
| if len(p) > 0 { |
| println("gcprog: found end instruction after", n, "ptrs, with", len(p), "bytes remaining") |
| panic("gcprog: extra data at end of program") |
| } |
| return n |
| } |
|
|
| |
| func readvarint(p []byte) (int64, []byte) { |
| var v int64 |
| var nb uint |
| for { |
| c := p[0] |
| p = p[1:] |
| v |= int64(c&^0x80) << nb |
| nb += 7 |
| if c&0x80 == 0 { |
| break |
| } |
| } |
| return v, p |
| } |
|
|
| |
| func (w *Writer) lit(x byte) { |
| if w.nb == progMaxLiteral { |
| w.flushlit() |
| } |
| w.b[w.nb] = x |
| w.nb++ |
| w.index++ |
| } |
|
|
| |
| func (w *Writer) varint(x int64) { |
| if x < 0 { |
| panic("gcprog: negative varint") |
| } |
| for x >= 0x80 { |
| w.byte(byte(0x80 | x)) |
| x >>= 7 |
| } |
| w.byte(byte(x)) |
| } |
|
|
| |
| func (w *Writer) flushlit() { |
| if w.nb == 0 { |
| return |
| } |
| if w.debug != nil { |
| fmt.Fprintf(w.debug, "gcprog: flush %d literals\n", w.nb) |
| fmt.Fprintf(w.debug, "\t%v\n", w.b[:w.nb]) |
| fmt.Fprintf(w.debug, "\t%02x", byte(w.nb)) |
| } |
| w.byte(byte(w.nb)) |
| var bits uint8 |
| for i := 0; i < w.nb; i++ { |
| bits |= w.b[i] << uint(i%8) |
| if (i+1)%8 == 0 { |
| if w.debug != nil { |
| fmt.Fprintf(w.debug, " %02x", bits) |
| } |
| w.byte(bits) |
| bits = 0 |
| } |
| } |
| if w.nb%8 != 0 { |
| if w.debug != nil { |
| fmt.Fprintf(w.debug, " %02x", bits) |
| } |
| w.byte(bits) |
| } |
| if w.debug != nil { |
| fmt.Fprintf(w.debug, "\n") |
| } |
| w.nb = 0 |
| } |
|
|