This commit is contained in:
gingerBill
2021-02-25 09:55:56 +00:00
5 changed files with 71 additions and 25 deletions
+10 -4
View File
@@ -2,8 +2,9 @@
release_mode=$1 release_mode=$1
warnings_to_disable="-std=c++11 -Wno-switch -Wno-pointer-sign -Wno-tautological-constant-out-of-range-compare -Wno-tautological-compare -Wno-macro-redefined" warnings_to_disable="-std=c++11 -Wno-switch"
libraries="-pthread -ldl -lm -lstdc++"
libraries="-pthread -ldl -lm -lstdc++ -lz -lcurses -lxml2"
other_args="-DLLVM_BACKEND_SUPPORT -DUSE_NEW_LLVM_ABI_SYSTEM" other_args="-DLLVM_BACKEND_SUPPORT -DUSE_NEW_LLVM_ABI_SYSTEM"
compiler="clang" compiler="clang"
@@ -22,10 +23,15 @@ if [[ "$(uname)" == "Darwin" ]]; then
# MacOS provides a symlink to clang called gcc, but it's nice to be explicit here. # MacOS provides a symlink to clang called gcc, but it's nice to be explicit here.
compiler="clang" compiler="clang"
llvm_config_flags="--cxxflags --ldflags"
# llvm_config_flags="${llvm_config_flags} --link-static"
llvm_config="llvm-config ${llvm_config_flags}"
other_args="${other_args} -liconv" other_args="${other_args} -liconv"
other_args="${other_args} `${llvm_config}` -lLLVM-C"
elif [[ "$(uname)" == "FreeBSD" ]]; then elif [[ "$(uname)" == "FreeBSD" ]]; then
compiler="clang" compiler="clang"
fi fi
${compiler} src/main.cpp ${warnings_to_disable} ${libraries} ${other_args} -o odin \ ${compiler} src/main.cpp ${warnings_to_disable} ${libraries} ${other_args} -o odin
&& ./odin run examples/demo/demo.odin -llvm-api # && ./odin run examples/demo/demo.odin -llvm-api
+16 -12
View File
@@ -23,7 +23,7 @@ enum TargetArchKind {
TargetArch_amd64, TargetArch_amd64,
TargetArch_386, TargetArch_386,
TargetArch_aarch64, TargetArch_arm64,
TargetArch_wasm32, TargetArch_wasm32,
TargetArch_COUNT, TargetArch_COUNT,
@@ -54,7 +54,7 @@ String target_arch_names[TargetArch_COUNT] = {
str_lit(""), str_lit(""),
str_lit("amd64"), str_lit("amd64"),
str_lit("386"), str_lit("386"),
str_lit("aarch64"), str_lit("arm64"),
str_lit("wasm32"), str_lit("wasm32"),
}; };
@@ -270,13 +270,13 @@ gb_global TargetMetrics target_darwin_amd64 = {
str_lit("e-m:o-i64:64-f80:128-n8:16:32:64-S128"), str_lit("e-m:o-i64:64-f80:128-n8:16:32:64-S128"),
}; };
gb_global TargetMetrics target_darwin_aarch64 = { gb_global TargetMetrics target_darwin_arm64 = {
TargetOs_darwin, TargetOs_darwin,
TargetArch_aarch64, TargetArch_arm64,
8, 8,
16, 16,
str_lit("aarch64-apple-darwin"), str_lit("arm64-apple-macosx11.0.0"),
str_lit("e-m:o-i64:64-f64:128-n8:16:32:64-S128"), // TODO(bill): Is this correct? str_lit("e-m:o-i64:64-i128:128-n32:64-S128"), // TODO(bill): Is this correct?
}; };
gb_global TargetMetrics target_freebsd_386 = { gb_global TargetMetrics target_freebsd_386 = {
@@ -322,7 +322,7 @@ struct NamedTargetMetrics {
gb_global NamedTargetMetrics named_targets[] = { gb_global NamedTargetMetrics named_targets[] = {
{ str_lit("darwin_amd64"), &target_darwin_amd64 }, { str_lit("darwin_amd64"), &target_darwin_amd64 },
{ str_lit("darwin_aarch64"), &target_darwin_aarch64 }, { str_lit("darwin_arm64"), &target_darwin_arm64 },
{ str_lit("essence_amd64"), &target_essence_amd64 }, { str_lit("essence_amd64"), &target_essence_amd64 },
{ str_lit("js_wasm32"), &target_js_wasm32 }, { str_lit("js_wasm32"), &target_js_wasm32 },
{ str_lit("linux_386"), &target_linux_386 }, { str_lit("linux_386"), &target_linux_386 },
@@ -733,7 +733,11 @@ void init_build_context(TargetMetrics *cross_target) {
#if defined(GB_SYSTEM_WINDOWS) #if defined(GB_SYSTEM_WINDOWS)
metrics = &target_windows_amd64; metrics = &target_windows_amd64;
#elif defined(GB_SYSTEM_OSX) #elif defined(GB_SYSTEM_OSX)
metrics = &target_darwin_amd64; #if defined(GB_CPU_ARM)
metrics = &target_darwin_arm64;
#else
metrics = &target_darwin_amd64;
#endif
#elif defined(GB_SYSTEM_FREEBSD) #elif defined(GB_SYSTEM_FREEBSD)
metrics = &target_freebsd_amd64; metrics = &target_freebsd_amd64;
#else #else
@@ -820,18 +824,18 @@ void init_build_context(TargetMetrics *cross_target) {
bc->link_flags = str_lit("-arch x86 "); bc->link_flags = str_lit("-arch x86 ");
break; break;
} }
} else if (bc->metrics.arch == TargetArch_aarch64) { } else if (bc->metrics.arch == TargetArch_arm64) {
if (bc->microarch.len == 0) { if (bc->microarch.len == 0) {
llc_flags = gb_string_appendc(llc_flags, "-march=aarch64 "); llc_flags = gb_string_appendc(llc_flags, "-march=arm64 ");
} }
switch (bc->metrics.os) { switch (bc->metrics.os) {
case TargetOs_darwin: case TargetOs_darwin:
bc->link_flags = str_lit("-arch aarch64 "); bc->link_flags = str_lit("-arch arm64 ");
break; break;
} }
if (!bc->use_llvm_api) { if (!bc->use_llvm_api) {
gb_printf_err("The aarch64 architecture is only supported with -llvm-api\n");; gb_printf_err("The arm64 architecture is only supported with -llvm-api\n");;
gb_exit(1); gb_exit(1);
} }
+16 -7
View File
@@ -443,7 +443,10 @@ namespace lbAbi386 {
case 4: return lb_arg_type_direct(return_type, LLVMIntTypeInContext(c, 32), nullptr, nullptr); case 4: return lb_arg_type_direct(return_type, LLVMIntTypeInContext(c, 32), nullptr, nullptr);
case 8: return lb_arg_type_direct(return_type, LLVMIntTypeInContext(c, 64), nullptr, nullptr); case 8: return lb_arg_type_direct(return_type, LLVMIntTypeInContext(c, 64), nullptr, nullptr);
} }
return lb_arg_type_indirect(return_type, lb_create_enum_attribute(c, "sret", true)); LLVMAttributeRef attr = nullptr;
// TODO(bill): sret doesn't work correct for LLVM C API
// attr = lb_create_enum_attribute(c, "sret", true);
return lb_arg_type_indirect(return_type, attr);
} }
return non_struct(c, return_type, true); return non_struct(c, return_type, true);
} }
@@ -601,7 +604,8 @@ namespace lbAbiAmd64SysV {
if (attribute_kind == Amd64TypeAttribute_ByVal) { if (attribute_kind == Amd64TypeAttribute_ByVal) {
attribute = lb_create_enum_attribute(c, "byval", true); attribute = lb_create_enum_attribute(c, "byval", true);
} else if (attribute_kind == Amd64TypeAttribute_StructRect) { } else if (attribute_kind == Amd64TypeAttribute_StructRect) {
attribute = lb_create_enum_attribute(c, "sret", true); // TODO(bill): sret doesn't work correct for LLVM C API
// attribute = lb_create_enum_attribute(c, "sret", true);
} }
return lb_arg_type_indirect(type, attribute); return lb_arg_type_indirect(type, attribute);
} else { } else {
@@ -900,7 +904,10 @@ namespace lbAbiAmd64SysV {
case 4: return lb_arg_type_direct(return_type, LLVMIntTypeInContext(c, 32), nullptr, nullptr); case 4: return lb_arg_type_direct(return_type, LLVMIntTypeInContext(c, 32), nullptr, nullptr);
case 8: return lb_arg_type_direct(return_type, LLVMIntTypeInContext(c, 64), nullptr, nullptr); case 8: return lb_arg_type_direct(return_type, LLVMIntTypeInContext(c, 64), nullptr, nullptr);
} }
return lb_arg_type_indirect(return_type, lb_create_enum_attribute(c, "sret", true)); LLVMAttributeRef attr = nullptr;
// TODO(bill): sret doesn't work correct for LLVM C API
// attr = lb_create_enum_attribute(c, "sret", true);
return lb_arg_type_indirect(return_type, attr);
} else if (build_context.metrics.os == TargetOs_windows && lb_is_type_kind(return_type, LLVMIntegerTypeKind) && lb_sizeof(return_type) == 16) { } else if (build_context.metrics.os == TargetOs_windows && lb_is_type_kind(return_type, LLVMIntegerTypeKind) && lb_sizeof(return_type) == 16) {
return lb_arg_type_direct(return_type, LLVMIntTypeInContext(c, 128), nullptr, nullptr); return lb_arg_type_direct(return_type, LLVMIntTypeInContext(c, 128), nullptr, nullptr);
} }
@@ -909,7 +916,7 @@ namespace lbAbiAmd64SysV {
}; };
namespace lbAbiAarch64 { namespace lbAbiArm64 {
Array<lbArgType> compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count); Array<lbArgType> compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count);
lbArgType compute_return_type(LLVMContextRef c, LLVMTypeRef return_type, bool return_is_defined); lbArgType compute_return_type(LLVMContextRef c, LLVMTypeRef return_type, bool return_is_defined);
bool is_homogenous_aggregate(LLVMContextRef c, LLVMTypeRef type, LLVMTypeRef *base_type_, unsigned *member_count_); bool is_homogenous_aggregate(LLVMContextRef c, LLVMTypeRef type, LLVMTypeRef *base_type_, unsigned *member_count_);
@@ -1048,7 +1055,9 @@ namespace lbAbiAarch64 {
} }
return lb_arg_type_direct(type, cast_type, nullptr, nullptr); return lb_arg_type_direct(type, cast_type, nullptr, nullptr);
} else { } else {
LLVMAttributeRef attr = lb_create_enum_attribute(c, "sret", true); LLVMAttributeRef attr = nullptr;
// TODO(bill): sret doesn't work correct for LLVM C API
// attr = lb_create_enum_attribute(c, "sret", true);
return lb_arg_type_indirect(type, attr); return lb_arg_type_indirect(type, attr);
} }
} }
@@ -1123,8 +1132,8 @@ LB_ABI_INFO(lb_get_abi_info) {
} }
} else if (build_context.metrics.arch == TargetArch_386) { } else if (build_context.metrics.arch == TargetArch_386) {
return lbAbi386::abi_info(c, arg_types, arg_count, return_type, return_is_defined, calling_convention); return lbAbi386::abi_info(c, arg_types, arg_count, return_type, return_is_defined, calling_convention);
} else if (build_context.metrics.arch == TargetArch_aarch64) { } else if (build_context.metrics.arch == TargetArch_arm64) {
return lbAbiAarch64::abi_info(c, arg_types, arg_count, return_type, return_is_defined, calling_convention); return lbAbiArm64::abi_info(c, arg_types, arg_count, return_type, return_is_defined, calling_convention);
} else if (build_context.metrics.arch == TargetArch_wasm32) { } else if (build_context.metrics.arch == TargetArch_wasm32) {
return lbAbi386::abi_info(c, arg_types, arg_count, return_type, return_is_defined, calling_convention); return lbAbi386::abi_info(c, arg_types, arg_count, return_type, return_is_defined, calling_convention);
} }
+22 -1
View File
@@ -28,6 +28,14 @@ gb_global Timings global_timings = {0};
#if defined(LLVM_BACKEND_SUPPORT) #if defined(LLVM_BACKEND_SUPPORT)
#include "llvm_backend.cpp" #include "llvm_backend.cpp"
#if defined(GB_SYSTEM_OSX)
#include <llvm/Config/llvm-config.h>
#if LLVM_VERSION_MAJOR < 11
#error LLVM Version 11+ is required => "brew install llvm@11"
#endif
#endif
#endif #endif
#include "ir.cpp" #include "ir.cpp"
@@ -431,7 +439,11 @@ void linker_stage(lbGenerator *gen) {
// This sets a requirement of Mountain Lion and up, but the compiler doesn't work without this limit. // This sets a requirement of Mountain Lion and up, but the compiler doesn't work without this limit.
// NOTE: If you change this (although this minimum is as low as you can go with Odin working) // NOTE: If you change this (although this minimum is as low as you can go with Odin working)
// make sure to also change the 'mtriple' param passed to 'opt' // make sure to also change the 'mtriple' param passed to 'opt'
#if defined(GB_CPU_ARM)
" -macosx_version_min 11.0.0 "
#else
" -macosx_version_min 10.8.0 " " -macosx_version_min 10.8.0 "
#endif
// This points the linker to where the entry point is // This points the linker to where the entry point is
" -e _main " " -e _main "
#endif #endif
@@ -1830,6 +1842,15 @@ void print_show_unused(Checker *c) {
print_usage_line(0, ""); print_usage_line(0, "");
} }
void enforce_platform_settings(void) {
#if defined(GB_SYSTEM_OSX) && defined(GB_CPU_ARM)
#if defined(USE_NEW_LLVM_ABI_SYSTEM)
build_context.use_llvm_api = true;
#endif
#endif
}
int main(int arg_count, char const **arg_ptr) { int main(int arg_count, char const **arg_ptr) {
if (arg_count < 2) { if (arg_count < 2) {
usage(make_string_c(arg_ptr[0])); usage(make_string_c(arg_ptr[0]));
@@ -1983,7 +2004,7 @@ int main(int arg_count, char const **arg_ptr) {
return 0; return 0;
} }
enforce_platform_settings();
// NOTE(bill): add 'shared' directory if it is not already set // NOTE(bill): add 'shared' directory if it is not already set
if (!find_library_collection_path(str_lit("shared"), nullptr)) { if (!find_library_collection_path(str_lit("shared"), nullptr)) {
+7 -1
View File
@@ -42,7 +42,13 @@ u64 osx_time_stamp__freq(void) {
data.numer = 0; data.numer = 0;
data.denom = 0; data.denom = 0;
mach_timebase_info(&data); mach_timebase_info(&data);
return (data.numer / data.denom) * 1000000000; #if defined(GB_CPU_ARM)
// NOTE(bill, 2021-02-25): M1 Chip seems to have a different freq count
// TODO(bill): Is this truly correct?
return (1000000llu * cast(u64)data.numer) / cast(u64)data.denom;
#else
return (1000000000llu * cast(u64)data.numer) / cast(u64)data.denom;
#endif
} }
#elif defined(GB_SYSTEM_UNIX) #elif defined(GB_SYSTEM_UNIX)