Skip to content

Commit

Permalink
v.pref: extract architecture related code into arch.c.v; rename `pr…
Browse files Browse the repository at this point in the history
…ef.c.v` to `pref.v` (#21387)
  • Loading branch information
ttytm committed Apr 30, 2024
1 parent 12ca2f2 commit 08b6cc7
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 75 deletions.
70 changes: 70 additions & 0 deletions vlib/v/pref/arch.c.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
module pref

pub enum Arch {
_auto
amd64 // aka x86_64
arm64 // 64-bit arm
arm32 // 32-bit arm
rv64 // 64-bit risc-v
rv32 // 32-bit risc-v
i386
js_node
js_browser
js_freestanding
wasm32
_max
}

pub fn get_host_arch() Arch {
// Note: we can not use `$if arch` here, because V skips cgen for the non
// current comptime branches by default, so there is a bootstrapping
// problem => the __V_architecture macro is used to resolve it.
// TODO: think about how to solve it for non C backends, perhaps we
// need a comptime `$if native {` too, and/or a mechanism to always
// generate all branches for specific functions?
if C.__V_architecture <= int(Arch._auto) || C.__V_architecture >= int(Arch._max) {
return Arch.amd64
}
return unsafe { Arch(C.__V_architecture) }
}

pub fn arch_from_string(arch_str string) !Arch {
match arch_str {
'amd64', 'x86_64', 'x64', 'x86' { // amd64 recommended
return .amd64
}
'aarch64', 'arm64' { // arm64 recommended
return .arm64
}
'aarch32', 'arm32', 'arm' { // arm32 recommended
return .arm32
}
'rv64', 'riscv64', 'risc-v64', 'riscv', 'risc-v' { // rv64 recommended
return .rv64
}
'rv32', 'riscv32' { // rv32 recommended
return .rv32
}
'x86_32', 'x32', 'i386', 'IA-32', 'ia-32', 'ia32' { // i386 recommended
return .i386
}
'js', 'js_node' {
return .js_node
}
'js_browser' {
return .js_browser
}
'js_freestanding' {
return .js_freestanding
}
'wasm32', 'wasm' {
return .wasm32
}
'' {
return ._auto
}
else {
return error('invalid arch: ${arch_str}')
}
}
}
75 changes: 0 additions & 75 deletions vlib/v/pref/pref.c.v → vlib/v/pref/pref.v
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,6 @@ pub enum CompilerType {
cplusplus
}

pub enum Arch {
_auto
amd64 // aka x86_64
arm64 // 64-bit arm
arm32 // 32-bit arm
rv64 // 64-bit risc-v
rv32 // 32-bit risc-v
i386
js_node
js_browser
js_freestanding
wasm32
_max
}

pub const list_of_flags_with_param = ['b', 'd', 'e', 'o', 'define', 'backend', 'cc', 'os', 'cflags',
'ldflags', 'path', 'arch']

Expand Down Expand Up @@ -1115,53 +1100,6 @@ pub fn (pref &Preferences) should_output_to_stdout() bool {
return pref.out_name.ends_with('/-') || pref.out_name.ends_with(r'\-')
}

pub fn arch_from_string(arch_str string) !Arch {
match arch_str {
'amd64', 'x86_64', 'x64', 'x86' { // amd64 recommended

return .amd64
}
'aarch64', 'arm64' { // arm64 recommended

return .arm64
}
'aarch32', 'arm32', 'arm' { // arm32 recommended

return .arm32
}
'rv64', 'riscv64', 'risc-v64', 'riscv', 'risc-v' { // rv64 recommended

return .rv64
}
'rv32', 'riscv32' { // rv32 recommended

return .rv32
}
'x86_32', 'x32', 'i386', 'IA-32', 'ia-32', 'ia32' { // i386 recommended

return .i386
}
'js', 'js_node' {
return .js_node
}
'js_browser' {
return .js_browser
}
'js_freestanding' {
return .js_freestanding
}
'wasm32', 'wasm' {
return .wasm32
}
'' {
return ._auto
}
else {
return error('invalid arch: ${arch_str}')
}
}
}

fn must_exist(path string) {
if !os.exists(path) {
eprintln_exit('v expects that `${path}` exists, but it does not')
Expand Down Expand Up @@ -1218,19 +1156,6 @@ pub fn cc_from_string(cc_str string) CompilerType {
return .gcc
}

pub fn get_host_arch() Arch {
// Note: we can not use `$if arch` here, because V skips cgen for the non
// current comptime branches by default, so there is a bootstrapping
// problem => the __V_architecture macro is used to resolve it.
// TODO: think about how to solve it for non C backends, perhaps we
// need a comptime `$if native {` too, and/or a mechanism to always
// generate all branches for specific functions?
if C.__V_architecture <= int(Arch._auto) || C.__V_architecture >= int(Arch._max) {
return Arch.amd64
}
return unsafe { Arch(C.__V_architecture) }
}

fn (mut prefs Preferences) parse_define(define string) {
define_parts := define.split('=')
prefs.diagnose_deprecated_defines(define_parts)
Expand Down

0 comments on commit 08b6cc7

Please sign in to comment.