Skip to content

Commit dbb398f

Browse files
authored
Merge pull request #574 from thisisthepy/feat/tp-s2-land
TypedPython: list[list[int]] stage 1, float(str) (#443)
2 parents f7e25b6 + 4fde287 commit dbb398f

10 files changed

Lines changed: 567 additions & 31 deletions

File tree

‎python-multiplatform-ksp/src/main/python/typedpython/aot/emit.py‎

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ def owned_param(self, f: ir.Function, p: object) -> bool:
684684
if isinstance(p, ir.Param) and p.type is Type.OBJ and p.cls is not None:
685685
return p.name in assigned_names(f)
686686
if isinstance(p, ir.Param) and p.type in (Type.STR, *STR_LISTS, Type.STR_OPT, Type.BYTES, Type.OBJ_LIST, Type.DICT,
687-
Type.DICT_LIST):
687+
Type.DICT_LIST, Type.LIST_LIST):
688688
return p.name in assigned_names(f) # tier S1, S2a, S2b
689689
return False
690690

@@ -716,7 +716,7 @@ def signature(self, f: ir.Function) -> str:
716716
elif isinstance(p, ir.Param) and p.type is Type.DICT:
717717
self.uses_str = self.uses_dict = True
718718
params.append(f"tpa_dict *{self.param_c(f, p)} TPA_UNUSED") # tier S2b, borrowed
719-
elif isinstance(p, ir.Param) and p.type is Type.DICT_LIST:
719+
elif isinstance(p, ir.Param) and p.type in (Type.DICT_LIST, Type.LIST_LIST):
720720
self.uses_str = self.uses_dict = True
721721
params.append(f"tpa_obj_list *{self.param_c(f, p)} TPA_UNUSED") # tier S2b, borrowed
722722
elif isinstance(p, ir.Param) and p.type in CTYPE:
@@ -740,7 +740,7 @@ def signature(self, f: ir.Function) -> str:
740740
elif f.returns is Type.DICT:
741741
self.uses_str = self.uses_dict = True
742742
params.append("tpa_dict **out") # tier S2b, a new reference
743-
elif f.returns is Type.DICT_LIST:
743+
elif f.returns in (Type.DICT_LIST, Type.LIST_LIST):
744744
self.uses_str = self.uses_dict = True
745745
params.append("tpa_obj_list **out") # tier S2b, a new reference
746746
elif kinds is not None:
@@ -1777,6 +1777,10 @@ def ev_StrParseInt(self, e: ir.StrParseInt) -> str:
17771777
s = self.str_value(e.value, "the argument of int()")
17781778
return self.fallible_int(f"tpa_str_to_int({s}, @)")
17791779

1780+
def ev_StrParseFloat(self, e: ir.StrParseFloat) -> str:
1781+
s = self.str_value(e.value, "the argument of float()") # tier S2c
1782+
return self.fallible("double", f"tpa_str_to_f64({s}, @)")
1783+
17801784
def ev_StrWrite(self, e: ir.StrWrite) -> str:
17811785
s = self.str_value(e.value, "the argument of write()")
17821786
if e.err:
@@ -1893,7 +1897,7 @@ def value_kind(self, e: ir.Expr) -> str | None:
18931897
def kinds_agree(self, e: ir.Expr, kind: str, what: str) -> bool:
18941898
"""A list name or a call result bound to a list of `kind` must be a list of that kind: the classification merges
18951899
them, so a mismatch is a defect of the analysis, reported instead of generating a wrong access."""
1896-
got = self.value_kind(e)
1900+
got = "int" if isinstance(e, ir.ListRow) else self.value_kind(e) # (tier S2c: a row is big-capable)
18971901
if got is not None and got != kind:
18981902
self.bad("aot/type", what, f"a list of kind {got} is used where one of kind {kind} is needed (the list "
18991903
"classification did not merge them)")
@@ -2157,7 +2161,8 @@ def _is_class_local(self, name: str) -> bool:
21572161

