~ubuntu-branches/ubuntu/karmic/pypy/karmic

« back to all changes in this revision

Viewing changes to pypy/jit/codegen/llvm/genvarorconst.py

  • Committer: Bazaar Package Importer
  • Author(s): Alexandre Fayolle
  • Date: 2007-04-13 09:33:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070413093309-yoojh4jcoocu2krz
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import py, os
 
2
from pypy.rlib.objectmodel import specialize
 
3
from pypy.rpython.lltypesystem import lltype, llmemory
 
4
from pypy.rlib.rarithmetic import intmask
 
5
from pypy.jit.codegen.model import GenVar, GenConst
 
6
from pypy.jit.codegen.llvm.logger import logger
 
7
from pypy.jit.codegen.llvm.compatibility import i1, i8, i16, i32, f64
 
8
 
 
9
 
 
10
pi8  = i8  + '*'
 
11
pi32 = i32 + '*'
 
12
u32  = i32
 
13
 
 
14
 
 
15
class Count(object):
 
16
    n_vars = 0
 
17
    n_labels = 0
 
18
 
 
19
    def newlabel(self):
 
20
        label = 'L%d' % (self.n_labels,)
 
21
        self.n_labels += 1
 
22
        return label
 
23
 
 
24
count = Count()
 
25
 
 
26
 
 
27
class Var(GenVar):
 
28
 
 
29
    def __init__(self, type):
 
30
        self.n = count.n_vars
 
31
        self.type = type
 
32
        self.signed = type is i32 or type is f64
 
33
        count.n_vars += 1
 
34
 
 
35
    def operand(self):
 
36
        return '%s %s' % (self.type, self.operand2())
 
37
 
 
38
    def operand2(self):
 
39
        return '%%v%d' % (self.n,)
 
40
 
 
41
 
 
42
class GenericConst(GenConst):
 
43
 
 
44
    def operand(self):
 
45
        return '%s %s' % (self.type, self.operand2())
 
46
 
 
47
    @specialize.arg(1)
 
48
    def revealconst(self, T):
 
49
        if isinstance(T, lltype.Ptr):
 
50
            return lltype.cast_int_to_ptr(T, self.get_integer_value())
 
51
        elif T is llmemory.Address:
 
52
            return llmemory.cast_int_to_adr(self.get_integer_value())
 
53
        else:
 
54
            return lltype.cast_primitive(T, self.get_integer_value())
 
55
 
 
56
 
 
57
class BoolConst(GenericConst):
 
58
    type = i1
 
59
    signed = False
 
60
 
 
61
    def __init__(self, value):
 
62
        self.value = bool(value)
 
63
 
 
64
    def operand2(self):
 
65
        if self.value:
 
66
            return 'true'
 
67
        else:
 
68
            return 'false'
 
69
 
 
70
    def get_integer_value(self):
 
71
        return int(self.value)
 
72
 
 
73
 
 
74
class CharConst(GenericConst):
 
75
    type = i8
 
76
    signed = False
 
77
 
 
78
    def __init__(self, value):
 
79
        self.value = ord(value)
 
80
 
 
81
    def operand2(self):
 
82
        return '%d' % self.value
 
83
 
 
84
    def get_integer_value(self):
 
85
        return self.value
 
86
 
 
87
 
 
88
class UniCharConst(GenericConst):
 
89
    type = i32
 
90
    signed = True
 
91
 
 
92
    def __init__(self, value):
 
93
        self.value = unicode(value)
 
94
 
 
95
    def operand2(self):
 
96
        return '%s' % self.value
 
97
 
 
98
    def get_integer_value(self):
 
99
        return int(self.value)
 
100
 
 
101
 
 
102
class IntConst(GenericConst):
 
103
    type = i32
 
104
    signed = True
 
105
 
 
106
    def __init__(self, value):
 
107
        self.value = int(value)
 
108
 
 
109
    def operand2(self):
 
110
        return str(self.value)
 
111
 
 
112
    def get_integer_value(self):
 
113
        return self.value
 
114
 
 
115
 
 
116
class UIntConst(GenericConst):
 
117
    type = u32
 
118
    signed = False
 
119
 
 
120
    def __init__(self, value):
 
121
        self.value = value
 
122
 
 
123
    def operand2(self):
 
124
        return str(self.value)
 
125
 
 
126
    def get_integer_value(self):
 
127
        return intmask(self.value)
 
128
 
 
129
 
 
130
class FloatConst(GenericConst):
 
131
    type = f64
 
132
    signed = True
 
133
 
 
134
    def __init__(self, value):
 
135
        self.value = float(value)
 
136
 
 
137
    def operand2(self):
 
138
        return str(self.value)
 
139
 
 
140
    @specialize.arg(1)
 
141
    def revealconst(self, T):
 
142
        assert T is lltype.Float
 
143
        return self.value
 
144
 
 
145
 
 
146
class AddrConst(GenConst):
 
147
    type = pi8
 
148
    signed = False
 
149
    addr = llmemory.NULL #have 'addr' even when not instantiated
 
150
 
 
151
    def __init__(self, addr):
 
152
        self.addr = addr
 
153
 
 
154
    def operand(self):
 
155
        return '%s %s' % (self.type, self.operand2())
 
156
 
 
157
    def operand2(self):
 
158
        addr = self.addr
 
159
        s = str(llmemory.cast_adr_to_int(addr))
 
160
        if s == '0':
 
161
            s = 'null'
 
162
        return s
 
163
 
 
164
    @specialize.arg(1)
 
165
    def revealconst(self, T):
 
166
        if T is llmemory.Address:
 
167
            return self.addr
 
168
        elif isinstance(T, lltype.Ptr):
 
169
            return llmemory.cast_adr_to_ptr(self.addr, T)
 
170
        elif T is lltype.Signed:
 
171
            return llmemory.cast_adr_to_int(self.addr)
 
172
        else:
 
173
            msg = 'XXX not implemented'
 
174
            logger.dump(msg)
 
175
            assert 0, msg