ContinuumIO/lltypes

Name: lltypes

Owner: Continuum Analytics, Inc.

Description: A type system for Python backed by llvm and ctypes

Created: 2013-06-22 17:38:18.0

Updated: 2017-09-17 18:43:40.0

Pushed: 2013-06-26 15:23:26.0

Homepage: null

Size: 161

Language: Python

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

lltypes

A type system for Python backed by llvm and ctypes

This project is a wrapping for spelling and translating between ctypes, LLVM types and Numpy dtype.

Example: Structs

In C99 we might define the following structure:

ct {
ool a;
nt b;
loat c;
struct;

We can map this structure in Python:

 lltypes import *
ruct = Struct(
'mystruct',
Bool('a'),
Int32('b'),
Float32('c'),

Which can be converted to ctypes using to_ctypes:

truct = mystruct.to_ctypes()

 = mycstruct(
True,
3,
3.14


inst.a

inst.b

inst.c
0000104904175

And to LLVM using to_llvm:

ruct = mystruct.to_llvm()

print llstruct
truct = type { i1, i32, float }

And to dtype using to_dtype:

ruct = mystruct.to_dtype()

print dtstruct
e([('a', '?'), ('b', 'i32'), ('c', '<f4')])
Example: Arrays

Blaze defines a family of parameterized types for its array objects. These are first class polytypes in lltypes with the following schema:

= 1 | 2 | 3 | 4 | 5

 := Byte | Int8 | Int32 | ...

 := Array_C <mono> <nd>
  | Array_F <mono> <nd>
  | Array_S <mono> <nd>

In C these are structures of array kinds parameterized by eltype and nd.

ontiguous or Fortran
ct {
ltype *data;
ntp shape[nd];
ray_C;

ct {
ltype *data;
iminfo shape[nd];
ray_F;

ct {
ltype *data;
ntp shape[nd];
ntp stride[nd];
ray_S;

In lltypes these are expanded out into lower types by a simple function.

Array_C(name, ty, nd):
return Struct('Array_C',
    Pointer(ty('data')),
    Sequence(UNInt8('shape'), nd),
)

Array_F(name, ty, nd):
return Struct('Array_F',
    Pointer(ty('data')),
    Sequence(UNInt8('shape'), nd),
)

Array_S(name, ty, nd):
return Struct('Array_S',
    Pointer(ty('data')),
    Sequence(UNInt8('shape'), nd),
    Sequence(UNInt8('stride'), nd),
)
ython
c = Array_C('foo', UNInt8, 3)
f = Array_F('foo', UNInt8, 3)
s = Array_S('foo', UNInt8, 3)

print c.to_llvm()
ay_C = type { i8*, [3 x i8] }
print f.to_llvm()
ay_F = type { i8*, [3 x i8] }
print s.to_llvm()
ay_S = type { i8*, [3 x i8], [3 x i8] }
Tests

Test suite can be run with either of the following:

on -m unittest discover

or:

 lltypes import test
()

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.