Skip to content

Commit 0ba5241

Browse files
daxpeddaLiamolucko
andcommitted
Rework DeferCallCore to DeferFree
Co-Authored-By: Liam Murphy <43807659+Liamolucko@users.noreply.github.com>
1 parent 033a712 commit 0ba5241

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

crates/cli-support/src/js/binding.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -548,15 +548,17 @@ fn instruction(js: &mut JsBuilder, instr: &Instruction, log_error: &mut bool) ->
548548
| Instruction::CallExport(_)
549549
| Instruction::CallAdapter(_)
550550
| Instruction::CallTableElement(_)
551-
| Instruction::DeferCallCore(_) => {
551+
| Instruction::DeferFree { .. } => {
552552
let invoc = Invocation::from(instr, js.cx.module)?;
553553
let (mut params, results) = invoc.params_results(js.cx);
554554

555555
let mut args = Vec::new();
556556
let tmp = js.tmp();
557557
if invoc.defer() {
558-
// substract alignment
559-
params -= 1;
558+
if let Instruction::DeferFree { .. } = instr {
559+
// substract alignment
560+
params -= 1;
561+
}
560562
// If the call is deferred, the arguments to the function still need to be
561563
// accessible in the `finally` block, so we declare variables to hold the args
562564
// outside of the try-finally block and then set those to the args.
@@ -566,8 +568,10 @@ fn instruction(js: &mut JsBuilder, instr: &Instruction, log_error: &mut bool) ->
566568
writeln!(js.prelude, "{name} = {arg};").unwrap();
567569
args.push(name);
568570
}
569-
// add alignment
570-
args.push(String::from("4"));
571+
if let Instruction::DeferFree { align, .. } = instr {
572+
// add alignment
573+
args.push(align.to_string());
574+
}
571575
} else {
572576
// Otherwise, pop off the number of parameters for the function we're calling.
573577
for _ in 0..params {
@@ -1194,8 +1198,8 @@ impl Invocation {
11941198
defer: false,
11951199
},
11961200

1197-
DeferCallCore(f) => Invocation::Core {
1198-
id: *f,
1201+
DeferFree { free, .. } => Invocation::Core {
1202+
id: *free,
11991203
defer: true,
12001204
},
12011205

crates/cli-support/src/wit/outgoing.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl InstructionBuilder<'_, '_> {
105105
// ... then defer a call to `free` to happen later
106106
let free = self.cx.free()?;
107107
self.instructions.push(InstructionData {
108-
instr: Instruction::DeferCallCore(free),
108+
instr: Instruction::DeferFree { free, align: 4 },
109109
stack_change: StackChange::Modified {
110110
popped: 2,
111111
pushed: 2,
@@ -389,7 +389,7 @@ impl InstructionBuilder<'_, '_> {
389389
// special case it.
390390
assert!(!self.instructions[len..]
391391
.iter()
392-
.any(|idata| matches!(idata.instr, Instruction::DeferCallCore(_))));
392+
.any(|idata| matches!(idata.instr, Instruction::DeferFree { .. })));
393393

394394
// Finally, we add the two inputs to UnwrapResult, and everything checks out
395395
//
@@ -429,7 +429,7 @@ impl InstructionBuilder<'_, '_> {
429429
// implementation.
430430
let free = self.cx.free()?;
431431
self.instructions.push(InstructionData {
432-
instr: Instruction::DeferCallCore(free),
432+
instr: Instruction::DeferFree { free, align: 4 },
433433
stack_change: StackChange::Modified {
434434
popped: 2,
435435
pushed: 2,

crates/cli-support/src/wit/standard.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ pub enum Instruction {
9595
CallCore(walrus::FunctionId),
9696
/// Schedules a function to be called after the whole lift/lower cycle is
9797
/// finished, e.g. to deallocate a string or something.
98-
DeferCallCore(walrus::FunctionId),
98+
DeferFree {
99+
free: walrus::FunctionId,
100+
align: usize,
101+
},
99102
/// A call to one of our own defined adapters, similar to the standard
100103
/// call-adapter instruction
101104
CallAdapter(AdapterId),
@@ -423,7 +426,7 @@ impl walrus::CustomSection for NonstandardWitSection {
423426
};
424427
for instr in instrs {
425428
match instr.instr {
426-
DeferCallCore(f) | CallCore(f) => {
429+
DeferFree { free: f, .. } | CallCore(f) => {
427430
roots.push_func(f);
428431
}
429432
StoreRetptr { mem, .. }

0 commit comments

Comments
 (0)