Skip to content

Commit 6815cf4

Browse files
committed
Support malloc instruction
1 parent 6022c93 commit 6815cf4

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

0 commit comments

Comments
 (0)