Skip to content

Commit 55c9f56

Browse files
committed
Support malloc instruction
1 parent b25ca26 commit 55c9f56

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

cmd/gravity/src/codegen/func.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1492,7 +1492,45 @@ impl Bindgen for Func<'_> {
14921492
todo!("implement instruction: {inst:?}")
14931493
}
14941494
Instruction::EnumLift { .. } => todo!("implement instruction: {inst:?}"),
1495-
Instruction::Malloc { .. } => todo!("implement instruction: {inst:?}"),
1495+
Instruction::Malloc {
1496+
realloc,
1497+
size,
1498+
align,
1499+
} => {
1500+
let tmp = self.tmp();
1501+
let ptr = &format!("ptr{tmp}");
1502+
let result = &format!("result{tmp}");
1503+
let err = &format!("err{tmp}");
1504+
let default = &format!("default{tmp}");
1505+
let size = size.size_wasm32();
1506+
let align = align.align_wasm32();
1507+
1508+
quote_in! { self.body =>
1509+
$['\r']
1510+
$result, $err := i.module.ExportedFunction($(quoted(*realloc))).Call(ctx, 0, 0, $align, $size)
1511+
$(match &self.result {
1512+
GoResult::Anon(GoType::ValueOrError(typ)) => {
1513+
if $err != nil {
1514+
var $default $(typ.as_ref())
1515+
return $default, $err
1516+
}
1517+
}
1518+
GoResult::Anon(GoType::Error) => {
1519+
if $err != nil {
1520+
return $err
1521+
}
1522+
}
1523+
GoResult::Anon(_) | GoResult::Empty => {
1524+
$(comment(&["The return type doesn't contain an error so we panic if one is encountered"]))
1525+
if $err != nil {
1526+
panic($err)
1527+
}
1528+
}
1529+
})
1530+
$ptr := $result[0]
1531+
}
1532+
results.push(Operand::SingleValue(ptr.into()));
1533+
}
14961534
Instruction::HandleLower { .. } | Instruction::HandleLift { .. } => {
14971535
todo!("implement resources: {inst:?}")
14981536
}

0 commit comments

Comments
 (0)