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 2 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
5 changes: 4 additions & 1 deletion mypyc/analysis/dataflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
BasicBlock, OpVisitor, Assign, LoadInt, LoadErrorValue, RegisterOp, Goto, Branch, Return, Call,
Environment, Box, Unbox, Cast, Op, Unreachable, TupleGet, TupleSet, GetAttr, SetAttr,
LoadStatic, InitStatic, PrimitiveOp, MethodCall, RaiseStandardError, CallC, LoadGlobal,
Truncate, BinaryIntOp, LoadMem
Truncate, BinaryIntOp, LoadMem, GetElementPtr
)


Expand Down Expand Up @@ -211,6 +211,9 @@ def visit_binary_int_op(self, op: BinaryIntOp) -> GenAndKill:
def visit_load_mem(self, op: LoadMem) -> GenAndKill:
return self.visit_register_op(op)

def visit_get_element_ptr(self, op: GetElementPtr) -> GenAndKill:
return self.visit_register_op(op)


class DefinedVisitor(BaseAnalysisVisitor):
"""Visitor for finding defined registers.
Expand Down
13 changes: 11 additions & 2 deletions mypyc/codegen/emitfunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
LoadStatic, InitStatic, TupleGet, TupleSet, Call, IncRef, DecRef, Box, Cast, Unbox,
BasicBlock, Value, MethodCall, PrimitiveOp, EmitterInterface, Unreachable, NAMESPACE_STATIC,
NAMESPACE_TYPE, NAMESPACE_MODULE, RaiseStandardError, CallC, LoadGlobal, Truncate,
BinaryIntOp, LoadMem
BinaryIntOp, LoadMem, GetElementPtr
)
from mypyc.ir.rtypes import (
RType, RTuple, is_tagged, is_int32_rprimitive, is_int64_rprimitive
RType, RTuple, is_tagged, is_int32_rprimitive, is_int64_rprimitive, RStruct
)
from mypyc.ir.func_ir import FuncIR, FuncDecl, FUNC_STATICMETHOD, FUNC_CLASSMETHOD
from mypyc.ir.class_ir import ClassIR
Expand Down Expand Up @@ -471,6 +471,15 @@ def visit_load_mem(self, op: LoadMem) -> None:
type = self.ctype(op.type)
self.emit_line('%s = *(%s *)%s;' % (dest, type, src))

def visit_get_element_ptr(self, op: GetElementPtr) -> None:
dest = self.reg(op)
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))

# Helpers

def label(self, label: BasicBlock) -> str:
Expand Down
29 changes: 28 additions & 1 deletion mypyc/ir/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
from mypyc.ir.rtypes import (
RType, RInstance, RTuple, RVoid, is_bool_rprimitive, is_int_rprimitive,
is_short_int_rprimitive, is_none_rprimitive, object_rprimitive, bool_rprimitive,
short_int_rprimitive, int_rprimitive, void_rtype, is_c_py_ssize_t_rprimitive
short_int_rprimitive, int_rprimitive, void_rtype, is_c_py_ssize_t_rprimitive,
c_pyssize_t_rprimitive
)
from mypyc.common import short_name

Expand Down Expand Up @@ -1372,6 +1373,28 @@ def accept(self, visitor: 'OpVisitor[T]') -> T:
return visitor.visit_load_mem(self)


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:
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

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)

def accept(self, visitor: 'OpVisitor[T]') -> T:
return visitor.visit_get_element_ptr(self)


@trait
class OpVisitor(Generic[T]):
"""Generic visitor over ops (uses the visitor design pattern)."""
Expand Down Expand Up @@ -1482,6 +1505,10 @@ def visit_binary_int_op(self, op: BinaryIntOp) -> T:
def visit_load_mem(self, op: LoadMem) -> T:
raise NotImplementedError

@abstractmethod
def visit_get_element_ptr(self, op: GetElementPtr) -> T:
raise NotImplementedError


# TODO: Should this live somewhere else?
LiteralsMap = Dict[Tuple[Type[object], Union[int, float, str, bytes, complex]], str]
Expand Down
14 changes: 12 additions & 2 deletions mypyc/test/test_emitfunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
from mypyc.ir.ops import (
Environment, BasicBlock, Goto, Return, LoadInt, Assign, IncRef, DecRef, Branch,
Call, Unbox, Box, TupleGet, GetAttr, PrimitiveOp, RegisterOp,
SetAttr, Op, Value, CallC, BinaryIntOp, LoadMem
SetAttr, Op, Value, CallC, BinaryIntOp, LoadMem, GetElementPtr
)
from mypyc.ir.rtypes import (
RTuple, RInstance, int_rprimitive, bool_rprimitive, list_rprimitive,
dict_rprimitive, object_rprimitive, c_int_rprimitive, short_int_rprimitive, int32_rprimitive,
int64_rprimitive
int64_rprimitive, StructInfo, RStruct
)
from mypyc.ir.func_ir import FuncIR, FuncDecl, RuntimeArg, FuncSignature
from mypyc.ir.class_ir import ClassIR
Expand Down Expand Up @@ -282,6 +282,16 @@ def test_load_mem(self) -> None:
self.assert_emit(LoadMem(bool_rprimitive, self.i64),
"""cpy_r_r0 = *(char *)cpy_r_i64;""")

def test_get_element_ptr(self) -> None:
info = StructInfo("", [], [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;""")

def assert_emit(self, op: Op, expected: str) -> None:
self.emitter.fragments = []
self.declarations.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