Skip to content

[mypyc] Introduce GetElementPtr #9260

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 4 commits into from
Aug 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
using field name instead of field index
  • Loading branch information
TH3CHARLie committed Aug 4, 2020
commit 807ab93a7098ae1f7e57c2086cf383aae4aa2a6d
5 changes: 2 additions & 3 deletions mypyc/codegen/emitfunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,9 +476,8 @@ def visit_get_element_ptr(self, op: GetElementPtr) -> None:
src = self.reg(op.src)
# TODO: support tuple type
assert isinstance(op.src_type, RStruct)
assert op.index < len(op.src_type.offsets), "Invalid element index."
offset = op.src_type.offsets[op.index]
self.emit_line('%s = (char *)%s + %s;' % (dest, src, offset))
assert op.field in op.src_type.names, "Invalid field name."
self.emit_line('%s = &%s.%s;' % (dest, src, op.field))

# Helpers

Expand Down
6 changes: 3 additions & 3 deletions mypyc/ir/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1377,19 +1377,19 @@ class GetElementPtr(RegisterOp):
"""Get the address of a struct element"""
error_kind = ERR_NEVER

def __init__(self, src: Value, src_type: RType, index: int, line: int = -1) -> None:
def __init__(self, src: Value, src_type: RType, field: str, line: int = -1) -> None:
super().__init__(line)
self.type = c_pyssize_t_rprimitive
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Later on, this can be switched to a dedicated pointer rtype (which is still treated as an integer).

self.src = src
self.src_type = src_type
self.index = index
self.field = field

def sources(self) -> List[Value]:
return [self.src]

def to_str(self, env: Environment) -> str:
return env.format("%r = get_element_ptr %r %r :: %r", self, self.src,
self.index, self.src_type)
self.field, self.src_type)

def accept(self, visitor: 'OpVisitor[T]') -> T:
return visitor.visit_get_element_ptr(self)
Expand Down
14 changes: 7 additions & 7 deletions mypyc/test/test_emitfunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,14 @@ def test_load_mem(self) -> None:
"""cpy_r_r0 = *(char *)cpy_r_i64;""")

def test_get_element_ptr(self) -> None:
info = StructInfo("", [], [bool_rprimitive, int32_rprimitive, int64_rprimitive])
info = StructInfo("Foo", ["b", "i32", "i64"], [bool_rprimitive, int32_rprimitive, int64_rprimitive])
r = RStruct(info)
self.assert_emit(GetElementPtr(self.o, r, 0),
"""cpy_r_r0 = (char *)cpy_r_o + 0;""")
self.assert_emit(GetElementPtr(self.o, r, 1),
"""cpy_r_r00 = (char *)cpy_r_o + 4;""")
self.assert_emit(GetElementPtr(self.o, r, 2),
"""cpy_r_r01 = (char *)cpy_r_o + 8;""")
self.assert_emit(GetElementPtr(self.o, r, "b"),
"""cpy_r_r0 = &cpy_r_o.b;""")
self.assert_emit(GetElementPtr(self.o, r, "i32"),
"""cpy_r_r00 = &cpy_r_o.i32;""")
self.assert_emit(GetElementPtr(self.o, r, "i64"),
"""cpy_r_r01 = &cpy_r_o.i64;""")

def assert_emit(self, op: Op, expected: str) -> None:
self.emitter.fragments = []
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy