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