21582162
def ev_Local(self, e: ir.Local) -> str:
21592163
if e.type not in CTYPE and e.type not in LIST and e.type not in (Type.STR, *STR_LISTS, Type.STR_OPT, Type.BYTES,
2160-
Type.OBJ_LIST, Type.DICT, Type.DICT_LIST) \
2164+
Type.OBJ_LIST, Type.DICT, Type.DICT_LIST,
2165+
Type.LIST_LIST) \
21612166
and not (e.type is Type.OBJ and self._is_class_local(e.name)):
21622167
self.ctype(e.type, e.name)
21632168
return f"v_{mangle(e.name)}"
@@ -2642,7 +2647,7 @@ def ev_Call(self, e: ir.Call) -> str:
26422647
t = self.dslot()
26432648
self.emit(f"if ({name}({', '.join(args + ['&' + t])})) {self.fail()}")
26442649
return t
2645-
if callee.returns is Type.DICT_LIST:
2650+
if callee.returns in (Type.DICT_LIST, Type.LIST_LIST):
26462651
t = self.olslot()
26472652
self.emit(f"if ({name}({', '.join(args + ['&' + t])})) {self.fail()}")
26482653
return t
@@ -2748,15 +2753,40 @@ def list_value(self, e: ir.Expr, want: Type) -> tuple[str, bool]:
27482753
return "NULL", True
27492754
if isinstance(e, (ir.Local, ir.FieldGet)):
27502755
return self.ev(e), False
2756+
if isinstance(e, ir.ListRow):
2757+
return self.ev(e), False # tier S2c: the row itself, borrowed
27512758
return self.ev(e), True
27522759

