Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Mypyc Object Representation

Jukka Lehtosalo edited this pageOct 24, 2022 ·2 revisions

Mypyc uses a tagged pointer representation for values of typeint(CPyTagged),char for booleans, and C structs for tuples. For mostother objects mypyc uses the CPythonPyObject *.

Tagged Integers

Python integers that fit in 31/63 bits (depending on whether we are ona 32-bit or 64-bit platform) are represented as C integers(CPyTagged) shifted left by 1. Integers that don't fit in thisrepresentation are represented as pointers to aPyObject * (this isalways a Pythonint object) with the least significant bitset.

Tagged integers have an arbitrary precision. By using a tagged pointerrepresentation, common operations are pretty quick and don't requireusing heap-allocated objects.

Tagged integer operations are defined inmypyc/lib-rt/int_ops.candmypyc/lib-rt/CPy.h.

Native Integers

There are also native, fixed-width integer types, such asint32 (seemypyc.ir.rtypes), that don't use the tagged representation. Thesetypes are not yet exposed to users, but they are used in generated code.(Exposing these to users is work in progress.)

Error Values

If an exception is raised in a function or in a primitive operation, wenormally represent it through anerror value. Each type has someerror value, which is normally chosen so that it isn't a valid realvalue:

  • For tagged integers, the error value is 1.
  • For bool, the error value is 2 (False and True are represented as 0 and 1, respectively).
  • For any value of typePyObject *, the error value is 0 (null pointer).

Some types can't have a reserved error value, such asint32. For these,we use anoverlapping error value. Errors are always signaled usingthe error value, but it could also represent a valid value. We must callPyErr_Occurred() to double check if the value is really an error. Theerror value is chosen so that it comes up rarely. This way we can mostlyavoid callingPyErr_Occurred() needlessly.

If we call a function, we must usually check for an error value in thegenerated C. Example pseudo-C:

r0=myfunction();if (r0==ERROR) {<addtracebackentry>;returnERROR;//propagateerrortocaller}

Mypyc has a pass that inserts error value checks automatically. When generatingIR, it's normally just necessary to describe how errors are reported foreach op that could fail. Operations have theerror_kind attribute for this purpose.Typical values includeERR_MAGIC (use type-specific error value to signal anerror) andERR_NEVER (the op can never fail, or it aborts the process on error).

Clone this wiki locally


[8]ページ先頭

©2009-2025 Movatter.jp