public
Description: a ruby-to-pyc compiler
Clone URL: git://github.com/why/unholy.git
Search Repo:
 * lib/unholy/pyasm.rb: got simple classes working.  added opcodes 
 load_locals and putobject.
why (author)
Thu May 08 07:22:16 -0700 2008
commit  aa1939a1878fbe48fb9b743a39e8a42da2f83d02
tree    4ba1dbb7c6b8f954b2e50e5322a14d7036fb95ab
parent  eab2a68059233e78ff25e8271e85988e6015fa61
...
1
 
 
 
2
3
4
5
6
7
8
9
10
 
 
 
 
11
12
13
...
48
49
50
 
51
52
53
...
159
160
161
162
 
 
 
 
 
163
164
165
...
175
176
177
 
 
 
178
179
180
181
...
239
240
241
242
 
 
 
 
 
243
244
245
246
 
247
248
249
...
284
285
286
287
288
289
290
...
1
2
3
4
5
6
7
8
9
10
 
 
 
11
12
13
14
15
16
17
...
52
53
54
55
56
57
58
...
164
165
166
 
167
168
169
170
171
172
173
174
...
184
185
186
187
188
189
190
191
192
193
...
251
252
253
 
254
255
256
257
258
259
260
261
 
262
263
264
265
...
300
301
302
 
303
304
305
0
@@ -1,13 +1,17 @@
0
 class Pyasm
0
+ CO_NEWLOCALS = 0x02
0
+ CO_NOFREE = 0x40
0
+
0
   OPS = {
0
     :== => 2
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
- @argc, @nlocals, @stacksize, @flags, @filename, @lineno, @name, @stack, @nopop =
0
- 0, 0, 1, 0x40, fname, lineno, name, [], 0
0
+ def initialize(fname, type = nil, name = "<module>", lineno = 0)
0
+ @argc, @nlocals, @stacksize, @flags, @filename, @lineno, @name, @stack, @nopop, @type =
0
+ 0, 0, 1, CO_NOFREE, fname, lineno, name, [], 0, type
0
+ @flags |= CO_NEWLOCALS if type == :class
0
     @consts = [-1, nil]
0
     @symbols = [:Kernel]
0
     @bytecode, @varsyms, @labels, @lines, @jumps = [], [], {}, [], {}
0
@@ -48,6 +52,7 @@
0
     @stack.pop
0
     add
0
   end
0
+ def load_locals; bc 0x52 end
0
   def ret_val; bc 0x53 end
0
   def build_class; bc 0x59 end
0
   def store_name(name)
0
@@ -159,7 +164,11 @@
0
   end
0
 
0
   def leave
0
- load_const(nil) if @stack.empty?
0
+ if @type == :class
0
+ load_locals
0
+ else
0
+ load_const(nil) if @stack.empty?
0
+ end
0
     ret_val
0
   end
0
   def newarray size
0
@@ -175,6 +184,9 @@
0
     str = @filename if str == "<compiled>"
0
     load_const(str)
0
   end
0
+ def putobject(obj)
0
+ load_const(obj)
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
@@ -239,11 +251,15 @@
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, type, id.to_s, @lines.last[0])
0
+ if type == :class
0
+ asm.load_name(:__name__)
0
+ asm.store_name(:__module__)
0
+ end
0
     asm.load_iseq iseq
0
     if type == :class
0
       load_const(id.to_s)
0
- if bytes.empty??
0
+ if bytes.empty? or @consts[bytes[0][1]].nil?
0
         load_const(tuple())
0
       else
0
         @bytecode += bytes
0
@@ -284,7 +300,6 @@
0
       when Symbol
0
         label inst
0
       when Array
0
- # p inst
0
         inst[0] = :message if inst[0] == :send
0
         self.prep_send
0
         self.send *inst

Comments

    No one has commented yet.