| 1 |
package compile |
| 2 |
|
| 3 |
// This file defines functions to read and write a compile.Program to a file. |
| 4 |
// |
| 5 |
// It is the client's responsibility to avoid version skew between the |
| 6 |
// compiler used to produce a file and the interpreter that consumes it. |
| 7 |
// The version number is provided as a constant. |
| 8 |
// Incompatible protocol changes should also increment the version number. |
| 9 |
// |
| 10 |
// Encoding |
| 11 |
// |
| 12 |
// Program: |
| 13 |
// "sky!" [4]byte # magic number |
| 14 |
// str uint32le # offset of <strings> section |
| 15 |
// version varint # must match Version |
| 16 |
// filename string |
| 17 |
// numloads varint |
| 18 |
// loads []Ident |
| 19 |
// numnames varint |
| 20 |
// names []string |
| 21 |
// numconsts varint |
| 22 |
// consts []Constant |
| 23 |
// numglobals varint |
| 24 |
// globals []Ident |
| 25 |
// toplevel Funcode |
| 26 |
// numfuncs varint |
| 27 |
// funcs []Funcode |
| 28 |
// recursion varint (0 or 1) |
| 29 |
// <strings> []byte # concatenation of all referenced strings |
| 30 |
// EOF |
| 31 |
// |
| 32 |
// Funcode: |
| 33 |
// id Ident |
| 34 |
// code []byte |
| 35 |
// pclinetablen varint |
| 36 |
// pclinetab []varint |
| 37 |
// numlocals varint |
| 38 |
// locals []Ident |
| 39 |
// numcells varint |
| 40 |
// cells []int |
| 41 |
// numfreevars varint |
| 42 |
// freevar []Ident |
| 43 |
// maxstack varint |
| 44 |
// numparams varint |
| 45 |
// numkwonlyparams varint |
| 46 |
// hasvarargs varint (0 or 1) |
| 47 |
// haskwargs varint (0 or 1) |
| 48 |
// |
| 49 |
// Ident: |
| 50 |
// filename string |
| 51 |
// line, col varint |
| 52 |
// |
| 53 |
// Constant: # type data |
| 54 |
// type varint # 0=string string |
| 55 |
// data ... # 1=bytes string |
| 56 |
// # 2=int varint |
| 57 |
// # 3=float varint (bits as uint64) |
| 58 |
// # 4=bigint string (decimal ASCII text) |
| 59 |
// |
| 60 |
// The encoding starts with a four-byte magic number. |
| 61 |
// The next four bytes are a little-endian uint32 |
| 62 |
// that provides the offset of the string section |
| 63 |
// at the end of the file, which contains the ordered |
| 64 |
// concatenation of all strings referenced by the |
| 65 |
// program. This design permits the decoder to read |
| 66 |
// the first and second parts of the file into different |
| 67 |
// memory allocations: the first (the encoded program) |
| 68 |
// is transient, but the second (the strings) persists |
| 69 |
// for the life of the Program. |
| 70 |
// |
| 71 |
// Within the encoded program, all strings are referred |
| 72 |
// to by their length. As the encoder and decoder process |
| 73 |
// the entire file sequentially, they are in lock step, |
| 74 |
// so the start offset of each string is implicit. |
| 75 |
// |
| 76 |
// Program.Code is represented as a []byte slice to permit |
| 77 |
// modification when breakpoints are set. All other strings |
| 78 |
// are represented as strings. They all (unsafely) share the |
| 79 |
// same backing byte slice. |
| 80 |
// |
| 81 |
// Aside from the str field, all integers are encoded as varints. |
| 82 |
|
| 83 |
import ( |
| 84 |
"encoding/binary" |
| 85 |
"fmt" |
| 86 |
"math" |
| 87 |
"math/big" |
| 88 |
debugpkg "runtime/debug" |
| 89 |
"slices" |
| 90 |
"unsafe" |
| 91 |
|
| 92 |
"go.starlark.net/syntax" |
| 93 |
) |
| 94 |
|
| 95 |
const magic = "!sky" |
| 96 |
|
| 97 |
// Encode encodes a compiled Starlark program. |
| 98 |
func (prog *Program) Encode() []byte { |
| 99 |
var e encoder |
| 100 |
e.p = append(e.p, magic...) |
| 101 |
e.p = append(e.p, "????"...) // string data offset; filled in later |
| 102 |
e.int(Version) |
| 103 |
e.string(prog.Toplevel.Pos.Filename()) |
| 104 |
e.bindings(prog.Loads) |
| 105 |
e.int(len(prog.Names)) |
| 106 |
for _, name := range prog.Names { |
| 107 |
e.string(name) |
| 108 |
} |
| 109 |
e.int(len(prog.Constants)) |
| 110 |
for _, c := range prog.Constants { |
| 111 |
switch c := c.(type) { |
| 112 |
case string: |
| 113 |
e.int(0) |
| 114 |
e.string(c) |
| 115 |
case Bytes: |
| 116 |
e.int(1) |
| 117 |
e.string(string(c)) |
| 118 |
case int64: |
| 119 |
e.int(2) |
| 120 |
e.int64(c) |
| 121 |
case float64: |
| 122 |
e.int(3) |
| 123 |
e.uint64(math.Float64bits(c)) |
| 124 |
case *big.Int: |
| 125 |
e.int(4) |
| 126 |
e.string(c.Text(10)) |
| 127 |
} |
| 128 |
} |
| 129 |
e.bindings(prog.Globals) |
| 130 |
e.function(prog.Toplevel) |
| 131 |
e.int(len(prog.Functions)) |
| 132 |
for _, fn := range prog.Functions { |
| 133 |
e.function(fn) |
| 134 |
} |
| 135 |
e.int(b2i(prog.Recursion)) |
| 136 |
|
| 137 |
// Patch in the offset of the string data section. |
| 138 |
binary.LittleEndian.PutUint32(e.p[4:8], uint32(len(e.p))) |
| 139 |
|
| 140 |
return append(e.p, e.s...) |
| 141 |
} |
| 142 |
|
| 143 |
type encoder struct { |
| 144 |
p []byte // encoded program |
| 145 |
s []byte // strings |
| 146 |
tmp [binary.MaxVarintLen64]byte |
| 147 |
} |
| 148 |
|
| 149 |
func (e *encoder) int(x int) { |
| 150 |
e.int64(int64(x)) |
| 151 |
} |
| 152 |
|
| 153 |
func (e *encoder) int64(x int64) { |
| 154 |
n := binary.PutVarint(e.tmp[:], x) |
| 155 |
e.p = append(e.p, e.tmp[:n]...) |
| 156 |
} |
| 157 |
|
| 158 |
func (e *encoder) uint64(x uint64) { |
| 159 |
n := binary.PutUvarint(e.tmp[:], x) |
| 160 |
e.p = append(e.p, e.tmp[:n]...) |
| 161 |
} |
| 162 |
|
| 163 |
func (e *encoder) string(s string) { |
| 164 |
e.int(len(s)) |
| 165 |
e.s = append(e.s, s...) |
| 166 |
} |
| 167 |
|
| 168 |
func (e *encoder) bytes(b []byte) { |
| 169 |
e.int(len(b)) |
| 170 |
e.s = append(e.s, b...) |
| 171 |
} |
| 172 |
|
| 173 |
func (e *encoder) binding(bind Binding) { |
| 174 |
e.string(bind.Name) |
| 175 |
e.int(int(bind.Pos.Line)) |
| 176 |
e.int(int(bind.Pos.Col)) |
| 177 |
} |
| 178 |
|
| 179 |
func (e *encoder) bindings(binds []Binding) { |
| 180 |
e.int(len(binds)) |
| 181 |
for _, bind := range binds { |
| 182 |
e.binding(bind) |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
func (e *encoder) function(fn *Funcode) { |
| 187 |
e.binding(Binding{fn.Name, fn.Pos}) |
| 188 |
e.string(fn.Doc) |
| 189 |
e.bytes(fn.Code) |
| 190 |
e.int(len(fn.pclinetab)) |
| 191 |
for _, x := range fn.pclinetab { |
| 192 |
e.int64(int64(x)) |
| 193 |
} |
| 194 |
e.bindings(fn.Locals) |
| 195 |
e.int(len(fn.Cells)) |
| 196 |
for _, index := range fn.Cells { |
| 197 |
e.int(index) |
| 198 |
} |
| 199 |
e.bindings(fn.FreeVars) |
| 200 |
e.int(fn.MaxStack) |
| 201 |
e.int(fn.NumParams) |
| 202 |
e.int(fn.NumKwonlyParams) |
| 203 |
e.int(b2i(fn.HasVarargs)) |
| 204 |
e.int(b2i(fn.HasKwargs)) |
| 205 |
} |
| 206 |
|
| 207 |
func b2i(b bool) int { |
| 208 |
if b { |
| 209 |
return 1 |
| 210 |
} else { |
| 211 |
return 0 |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
// DecodeProgram decodes a compiled Starlark program from data. |
| 216 |
func DecodeProgram(data []byte) (_ *Program, err error) { |
| 217 |
if len(data) < len(magic) { |
| 218 |
return nil, fmt.Errorf("not a compiled module: no magic number") |
| 219 |
} |
| 220 |
if got := string(data[:4]); got != magic { |
| 221 |
return nil, fmt.Errorf("not a compiled module: got magic number %q, want %q", |
| 222 |
got, magic) |
| 223 |
} |
| 224 |
defer func() { |
| 225 |
if x := recover(); x != nil { |
| 226 |
debugpkg.PrintStack() |
| 227 |
err = fmt.Errorf("internal error while decoding program: %v", x) |
| 228 |
} |
| 229 |
}() |
| 230 |
|
| 231 |
offset := binary.LittleEndian.Uint32(data[4:8]) |
| 232 |
d := decoder{ |
| 233 |
p: data[8:offset], |
| 234 |
s: slices.Clone(data[offset:]), // allocate a copy, which will persist |
| 235 |
} |
| 236 |
|
| 237 |
if v := d.int(); v != Version { |
| 238 |
return nil, fmt.Errorf("version mismatch: read %d, want %d", v, Version) |
| 239 |
} |
| 240 |
|
| 241 |
filename := d.string() |
| 242 |
d.filename = &filename |
| 243 |
|
| 244 |
loads := d.bindings() |
| 245 |
|
| 246 |
names := make([]string, d.int()) |
| 247 |
for i := range names { |
| 248 |
names[i] = d.string() |
| 249 |
} |
| 250 |
|
| 251 |
// constants |
| 252 |
constants := make([]any, d.int()) |
| 253 |
for i := range constants { |
| 254 |
var c any |
| 255 |
switch d.int() { |
| 256 |
case 0: |
| 257 |
c = d.string() |
| 258 |
case 1: |
| 259 |
c = Bytes(d.string()) |
| 260 |
case 2: |
| 261 |
c = d.int64() |
| 262 |
case 3: |
| 263 |
c = math.Float64frombits(d.uint64()) |
| 264 |
case 4: |
| 265 |
c, _ = new(big.Int).SetString(d.string(), 10) |
| 266 |
} |
| 267 |
constants[i] = c |
| 268 |
} |
| 269 |
|
| 270 |
globals := d.bindings() |
| 271 |
toplevel := d.function() |
| 272 |
funcs := make([]*Funcode, d.int()) |
| 273 |
for i := range funcs { |
| 274 |
funcs[i] = d.function() |
| 275 |
} |
| 276 |
recursion := d.int() != 0 |
| 277 |
|
| 278 |
prog := &Program{ |
| 279 |
Loads: loads, |
| 280 |
Names: names, |
| 281 |
Constants: constants, |
| 282 |
Globals: globals, |
| 283 |
Functions: funcs, |
| 284 |
Toplevel: toplevel, |
| 285 |
Recursion: recursion, |
| 286 |
} |
| 287 |
toplevel.Prog = prog |
| 288 |
for _, f := range funcs { |
| 289 |
f.Prog = prog |
| 290 |
} |
| 291 |
|
| 292 |
if len(d.p)+len(d.s) > 0 { |
| 293 |
return nil, fmt.Errorf("internal error: unconsumed data during decoding") |
| 294 |
} |
| 295 |
|
| 296 |
return prog, nil |
| 297 |
} |
| 298 |
|
| 299 |
type decoder struct { |
| 300 |
p []byte // encoded program |
| 301 |
s []byte // strings |
| 302 |
filename *string // (indirect to avoid keeping decoder live) |
| 303 |
} |
| 304 |
|
| 305 |
func (d *decoder) int() int { |
| 306 |
return int(d.int64()) |
| 307 |
} |
| 308 |
|
| 309 |
func (d *decoder) int64() int64 { |
| 310 |
x, len := binary.Varint(d.p[:]) |
| 311 |
d.p = d.p[len:] |
| 312 |
return x |
| 313 |
} |
| 314 |
|
| 315 |
func (d *decoder) uint64() uint64 { |
| 316 |
x, len := binary.Uvarint(d.p[:]) |
| 317 |
d.p = d.p[len:] |
| 318 |
return x |
| 319 |
} |
| 320 |
|
| 321 |
func (d *decoder) string() string { |
| 322 |
// Avoid a memory allocation for each string |
| 323 |
// by unsafely aliasing slice. |
| 324 |
slice := d.bytes() |
| 325 |
return unsafe.String(unsafe.SliceData(slice), len(slice)) |
| 326 |
} |
| 327 |
|
| 328 |
func (d *decoder) bytes() []byte { |
| 329 |
len := d.int() |
| 330 |
r := d.s[:len:len] |
| 331 |
d.s = d.s[len:] |
| 332 |
return r |
| 333 |
} |
| 334 |
|
| 335 |
func (d *decoder) binding() Binding { |
| 336 |
name := d.string() |
| 337 |
line := int32(d.int()) |
| 338 |
col := int32(d.int()) |
| 339 |
return Binding{Name: name, Pos: syntax.MakePosition(d.filename, line, col)} |
| 340 |
} |
| 341 |
|
| 342 |
func (d *decoder) bindings() []Binding { |
| 343 |
bindings := make([]Binding, d.int()) |
| 344 |
for i := range bindings { |
| 345 |
bindings[i] = d.binding() |
| 346 |
} |
| 347 |
return bindings |
| 348 |
} |
| 349 |
|
| 350 |
func (d *decoder) ints() []int { |
| 351 |
ints := make([]int, d.int()) |
| 352 |
for i := range ints { |
| 353 |
ints[i] = d.int() |
| 354 |
} |
| 355 |
return ints |
| 356 |
} |
| 357 |
|
| 358 |
func (d *decoder) bool() bool { return d.int() != 0 } |
| 359 |
|
| 360 |
func (d *decoder) function() *Funcode { |
| 361 |
id := d.binding() |
| 362 |
doc := d.string() |
| 363 |
code := d.bytes() |
| 364 |
pclinetab := make([]uint16, d.int()) |
| 365 |
for i := range pclinetab { |
| 366 |
pclinetab[i] = uint16(d.int()) |
| 367 |
} |
| 368 |
locals := d.bindings() |
| 369 |
cells := d.ints() |
| 370 |
freevars := d.bindings() |
| 371 |
maxStack := d.int() |
| 372 |
numParams := d.int() |
| 373 |
numKwonlyParams := d.int() |
| 374 |
hasVarargs := d.int() != 0 |
| 375 |
hasKwargs := d.int() != 0 |
| 376 |
return &Funcode{ |
| 377 |
// Prog is filled in later. |
| 378 |
Pos: id.Pos, |
| 379 |
Name: id.Name, |
| 380 |
Doc: doc, |
| 381 |
Code: code, |
| 382 |
pclinetab: pclinetab, |
| 383 |
Locals: locals, |
| 384 |
Cells: cells, |
| 385 |
FreeVars: freevars, |
| 386 |
MaxStack: maxStack, |
| 387 |
NumParams: numParams, |
| 388 |
NumKwonlyParams: numKwonlyParams, |
| 389 |
HasVarargs: hasVarargs, |
| 390 |
HasKwargs: hasKwargs, |
| 391 |
} |
| 392 |
} |