blob: 7715ab1886681c63766212e23bd3ea67e477ca21 [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
23// PNaClTriple is the LLVM target triple that should be used to compile
24// modules to be compatible with PNaCl (Portable Native Client).
25const PNaClTriple = "armv7-none-linux-gnueabi"
26
27// Below are the target data representation constants generated by clang.
28// For unknown targets, we enumerate all targets known to LLVM and use
29// the first one with a matching architecture.
30const (
31 x86TargetData = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
32)
33
34// llvmDataLayout returns the data layout string
35// representation for the specified LLVM triple.
36func llvmDataLayout(triple string) (string, error) {
37 // Triples are several fields separated by '-' characters.
38 // The first field is the architecture. The architecture's
39 // canonical form may include a '-' character, which would
40 // have been translated to '_' for inclusion in a triple.
41 arch := parseArch(triple[:strings.IndexRune(triple, '-')])
42 switch arch {
43 case "x86-64":
44 return x86TargetData, nil
45 }
46 for target := llvm.FirstTarget(); target.C != nil; target = target.NextTarget() {
47 if arch == target.Name() {
48 machine := target.CreateTargetMachine(
49 triple, "", "",
50 llvm.CodeGenLevelDefault,
51 llvm.RelocDefault,
52 llvm.CodeModelDefault,
53 )
54 target := machine.TargetData().String()
55 machine.Dispose()
56 return target, nil
57 }
58 }
59 return "", fmt.Errorf("Invalid target triple: %s", triple)
60}
61
62// Based on parseArch from LLVM's lib/Support/Triple.cpp.
63// This is used to match the target machine type.
64func parseArch(arch string) string {
65 switch arch {
66 case "i386", "i486", "i586", "i686", "i786", "i886", "i986":
67 return "x86"
68 case "amd64", "x86_64":
69 return "x86-64"
70 case "powerpc":
71 return "ppc"
72 case "powerpc64", "ppu":
73 return "ppc64"
74 case "mblaze":
75 return "mblaze"
76 case "arm", "xscale":
77 return "arm"
78 case "thumb":
79 return "thumb"
80 case "spu", "cellspu":
81 return "cellspu"
82 case "msp430":
83 return "msp430"
84 case "mips", "mipseb", "mipsallegrex":
85 return "mips"
86 case "mipsel", "mipsallegrexel":
87 return "mipsel"
88 case "mips64", "mips64eb":
89 return "mips64"
90 case "mipsel64":
91 return "mipsel64"
92 case "r600", "hexagon", "sparc", "sparcv9", "tce",
93 "xcore", "nvptx", "nvptx64", "le32", "amdil":
94 return arch
95 }
96 if strings.HasPrefix(arch, "armv") {
97 return "arm"
98 } else if strings.HasPrefix(arch, "thumbv") {
99 return "thumb"
100 }
101 return "unknown"
102}