llvm-project
218 строк · 7.7 Кб
1!===-- module/__fortran_builtins.f90 ---------------------------------------===!
2!
3! Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4! See https://llvm.org/LICENSE.txt for license information.
5! SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6!
7!===------------------------------------------------------------------------===!
8
9include '../include/flang/Runtime/magic-numbers.h'
10
11! These naming shenanigans prevent names from Fortran intrinsic modules
12! from being usable on INTRINSIC statements, and force the program
13! to USE the standard intrinsic modules in order to access the
14! standard names of the procedures.
15module __fortran_builtins
16implicit none
17
18! Set PRIVATE by default to explicitly only export what is meant
19! to be exported by this MODULE.
20private
21
22intrinsic :: __builtin_c_loc
23public :: __builtin_c_loc
24
25intrinsic :: __builtin_c_f_pointer
26public :: __builtin_c_f_pointer
27
28intrinsic :: sizeof ! extension
29public :: sizeof
30
31intrinsic :: selected_int_kind
32integer, parameter :: int64 = selected_int_kind(18)
33
34type, bind(c), public :: __builtin_c_ptr
35integer(kind=int64), private :: __address
36end type
37
38type, bind(c), public :: __builtin_c_funptr
39integer(kind=int64), private :: __address
40end type
41
42type, public :: __builtin_event_type
43integer(kind=int64), private :: __count
44end type
45
46type, public :: __builtin_notify_type
47integer(kind=int64), private :: __count
48end type
49
50type, public :: __builtin_lock_type
51integer(kind=int64), private :: __count
52end type
53
54type, public :: __builtin_ieee_flag_type
55integer(kind=1), private :: flag = 0
56end type
57
58type(__builtin_ieee_flag_type), parameter, public :: &
59__builtin_ieee_invalid = &
60__builtin_ieee_flag_type(_FORTRAN_RUNTIME_IEEE_INVALID), &
61__builtin_ieee_overflow = &
62__builtin_ieee_flag_type(_FORTRAN_RUNTIME_IEEE_OVERFLOW), &
63__builtin_ieee_divide_by_zero = &
64__builtin_ieee_flag_type(_FORTRAN_RUNTIME_IEEE_DIVIDE_BY_ZERO), &
65__builtin_ieee_underflow = &
66__builtin_ieee_flag_type(_FORTRAN_RUNTIME_IEEE_UNDERFLOW), &
67__builtin_ieee_inexact = &
68__builtin_ieee_flag_type(_FORTRAN_RUNTIME_IEEE_INEXACT), &
69__builtin_ieee_denorm = & ! extension
70__builtin_ieee_flag_type(_FORTRAN_RUNTIME_IEEE_DENORM)
71
72type, public :: __builtin_ieee_round_type
73integer(kind=1), private :: mode = 0
74end type
75
76type(__builtin_ieee_round_type), parameter, public :: &
77__builtin_ieee_to_zero = &
78__builtin_ieee_round_type(_FORTRAN_RUNTIME_IEEE_TO_ZERO), &
79__builtin_ieee_nearest = &
80__builtin_ieee_round_type(_FORTRAN_RUNTIME_IEEE_NEAREST), &
81__builtin_ieee_up = &
82__builtin_ieee_round_type(_FORTRAN_RUNTIME_IEEE_UP), &
83__builtin_ieee_down = &
84__builtin_ieee_round_type(_FORTRAN_RUNTIME_IEEE_DOWN), &
85__builtin_ieee_away = &
86__builtin_ieee_round_type(_FORTRAN_RUNTIME_IEEE_AWAY), &
87__builtin_ieee_other = &
88__builtin_ieee_round_type(_FORTRAN_RUNTIME_IEEE_OTHER)
89
90type, public :: __builtin_team_type
91integer(kind=int64), private :: __id
92end type
93
94integer, parameter, public :: __builtin_atomic_int_kind = selected_int_kind(18)
95integer, parameter, public :: &
96__builtin_atomic_logical_kind = __builtin_atomic_int_kind
97
98type, public :: __builtin_dim3
99integer :: x=1, y=1, z=1
100end type
101type(__builtin_dim3), public :: &
102__builtin_threadIdx, __builtin_blockDim, __builtin_blockIdx, &
103__builtin_gridDim
104integer, parameter, public :: __builtin_warpsize = 32
105
106intrinsic :: __builtin_fma
107intrinsic :: __builtin_ieee_is_nan, __builtin_ieee_is_negative, &
108__builtin_ieee_is_normal
109intrinsic :: __builtin_ieee_next_after, __builtin_ieee_next_down, &
110__builtin_ieee_next_up
111intrinsic :: scale ! for ieee_scalb
112intrinsic :: __builtin_ieee_selected_real_kind
113intrinsic :: __builtin_ieee_support_datatype, &
114__builtin_ieee_support_denormal, __builtin_ieee_support_divide, &
115__builtin_ieee_support_flag, __builtin_ieee_support_halting, &
116__builtin_ieee_support_inf, __builtin_ieee_support_io, &
117__builtin_ieee_support_nan, __builtin_ieee_support_rounding, &
118__builtin_ieee_support_sqrt, &
119__builtin_ieee_support_standard, __builtin_ieee_support_subnormal, &
120__builtin_ieee_support_underflow_control
121public :: __builtin_fma
122public :: __builtin_ieee_is_nan, __builtin_ieee_is_negative, &
123__builtin_ieee_is_normal
124public :: __builtin_ieee_next_after, __builtin_ieee_next_down, &
125__builtin_ieee_next_up
126public :: scale ! for ieee_scalb
127public :: __builtin_ieee_selected_real_kind
128public :: __builtin_ieee_support_datatype, &
129__builtin_ieee_support_denormal, __builtin_ieee_support_divide, &
130__builtin_ieee_support_flag, __builtin_ieee_support_halting, &
131__builtin_ieee_support_inf, __builtin_ieee_support_io, &
132__builtin_ieee_support_nan, __builtin_ieee_support_rounding, &
133__builtin_ieee_support_sqrt, &
134__builtin_ieee_support_standard, __builtin_ieee_support_subnormal, &
135__builtin_ieee_support_underflow_control
136
137type :: __force_derived_type_instantiations
138type(__builtin_c_ptr) :: c_ptr
139type(__builtin_c_funptr) :: c_funptr
140type(__builtin_event_type) :: event_type
141type(__builtin_lock_type) :: lock_type
142type(__builtin_team_type) :: team_type
143end type
144
145intrinsic :: __builtin_compiler_options, __builtin_compiler_version
146public :: __builtin_compiler_options, __builtin_compiler_version
147
148interface operator(==)
149module procedure __builtin_c_ptr_eq
150end interface
151public :: operator(==)
152
153interface operator(/=)
154module procedure __builtin_c_ptr_ne
155end interface
156public :: operator(/=)
157
158interface __builtin_c_associated
159module procedure c_associated_c_ptr
160module procedure c_associated_c_funptr
161end interface
162public :: __builtin_c_associated
163! private :: c_associated_c_ptr, c_associated_c_funptr
164
165type(__builtin_c_ptr), parameter, public :: __builtin_c_null_ptr = __builtin_c_ptr(0)
166type(__builtin_c_funptr), parameter, public :: &
167__builtin_c_null_funptr = __builtin_c_funptr(0)
168
169public :: __builtin_c_ptr_eq
170public :: __builtin_c_ptr_ne
171public :: __builtin_c_funloc
172
173contains
174
175elemental logical function __builtin_c_ptr_eq(x, y)
176type(__builtin_c_ptr), intent(in) :: x, y
177__builtin_c_ptr_eq = x%__address == y%__address
178end function
179
180elemental logical function __builtin_c_ptr_ne(x, y)
181type(__builtin_c_ptr), intent(in) :: x, y
182__builtin_c_ptr_ne = x%__address /= y%__address
183end function
184
185! Semantics has some special-case code that allows c_funloc()
186! to appear in a specification expression and exempts it
187! from the requirement that "x" be a pure dummy procedure.
188pure function __builtin_c_funloc(x)
189type(__builtin_c_funptr) :: __builtin_c_funloc
190external :: x
191__builtin_c_funloc = __builtin_c_funptr(loc(x))
192end function
193
194pure logical function c_associated_c_ptr(c_ptr_1, c_ptr_2)
195type(__builtin_c_ptr), intent(in) :: c_ptr_1
196type(__builtin_c_ptr), intent(in), optional :: c_ptr_2
197if (c_ptr_1%__address == __builtin_c_null_ptr%__address) then
198c_associated_c_ptr = .false.
199else if (present(c_ptr_2)) then
200c_associated_c_ptr = c_ptr_1%__address == c_ptr_2%__address
201else
202c_associated_c_ptr = .true.
203end if
204end function c_associated_c_ptr
205
206pure logical function c_associated_c_funptr(c_ptr_1, c_ptr_2)
207type(__builtin_c_funptr), intent(in) :: c_ptr_1
208type(__builtin_c_funptr), intent(in), optional :: c_ptr_2
209if (c_ptr_1%__address == __builtin_c_null_ptr%__address) then
210c_associated_c_funptr = .false.
211else if (present(c_ptr_2)) then
212c_associated_c_funptr = c_ptr_1%__address == c_ptr_2%__address
213else
214c_associated_c_funptr = .true.
215end if
216end function c_associated_c_funptr
217
218end module
219