1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
# Python bindings for Yasm: Pyrex input file for symrec.h
#
# Copyright (C) 2006 Michael Urman, Peter Johnson
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
cdef class Symbol:
cdef yasm_symrec *sym
def __new__(self, symrec):
self.sym = NULL
if PyCObject_Check(symrec):
self.sym = <yasm_symrec *>__get_voidp(symrec, Symbol)
else:
raise NotImplementedError
# no deref or destroy necessary
property name:
def __get__(self): return yasm_symrec_get_name(self.sym)
property status:
def __get__(self):
cdef yasm_sym_status status
s = set()
status = yasm_symrec_get_status(self.sym)
if <int>status & <int>SYM_USED: s.add('used')
if <int>status & <int>SYM_DEFINED: s.add('defined')
if <int>status & <int>SYM_VALUED: s.add('valued')
return s
property in_table:
def __get__(self):
return bool(<int>yasm_symrec_get_status(self.sym) &
<int>SYM_NOTINTABLE)
property visibility:
def __get__(self):
cdef yasm_sym_vis vis
s = set()
vis = yasm_symrec_get_visibility(self.sym)
if <int>vis & <int>YASM_SYM_GLOBAL: s.add('global')
if <int>vis & <int>YASM_SYM_COMMON: s.add('common')
if <int>vis & <int>YASM_SYM_EXTERN: s.add('extern')
if <int>vis & <int>YASM_SYM_DLOCAL: s.add('dlocal')
return s
property equ:
def __get__(self):
cdef yasm_expr *e
e = yasm_symrec_get_equ(self.sym)
if not e:
raise AttributeError("not an EQU")
return __make_expression(yasm_expr_copy(e))
property label:
def __get__(self):
cdef yasm_symrec_get_label_bytecodep bc
if yasm_symrec_get_label(self.sym, &bc):
return None #Bytecode(bc)
else:
raise AttributeError("not a label or not defined")
property is_special:
def __get__(self): return bool(yasm_symrec_is_special(self.sym))
property is_curpos:
def __get__(self): return bool(yasm_symrec_is_curpos(self.sym))
def get_data(self): pass # TODO
#return <object>(yasm_symrec_get_data(self.sym, PyYasmAssocData))
def set_data(self, data): pass # TODO
#yasm_symrec_set_data(self.sym, PyYasmAssocData, data)
#
# Use associated data mechanism to keep Symbol reference paired with symrec.
#
cdef void __python_symrec_cb_destroy(void *data):
Py_DECREF(<object>data)
cdef void __python_symrec_cb_print(void *data, FILE *f, int indent_level):
pass
__python_symrec_cb = __assoc_data_callback(
PyCObject_FromVoidPtr(&__python_symrec_cb_destroy, NULL),
PyCObject_FromVoidPtr(&__python_symrec_cb_print, NULL))
cdef object __make_symbol(yasm_symrec *symrec):
cdef void *data
__error_check()
data = yasm_symrec_get_data(symrec,
(<__assoc_data_callback>__python_symrec_cb).cb)
if data != NULL:
return <object>data
symbol = Symbol(__pass_voidp(symrec, Symbol))
yasm_symrec_add_data(symrec,
(<__assoc_data_callback>__python_symrec_cb).cb,
<void *>symbol)
Py_INCREF(symbol) # We're keeping a reference on the C side!
return symbol
cdef class Bytecode
cdef class SymbolTable
cdef class SymbolTableKeyIterator:
cdef yasm_symtab_iter *iter
def __new__(self, symtab):
if not isinstance(symtab, SymbolTable):
raise TypeError
self.iter = yasm_symtab_first((<SymbolTable>symtab).symtab)
def __iter__(self):
return self
def __next__(self):
if self.iter == NULL:
raise StopIteration
rv = yasm_symrec_get_name(yasm_symtab_iter_value(self.iter))
self.iter = yasm_symtab_next(self.iter)
return rv
cdef class SymbolTableValueIterator:
cdef yasm_symtab_iter *iter
def __new__(self, symtab):
if not isinstance(symtab, SymbolTable):
raise TypeError
self.iter = yasm_symtab_first((<SymbolTable>symtab).symtab)
def __iter__(self):
return self
def __next__(self):
if self.iter == NULL:
raise StopIteration
rv = __make_symbol(yasm_symtab_iter_value(self.iter))
self.iter = yasm_symtab_next(self.iter)
return rv
cdef class SymbolTableItemIterator:
cdef yasm_symtab_iter *iter
def __new__(self, symtab):
if not isinstance(symtab, SymbolTable):
raise TypeError
self.iter = yasm_symtab_first((<SymbolTable>symtab).symtab)
def __iter__(self):
return self
def __next__(self):
cdef yasm_symrec *sym
if self.iter == NULL:
raise StopIteration
sym = yasm_symtab_iter_value(self.iter)
rv = (yasm_symrec_get_name(sym), __make_symbol(sym))
self.iter = yasm_symtab_next(self.iter)
return rv
cdef int __parse_vis(vis) except -1:
if not vis or vis == 'local': return YASM_SYM_LOCAL
if vis == 'global': return YASM_SYM_GLOBAL
if vis == 'common': return YASM_SYM_COMMON
if vis == 'extern': return YASM_SYM_EXTERN
if vis == 'dlocal': return YASM_SYM_DLOCAL
msg = "bad visibility value %r" % vis
PyErr_SetString(ValueError, msg)
return -1
cdef class SymbolTable:
cdef yasm_symtab *symtab
def __new__(self):
self.symtab = yasm_symtab_create()
def __dealloc__(self):
if self.symtab != NULL: yasm_symtab_destroy(self.symtab)
def use(self, name, line):
return __make_symbol(yasm_symtab_use(self.symtab, name, line))
def define_equ(self, name, expr, line):
if not isinstance(expr, Expression):
raise TypeError
return __make_symbol(yasm_symtab_define_equ(self.symtab, name,
yasm_expr_copy((<Expression>expr).expr), line))
def define_label(self, name, precbc, in_table, line):
if not isinstance(precbc, Bytecode):
raise TypeError
return __make_symbol(yasm_symtab_define_label(self.symtab, name,
(<Bytecode>precbc).bc, in_table, line))
def define_special(self, name, vis):
return __make_symbol(
yasm_symtab_define_special(self.symtab, name,
<yasm_sym_vis>__parse_vis(vis)))
def declare(self, name, vis, line):
return __make_symbol(
yasm_symtab_declare(self.symtab, name,
<yasm_sym_vis>__parse_vis(vis), line))
#
# Methods to make SymbolTable behave like a dictionary of Symbols.
#
def __getitem__(self, key):
cdef yasm_symrec *symrec
symrec = yasm_symtab_get(self.symtab, key)
if symrec == NULL:
raise KeyError
return __make_symbol(symrec)
def __contains__(self, key):
cdef yasm_symrec *symrec
symrec = yasm_symtab_get(self.symtab, key)
return symrec != NULL
def keys(self):
cdef yasm_symtab_iter *iter
l = []
iter = yasm_symtab_first(self.symtab)
while iter != NULL:
l.append(yasm_symrec_get_name(yasm_symtab_iter_value(iter)))
iter = yasm_symtab_next(iter)
return l
def values(self):
cdef yasm_symtab_iter *iter
l = []
iter = yasm_symtab_first(self.symtab)
while iter != NULL:
l.append(__make_symbol(yasm_symtab_iter_value(iter)))
iter = yasm_symtab_next(iter)
return l
def items(self):
cdef yasm_symtab_iter *iter
cdef yasm_symrec *sym
l = []
iter = yasm_symtab_first(self.symtab)
while iter != NULL:
sym = yasm_symtab_iter_value(iter)
l.append((yasm_symrec_get_name(sym), __make_symbol(sym)))
iter = yasm_symtab_next(iter)
return l
def has_key(self, key):
cdef yasm_symrec *symrec
symrec = yasm_symtab_get(self.symtab, key)
return symrec != NULL
def get(self, key, x):
cdef yasm_symrec *symrec
symrec = yasm_symtab_get(self.symtab, key)
if symrec == NULL:
return x
return __make_symbol(symrec)
def iterkeys(self): return SymbolTableKeyIterator(self)
def itervalues(self): return SymbolTableValueIterator(self)
def iteritems(self): return SymbolTableItemIterator(self)
def __iter__(self): return SymbolTableKeyIterator(self)
|