MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / libcall_function

Function libcall_function

cranelift/object/tests/basic.rs:167–217  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

165
166#[test]
167fn libcall_function() {
168 let flag_builder = settings::builder();
169 let isa_builder = cranelift_codegen::isa::lookup_by_name("x86_64-unknown-linux-gnu").unwrap();
170 let isa = isa_builder
171 .finish(settings::Flags::new(flag_builder))
172 .unwrap();
173 let mut module =
174 ObjectModule::new(ObjectBuilder::new(isa, "foo", default_libcall_names()).unwrap());
175
176 let sig = Signature {
177 params: vec![],
178 returns: vec![],
179 call_conv: CallConv::SystemV,
180 };
181
182 let func_id = module
183 .declare_function("function", Linkage::Local, &sig)
184 .unwrap();
185
186 let mut ctx = Context::new();
187 ctx.func = Function::with_name_signature(UserFuncName::user(0, func_id.as_u32()), sig);
188 let mut func_ctx = FunctionBuilderContext::new();
189 {
190 let mut bcx: FunctionBuilder = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);
191 let block = bcx.create_block();
192 bcx.switch_to_block(block);
193
194 let int = module.target_config().pointer_type();
195 let zero = bcx.ins().iconst(I16, 0);
196 let size = bcx.ins().iconst(int, 10);
197
198 let mut signature = module.make_signature();
199 signature.params.push(AbiParam::new(int));
200 signature.returns.push(AbiParam::new(int));
201 let callee = module
202 .declare_function("malloc", Linkage::Import, &signature)
203 .expect("declare malloc function");
204 let local_callee = module.declare_func_in_func(callee, &mut bcx.func);
205 let argument_exprs = vec![size];
206 let call = bcx.ins().call(local_callee, &argument_exprs);
207 let buffer = bcx.inst_results(call)[0];
208
209 bcx.call_memset(module.target_config(), buffer, zero, size);
210
211 bcx.ins().return_(&[]);
212 }
213
214 module.define_function(func_id, &mut ctx).unwrap();
215
216 module.finish();
217}
218
219#[test]
220#[should_panic(expected = "has a null byte, which is disallowed")]

Callers

nothing calls this directly

Calls 15

lookup_by_nameFunction · 0.85
default_libcall_namesFunction · 0.85
create_blockMethod · 0.80
switch_to_blockMethod · 0.80
call_memsetMethod · 0.80
builderFunction · 0.50
newFunction · 0.50
unwrapMethod · 0.45
finishMethod · 0.45
declare_functionMethod · 0.45
as_u32Method · 0.45
pointer_typeMethod · 0.45

Tested by

no test coverage detected