Move SourceLoc into compiler

This commit is contained in:
Syrus
2020-04-21 23:05:58 -07:00
parent 68b500ad84
commit b360bd40bb
9 changed files with 13 additions and 12 deletions

View File

@@ -1,9 +1,9 @@
//! Data structures to provide transformation of the source
// addresses of a WebAssembly module into the native code.
use crate::sourceloc::SourceLoc;
use crate::std::vec::Vec;
use serde::{Deserialize, Serialize};
use wasm_common::SourceLoc;
/// Single source location to generated address mapping.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]

View File

@@ -48,6 +48,7 @@ mod trap;
mod unwind;
#[macro_use]
mod translator;
mod sourceloc;
pub use crate::address_map::{FunctionAddressMap, InstructionAddressMap};
pub use crate::compiler::Compiler;
@@ -56,6 +57,7 @@ pub use crate::errors::CompileError;
pub use crate::function::{Compilation, CompiledFunction, Functions};
pub use crate::jump_table::{JumpTable, JumpTableOffsets};
pub use crate::relocation::{Relocation, RelocationKind, RelocationTarget, Relocations};
pub use crate::sourceloc::SourceLoc;
pub use crate::translator::{
to_wasm_error, translate_module, FunctionBodyData, ModuleEnvironment, ModuleTranslation,
ModuleTranslationState, WasmError, WasmResult,

View File

@@ -0,0 +1,61 @@
//! Source locations.
//!
//! A [`SourceLoc`] determines the position of a certain instruction
//! relative to the WebAssembly module. This is used mainly for debugging
//! and tracing errors.
use core::fmt;
use serde::{Deserialize, Serialize};
/// A source location.
///
/// The default source location uses the all-ones bit pattern `!0`. It is used for instructions
/// that can't be given a real source location.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct SourceLoc(u32);
impl SourceLoc {
/// Create a new source location with the given bits.
pub fn new(bits: u32) -> Self {
Self(bits)
}
/// Is this the default source location?
pub fn is_default(self) -> bool {
self == Default::default()
}
/// Read the bits of this source location.
pub fn bits(self) -> u32 {
self.0
}
}
impl Default for SourceLoc {
fn default() -> Self {
Self(!0)
}
}
impl fmt::Display for SourceLoc {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.is_default() {
write!(f, "0x-")
} else {
write!(f, "0x{:04x}", self.0)
}
}
}
#[cfg(test)]
mod tests {
use super::SourceLoc;
#[test]
fn display() {
assert_eq!(SourceLoc::default().to_string(), "0x-");
assert_eq!(SourceLoc::new(0).to_string(), "0x0000");
assert_eq!(SourceLoc::new(16).to_string(), "0x0010");
assert_eq!(SourceLoc::new(0xabcdef).to_string(), "0xabcdef");
}
}

View File

@@ -1,6 +1,6 @@
use crate::sourceloc::SourceLoc;
use crate::CodeOffset;
use serde::{Deserialize, Serialize};
use wasm_common::SourceLoc;
use wasmer_runtime::TrapCode;
/// Information about trap.