public
Description: a ruby-to-pyc compiler
Clone URL: git://github.com/why/unholy.git
Search Repo:
 * lib/unholy/pyasm.rb: rewriting the POP_TOP bytecodes is trick since 
 method name isn't placed on the stack in ruby, but is present in python.
why (author)
Mon May 05 20:10:31 -0700 2008
commit  3b65c66935a74db4c6580b425afe0518aa59699b
tree    d0ab7b1705d4c7748f16a546e38634d9233cc7cf
parent  6ee028c13014248abaebd0d9ef13f7127c853cc5
0
...
26
27
28
29
30
 
31
32
33
...
26
27
28
 
 
29
30
31
32
0
@@ -26,8 +26,7 @@
0
   To compile Ruby to a .pyc:
0
 
0
   > bin/unholy test.rb
0
- > PYTHONPATH=python \
0
- python test.rb.pyc
0
+ > python test.rb.pyc
0
   
0
             ---
0
 
...
4
5
6
7
8
 
 
9
10
 
11
12
13
 
14
15
16
...
43
44
45
46
47
48
49
50
51
52
53
...
91
92
93
94
95
96
97
98
...
101
102
103
104
105
 
 
106
107
108
109
110
111
...
163
164
165
166
167
168
169
170
171
...
214
215
216
217
 
218
219
220
...
237
238
239
240
 
241
242
243
244
245
...
278
279
280
281
 
282
283
 
284
285
286
 
287
288
289
...
345
346
347
348
349
350
351
352
353
354
355
 
356
357
358
...
4
5
6
 
 
7
8
9
 
10
11
12
 
13
14
15
16
...
43
44
45
 
 
 
 
 
46
47
48
...
86
87
88
 
89
90
91
92
...
95
96
97
 
 
98
99
100
101
 
102
103
104
...
156
157
158
 
 
 
159
160
161
...
204
205
206
 
207
208
209
210
...
227
228
229
 
230
231
232
233
234
235
...
268
269
270
 
271
272
 
273
274
275
276
277
278
279
280
...
336
337
338
 
 
 
 
 
 
 
 
339
340
341
342
0
@@ -4,13 +4,13 @@
0
   }
0
 
0
   attr_accessor :argc, :nlocals, :stacksize, :flags, :consts, :bytecode,
0
- :filename, :lineno, :name, :symbols, :stacknow, :varsyms, :jumps, :labels, :lines
0
- def initialize(fname, name = "<module>", lineno = 0)
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, lineno, name, [], 0
0
+ 0, 0, 1, 0x40, fname, 1, name, [], 0
0
     @consts = [-1, nil]
0
     @symbols = [:Kernel]
0
- @bytecode, @varsyms, @labels, @lines, @jumps = [], [], {}, [], {}
0
+ @bytecode, @varsyms, @labels, @jumps = [], [], {}, {}
0
   end
0
 
0
   def add_const(obj)
0
@@ -43,11 +43,6 @@
0
   end
0
 
0
   def pop_top; bc 0x01; dump_stack end
0
- def binary_add
0
- add = bc 0x17
0
- @stack.pop
0
- add
0
- end
0
   def ret_val; bc 0x53 end
0
   def build_class; bc 0x59 end
0
   def store_name(name)
0
@@ -91,7 +86,6 @@
0
     stack_push Object.new, bc(0x7c, n, 0x0)
0
   end
0
   def store_fast(n)
0
- dump_stack
0
     bc 0x7d, n, 0x0
0
   end
0
   def call_func(arity)
0
0
@@ -101,11 +95,10 @@
0
   def make_func(arity)
0
     bc 0x84, arity, 0x0
0
   end
0
- def line n
0
- @lines << [n, @bytecode.flatten.length]
0
+ def prep_send
0
+ @nopop -= 1 if @nopop > 0
0
   end
0
   def label l
0
- @nopop -= 1 if @nopop > 0
0
     @labels[l] = @bytecode.flatten.length
0
   end
0
 
0
@@ -163,9 +156,6 @@
0
   def newarray size
0
     build_list size
0
   end
0
- def opt_plus
0
- binary_add
0
- end
0
   def putnil
0
     load_const(nil)
0
   end
0
@@ -214,7 +204,7 @@
0
     @nopop = 2
0
   end
0
   def pop
0
- pop_top # unless @nopop > 0
0
+ pop_top unless @nopop > 0
0
   end
0
   def opt_eq arg
0
     compare_op :==
0
@@ -237,7 +227,7 @@
0
     bytes = @bytecode.slice! idx..-1
0
     bytes.shift unless receiver
0
 
0
- asm = Pyasm.new(@filename, id.to_s, @lines.last[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
0
0
@@ -278,12 +268,13 @@
0
     iseq.last.each do |inst|
0
       case inst
0
       when Integer # line no
0
- line inst
0
+ nil
0
       when Symbol
0
- label inst
0
+ self.label inst
0
       when Array
0
         # p inst
0
         inst[0] = :message if inst[0] == :send
0
+ self.prep_send
0
         self.send *inst
0
       end
0
     end
0
@@ -345,14 +336,7 @@
0
     f << @filename.to_pickle
0
     f << @name.to_pickle
0
     f << 1.to_plong
0
-
0
- lnotab = ""
0
- lastn, lastpos = @lines[0]
0
- @lines[1..-1].each do |n, pos|
0
- lnotab << [pos - lastpos, n - lastn].pack("cc")
0
- lastn, lastpos = n, pos
0
- end
0
- f << lnotab.to_pickle
0
+ f << "".to_pickle
0
     f
0
   end
0
 
...
1
2
3
4
5
6
...
1
2
3
 
 
 
0
@@ -1,7 +1,4 @@
0
 def puts(*args):
0
   for x in args: print x
0
   if not args: print
0
-
0
-class BasicObject:
0
- pass

Comments

    No one has commented yet.