Move some wasm constants and macros

This commit is contained in:
Brian Carroll 2022-11-13 09:03:26 +00:00
parent 63d9187343
commit 2e67bdf4d0
No known key found for this signature in database
GPG Key ID: 5C7B2EC4101703C0
4 changed files with 31 additions and 29 deletions

View File

@ -19,8 +19,8 @@ use crate::layout::{CallConv, ReturnMethod, WasmLayout};
use crate::low_level::{call_higher_order_lowlevel, LowLevelCall};
use crate::storage::{AddressValue, Storage, StoredValue, StoredVarKind};
use crate::{
copy_memory, round_up_to_alignment, CopyMemoryConfig, Env, DEBUG_SETTINGS, MEMORY_NAME,
PTR_SIZE, PTR_TYPE, TARGET_INFO,
copy_memory, CopyMemoryConfig, Env, DEBUG_SETTINGS, MEMORY_NAME, PTR_SIZE, PTR_TYPE,
TARGET_INFO,
};
use roc_wasm_module::linking::{DataSymbol, WasmObjectSymbol};
use roc_wasm_module::sections::{
@ -28,7 +28,8 @@ use roc_wasm_module::sections::{
MemorySection, NameSection,
};
use roc_wasm_module::{
code_builder, CodeBuilder, ExportType, LocalId, Signature, SymInfo, ValueType, WasmModule,
code_builder, round_up_to_alignment, CodeBuilder, ExportType, LocalId, Signature, SymInfo,
ValueType, WasmModule,
};
#[derive(Clone, Copy, Debug)]

View File

@ -35,8 +35,6 @@ const PTR_SIZE: u32 = {
};
const PTR_TYPE: ValueType = ValueType::I32;
pub const STACK_POINTER_GLOBAL_ID: u32 = 0;
pub const FRAME_ALIGNMENT_BYTES: i32 = 16;
pub const MEMORY_NAME: &str = "memory";
pub const BUILTINS_IMPORT_MODULE_NAME: &str = "env";
pub const STACK_POINTER_NAME: &str = "__stack_pointer";
@ -234,26 +232,6 @@ pub fn copy_memory(code_builder: &mut CodeBuilder, config: CopyMemoryConfig) {
}
}
/// Round up to alignment_bytes (which must be a power of 2)
#[macro_export]
macro_rules! round_up_to_alignment {
($unaligned: expr, $alignment_bytes: expr) => {
if $alignment_bytes <= 1 {
$unaligned
} else if $alignment_bytes.count_ones() != 1 {
internal_error!(
"Cannot align to {} bytes. Not a power of 2.",
$alignment_bytes
);
} else {
let mut aligned = $unaligned;
aligned += $alignment_bytes - 1; // if lower bits are non-zero, push it over the next boundary
aligned &= !$alignment_bytes + 1; // mask with a flag that has upper bits 1, lower bits 0
aligned
}
};
}
pub struct WasmDebugSettings {
proc_start_end: bool,
user_procs_ir: bool,

View File

@ -7,8 +7,10 @@ use roc_module::symbol::Symbol;
use roc_mono::layout::{Layout, STLayoutInterner};
use crate::layout::{CallConv, ReturnMethod, StackMemoryFormat, WasmLayout};
use crate::{copy_memory, round_up_to_alignment, CopyMemoryConfig, PTR_TYPE};
use roc_wasm_module::{Align, CodeBuilder, LocalId, ValueType, VmSymbolState};
use crate::{copy_memory, CopyMemoryConfig, PTR_TYPE};
use roc_wasm_module::{
round_up_to_alignment, Align, CodeBuilder, LocalId, ValueType, VmSymbolState,
};
pub enum StoredVarKind {
Variable,

View File

@ -14,8 +14,6 @@ pub use sections::{ConstExpr, Export, ExportType, Global, GlobalType, Signature}
use bitvec::vec::BitVec;
use bumpalo::{collections::Vec, Bump};
use crate::DEBUG_SETTINGS;
use self::linking::{IndexRelocType, LinkingSection, RelocationSection, WasmObjectSymbol};
use self::parse::{Parse, ParseError};
use self::sections::{
@ -25,6 +23,9 @@ use self::sections::{
};
use self::serialize::{SerialBuffer, Serialize};
pub const STACK_POINTER_GLOBAL_ID: u32 = 0;
pub const FRAME_ALIGNMENT_BYTES: i32 = 16;
/// A representation of the WebAssembly binary file format
/// https://webassembly.github.io/spec/core/binary/modules.html
#[derive(Debug)]
@ -593,3 +594,23 @@ impl<'a> WasmModule<'a> {
)
}
}
/// Round up to alignment_bytes (which must be a power of 2)
#[macro_export]
macro_rules! round_up_to_alignment {
($unaligned: expr, $alignment_bytes: expr) => {
if $alignment_bytes <= 1 {
$unaligned
} else if $alignment_bytes.count_ones() != 1 {
internal_error!(
"Cannot align to {} bytes. Not a power of 2.",
$alignment_bytes
);
} else {
let mut aligned = $unaligned;
aligned += $alignment_bytes - 1; // if lower bits are non-zero, push it over the next boundary
aligned &= !$alignment_bytes + 1; // mask with a flag that has upper bits 1, lower bits 0
aligned
}
};
}