@@ -231,11 +231,25 @@ Installing a released version of mypy using `pip` (which is compiled)
231
231
and using ` dmypy ` (mypy daemon) is a much, much faster way to type
232
232
check mypyc during development.
233
233
234
- ## Overview of Generated C
234
+ ## Value Representation
235
+
236
+ Mypyc uses a tagged pointer representation for values of type ` int `
237
+ (` CPyTagged ` ), ` char ` for booleans, and C structs for tuples. For most
238
+ other objects mypyc uses the CPython ` PyObject * ` .
239
+
240
+ Python integers that fit in 31/63 bits (depending on whether we are on
241
+ a 32-bit or 64-bit platform) are represented as C integers
242
+ (` CPyTagged ` ) shifted left by 1. Integers that don't fit in this
243
+ representation are represented as pointers to a ` PyObject * ` (this is
244
+ always a Python ` int ` object) with the least significant bit
245
+ set. Tagged integer operations are defined in ` mypyc/lib-rt/int_ops.c `
246
+ and ` mypyc/lib-rt/CPy.h ` .
235
247
236
- Mypyc uses a tagged pointer representation for integers, ` char ` for
237
- booleans, and C structs for tuples. For most other objects mypyc uses
238
- the CPython ` PyObject * ` .
248
+ There are also low-level integer types, such as ` int32 ` (see
249
+ ` mypyc.ir.rtypes ` ), that don't use the tagged representation. These
250
+ types are not exposed to users, but they are used in generated code.
251
+
252
+ ## Overview of Generated C
239
253
240
254
Mypyc compiles a function into two functions, a native function and
241
255
a wrapper function:
@@ -261,10 +275,8 @@ insert a runtime type check (an unbox or a cast operation), since
261
275
Python lists can contain arbitrary objects.
262
276
263
277
The generated code uses various helpers defined in
264
- ` mypyc/lib-rt/CPy.h ` . The header must only contain static functions,
265
- since it is included in many files. ` mypyc/lib-rt/CPy.c ` contains
266
- definitions that must only occur once, but really most of ` CPy.h `
267
- should be moved into it.
278
+ ` mypyc/lib-rt/CPy.h ` . The implementations are in various ` .c ` files
279
+ under ` mypyc/lib-rt ` .
268
280
269
281
## Inspecting Generated C
270
282
@@ -298,10 +310,10 @@ also write tests that test the generated IR, however.
298
310
### Tests that compile and run code
299
311
300
312
Test cases that compile and run code are located in
301
- ` test-data/run*.test ` and the test runner is in ` mypyc.test.test_run ` .
302
- The code to compile comes after ` [case test<name>] ` . The code gets
303
- saved into the file ` native.py ` , and it gets compiled into the module
304
- ` native ` .
313
+ ` mypyc/ test-data/run*.test` and the test runner is in
314
+ ` mypyc.test.test_run ` . The code to compile comes after `[ case
315
+ test< name > ] ` . The code gets saved into the file ` native.py`, and it
316
+ gets compiled into the module ` native ` .
305
317
306
318
Each test case uses a non-compiled Python driver that imports the
307
319
` native ` module and typically calls some compiled functions. Some
@@ -312,8 +324,10 @@ driver just calls each module-level function that is prefixed with
312
324
` test_ ` and reports any uncaught exceptions as failures. (Failure to
313
325
build or a segfault also count as failures.) ` testStringOps ` in
314
326
` mypyc/test-data/run-strings.test ` is an example of a test that uses
315
- the default driver. You should usually use the default driver. It's
316
- the simplest way to write most tests.
327
+ the default driver.
328
+
329
+ You should usually use the default driver (don't include
330
+ ` driver.py ` ). It's the simplest way to write most tests.
317
331
318
332
Here's an example test case that uses the default driver:
319
333
@@ -412,23 +426,22 @@ If you add an operation that compiles into a lot of C code, you may
412
426
also want to add a C helper function for the operation to make the
413
427
generated code smaller. Here is how to do this:
414
428
415
- * Add the operation to ` mypyc/lib-rt/CPy.h ` . Usually defining a static
416
- function is the right thing to do, but feel free to also define
417
- inline functions for very simple and performance-critical
418
- operations. We avoid macros since they are error-prone.
429
+ * Declare the operation in ` mypyc/lib-rt/CPy.h ` . We avoid macros, and
430
+ we generally avoid inline functions to make it easier to target
431
+ additional backends in the future.
419
432
420
433
* Consider adding a unit test for your C helper in ` mypyc/lib-rt/test_capi.cc ` .
421
434
We use
422
435
[ Google Test] ( https://github.com/google/googletest ) for writing
423
436
tests in C++. The framework is included in the repository under the
424
437
directory ` googletest/ ` . The C unit tests are run as part of the
425
- pytest test suite (` test_c_unit_tests ` ).
438
+ pytest test suite (` test_c_unit_test ` ).
426
439
427
440
### Adding a Specialized Primitive Operation
428
441
429
442
Mypyc speeds up operations on primitive types such as ` list ` and ` int `
430
443
by having primitive operations specialized for specific types. These
431
- operations are defined in ` mypyc.primitives ` (and
444
+ operations are declared in ` mypyc.primitives ` (and
432
445
` mypyc/lib-rt/CPy.h ` ). For example, ` mypyc.primitives.list_ops `
433
446
contains primitives that target list objects.
434
447
@@ -487,7 +500,7 @@ operations, and so on. You likely also want to add some faster,
487
500
specialized primitive operations for the type (see Adding a
488
501
Specialized Primitive Operation above for how to do this).
489
502
490
- Add a test case to ` mypyc/test-data/run.test ` to test compilation and
503
+ Add a test case to ` mypyc/test-data/run* .test ` to test compilation and
491
504
running compiled code. Ideas for things to test:
492
505
493
506
* Test using the type as an argument.
@@ -523,7 +536,9 @@ about how to do this.
523
536
524
537
* Feel free to open GitHub issues with questions if you need help when
525
538
contributing, or ask questions in existing issues. Note that we only
526
- support contributors. Mypyc is not (yet) an end-user product.
539
+ support contributors. Mypyc is not (yet) an end-user product. You
540
+ can also ask questions in our Gitter chat
541
+ (https://gitter.im/mypyc-dev/community ).
527
542
528
543
## Undocumented Workflows
529
544
0 commit comments