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 mra 2 3import ( 4"bytes" 5"encoding/xml" 6"fmt" 7"log" 8"regexp" 9"strings" 10) 11 12const ( 13InstrGeneral = "general" 14InstrSystem = "system" 15InstrFloat = "float" 16InstrFpSimd = "fpsimd" 17InstrAdvSimd = "advsimd" 18InstrSve = "sve" 19InstrSve2 = "sve2" 20InstrMortlach = "mortlach" 21InstrMortlach2 = "mortlach2" 22) 23 24const ( 25InstrGeneralID uint8 = iota 26InstrSystemID 27InstrFloatID 28InstrFpSimdID 29InstrAdvSimdID 30InstrSveID 31InstrSve2ID 32InstrMortlachID 33InstrMortlach2ID 34InstrNumIDs 35) 36 37func IDToClass (class uint8 )string { 38switch class { 39case InstrGeneralID : 40return InstrGeneral 41case InstrSystemID : 42return InstrSystem 43case InstrFloatID : 44return InstrFloat 45case InstrFpSimdID : 46return InstrFpSimd 47case InstrAdvSimdID : 48return InstrAdvSimd 49case InstrSveID : 50return InstrSve 51case InstrSve2ID : 52return InstrSve2 53case InstrMortlachID : 54return InstrMortlach 55case InstrMortlach2ID : 56return InstrMortlach2 57 } 58return "other" 59} 60 61func ClassToID (class string )uint8 { 62switch class { 63case InstrGeneral : 64return InstrGeneralID 65case InstrSystem : 66return InstrSystemID 67case InstrFloat : 68return InstrFloatID 69case InstrFpSimd : 70return InstrFpSimdID 71case InstrAdvSimd : 72return InstrAdvSimdID 73case InstrSve : 74return InstrSveID 75case InstrSve2 : 76return InstrSve2ID 77case InstrMortlach : 78return InstrMortlachID 79case InstrMortlach2 : 80return InstrMortlach2ID 81 } 82return InstrNumIDs 83} 84 85var InstrBase = strings .Join ([]string { 86InstrGeneral , 87InstrFloat , 88InstrFpSimd , 89InstrAdvSimd , 90},"," ) 91 92type DocVar struct { 93XMLName xml.Name `xml:"docvar"` 94Key string `xml:"key,attr"` 95Val string `xml:"value,attr"` 96} 97 98type DocVars struct { 99XMLName xml.Name `xml:"docvars"` 100Vars []DocVar `xml:"docvar"` 101} 102 103func (d DocVars )Mnemonic ()string { 104for _ ,v := range d .Vars { 105if v .Key == "mnemonic" { 106return v .Val 107 } 108 } 109return "" 110} 111 112func (d DocVars )InstrClass ()string { 113for _ ,v := range d .Vars { 114if v .Key == "instr-class" { 115return v .Val 116 } 117 } 118return "" 119} 120 121type PsText struct { 122XMLName xml.Name `xml:"pstext"` 123Content string `xml:",innerxml"` 124} 125 126type Ps struct { 127XMLName xml.Name `xml:"ps"` 128Name string `xml:"name,attr"` 129PsText PsText `xml:"pstext"` 130} 131 132type PsSection struct { 133XMLName xml.Name `xml:"ps_section"` 134Ps Ps 135} 136 137type ArchVariant struct { 138XMLName xml.Name `xml:"arch_variant"` 139Name string `xml:"name,attr"` 140Feature string `xml:"feature,attr"` 141} 142 143type ArchVariants struct { 144XMLName xml.Name `xml:"arch_variants"` 145Variants []ArchVariant `xml:"arch_variant"` 146} 147 148func (a ArchVariants )GetVariants () []string { 149var vars []string 150for _ ,v := range a .Variants { 151vars = append (vars ,v .Name ) 152 } 153return vars 154} 155 156func (a ArchVariants )GetFeatures () []string { 157var feats []string 158for _ ,v := range a .Variants { 159feats = append (feats ,v .Feature ) 160 } 161return feats 162} 163 164type BitC struct { 165XMLName xml.Name `xml:"c"` 166Cols int `xml:"colspan,attr"` 167Value string `xml:",chardata"` 168} 169 170type Box struct { 171XMLName xml.Name `xml:"box"` 172Bits []BitC `xml:"c"` 173Name string `xml:"name,attr"` 174Constraint string `xml:"constraint,attr"` 175} 176 177type RegDiagram struct { 178XMLName xml.Name `xml:"regdiagram"` 179Name string `xml:"psname,attr"` 180Boxes []Box `xml:"box"` 181} 182 183func (r RegDiagram )String ()string { 184b := & bytes.Buffer {} 185for i ,box := range r .Boxes { 186if box .Name != "" { 187fmt .Fprint (b ,box .Name ) 188 } 189if box .Constraint != "" { 190fmt .Fprint (b ,strings .ReplaceAll (box .Constraint ," " ,"" ),"|" ) 191continue 192 }else if box .Name != "" { 193fmt .Fprint (b ,"=" ) 194 } 195for _ ,bit := range box .Bits { 196if bit .Value == "" { 197bit .Value = "x" 198 } 199if bit .Cols == 0 { 200bit .Cols = 1 201 } 202bit .Value = strings .ReplaceAll (bit .Value ,"(1)" ,"x" ) 203bit .Value = strings .ReplaceAll (bit .Value ,"(0)" ,"x" ) 204fmt .Fprint (b ,strings .Repeat (bit .Value ,bit .Cols )) 205 } 206if i != len (r .Boxes )- 1 { 207fmt .Fprint (b ,"|" ) 208 } 209 } 210return b .String () 211} 212 213type Encoding struct { 214XMLName xml.Name `xml:"encoding"` 215Name string `xml:"name,attr"` 216Docs DocVars `xml:"docvars"` 217} 218 219type IClass struct { 220XMLName xml.Name `xml:"iclass"` 221Name string `xml:"name,attr"` 222Id string `xml:"id,attr"` 223RegDiagram RegDiagram `xml:"regdiagram"` 224ArchVariants ArchVariants `xml:"arch_variants"` 225Code PsSection `xml:"ps_section"` 226Encodings []Encoding `xml:"encoding"` 227Docs DocVars `xml:"docvars"` 228} 229 230type Classes struct { 231XMLName xml.Name `xml:"classes"` 232IClass []IClass `xml:"iclass"` 233} 234 235type InsnSection struct { 236XMLName xml.Name `xml:"instructionsection"` 237Docs DocVars `xml:"docvars"` 238Type string `xml:"type,attr"` 239Id string `xml:"id,attr"` 240Classes Classes `xml:"classes"` 241Code PsSection `xml:"ps_section"` 242} 243 244type Assign struct { 245Dest string 246Src string 247} 248 249func getAssigns (code string ) []Assign { 250lines := strings .Split (code ,"\n" ) 251var assigns []Assign 252for _ ,l := range lines { 253before ,after ,found := strings .Cut (l ," = " ) 254if ! found { 255continue 256 } 257assigns = append (assigns ,Assign { 258Dest :before , 259Src :after , 260 }) 261 } 262return assigns 263} 264 265var linkrx = regexp .MustCompile (`<a.*?>` ) 266 267func (is InsnSection )ReadSet () []string { 268lines := strings .Split (is .Code .Ps .PsText .Content ,"\n" ) 269var set []string 270for _ ,l := range lines { 271if strings .Contains (l ,"impl-aarch64.X.read.2" ) { 272l = strings .ReplaceAll (l ,"</a>" ,"" ) 273l = linkrx .ReplaceAllLiteralString (l ,"" ) 274_ ,after ,found := strings .Cut (l ,"=" ) 275if ! found { 276log .Fatal ("no = in " ,l ) 277 } 278set = append (set ,strings .TrimSpace (after )) 279 } 280 } 281return set 282} 283 284func (is InsnSection )Uses (op string )bool { 285lines := strings .Split (is .Code .Ps .PsText .Content ,"\n" ) 286for _ ,l := range lines { 287if strings .Contains (l ,op ) { 288return true 289 } 290 } 291return false 292} 293 294func (is InsnSection )UsesPc ()bool { 295return is .Uses ("impl-aarch64.PC.read.0" ) 296} 297 298func (is InsnSection )ReadsMem ()bool { 299return is .Uses ("impl-aarch64.Mem.read.3" ) 300} 301 302func (is InsnSection )WritesMem ()bool { 303return is .Uses ("impl-aarch64.Mem.write.3" ) 304} 305 306func (is InsnSection )MemAtomic ()bool { 307return is .Uses ("impl-aarch64.MemAtomic.4" ) 308} 309 310func (is InsnSection )IsBranch ()bool { 311return is .Uses ("impl-shared.BranchTo.3" ) 312} 313 314func (is InsnSection )BaseVariant ()bool { 315for _ ,c := range is .Classes .IClass { 316if len (c .ArchVariants .Variants )!= 0 { 317return false 318 } 319 } 320return true 321} 322 323func (is InsnSection )Variant (version string )bool { 324for _ ,c := range is .Classes .IClass { 325for _ ,v := range c .ArchVariants .Variants { 326if v .Name == version { 327return true 328 } 329 } 330 } 331return false 332} 333 334func (is InsnSection )VariantLE (version string )bool { 335for _ ,c := range is .Classes .IClass { 336if len (c .ArchVariants .Variants )== 0 { 337return true 338 } 339for _ ,v := range c .ArchVariants .Variants { 340if strings .Compare (v .Name ,version )<= 0 { 341return true 342 } 343 } 344 } 345return false 346} 347 348func (is InsnSection )HasClass (class string )bool { 349if class == "all" { 350return true 351 } 352for _ ,c := range is .Classes .IClass { 353if c .Docs .InstrClass ()== class { 354return true 355 } 356 } 357if is .Docs .InstrClass ()!= "" && is .Docs .InstrClass ()== class { 358return true 359 } 360return false 361} 362 363func (is InsnSection )GetClasses () []string { 364m := make (map [string ]bool ) 365for _ ,c := range is .Classes .IClass { 366if c .Docs .InstrClass ()!= "" { 367m [c .Docs .InstrClass ()]= true 368 } 369 } 370if is .Docs .InstrClass ()!= "" { 371m [is .Docs .InstrClass ()]= true 372 } 373var classes []string 374for k := range m { 375classes = append (classes ,k ) 376 } 377return classes 378} 379 380func (is InsnSection )BaseArch ()bool { 381if ! strings .Contains (InstrBase ,is .Docs .InstrClass ()) { 382return false 383 } 384return is .BaseVariant () 385} 386 387func (is InsnSection )MatchesArch (name string ,feature string )bool { 388for _ ,c := range is .Classes .IClass { 389for _ ,v := range c .ArchVariants .Variants { 390if strings .Contains (v .Name ,name )&& strings .Contains (v .Feature ,feature ) { 391return true 392 } 393 } 394 } 395return false 396} 397 398func (is InsnSection )WriteSet () []string { 399lines := strings .Split (is .Code .Ps .PsText .Content ,"\n" ) 400set := make (map [string ]bool ) 401for _ ,l := range lines { 402if strings .Contains (l ,"impl-aarch64.X.write.2" ) { 403l = strings .ReplaceAll (l ,"</a>" ,"" ) 404l = linkrx .ReplaceAllLiteralString (l ,"" ) 405before ,_ ,found := strings .Cut (l ,"=" ) 406if ! found { 407log .Fatal ("no = in " ,l ) 408 } 409set [strings .TrimSpace (before )]= true 410 } 411 } 412var ret []string 413for k := range set { 414ret = append (ret ,k ) 415 } 416return ret 417} 418 419func (is InsnSection )Names () []string { 420set := make (map [string ]bool ) 421for _ ,c := range is .Classes .IClass { 422for _ ,e := range c .Encodings { 423set [e .Docs .Mnemonic ()]= true 424 } 425 } 426var names []string 427for k := range set { 428names = append (names ,k ) 429 } 430return names 431} 432 433type Record struct { 434File string 435Name string 436IClass string 437Path string 438Variants string 439Features string 440InstrClass string 441RegDiagram string 442} 443 444func NewRecords (file string ,insn InsnSection ) []Record { 445var records []Record 446for _ ,c := range insn .Classes .IClass { 447set := make (map [string ]bool ) 448for _ ,e := range c .Encodings { 449set [e .Docs .Mnemonic ()]= true 450 } 451var names []string 452for k := range set { 453names = append (names ,k ) 454 } 455records = append (records ,Record { 456File :file , 457Name :strings .Join (names ,";" ), 458IClass :c .Id , 459Path :c .RegDiagram .Name , 460Variants :strings .Join (c .ArchVariants .GetVariants (),";" ), 461Features :strings .Join (c .ArchVariants .GetFeatures (),";" ), 462InstrClass :c .Docs .InstrClass (), 463RegDiagram :c .RegDiagram .String (), 464 }) 465 } 466return records 467}