aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/vendor/golang.org/x/arch/x86/x86asm/plan9ext_test.go
blob: 9bd296cf7560007382da61709e0735235fdd5226 (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
// Copyright 2014 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package x86asm

import (
	"bytes"
	"fmt"
	"io"
	"log"
	"os"
	"strconv"
	"testing"
)

const plan9Path = "testdata/libmach8db"

func testPlan9Arch(t *testing.T, arch int, generate func(func([]byte))) {
	if testing.Short() {
		t.Skip("skipping libmach test in short mode")
	}
	if _, err := os.Stat(plan9Path); err != nil {
		t.Skip(err)
	}

	testExtDis(t, "plan9", arch, plan9, generate, allowedMismatchPlan9)
}

func testPlan932(t *testing.T, generate func(func([]byte))) {
	testPlan9Arch(t, 32, generate)
}

func testPlan964(t *testing.T, generate func(func([]byte))) {
	testPlan9Arch(t, 64, generate)
}

func plan9(ext *ExtDis) error {
	flag := "-8"
	if ext.Arch == 64 {
		flag = "-6"
	}
	b, err := ext.Run(plan9Path, flag, ext.File.Name())
	if err != nil {
		return err
	}

	nmatch := 0
	next := uint32(start)
	var (
		addr   uint32
		encbuf [32]byte
		enc    []byte
		text   string
	)

	for {
		line, err := b.ReadSlice('\n')
		if err != nil {
			if err == io.EOF {
				break
			}
			return fmt.Errorf("reading libmach8db output: %v", err)
		}
		if debug {
			os.Stdout.Write(line)
		}
		nmatch++
		addr, enc, text = parseLinePlan9(line, encbuf[:0])
		if addr > next {
			return fmt.Errorf("address out of sync expected <= %#x at %q in:\n%s", next, line, line)
		}
		if addr < next {
			continue
		}
		if m := pcrelw.FindStringSubmatch(text); m != nil {
			targ, _ := strconv.ParseUint(m[2], 16, 64)
			text = fmt.Sprintf("%s .%+#x", m[1], int16(uint32(targ)-uint32(uint16(addr))-uint32(len(enc))))
		}
		if m := pcrel.FindStringSubmatch(text); m != nil {
			targ, _ := strconv.ParseUint(m[2], 16, 64)
			text = fmt.Sprintf("%s .%+#x", m[1], int32(uint32(targ)-addr-uint32(len(enc))))
		}
		ext.Dec <- ExtInst{addr, encbuf, len(enc), text}
		encbuf = [32]byte{}
		enc = nil
		next += 32
	}
	if next != start+uint32(ext.Size) {
		return fmt.Errorf("not enough results found [%d %d]", next, start+ext.Size)
	}
	if err := ext.Wait(); err != nil {
		return fmt.Errorf("exec: %v", err)
	}

	return nil
}

func parseLinePlan9(line []byte, encstart []byte) (addr uint32, enc []byte, text string) {
	i := bytes.IndexByte(line, ' ')
	if i < 0 || line[0] != '0' || line[1] != 'x' {
		log.Fatalf("cannot parse disassembly: %q", line)
	}
	j := bytes.IndexByte(line[i+1:], ' ')
	if j < 0 {
		log.Fatalf("cannot parse disassembly: %q", line)
	}
	j += i + 1
	x, err := strconv.ParseUint(string(trimSpace(line[2:i])), 16, 32)
	if err != nil {
		log.Fatalf("cannot parse disassembly: %q", line)
	}
	addr = uint32(x)
	enc, ok := parseHex(line[i+1:j], encstart)
	if !ok {
		log.Fatalf("cannot parse disassembly: %q", line)
	}
	return addr, enc, string(fixSpace(line[j+1:]))
}