Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start decoupling llvm bindings, run cargo fmt #29

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ edition = "2018"

[dependencies]
flexi_logger = "0.14.4"
# FIXME: Migrate
# inkwell = { git = "https://github.com/TheDan64/inkwell.git", branch = "llvm10-0" }
llvm-alt = { git = "https://github.com/gwsystems/llvm-rs", branch = "sfbase"}
log = "0.4.8"
structopt = "0.3.2"
Expand Down
2 changes: 1 addition & 1 deletion runtime/libc/wasmception_backing.c
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ i32 wasm_writev(i32 fd, i32 iov_offset, i32 iovcnt) {
i32 len = iov[i].len;
void* ptr = get_memory_ptr_void(iov[i].base_offset, len);

printf("%.*s", len, ptr);
printf("%.*s", len, (char*) ptr);
sum += len;
}
return sum;
Expand Down
3 changes: 2 additions & 1 deletion src/codegen/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,8 @@ pub fn compile_block<'a, 'b>(
&[table_index, type_index.compile(m_ctx.llvm_ctx)],
);
// Then cast it from a void pointer to a function pointer
let f_type = PointerType::new(wasm_func_type_to_llvm_type(m_ctx.llvm_ctx, f_type));
let f_type =
PointerType::new(wasm_func_type_to_llvm_type(m_ctx.llvm_ctx, f_type).itype());
let f_ptr = b.build_bit_cast(f_ptr_as_void, f_type);

let result = b.build_value_call(f_ptr, &args);
Expand Down
10 changes: 7 additions & 3 deletions src/codegen/breakout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ impl<'a> BreakoutTarget<'a> {
}
}

pub fn new_wrapped(bb: &'a BasicBlock, result_type: Option<TypeOrFuncType>) -> WBreakoutTarget<'a> {
pub fn new_wrapped(
bb: &'a BasicBlock,
result_type: Option<TypeOrFuncType>,
) -> WBreakoutTarget<'a> {
let bt = Self::new(bb, result_type);
Rc::new(RefCell::new(bt))
}
Expand Down Expand Up @@ -68,7 +71,7 @@ impl<'a> BreakoutTarget<'a> {
// We probably should emit an unreachable instruction in this case, but that might mess up future compilation
// Instead we just produce zeroes, and hope llvm notices this is unreachable code
// TODO: Figure out if we can instead produce an unreachable result
new_locals.push(llvm_type_to_zeroed_value(ctx, local_type));
new_locals.push(llvm_type_to_zeroed_value(ctx, local_type).ivalue());
} else if jump_back_count == 1 {
new_locals.push(jump_back_values[i][0].1);
} else if !jump_back_values[i].is_empty() {
Expand All @@ -79,6 +82,7 @@ impl<'a> BreakoutTarget<'a> {
new_locals
}

#[allow(clippy::needless_range_loop)]
pub fn modify_phis<'b>(&self, phi_instructions: &[&'b Value])
where
'a: 'b,
Expand Down Expand Up @@ -113,7 +117,7 @@ impl<'a> BreakoutTarget<'a> {
// See above comment about unreachablity
// TODO: Figure out if we can instead produce an unreachable result
if let TypeOrFuncType::Type(t) = ty {
wasm_type_to_zeroed_value(ctx, t)
wasm_type_to_zeroed_value(ctx, t).ivalue()
} else {
panic!()
}
Expand Down
115 changes: 25 additions & 90 deletions src/codegen/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use std::ffi::CString;
use std::mem;
use std::sync::atomic::{AtomicBool, Ordering};

use llvm::{BasicBlock, Sub};
use llvm::Builder;
use llvm::Function;
use llvm::Value;
use llvm::{BasicBlock, Sub};

use wasmparser::TypeOrFuncType;

Expand Down Expand Up @@ -48,12 +48,7 @@ unsafe fn add_string_attr(k: &str, v: &str, v_ref: *mut llvm::ffi::LLVMValue, ct
target_features_value.as_bytes().len() as u32,
);

crate::llvm_externs::LLVMAddAttributeAtIndex(
v_ref,
idx,
attr_ref
);

crate::llvm_externs::LLVMAddAttributeAtIndex(v_ref, idx, attr_ref);
}

