Skip to content

In the Canonical ABI, disallow empty types. #218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions design/mvp/Binary.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,11 @@ primvaltype ::= 0x7f => bool
| 0x74 => char
| 0x73 => string
defvaltype ::= pvt:<primvaltype> => pvt
| 0x72 lt*:vec(<labelvaltype>) => (record (field lt)*)
| 0x72 lt*:vec(<labelvaltype>) => (record (field lt)*) (if |lt*| > 0)
| 0x71 case*:vec(<case>) => (variant case*)
| 0x70 t:<valtype> => (list t)
| 0x6f t*:vec(<valtype>) => (tuple t*)
| 0x6e l*:vec(<label>) => (flags l*)
| 0x6f t*:vec(<valtype>) => (tuple t+) (if |t*| > 0)
| 0x6e l*:vec(<label>) => (flags l+) (if |l*| > 0)
| 0x6d l*:vec(<label>) => (enum l*)
| 0x6c t*:vec(<valtype>) => (union t*)
| 0x6b t:<valtype> => (option t)
Expand Down
6 changes: 4 additions & 2 deletions design/mvp/CanonicalABI.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ below.
### Size

Each value type is also assigned a `size`, measured in bytes, which corresponds
the `sizeof` operator in C:
the `sizeof` operator in C. Empty types, such as records with no fields, are
not permitted, to avoid complications in source languages.
```python
def size(t):
match despecialize(t):
Expand All @@ -184,6 +185,7 @@ def size_record(fields):
for f in fields:
s = align_to(s, alignment(f.t))
s += size(f.t)
assert(s > 0)
return align_to(s, alignment_record(fields))

def align_to(ptr, alignment):
Expand All @@ -201,7 +203,7 @@ def size_variant(cases):

def size_flags(labels):
n = len(labels)
if n == 0: return 0
assert(n > 0)
if n <= 8: return 1
if n <= 16: return 2
return 4 * num_i32_flags(labels)
Expand Down
6 changes: 3 additions & 3 deletions design/mvp/Explainer.md
Original file line number Diff line number Diff line change
Expand Up @@ -487,11 +487,11 @@ defvaltype ::= bool
| s8 | u8 | s16 | u16 | s32 | u32 | s64 | u64
| float32 | float64
| char | string
| (record (field <label> <valtype>)*)
| (record (field <label> <valtype>)+)
| (variant (case <id>? <label> <valtype>? (refines <id>)?)+)
| (list <valtype>)
| (tuple <valtype>*)
| (flags <label>*)
| (tuple <valtype>+)
| (flags <label>+)
| (enum <label>+)
| (union <valtype>+)
| (option <valtype>)
Expand Down
3 changes: 2 additions & 1 deletion design/mvp/canonical-abi/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ def size_record(fields):
for f in fields:
s = align_to(s, alignment(f.t))
s += size(f.t)
assert(s > 0)
return align_to(s, alignment_record(fields))

def align_to(ptr, alignment):
Expand All @@ -266,7 +267,7 @@ def size_variant(cases):

def size_flags(labels):
n = len(labels)
if n == 0: return 0
assert(n > 0)
if n <= 8: return 1
if n <= 16: return 2
return 4 * num_i32_flags(labels)
Expand Down
29 changes: 17 additions & 12 deletions design/mvp/canonical-abi/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,13 @@ def test_name():
if not equal_modulo_string_encoding(got, lower_v):
fail("{} re-lift expected {} but got {}".format(test_name(), lower_v, got))

test(Record([]), [], {})
# Empty record types are not permitted yet.
#test(Record([]), [], {})
test(Record([Field('x',U8()), Field('y',U16()), Field('z',U32())]), [1,2,3], {'x':1,'y':2,'z':3})
test(Tuple([Tuple([U8(),U8()]),U8()]), [1,2,3], {'0':{'0':1,'1':2},'1':3})
t = Flags([])
test(t, [], {})
# Empty flags types are not permitted yet.
#t = Flags([])
#test(t, [], {})
t = Flags(['a','b'])
test(t, [0], {'a':False,'b':False})
test(t, [2], {'a':False,'b':True})
Expand Down Expand Up @@ -242,7 +244,8 @@ def test_heap(t, expect, args, byte_array):
cx = mk_cx(heap.memory)
test(t, args, expect, cx)

test_heap(List(Record([])), [{},{},{}], [0,3], [])
# Empty record types are not permitted yet.
#test_heap(List(Record([])), [{},{},{}], [0,3], [])
test_heap(List(Bool()), [True,False,True], [0,3], [1,0,1])
test_heap(List(Bool()), [True,False,True], [0,3], [1,0,2])
test_heap(List(Bool()), [True,False,True], [3,3], [0xff,0xff,0xff, 1,0,1])
Expand Down Expand Up @@ -276,21 +279,23 @@ def test_heap(t, expect, args, byte_array):
[6,0, 7, 0x0ff, 8,0, 9, 0xff])
test_heap(List(Tuple([Tuple([U16(),U8()]),U8()])), [mk_tup([4,5],6),mk_tup([7,8],9)], [0,2],
[4,0, 5,0xff, 6,0xff, 7,0, 8,0xff, 9,0xff])
test_heap(List(Union([Record([]),U8(),Tuple([U8(),U16()])])), [{'0':{}}, {'1':42}, {'2':mk_tup(6,7)}], [0,3],
[0,0xff,0xff,0xff,0xff,0xff, 1,0xff,42,0xff,0xff,0xff, 2,0xff,6,0xff,7,0])
# Empty record types are not permitted yet.
#test_heap(List(Union([Record([]),U8(),Tuple([U8(),U16()])])), [{'0':{}}, {'1':42}, {'2':mk_tup(6,7)}], [0,3],
# [0,0xff,0xff,0xff,0xff,0xff, 1,0xff,42,0xff,0xff,0xff, 2,0xff,6,0xff,7,0])
test_heap(List(Union([U32(),U8()])), [{'0':256}, {'1':42}], [0,2],
[0,0xff,0xff,0xff,0,1,0,0, 1,0xff,0xff,0xff,42,0xff,0xff,0xff])
test_heap(List(Tuple([Union([U8(),Tuple([U16(),U8()])]),U8()])),
[mk_tup({'1':mk_tup(5,6)},7),mk_tup({'0':8},9)], [0,2],
[1,0xff,5,0,6,0xff,7,0xff, 0,0xff,8,0xff,0xff,0xff,9,0xff])
test_heap(List(Union([U8()])), [{'0':6},{'0':7},{'0':8}], [0,3],
[0,6, 0,7, 0,8])
t = List(Flags([]))
test_heap(t, [{},{},{}], [0,3],
[])
t = List(Tuple([Flags([]), U8()]))
test_heap(t, [mk_tup({}, 42), mk_tup({}, 43), mk_tup({}, 44)], [0,3],
[42,43,44])
# Empty flags types are not permitted yet.
#t = List(Flags([]))
#test_heap(t, [{},{},{}], [0,3],
# [])
#t = List(Tuple([Flags([]), U8()]))
#test_heap(t, [mk_tup({}, 42), mk_tup({}, 43), mk_tup({}, 44)], [0,3],
# [42,43,44])
t = List(Flags(['a','b']))
test_heap(t, [{'a':False,'b':False},{'a':False,'b':True},{'a':True,'b':True}], [0,3],
[0,2,3])
Expand Down