haxor 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +101 -335
- data/bin/{hcc → hc} +1 -9
- data/bin/hvm +13 -8
- data/examples/build.sh +1 -1
- data/examples/guess-the-number.hax +65 -76
- data/lib/haxor.rb +2 -14
- data/lib/haxor/compiler/core.rb +249 -18
- data/lib/haxor/consts.rb +4 -17
- data/lib/haxor/linker.rb +12 -4
- data/lib/haxor/token/cmd.rb +29 -2
- data/lib/haxor/utils.rb +24 -0
- data/lib/haxor/vm/core.rb +6 -9
- data/lib/haxor/vm/cpu/core.rb +214 -83
- data/lib/haxor/vm/mem.rb +1 -36
- data/lib/haxor/vm/os.rb +7 -13
- data/lib/haxor/vm/stack.rb +4 -4
- data/media/memory.png +0 -0
- metadata +5 -19
- data/lib/haxor/compiler/component/arithmetic.rb +0 -45
- data/lib/haxor/compiler/component/base.rb +0 -41
- data/lib/haxor/compiler/component/data.rb +0 -32
- data/lib/haxor/compiler/component/jumps.rb +0 -83
- data/lib/haxor/compiler/component/logical.rb +0 -35
- data/lib/haxor/compiler/component/other.rb +0 -21
- data/lib/haxor/compiler/component/transfer.rb +0 -25
- data/lib/haxor/compiler/component/various.rb +0 -30
- data/lib/haxor/vm/cpu/unit/arithmetic.rb +0 -78
- data/lib/haxor/vm/cpu/unit/base.rb +0 -46
- data/lib/haxor/vm/cpu/unit/jumps.rb +0 -118
- data/lib/haxor/vm/cpu/unit/logical.rb +0 -59
- data/lib/haxor/vm/cpu/unit/transfer.rb +0 -37
- data/lib/haxor/vm/cpu/unit/various.rb +0 -47
- data/media/vm.png +0 -0
data/lib/haxor/vm/mem.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module Haxor
|
2
2
|
module Vm
|
3
3
|
class Mem < Subsystem
|
4
|
-
attr_accessor :memory
|
4
|
+
#attr_accessor :memory
|
5
5
|
|
6
6
|
def initialize(bytes)
|
7
7
|
@memory = Array.new(bytes, 0).pack('C*')
|
@@ -56,44 +56,9 @@ module Haxor
|
|
56
56
|
@memory[addr...x] = data
|
57
57
|
end
|
58
58
|
|
59
|
-
# (see #read)
|
60
|
-
def dereference(addr)
|
61
|
-
read addr
|
62
|
-
end
|
63
|
-
|
64
|
-
# Reads data pointed by IP register and moves IP forward
|
65
|
-
#
|
66
|
-
# @return [Integer] Data stored under address pointed by IP
|
67
|
-
def next_word
|
68
|
-
data = read ip
|
69
|
-
move_ip Consts::WORD_SIZE
|
70
|
-
data
|
71
|
-
end
|
72
|
-
|
73
|
-
# TODO: BC
|
74
|
-
def next_cell
|
75
|
-
next_word
|
76
|
-
end
|
77
|
-
|
78
|
-
def ip
|
79
|
-
read 'ip'
|
80
|
-
end
|
81
|
-
|
82
|
-
def move_ip(step)
|
83
|
-
write 'ip', ip + step
|
84
|
-
end
|
85
|
-
|
86
|
-
def label(name)
|
87
|
-
@labels[addr]
|
88
|
-
end
|
89
|
-
|
90
59
|
private
|
91
60
|
|
92
61
|
def resolve(addr)
|
93
|
-
if addr.is_a? String
|
94
|
-
fail "Unknown label #{addr}." unless @labels.key? addr
|
95
|
-
addr = @labels[addr]
|
96
|
-
end
|
97
62
|
Range.new(addr, addr + Consts::WORD_SIZE, true)
|
98
63
|
end
|
99
64
|
end
|
data/lib/haxor/vm/os.rb
CHANGED
@@ -2,14 +2,13 @@ module Haxor
|
|
2
2
|
module Vm
|
3
3
|
class Os < Subsystem
|
4
4
|
TABLE = {
|
5
|
-
0x01 => :
|
6
|
-
0x02 => :
|
7
|
-
0x03 => :
|
8
|
-
0x04 => :sys_rand
|
5
|
+
0x01 => :sys_printf,
|
6
|
+
0x02 => :sys_scanf,
|
7
|
+
0x03 => :sys_rand
|
9
8
|
}
|
10
9
|
|
11
10
|
def syscall
|
12
|
-
func = subsystem(:
|
11
|
+
func = subsystem(:cpu).reg(Vm::Cpu::Core::REG_SYSCALL)
|
13
12
|
send(TABLE[func])
|
14
13
|
end
|
15
14
|
|
@@ -50,14 +49,8 @@ module Haxor
|
|
50
49
|
types
|
51
50
|
end
|
52
51
|
|
53
|
-
def sys_exit
|
54
|
-
code = subsystem(:stack).pop_value
|
55
|
-
exit code
|
56
|
-
end
|
57
|
-
|
58
52
|
def sys_printf
|
59
53
|
fd = subsystem(:stack).pop_value
|
60
|
-
|
61
54
|
x = subsystem(:stack).pop_value
|
62
55
|
fmt = collect_string x
|
63
56
|
args = []
|
@@ -85,7 +78,7 @@ module Haxor
|
|
85
78
|
result = file.scanf fmt
|
86
79
|
|
87
80
|
if result.size != types.size
|
88
|
-
subsystem(:
|
81
|
+
subsystem(:cpu).reg Vm::Cpu::Core::REG_SYSCALL, -1
|
89
82
|
return
|
90
83
|
end
|
91
84
|
|
@@ -108,7 +101,8 @@ module Haxor
|
|
108
101
|
max = subsystem(:stack).pop_value
|
109
102
|
|
110
103
|
prng = Random.new
|
111
|
-
|
104
|
+
v = prng.rand(min..max)
|
105
|
+
subsystem(:stack).push_value v
|
112
106
|
end
|
113
107
|
end
|
114
108
|
end
|
data/lib/haxor/vm/stack.rb
CHANGED
@@ -7,9 +7,9 @@ module Haxor
|
|
7
7
|
end
|
8
8
|
|
9
9
|
def push_value(value)
|
10
|
-
sp = @vm.subsystem(:
|
10
|
+
sp = @vm.subsystem(:cpu).reg Vm::Cpu::Core::REG_STACK
|
11
11
|
sp -= Consts::WORD_SIZE
|
12
|
-
@vm.subsystem(:
|
12
|
+
@vm.subsystem(:cpu).reg Vm::Cpu::Core::REG_STACK, sp
|
13
13
|
@vm.subsystem(:mem).write sp, value
|
14
14
|
end
|
15
15
|
|
@@ -19,10 +19,10 @@ module Haxor
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def pop_value
|
22
|
-
sp = @vm.subsystem(:
|
22
|
+
sp = @vm.subsystem(:cpu).reg Vm::Cpu::Core::REG_STACK
|
23
23
|
value = @vm.subsystem(:mem).read sp
|
24
24
|
sp += Consts::WORD_SIZE
|
25
|
-
@vm.subsystem(:
|
25
|
+
@vm.subsystem(:cpu).reg Vm::Cpu::Core::REG_STACK, sp
|
26
26
|
|
27
27
|
value
|
28
28
|
end
|
data/media/memory.png
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: haxor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Krzysztof Magosa
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-01-
|
11
|
+
date: 2016-01-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -42,7 +42,7 @@ description: Please follow to GitHub repository for more information.
|
|
42
42
|
email:
|
43
43
|
- krzysztof@magosa.pl
|
44
44
|
executables:
|
45
|
-
-
|
45
|
+
- hc
|
46
46
|
- hld
|
47
47
|
- hvm
|
48
48
|
extensions: []
|
@@ -54,21 +54,13 @@ files:
|
|
54
54
|
- LICENSE.txt
|
55
55
|
- README.md
|
56
56
|
- Rakefile
|
57
|
-
- bin/
|
57
|
+
- bin/hc
|
58
58
|
- bin/hld
|
59
59
|
- bin/hvm
|
60
60
|
- examples/build.sh
|
61
61
|
- examples/guess-the-number.hax
|
62
62
|
- haxor.gemspec
|
63
63
|
- lib/haxor.rb
|
64
|
-
- lib/haxor/compiler/component/arithmetic.rb
|
65
|
-
- lib/haxor/compiler/component/base.rb
|
66
|
-
- lib/haxor/compiler/component/data.rb
|
67
|
-
- lib/haxor/compiler/component/jumps.rb
|
68
|
-
- lib/haxor/compiler/component/logical.rb
|
69
|
-
- lib/haxor/compiler/component/other.rb
|
70
|
-
- lib/haxor/compiler/component/transfer.rb
|
71
|
-
- lib/haxor/compiler/component/various.rb
|
72
64
|
- lib/haxor/compiler/core.rb
|
73
65
|
- lib/haxor/compiler/section.rb
|
74
66
|
- lib/haxor/compiler/unit.rb
|
@@ -81,21 +73,15 @@ files:
|
|
81
73
|
- lib/haxor/token/int64.rb
|
82
74
|
- lib/haxor/token/label.rb
|
83
75
|
- lib/haxor/token/pointer.rb
|
76
|
+
- lib/haxor/utils.rb
|
84
77
|
- lib/haxor/vm/core.rb
|
85
78
|
- lib/haxor/vm/cpu/core.rb
|
86
|
-
- lib/haxor/vm/cpu/unit/arithmetic.rb
|
87
|
-
- lib/haxor/vm/cpu/unit/base.rb
|
88
|
-
- lib/haxor/vm/cpu/unit/jumps.rb
|
89
|
-
- lib/haxor/vm/cpu/unit/logical.rb
|
90
|
-
- lib/haxor/vm/cpu/unit/transfer.rb
|
91
|
-
- lib/haxor/vm/cpu/unit/various.rb
|
92
79
|
- lib/haxor/vm/mem.rb
|
93
80
|
- lib/haxor/vm/os.rb
|
94
81
|
- lib/haxor/vm/registers.rb
|
95
82
|
- lib/haxor/vm/stack.rb
|
96
83
|
- lib/haxor/vm/subsystem.rb
|
97
84
|
- media/memory.png
|
98
|
-
- media/vm.png
|
99
85
|
homepage: https://github.com/krzysztof-magosa/haxor
|
100
86
|
licenses:
|
101
87
|
- BSD-3
|
@@ -1,45 +0,0 @@
|
|
1
|
-
module Haxor
|
2
|
-
module Compiler
|
3
|
-
module Component
|
4
|
-
class Arithmetic < Base
|
5
|
-
def register
|
6
|
-
bind_cmd 'inc', :cmd_inc
|
7
|
-
bind_cmd 'dec', :cmd_dec
|
8
|
-
bind_cmd 'add', :cmd_add
|
9
|
-
bind_cmd 'sub', :cmd_sub
|
10
|
-
bind_cmd 'div', :cmd_div
|
11
|
-
bind_cmd 'mul', :cmd_mul
|
12
|
-
bind_cmd 'cmp', :cmd_cmp
|
13
|
-
end
|
14
|
-
|
15
|
-
def cmd_add(a, b)
|
16
|
-
add_cmd Vm::Cpu::Unit::Arithmetic::OP_ADD, a, b
|
17
|
-
end
|
18
|
-
|
19
|
-
def cmd_sub(a, b)
|
20
|
-
add_cmd Vm::Cpu::Unit::Arithmetic::OP_SUB, a, b
|
21
|
-
end
|
22
|
-
|
23
|
-
def cmd_div(a)
|
24
|
-
add_cmd Vm::Cpu::Unit::Arithmetic::OP_DIV, a
|
25
|
-
end
|
26
|
-
|
27
|
-
def cmd_mul(a)
|
28
|
-
add_cmd Vm::Cpu::Unit::Arithmetic::OP_MUL, a
|
29
|
-
end
|
30
|
-
|
31
|
-
def cmd_cmp(a, b)
|
32
|
-
add_cmd Vm::Cpu::Unit::Arithmetic::OP_CMP, a, b
|
33
|
-
end
|
34
|
-
|
35
|
-
def cmd_inc(a)
|
36
|
-
add_cmd Vm::Cpu::Unit::Arithmetic::OP_INC, a
|
37
|
-
end
|
38
|
-
|
39
|
-
def cmd_dec(a)
|
40
|
-
add_cmd Vm::Cpu::Unit::Arithmetic::OP_DEC, a
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
@@ -1,41 +0,0 @@
|
|
1
|
-
module Haxor
|
2
|
-
module Compiler
|
3
|
-
module Component
|
4
|
-
class Base
|
5
|
-
attr_accessor :compiler
|
6
|
-
|
7
|
-
def register
|
8
|
-
fail 'this method must be implemented.'
|
9
|
-
end
|
10
|
-
|
11
|
-
def bind_cmd(cmd, method)
|
12
|
-
compiler.bind_cmd cmd, self, method
|
13
|
-
end
|
14
|
-
|
15
|
-
def add(token)
|
16
|
-
compiler.add token
|
17
|
-
end
|
18
|
-
|
19
|
-
def add_cmd(opcode, a = nil, b = nil)
|
20
|
-
compiler.add_cmd opcode, a, b
|
21
|
-
end
|
22
|
-
|
23
|
-
def parse_value(value)
|
24
|
-
compiler.parse_value value
|
25
|
-
end
|
26
|
-
|
27
|
-
def offset_flags(a, b = nil)
|
28
|
-
compiler.offset_flags(a, b)
|
29
|
-
end
|
30
|
-
|
31
|
-
def offset?(value)
|
32
|
-
compiler.offset? value
|
33
|
-
end
|
34
|
-
|
35
|
-
def strip_offset(value)
|
36
|
-
compiler.strip_offset value
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
@@ -1,32 +0,0 @@
|
|
1
|
-
module Haxor
|
2
|
-
module Compiler
|
3
|
-
module Component
|
4
|
-
class Data < Base
|
5
|
-
def register
|
6
|
-
bind_cmd 'dw', :cmd_dw
|
7
|
-
bind_cmd 'resw', :cmd_resw
|
8
|
-
end
|
9
|
-
|
10
|
-
def cmd_dw(*args)
|
11
|
-
add Token::Label.new(args[0])
|
12
|
-
|
13
|
-
(1...args.size).each do |i|
|
14
|
-
begin
|
15
|
-
add Token::Int64.new(Integer(args[i]))
|
16
|
-
rescue
|
17
|
-
args[i][1...-1].each_char do |c|
|
18
|
-
add Token::Int64.new(c.ord)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
def cmd_resw(*args)
|
25
|
-
(1..args[0].to_i).each do
|
26
|
-
add Token::Int64.new(0)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
@@ -1,83 +0,0 @@
|
|
1
|
-
module Haxor
|
2
|
-
module Compiler
|
3
|
-
module Component
|
4
|
-
class Jumps < Base
|
5
|
-
def register
|
6
|
-
bind_cmd 'call', :cmd_call
|
7
|
-
bind_cmd 'ret', :cmd_ret
|
8
|
-
bind_cmd 'iret', :cmd_iret
|
9
|
-
|
10
|
-
bind_cmd 'jmp', :cmd_jmp
|
11
|
-
|
12
|
-
bind_cmd 'je', :cmd_je
|
13
|
-
bind_cmd 'jg', :cmd_jg
|
14
|
-
bind_cmd 'jge', :cmd_jge
|
15
|
-
bind_cmd 'jl', :cmd_jl
|
16
|
-
bind_cmd 'jle', :cmd_jle
|
17
|
-
|
18
|
-
bind_cmd 'jne', :cmd_jne
|
19
|
-
bind_cmd 'jng', :cmd_jng
|
20
|
-
bind_cmd 'jnge', :cmd_jnge
|
21
|
-
bind_cmd 'jnl', :cmd_jnl
|
22
|
-
bind_cmd 'jnle', :cmd_jnle
|
23
|
-
end
|
24
|
-
|
25
|
-
def cmd_call(a)
|
26
|
-
add_cmd Vm::Cpu::Unit::Jumps::OP_CALL, a
|
27
|
-
end
|
28
|
-
|
29
|
-
def cmd_ret(*_args)
|
30
|
-
add_cmd Vm::Cpu::Unit::Jumps::OP_RET
|
31
|
-
end
|
32
|
-
|
33
|
-
def cmd_iret(*_args)
|
34
|
-
add_cmd Vm::Cpu::Unit::Jumps::OP_IRET
|
35
|
-
end
|
36
|
-
|
37
|
-
def cmd_jmp(a)
|
38
|
-
add_cmd Vm::Cpu::Unit::Jumps::OP_JMP, a
|
39
|
-
end
|
40
|
-
|
41
|
-
def cmd_je(a)
|
42
|
-
add_cmd Vm::Cpu::Unit::Jumps::OP_JE, a
|
43
|
-
end
|
44
|
-
|
45
|
-
def cmd_jg(a)
|
46
|
-
add_cmd Vm::Cpu::Unit::Jumps::OP_JG, a
|
47
|
-
end
|
48
|
-
|
49
|
-
def cmd_jge(a)
|
50
|
-
add_cmd Vm::Cpu::Unit::Jumps::OP_JGE, a
|
51
|
-
end
|
52
|
-
|
53
|
-
def cmd_jl(a)
|
54
|
-
add_cmd Vm::Cpu::Unit::Jumps::OP_JL, a
|
55
|
-
end
|
56
|
-
|
57
|
-
def cmd_jle(a)
|
58
|
-
add_cmd Vm::Cpu::Unit::Jumps::OP_JLE, a
|
59
|
-
end
|
60
|
-
|
61
|
-
def cmd_jne(a)
|
62
|
-
add_cmd Vm::Cpu::Unit::Jumps::OP_JNE, a
|
63
|
-
end
|
64
|
-
|
65
|
-
def cmd_jng(a)
|
66
|
-
add_cmd Vm::Cpu::Unit::Jumps::OP_JNG, a
|
67
|
-
end
|
68
|
-
|
69
|
-
def cmd_jnge(a)
|
70
|
-
add_cmd Vm::Cpu::Unit::Jumps::OP_JNGE, a
|
71
|
-
end
|
72
|
-
|
73
|
-
def cmd_jnl(a)
|
74
|
-
add_cmd Vm::Cpu::Unit::Jumps::OP_JNL, a
|
75
|
-
end
|
76
|
-
|
77
|
-
def cmd_jnle(a)
|
78
|
-
add_cmd Vm::Cpu::Unit::Jumps::OP_JNLE, a
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
@@ -1,35 +0,0 @@
|
|
1
|
-
module Haxor
|
2
|
-
module Compiler
|
3
|
-
module Component
|
4
|
-
class Logical < Base
|
5
|
-
def register
|
6
|
-
bind_cmd 'and', :cmd_and
|
7
|
-
bind_cmd 'neg', :cmd_neg
|
8
|
-
bind_cmd 'not', :cmd_not
|
9
|
-
bind_cmd 'or', :cmd_or
|
10
|
-
bind_cmd 'xor', :cmd_xor
|
11
|
-
end
|
12
|
-
|
13
|
-
def cmd_and(a, b)
|
14
|
-
add_cmd Vm::Cpu::Unit::Logical::OP_AND, a, b
|
15
|
-
end
|
16
|
-
|
17
|
-
def cmd_neg(a)
|
18
|
-
add_cmd Vm::Cpu::Unit::Logical::OP_NEG, a
|
19
|
-
end
|
20
|
-
|
21
|
-
def cmd_not(a)
|
22
|
-
add_cmd Vm::Cpu::Unit::Logical::OP_NOT, a
|
23
|
-
end
|
24
|
-
|
25
|
-
def cmd_or(a, b)
|
26
|
-
add_cmd Vm::Cpu::Unit::Logical::OP_OR, a, b
|
27
|
-
end
|
28
|
-
|
29
|
-
def cmd_xor(a, b)
|
30
|
-
add_cmd Vm::Cpu::Unit::Logical::OP_XOR, a, b
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
module Haxor
|
2
|
-
module Compiler
|
3
|
-
module Component
|
4
|
-
class Other < Base
|
5
|
-
def register
|
6
|
-
bind_cmd 'label', :cmd_label
|
7
|
-
bind_cmd 'rem', :cmd_rem
|
8
|
-
bind_cmd '#', :cmd_rem
|
9
|
-
end
|
10
|
-
|
11
|
-
def cmd_rem(*_args)
|
12
|
-
# nothing
|
13
|
-
end
|
14
|
-
|
15
|
-
def cmd_label(*args)
|
16
|
-
add Token::Label.new(args[0])
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|