Skip to content

Commit 2c9f574

Browse files
committed
CABI: simplify stream code a bit more (no change)
1 parent 4841186 commit 2c9f574

2 files changed

Lines changed: 32 additions & 46 deletions

File tree

design/mvp/CanonicalABI.md

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2826,16 +2826,13 @@ of component-level values with types `ts`.
28262826
def lift_flat_values(cx, max_flat, vi, ts):
28272827
flat_types = flatten_types(ts)
28282828
if len(flat_types) > max_flat:
2829-
return lift_heap_values(cx, vi, ts)
2829+
ptr = vi.next('i32')
2830+
tuple_type = TupleType(ts)
2831+
trap_if(ptr != align_to(ptr, alignment(tuple_type)))
2832+
trap_if(ptr + elem_size(tuple_type) > len(cx.opts.memory))
2833+
return list(load(cx, ptr, tuple_type).values())
28302834
else:
28312835
return [ lift_flat(cx, vi, t) for t in ts ]
2832-
2833-
def lift_heap_values(cx, vi, ts):
2834-
ptr = vi.next('i32')
2835-
tuple_type = TupleType(ts)
2836-
trap_if(ptr != align_to(ptr, alignment(tuple_type)))
2837-
trap_if(ptr + elem_size(tuple_type) > len(cx.opts.memory))
2838-
return list(load(cx, ptr, tuple_type).values())
28392836
```
28402837

28412838
Symmetrically, the `lower_flat_values` function defines how to lower a
@@ -2849,27 +2846,23 @@ def lower_flat_values(cx, max_flat, vs, ts, out_param = None):
28492846
cx.inst.may_leave = False
28502847
flat_types = flatten_types(ts)
28512848
if len(flat_types) > max_flat:
2852-
flat_vals = lower_heap_values(cx, vs, ts, out_param)
2849+
tuple_type = TupleType(ts)
2850+
tuple_value = {str(i): v for i,v in enumerate(vs)}
2851+
if out_param is None:
2852+
ptr = cx.opts.realloc(0, 0, alignment(tuple_type), elem_size(tuple_type))
2853+
flat_vals = [ptr]
2854+
else:
2855+
ptr = out_param.next('i32')
2856+
flat_vals = []
2857+
trap_if(ptr != align_to(ptr, alignment(tuple_type)))
2858+
trap_if(ptr + elem_size(tuple_type) > len(cx.opts.memory))
2859+
store(cx, tuple_value, tuple_type, ptr)
28532860
else:
28542861
flat_vals = []
28552862
for i in range(len(vs)):
28562863
flat_vals += lower_flat(cx, vs[i], ts[i])
28572864
cx.inst.may_leave = True
28582865
return flat_vals
2859-
2860-
def lower_heap_values(cx, vs, ts, out_param):
2861-
tuple_type = TupleType(ts)
2862-
tuple_value = {str(i): v for i,v in enumerate(vs)}
2863-
if out_param is None:
2864-
ptr = cx.opts.realloc(0, 0, alignment(tuple_type), elem_size(tuple_type))
2865-
flat_vals = [ptr]
2866-
else:
2867-
ptr = out_param.next('i32')
2868-
flat_vals = []
2869-
trap_if(ptr != align_to(ptr, alignment(tuple_type)))
2870-
trap_if(ptr + elem_size(tuple_type) > len(cx.opts.memory))
2871-
store(cx, tuple_value, tuple_type, ptr)
2872-
return flat_vals
28732866
```
28742867
The `may_leave` flag is guarded by `canon_lower` below to prevent a component
28752868
from calling out of the component while in the middle of lowering, ensuring

design/mvp/canonical-abi/definitions.py

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,43 +1801,36 @@ def lower_flat_flags(v, labels):
18011801
def lift_flat_values(cx, max_flat, vi, ts):
18021802
flat_types = flatten_types(ts)
18031803
if len(flat_types) > max_flat:
1804-
return lift_heap_values(cx, vi, ts)
1804+
ptr = vi.next('i32')
1805+
tuple_type = TupleType(ts)
1806+
trap_if(ptr != align_to(ptr, alignment(tuple_type)))
1807+
trap_if(ptr + elem_size(tuple_type) > len(cx.opts.memory))
1808+
return list(load(cx, ptr, tuple_type).values())
18051809
else:
18061810
return [ lift_flat(cx, vi, t) for t in ts ]
18071811

1808-
def lift_heap_values(cx, vi, ts):
1809-
ptr = vi.next('i32')
1810-
tuple_type = TupleType(ts)
1811-
trap_if(ptr != align_to(ptr, alignment(tuple_type)))
1812-
trap_if(ptr + elem_size(tuple_type) > len(cx.opts.memory))
1813-
return list(load(cx, ptr, tuple_type).values())
1814-
18151812
def lower_flat_values(cx, max_flat, vs, ts, out_param = None):
18161813
cx.inst.may_leave = False
18171814
flat_types = flatten_types(ts)
18181815
if len(flat_types) > max_flat:
1819-
flat_vals = lower_heap_values(cx, vs, ts, out_param)
1816+
tuple_type = TupleType(ts)
1817+
tuple_value = {str(i): v for i,v in enumerate(vs)}
1818+
if out_param is None:
1819+
ptr = cx.opts.realloc(0, 0, alignment(tuple_type), elem_size(tuple_type))
1820+
flat_vals = [ptr]
1821+
else:
1822+
ptr = out_param.next('i32')
1823+
flat_vals = []
1824+
trap_if(ptr != align_to(ptr, alignment(tuple_type)))
1825+
trap_if(ptr + elem_size(tuple_type) > len(cx.opts.memory))
1826+
store(cx, tuple_value, tuple_type, ptr)
18201827
else:
18211828
flat_vals = []
18221829
for i in range(len(vs)):
18231830
flat_vals += lower_flat(cx, vs[i], ts[i])
18241831
cx.inst.may_leave = True
18251832
return flat_vals
18261833

1827-
def lower_heap_values(cx, vs, ts, out_param):
1828-
tuple_type = TupleType(ts)
1829-
tuple_value = {str(i): v for i,v in enumerate(vs)}
1830-
if out_param is None:
1831-
ptr = cx.opts.realloc(0, 0, alignment(tuple_type), elem_size(tuple_type))
1832-
flat_vals = [ptr]
1833-
else:
1834-
ptr = out_param.next('i32')
1835-
flat_vals = []
1836-
trap_if(ptr != align_to(ptr, alignment(tuple_type)))
1837-
trap_if(ptr + elem_size(tuple_type) > len(cx.opts.memory))
1838-
store(cx, tuple_value, tuple_type, ptr)
1839-
return flat_vals
1840-
18411834
### `canon lift`
18421835

18431836
async def canon_lift(opts, inst, ft, callee, caller, on_start, on_resolve, on_block):

0 commit comments

Comments
 (0)