2760+
def ev_ListRow(self, e: ir.ListRow) -> str:
2761+
"""`m[i]` of a list[list[int]]: the row, borrowed from the list (the name it is bound to retains it)."""
2762+
lst = f"v_{mangle(e.array)}"
2763+
idx = self.index_c(e.index, "tpa_int_to_ssize") if self.rep(e.index) != TAGGED else None
2764+
if idx is None:
2765+
v, n = self.ev(e.index), self.tmp("int64_t")
2766+
self.emit(f"if (tpa_slot_int(({lst})->len, {v}, 0, &{n})) {self.fail()}")
2767+
idx = n
2768+
p = self.tmp("void *", "NULL")
2769+
self.emit(f"if (tpa_obj_list_get({lst}, {idx}, &{p})) {self.fail()}")
2770+
return f"((tpa_int_list *){p})"
2771+
2772+
def ev_ListListDisplay(self, e: ir.ListListDisplay) -> str:
2773+
self.g.uses_str = self.g.uses_dict = True
2774+
t = self.olslot()
2775+
self.emit(f"if (tpa_obj_list_alloc({len(e.rows)}, tpa_int_list__vret, tpa_int_list__vrel, &{t})) {self.fail()}")
2776+
for i, r in enumerate(e.rows):
2777+
if self.kind(r) != "int":
2778+
self.bad("aot/type", r, "a row of a list[list[int]] is not big-capable (the list classification did "
2779+
"not mark it)")
2780+
self.emit(f"tpa_int_list_retain(v_{mangle(r)}); {t}->d[{i}] = v_{mangle(r)};")
2781+
return t
2782+
27532783
def st_Assign(self, s: ir.Assign) -> None:
27542784
want = self.types.get(s.target)
27552785
if want is Type.STR_OPT: # tier S2a: a str, None or another `str | None`
27562786
self.emit(f"tpa_str_set(&v_{mangle(s.target)}, {self.ev(s.value)});")
27572787
return
2758-
if want is Type.DICT_LIST: # tier S2b: a list of dicts, shared by the name
2759-
if s.value.type is not Type.DICT_LIST:
2788+
if want in (Type.DICT_LIST, Type.LIST_LIST): # tier S2b / S2c: a list of dicts or of lists, shared
2789+
if s.value.type is not want:
27602790
self.bad("aot/type", s.target, f"`{s.target}` is a list of dicts but is assigned a {s.value.type.value}")
27612791
return
27622792
self.emit(f"tpa_obj_list_set(&v_{mangle(s.target)}, {self.ev(s.value)});")
@@ -3073,6 +3103,9 @@ def grow_list(self, name: str, what: str) -> tuple[str, str] | None:
30733103
return f"v_{mangle(name)}", "str"
30743104
if self.types.get(name) is Type.OBJ_LIST:
30753105
return f"v_{mangle(name)}", "obj" # tier S2a: a list of class instances
3106+
if self.types.get(name) is Type.LIST_LIST:
3107+
self.g.uses_str = self.g.uses_dict = True
3108+
return f"v_{mangle(name)}", "obj" # tier S2c: a list of int lists
30763109
if self.types.get(name) is Type.DICT_LIST:
30773110
self.g.uses_str = self.g.uses_dict = True
30783111
return f"v_{mangle(name)}", "dict" # tier S2b: a list of dicts (a tpa_obj_list)
@@ -3097,7 +3130,12 @@ def list_item_c(self, e: ir.Expr, name: str) -> str:
30973130
if self.types.get(name) is Type.STR_OPT_LIST:
30983131
return self.opt_str_value(e, f"the item given to a method of `{name}`")
30993132
if self.types.get(name) in (Type.OBJ_LIST, Type.DICT_LIST):
3100-
return self.ev(e) # an instance or a dict, borrowed (the list retains it)
3133+
return self.ev(e)
3134+
if self.types.get(name) is Type.LIST_LIST:
3135+
if self.kind(e.name) != "int": # type: ignore[attr-defined]
3136+
self.bad("aot/type", name, "a row of a list[list[int]] is not big-capable (the list classification "
3137+
"did not mark it)")
3138+
return self.ev(e) # a row, borrowed (the list retains it) # an instance or a dict, borrowed (the list retains it)
31013139
if self.types.get(name) in LIST and self.kind(name) == "int":
31023140
return self.tagged(e)
31033141
if e.type is Type.I64 and self.rep(e) == TAGGED:
@@ -3347,6 +3385,9 @@ def ev_ListCompare(self, e: ir.ListCompare) -> str:
33473385

