authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-02-10 15:41:50-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-02-10 15:41:50-07:00
log1ff2edf67ef7a5d434697bb494247b76f34f6c84
treef395713208e536e8b3cb6aee6ff1fb1574898f85
parentb988017547581ea39163913e400c962f74fc9b32

add "targets" command to list architectures, oses, abis


9 files changed, 435 insertions(+), 9 deletions(-)

CMakeLists.txt+1
......@@ -27,6 +27,7 @@ include_directories(
2727)
2828
2929set(ZIG_SOURCES
30 "${CMAKE_SOURCE_DIR}/src/target.cpp"
3031 "${CMAKE_SOURCE_DIR}/src/ast_render.cpp"
3132 "${CMAKE_SOURCE_DIR}/src/bignum.cpp"
3233 "${CMAKE_SOURCE_DIR}/src/tokenizer.cpp"
doc/langref.md+3-3
......@@ -350,9 +350,9 @@ The functions take an integer (TODO float?) type, two variables of the specified
350350
351351```
352352Function Operation
353bool add_with_overflow(type, a: type, b: type, x: &type) *x = a + b
354bool sub_with_overflow(type, a: type, b: type, x: &type) *x = a - b
355bool mul_with_overflow(type, a: type, b: type, x: &type) *x = a * b
353bool add_with_overflow(T: type, a: T, b: T, x: &T) *x = a + b
354bool sub_with_overflow(T: type, a: T, b: T, x: &T) *x = a - b
355bool mul_with_overflow(T: type, a: T, b: T, x: &T) *x = a * b
356356```
357357
358358### Memory Operations
src/all_types.hpp-1
......@@ -1124,7 +1124,6 @@ struct CodeGen {
11241124 bool is_test_build;
11251125 LLVMTargetMachineRef target_machine;
11261126 LLVMZigDIFile *dummy_di_file;
1127 bool is_native_target;
11281127 Buf *root_source_dir;
11291128 Buf *root_out_name;
11301129
src/codegen.cpp-3
......@@ -3498,8 +3498,6 @@ static void define_builtin_fns(CodeGen *g) {
34983498 create_builtin_fn_with_arg_count(g, BuiltinFnIdConstEval, "const_eval", 1);
34993499}
35003500
3501
3502
35033501static void init(CodeGen *g, Buf *source_path) {
35043502 g->lib_search_paths.append(g->root_source_dir);
35053503 g->lib_search_paths.append(buf_create_from_str(ZIG_STD_DIR));
......@@ -3510,7 +3508,6 @@ static void init(CodeGen *g, Buf *source_path) {
35103508 LLVMInitializeAllAsmParsers();
35113509 LLVMInitializeNativeTarget();
35123510
3513 g->is_native_target = true;
35143511 char *native_triple = LLVMGetDefaultTargetTriple();
35153512
35163513 g->module = LLVMModuleCreateWithName(buf_ptr(source_path));
src/main.cpp+40
......@@ -10,6 +10,7 @@
1010#include "codegen.hpp"
1111#include "os.hpp"
1212#include "error.hpp"
13#include "target.hpp"
1314
1415#include <stdio.h>
1516
......@@ -20,6 +21,7 @@ static int usage(const char *arg0) {
2021 " test create and run a test build\n"
2122 " version print version number and exit\n"
2223 " parseh convert a c header file to zig extern declarations\n"
24 " targets list available compilation targets\n"
2325 "Options:\n"
2426 " --release build with optimizations on and debug protection off\n"
2527 " --static output will be statically linked\n"
......@@ -40,12 +42,45 @@ static int usage(const char *arg0) {
4042 return EXIT_FAILURE;
4143}
4244
45static int print_target_list(FILE *f) {
46 fprintf(f, "Architectures:\n");
47 int arch_count = target_arch_count();
48 int sub_arch_count = target_sub_arch_count();
49 for (int arch_i = 0; arch_i < arch_count; arch_i += 1) {
50 const ArchType *arch = get_target_arch(arch_i);
51 fprintf(f, " %s\n", ZigLLVMGetArchTypeName(arch->llvm_arch));
52 for (int sub_arch_i = 0; sub_arch_i < sub_arch_count; sub_arch_i += 1) {
53 const SubArchType *sub_arch = get_target_sub_arch(sub_arch_i);
54 if (sub_arch->arch == arch->llvm_arch) {
55 fprintf(f, " %s\n", sub_arch->name);
56 }
57 }
58 }
59
60 fprintf(f, "\nOperating Systems:\n");
61 int os_count = target_os_count();
62 for (int i = 0; i < os_count; i += 1) {
63 const OsType *os_type = get_target_os(i);
64 fprintf(f, " %s\n", get_target_os_name(os_type));
65 }
66
67 fprintf(f, "\nABIs:\n");
68 int environ_count = target_environ_count();
69 for (int i = 0; i < environ_count; i += 1) {
70 const EnvironmentType *environ_type = get_target_environ(i);
71 fprintf(f, " %s\n", ZigLLVMGetEnvironmentTypeName(environ_type->llvm_environment));
72 }
73
74 return EXIT_SUCCESS;
75}
76
4377enum Cmd {
4478 CmdInvalid,
4579 CmdBuild,
4680 CmdTest,
4781 CmdVersion,
4882 CmdParseH,
83 CmdTargets,
4984};
5085
5186int main(int argc, char **argv) {
......@@ -139,6 +174,8 @@ int main(int argc, char **argv) {
139174 cmd = CmdParseH;
140175 } else if (strcmp(arg, "test") == 0) {
141176 cmd = CmdTest;
177 } else if (strcmp(arg, "targets") == 0) {
178 cmd = CmdTargets;
142179 } else {
143180 fprintf(stderr, "Unrecognized command: %s\n", arg);
144181 return usage(arg0);
......@@ -155,6 +192,7 @@ int main(int argc, char **argv) {
155192 }
156193 break;
157194 case CmdVersion:
195 case CmdTargets:
158196 return usage(arg0);
159197 case CmdInvalid:
160198 zig_unreachable();
......@@ -249,6 +287,8 @@ int main(int argc, char **argv) {
249287 case CmdVersion:
250288 printf("%s\n", ZIG_VERSION_STRING);
251289 return EXIT_SUCCESS;
290 case CmdTargets:
291 return print_target_list(stdout);
252292 case CmdInvalid:
253293 return usage(arg0);
254294 }
src/target.cpp created+169
......@@ -0,0 +1,169 @@
1/*
2 * Copyright (c) 2016 Andrew Kelley
3 *
4 * This file is part of zig, which is MIT licensed.
5 * See http://opensource.org/licenses/MIT
6 */
7
8#include "target.hpp"
9#include "util.hpp"
10
11static const ArchType arch_list[] = {
12 {ZigLLVM_arm},
13 {ZigLLVM_armeb},
14 {ZigLLVM_aarch64},
15 {ZigLLVM_aarch64_be},
16 {ZigLLVM_bpfel},
17 {ZigLLVM_bpfeb},
18 {ZigLLVM_hexagon},
19 {ZigLLVM_mips},
20 {ZigLLVM_mipsel},
21 {ZigLLVM_mips64},
22 {ZigLLVM_mips64el},
23 {ZigLLVM_msp430},
24 {ZigLLVM_ppc},
25 {ZigLLVM_ppc64},
26 {ZigLLVM_ppc64le},
27 {ZigLLVM_r600},
28 {ZigLLVM_amdgcn},
29 {ZigLLVM_sparc},
30 {ZigLLVM_sparcv9},
31 {ZigLLVM_sparcel},
32 {ZigLLVM_systemz},
33 {ZigLLVM_tce},
34 {ZigLLVM_thumb},
35 {ZigLLVM_thumbeb},
36 {ZigLLVM_x86},
37 {ZigLLVM_x86_64},
38 {ZigLLVM_xcore},
39 {ZigLLVM_nvptx},
40 {ZigLLVM_nvptx64},
41 {ZigLLVM_le32},
42 {ZigLLVM_le64},
43 {ZigLLVM_amdil},
44 {ZigLLVM_amdil64},
45 {ZigLLVM_hsail},
46 {ZigLLVM_hsail64},
47 {ZigLLVM_spir},
48 {ZigLLVM_spir64},
49 {ZigLLVM_kalimba},
50 {ZigLLVM_shave},
51 {ZigLLVM_wasm32},
52 {ZigLLVM_wasm64},
53};
54
55static const SubArchType sub_arch_list[] = {
56 {ZigLLVM_arm, ZigLLVM_ARMSubArch_v8_1a, "v8_1a"},
57 {ZigLLVM_arm, ZigLLVM_ARMSubArch_v8, "v8"},
58 {ZigLLVM_arm, ZigLLVM_ARMSubArch_v7, "v7"},
59 {ZigLLVM_arm, ZigLLVM_ARMSubArch_v7em, "v7em"},
60 {ZigLLVM_arm, ZigLLVM_ARMSubArch_v7m, "v7m"},
61 {ZigLLVM_arm, ZigLLVM_ARMSubArch_v7s, "v7s"},
62 {ZigLLVM_arm, ZigLLVM_ARMSubArch_v6, "v6"},
63 {ZigLLVM_arm, ZigLLVM_ARMSubArch_v6m, "v6m"},
64 {ZigLLVM_arm, ZigLLVM_ARMSubArch_v6k, "v6k"},
65 {ZigLLVM_arm, ZigLLVM_ARMSubArch_v6t2, "v6t2"},
66 {ZigLLVM_arm, ZigLLVM_ARMSubArch_v5, "v5"},
67 {ZigLLVM_arm, ZigLLVM_ARMSubArch_v5te, "v5te"},
68 {ZigLLVM_arm, ZigLLVM_ARMSubArch_v4t, "v4t"},
69
70 {ZigLLVM_kalimba, ZigLLVM_KalimbaSubArch_v3, "v3"},
71 {ZigLLVM_kalimba, ZigLLVM_KalimbaSubArch_v4, "v4"},
72 {ZigLLVM_kalimba, ZigLLVM_KalimbaSubArch_v5, "v5"},
73};
74
75static const VendorType vendor_list[] = {
76 {ZigLLVM_Apple},
77 {ZigLLVM_PC},
78 {ZigLLVM_SCEI},
79 {ZigLLVM_BGP},
80 {ZigLLVM_BGQ},
81 {ZigLLVM_Freescale},
82 {ZigLLVM_IBM},
83 {ZigLLVM_ImaginationTechnologies},
84 {ZigLLVM_MipsTechnologies},
85 {ZigLLVM_NVIDIA},
86 {ZigLLVM_CSR},
87};
88
89static const OsType os_list[] = {
90 {ZigLLVM_UnknownOS},
91 {ZigLLVM_CloudABI},
92 {ZigLLVM_Darwin},
93 {ZigLLVM_DragonFly},
94 {ZigLLVM_FreeBSD},
95 {ZigLLVM_IOS},
96 {ZigLLVM_KFreeBSD},
97 {ZigLLVM_Linux},
98 {ZigLLVM_Lv2},
99 {ZigLLVM_MacOSX},
100 {ZigLLVM_NetBSD},
101 {ZigLLVM_OpenBSD},
102 {ZigLLVM_Solaris},
103 {ZigLLVM_Win32},
104 {ZigLLVM_Haiku},
105 {ZigLLVM_Minix},
106 {ZigLLVM_RTEMS},
107 {ZigLLVM_NaCl},
108 {ZigLLVM_CNK},
109 {ZigLLVM_Bitrig},
110 {ZigLLVM_AIX},
111 {ZigLLVM_CUDA},
112 {ZigLLVM_NVCL},
113 {ZigLLVM_AMDHSA},
114 {ZigLLVM_PS4},
115};
116
117static const EnvironmentType environ_list[] = {
118 {ZigLLVM_GNU},
119 {ZigLLVM_GNUEABI},
120 {ZigLLVM_GNUEABIHF},
121 {ZigLLVM_GNUX32},
122 {ZigLLVM_CODE16},
123 {ZigLLVM_EABI},
124 {ZigLLVM_EABIHF},
125 {ZigLLVM_Android},
126 {ZigLLVM_MSVC},
127 {ZigLLVM_Itanium},
128 {ZigLLVM_Cygnus},
129};
130
131int target_arch_count(void) {
132 return array_length(arch_list);
133}
134
135const ArchType *get_target_arch(int index) {
136 return &arch_list[index];
137}
138
139int target_sub_arch_count(void) {
140 return array_length(sub_arch_list);
141}
142const SubArchType *get_target_sub_arch(int index) {
143 return &sub_arch_list[index];
144}
145
146int target_vendor_count(void) {
147 return array_length(vendor_list);
148}
149
150const VendorType *get_target_vendor(int index) {
151 return &vendor_list[index];
152}
153
154int target_os_count(void) {
155 return array_length(os_list);
156}
157const OsType *get_target_os(int index) {
158 return &os_list[index];
159}
160const char *get_target_os_name(const OsType *os_type) {
161 return (os_type->llvm_os == ZigLLVM_UnknownOS) ? "freestanding" : ZigLLVMGetOSTypeName(os_type->llvm_os);
162}
163
164int target_environ_count(void) {
165 return array_length(environ_list);
166}
167const EnvironmentType *get_target_environ(int index) {
168 return &environ_list[index];
169}
src/target.hpp created+51
......@@ -0,0 +1,51 @@
1/*
2 * Copyright (c) 2016 Andrew Kelley
3 *
4 * This file is part of zig, which is MIT licensed.
5 * See http://opensource.org/licenses/MIT
6 */
7
8#ifndef ZIG_TARGET_HPP
9#define ZIG_TARGET_HPP
10
11#include <zig_llvm.hpp>
12
13struct ArchType {
14 ZigLLVM_ArchType llvm_arch;
15};
16
17struct SubArchType {
18 ZigLLVM_ArchType arch; // which arch it applies to
19 ZigLLVM_SubArchType sub_arch;
20 const char *name;
21};
22
23struct VendorType {
24 ZigLLVM_VendorType llvm_vendor;
25};
26
27struct OsType {
28 ZigLLVM_OSType llvm_os;
29};
30
31struct EnvironmentType {
32 ZigLLVM_EnvironmentType llvm_environment;
33};
34
35int target_arch_count(void);
36const ArchType *get_target_arch(int index);
37
38int target_sub_arch_count(void);
39const SubArchType *get_target_sub_arch(int index);
40
41int target_vendor_count(void);
42const VendorType *get_target_vendor(int index);
43
44int target_os_count(void);
45const OsType *get_target_os(int index);
46const char *get_target_os_name(const OsType *os_type);
47
48int target_environ_count(void);
49const EnvironmentType *get_target_environ(int index);
50
51#endif
src/zig_llvm.cpp+24
......@@ -514,8 +514,32 @@ void LLVMZigSetFastMath(LLVMBuilderRef builder_wrapped, bool on_state) {
514514 }
515515}
516516
517
518static_assert((Triple::ArchType)ZigLLVM_LastArchType == Triple::LastArchType, "");
519static_assert((Triple::VendorType)ZigLLVM_LastVendorType == Triple::LastVendorType, "");
520static_assert((Triple::OSType)ZigLLVM_LastOSType == Triple::LastOSType, "");
521static_assert((Triple::EnvironmentType)ZigLLVM_LastEnvironmentType == Triple::LastEnvironmentType, "");
522
523const char *ZigLLVMGetArchTypeName(ZigLLVM_ArchType arch) {
524 return Triple::getArchTypeName((Triple::ArchType)arch);
525}
526
527const char *ZigLLVMGetVendorTypeName(ZigLLVM_VendorType vendor) {
528 return Triple::getVendorTypeName((Triple::VendorType)vendor);
529}
530
531const char *ZigLLVMGetOSTypeName(ZigLLVM_OSType os) {
532 return Triple::getOSTypeName((Triple::OSType)os);
533}
534
535const char *ZigLLVMGetEnvironmentTypeName(ZigLLVM_EnvironmentType environ) {
536 return Triple::getEnvironmentTypeName((Triple::EnvironmentType)environ);
537}
538
517539//------------------------------------
518540
541#include "buffer.hpp"
542
519543enum FloatAbi {
520544 FloatAbiHard,
521545 FloatAbiSoft,
src/zig_llvm.hpp+147-2
......@@ -143,11 +143,156 @@ LLVMZigDILocation *LLVMZigGetDebugLoc(unsigned line, unsigned col, LLVMZigDIScop
143143void LLVMZigSetFastMath(LLVMBuilderRef builder_wrapped, bool on_state);
144144
145145
146// copied from include/llvm/ADT/Triple.h
147
148enum ZigLLVM_ArchType {
149 ZigLLVM_UnknownArch,
150
151 ZigLLVM_arm, // ARM (little endian): arm, armv.*, xscale
152 ZigLLVM_armeb, // ARM (big endian): armeb
153 ZigLLVM_aarch64, // AArch64 (little endian): aarch64
154 ZigLLVM_aarch64_be, // AArch64 (big endian): aarch64_be
155 ZigLLVM_bpfel, // eBPF or extended BPF or 64-bit BPF (little endian)
156 ZigLLVM_bpfeb, // eBPF or extended BPF or 64-bit BPF (big endian)
157 ZigLLVM_hexagon, // Hexagon: hexagon
158 ZigLLVM_mips, // MIPS: mips, mipsallegrex
159 ZigLLVM_mipsel, // MIPSEL: mipsel, mipsallegrexel
160 ZigLLVM_mips64, // MIPS64: mips64
161 ZigLLVM_mips64el, // MIPS64EL: mips64el
162 ZigLLVM_msp430, // MSP430: msp430
163 ZigLLVM_ppc, // PPC: powerpc
164 ZigLLVM_ppc64, // PPC64: powerpc64, ppu
165 ZigLLVM_ppc64le, // PPC64LE: powerpc64le
166 ZigLLVM_r600, // R600: AMD GPUs HD2XXX - HD6XXX
167 ZigLLVM_amdgcn, // AMDGCN: AMD GCN GPUs
168 ZigLLVM_sparc, // Sparc: sparc
169 ZigLLVM_sparcv9, // Sparcv9: Sparcv9
170 ZigLLVM_sparcel, // Sparc: (endianness = little). NB: 'Sparcle' is a CPU variant
171 ZigLLVM_systemz, // SystemZ: s390x
172 ZigLLVM_tce, // TCE (http://tce.cs.tut.fi/): tce
173 ZigLLVM_thumb, // Thumb (little endian): thumb, thumbv.*
174 ZigLLVM_thumbeb, // Thumb (big endian): thumbeb
175 ZigLLVM_x86, // X86: i[3-9]86
176 ZigLLVM_x86_64, // X86-64: amd64, x86_64
177 ZigLLVM_xcore, // XCore: xcore
178 ZigLLVM_nvptx, // NVPTX: 32-bit
179 ZigLLVM_nvptx64, // NVPTX: 64-bit
180 ZigLLVM_le32, // le32: generic little-endian 32-bit CPU (PNaCl / Emscripten)
181 ZigLLVM_le64, // le64: generic little-endian 64-bit CPU (PNaCl / Emscripten)
182 ZigLLVM_amdil, // AMDIL
183 ZigLLVM_amdil64, // AMDIL with 64-bit pointers
184 ZigLLVM_hsail, // AMD HSAIL
185 ZigLLVM_hsail64, // AMD HSAIL with 64-bit pointers
186 ZigLLVM_spir, // SPIR: standard portable IR for OpenCL 32-bit version
187 ZigLLVM_spir64, // SPIR: standard portable IR for OpenCL 64-bit version
188 ZigLLVM_kalimba, // Kalimba: generic kalimba
189 ZigLLVM_shave, // SHAVE: Movidius vector VLIW processors
190 ZigLLVM_wasm32, // WebAssembly with 32-bit pointers
191 ZigLLVM_wasm64, // WebAssembly with 64-bit pointers
192 ZigLLVM_LastArchType = ZigLLVM_wasm64
193};
194
195enum ZigLLVM_SubArchType {
196 ZigLLVM_NoSubArch,
197
198 ZigLLVM_ARMSubArch_v8_1a,
199 ZigLLVM_ARMSubArch_v8,
200 ZigLLVM_ARMSubArch_v7,
201 ZigLLVM_ARMSubArch_v7em,
202 ZigLLVM_ARMSubArch_v7m,
203 ZigLLVM_ARMSubArch_v7s,
204 ZigLLVM_ARMSubArch_v6,
205 ZigLLVM_ARMSubArch_v6m,
206 ZigLLVM_ARMSubArch_v6k,
207 ZigLLVM_ARMSubArch_v6t2,
208 ZigLLVM_ARMSubArch_v5,
209 ZigLLVM_ARMSubArch_v5te,
210 ZigLLVM_ARMSubArch_v4t,
211
212 ZigLLVM_KalimbaSubArch_v3,
213 ZigLLVM_KalimbaSubArch_v4,
214 ZigLLVM_KalimbaSubArch_v5
215};
216enum ZigLLVM_VendorType {
217 ZigLLVM_UnknownVendor,
218
219 ZigLLVM_Apple,
220 ZigLLVM_PC,
221 ZigLLVM_SCEI,
222 ZigLLVM_BGP,
223 ZigLLVM_BGQ,
224 ZigLLVM_Freescale,
225 ZigLLVM_IBM,
226 ZigLLVM_ImaginationTechnologies,
227 ZigLLVM_MipsTechnologies,
228 ZigLLVM_NVIDIA,
229 ZigLLVM_CSR,
230 ZigLLVM_LastVendorType = ZigLLVM_CSR
231};
232enum ZigLLVM_OSType {
233 ZigLLVM_UnknownOS,
234
235 ZigLLVM_CloudABI,
236 ZigLLVM_Darwin,
237 ZigLLVM_DragonFly,
238 ZigLLVM_FreeBSD,
239 ZigLLVM_IOS,
240 ZigLLVM_KFreeBSD,
241 ZigLLVM_Linux,
242 ZigLLVM_Lv2, // PS3
243 ZigLLVM_MacOSX,
244 ZigLLVM_NetBSD,
245 ZigLLVM_OpenBSD,
246 ZigLLVM_Solaris,
247 ZigLLVM_Win32,
248 ZigLLVM_Haiku,
249 ZigLLVM_Minix,
250 ZigLLVM_RTEMS,
251 ZigLLVM_NaCl, // Native Client
252 ZigLLVM_CNK, // BG/P Compute-Node Kernel
253 ZigLLVM_Bitrig,
254 ZigLLVM_AIX,
255 ZigLLVM_CUDA, // NVIDIA CUDA
256 ZigLLVM_NVCL, // NVIDIA OpenCL
257 ZigLLVM_AMDHSA, // AMD HSA Runtime
258 ZigLLVM_PS4,
259 ZigLLVM_LastOSType = ZigLLVM_PS4
260};
261enum ZigLLVM_EnvironmentType {
262 ZigLLVM_UnknownEnvironment,
263
264 ZigLLVM_GNU,
265 ZigLLVM_GNUEABI,
266 ZigLLVM_GNUEABIHF,
267 ZigLLVM_GNUX32,
268 ZigLLVM_CODE16,
269 ZigLLVM_EABI,
270 ZigLLVM_EABIHF,
271 ZigLLVM_Android,
272
273 ZigLLVM_MSVC,
274 ZigLLVM_Itanium,
275 ZigLLVM_Cygnus,
276 ZigLLVM_LastEnvironmentType = ZigLLVM_Cygnus
277};
278enum ZigLLVM_ObjectFormatType {
279 ZigLLVM_UnknownObjectFormat,
280
281 ZigLLVM_COFF,
282 ZigLLVM_ELF,
283 ZigLLVM_MachO,
284};
285
286const char *ZigLLVMGetArchTypeName(ZigLLVM_ArchType arch);
287const char *ZigLLVMGetVendorTypeName(ZigLLVM_VendorType vendor);
288const char *ZigLLVMGetOSTypeName(ZigLLVM_OSType os);
289const char *ZigLLVMGetEnvironmentTypeName(ZigLLVM_EnvironmentType environ);
290
291
146292/*
147293 * This stuff is not LLVM API but it depends on the LLVM C++ API so we put it here.
148294 */
149#include "buffer.hpp"
150
295struct Buf;
151296Buf *get_dynamic_linker(LLVMTargetMachineRef target_machine);
152297
153298#endif