asmrb 0.0.2.6 → 0.0.2.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/asmrb.rb +88 -30
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 771ddde246ce924141e25cc6c4f8d6a95342f74c
4
- data.tar.gz: 8dd350e2b9e6d536a019c553c4d229d1e9325e2b
3
+ metadata.gz: 89294081ff079041464adfb3f71fd744e635bc43
4
+ data.tar.gz: 548d68fb5bbe3d58af342f206cb7861d11f977ad
5
5
  SHA512:
6
- metadata.gz: 06a741a3c1589f1b6ffc4afc70f5596e462e0a5c3abaa84ef46ef56f814a9a3f2dd690092a38ac66ebbb4a482e426840d4e4885b5ae2b4dc4d79c81f8a4bc766
7
- data.tar.gz: c0729a4f76c4ab2835e019f59493dabce3e4080eaad0749d4ae1b802b41ca648173e81857df8a55deeff770b6db2145926df467aaf7a7cd9d2712cbd17bf2997
6
+ metadata.gz: bec94b11bd62ec137fe1c9520559632074446d792b82390b7ed82be5cf93aa34560f841614d87ea882d3b270f970ba4f3720e537c5f3c75efb03e297489931ec
7
+ data.tar.gz: ba09be2b2a7b4c3cc8437392209782598e85c4c2f68ac8315c7c517ed9856cc294273a3e466dbde6cc44216dc67786c9f982967d14be1c09a1c9b59a06a8d981
data/lib/asmrb.rb CHANGED
@@ -1,17 +1,20 @@
1
- require 'benchmark'
2
- require 'colorize'
3
1
  require 'pry'
2
+ require 'colorize'
3
+ require 'llvm/core'
4
+ require 'benchmark'
5
+ require 'llvm/execution_engine'
6
+ require 'llvm/transforms/scalar'
4
7
 
5
8
  # You can make an instruction-based function with high level ruby by this Asm class.
6
- class Asm
9
+ class Asmrb
7
10
  # Sample with recursion:
8
11
  #
9
- # Asm.new do
12
+ # Asmrb.new do
10
13
  # defn :rec_print # define a function, also a label to jump.
11
14
  # arg arr # arguments
15
+ # push 0 # get value to l variable
12
16
  # len arr # get length of arr as array
13
- # pop l # get value to l variable
14
- # cmp l , 0 # is l > 0 ?
17
+ # cmp # is l > 0 ?
15
18
  # jge :print # then jump to :print block
16
19
  # jmp :exit # else, jump to :exit block
17
20
  # label :print # start of :print
@@ -31,24 +34,26 @@ class Asm
31
34
  #
32
35
  #
33
36
 
34
- attr_reader :variables, :stack, :name, :is_debug, :result, :functions, :params
37
+ attr_reader :variables, :stack, :source, :name, :result, :params,:is_debug, :is_compiled
35
38
  attr_writer :is_debug
36
39
 
37
40
  def initialize(&block)
38
- @functions = {}
39
41
  new_scope
40
42
  assemble &block
41
43
  end
42
44
 
43
45
  def new_scope
44
46
  @variables = {}
47
+ @source = []
45
48
  @program = []
46
49
  @params = 0
50
+ @name = "it"
47
51
  @labels = {}
48
52
  @stack = []
53
+ @pc = 0
49
54
  @result = nil
50
55
  @is_debug = false
51
- @name = "it"
56
+ @is_compiled = false
52
57
  end
53
58
 
54
59
  def refvar(val)
@@ -155,7 +160,7 @@ class Asm
155
160
 
156
161
  "jmp" => lambda { |name|
157
162
  @pc = @labels[name]
158
- new_title "[#{name}]"
163
+ new_title "[#{name}]" if @is_debug
159
164
  },
160
165
 
161
166
  "pop" => lambda {|val|
@@ -219,6 +224,13 @@ class Asm
219
224
  end
220
225
  @stack.push invoke if !invoke.nil?
221
226
  },
227
+ "rec" => lambda {
228
+ @pc = -1
229
+ },
230
+
231
+ "ret" => lambda {
232
+ @pc = @program.length-1
233
+ },
222
234
 
