Skip to content

Commit

Permalink
feature: make frontend's translate_module_as_component to wrap
Browse files Browse the repository at this point in the history
`Component` in a `World`
  • Loading branch information
greenhat committed Feb 24, 2025
1 parent 62d2fb1 commit e64bb1f
Show file tree
Hide file tree
Showing 138 changed files with 4,061 additions and 3,652 deletions.
551 changes: 278 additions & 273 deletions frontend-wasm2/src/code_translator/tests.rs

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions frontend-wasm2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ pub fn translate(
wasm: &[u8],
config: &WasmTranslationConfig,
context: Rc<Context>,
) -> WasmResult<midenc_hir2::dialects::builtin::ComponentRef> {
) -> WasmResult<midenc_hir2::dialects::builtin::WorldRef> {
if wasm[4..8] == [0x01, 0x00, 0x00, 0x00] {
// Wasm core module
// see https://github.com/WebAssembly/component-model/blob/main/design/mvp/Binary.md#component-definitions
translate_module_as_component(wasm, config, context)
} else {
translate_component(wasm, config, context)
todo!()
// translate_component(wasm, config, context)
}
}

Expand Down
22 changes: 13 additions & 9 deletions frontend-wasm2/src/module/build_ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ use midenc_hir::{
};
use midenc_hir2::{
constants::ConstantData,
dialects::builtin::{Component, ComponentBuilder, Function, Module, ModuleBuilder, ModuleRef},
dialects::builtin::{
Component, ComponentBuilder, Function, Module, ModuleBuilder, ModuleRef, World,
WorldBuilder, WorldRef,
},
version::Version,
Builder, BuilderExt, CallConv, Context, Ident, Immediate, Op, OpBuilder, Visibility,
};
Expand Down Expand Up @@ -37,7 +40,7 @@ pub fn translate_module_as_component(
wasm: &[u8],
config: &WasmTranslationConfig,
context: Rc<Context>,
) -> WasmResult<midenc_hir2::dialects::builtin::ComponentRef> {
) -> WasmResult<midenc_hir2::dialects::builtin::WorldRef> {
let mut validator = Validator::new_with_features(crate::supported_features());
let parser = wasmparser::Parser::new(0);
let mut module_types_builder = Default::default();
Expand All @@ -53,15 +56,16 @@ pub fn translate_module_as_component(
}
let module_types = module_types_builder.finish();

let world_name = Ident::from("world");

let world_ref =
context.clone().builder().create::<World, (Ident,)>(Default::default())(world_name)?;
let mut world_builder = WorldBuilder::new(world_ref);

let ns = Ident::from("root_ns");
let name = Ident::from("root");
let ver = Version::parse("1.0.0").unwrap();
let mut component_ref =
context
.clone()
.builder()
.create::<Component, (Ident, Ident, Version)>(Default::default())(ns, name, ver)
.unwrap();
let component_ref = world_builder.define_component(ns, name, ver)?;
let mut cb = ComponentBuilder::new(component_ref);
let module_name = parsed_module.module.name().as_str();
let mut module_ref = cb.define_module(Ident::from(module_name)).unwrap();
Expand All @@ -76,7 +80,7 @@ pub fn translate_module_as_component(
&context.session.diagnostics,
);
build_ir_module(&mut parsed_module, &module_types, &mut module_state, config, context)?;
Ok(component_ref)
Ok(world_ref)
}

pub fn build_ir_module(
Expand Down
2 changes: 1 addition & 1 deletion hir2/src/dialects/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod builders;
mod ops;

pub use self::{
builders::{ComponentBuilder, ModuleBuilder},
builders::{ComponentBuilder, ModuleBuilder, WorldBuilder},
ops::*,
};
use crate::{
Expand Down
2 changes: 1 addition & 1 deletion hir2/src/dialects/builtin/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ mod component;
mod module;
mod world;

pub use self::{component::*, module::*};
pub use self::{component::*, module::*, world::*};
307 changes: 155 additions & 152 deletions tests/integration/expected/abi_transform_stdlib_blake3_hash.hir

Large diffs are not rendered by default.

59 changes: 31 additions & 28 deletions tests/integration/expected/abi_transform_tx_kernel_get_id.hir
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
builtin.component #[name = #root] #[namespace = #root_ns] #[version = 1.0.0] #[visibility = public] {
builtin.world #[name = #world] {

builtin.module #[name = #abi_transform_tx_kernel_get_id] #[visibility = public] {
builtin.component #[name = #root] #[namespace = #root_ns] #[version = 1.0.0] #[visibility = public] {

builtin.function public @miden_base_sys::bindings::account::extern_account_get_id() -> felt {
^block2:
v0 = hir.exec : felt #[callee = root/miden::account/get_id] #[signature = (result felt)];
hir.br block4 v0;
^block4(v1: felt):
hir.ret ;
};
builtin.function public @entrypoint() -> felt {
^block6:
v3 = hir.exec : felt #[callee = root/abi_transform_tx_kernel_get_id/miden_base_sys::bindings::account::get_id] #[signature = (result felt)];
hir.br block7 v3;
^block7(v2: felt):
hir.ret v2;
};
builtin.function public @miden_base_sys::bindings::account::get_id() -> felt {
^block8:
v5 = hir.exec : felt #[callee = root/abi_transform_tx_kernel_get_id/miden_base_sys::bindings::account::extern_account_get_id] #[signature = (result felt)];
hir.br block9 v5;
^block9(v4: felt):
hir.ret v4;
};
builtin.global_variable #[name = #__stack_pointer] #[ty = i32] #[visibility = private] {
builtin.module #[name = #abi_transform_tx_kernel_get_id] #[visibility = public] {

hir.ret_imm #[value = 1048576];
builtin.function public @miden_base_sys::bindings::account::extern_account_get_id() -> felt {
^block3:
v0 = hir.exec : felt #[callee = world/root/miden::account/get_id] #[signature = (result felt)];
hir.br block5 v0;
^block5(v1: felt):
hir.ret ;
};
builtin.function public @entrypoint() -> felt {
^block7:
v3 = hir.exec : felt #[callee = world/root/abi_transform_tx_kernel_get_id/miden_base_sys::bindings::account::get_id] #[signature = (result felt)];
hir.br block8 v3;
^block8(v2: felt):
hir.ret v2;
};
builtin.function public @miden_base_sys::bindings::account::get_id() -> felt {
^block9:
v5 = hir.exec : felt #[callee = world/root/abi_transform_tx_kernel_get_id/miden_base_sys::bindings::account::extern_account_get_id] #[signature = (result felt)];
hir.br block10 v5;
^block10(v4: felt):
hir.ret v4;
};
builtin.global_variable #[name = #__stack_pointer] #[ty = i32] #[visibility = private] {

hir.ret_imm #[value = 1048576];
};
};
};
builtin.module #[name = #miden::account] #[visibility = public] {
builtin.module #[name = #miden::account] #[visibility = public] {

builtin.function public @get_id(result felt) {
builtin.function public @get_id(result felt) {

};
};
};
};
Loading

0 comments on commit e64bb1f

Please sign in to comment.