blob: 3571cbe194d39e996d68ace333f2200a4f1a2f3e [file] [log] [blame]
Peter Collingbournead9841e2014-11-27 00:06:42 +00001//===- targets.go - target data -------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains functions for retrieving target-specific data.
11//
12//===----------------------------------------------------------------------===//
13
14package irgen
15
16import (
17 "fmt"
18 "strings"
19
20 "llvm.org/llvm/bindings/go/llvm"
21)
22
Peter Collingbournead9841e2014-11-27 00:06:42 +000023// llvmDataLayout returns the data layout string
24// representation for the specified LLVM triple.
25func llvmDataLayout(triple string) (string, error) {
26 // Triples are several fields separated by '-' characters.
27 // The first field is the architecture. The architecture's
28 // canonical form may include a '-' character, which would
29 // have been translated to '_' for inclusion in a triple.
30 arch := parseArch(triple[:strings.IndexRune(triple, '-')])
Peter Collingbournead9841e2014-11-27 00:06:42 +000031 for target := llvm.FirstTarget(); target.C != nil; target = target.NextTarget() {
32 if arch == target.Name() {
33 machine := target.CreateTargetMachine(
34 triple, "", "",
35 llvm.CodeGenLevelDefault,
36 llvm.RelocDefault,
37 llvm.CodeModelDefault,
38 )
39 target := machine.TargetData().String()
40 machine.Dispose()
41 return target, nil
42 }
43 }
44 return "", fmt.Errorf("Invalid target triple: %s", triple)
45}
46
47// Based on parseArch from LLVM's lib/Support/Triple.cpp.
48// This is used to match the target machine type.
49func parseArch(arch string) string {
50 switch arch {
51 case "i386", "i486", "i586", "i686", "i786", "i886", "i986":
52 return "x86"
53 case "amd64", "x86_64":
54 return "x86-64"
55 case "powerpc":
56 return "ppc"
57 case "powerpc64", "ppu":
58 return "ppc64"
59 case "mblaze":
60 return "mblaze"
61 case "arm", "xscale":
62 return "arm"
63 case "thumb":
64 return "thumb"
65 case "spu", "cellspu":
66 return "cellspu"
67 case "msp430":
68 return "msp430"
69 case "mips", "mipseb", "mipsallegrex":
70 return "mips"
71 case "mipsel", "mipsallegrexel":
72 return "mipsel"
73 case "mips64", "mips64eb":
74 return "mips64"
75 case "mipsel64":
76 return "mipsel64"
77 case "r600", "hexagon", "sparc", "sparcv9", "tce",
78 "xcore", "nvptx", "nvptx64", "le32", "amdil":
79 return arch
80 }
81 if strings.HasPrefix(arch, "armv") {
82 return "arm"
83 } else if strings.HasPrefix(arch, "thumbv") {
84 return "thumb"
85 }
86 return "unknown"
87}