Merge commit 'origin/master' into gallium-0.2
[mesa.git] / src / gallium / state_trackers / python / retrace / model.py
1 #!/usr/bin/env python
2 ##########################################################################
3 #
4 # Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
5 # All Rights Reserved.
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining a
8 # copy of this software and associated documentation files (the
9 # "Software"), to deal in the Software without restriction, including
10 # without limitation the rights to use, copy, modify, merge, publish,
11 # distribute, sub license, and/or sell copies of the Software, and to
12 # permit persons to whom the Software is furnished to do so, subject to
13 # the following conditions:
14 #
15 # The above copyright notice and this permission notice (including the
16 # next paragraph) shall be included in all copies or substantial portions
17 # of the Software.
18 #
19 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 # IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 # ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #
27 ##########################################################################
28
29
30 '''Trace data model.'''
31
32
33 import sys
34 import string
35 import format
36
37 try:
38 from cStringIO import StringIO
39 except ImportError:
40 from StringIO import StringIO
41
42
43 class Node:
44
45 def visit(self, visitor):
46 raise NotImplementedError
47
48 def __str__(self):
49 stream = StringIO()
50 formatter = format.DefaultFormatter(stream)
51 pretty_printer = PrettyPrinter(formatter)
52 self.visit(pretty_printer)
53 return stream.getvalue()
54
55
56 class Literal(Node):
57
58 def __init__(self, value):
59 self.value = value
60
61 def visit(self, visitor):
62 visitor.visit_literal(self)
63
64
65 class NamedConstant(Node):
66
67 def __init__(self, name):
68 self.name = name
69
70 def visit(self, visitor):
71 visitor.visit_named_constant(self)
72
73
74 class Array(Node):
75
76 def __init__(self, elements):
77 self.elements = elements
78
79 def visit(self, visitor):
80 visitor.visit_array(self)
81
82
83 class Struct(Node):
84
85 def __init__(self, name, members):
86 self.name = name
87 self.members = members
88
89 def visit(self, visitor):
90 visitor.visit_struct(self)
91
92
93 class Pointer(Node):
94
95 def __init__(self, address):
96 self.address = address
97
98 def visit(self, visitor):
99 visitor.visit_pointer(self)
100
101
102 class Call:
103
104 def __init__(self, klass, method, args, ret):
105 self.klass = klass
106 self.method = method
107 self.args = args
108 self.ret = ret
109
110 def visit(self, visitor):
111 visitor.visit_call(self)
112
113
114 class Trace:
115
116 def __init__(self, calls):
117 self.calls = calls
118
119 def visit(self, visitor):
120 visitor.visit_trace(self)
121
122
123 class Visitor:
124
125 def visit_literal(self, node):
126 raise NotImplementedError
127
128 def visit_named_constant(self, node):
129 raise NotImplementedError
130
131 def visit_array(self, node):
132 raise NotImplementedError
133
134 def visit_struct(self, node):
135 raise NotImplementedError
136
137 def visit_pointer(self, node):
138 raise NotImplementedError
139
140 def visit_call(self, node):
141 raise NotImplementedError
142
143 def visit_trace(self, node):
144 raise NotImplementedError
145
146
147 class PrettyPrinter:
148
149 def __init__(self, formatter):
150 self.formatter = formatter
151
152 def visit_literal(self, node):
153 if isinstance(node.value, basestring):
154 if len(node.value) >= 4096 or node.value.strip(string.printable):
155 self.formatter.text('...')
156 return
157
158 self.formatter.literal('"' + node.value + '"')
159 return
160
161 self.formatter.literal(repr(node.value))
162
163 def visit_named_constant(self, node):
164 self.formatter.literal(node.name)
165
166 def visit_array(self, node):
167 self.formatter.text('{')
168 sep = ''
169 for value in node.elements:
170 self.formatter.text(sep)
171 value.visit(self)
172 sep = ', '
173 self.formatter.text('}')
174
175 def visit_struct(self, node):
176 self.formatter.text('{')
177 sep = ''
178 for name, value in node.members:
179 self.formatter.text(sep)
180 self.formatter.variable(name)
181 self.formatter.text(' = ')
182 value.visit(self)
183 sep = ', '
184 self.formatter.text('}')
185
186 def visit_pointer(self, node):
187 self.formatter.address(node.address)
188
189 def visit_call(self, node):
190 if node.klass is not None:
191 self.formatter.function(node.klass + '::' + node.method)
192 else:
193 self.formatter.function(node.method)
194 self.formatter.text('(')
195 sep = ''
196 for name, value in node.args:
197 self.formatter.text(sep)
198 self.formatter.variable(name)
199 self.formatter.text(' = ')
200 value.visit(self)
201 sep = ', '
202 self.formatter.text(')')
203 if node.ret is not None:
204 self.formatter.text(' = ')
205 node.ret.visit(self)
206
207 def visit_trace(self, node):
208 for call in node.calls:
209 call.visit(self)
210 self.formatter.newline()
211