33483386
def ev_ListStr(self, e: ir.ListStr) -> str:
33493387
"""repr of a list[float] / list[int], a new str held by a slot (borrowed by the expression that reads it)."""
3388+
if self.types.get(e.array) is Type.LIST_LIST: # tier S2c: `[[1, 2], [3]]`
3389+
self.g.uses_str = self.g.uses_dict = True
3390+
return self.pstr(f"tpa_list_list_repr(v_{mangle(e.array)}, @)")
33503391
found = self.list_of(e.array, "str()")
33513392
if found is None:
33523393
return "NULL"
@@ -3669,7 +3710,7 @@ def st_Return(self, s: ir.Return) -> None:
36693710
self.emit(f"*out = {v};")
36703711
self.leave()
36713712
return
3672-
if returns is Type.DICT_LIST:
3713+
if returns in (Type.DICT_LIST, Type.LIST_LIST):
36733714
v = self.ev(s.value)
36743715
self.emit(f"tpa_obj_list_retain({v});") # the caller gets its own reference
36753716
self.emit(f"*out = {v};")
@@ -3794,14 +3835,15 @@ def run(self) -> list[str] | None:
37943835
for p in f.params:
37953836
ok = ((isinstance(p, ir.Param) and p.type in CTYPE and p.cls is None)
37963837
or (isinstance(p, ir.Param) and p.type in (Type.STR, *STR_LISTS, Type.STR_OPT, Type.BYTES, Type.OBJ_LIST,
3797-
Type.DICT, Type.DICT_LIST))
3838+
Type.DICT, Type.DICT_LIST, Type.LIST_LIST))
37983839
or (isinstance(p, ir.Param) and p.type is Type.OBJ and p.cls is not None)
37993840
or (isinstance(p, ir.ArrayParam) and p.type in LIST and p.buffer is None))
38003841
if not ok:
38013842
self.bad("aot/type", getattr(p, "name", "?"), f"the parameter `{getattr(p, 'name', '?')}` is not an "
38023843
"int, float, bool, list[float], list[int] or compiled class")
38033844
if f.returns not in CTYPE and f.returns not in LIST and f.returns is not Type.NONE \
3804-
and f.returns not in (Type.STR, *STR_LISTS, Type.STR_OPT, Type.BYTES, Type.OBJ_LIST, Type.DICT, Type.DICT_LIST) \
3845+
and f.returns not in (Type.STR, *STR_LISTS, Type.STR_OPT, Type.BYTES, Type.OBJ_LIST, Type.DICT, Type.DICT_LIST,
3846+
Type.LIST_LIST) \
38053847
and not (f.returns is Type.OBJ and f.return_cls is not None) \
38063848
and self.g.tuple_kinds(f) is None:
38073849
self.bad("aot/type", f.name, f"the return type {f.returns.value} is outside the AOT mode (int, float, "
@@ -3813,7 +3855,7 @@ def run(self) -> list[str] | None:
38133855
self.g.uses_str = True
38143856
elif t is Type.OBJ_LIST:
38153857
pass # tier S2a
3816-
elif t in (Type.DICT, Type.DICT_LIST):
3858+
elif t in (Type.DICT, Type.DICT_LIST, Type.LIST_LIST):
38173859
self.g.uses_str = self.g.uses_dict = True # tier S2b
38183860
elif t not in LIST and not (t is Type.OBJ and name in f.local_classes):
38193861
self.ctype(t, name)
@@ -3827,7 +3869,7 @@ def run(self) -> list[str] | None:
38273869
self.emit(f"tpa_obj_list_set(&v_{mangle(p.name)}, pv_{mangle(p.name)});")
38283870
elif isinstance(p, ir.Param) and p.type is Type.DICT:
38293871
self.emit(f"tpa_dict_set(&v_{mangle(p.name)}, pv_{mangle(p.name)});")
3830-
elif isinstance(p, ir.Param) and p.type is Type.DICT_LIST:
3872+
elif isinstance(p, ir.Param) and p.type in (Type.DICT_LIST, Type.LIST_LIST):
38313873
self.emit(f"tpa_obj_list_set(&v_{mangle(p.name)}, pv_{mangle(p.name)});")
38323874
elif isinstance(p, ir.Param) and p.type is Type.OBJ and p.cls is not None:
38333875
mc = mangle(p.cls)
@@ -3853,7 +3895,7 @@ def run(self) -> list[str] | None:
38533895
elif isinstance(p, ir.Param) and p.type is Type.DICT:
38543896
decls.append(f" tpa_dict *v_{mangle(p.name)} TPA_UNUSED = NULL;")
38553897
releases.append(f" tpa_dict_release(v_{mangle(p.name)});")
3856-
elif isinstance(p, ir.Param) and p.type is Type.DICT_LIST:
3898+
elif isinstance(p, ir.Param) and p.type in (Type.DICT_LIST, Type.LIST_LIST):
38573899
decls.append(f" tpa_obj_list *v_{mangle(p.name)} TPA_UNUSED = NULL;")
38583900
releases.append(f" tpa_obj_list_release(v_{mangle(p.name)});")
38593901
elif isinstance(p, ir.Param) and p.type is Type.OBJ and p.cls is not None:
@@ -3880,7 +3922,7 @@ def run(self) -> list[str] | None:
38803922
decls.append(f" tpa_dict *v_{mangle(name)} TPA_UNUSED = NULL;")
38813923
releases.append(f" tpa_dict_release(v_{mangle(name)});")
38823924
continue
3883-
if t is Type.DICT_LIST:
3925+
if t in (Type.DICT_LIST, Type.LIST_LIST):
38843926
decls.append(f" tpa_obj_list *v_{mangle(name)} TPA_UNUSED = NULL;")
38853927
releases.append(f" tpa_obj_list_release(v_{mangle(name)});")
38863928
continue

‎python-multiplatform-ksp/src/main/python/typedpython/aot/program.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ def annotation(self, ann: ast.expr | None, what: str, node: ast.AST, allow_none:
240240
"not carry yet (a program has it: tier S2c)")
241241
elif _is_list_annotation(ann) or _is_str_list_annotation(ann) or _is_optional_str_annotation(ann):
242242
return
243+
elif not self.library and _is_list_list_annotation(ann):
244+
return # tier S2c: list[list[int]] (a library's C ABI does not carry it yet)
243245
elif _is_class_list_annotation(ann, self.class_names):
244246
return # tier S2a: list[C] of a compiled class
245247
elif _is_class_annotation(ann, self.class_names):
@@ -353,6 +355,7 @@ def stmt(self, s: ast.stmt) -> None:
353355
or _is_tuple_list_annotation(s.annotation)
354356
or _is_list_annotation(s.annotation)
355357
or _is_dict_list_annotation(s.annotation)
358+
or _is_list_list_annotation(s.annotation)
356359
or _list_class_name(s.annotation, self.class_names))):
357360
self.expr(s.value) # (`x: list[str] = []`: an empty list of str, tier S1b; of int, float and
358361
# tuples, tier S2a)
@@ -1085,6 +1088,13 @@ def _is_view_annotation(ann: ast.expr) -> bool:
10851088
return _is_dict_annotation(ast.Subscript(value=ast.Name(id="dict"), slice=pair))
10861089

