Skip to content

Commit eb74b0a

Browse files
committed
feat!: Implement GC instructions
1 parent 39fb2d9 commit eb74b0a

File tree

15 files changed

+1286
-23
lines changed

15 files changed

+1286
-23
lines changed

src/dune

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
heap_type
2424
signature_type
2525
struct_type
26+
type_builder
2627
ocaml_helpers)
2728
(flags :standard -O2 -Wall -Wextra))
2829
(js_of_ocaml
@@ -44,4 +45,5 @@
4445
packed_type.js
4546
heap_type.js
4647
signature_type.js
48+
type_builder.js
4749
struct_type.js)))

src/expression.c

Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,23 @@ caml_binaryen_call_indirect__bytecode(value * argv) {
124124
return caml_binaryen_call_indirect(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
125125
}
126126

127+
CAMLprim value
128+
caml_binaryen_call_ref(value _module, value _target, value _params, value _ty, value _is_return) {
129+
CAMLparam5(_module, _target, _params, _ty, _is_return);
130+
BinaryenModuleRef module = BinaryenModuleRef_val(_module);
131+
BinaryenExpressionRef target = BinaryenExpressionRef_val(_target);
132+
_params = array_of_list(_params);
133+
int paramsLen = array_length(_params);
134+
BinaryenExpressionRef params[paramsLen];
135+
for (int i = 0; i < paramsLen; i++) {
136+
params[i] = BinaryenExpressionRef_val(Field(_params, i));
137+
}
138+
BinaryenType ty = BinaryenType_val(_ty);
139+
bool is_return = Bool_val(_is_return);
140+
BinaryenExpressionRef exp = BinaryenCallRef(module, target, params, paramsLen, ty, is_return);
141+
CAMLreturn(alloc_BinaryenExpressionRef(exp));
142+
}
143+
127144
CAMLprim value
128145
caml_binaryen_return_call(value _module, value _name, value _params, value _retty) {
129146
CAMLparam4(_module, _name, _params, _retty);
@@ -459,6 +476,38 @@ caml_binaryen_i31_get(value _module, value _val, value _signed) {
459476
CAMLreturn(alloc_BinaryenExpressionRef(exp));
460477
}
461478

479+
CAMLprim value
480+
caml_binaryen_ref_test(value _module, value _ref, value _castType) {
481+
CAMLparam3(_module, _ref, _castType);
482+
BinaryenModuleRef module = BinaryenModuleRef_val(_module);
483+
BinaryenExpressionRef ref = BinaryenExpressionRef_val(_ref);
484+
BinaryenType castType = BinaryenType_val(_castType);
485+
BinaryenExpressionRef exp = BinaryenRefTest(module, ref, castType);
486+
CAMLreturn(alloc_BinaryenExpressionRef(exp));
487+
}
488+
489+
CAMLprim value
490+
caml_binaryen_ref_cast(value _module, value _ref, value _castType) {
491+
CAMLparam3(_module, _ref, _castType);
492+
BinaryenModuleRef module = BinaryenModuleRef_val(_module);
493+
BinaryenExpressionRef ref = BinaryenExpressionRef_val(_ref);
494+
BinaryenType castType = BinaryenType_val(_castType);
495+
BinaryenExpressionRef exp = BinaryenRefCast(module, ref, castType);
496+
CAMLreturn(alloc_BinaryenExpressionRef(exp));
497+
}
498+
499+
CAMLprim value
500+
caml_binaryen_br_on(value _module, value _op, value _name, value _ref, value _castType) {
501+
CAMLparam5(_module, _op, _name, _ref, _castType);
502+
BinaryenModuleRef module = BinaryenModuleRef_val(_module);
503+
BinaryenOp op = BinaryenOp_val(_op);
504+
char* name = Safe_String_val(_name);
505+
BinaryenExpressionRef ref = BinaryenExpressionRef_val(_ref);
506+
BinaryenType castType = BinaryenType_val(_castType);
507+
BinaryenExpressionRef exp = BinaryenBrOn(module, op, name, ref, castType);
508+
CAMLreturn(alloc_BinaryenExpressionRef(exp));
509+
}
510+
462511
CAMLprim value
463512
caml_binaryen_expression_id_invalid(value unit) {
464513
CAMLparam1(unit);
@@ -1328,6 +1377,90 @@ caml_binaryen_call_indirect_set_return(value _exp, value _isReturn) {
13281377
CAMLreturn(Val_unit);
13291378
}
13301379

1380+
CAMLprim value
1381+
caml_binaryen_call_ref_get_target(value _exp) {
1382+
CAMLparam1(_exp);
1383+
BinaryenExpressionRef exp = BinaryenExpressionRef_val(_exp);
1384+
BinaryenExpressionRef target = BinaryenCallRefGetTarget(exp);
1385+
CAMLreturn(alloc_BinaryenExpressionRef(target));
1386+
}
1387+
1388+
CAMLprim value
1389+
caml_binaryen_call_ref_set_target(value _exp, value _target) {
1390+
CAMLparam2(_exp, _target);
1391+
BinaryenExpressionRef exp = BinaryenExpressionRef_val(_exp);
1392+
BinaryenExpressionRef target = BinaryenExpressionRef_val(_target);
1393+
BinaryenCallRefSetTarget(exp, target);
1394+
CAMLreturn(Val_unit);
1395+
}
1396+
1397+
CAMLprim value
1398+
caml_binaryen_call_ref_get_num_operands(value _exp) {
1399+
CAMLparam1(_exp);
1400+
BinaryenExpressionRef exp = BinaryenExpressionRef_val(_exp);
1401+
CAMLreturn(Val_int(BinaryenCallRefGetNumOperands(exp)));
1402+
}
1403+
1404+
CAMLprim value
1405+
caml_binaryen_call_ref_get_operand_at(value _exp, value _index) {
1406+
CAMLparam2(_exp, _index);
1407+
BinaryenExpressionRef exp = BinaryenExpressionRef_val(_exp);
1408+
BinaryenIndex index = Int_val(_index);
1409+
CAMLreturn(alloc_BinaryenExpressionRef(BinaryenCallRefGetOperandAt(exp, index)));
1410+
}
1411+
1412+
CAMLprim value
1413+
caml_binaryen_call_ref_set_operand_at(value _exp, value _index, value _operand) {
1414+
CAMLparam3(_exp, _index, _operand);
1415+
BinaryenExpressionRef exp = BinaryenExpressionRef_val(_exp);
1416+
BinaryenIndex index = Int_val(_index);
1417+
BinaryenExpressionRef operand = BinaryenExpressionRef_val(_operand);
1418+
BinaryenCallRefSetOperandAt(exp, index, operand);
1419+
CAMLreturn(Val_unit);
1420+
}
1421+
1422+
CAMLprim value
1423+
caml_binaryen_call_ref_append_operand(value _exp, value _operand) {
1424+
CAMLparam2(_exp, _operand);
1425+
BinaryenExpressionRef exp = BinaryenExpressionRef_val(_exp);
1426+
BinaryenExpressionRef operand = BinaryenExpressionRef_val(_operand);
1427+
CAMLreturn(Val_int(BinaryenCallRefAppendOperand(exp, operand)));
1428+
}
1429+
1430+
CAMLprim value
1431+
caml_binaryen_call_ref_insert_operand_at(value _exp, value _index, value _operand) {
1432+
CAMLparam3(_exp, _index, _operand);
1433+
BinaryenExpressionRef exp = BinaryenExpressionRef_val(_exp);
1434+
BinaryenIndex index = Int_val(_index);
1435+
BinaryenExpressionRef operand = BinaryenExpressionRef_val(_operand);
1436+
BinaryenCallRefInsertOperandAt(exp, index, operand);
1437+
CAMLreturn(Val_unit);
1438+
}
1439+
1440+
CAMLprim value
1441+
caml_binaryen_call_ref_remove_operand_at(value _exp, value _index) {
1442+
CAMLparam2(_exp, _index);
1443+
BinaryenExpressionRef exp = BinaryenExpressionRef_val(_exp);
1444+
BinaryenIndex index = Int_val(_index);
1445+
CAMLreturn(alloc_BinaryenExpressionRef(BinaryenCallRefRemoveOperandAt(exp, index)));
1446+
}
1447+
1448+
CAMLprim value
1449+
caml_binaryen_call_ref_is_return(value _exp) {
1450+
CAMLparam1(_exp);
1451+
BinaryenExpressionRef exp = BinaryenExpressionRef_val(_exp);
1452+
CAMLreturn(Val_bool(BinaryenCallRefIsReturn(exp)));
1453+
}
1454+
1455+
CAMLprim value
1456+
caml_binaryen_call_ref_set_return(value _exp, value _isReturn) {
1457+
CAMLparam2(_exp, _isReturn);
1458+
BinaryenExpressionRef exp = BinaryenExpressionRef_val(_exp);
1459+
int isReturn = Bool_val(_isReturn);
1460+
BinaryenCallRefSetReturn(exp, isReturn);
1461+
CAMLreturn(Val_unit);
1462+
}
1463+
13311464
CAMLprim value
13321465
caml_binaryen_local_set_get_value(value _exp) {
13331466
CAMLparam1(_exp);
@@ -1883,6 +2016,141 @@ caml_binaryen_ref_eq(value _module, value _left, value _right) {
18832016
CAMLreturn(alloc_BinaryenExpressionRef(exp));
18842017
}
18852018

2019+
// Struct operations
2020+
2021+
CAMLprim value
2022+
caml_binaryen_struct_new(value _module, value _operands, value _type) {
2023+
CAMLparam3(_module, _operands, _type);
2024+
BinaryenModuleRef module = BinaryenModuleRef_val(_module);
2025+
BinaryenHeapType type = BinaryenHeapType_val(_type);
2026+
if (Is_some(_operands)) {
2027+
_operands = array_of_list(Some_val(_operands));
2028+
int operandsLen = array_length(_operands);
2029+
BinaryenExpressionRef operands[operandsLen];
2030+
for (int i = 0; i < operandsLen; i++) {
2031+
operands[i] = BinaryenExpressionRef_val(Field(_operands, i));
2032+
}
2033+
BinaryenExpressionRef exp = BinaryenStructNew(module, operands, operandsLen, type);
2034+
CAMLreturn(alloc_BinaryenExpressionRef(exp));
2035+
} else {
2036+
BinaryenExpressionRef exp = BinaryenStructNew(module, NULL, 0, type);
2037+
CAMLreturn(alloc_BinaryenExpressionRef(exp));
2038+
}
2039+
}
2040+
2041+
CAMLprim value
2042+
caml_binaryen_struct_get(value _module, value _index, value _ref, value _type, value _signed) {
2043+
CAMLparam5(_module, _index, _ref, _type, _signed);
2044+
BinaryenModuleRef module = BinaryenModuleRef_val(_module);
2045+
BinaryenIndex index = Int_val(_index);
2046+
BinaryenExpressionRef ref = BinaryenExpressionRef_val(_ref);
2047+
BinaryenType type = BinaryenType_val(_type);
2048+
bool signed_ = Bool_val(_signed);
2049+
BinaryenExpressionRef exp = BinaryenStructGet(module, index, ref, type, signed_);
2050+
CAMLreturn(alloc_BinaryenExpressionRef(exp));
2051+
}
2052+
2053+
CAMLprim value
2054+
caml_binaryen_struct_set(value _module, value _index, value _ref, value _value) {
2055+
CAMLparam4(_module, _index, _ref, _value);
2056+
BinaryenModuleRef module = BinaryenModuleRef_val(_module);
2057+
BinaryenIndex index = Int_val(_index);
2058+
BinaryenExpressionRef ref = BinaryenExpressionRef_val(_ref);
2059+
BinaryenExpressionRef value_ = BinaryenExpressionRef_val(_value);
2060+
BinaryenExpressionRef exp = BinaryenStructSet(module, index, ref, value_);
2061+
CAMLreturn(alloc_BinaryenExpressionRef(exp));
2062+
}
2063+
2064+
// Array operations
2065+
2066+
CAMLprim value
2067+
caml_binaryen_array_new(value _module, value _type, value _size, value _init) {
2068+
CAMLparam4(_module, _type, _size, _init);
2069+
BinaryenModuleRef module = BinaryenModuleRef_val(_module);
2070+
BinaryenHeapType type = BinaryenHeapType_val(_type);
2071+
BinaryenExpressionRef size = BinaryenExpressionRef_val(_size);
2072+
BinaryenExpressionRef init = BinaryenExpressionRef_val(_init);
2073+
BinaryenExpressionRef exp = BinaryenArrayNew(module, type, size, init);
2074+
CAMLreturn(alloc_BinaryenExpressionRef(exp));
2075+
}
2076+
2077+
CAMLprim value
2078+
caml_binaryen_array_new_data(value _module, value _type, value _name, value _offset, value _size) {
2079+
CAMLparam5(_module, _type, _name, _offset, _size);
2080+
BinaryenModuleRef module = BinaryenModuleRef_val(_module);
2081+
BinaryenHeapType type = BinaryenHeapType_val(_type);
2082+
char* name = Safe_String_val(_name);
2083+
BinaryenExpressionRef offset = BinaryenExpressionRef_val(_offset);
2084+
BinaryenExpressionRef size = BinaryenExpressionRef_val(_size);
2085+
BinaryenExpressionRef exp = BinaryenArrayNewData(module, type, name, offset, size);
2086+
CAMLreturn(alloc_BinaryenExpressionRef(exp));
2087+
}
2088+
2089+
CAMLprim value
2090+
caml_binaryen_array_new_fixed(value _module, value _type, value _values) {
2091+
CAMLparam3(_module, _type, _values);
2092+
BinaryenModuleRef module = BinaryenModuleRef_val(_module);
2093+
BinaryenHeapType type = BinaryenHeapType_val(_type);
2094+
_values = array_of_list(_values);
2095+
int valuesLen = array_length(_values);
2096+
BinaryenExpressionRef values[valuesLen];
2097+
for (int i = 0; i < valuesLen; i++) {
2098+
values[i] = BinaryenExpressionRef_val(Field(_values, i));
2099+
}
2100+
BinaryenExpressionRef exp = BinaryenArrayNewFixed(module, type, values, valuesLen);
2101+
CAMLreturn(alloc_BinaryenExpressionRef(exp));
2102+
}
2103+
2104+
CAMLprim value
2105+
caml_binaryen_array_get(value _module, value _ref, value _index, value _type, value _signed) {
2106+
CAMLparam5(_module, _ref, _index, _type, _signed);
2107+
BinaryenModuleRef module = BinaryenModuleRef_val(_module);
2108+
BinaryenExpressionRef ref = BinaryenExpressionRef_val(_ref);
2109+
BinaryenExpressionRef index = BinaryenExpressionRef_val(_index);
2110+
BinaryenType type = BinaryenType_val(_type);
2111+
bool signed_ = Bool_val(_signed);
2112+
BinaryenExpressionRef exp = BinaryenArrayGet(module, ref, index, type, signed_);
2113+
CAMLreturn(alloc_BinaryenExpressionRef(exp));
2114+
}
2115+
2116+
CAMLprim value
2117+
caml_binaryen_array_set(value _module, value _ref, value _index, value _value) {
2118+
CAMLparam4(_module, _ref, _index, _value);
2119+
BinaryenModuleRef module = BinaryenModuleRef_val(_module);
2120+
BinaryenExpressionRef ref = BinaryenExpressionRef_val(_ref);
2121+
BinaryenExpressionRef index = BinaryenExpressionRef_val(_index);
2122+
BinaryenExpressionRef value_ = BinaryenExpressionRef_val(_value);
2123+
BinaryenExpressionRef exp = BinaryenArraySet(module, ref, index, value_);
2124+
CAMLreturn(alloc_BinaryenExpressionRef(exp));
2125+
}
2126+
2127+
CAMLprim value
2128+
caml_binaryen_array_len(value _module, value _ref) {
2129+
CAMLparam2(_module, _ref);
2130+
BinaryenModuleRef module = BinaryenModuleRef_val(_module);
2131+
BinaryenExpressionRef ref = BinaryenExpressionRef_val(_ref);
2132+
BinaryenExpressionRef exp = BinaryenArrayLen(module, ref);
2133+
CAMLreturn(alloc_BinaryenExpressionRef(exp));
2134+
}
2135+
2136+
CAMLprim value
2137+
caml_binaryen_array_copy(value _module, value _destRef, value _destIndex, value _srcRef, value _srcIndex, value _length) {
2138+
CAMLparam5(_module, _destRef, _destIndex, _srcRef, _srcIndex);
2139+
CAMLxparam1(_length);
2140+
BinaryenModuleRef module = BinaryenModuleRef_val(_module);
2141+
BinaryenExpressionRef destRef = BinaryenExpressionRef_val(_destRef);
2142+
BinaryenExpressionRef destIndex = BinaryenExpressionRef_val(_destIndex);
2143+
BinaryenExpressionRef srcRef = BinaryenExpressionRef_val(_srcRef);
2144+
BinaryenExpressionRef srcIndex = BinaryenExpressionRef_val(_srcIndex);
2145+
BinaryenExpressionRef length = BinaryenExpressionRef_val(_length);
2146+
BinaryenExpressionRef exp = BinaryenArrayCopy(module, destRef, destIndex, srcRef, srcIndex, length);
2147+
CAMLreturn(alloc_BinaryenExpressionRef(exp));
2148+
}
2149+
CAMLprim value
2150+
caml_binaryen_array_copy__bytecode(value * argv) {
2151+
return caml_binaryen_array_copy(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
2152+
}
2153+
18862154
// Table operations
18872155
CAMLprim value
18882156
caml_binaryen_table_get(value _module, value _name, value _index, value _ty) {

0 commit comments

Comments
 (0)