├── .gitignore ├── LICENSE ├── README.md ├── asm2plan9s.go ├── asm2plan9s_amd64.go ├── asm2plan9s_amd64_test.go ├── asm2plan9s_arm64.go ├── example.s ├── neon.asm └── yasm.go /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | asm2plan9s 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | asm2plan9s 3 | ========== 4 | 5 | Tool to generate BYTE sequences for Go assembly as generated by YASM/GAS (for Intel) or GAS (for ARM). 6 | 7 | Installation 8 | ------------ 9 | 10 | Make sure either YASM or GAS is installed on your platform. Note that YASM takes precedence over GAS if both are installed. 11 | 12 | ##### GAS (Intel/AMD64 or ARM): 13 | 14 | Typically `as` or `gas` (GNU Assembler) will already be installed as it is part of binutils, but if need be, you can eg. do as follows: 15 | ``` 16 | sudo apt-get install build-essential 17 | ``` 18 | 19 | ##### YASM (Intel/AMD64): 20 | 21 | ``` 22 | $ yasm --version 23 | yasm 1.2.0 24 | Compiled on Sep 30 2013. 25 | Copyright (c) 2001-2011 Peter Johnson and other Yasm developers. 26 | Run yasm --license for licensing overview and summary. 27 | ``` 28 | 29 | ### asm2plan9s 30 | 31 | `go get -u github.com/minio/asm2plan9s` 32 | 33 | ### AVX512 support 34 | 35 | Note that AVX512 support is currently only available for GAS. 36 | 37 | Example 38 | ------- 39 | 40 | ``` 41 | $ more example.s 42 | // VPADDQ XMM0,XMM1,XMM8 43 | $ asm2plan9s example.s 44 | $ echo example.s 45 | LONG $0xd471c1c4; BYTE $0xc0 // VPADDQ XMM0,XMM1,XMM8 46 | ``` 47 | 48 | The instruction to be assembled needs to start with a `//` preceded by either a single space or a tab character. 49 | The preceding characters will be overwitten by the correct sequence (irrespective of its contents) so when changing the instruction, rerunning `asm2plan9s` will update the BYTE sequence generated. 50 | 51 | Starting position of instruction 52 | -------------------------------- 53 | 54 | The starting position of the `//` comment needs to follow the (imaginary) sequence with either a single space or a space followed by a back slash plus another space (see support for defines below). 55 | Upon first entering an instruction you can also type eg `LONG $0x00000000; BYTE $0x00 // VZEROUPPER` to trigger the assembler. 56 | 57 | Support for defines 58 | ------------------- 59 | 60 | If you are using #define for 'macros' with the back-slash delimiter to continue on the next line, this will be preserved. 61 | 62 | For instance: 63 | ``` 64 | \ // VPADDQ XMM0,XMM1,XMM8 65 | ``` 66 | 67 | will be assembled into 68 | 69 | ``` 70 | LONG $0xd471c1c4; BYTE $0xc0 \ // VPADDQ XMM0,XMM1,XMM8 71 | ``` 72 | 73 | asmfmt 74 | ------ 75 | 76 | asm2plan9s works nicely together with [asmfmt](https://github.com/klauspost/asmfmt) in order to format the assembly code (in a similar style to `go fmt`). 77 | 78 | Extensive example 79 | ----------------- 80 | 81 | For a more extensive example see (for Intel) [compressAvx_amd64.s](https://github.com/minio/blake2b-simd/blob/master/compressAvx_amd64.s) or (for ARM) [highwayhash_arm64.s](https://github.com/minio/highwayhash/blob/master/highwayhash_arm64.s). 82 | 83 | License 84 | ------- 85 | 86 | Released under the Apache License v2.0. You can find the complete text in the file LICENSE. 87 | 88 | Contributing 89 | ------------ 90 | 91 | Contributions are welcome, please send PRs for any enhancements. -------------------------------------------------------------------------------- /asm2plan9s.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Minio Cloud Storage, (C) 2016-2017 Minio, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "bufio" 21 | "fmt" 22 | "io" 23 | "log" 24 | "os" 25 | "strings" 26 | ) 27 | 28 | type Instruction struct { 29 | instruction string 30 | lineno int 31 | commentPos int 32 | inDefine bool 33 | assembled string 34 | opcodes []byte 35 | } 36 | 37 | type Assembler struct { 38 | Prescan bool 39 | Instructions []Instruction 40 | Compact bool 41 | } 42 | 43 | // assemble assembles an array of lines into their 44 | // resulting plan9 equivalents 45 | func (a *Assembler) assemble(lines []string) ([]string, error) { 46 | 47 | result := make([]string, 0) 48 | 49 | for lineno, line := range lines { 50 | startsWithTab := strings.HasPrefix(line, "\t") 51 | line := strings.Replace(line, "\t", " ", -1) 52 | fields := strings.Split(line, "//") 53 | if len(fields) == 2 && (startsAfterLongWordByteSequence(fields[0]) || len(fields[0]) == 65) { 54 | 55 | // test whether string before instruction is terminated with a backslash (so used in a #define) 56 | trimmed := strings.TrimSpace(fields[0]) 57 | inDefine := len(trimmed) > 0 && string(trimmed[len(trimmed)-1]) == `\` 58 | 59 | // While prescanning collect the instructions 60 | if a.Prescan { 61 | ins := Instruction{instruction: fields[1], lineno: lineno, commentPos: len(fields[0]), inDefine: inDefine} 62 | a.Instructions = append(a.Instructions, ins) 63 | continue 64 | } 65 | 66 | var ins *Instruction 67 | for i := range a.Instructions { 68 | if lineno == a.Instructions[i].lineno { 69 | ins = &a.Instructions[i] 70 | } 71 | } 72 | if ins == nil { 73 | if a.Compact { 74 | continue 75 | } 76 | panic("failed to find entry with correct line number") 77 | } 78 | if startsWithTab { 79 | ins.assembled = strings.Replace(ins.assembled, " ", "\t", 1) 80 | } 81 | result = append(result, ins.assembled) 82 | } else if !a.Prescan { 83 | if startsWithTab { 84 | line = strings.Replace(line, " ", "\t", 1) 85 | } 86 | result = append(result, line) 87 | } 88 | } 89 | 90 | return result, nil 91 | } 92 | 93 | // startsAfterLongWordByteSequence determines if an assembly instruction 94 | // starts on a position after a combination of LONG, WORD, BYTE sequences 95 | func startsAfterLongWordByteSequence(prefix string) bool { 96 | 97 | if len(strings.TrimSpace(prefix)) != 0 && !strings.HasPrefix(prefix, " LONG $0x") && 98 | !strings.HasPrefix(prefix, " WORD $0x") && !strings.HasPrefix(prefix, " BYTE $0x") { 99 | return false 100 | } 101 | 102 | length := 4 + len(prefix) + 1 103 | 104 | for objcodes := 3; objcodes <= 8; objcodes++ { 105 | 106 | ls, ws, bs := 0, 0, 0 107 | 108 | oc := objcodes 109 | 110 | for ; oc >= 4; oc -= 4 { 111 | ls++ 112 | } 113 | if oc >= 2 { 114 | ws++ 115 | oc -= 2 116 | } 117 | if oc == 1 { 118 | bs++ 119 | } 120 | size := 4 + ls*(len("LONG $0x")+8) + ws*(len("WORD $0x")+4) + bs*(len("BYTE $0x")+2) + (ls+ws+bs-1)*len("; ") 121 | 122 | if length == size+6 { // comment starts after a space 123 | return true 124 | } 125 | } 126 | return false 127 | } 128 | 129 | // combineLines shortens the output by combining consecutive lines into a larger list of opcodes 130 | func (a *Assembler) combineLines() { 131 | startIndex, startLine, opcodes := -1, -1, make([]byte, 0, 1024) 132 | combined := make([]Instruction, 0, 100) 133 | for i, ins := range a.Instructions { 134 | if startIndex == -1 { 135 | startIndex, startLine = i, ins.lineno 136 | } 137 | if ins.lineno != startLine+(i-startIndex) { // we have found a non-consecutive line 138 | combiAssem, _ := toPlan9s(opcodes, "", 0, false) 139 | combiIns := Instruction{assembled: combiAssem, lineno: startLine, inDefine: false} 140 | 141 | combined = append(combined, combiIns) 142 | opcodes = opcodes[:0] 143 | startIndex, startLine = i, ins.lineno 144 | } 145 | opcodes = append(opcodes, ins.opcodes...) 146 | } 147 | if len(opcodes) > 0 { 148 | combiAssem, _ := toPlan9s(opcodes, "", 0, false) 149 | ins := Instruction{assembled: combiAssem, lineno: startLine, inDefine: false} 150 | 151 | combined = append(combined, ins) 152 | } 153 | 154 | a.Instructions = combined 155 | } 156 | 157 | // readLines reads a whole file into memory 158 | // and returns a slice of its lines. 159 | func readLines(path string, in io.Reader) ([]string, error) { 160 | if in == nil { 161 | file, err := os.Open(path) 162 | if err != nil { 163 | return nil, err 164 | } 165 | defer file.Close() 166 | in = file 167 | } 168 | 169 | var lines []string 170 | scanner := bufio.NewScanner(in) 171 | for scanner.Scan() { 172 | lines = append(lines, scanner.Text()) 173 | } 174 | return lines, scanner.Err() 175 | } 176 | 177 | // writeLines writes the lines to the given file. 178 | func writeLines(lines []string, path string, out io.Writer) error { 179 | if path != "" { 180 | file, err := os.Create(path) 181 | if err != nil { 182 | return err 183 | } 184 | defer file.Close() 185 | out = file 186 | } 187 | 188 | w := bufio.NewWriter(out) 189 | for _, line := range lines { 190 | fmt.Fprintln(w, line) 191 | } 192 | return w.Flush() 193 | } 194 | 195 | func assemble(lines []string, compact bool) (result []string, err error) { 196 | 197 | // TODO: Make compaction configurable 198 | a := Assembler{Prescan: true, Compact: compact} 199 | 200 | _, err = a.assemble(lines) 201 | if err != nil { 202 | return result, err 203 | } 204 | 205 | err = as(a.Instructions) 206 | if err != nil { 207 | return result, err 208 | } 209 | 210 | if a.Compact { 211 | a.combineLines() 212 | } 213 | 214 | a.Prescan = false 215 | result, err = a.assemble(lines) 216 | if err != nil { 217 | return result, err 218 | } 219 | 220 | return result, nil 221 | } 222 | 223 | func main() { 224 | 225 | file := "" 226 | if len(os.Args) >= 2 { 227 | file = os.Args[1] 228 | } 229 | 230 | var lines []string 231 | var err error 232 | if len(file) > 0 { 233 | fmt.Println("Processing file", file) 234 | lines, err = readLines(file, nil) 235 | } else { 236 | lines, err = readLines("", os.Stdin) 237 | } 238 | if err != nil { 239 | log.Fatalf("readLines: %s", err) 240 | } 241 | 242 | result, err := assemble(lines, false) 243 | if err != nil { 244 | fmt.Print(err) 245 | os.Exit(-1) 246 | } 247 | 248 | err = writeLines(result, file, os.Stdout) 249 | if err != nil { 250 | log.Fatalf("writeLines: %s", err) 251 | } 252 | } 253 | -------------------------------------------------------------------------------- /asm2plan9s_amd64.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Minio Cloud Storage, (C) 2016-2017 Minio, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "encoding/hex" 21 | "errors" 22 | "fmt" 23 | "io/ioutil" 24 | "os" 25 | "os/exec" 26 | "regexp" 27 | "strconv" 28 | "strings" 29 | ) 30 | 31 | // as: assemble instruction by either invoking yasm or gas 32 | func as(instructions []Instruction) error { 33 | 34 | // First to yasm (will return error when not installed) 35 | e := yasm(instructions) 36 | if e == nil { 37 | return e 38 | } 39 | // Try gas if yasm not installed 40 | return gas(instructions) 41 | } 42 | 43 | // See below for YASM support (older, no AVX512) 44 | 45 | /////////////////////////////////////////////////////////////////////////////// 46 | // 47 | // G A S S U P P O R T 48 | // 49 | /////////////////////////////////////////////////////////////////////////////// 50 | 51 | // 52 | // frank@hemelmeer: asm2plan9s$ more example.s 53 | // .intel_syntax noprefix 54 | // 55 | // VPANDQ ZMM0, ZMM1, ZMM2 56 | // 57 | // frank@hemelmeer: asm2plan9s$ as -o example.o -al=example.lis example.s 58 | // frank@hemelmeer: asm2plan9s$ more example.lis 59 | // GAS LISTING example.s page 1 60 | // 1 .intel_syntax noprefix 61 | // 2 62 | // 3 0000 62F1F548 VPANDQ ZMM0, ZMM1, ZMM2 63 | // 3 DBC2 64 | // 65 | 66 | func gas(instructions []Instruction) error { 67 | 68 | tmpfile, err := ioutil.TempFile("", "asm2plan9s") 69 | if err != nil { 70 | return err 71 | } 72 | if _, err := tmpfile.Write([]byte(fmt.Sprintf(".intel_syntax noprefix\n"))); err != nil { 73 | return err 74 | } 75 | 76 | for _, instr := range instructions { 77 | instrFields := strings.Split(instr.instruction, "/*") 78 | if len(instrFields) == 1 { 79 | instrFields = strings.Split(instr.instruction, ";") // try again with ; separator 80 | } 81 | content := []byte(instrFields[0] + "\n") 82 | 83 | if _, err := tmpfile.Write([]byte(content)); err != nil { 84 | return err 85 | } 86 | } 87 | 88 | if err := tmpfile.Close(); err != nil { 89 | return err 90 | } 91 | 92 | asmFile := tmpfile.Name() + ".asm" 93 | lisFile := tmpfile.Name() + ".lis" 94 | objFile := tmpfile.Name() + ".obj" 95 | os.Rename(tmpfile.Name(), asmFile) 96 | 97 | defer os.Remove(asmFile) // clean up 98 | defer os.Remove(lisFile) // clean up 99 | defer os.Remove(objFile) // clean up 100 | 101 | // as -o example.o -al=example.lis example.s 102 | app := "as" 103 | 104 | arg0 := "-o" 105 | arg1 := objFile 106 | arg2 := fmt.Sprintf("-aln=%s", lisFile) 107 | arg3 := asmFile 108 | 109 | cmd := exec.Command(app, arg0, arg1, arg2, arg3) 110 | cmb, err := cmd.CombinedOutput() 111 | if err != nil { 112 | asmErrs := strings.Split(string(cmb)[len(asmFile)+1:], ":") 113 | asmErr := strings.Join(asmErrs[1:], ":") 114 | // TODO: Fix proper error reporting 115 | lineno := -1 116 | instr := "TODO: fix" 117 | return errors.New(fmt.Sprintf("GAS error (line %d for '%s'):", lineno+1, strings.TrimSpace(instr)) + asmErr) 118 | } 119 | 120 | opcodes, err := toPlan9sGas(lisFile) 121 | if err != nil { 122 | return err 123 | } 124 | 125 | if len(instructions) != len(opcodes) { 126 | panic("Unequal length between instructions to be assembled and opcodes returned") 127 | } 128 | 129 | for i, opcode := range opcodes { 130 | assembled, err := toPlan9s(opcode, instructions[i].instruction, instructions[i].commentPos, instructions[i].inDefine) 131 | if err != nil { 132 | return err 133 | } 134 | instructions[i].assembled = assembled 135 | instructions[i].opcodes = make([]byte, len(opcode)) 136 | copy(instructions[i].opcodes, opcode) 137 | } 138 | 139 | return nil 140 | } 141 | 142 | func toPlan9sGas(listFile string) ([][]byte, error) { 143 | 144 | opcodes := make([][]byte, 0, 10) 145 | 146 | outputLines, err := readLines(listFile, nil) 147 | if err != nil { 148 | return opcodes, err 149 | } 150 | 151 | var regexpHeader = regexp.MustCompile(`^\s+(\d+)\s+[0-9a-fA-F]+\s+([0-9a-fA-F]+)`) 152 | var regexpSequel = regexp.MustCompile(`^\s+(\d+)\s+([0-9a-fA-F]+)`) 153 | 154 | lineno, opcode := -1, make([]byte, 0, 10) 155 | 156 | for _, line := range outputLines { 157 | 158 | if match := regexpHeader.FindStringSubmatch(line); len(match) > 2 { 159 | l, e := strconv.Atoi(match[1]) 160 | if e != nil { 161 | panic(e) 162 | } 163 | if lineno != -1 { 164 | opcodes = append(opcodes, opcode) 165 | } 166 | lineno = l 167 | opcode = make([]byte, 0, 10) 168 | b, e := hex.DecodeString(match[2]) 169 | if e != nil { 170 | panic(e) 171 | } 172 | opcode = append(opcode, b...) 173 | } else if match := regexpSequel.FindStringSubmatch(line); len(match) > 2 { 174 | l, e := strconv.Atoi(match[1]) 175 | if e != nil { 176 | panic(e) 177 | } 178 | if l != lineno { 179 | panic("bad line number)") 180 | } 181 | b, e := hex.DecodeString(match[2]) 182 | if e != nil { 183 | panic(e) 184 | } 185 | opcode = append(opcode, b...) 186 | } 187 | } 188 | 189 | opcodes = append(opcodes, opcode) 190 | 191 | return opcodes, nil 192 | } 193 | -------------------------------------------------------------------------------- /asm2plan9s_amd64_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Minio Cloud Storage, (C) 2016-2017 Minio, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "bytes" 21 | "io/ioutil" 22 | "os" 23 | "testing" 24 | ) 25 | 26 | func TestInstruction(t *testing.T) { 27 | 28 | ins := " // VPADDQ XMM0,XMM1,XMM8" 29 | out := " LONG $0xd471c1c4; BYTE $0xc0 // VPADDQ XMM0,XMM1,XMM8" 30 | 31 | result, _ := assemble([]string{ins}, false) 32 | 33 | if result[0] != out { 34 | t.Errorf("expected %s\ngot %s", out, result[0]) 35 | } 36 | } 37 | 38 | func TestInstructionPresent(t *testing.T) { 39 | 40 | ins := " LONG $0xd471c1c4; BYTE $0xc0 // VPADDQ XMM0,XMM1,XMM8" 41 | out := " LONG $0xd471c1c4; BYTE $0xc0 // VPADDQ XMM0,XMM1,XMM8" 42 | 43 | result, _ := assemble([]string{ins}, false) 44 | 45 | if result[0] != out { 46 | t.Errorf("expected %s\ngot %s", out, result[0]) 47 | } 48 | } 49 | 50 | func TestInstructionWrongBytes(t *testing.T) { 51 | 52 | ins := " LONG $0x003377bb; BYTE $0xff // VPADDQ XMM0,XMM1,XMM8" 53 | out := " LONG $0xd471c1c4; BYTE $0xc0 // VPADDQ XMM0,XMM1,XMM8" 54 | 55 | result, _ := assemble([]string{ins}, false) 56 | 57 | if result[0] != out { 58 | t.Errorf("expected %s\ngot %s", out, result[0]) 59 | } 60 | } 61 | 62 | func TestInstructionInDefine(t *testing.T) { 63 | 64 | ins := ` LONG $0x00000000; BYTE $0xdd \ // VPADDQ XMM0,XMM1,XMM8` 65 | out := ` LONG $0xd471c1c4; BYTE $0xc0 \ // VPADDQ XMM0,XMM1,XMM8` 66 | 67 | result, _ := assemble([]string{ins}, false) 68 | 69 | if result[0] != out { 70 | t.Errorf("expected %s\ngot %s", out, result[0]) 71 | } 72 | } 73 | 74 | func TestCompactMultipleInstructions(t *testing.T) { 75 | 76 | ins1 := " // VPADDQ XMM0,XMM1,XMM8" 77 | ins2 := " // VPADDQ XMM1,XMM2,XMM3" 78 | ins3 := " // VPADDQ XMM4,XMM5,XMM6" 79 | ins4 := " // VPADDQ XMM4,XMM5,XMM6" 80 | ins5 := " // VPADDQ XMM4,XMM5,XMM6" 81 | ins6 := " // VPADDQ XMM4,XMM5,XMM6" 82 | ins7 := " // VPADDQ XMM4,XMM5,XMM6" 83 | ins8 := " MOVQ AX, BX" 84 | ins9 := " // VPADDQ XMM0,XMM1,XMM8" 85 | ins10 := " // VPADDQ XMM1,XMM2,XMM3" 86 | ins11 := " MOVQ BX, CX" 87 | ins12 := " // VPADDQ XMM4,XMM5,XMM6" 88 | ins13 := " // VPADDQ XMM5,XMM6,XMM0" 89 | out0 := " QUAD $0xd4e9c5c0d471c1c4; QUAD $0xd4d1c5e6d4d1c5cb; QUAD $0xd4d1c5e6d4d1c5e6; QUAD $0x71c1c4e6d4d1c5e6; WORD $0xc0d4" 90 | out1 := " MOVQ AX, BX" 91 | out2 := " QUAD $0xe6d4d1c5cbd4e9c5" 92 | out3 := " MOVQ BX, CX" 93 | out4 := " LONG $0xe8d4c9c5" 94 | out := make([]string, 5) 95 | out[0], out[1], out[2], out[3], out[4] = out0, out1, out2, out3, out4 96 | 97 | result, _ := assemble([]string{ins1, ins2, ins3, ins4, ins5, ins6, ins7, ins8, ins9, ins10, ins11, ins12, ins13}, true) 98 | if len(result) != len(out) { 99 | t.Errorf("expected length %d\ngot length %d", len(out), len(result)) 100 | } 101 | for i := range result { 102 | if result[i] != out[i] { 103 | t.Errorf("expected %s\ngot %s", out[i], result[i]) 104 | } 105 | } 106 | } 107 | 108 | func TestLongInstruction(t *testing.T) { 109 | 110 | ins := " // VPALIGNR XMM8, XMM12, XMM12, 0x8" 111 | out := " LONG $0x0f1943c4; WORD $0x08c4 // VPALIGNR XMM8, XMM12, XMM12, 0x8" 112 | 113 | result, _ := assemble([]string{ins}, false) 114 | 115 | if result[0] != out { 116 | t.Errorf("expected %s\ngot %s", out, result[0]) 117 | } 118 | } 119 | 120 | func TestToPlan9sGasSingleLineEVEX(t *testing.T) { 121 | 122 | ins := `1 .intel_syntax noprefix 123 | 2 0000 62D1F548 VPADDQ ZMM0,ZMM1,ZMM8 124 | 2 D4C0 125 | 3 ` 126 | 127 | out := make([][]byte, 1, 1) 128 | out[0] = []byte{98, 209, 245, 72, 212, 192} 129 | 130 | tmpfile, err := ioutil.TempFile("", "test") 131 | if err != nil { 132 | return 133 | } 134 | 135 | if _, err := tmpfile.Write([]byte(ins)); err != nil { 136 | return 137 | } 138 | if err := tmpfile.Close(); err != nil { 139 | return 140 | } 141 | defer os.Remove(tmpfile.Name()) // clean up 142 | 143 | result, _ := toPlan9sGas(tmpfile.Name()) 144 | if len(result) != len(out) || !bytes.Equal(result[0], out[0]) { 145 | t.Errorf("expected %v\ngot %v", out, result) 146 | } 147 | } 148 | 149 | func TestToPlan9sGasMultiLinesVEX(t *testing.T) { 150 | 151 | ins := ` 1 .intel_syntax noprefix 152 | 2 0000 C4C171D4 VPADDQ XMM0,XMM1,XMM8 153 | 2 C0 154 | 3 0005 C4C169D4 VPADDQ XMM1,XMM2,XMM9 155 | 3 C9 156 | 4 000a C4C161D4 VPADDQ XMM2,XMM3,XMM10 157 | 4 D2 158 | 5 ` 159 | 160 | out := make([][]byte, 3, 3) 161 | out[0] = []byte{196, 193, 113, 212, 192} 162 | out[1] = []byte{196, 193, 105, 212, 201} 163 | out[2] = []byte{196, 193, 97, 212, 210} 164 | 165 | tmpfile, err := ioutil.TempFile("", "test") 166 | if err != nil { 167 | return 168 | } 169 | 170 | if _, err := tmpfile.Write([]byte(ins)); err != nil { 171 | return 172 | } 173 | if err := tmpfile.Close(); err != nil { 174 | return 175 | } 176 | defer os.Remove(tmpfile.Name()) // clean up 177 | 178 | result, _ := toPlan9sGas(tmpfile.Name()) 179 | if len(result) != len(out) || !bytes.Equal(result[0], out[0]) || 180 | !bytes.Equal(result[1], out[1]) || !bytes.Equal(result[2], out[2]) { 181 | t.Errorf("expected %v\ngot %v", out, result) 182 | } 183 | } 184 | -------------------------------------------------------------------------------- /asm2plan9s_arm64.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Minio Cloud Storage, (C) 2016-2017 Minio, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "errors" 21 | "fmt" 22 | "io/ioutil" 23 | "os" 24 | "os/exec" 25 | "regexp" 26 | "strings" 27 | ) 28 | 29 | func as(instructions []Instruction) error { 30 | 31 | // First to yasm (will return error when not installed) 32 | e := yasm(instructions) 33 | if e == nil { 34 | return e 35 | } 36 | // Try gas if yasm not installed 37 | return gas(instructions) 38 | } 39 | 40 | func gas(instructions []Instruction) error { 41 | for i, ins := range instructions { 42 | assembled, opcodes, err := asSingle(ins.instruction, ins.lineno, ins.commentPos, ins.inDefine) 43 | if err != nil { 44 | return err 45 | } 46 | instructions[i].assembled = assembled 47 | instructions[i].opcodes = make([]byte, len(opcodes)) 48 | copy(instructions[i].opcodes[:], opcodes) 49 | } 50 | return nil 51 | } 52 | 53 | func asSingle(instr string, lineno, commentPos int, inDefine bool) (string, []byte, error) { 54 | 55 | instrFields := strings.Split(instr, "/*") 56 | content := []byte(instrFields[0] + "\n") 57 | tmpfile, err := ioutil.TempFile("", "asm2plan9s") 58 | if err != nil { 59 | return "", nil, err 60 | } 61 | 62 | if _, err := tmpfile.Write(content); err != nil { 63 | return "", nil, err 64 | } 65 | if err := tmpfile.Close(); err != nil { 66 | return "", nil, err 67 | } 68 | 69 | asmFile := tmpfile.Name() + ".asm" 70 | lisFile := tmpfile.Name() + ".lis" 71 | objFile := tmpfile.Name() + ".obj" 72 | os.Rename(tmpfile.Name(), asmFile) 73 | 74 | defer os.Remove(asmFile) // clean up 75 | defer os.Remove(lisFile) // clean up 76 | defer os.Remove(objFile) // clean up 77 | 78 | // as -march=armv8-a+crypto -o first.out -al=first.lis first.s 79 | app := "as" 80 | 81 | arg0 := "-march=armv8-a+crypto" // See https://gcc.gnu.org/onlinedocs/gcc-4.9.1/gcc/ARM-Options.html 82 | arg1 := "-o" 83 | arg2 := objFile 84 | arg3 := fmt.Sprintf("-al=%s", lisFile) 85 | arg4 := asmFile 86 | 87 | cmd := exec.Command(app, arg0, arg1, arg2, arg3, arg4) 88 | cmb, err := cmd.CombinedOutput() 89 | if err != nil { 90 | asmErrs := strings.Split(string(cmb)[len(asmFile)+1:], ":") 91 | asmErr := strings.Join(asmErrs[1:], ":") 92 | return "", nil, errors.New(fmt.Sprintf("GAS error (line %d for '%s'):", lineno+1, strings.TrimSpace(instr)) + asmErr) 93 | } 94 | 95 | return toPlan9sArm(lisFile, instr) 96 | } 97 | 98 | func toPlan9sArm(listFile, instr string) (string, []byte, error) { 99 | 100 | var r = regexp.MustCompile(`^\s+\d+\s+\d+\s+([0-9a-fA-F]+)`) 101 | 102 | outputLines, err := readLines(listFile, nil) 103 | if err != nil { 104 | return "", nil, err 105 | } 106 | 107 | lastLine := outputLines[len(outputLines)-1] 108 | 109 | sline := " " 110 | 111 | if match := r.FindStringSubmatch(lastLine); len(match) > 1 { 112 | sline += fmt.Sprintf("WORD $0x%s%s%s%s", strings.ToLower(match[1][6:8]), strings.ToLower(match[1][4:6]), strings.ToLower(match[1][2:4]), strings.ToLower(match[1][0:2])) 113 | } else { 114 | return "", nil, errors.New("regexp failed") 115 | } 116 | 117 | sline += " //" + instr 118 | 119 | // fmt.Println(sline) 120 | 121 | return sline, nil, nil 122 | } 123 | -------------------------------------------------------------------------------- /example.s: -------------------------------------------------------------------------------- 1 | // VPADDQ XMM0,XMM1,XMM8 2 | -------------------------------------------------------------------------------- /neon.asm: -------------------------------------------------------------------------------- 1 | WORD $0x00000000 // ld1 {v16.4s-v19.4s}, [x3], #64 2 | -------------------------------------------------------------------------------- /yasm.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "io/ioutil" 7 | "os" 8 | "os/exec" 9 | "strings" 10 | "unicode" 11 | ) 12 | 13 | /////////////////////////////////////////////////////////////////////////////// 14 | // 15 | // Y A S M S U P P O R T 16 | // 17 | /////////////////////////////////////////////////////////////////////////////// 18 | 19 | // 20 | // yasm-assemble-disassemble-roundtrip-sse.txt 21 | // 22 | // franks-mbp:sse frankw$ more assembly.asm 23 | // [bits 64] 24 | // 25 | // VPXOR YMM4, YMM2, YMM3 ; X4: Result 26 | // franks-mbp:sse frankw$ yasm assembly.asm 27 | // franks-mbp:sse frankw$ hexdump -C assembly 28 | // 00000000 c5 ed ef e3 |....| 29 | // 00000004 30 | // franks-mbp:sse frankw$ echo 'lbl: db 0xc5, 0xed, 0xef, 0xe3' | yasm -f elf - -o assembly.o 31 | // franks-mbp:sse frankw$ gobjdump -d -M intel assembly.o 32 | // 33 | // assembly.o: file format elf32-i386 34 | // 35 | // 36 | // Disassembly of section .text: 37 | // 38 | // 00000000 <.text>: 39 | // 0: c5 ed ef e3 vpxor ymm4,ymm2,ymm3 40 | 41 | func yasm(instructions []Instruction) error { 42 | for i, ins := range instructions { 43 | assembled, opcodes, err := yasmSingle(ins.instruction, ins.lineno, ins.commentPos, ins.inDefine) 44 | if err != nil { 45 | return err 46 | } 47 | instructions[i].assembled = assembled 48 | instructions[i].opcodes = make([]byte, len(opcodes)) 49 | copy(instructions[i].opcodes[:], opcodes) 50 | } 51 | return nil 52 | } 53 | 54 | func yasmSingle(instr string, lineno, commentPos int, inDefine bool) (string, []byte, error) { 55 | 56 | instrFields := strings.Split(instr, "/*") 57 | content := []byte("[bits 64]\n" + instrFields[0]) 58 | tmpfile, err := ioutil.TempFile("", "asm2plan9s") 59 | if err != nil { 60 | return "", nil, err 61 | } 62 | 63 | if _, err := tmpfile.Write(content); err != nil { 64 | return "", nil, err 65 | } 66 | if err := tmpfile.Close(); err != nil { 67 | return "", nil, err 68 | } 69 | 70 | asmFile := tmpfile.Name() + ".asm" 71 | objFile := tmpfile.Name() + ".obj" 72 | os.Rename(tmpfile.Name(), asmFile) 73 | 74 | defer os.Remove(asmFile) // clean up 75 | defer os.Remove(objFile) // clean up 76 | 77 | app := "yasm" 78 | 79 | arg0 := "-o" 80 | arg1 := objFile 81 | arg2 := asmFile 82 | 83 | cmd := exec.Command(app, arg0, arg1, arg2) 84 | cmb, err := cmd.CombinedOutput() 85 | if err != nil { 86 | if len(string(cmb)) == 0 { // command invocation failed 87 | return "", nil, errors.New("exec error: YASM not installed?") 88 | } 89 | yasmErrs := strings.Split(string(cmb)[len(asmFile)+1:], ":") 90 | yasmErr := strings.Join(yasmErrs[1:], ":") 91 | return "", nil, errors.New(fmt.Sprintf("YASM error (line %d for '%s'):", lineno+1, strings.TrimSpace(instr)) + yasmErr) 92 | } 93 | 94 | return toPlan9sYasm(objFile, instr, commentPos, inDefine) 95 | } 96 | 97 | func toPlan9sYasm(objFile, instr string, commentPos int, inDefine bool) (string, []byte, error) { 98 | opcodes, err := ioutil.ReadFile(objFile) 99 | if err != nil { 100 | return "", nil, err 101 | } 102 | 103 | s, err := toPlan9s(opcodes, instr, commentPos, inDefine) 104 | return s, opcodes, err 105 | } 106 | 107 | func toPlan9s(opcodes []byte, instr string, commentPos int, inDefine bool) (string, error) { 108 | sline := " " 109 | i := 0 110 | // First do QUADs (as many as needed) 111 | for ; len(opcodes) >= 8; i++ { 112 | if i != 0 { 113 | sline += "; " 114 | } 115 | sline += fmt.Sprintf("QUAD $0x%02x%02x%02x%02x%02x%02x%02x%02x", opcodes[7], opcodes[6], opcodes[5], opcodes[4], opcodes[3], opcodes[2], opcodes[1], opcodes[0]) 116 | 117 | opcodes = opcodes[8:] 118 | } 119 | // Then do LONGs (as many as needed) 120 | for ; len(opcodes) >= 4; i++ { 121 | if i != 0 { 122 | sline += "; " 123 | } 124 | sline += fmt.Sprintf("LONG $0x%02x%02x%02x%02x", opcodes[3], opcodes[2], opcodes[1], opcodes[0]) 125 | 126 | opcodes = opcodes[4:] 127 | } 128 | 129 | // Then do a WORD (if needed) 130 | if len(opcodes) >= 2 { 131 | 132 | if i != 0 { 133 | sline += "; " 134 | } 135 | sline += fmt.Sprintf("WORD $0x%02x%02x", opcodes[1], opcodes[0]) 136 | 137 | i++ 138 | opcodes = opcodes[2:] 139 | } 140 | 141 | // And close with a BYTE (if needed) 142 | if len(opcodes) == 1 { 143 | if i != 0 { 144 | sline += "; " 145 | } 146 | sline += fmt.Sprintf("BYTE $0x%02x", opcodes[0]) 147 | 148 | i++ 149 | opcodes = opcodes[1:] 150 | } 151 | 152 | if inDefine { 153 | if commentPos > commentPos-2-len(sline) { 154 | if commentPos-2-len(sline) > 0 { 155 | sline += strings.Repeat(" ", commentPos-2-len(sline)) 156 | } 157 | } else { 158 | sline += " " 159 | } 160 | sline += `\ ` 161 | } else { 162 | if commentPos > len(sline) { 163 | if commentPos-len(sline) > 0 { 164 | sline += strings.Repeat(" ", commentPos-len(sline)) 165 | } 166 | } else { 167 | sline += " " 168 | } 169 | } 170 | 171 | if instr != "" { 172 | sline += "//" + instr 173 | } 174 | 175 | return strings.TrimRightFunc(sline, unicode.IsSpace), nil 176 | } 177 | --------------------------------------------------------------------------------