github.com/goplus/llgo@v0.8.3/ssa/target.go (about) 1 /* 2 * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved. 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 ssa 18 19 // ----------------------------------------------------------------------------- 20 21 type Target struct { 22 GOOS string 23 GOARCH string 24 GOARM string // "5", "6", "7" (default) 25 } 26 27 /* 28 func (p *Program) targetMachine() llvm.TargetMachine { 29 if p.tm.C == nil { 30 spec := p.target.toSpec() 31 target, err := llvm.GetTargetFromTriple(spec.triple) 32 if err != nil { 33 panic(err) 34 } 35 p.tm = target.CreateTargetMachine( 36 spec.triple, 37 spec.cpu, 38 spec.features, 39 llvm.CodeGenLevelDefault, 40 llvm.RelocDefault, 41 llvm.CodeModelDefault, 42 ) 43 } 44 return p.tm 45 } 46 47 type targetSpec struct { 48 triple string 49 cpu string 50 features string 51 } 52 53 func (p *Target) toSpec() (spec targetSpec) { 54 // Configure based on GOOS/GOARCH environment variables (falling back to 55 // runtime.GOOS/runtime.GOARCH), and generate a LLVM target based on it. 56 var llvmarch string 57 var goarch = p.GOARCH 58 var goos = p.GOOS 59 if goarch == "" { 60 goarch = runtime.GOARCH 61 } 62 if goos == "" { 63 goos = runtime.GOOS 64 } 65 switch goarch { 66 case "386": 67 llvmarch = "i386" 68 case "amd64": 69 llvmarch = "x86_64" 70 case "arm64": 71 llvmarch = "aarch64" 72 case "arm": 73 switch p.GOARM { 74 case "5": 75 llvmarch = "armv5" 76 case "6": 77 llvmarch = "armv6" 78 default: 79 llvmarch = "armv7" 80 } 81 case "wasm": 82 llvmarch = "wasm32" 83 default: 84 llvmarch = goarch 85 } 86 llvmvendor := "unknown" 87 llvmos := goos 88 switch goos { 89 case "darwin": 90 // Use macosx* instead of darwin, otherwise darwin/arm64 will refer 91 // to iOS! 92 llvmos = "macosx10.12.0" 93 if llvmarch == "aarch64" { 94 // Looks like Apple prefers to call this architecture ARM64 95 // instead of AArch64. 96 llvmarch = "arm64" 97 llvmos = "macosx11.0.0" 98 } 99 llvmvendor = "apple" 100 case "wasip1": 101 llvmos = "wasi" 102 } 103 // Target triples (which actually have four components, but are called 104 // triples for historical reasons) have the form: 105 // arch-vendor-os-environment 106 spec.triple = llvmarch + "-" + llvmvendor + "-" + llvmos 107 if llvmos == "windows" { 108 spec.triple += "-gnu" 109 } else if goarch == "arm" { 110 spec.triple += "-gnueabihf" 111 } 112 switch goarch { 113 case "386": 114 spec.cpu = "pentium4" 115 spec.features = "+cx8,+fxsr,+mmx,+sse,+sse2,+x87" 116 case "amd64": 117 spec.cpu = "x86-64" 118 spec.features = "+cx8,+fxsr,+mmx,+sse,+sse2,+x87" 119 case "arm": 120 spec.cpu = "generic" 121 switch llvmarch { 122 case "armv5": 123 spec.features = "+armv5t,+strict-align,-aes,-bf16,-d32,-dotprod,-fp-armv8,-fp-armv8d16,-fp-armv8d16sp,-fp-armv8sp,-fp16,-fp16fml,-fp64,-fpregs,-fullfp16,-mve.fp,-neon,-sha2,-thumb-mode,-vfp2,-vfp2sp,-vfp3,-vfp3d16,-vfp3d16sp,-vfp3sp,-vfp4,-vfp4d16,-vfp4d16sp,-vfp4sp" 124 case "armv6": 125 spec.features = "+armv6,+dsp,+fp64,+strict-align,+vfp2,+vfp2sp,-aes,-d32,-fp-armv8,-fp-armv8d16,-fp-armv8d16sp,-fp-armv8sp,-fp16,-fp16fml,-fullfp16,-neon,-sha2,-thumb-mode,-vfp3,-vfp3d16,-vfp3d16sp,-vfp3sp,-vfp4,-vfp4d16,-vfp4d16sp,-vfp4sp" 126 case "armv7": 127 spec.features = "+armv7-a,+d32,+dsp,+fp64,+neon,+vfp2,+vfp2sp,+vfp3,+vfp3d16,+vfp3d16sp,+vfp3sp,-aes,-fp-armv8,-fp-armv8d16,-fp-armv8d16sp,-fp-armv8sp,-fp16,-fp16fml,-fullfp16,-sha2,-thumb-mode,-vfp4,-vfp4d16,-vfp4d16sp,-vfp4sp" 128 } 129 case "arm64": 130 spec.cpu = "generic" 131 if goos == "darwin" { 132 spec.features = "+neon" 133 } else { // windows, linux 134 spec.features = "+neon,-fmv" 135 } 136 case "wasm": 137 spec.cpu = "generic" 138 spec.features = "+bulk-memory,+mutable-globals,+nontrapping-fptoint,+sign-ext" 139 } 140 return 141 } 142 */ 143 144 // -----------------------------------------------------------------------------