about summary refs log tree commit diff
path: root/pkg/lang/vm/code/builder.go
blob: f027c4948abc6ab0fb5cc9a940242f9c6450bfe3 (plain)
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
package code

import (
	"encoding/binary"
	"fmt"
	"math"
)

type Builder struct {
	code           []byte
	debugInfo      DebugInfo
	relativePcRefs map[int][]int

	currentLine int
	lineStart   int
}

func NewBuilder() Builder {
	return Builder{
		code:           make([]byte, 0, 64),
		debugInfo:      NewDebugInfo("unknown file"),
		relativePcRefs: make(map[int][]int),
		lineStart:      -1,
		currentLine:    -1,
	}
}

func (b *Builder) AppendOp(op Op) {
	b.code = append(b.code, byte(op))
}

func (b *Builder) AppendInt(x int64) {
	b.code = append(b.code, make([]byte, 8)...)
	binary.LittleEndian.PutUint64(b.code[len(b.code)-8:], uint64(x))
}

func (b *Builder) AppendFloat(x float64) {
	b.AppendInt(int64(math.Float64bits(x)))
}

func (b *Builder) AppendString(s string) {
	b.code = append(b.code, []byte(s)...)
	b.code = append(b.code, 0)
}

func (b *Builder) AppendReferenceToPc(pc int64) {
	b.addPcRef(int(pc), b.Len())
	b.AppendInt(pc)
}

func (b *Builder) AppendRaw(raw Raw) {
	b.code = append(b.code, raw...)
}

func (b *Builder) AppendBuilder(other Builder) {
	code := other.code
	for pc, refsToPc := range other.relativePcRefs {
		newPc := b.Len() + pc
		for _, ref := range refsToPc {
			valueAtRef := binary.LittleEndian.Uint64(code[ref : ref+8])
			if int(valueAtRef) != pc {
				panic(fmt.Errorf("reference to pc in builder does not actually reference pc. (pc: %d, value at reference: %d)", pc, valueAtRef))
			}

			binary.LittleEndian.PutUint64(code[ref:], uint64(newPc))
			b.addPcRef(newPc, ref+b.Len())
		}
	}

	b.code = append(b.code, code...)

	if other.debugInfo.pcToLine.Len() != 0 || other.currentLine != -1 || other.lineStart != -1 {
		panic("appending debug infos not implemented yet")
	}
}

func (b *Builder) AppendBuilderWithoutAdjustingReferences(other Builder) {
	code := other.code
	for pc, refsToPc := range other.relativePcRefs {
		for _, ref := range refsToPc {
			b.addPcRef(pc, ref+b.Len())
		}
	}

	b.code = append(b.code, code...)

	if other.debugInfo.pcToLine.Len() != 0 || other.currentLine != -1 || other.lineStart != -1 {
		panic("appending debug infos not implemented yet")
	}
}

func (b *Builder) StartLine(line int) {
	if b.lineStart != -1 {
		panic("line already started")
	}

	b.currentLine = line
	b.lineStart = b.Len()
}

func (b *Builder) EndLine() {
	defer func() {
		b.currentLine = -1
		b.lineStart = -1
	}()

	if b.lineStart == -1 {
		panic("line not started")
	}

	if b.lineStart == b.Len() {
		return
	}

	b.debugInfo.AppendLine(b.Len()-1, b.currentLine)
}

func (b *Builder) SetInt(at int, x int64) {
	binary.LittleEndian.PutUint64(b.code[at:], uint64(x))
}

func (b *Builder) Code() Raw {
	return Raw(b.code)
}

func (b *Builder) Len() int {
	return len(b.code)
}

func (b *Builder) Build() Code {
	return New(b.code, b.debugInfo)
}

func (b *Builder) addPcRef(pc int, at int) {
	refs, ok := b.relativePcRefs[pc]
	if ok {
		refs = append(refs, at)
	} else {
		refs = make([]int, 1)
		refs[0] = at
	}

	b.relativePcRefs[pc] = refs
}