// HACK: We only want to emit a note about the cortex-m override once
Expand Down Expand Up @@ -96,13 +91,16 @@ pub fn compile_function(ctx: &ModuleCtx, f: &ImplementedFunction) {
let llvm_ctx: *mut llvm::ffi::LLVMContext = mem::transmute(ctx.llvm_ctx as &llvm::Context);

let nounwind = CString::new("nounwind").unwrap();
let kind = crate::llvm_externs::LLVMGetEnumAttributeKindForName(nounwind.as_ptr(), nounwind.as_bytes().len());
let kind = crate::llvm_externs::LLVMGetEnumAttributeKindForName(
nounwind.as_ptr(),
nounwind.as_bytes().len(),
);
let attr_ref = crate::llvm_externs::LLVMCreateEnumAttribute(llvm_ctx, kind, 0);

crate::llvm_externs::LLVMAddAttributeAtIndex(
v_ref,
crate::llvm_externs::LLVMAttributeFunctionIndex,
attr_ref
attr_ref,
);
if cm_override {
if !CORTEX_OVERRIDE_MESSAGE_SENT.load(Ordering::Relaxed) {
Expand All @@ -112,75 +110,25 @@ pub fn compile_function(ctx: &ModuleCtx, f: &ImplementedFunction) {
CORTEX_OVERRIDE_MESSAGE_SENT.store(true, Ordering::Relaxed);
}

add_string_attr(
"correctly-rounded-divide-sqrt-fp-math",
"false",
v_ref,
ctx
);
add_string_attr("correctly-rounded-divide-sqrt-fp-math", "false", v_ref, ctx);

add_string_attr(
"disable-tail-calls",
"false",
v_ref,
ctx
);
add_string_attr("disable-tail-calls", "false", v_ref, ctx);

add_string_attr(
"less-precise-fpmad",
"false",
v_ref,
ctx
);
add_string_attr("less-precise-fpmad", "false", v_ref, ctx);

add_string_attr(
"min-legal-vector-width",
"0",
v_ref,
ctx
);
add_string_attr("min-legal-vector-width", "0", v_ref, ctx);

add_string_attr(
"no-frame-pointer-elim",
"false",
v_ref,
ctx
);
add_string_attr("no-frame-pointer-elim", "false", v_ref, ctx);

add_string_attr(
"no-infs-fp-math",
"false",
v_ref,
ctx
);
add_string_attr("no-infs-fp-math", "false", v_ref, ctx);

add_string_attr(
"no-jump-tables",
"false",
v_ref,
ctx
);
add_string_attr("no-jump-tables", "false", v_ref, ctx);

add_string_attr(
"no-nans-fp-math",
"false",
v_ref,
ctx
);
add_string_attr("no-nans-fp-math", "false", v_ref, ctx);

add_string_attr(
"no-signed-zeros-fp-math",
"false",
v_ref,
ctx
);
add_string_attr("no-signed-zeros-fp-math", "false", v_ref, ctx);

add_string_attr(
"no-trapping-math",
"false",
v_ref,
ctx
);
add_string_attr("no-trapping-math", "false", v_ref, ctx);

// add_string_attr(
// "stack-protector-buffer-size",
Expand All @@ -189,27 +137,11 @@ pub fn compile_function(ctx: &ModuleCtx, f: &ImplementedFunction) {
// ctx
// );

add_string_attr(
"target-cpu",
"cortex-m7",
v_ref,
ctx
);

add_string_attr(
"unsafe-fp-math",
"false",
v_ref,
ctx
);
add_string_attr("target-cpu", "cortex-m7", v_ref, ctx);

add_string_attr(
"use-soft-float",
"false",
v_ref,
ctx
);
add_string_attr("unsafe-fp-math", "false", v_ref, ctx);

add_string_attr("use-soft-float", "false", v_ref, ctx);

add_string_attr(
"target-features",
Expand All @@ -227,7 +159,7 @@ pub fn compile_function(ctx: &ModuleCtx, f: &ImplementedFunction) {
}
// Then the actual locals
for local in &f.locals {
locals.push(wasm_type_to_zeroed_value(ctx.llvm_ctx, *local));
locals.push(wasm_type_to_zeroed_value(ctx.llvm_ctx, *local).ivalue());
}

let builder = &*Builder::new(ctx.llvm_ctx);
Expand All @@ -242,7 +174,10 @@ pub fn compile_function(ctx: &ModuleCtx, f: &ImplementedFunction) {
};

let termination_block = llvm_f.append("exit");
let root_breakout_target = BreakoutTarget::new_wrapped(termination_block, f.get_return_type().map(TypeOrFuncType::Type));
let root_breakout_target = BreakoutTarget::new_wrapped(
termination_block,
f.get_return_type().map(TypeOrFuncType::Type),
);

// In WASM, a break out of the root block is the same as returning from the function
// Thus the termination block needs to do that
Expand Down
8 changes: 5 additions & 3 deletions src/codegen/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ use crate::wasm::Global;
use crate::wasm::Instruction;
use crate::Opt;

use crate::codegen::ModuleCtx;
use crate::codegen::runtime_stubs::*;
use crate::codegen::type_conversions::llvm_type_to_wasm_type;
use crate::codegen::type_conversions::wasm_type_to_llvm_type;
use crate::codegen::ModuleCtx;

pub enum GlobalValue<'a> {
InlinedConstant(&'a Value),
Expand Down Expand Up @@ -85,8 +85,10 @@ fn insert_native_globals<'a>(
content_type,
mutable,
} => {
let llvm_global =
llvm_module.add_global(&name, wasm_type_to_llvm_type(&*llvm_ctx, content_type));
let llvm_global = llvm_module.add_global(
&name,
wasm_type_to_llvm_type(&*llvm_ctx, content_type).itype(),
);
llvm_global.set_constant(!mutable);
GlobalValue::Native(llvm_global.to_super())
}
Expand Down
77 changes: 77 additions & 0 deletions src/codegen/llvm_wrapper/llvm_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use llvm::Compile;
use llvm::Type;

#[derive(Clone)]
pub struct BasicType<'a> {
ty: &'a Type,
}

impl<'a> BasicType<'a> {
pub fn void_ty<'b: 'a>(ctx: &'b llvm::Context) -> Self {
Self {
ty: <()>::get_type(ctx),
}
}

pub fn i32_ty<'b: 'a>(ctx: &'b llvm::Context) -> Self {
Self {
ty: <i32>::get_type(ctx),
}
}

pub fn i64_ty<'b: 'a>(ctx: &'b llvm::Context) -> Self {
Self {
ty: <i64>::get_type(ctx),
}
}

pub fn f32_ty<'b: 'a>(ctx: &'b llvm::Context) -> Self {
Self {
ty: <f32>::get_type(ctx),
}
}

pub fn f64_ty<'b: 'a>(ctx: &'b llvm::Context) -> Self {
Self {
ty: <f64>::get_type(ctx),
}
}

pub fn itype(&self) -> &'a Type {
self.ty
}
}

#[derive(Clone)]
pub struct FunctionType<'a> {
ty: &'a Type,
}

impl<'a> FunctionType<'a> {
pub fn new<'b: 'a, 'c: 'a>(
_ctx: &'b llvm::Context,
params: &[BasicType<'c>],
res: BasicType<'c>,
) -> Self {
let mut wrapped_params = Vec::new();
wrapped_params.extend(params.iter().map(BasicType::itype));

let ty = llvm::FunctionType::new(res.itype(), &wrapped_params);

Self { ty }
}

#[allow(dead_code)]
pub fn new_ret_void<'b: 'a, 'c: 'a>(ctx: &'b llvm::Context, params: &[BasicType<'c>]) -> Self {
let mut wrapped_params = Vec::new();
wrapped_params.extend(params.iter().map(BasicType::itype));

let ty = llvm::FunctionType::new(BasicType::void_ty(ctx).itype(), &wrapped_params);

Self { ty }
}

pub fn itype(&self) -> &'a Type {
self.ty
}
}
37 changes: 37 additions & 0 deletions src/codegen/llvm_wrapper/llvm_value.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use llvm::Compile;
use llvm::Context;
use llvm::Value;

pub struct BasicValue<'a> {
v: &'a Value,
}

impl<'a> BasicValue<'a> {
pub fn i32_zero<'b: 'a>(ctx: &'b Context) -> Self {
Self {
v: 0i32.compile(ctx),
}
}

pub fn i64_zero<'b: 'a>(ctx: &'b Context) -> Self {
Self {
v: 0i64.compile(ctx),
}
}

pub fn f32_zero<'b: 'a>(ctx: &'b Context) -> Self {
Self {
v: 0f32.compile(ctx),
}
}

pub fn f64_zero<'b: 'a>(ctx: &'b Context) -> Self {
Self {
v: 0f64.compile(ctx),
}
}

pub fn ivalue(&self) -> &'a Value {
self.v
}
}
5 changes: 5 additions & 0 deletions src/codegen/llvm_wrapper/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod llvm_type;
pub use llvm_type::*;

mod llvm_value;
pub use llvm_value::*;
Loading