223
235
  "exit" => lambda {
224
236
  exit
@@ -231,6 +243,9 @@ class Asm
231
243
  end
232
244
 
233
245
  def method_missing(name, *args)
246
+ # save source
247
+ @source << [name, args]
248
+
234
249
  # :name => "name"
235
250
  sname = name.to_s
236
251
 
@@ -248,7 +263,7 @@ class Asm
248
263
 
249
264
  when "defn"
250
265
  @name = args.first
251
- create_label args.first
266
+ #create_label args.first
252
267
 
253
268
  else
254
269
  return name
@@ -300,7 +315,7 @@ class Asm
300
315
  end
301
316
 
302
317
  def new_title(title)
303
- puts "\n::#{title}" if is_debug
318
+ puts "\n::#{title}"
304
319
  end
305
320
 
306
321
  def new_line
@@ -345,6 +360,7 @@ class Asm
345
360
  end
346
361
 
347
362
  def clear
363
+ @pc = 0
348
364
  @variables = { }
349
365
  @stack = []
350
366
  end
@@ -355,7 +371,7 @@ class Asm
355
371
  eval "$#{@name}"
356
372
  end
357
373
 
358
- #primitive fac: that will overflow
374
+ #primitive fac: that will overflow
359
375
  def self.fac(acc, n)
360
376
  if(n > 0)
361
377
  fac(
@@ -366,38 +382,80 @@ class Asm
366
382
  end
367
383
  end
368
384
 
369
- def self.fac2(n)
370
- if(n > 1)
371
- n * fac2(n - 1)
385
+ def llvm_init
386
+ LLVM.init_jit
387
+ @mod = LLVM::Module.new @name.to_s
388
+ puts "[LLVM]: initialized".light_green
389
+ end
390
+
391
+ def llvm_finalize
392
+ @mod.verify
393
+ @mod.dump
394
+ @engine = LLVM::JITCompiler.new(@mod)
395
+ end
396
+
397
+ def llvm_partition
398
+ puts "partitioning...".light_green if@is_debug
399
+ if @labels.empty?
400
+ @partitions = { @name => @source }
372
401
  else
373
- 1
402
+ @partitions = {}
403
+ index = 0
404
+ @labels.each do |label|
405
+ puts "block #{label[0]}:".light_yellow
406
+ partition = []
407
+ (@source.length - index).times.each do
408
+ if @source[index][0] != :label
409
+ puts " #{index}: #{@source[index]}".white if@is_debug
410
+ partition.push @source[index]
411
+ index+=1
412
+ else
413
+ index+=1
414
+ break
415
+ end
416
+ end
417
+ @partitions[label[0]] = partition.dup
418
+ end
374
419
  end
420
+ puts "[LLVM]: partitioning completed.".light_green
421
+ @partitions if@is_debug
375
422
  end
376
423
 
377
- end
424
+ def llvm_emit
425
+ # Checkout kaleidescope sample of ruby-llvm
426
+ end
378
427
 
428
+ def compile
429
+ llvm_init
430
+ llvm_partition
431
+ llvm_emit
432
+ #llvm_finalize
433
+ end
434
+
435
+ end
379
436
 
380
- Asm.new do
437
+ #testing :
438
+ Asmrb.new do
381
439
  defn :factorial
382
- arg acc, n
440
+ arg :acc, :n
383
441
  push 1
384
- push n
442
+ push :n
385
443
  cmp
386
444
  jge :recursion
387
- jmp :exit
445
+ push :acc
446
+ ret
388
447
 
389
448
  label :recursion
390
- push acc
391
- push n
449
+ push :acc
450
+ push :n
392
451
  mul
393
452
  push 1
394
- push n
453
+ push :n
395
454
  sub
396
- jmp :factorial
397
- label :exit
398
- push acc
455
+ rec
399
456
  end
400
457
 
401
- $factorial.is_debug = true
458
+ #$factorial.is_debug = true
459
+ #puts $factorial.invoke 1,3
402
460
 
403
- puts $factorial.invoke 1,3
461
+ #$factorial.compile
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: asmrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2.6
4
+ version: 0.0.2.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - The Trung