Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Platform.is_arm and Platform.is_x86 functions. #469

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions core/Platform.savi
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@
:: It is mutually exclusive with `is_posix`.
:const is_windows Bool: compiler intrinsic

:: SECTION: CPU Architecture

:: Returns `True` if the target platform uses an ARM-based CPU architecture.
::
:: This is mutually exclusive with `is_x86`.
:const is_arm Bool: compiler intrinsic

:: Returns `True` if the target platform uses an x86-based CPU architecture.
::
:: This is mutually exclusive with `is_arm`.
:const is_x86 Bool: compiler intrinsic

:: SECTION: Data Type Sizes

:: Returns `True` if the target platform uses ILP32 data type sizes.
Expand Down
5 changes: 5 additions & 0 deletions spec/core/Platform.Spec.savi
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
(if Platform.is_macos (1 | 0)) +
(if Platform.is_windows (1 | 0))

:it "returns True for exactly one of {is_arm, is_x86}"
assert: U8[1] == U8[0] +
(if Platform.is_arm (1 | 0)) +
(if Platform.is_x86 (1 | 0))

:it "returns True for is_posix on POSIX platforms"
if Platform.is_linux (assert: Platform.is_posix)
if Platform.is_bsd (assert: Platform.is_posix)
Expand Down
4 changes: 4 additions & 0 deletions src/savi/compiler/code_gen.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,10 @@ class Savi::Compiler::CodeGen
gen_bool(!target.windows?)
when "is_windows"
gen_bool(target.windows?)
when "is_arm"
gen_bool(target.any_arm?)
when "is_x86"
gen_bool(target.any_x86?)
when "is_ilp32"
gen_bool(abi_size_of(@isize) == 4)
when "is_lp64"
Expand Down
8 changes: 8 additions & 0 deletions src/savi/compiler/target.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
require "compiler/crystal/codegen/target"

class Savi::Compiler::Target < Crystal::Codegen::Target
def any_arm?
architecture == "arm" || architecture == "aarch64"
end

def any_x86?
architecture == "i386" || architecture == "x86_64"
end

def arm64?
architecture == "aarch64"
end
Expand Down
Loading