public
Description: a ruby-to-pyc compiler
Clone URL: git://github.com/why/unholy.git
Search Repo:
 * lib/: initial checkin
why (author)
Mon May 05 02:11:15 -0700 2008
commit  4847fc032379eb12fc3401e69c7e0944f9231255
tree    205c8f668b961227d17cf52638e3a4f80bcd8c9e
...
 
 
 
...
1
2
3
0
@@ -0,0 +1,3 @@
0
+#!/usr/bin/env python
0
+import py_compile, sys
0
+py_compile.compile(sys.argv[1])
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
0
@@ -0,0 +1,47 @@
0
+#!/usr/bin/env python
0
+import dis, marshal, struct, sys, time, types
0
+
0
+def show_file(fname):
0
+ f = open(fname, "rb")
0
+ magic = f.read(4)
0
+ moddate = f.read(4)
0
+ modtime = time.asctime(time.localtime(struct.unpack('L', moddate)[0]))
0
+ print "magic %s" % (magic.encode('hex'))
0
+ print "moddate %s (%s)" % (moddate.encode('hex'), modtime)
0
+ code = marshal.load(f)
0
+ show_code(code)
0
+
0
+def show_code(code, indent=''):
0
+ print "%scode" % indent
0
+ indent += ' '
0
+ print "%sargcount %d" % (indent, code.co_argcount)
0
+ print "%snlocals %d" % (indent, code.co_nlocals)
0
+ print "%sstacksize %d" % (indent, code.co_stacksize)
0
+ print "%sflags %04x" % (indent, code.co_flags)
0
+ show_hex("code", code.co_code, indent=indent)
0
+ dis.disassemble(code)
0
+ print "%sconsts" % indent
0
+ for const in code.co_consts:
0
+ if type(const) == types.CodeType:
0
+ show_code(const, indent+' ')
0
+ else:
0
+ print " %s%r" % (indent, const)
0
+ print "%snames %r" % (indent, code.co_names)
0
+ print "%svarnames %r" % (indent, code.co_varnames)
0
+ print "%sfreevars %r" % (indent, code.co_freevars)
0
+ print "%scellvars %r" % (indent, code.co_cellvars)
0
+ print "%sfilename %r" % (indent, code.co_filename)
0
+ print "%sname %r" % (indent, code.co_name)
0
+ print "%sfirstlineno %d" % (indent, code.co_firstlineno)
0
+ show_hex("lnotab", code.co_lnotab, indent=indent)
0
+
0
+def show_hex(label, h, indent):
0
+ h = h.encode('hex')
0
+ if len(h) < 60:
0
+ print "%s%s %s" % (indent, label, h)
0
+ else:
0
+ print "%s%s" % (indent, label)
0
+ for i in range(0, len(h), 60):
0
+ print "%s %s" % (indent, h[i:i+60])
0
+
0
+show_file(sys.argv[1])
...
 
 
 
 
 
...
1
2
3
4
5
0
@@ -0,0 +1,5 @@
0
+#!/usr/bin/env ruby
0
+iseq = VM::InstructionSequence.compile(IO.read(ARGV[0]))
0
+iseq.to_a.last.each do |inst|
0
+ p inst
0
+end
...
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
0
@@ -0,0 +1,7 @@
0
+#!/usr/bin/env ruby
0
+require 'unholy'
0
+
0
+asm = Pyasm.new(ARGV[0])
0
+asm.import :Kernel
0
+asm.eval IO.read(ARGV[0])
0
+asm.compile(ARGV[0]+".pyc")
...
 
 
 
