assembler/lib.rs
1// This file is part of assembler. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/assembler/master/COPYRIGHT. No part of assembler, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
2// Copyright © 2018 The developers of assembler. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/assembler/master/COPYRIGHT.
3
4
5#![allow(non_snake_case)]
6#![allow(non_upper_case_globals)]
7#![allow(non_camel_case_types)]
8#![deny(missing_docs)]
9#![deny(unreachable_patterns)]
10#![feature(core_intrinsics)]
11
12
13//! #assembler
14//!
15//! This is a new code base to efficiently assemble 64-bit x86 (AMD64 long mode) machine code at runtime.
16//!
17//! Instructions are emitted by using the mnemonic-like methods on the `InstructionStream`.
18//! These are designed to work with Rust's release build optimizations to compile down to just a sequence of byte stores to memory.
19//!
20//! In addition, labels are supported; use of short (8-bit) labelled jumps is supported, and, where possible, are used.
21//!
22//! ## Example Usage
23//!
24//! ```
25//! extern crate assembler;
26//!
27//! use ::assembler::*;
28//! use ::assembler::mnemonic_parameter_types::BranchHint;
29//! use ::assembler::mnemonic_parameter_types::Label;
30//! use ::assembler::mnemonic_parameter_types::memory_operands::*;
31//! use ::assembler::mnemonic_parameter_types::immediates::*;
32//! use ::assembler::mnemonic_parameter_types::registers::*;
33//!
34//! fn main()
35//! {
36//! // SOME_LENGTH will be rounded up to the nearest power of two, with a minimum of the page size (4Kb).
37//! const SOME_LENGTH: usize = 4096;
38//! let memory_map = ExecutableAnonymousMemoryMap::new(SOME_LENGTH).unwrap();
39//!
40//! const SOME_LIKELY_NUMBER_OF_LABELS: usize = 512;
41//! let instruction_stream = memory_map.instruction_stream(SOME_LIKELY_NUMBER_OF_LABELS);
42//!
43//! instruction_stream.mov_...();
44//!
45//! let label = instruction_stream.create_and_attach_label();
46//!
47//! instruction_stream.jmp_Label(label)?;
48//!
49//! let function_pointer = instruction_stream.nullary_function::<()>():
50//!
51//! instruction_stream.finish();
52//!
53//! function_pointer();
54//! }
55//!
56//! ```
57
58
59extern crate libc;
60#[macro_use] extern crate likely;
61
62
63use self::mnemonic_parameter_types::*;
64use self::mnemonic_parameter_types::immediates::*;
65use self::mnemonic_parameter_types::memory::*;
66use self::mnemonic_parameter_types::memory_offsets::*;
67use self::mnemonic_parameter_types::registers::*;
68use self::mnemonic_parameter_types::relative_addresses::*;
69use ::libc::*;
70use ::std::alloc::alloc;
71use ::std::alloc::realloc;
72use ::std::alloc::dealloc;
73use ::std::alloc::Layout;
74use ::std::error::Error;
75use ::std::fmt;
76use ::std::fmt::Display;
77use ::std::fmt::Formatter;
78use ::std::io;
79use ::std::mem::align_of;
80use ::std::mem::size_of;
81use ::std::mem::transmute;
82use ::std::ops::Add;
83use ::std::ops::AddAssign;
84use ::std::ops::BitAnd;
85use ::std::ops::BitAndAssign;
86use ::std::ops::BitOr;
87use ::std::ops::BitOrAssign;
88use ::std::ops::BitXor;
89use ::std::ops::BitXorAssign;
90use ::std::ops::Div;
91use ::std::ops::DivAssign;
92use ::std::ops::Mul;
93use ::std::ops::MulAssign;
94use ::std::ops::Neg;
95use ::std::ops::Not;
96use ::std::ops::Rem;
97use ::std::ops::RemAssign;
98use ::std::ops::Shl;
99use ::std::ops::ShlAssign;
100use ::std::ops::Shr;
101use ::std::ops::ShrAssign;
102use ::std::ops::Sub;
103use ::std::ops::SubAssign;
104use ::std::ptr::copy_nonoverlapping;
105use ::std::ptr::NonNull;
106use ::std::ptr::null_mut;
107use ::std::slice::from_raw_parts;
108
109
110/// Mnemonic parameter types.
111pub mod mnemonic_parameter_types;
112
113#[cfg(test)]
114mod tests;
115
116
117include!("ByteEmitter.rs");
118include!("Displacement.rs");
119include!("ExecutableAnonymousMemoryMap.rs");
120include!("ExecutableAnonymousMemoryMapCreationError.rs");
121include!("InstructionPointer.rs");
122include!("InstructionPointerValidity.rs");
123include!("InstructionStream.rs");
124include!("InstructionStreamHints.rs");
125include!("LabelledLocations.rs");
126include!("NearJmpResult.rs");
127include!("ShortJmpResult.rs");