10871090

1091+
def _is_list_list_annotation(ann: ast.expr) -> bool:
1092+
"""`list[list[int]]` (tier S2c)."""
1093+
return (isinstance(ann, ast.Subscript) and isinstance(ann.value, ast.Name) and ann.value.id == "list"
1094+
and isinstance(ann.slice, ast.Subscript) and isinstance(ann.slice.value, ast.Name)
1095+
and ann.slice.value.id == "list" and isinstance(ann.slice.slice, ast.Name) and ann.slice.slice.id == "int")
1096+
1097+
10881098
def _is_dict_list_annotation(ann: ast.expr) -> bool:
10891099
"""`list[dict[K, V]]` (tier S2b)."""
10901100
return (isinstance(ann, ast.Subscript) and isinstance(ann.value, ast.Name) and ann.value.id in ("list", "List")

‎python-multiplatform-ksp/src/main/python/typedpython/aot/rep.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,13 @@ def build_lists(self) -> None:
351351
self.bind((f.name, RETURN), n.value, f, cur, "return")
352352
elif isinstance(n, ir.Call):
353353
self.bind_call(n, f, cur)
354+
elif isinstance(n, ir.ListListDisplay):
355+
for r in n.rows: # tier S2c: every row of a list[list[int]] is big-capable
356+
if (f.name, r) in self._parent:
357+
self.mark_big((f.name, r), f.name, cur, "a row of a list[list[int]]")
358+
elif isinstance(n, ir.ListAppend) and isinstance(n.value, ir.Local) \
359+
and self.types_of(f).get(n.array) is Type.LIST_LIST and (f.name, n.value.name) in self._parent:
360+
self.mark_big((f.name, n.value.name), f.name, cur, "a row of a list[list[int]]")
354361
elif isinstance(n, ir.ListExtend) and (f.name, n.array) in self._parent \
355362
and (f.name, n.other) in self._parent:
356363
# tier S2a: the items of `other` move into `array` (a copy of each item, so one group)

0 commit comments

Comments
 (0)