collab/polyglot-riscv-arm

what if the same machine code was valid riscv and arm asm

git clone https://git.julia.blue/collab/polyglot-riscv-arm

Jonathan Dönszelmannwebsite start885cc4e

main
3.7 KiB161 linesraw
1package main
2
3import (
4	"bufio"
5	"flag"
6	"fmt"
7	"image"
8	"image/color"
9	"image/draw"
10	"image/png"
11	"log"
12	"os"
13	"strconv"
14	"strings"
15
16	tc "github.com/wayneashleyberry/truecolor/pkg/color"
17	"github.com/zyedidia/armvis/mra"
18)
19
20type Palette struct {
21	colors map[uint8]color.RGBA
22	bg     color.RGBA
23}
24
25func Hex(hex string) color.RGBA {
26	values, err := strconv.ParseUint(string(hex[1:]), 16, 32)
27	if err != nil {
28		panic(err)
29	}
30	return color.RGBA{R: uint8(values >> 16), G: uint8((values >> 8) & 0xFF), B: uint8(values & 0xFF), A: 255}
31}
32
33var solarized = Palette{
34	bg: color.RGBA{0, 43, 54, 0xff},
35	colors: map[uint8]color.RGBA{
36		mra.InstrGeneralID:   {181, 137, 0, 0xff},
37		mra.InstrSystemID:    {203, 75, 22, 0xff},
38		mra.InstrFloatID:     {220, 50, 47, 0xff},
39		mra.InstrFpSimdID:    {211, 54, 130, 0xff},
40		mra.InstrAdvSimdID:   {108, 113, 196, 0xff},
41		mra.InstrSveID:       {38, 139, 210, 0xff},
42		mra.InstrSve2ID:      {42, 161, 152, 0xff},
43		mra.InstrMortlachID:  {133, 153, 0, 0xff},
44		mra.InstrMortlach2ID: {253, 246, 227, 0xff},
45		mra.InstrNumIDs:      {131, 148, 150, 0xff},
46	},
47}
48
49var monokai = Palette{
50	bg: Hex("#272833"),
51	colors: map[uint8]color.RGBA{
52		mra.InstrGeneralID:   Hex("#66d9ef"),
53		mra.InstrSystemID:    Hex("#e6db74"),
54		mra.InstrFloatID:     Hex("#fd971f"),
55		mra.InstrFpSimdID:    Hex("#f92672"),
56		mra.InstrAdvSimdID:   Hex("#fd5ff0"),
57		mra.InstrSveID:       Hex("#ae81ff"),
58		mra.InstrSve2ID:      Hex("#a1efe4"),
59		mra.InstrMortlachID:  Hex("#a6e22e"),
60		mra.InstrMortlach2ID: Hex("#f8f8f2"),
61		mra.InstrNumIDs:      Hex("#75715e"),
62	},
63}
64
65var themes = map[string]Palette{
66	"monokai":   monokai,
67	"solarized": solarized,
68}
69
70func toInt(s string) int {
71	i, err := strconv.Atoi(s)
72	if err != nil {
73		log.Fatal(err)
74	}
75	return i
76}
77
78func main() {
79	out := flag.String("o", "arm64.png", "output file")
80	themename := flag.String("theme", "monokai", "color theme (monokai,solarized)")
81
82	flag.Parse()
83	args := flag.Args()
84	if len(args) <= 0 {
85		log.Fatal("no map input file")
86	}
87	f, err := os.Open(args[0])
88	if err != nil {
89		log.Fatal(err)
90	}
91
92	width := 4096
93	height := 4096
94	hilbertOrder := 12
95
96	img := image.NewRGBA(image.Rectangle{image.Point{0, 0}, image.Point{width, height}})
97	bg := image.NewRGBA(image.Rectangle{image.Point{0, 0}, image.Point{width, height}})
98
99	if _, ok := themes[*themename]; !ok {
100		log.Fatalf("theme %s does not exist", *themename)
101	}
102	theme := themes[*themename]
103
104	overlay := image.NewRGBA(image.Rectangle{image.Point{0, 0}, image.Point{width, height}})
105
106	// Fill background.
107	for x := 0; x < width; x++ {
108		for y := 0; y < height; y++ {
109			bg.Set(x, y, theme.bg)
110		}
111	}
112
113	scanner := bufio.NewScanner(f)
114	for scanner.Scan() {
115		fields := strings.Fields(scanner.Text())
116		s, err := strconv.ParseUint(fields[0], 10, 32)
117		if err != nil {
118			log.Fatal(err)
119		}
120		x, y := hilbertXY(uint32(s)/256, hilbertOrder)
121		c := theme.bg
122		for _, f := range fields[1:] {
123			parts := strings.Split(f, ":")
124			if len(parts) != 3 {
125				log.Fatal("each field should be of the form <class>:<subclass>:<count>")
126			}
127			i := toInt(parts[0])
128			// subclass := toInt(parts[1])
129			count := toInt(parts[2])
130			c = theme.colors[uint8(i)]
131			c.A = uint8(count - 1)
132			if c.A > 0 {
133				_, _, _, a := overlay.At(int(x), int(y)).RGBA()
134				if c.A > uint8(a) {
135					overlay.Set(int(x), int(y), c)
136				}
137			}
138			break
139		}
140	}
141
142	draw.Draw(img, img.Bounds(), bg, image.Point{0, 0}, draw.Src)
143	draw.DrawMask(img, img.Bounds(), overlay, image.Point{0, 0}, overlay, image.Point{0, 0}, draw.Over)
144
145	// Encode as PNG.
146	of, err := os.Create(*out)
147	if err != nil {
148		log.Fatal(err)
149	}
150	err = png.Encode(of, img)
151	if err != nil {
152		log.Fatal(err)
153	}
154
155	for i := uint8(0); i < uint8(len(theme.colors)); i++ {
156		c := theme.colors[i]
157		tc.Black().Background(c.R, c.G, c.B).Print(mra.IDToClass(i))
158		fmt.Print(" ")
159	}
160	fmt.Println()
161}