1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 /*! rustc compiler intrinsics.
13 The corresponding definitions are in librustc/middle/trans/foreign.rs.
17 The atomic intrinsics provide common atomic operations on machine
18 words, with multiple possible memory orderings. They obey the same
19 semantics as C++11. See the LLVM documentation on [[atomics]].
21 [atomics]: http://llvm.org/docs/Atomics.html
23 A quick refresher on memory ordering:
25 * Acquire - a barrier for acquiring a lock. Subsequent reads and writes
26 take place after the barrier.
27 * Release - a barrier for releasing a lock. Preceding reads and writes
28 take place before the barrier.
29 * Sequentially consistent - sequentially consistent operations are
30 guaranteed to happen in order. This is the standard mode for working
31 with atomic types and is equivalent to Java's `volatile`.
35 // This is needed to prevent duplicate lang item definitions.
37 pub use realstd::unstable::intrinsics::{TyDesc, Opaque, TyVisitor, TypeId};
39 pub type GlueFn = extern "Rust" fn(*i8);
41 // NB: this has to be kept in sync with `type_desc` in `rt`
51 // Called on a copy of a value of type `T` *after* memcpy
54 // Called when a value of type `T` is no longer needed
57 // Called by drop glue when a value of type `T` can be freed
60 // Called by reflection visitor to visit a value of type `T`
63 // If T represents a box pointer (`@U` or `~U`), then
64 // `borrow_offset` is the amount that the pointer must be adjusted
65 // to find the payload. This is always derivable from the type
66 // `U`, but in the case of `@Trait` or `~Trait` objects, the type
70 // Name corresponding to the type
83 fn visit_bot(&mut self) -> bool;
84 fn visit_nil(&mut self) -> bool;
85 fn visit_bool(&mut self) -> bool;
87 fn visit_int(&mut self) -> bool;
88 fn visit_i8(&mut self) -> bool;
89 fn visit_i16(&mut self) -> bool;
90 fn visit_i32(&mut self) -> bool;
91 fn visit_i64(&mut self) -> bool;
93 fn visit_uint(&mut self) -> bool;
94 fn visit_u8(&mut self) -> bool;
95 fn visit_u16(&mut self) -> bool;
96 fn visit_u32(&mut self) -> bool;
97 fn visit_u64(&mut self) -> bool;
99 fn visit_f32(&mut self) -> bool;
100 fn visit_f64(&mut self) -> bool;
102 fn visit_char(&mut self) -> bool;
104 fn visit_estr_box(&mut self) -> bool;
105 fn visit_estr_uniq(&mut self) -> bool;
106 fn visit_estr_slice(&mut self) -> bool;
107 fn visit_estr_fixed(&mut self, n: uint, sz: uint, align: uint) -> bool;
109 fn visit_box(&mut self, mtbl: uint, inner: *TyDesc) -> bool;
110 fn visit_uniq(&mut self, mtbl: uint, inner: *TyDesc) -> bool;
111 fn visit_uniq_managed(&mut self, mtbl: uint, inner: *TyDesc) -> bool;
112 fn visit_ptr(&mut self, mtbl: uint, inner: *TyDesc) -> bool;
113 fn visit_rptr(&mut self, mtbl: uint, inner: *TyDesc) -> bool;
115 fn visit_vec(&mut self, mtbl: uint, inner: *TyDesc) -> bool;
116 fn visit_unboxed_vec(&mut self, mtbl: uint, inner: *TyDesc) -> bool;
117 fn visit_evec_box(&mut self, mtbl: uint, inner: *TyDesc) -> bool;
118 fn visit_evec_uniq(&mut self, mtbl: uint, inner: *TyDesc) -> bool;
119 fn visit_evec_uniq_managed(&mut self, mtbl: uint, inner: *TyDesc) -> bool;
120 fn visit_evec_slice(&mut self, mtbl: uint, inner: *TyDesc) -> bool;
121 fn visit_evec_fixed(&mut self, n: uint, sz: uint, align: uint,
122 mtbl: uint, inner: *TyDesc) -> bool;
124 fn visit_enter_rec(&mut self, n_fields: uint,
125 sz: uint, align: uint) -> bool;
126 fn visit_rec_field(&mut self, i: uint, name: &str,
127 mtbl: uint, inner: *TyDesc) -> bool;
128 fn visit_leave_rec(&mut self, n_fields: uint,
129 sz: uint, align: uint) -> bool;
131 fn visit_enter_class(&mut self, name: &str, named_fields: bool, n_fields: uint,
132 sz: uint, align: uint) -> bool;
133 fn visit_class_field(&mut self, i: uint, name: &str, named: bool,
134 mtbl: uint, inner: *TyDesc) -> bool;
135 fn visit_leave_class(&mut self, name: &str, named_fields: bool, n_fields: uint,
136 sz: uint, align: uint) -> bool;
138 fn visit_enter_tup(&mut self, n_fields: uint,
139 sz: uint, align: uint) -> bool;
140 fn visit_tup_field(&mut self, i: uint, inner: *TyDesc) -> bool;
141 fn visit_leave_tup(&mut self, n_fields: uint,
142 sz: uint, align: uint) -> bool;
144 fn visit_enter_enum(&mut self, n_variants: uint,
145 get_disr: extern unsafe fn(ptr: *Opaque) -> Disr,
146 sz: uint, align: uint) -> bool;
147 fn visit_enter_enum_variant(&mut self, variant: uint,
151 fn visit_enum_variant_field(&mut self, i: uint, offset: uint, inner: *TyDesc) -> bool;
152 fn visit_leave_enum_variant(&mut self, variant: uint,
156 fn visit_leave_enum(&mut self, n_variants: uint,
157 get_disr: extern unsafe fn(ptr: *Opaque) -> Disr,
158 sz: uint, align: uint) -> bool;
160 fn visit_enter_fn(&mut self, purity: uint, proto: uint,
161 n_inputs: uint, retstyle: uint) -> bool;
162 fn visit_fn_input(&mut self, i: uint, mode: uint, inner: *TyDesc) -> bool;
163 fn visit_fn_output(&mut self, retstyle: uint, variadic: bool, inner: *TyDesc) -> bool;
164 fn visit_leave_fn(&mut self, purity: uint, proto: uint,
165 n_inputs: uint, retstyle: uint) -> bool;
167 fn visit_trait(&mut self, name: &str) -> bool;
168 fn visit_param(&mut self, i: uint) -> bool;
169 fn visit_self(&mut self) -> bool;
170 fn visit_type(&mut self) -> bool;
171 fn visit_opaque_box(&mut self) -> bool;
172 fn visit_closure_ptr(&mut self, ck: uint) -> bool;
175 extern "rust-intrinsic" {
176 /// Abort the execution of the process.
179 /// Execute a breakpoint trap, for inspection by a debugger.
182 /// Atomic compare and exchange, sequentially consistent.
183 pub fn atomic_cxchg(dst: &mut int, old: int, src: int) -> int;
184 /// Atomic compare and exchange, acquire ordering.
185 pub fn atomic_cxchg_acq(dst: &mut int, old: int, src: int) -> int;
186 /// Atomic compare and exchange, release ordering.
187 pub fn atomic_cxchg_rel(dst: &mut int, old: int, src: int) -> int;
189 pub fn atomic_cxchg_acqrel(dst: &mut int, old: int, src: int) -> int;
190 pub fn atomic_cxchg_relaxed(dst: &mut int, old: int, src: int) -> int;
193 /// Atomic load, sequentially consistent.
194 pub fn atomic_load(src: &int) -> int;
195 /// Atomic load, acquire ordering.
196 pub fn atomic_load_acq(src: &int) -> int;
198 pub fn atomic_load_relaxed(src: &int) -> int;
200 /// Atomic store, sequentially consistent.
201 pub fn atomic_store(dst: &mut int, val: int);
202 /// Atomic store, release ordering.
203 pub fn atomic_store_rel(dst: &mut int, val: int);
205 pub fn atomic_store_relaxed(dst: &mut int, val: int);
207 /// Atomic exchange, sequentially consistent.
208 pub fn atomic_xchg(dst: &mut int, src: int) -> int;
209 /// Atomic exchange, acquire ordering.
210 pub fn atomic_xchg_acq(dst: &mut int, src: int) -> int;
211 /// Atomic exchange, release ordering.
212 pub fn atomic_xchg_rel(dst: &mut int, src: int) -> int;
213 pub fn atomic_xchg_acqrel(dst: &mut int, src: int) -> int;
214 pub fn atomic_xchg_relaxed(dst: &mut int, src: int) -> int;
216 /// Atomic addition, sequentially consistent.
217 pub fn atomic_xadd(dst: &mut int, src: int) -> int;
218 /// Atomic addition, acquire ordering.
219 pub fn atomic_xadd_acq(dst: &mut int, src: int) -> int;
220 /// Atomic addition, release ordering.
221 pub fn atomic_xadd_rel(dst: &mut int, src: int) -> int;
222 pub fn atomic_xadd_acqrel(dst: &mut int, src: int) -> int;
223 pub fn atomic_xadd_relaxed(dst: &mut int, src: int) -> int;
225 /// Atomic subtraction, sequentially consistent.
226 pub fn atomic_xsub(dst: &mut int, src: int) -> int;
227 /// Atomic subtraction, acquire ordering.
228 pub fn atomic_xsub_acq(dst: &mut int, src: int) -> int;
229 /// Atomic subtraction, release ordering.
230 pub fn atomic_xsub_rel(dst: &mut int, src: int) -> int;
231 pub fn atomic_xsub_acqrel(dst: &mut int, src: int) -> int;
232 pub fn atomic_xsub_relaxed(dst: &mut int, src: int) -> int;
234 pub fn atomic_and(dst: &mut int, src: int) -> int;
235 pub fn atomic_and_acq(dst: &mut int, src: int) -> int;
236 pub fn atomic_and_rel(dst: &mut int, src: int) -> int;
237 pub fn atomic_and_acqrel(dst: &mut int, src: int) -> int;
238 pub fn atomic_and_relaxed(dst: &mut int, src: int) -> int;
240 pub fn atomic_nand(dst: &mut int, src: int) -> int;
241 pub fn atomic_nand_acq(dst: &mut int, src: int) -> int;
242 pub fn atomic_nand_rel(dst: &mut int, src: int) -> int;
243 pub fn atomic_nand_acqrel(dst: &mut int, src: int) -> int;
244 pub fn atomic_nand_relaxed(dst: &mut int, src: int) -> int;
246 pub fn atomic_or(dst: &mut int, src: int) -> int;
247 pub fn atomic_or_acq(dst: &mut int, src: int) -> int;
248 pub fn atomic_or_rel(dst: &mut int, src: int) -> int;
249 pub fn atomic_or_acqrel(dst: &mut int, src: int) -> int;
250 pub fn atomic_or_relaxed(dst: &mut int, src: int) -> int;
252 pub fn atomic_xor(dst: &mut int, src: int) -> int;
253 pub fn atomic_xor_acq(dst: &mut int, src: int) -> int;
254 pub fn atomic_xor_rel(dst: &mut int, src: int) -> int;
255 pub fn atomic_xor_acqrel(dst: &mut int, src: int) -> int;
256 pub fn atomic_xor_relaxed(dst: &mut int, src: int) -> int;
258 pub fn atomic_max(dst: &mut int, src: int) -> int;
259 pub fn atomic_max_acq(dst: &mut int, src: int) -> int;
260 pub fn atomic_max_rel(dst: &mut int, src: int) -> int;
261 pub fn atomic_max_acqrel(dst: &mut int, src: int) -> int;
262 pub fn atomic_max_relaxed(dst: &mut int, src: int) -> int;
264 pub fn atomic_min(dst: &mut int, src: int) -> int;
265 pub fn atomic_min_acq(dst: &mut int, src: int) -> int;
266 pub fn atomic_min_rel(dst: &mut int, src: int) -> int;
267 pub fn atomic_min_acqrel(dst: &mut int, src: int) -> int;
268 pub fn atomic_min_relaxed(dst: &mut int, src: int) -> int;
270 pub fn atomic_umin(dst: &mut int, src: int) -> int;
271 pub fn atomic_umin_acq(dst: &mut int, src: int) -> int;
272 pub fn atomic_umin_rel(dst: &mut int, src: int) -> int;
273 pub fn atomic_umin_acqrel(dst: &mut int, src: int) -> int;
274 pub fn atomic_umin_relaxed(dst: &mut int, src: int) -> int;
276 pub fn atomic_umax(dst: &mut int, src: int) -> int;
277 pub fn atomic_umax_acq(dst: &mut int, src: int) -> int;
278 pub fn atomic_umax_rel(dst: &mut int, src: int) -> int;
279 pub fn atomic_umax_acqrel(dst: &mut int, src: int) -> int;
280 pub fn atomic_umax_relaxed(dst: &mut int, src: int) -> int;
282 pub fn atomic_fence();
283 pub fn atomic_fence_acq();
284 pub fn atomic_fence_rel();
285 pub fn atomic_fence_acqrel();
287 /// The size of a type in bytes.
289 /// This is the exact number of bytes in memory taken up by a
290 /// value of the given type. In other words, a memset of this size
291 /// would *exactly* overwrite a value. When laid out in vectors
292 /// and structures there may be additional padding between
294 pub fn size_of<T>() -> uint;
296 /// Move a value to a memory location containing a value.
298 /// Drop glue is run on the destination, which must contain a
299 /// valid Rust value.
300 pub fn move_val<T>(dst: &mut T, src: T);
302 /// Move a value to an uninitialized memory location.
304 /// Drop glue is not run on the destination.
305 pub fn move_val_init<T>(dst: &mut T, src: T);
307 pub fn min_align_of<T>() -> uint;
308 pub fn pref_align_of<T>() -> uint;
310 /// Get a static pointer to a type descriptor.
311 pub fn get_tydesc<T>() -> *TyDesc;
313 /// Gets an identifier which is globally unique to the specified type. This
314 /// function will return the same value for a type regardless of whichever
315 /// crate it is invoked in.
316 pub fn type_id<T: 'static>() -> TypeId;
319 /// Create a value initialized to zero.
321 /// `init` is unsafe because it returns a zeroed-out datum,
322 /// which is unsafe unless T is POD. We don't have a POD
323 /// kind yet. (See #4074).
324 pub fn init<T>() -> T;
326 /// Create an uninitialized value.
327 pub fn uninit<T>() -> T;
329 /// Move a value out of scope without running drop glue.
331 /// `forget` is unsafe because the caller is responsible for
332 /// ensuring the argument is deallocated already.
333 pub fn forget<T>(_: T) -> ();
334 pub fn transmute<T,U>(e: T) -> U;
336 /// Returns `true` if a type requires drop glue.
337 pub fn needs_drop<T>() -> bool;
339 /// Returns `true` if a type is managed (will be allocated on the local heap)
340 pub fn owns_managed<T>() -> bool;
342 pub fn visit_tydesc(td: *TyDesc, tv: &mut TyVisitor);
344 /// Get the address of the `__morestack` stack growth function.
345 pub fn morestack_addr() -> *();
347 /// Calculates the offset from a pointer. The offset *must* be in-bounds of
348 /// the object, or one-byte-past-the-end. An arithmetic overflow is also
349 /// undefined behaviour.
351 /// This is implemented as an intrinsic to avoid converting to and from an
352 /// integer, since the conversion would throw away aliasing information.
353 pub fn offset<T>(dst: *T, offset: int) -> *T;
355 /// Equivalent to the appropriate `llvm.memcpy.p0i8.0i8.*` intrinsic, with
356 /// a size of `count` * `size_of::<T>()` and an alignment of
357 /// `min_align_of::<T>()`
358 pub fn copy_nonoverlapping_memory<T>(dst: *mut T, src: *T, count: uint);
360 /// Equivalent to the appropriate `llvm.memmove.p0i8.0i8.*` intrinsic, with
361 /// a size of `count` * `size_of::<T>()` and an alignment of
362 /// `min_align_of::<T>()`
363 pub fn copy_memory<T>(dst: *mut T, src: *T, count: uint);
365 /// Equivalent to the appropriate `llvm.memset.p0i8.*` intrinsic, with a
366 /// size of `count` * `size_of::<T>()` and an alignment of
367 /// `min_align_of::<T>()`
368 pub fn set_memory<T>(dst: *mut T, val: u8, count: uint);
370 pub fn sqrtf32(x: f32) -> f32;
371 pub fn sqrtf64(x: f64) -> f64;
373 pub fn powif32(a: f32, x: i32) -> f32;
374 pub fn powif64(a: f64, x: i32) -> f64;
376 pub fn sinf32(x: f32) -> f32;
377 pub fn sinf64(x: f64) -> f64;
379 pub fn cosf32(x: f32) -> f32;
380 pub fn cosf64(x: f64) -> f64;
382 pub fn powf32(a: f32, x: f32) -> f32;
383 pub fn powf64(a: f64, x: f64) -> f64;
385 pub fn expf32(x: f32) -> f32;
386 pub fn expf64(x: f64) -> f64;
388 pub fn exp2f32(x: f32) -> f32;
389 pub fn exp2f64(x: f64) -> f64;
391 pub fn logf32(x: f32) -> f32;
392 pub fn logf64(x: f64) -> f64;
394 pub fn log10f32(x: f32) -> f32;
395 pub fn log10f64(x: f64) -> f64;
397 pub fn log2f32(x: f32) -> f32;
398 pub fn log2f64(x: f64) -> f64;
400 pub fn fmaf32(a: f32, b: f32, c: f32) -> f32;
401 pub fn fmaf64(a: f64, b: f64, c: f64) -> f64;
403 pub fn fabsf32(x: f32) -> f32;
404 pub fn fabsf64(x: f64) -> f64;
406 pub fn copysignf32(x: f32, y: f32) -> f32;
407 pub fn copysignf64(x: f64, y: f64) -> f64;
409 pub fn floorf32(x: f32) -> f32;
410 pub fn floorf64(x: f64) -> f64;
412 pub fn ceilf32(x: f32) -> f32;
413 pub fn ceilf64(x: f64) -> f64;
415 pub fn truncf32(x: f32) -> f32;
416 pub fn truncf64(x: f64) -> f64;
418 pub fn rintf32(x: f32) -> f32;
419 pub fn rintf64(x: f64) -> f64;
421 pub fn nearbyintf32(x: f32) -> f32;
422 pub fn nearbyintf64(x: f64) -> f64;
424 pub fn roundf32(x: f32) -> f32;
425 pub fn roundf64(x: f64) -> f64;
427 pub fn ctpop8(x: i8) -> i8;
428 pub fn ctpop16(x: i16) -> i16;
429 pub fn ctpop32(x: i32) -> i32;
430 pub fn ctpop64(x: i64) -> i64;
432 pub fn ctlz8(x: i8) -> i8;
433 pub fn ctlz16(x: i16) -> i16;
434 pub fn ctlz32(x: i32) -> i32;
435 pub fn ctlz64(x: i64) -> i64;
437 pub fn cttz8(x: i8) -> i8;
438 pub fn cttz16(x: i16) -> i16;
439 pub fn cttz32(x: i32) -> i32;
440 pub fn cttz64(x: i64) -> i64;
442 pub fn bswap16(x: i16) -> i16;
443 pub fn bswap32(x: i32) -> i32;
444 pub fn bswap64(x: i64) -> i64;
446 pub fn i8_add_with_overflow(x: i8, y: i8) -> (i8, bool);
447 pub fn i16_add_with_overflow(x: i16, y: i16) -> (i16, bool);
448 pub fn i32_add_with_overflow(x: i32, y: i32) -> (i32, bool);
449 pub fn i64_add_with_overflow(x: i64, y: i64) -> (i64, bool);
451 pub fn u8_add_with_overflow(x: u8, y: u8) -> (u8, bool);
452 pub fn u16_add_with_overflow(x: u16, y: u16) -> (u16, bool);
453 pub fn u32_add_with_overflow(x: u32, y: u32) -> (u32, bool);
454 pub fn u64_add_with_overflow(x: u64, y: u64) -> (u64, bool);
456 pub fn i8_sub_with_overflow(x: i8, y: i8) -> (i8, bool);
457 pub fn i16_sub_with_overflow(x: i16, y: i16) -> (i16, bool);
458 pub fn i32_sub_with_overflow(x: i32, y: i32) -> (i32, bool);
459 pub fn i64_sub_with_overflow(x: i64, y: i64) -> (i64, bool);
461 pub fn u8_sub_with_overflow(x: u8, y: u8) -> (u8, bool);
462 pub fn u16_sub_with_overflow(x: u16, y: u16) -> (u16, bool);
463 pub fn u32_sub_with_overflow(x: u32, y: u32) -> (u32, bool);
464 pub fn u64_sub_with_overflow(x: u64, y: u64) -> (u64, bool);
466 pub fn i8_mul_with_overflow(x: i8, y: i8) -> (i8, bool);
467 pub fn i16_mul_with_overflow(x: i16, y: i16) -> (i16, bool);
468 pub fn i32_mul_with_overflow(x: i32, y: i32) -> (i32, bool);
469 pub fn i64_mul_with_overflow(x: i64, y: i64) -> (i64, bool);
471 pub fn u8_mul_with_overflow(x: u8, y: u8) -> (u8, bool);
472 pub fn u16_mul_with_overflow(x: u16, y: u16) -> (u16, bool);
473 pub fn u32_mul_with_overflow(x: u32, y: u32) -> (u32, bool);
474 pub fn u64_mul_with_overflow(x: u64, y: u64) -> (u64, bool);
477 #[cfg(target_endian = "little")] pub fn to_le16(x: i16) -> i16 { x }
478 #[cfg(target_endian = "big")] pub fn to_le16(x: i16) -> i16 { unsafe { bswap16(x) } }
479 #[cfg(target_endian = "little")] pub fn to_le32(x: i32) -> i32 { x }
480 #[cfg(target_endian = "big")] pub fn to_le32(x: i32) -> i32 { unsafe { bswap32(x) } }
481 #[cfg(target_endian = "little")] pub fn to_le64(x: i64) -> i64 { x }
482 #[cfg(target_endian = "big")] pub fn to_le64(x: i64) -> i64 { unsafe { bswap64(x) } }
484 #[cfg(target_endian = "little")] pub fn to_be16(x: i16) -> i16 { unsafe { bswap16(x) } }
485 #[cfg(target_endian = "big")] pub fn to_be16(x: i16) -> i16 { x }
486 #[cfg(target_endian = "little")] pub fn to_be32(x: i32) -> i32 { unsafe { bswap32(x) } }
487 #[cfg(target_endian = "big")] pub fn to_be32(x: i32) -> i32 { x }
488 #[cfg(target_endian = "little")] pub fn to_be64(x: i64) -> i64 { unsafe { bswap64(x) } }
489 #[cfg(target_endian = "big")] pub fn to_be64(x: i64) -> i64 { x }
492 /// `TypeId` represents a globally unique identifier for a type
493 #[lang="type_id"] // This needs to be kept in lockstep with the code in trans/intrinsic.rs and
494 // middle/lang_items.rs
495 #[deriving(Eq, IterBytes)]
503 /// Returns the `TypeId` of the type this generic function has been instantiated with
504 pub fn of<T: 'static>() -> TypeId {
505 unsafe { type_id::<T>() }