...
1
2
3
0
@@ -0,0 +1,3 @@
0
+require 'unholy/core'
0
+require 'unholy/tuple'
0
+require 'unholy/pyasm'
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
0
@@ -0,0 +1,19 @@
0
+class NilClass
0
+ def to_pickle; "N" end
0
+end
0
+
0
+class Integer
0
+ def to_plong; [self].pack("L") end
0
+ def to_pickle; "i#{to_plong}" end
0
+end
0
+
0
+class String
0
+ def to_pickle; "s" + length.to_plong + self end
0
+end
0
+
0
+class Symbol
0
+ def to_pickle
0
+ str = to_s
0
+ "t" + str.length.to_plong + str
0
+ end
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
0
@@ -0,0 +1,345 @@
0
+class Pyasm
0
+ OPS = {
0
+ :== => 2
0
+ }
0
+
0
+ attr_accessor :argc, :nlocals, :stacksize, :flags, :consts, :bytecode,
0
+ :filename, :lineno, :name, :symbols, :stacknow, :varsyms, :jumps, :labels
0
+ def initialize(fname, name = "<module>")
0
+ @argc, @nlocals, @stacksize, @flags, @filename, @lineno, @name, @stack, @nopop =
0
+ 0, 0, 1, 0x40, fname, 1, name, [], 0
0
+ @consts = [-1, nil]
0
+ @symbols = [:Kernel]
0
+ @bytecode, @varsyms, @labels, @jumps = [], [], {}, {}
0
+ end
0
+
0
+ def add_const(obj)
0
+ @consts << obj unless @consts.include? obj
0
+ @consts.index(obj)
0
+ end
0
+ def add_sym(name)
0
+ @symbols << name unless @symbols.include? name
0
+ @symbols.index(name)
0
+ end
0
+ def add_varsym(name)
0
+ @varsyms << name unless @varsyms.include? name
0
+ @varsyms.index(name)
0
+ end
0
+ def stack_push obj, b
0
+ @stack << [obj, b]
0
+ @stacksize = @stack.length if @stack.length > @stacksize
0
+ b
0
+ end
0
+ def dump_stack
0
+ @stack.clear
0
+ end
0
+ def bc *bytes
0
+ @bytecode << bytes
0
+ bytes
0
+ end
0
+ def mark_jump n, bc
0
+ @jumps[n] ||= []
0
+ @jumps[n] << bc
0
+ end
0
+
0
+ def pop_top; bc 0x01; dump_stack end
0
+ def ret_val; bc 0x53 end
0
+ def build_class; bc 0x59 end
0
+ def store_name(name)
0
+ dump_stack
0
+ bc 0x5a, add_sym(name), 0x0
0
+ end
0
+ def store_attr(name)
0
+ dump_stack
0
+ bc 0x5f, add_sym(name), 0x0
0
+ end
0
+ def load_const(obj)
0
+ stack_push obj, bc(0x64, add_const(obj), 0x0)
0
+ end
0
+ def load_name(name)
0
+ stack_push name, bc(0x65, add_sym(name), 0x0)
0
+ end
0
+ def build_tuple(n)
0
+ @stack.slice! -(n-1), (n-1)
0
+ bc(0x66, n, 0x0)
0
+ end
0
+ def build_list(n)
0
+ @stack.slice! -(n-1), (n-1)
0
+ bc(0x67, n, 0x0)
0
+ end
0
+ def load_attr(name)
0
+ stack_push name, bc(0x69, add_sym(name), 0x0)
0
+ end
0
+ def compare_op(op)
0
+ bc 0x6a, OPS[op], 0x0
0
+ end
0
+ def import_name(name)
0
+ bc 0x6b, add_sym(name), 0x0
0
+ end
0
+ def import_from(name)
0
+ bc 0x6c, add_sym(name), 0x0
0
+ end
0
+ def jump_if_false(n)
0
+ mark_jump n, bc(0x6f, n, 0x0)
0
+ end
0
+ def load_fast(n)
0
+ stack_push Object.new, bc(0x7c, n, 0x0)
0
+ end
0
+ def store_fast(n)
0
+ bc 0x7d, n, 0x0
0
+ end
0
+ def call_func(arity)
0
+ dump_stack
0
+ stack_push Object.new, bc(0x83, arity, 0x0)
0
+ end
0
+ def make_func(arity)
0
+ bc 0x84, arity, 0x0
0
+ end
0
+ def label l
0
+ @nopop -= 1 if @nopop > 0
0
+ @labels[l] = @bytecode.flatten.length
0
+ end
0
+
0
+ def import_split(mod)
0
+ mod = mod.to_s
0
+ return [mod[/^(.+?)(\.|$)/, 1].intern, mod.intern]
0
+ end
0
+
0
+ def getinlinecache ic, dst
0
+ end
0
+ def setinlinecache dst
0
+ end
0
+ def getlocal id
0
+ load_fast id - 2
0
+ end
0
+ def setlocal id
0
+ store_fast id - 2
0
+ end
0
+ def getconstant sym
0
+ load_name(sym)
0
+ end
0
+ def import(mod, inner = nil)
0
+ load_const(-1)
0
+
0
+ mod_in, mod = import_split(mod)
0
+ if inner
0
+ inner_in, inner = import_split(inner)
0
+ load_const(tuple(inner))
0
+ mod_in = inner_in
0
+ else
0
+ load_const(nil)
0
+ end
0
+
0
+ import_name(mod)
0
+ import_from(inner) if inner
0
+ store_name(mod_in)
0
+ pop_top if inner
0
+ end
0
+ def from(inner, mod)
0
+ import(inner, mod)
0
+ end
0
+ def kernel_send(meth, *args)
0
+ load_name(:Kernel)
0
+ load_attr(meth)
0
+ args.each do |obj|
0
+ load_const(obj)
0
+ end
0
+ call_func(args.length)
0
+ end
0
+
0
+ def leave
0
+ load_const(nil) if @stack.empty?
0
+ ret_val
0
+ end
0
+ def newarray size
0
+ build_list size
0
+ end
0
+ def putnil
0
+ load_const(nil)
0
+ end
0
+ def putstring(str)
0
+ str = @filename if str == "<compiled>"
0
+ load_const(str)
0
+ end
0
+ def message(meth, op_argc, blockiseq, op_flag, ic)
0
+ # args = @stack.slice! -op_argc, op_argc
0
+ # bytes = @bytecode.slice! -op_argc, op_argc
0
+
0
+ argc = op_argc + 1
0
+ receiver, recbytes = @stack[-argc]
0
+ args = @stack[-op_argc, op_argc].map { |o,_| o }
0
+ idx = @bytecode.index { |x| x.object_id == recbytes.object_id }
0
+ bytes = []
0
+ if idx
0
+ bytes = @bytecode.slice! idx..-1
0
+
0
+ unless receiver
0
+ unpop
0
+ case meth
0
+ when :import
0
+ return import(*args)
0
+ when :from
0
+ return from(*args)
0
+ when :tuple
0
+ return load_const(tuple(*args))
0
+ else
0
+ load_name(:Kernel)
0
+ bytes.shift
0
+ end
0
+ else
0
+ @bytecode << bytes.shift
0
+ end
0
+ elsif op_argc > 0
0
+ idx = @bytecode.index { |x| x.object_id == @stack[-op_argc][1].object_id }
0
+ bytes = @bytecode.slice! idx..-1
0
+ end
0
+
0
+ load_attr(meth)
0
+ @bytecode += bytes
0
+ call_func(op_argc)
0
+ end
0
+ def unpop
0
+ @nopop = 2
0
+ end
0
+ def pop
0
+ pop_top unless @nopop > 0
0
+ end
0
+ def opt_eq arg
0
+ compare_op :==
0
+ end
0
+ def getglobal sym
0
+ case sym when :$0
0
+ load_const(@filename)
0
+ end
0
+ end
0
+ def defineclass klass, iseq, is_singleton
0
+ define :class, klass, iseq, is_singleton
0
+ end
0
+ def definemethod meth, iseq, is_singleton
0
+ define :method, meth, iseq, is_singleton
0
+ end
0
+ def define type, id, iseq, is_singleton
0
+ c = (type == :class) ? -2 : -1
0
+ receiver, recbytes = @stack[c]
0
+ idx = @bytecode.index { |x| x.object_id == recbytes.object_id }
0
+ bytes = @bytecode.slice! idx..-1
0
+ bytes.shift unless receiver
0
+
0
+ asm = Pyasm.new(@filename, id.to_s)
0
+ asm.load_iseq iseq
0
+ if type == :class
0
+ load_const(id.to_s)
0
+ if bytes.empty?
0
+ load_const(tuple())
0
+ else
0
+ @bytecode += bytes
0
+ build_tuple(1)
0
+ end
0
+ end
0
+ load_const(asm)
0
+ make_func(0)
0
+ if type == :class
0
+ call_func(0)
0
+ build_class
0
+ else
0
+ @bytecode += bytes
0
+ end
0
+ store_name(id)
0
+
0
+ # this is a bit redundant, but decompyle requires it
0
+ if type == :method
0
+ unless receiver
0
+ load_name(id)
0
+ load_name(:Kernel)
0
+ store_attr(id)
0
+ end
0
+ end
0
+ end
0
+ def branchunless label
0
+ jump_if_false label
0
+ end
0
+
0
+ def load_iseq iseq
0
+ iseq = iseq.to_a
0
+ @varsyms += iseq[8].reverse
0
+
0
+ iseq.last.each do |inst|
0
+ case inst
0
+ when Integer # line no
0
+ nil
0
+ when Symbol
0
+ self.label inst
0
+ when Array
0
+ p inst
0
+ inst[0] = :message if inst[0] == :send
0
+ self.send *inst
0
+ end
0
+ end
0
+ end
0
+ def eval src
0
+ iseq = VM::InstructionSequence.compile(src)
0
+ load_iseq iseq
0
+ end
0
+
0
+ def to_pickle
0
+ # rewrite jumps to use python bytecode ordering
0
+ @jumps.each do |n, jset|
0
+ jset.each do |jump|
0
+ idx = @bytecode.index(jump)
0
+ pos = @bytecode[0,idx].flatten.length
0
+ jump[1] = (@labels[n] - pos) - jump.length
0
+ end
0
+ # @jumps[1] = @labels[n]
0
+ end
0
+
0
+ f = "c"
0
+ f << [@argc, @nlocals, @stacksize, @flags].pack("LLLL")
0
+
0
+ # bytecode
0
+ bytes = @bytecode.flatten
0
+ f << "s" << bytes.length.to_plong
0
+ f << bytes.pack("c*")
0
+
0
+ # constants
0
+ f << "(" << @consts.length.to_plong
0
+ @consts.each do |c|
0
+ f << c.to_pickle
0
+ end
0
+
0
+ # names
0
+ f << "(" << @symbols.length.to_plong
0
+ @symbols.each do |n|
0
+ f << n.to_pickle
0
+ end
0
+
0
+ # varnames
0
+ f << "(" << @varsyms.length.to_plong
0
+ @varsyms.each do |n|
0
+ f << n.to_pickle
0
+ end
0
+
0
+ # freevars
0
+ f << "(" << 0.to_plong
0
+
0
+ # cellvars
0
+ f << "(" << 0.to_plong
0
+
0
+ # metadata
0
+ f << @filename.to_pickle
0
+ f << @name.to_pickle
0
+ f << 1.to_plong
0
+ f << "".to_pickle
0
+ f
0
+ end
0
+
0
+ def compile(fname)
0
+ # dump the bytecode
0
+ File.open(fname, "wb") do |f|
0
+ f << "\xB3\xF2\r\n" # magic number for python 2.5
0
+ f << Time.now.to_i.to_plong
0
+
0
+ # code object
0
+ f << self.to_pickle
0
+ end
0
+ end
0
+end
...
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
0
@@ -0,0 +1,7 @@
0
+class Tuple < Array
0
+ def to_pickle
0
+ "(" + length.to_plong + map { |x| x.to_pickle }.join
0
+ end
0
+end
0
+
0
+def tuple(*ary); Tuple[*ary] end
...
 
 
...
1
2
0
@@ -0,0 +1,2 @@
0
+def puts(str):
0
+ print str

Comments

    No one has commented yet.