haxor 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +27 -0
- data/README.md +414 -0
- data/Rakefile +1 -0
- data/bin/hcc +21 -0
- data/bin/hld +38 -0
- data/bin/hvm +17 -0
- data/examples/build.sh +10 -0
- data/examples/guess-the-number.hax +100 -0
- data/haxor.gemspec +24 -0
- data/lib/haxor.rb +44 -0
- data/lib/haxor/compiler/component/arithmetic.rb +55 -0
- data/lib/haxor/compiler/component/base.rb +37 -0
- data/lib/haxor/compiler/component/data.rb +32 -0
- data/lib/haxor/compiler/component/jumps.rb +95 -0
- data/lib/haxor/compiler/component/logical.rb +43 -0
- data/lib/haxor/compiler/component/other.rb +21 -0
- data/lib/haxor/compiler/component/transfer.rb +29 -0
- data/lib/haxor/compiler/component/various.rb +33 -0
- data/lib/haxor/compiler/core.rb +134 -0
- data/lib/haxor/compiler/section.rb +6 -0
- data/lib/haxor/compiler/unit.rb +39 -0
- data/lib/haxor/consts.rb +28 -0
- data/lib/haxor/header.rb +40 -0
- data/lib/haxor/linker.rb +89 -0
- data/lib/haxor/token/base.rb +7 -0
- data/lib/haxor/token/cmd.rb +9 -0
- data/lib/haxor/token/data.rb +17 -0
- data/lib/haxor/token/int64.rb +17 -0
- data/lib/haxor/token/label.rb +23 -0
- data/lib/haxor/token/pointer.rb +13 -0
- data/lib/haxor/vm/core.rb +53 -0
- data/lib/haxor/vm/cpu/core.rb +129 -0
- data/lib/haxor/vm/cpu/unit/arithmetic.rb +92 -0
- data/lib/haxor/vm/cpu/unit/base.rb +46 -0
- data/lib/haxor/vm/cpu/unit/jumps.rb +123 -0
- data/lib/haxor/vm/cpu/unit/logical.rb +59 -0
- data/lib/haxor/vm/cpu/unit/transfer.rb +37 -0
- data/lib/haxor/vm/cpu/unit/various.rb +47 -0
- data/lib/haxor/vm/mem.rb +101 -0
- data/lib/haxor/vm/os.rb +115 -0
- data/lib/haxor/vm/stack.rb +31 -0
- data/lib/haxor/vm/subsystem.rb +16 -0
- data/media/memory.png +0 -0
- data/media/vm.png +0 -0
- metadata +122 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 86dcbbd321e0c9f81e94620624119b709967430b
|
4
|
+
data.tar.gz: b6aa04aa13522e6de177628276de6f4503913d29
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ffa365bb3a8118366f4179e77f10b4723a40c44f7ef2751142336412ada1711bd848958b641740b94f1278b61b18c592bdbde8fbba58bb7649fa736b1b92d887
|
7
|
+
data.tar.gz: 5eaeef6044009fe4709d2b85d7cf7a587010233e13106ad640f6c0a0311bd53974bf96ceb68ce5fe79ce4aab560d96e1e904019fa3b556ef3af200ec3e965416
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
Copyright (c) 2015, Krzysztof Magosa
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
5
|
+
modification, are permitted provided that the following conditions are met:
|
6
|
+
|
7
|
+
* Redistributions of source code must retain the above copyright notice, this
|
8
|
+
list of conditions and the following disclaimer.
|
9
|
+
|
10
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
11
|
+
this list of conditions and the following disclaimer in the documentation
|
12
|
+
and/or other materials provided with the distribution.
|
13
|
+
|
14
|
+
* Neither the name of 'Haxor' nor the names of its
|
15
|
+
contributors may be used to endorse or promote products derived from
|
16
|
+
this software without specific prior written permission.
|
17
|
+
|
18
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
19
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
20
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
21
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
22
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
23
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
24
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
25
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
26
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
27
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,414 @@
|
|
1
|
+
# Haxor VM
|
2
|
+
Haxor consists of compiler _hcc_, linker _hld_ and virtual machine _hvm_.
|
3
|
+
_hcc_ translates asm-like code into tokens, _hld_ links them into bytecode, while _hvm_ runs it.
|
4
|
+
|
5
|
+
## Man, why have you written that?
|
6
|
+
Writing own implementation of VM gives a lot of knowledge about
|
7
|
+
computer's architecture. You hit into issues not known during day
|
8
|
+
by day activity in high level languages you use. So... just to
|
9
|
+
broaden horizons and for fun ;)
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
Compilation:
|
13
|
+
```
|
14
|
+
hcc program.hax
|
15
|
+
```
|
16
|
+
|
17
|
+
Linking:
|
18
|
+
```
|
19
|
+
hld -o program.hax.e program.hax.u
|
20
|
+
hld -s 4096 -o program.hax.e program.hax.u # custom stack size
|
21
|
+
```
|
22
|
+
|
23
|
+
Run:
|
24
|
+
```
|
25
|
+
hvm program.hax.e
|
26
|
+
```
|
27
|
+
|
28
|
+
## License
|
29
|
+
Haxor is licensed under BSD 3-clause license. You can read it [here](LICENSE.txt).
|
30
|
+
|
31
|
+
## Architecture
|
32
|
+
|
33
|
+
<img src="media/vm.png" width="50%">
|
34
|
+
|
35
|
+
### General information
|
36
|
+
* Little Endian
|
37
|
+
* Size of WORD is 64 bit
|
38
|
+
* All registers are 64 bit
|
39
|
+
* All numbers are signed
|
40
|
+
* Memory works in flat model
|
41
|
+
* Only integer arithmetic is supported
|
42
|
+
* All memory cells are writable
|
43
|
+
|
44
|
+
### OpCodes
|
45
|
+
OpCode is 64 bit integer. 0-7 bits are used to designate command, 8-63 are used to specify flags.
|
46
|
+
Command can take 0, 1 or 2 operands. All operands are 64 bit addresses in memory. Const values
|
47
|
+
are pushed into _.data_ section by compiler and pointed by automatically generated labels.
|
48
|
+
|
49
|
+
```
|
50
|
+
[opcode (command + flags)] [operand1] [operand2]
|
51
|
+
```
|
52
|
+
|
53
|
+
### vCPU
|
54
|
+
vCPU registers are mapped into lowest memory addresses between 0 and 1024 bytes.
|
55
|
+
* _ip_ - instruction pointer
|
56
|
+
* _sp_ - stack pointer
|
57
|
+
* _bp_ - base pointer
|
58
|
+
* _ar_ - arithmetic register I
|
59
|
+
* _dr_ - arithmetic register II
|
60
|
+
* _fr_ - flags register
|
61
|
+
* _sc_ - syscall register
|
62
|
+
* _op_ - currently processed opcode
|
63
|
+
* _r01-10_ - general usage registres
|
64
|
+
|
65
|
+
### Flags register
|
66
|
+
Flags register is 64 bit.
|
67
|
+
* bit 0 - ZERO (`A-B=0`, so numbers are equal)
|
68
|
+
* bit 1 - SIGN (result is negative)
|
69
|
+
* rest is reserved for future use
|
70
|
+
|
71
|
+
## Memory map
|
72
|
+
<img src="media/memory.png" width="50%">
|
73
|
+
|
74
|
+
## Language
|
75
|
+
Haxor uses primitive asm-like syntax. Each command goes into separate line.
|
76
|
+
You can add comments in code, but they also need to be separate lines, beginning
|
77
|
+
from _#_ or _rem_.
|
78
|
+
For two-argument commands destination goes into first argument while source into second one.
|
79
|
+
In some commands you can dereference value by enclosing it in brackets (e.g. `[sp]`).
|
80
|
+
Program starts from _main_ label.
|
81
|
+
|
82
|
+
```
|
83
|
+
command A, B
|
84
|
+
```
|
85
|
+
|
86
|
+
## Instructions
|
87
|
+
### Arithmetic
|
88
|
+
#### add
|
89
|
+
Sums _A_ and _B_, result goes to _A_.
|
90
|
+
_A_ or/and _B_ can be dereferenced.
|
91
|
+
OpCode: 0x01.
|
92
|
+
```
|
93
|
+
add A, B
|
94
|
+
```
|
95
|
+
|
96
|
+
#### sub
|
97
|
+
Subtracts _B_ from _A_, result goes to _A_.
|
98
|
+
_A_ or/and _B_ can be dereferenced.
|
99
|
+
OpCode: 0x02.
|
100
|
+
```
|
101
|
+
sub A, B
|
102
|
+
```
|
103
|
+
|
104
|
+
#### div
|
105
|
+
Divides `ar` register by _A_. Result goes to register. Remainder goes to `dr` register.
|
106
|
+
_A_ can be dereferenced.
|
107
|
+
OpCode: 0x03.
|
108
|
+
```
|
109
|
+
div A
|
110
|
+
```
|
111
|
+
|
112
|
+
#### mul
|
113
|
+
Multiplies `ar` register by _A_. Result goes to register.
|
114
|
+
_A_ can be dereferenced.
|
115
|
+
OpCode: 0x04.
|
116
|
+
```
|
117
|
+
mul A
|
118
|
+
```
|
119
|
+
|
120
|
+
#### inc
|
121
|
+
Increments _A_ by 1.
|
122
|
+
_A_ can be dereferenced.
|
123
|
+
OpCode: 0x05.
|
124
|
+
```
|
125
|
+
inc A
|
126
|
+
```
|
127
|
+
|
128
|
+
#### dec
|
129
|
+
Decrements _A_ by 1.
|
130
|
+
_A_ can be dereferenced.
|
131
|
+
OpCode: 0x06.
|
132
|
+
```
|
133
|
+
dec A
|
134
|
+
```
|
135
|
+
|
136
|
+
#### cmp
|
137
|
+
Compares _A_ with _B_ by subtracting _B_ from _A_ and setting flags register bits.
|
138
|
+
_A_ or/and _B_ can be dereferenced.
|
139
|
+
OpCode: 0x07.
|
140
|
+
```
|
141
|
+
cmp A, B
|
142
|
+
```
|
143
|
+
|
144
|
+
### Logical
|
145
|
+
#### and
|
146
|
+
Performs bitwise AND operation.
|
147
|
+
_A_ or/and _B_ can be dereferenced.
|
148
|
+
OpCode: 0x40.
|
149
|
+
```
|
150
|
+
and A, B
|
151
|
+
```
|
152
|
+
|
153
|
+
#### neg
|
154
|
+
Reverses the sign of number _A_.
|
155
|
+
_A_ can be dereferenced.
|
156
|
+
OpCode: 0x41.
|
157
|
+
```
|
158
|
+
neg A
|
159
|
+
```
|
160
|
+
|
161
|
+
#### not
|
162
|
+
Performs bitwise NOT operation.
|
163
|
+
_A_ can be dereferenced.
|
164
|
+
OpCode: 0x42.
|
165
|
+
```
|
166
|
+
not A
|
167
|
+
```
|
168
|
+
|
169
|
+
#### or
|
170
|
+
Performs bitwise OR operation.
|
171
|
+
_A_ or/and _B_ can be dereferenced.
|
172
|
+
OpCode: 0x43.
|
173
|
+
```
|
174
|
+
or A, B
|
175
|
+
```
|
176
|
+
|
177
|
+
#### xor
|
178
|
+
Performs bitwise XOR operation.
|
179
|
+
_A_ or/and _B_ can be dereferenced.
|
180
|
+
OpCode: 0x44.
|
181
|
+
```
|
182
|
+
xor A, B
|
183
|
+
```
|
184
|
+
|
185
|
+
### Transfer
|
186
|
+
#### mov
|
187
|
+
Moves data from _B_ to _A_.
|
188
|
+
_A_ or/and _B_ can be dereferenced.
|
189
|
+
OpCode: 0x60.
|
190
|
+
```
|
191
|
+
mov A, B
|
192
|
+
```
|
193
|
+
|
194
|
+
#### push
|
195
|
+
Places _A_ on top of stack.
|
196
|
+
OpCode: 0x61.
|
197
|
+
```
|
198
|
+
push A
|
199
|
+
```
|
200
|
+
|
201
|
+
#### pop
|
202
|
+
Removes element from top of the stack and into _A_.
|
203
|
+
OpCode: 0x62.
|
204
|
+
```
|
205
|
+
pop A
|
206
|
+
```
|
207
|
+
|
208
|
+
### Jumps
|
209
|
+
#### call
|
210
|
+
Places _ip_ register on the stack and jumps to _A_.
|
211
|
+
_A_ can be dereferenced.
|
212
|
+
OpCode: 0x20.
|
213
|
+
```
|
214
|
+
call A
|
215
|
+
```
|
216
|
+
|
217
|
+
#### ret
|
218
|
+
Pops _ip_ from the stack, and jumps to it.
|
219
|
+
OpCode: 0x2c.
|
220
|
+
```
|
221
|
+
ret
|
222
|
+
```
|
223
|
+
|
224
|
+
### iret
|
225
|
+
Comes back from interrupt.
|
226
|
+
OpCode: 0x2d.
|
227
|
+
```
|
228
|
+
iret
|
229
|
+
```
|
230
|
+
|
231
|
+
#### jmp
|
232
|
+
Performs unconditional jump to _A_.
|
233
|
+
_A_ can be dereferenced.
|
234
|
+
OpCode: 0x21.
|
235
|
+
```
|
236
|
+
jmp A
|
237
|
+
```
|
238
|
+
|
239
|
+
#### je
|
240
|
+
Jumps to _A_ if in _cmp_ _A_ is equal to _B_.
|
241
|
+
_A_ can be dereferenced.
|
242
|
+
OpCode: 0x22.
|
243
|
+
```
|
244
|
+
je A
|
245
|
+
```
|
246
|
+
|
247
|
+
#### jg
|
248
|
+
Jumps to _A_ if in _cmp_ _A_ is greater than _B_.
|
249
|
+
_A_ can be dereferenced.
|
250
|
+
OpCode: 0x23.
|
251
|
+
```
|
252
|
+
jg A
|
253
|
+
```
|
254
|
+
|
255
|
+
#### jge
|
256
|
+
Jumps to _A_ if in _cmp_ _A_ is greater or equal to _B_.
|
257
|
+
_A_ can be dereferenced.
|
258
|
+
OpCode: 0x24.
|
259
|
+
```
|
260
|
+
jge A
|
261
|
+
```
|
262
|
+
|
263
|
+
#### jl
|
264
|
+
Jumps to _A_ if in _cmp_ _A_ is less than _B_.
|
265
|
+
_A_ can be dereferenced.
|
266
|
+
OpCode: 0x25.
|
267
|
+
```
|
268
|
+
jl A
|
269
|
+
```
|
270
|
+
|
271
|
+
#### jle
|
272
|
+
Jumps to _A_ if in _cmp_ _A_ is less or equal to _B_.
|
273
|
+
_A_ can be dereferenced.
|
274
|
+
OpCode: 0x26.
|
275
|
+
```
|
276
|
+
jle A
|
277
|
+
```
|
278
|
+
|
279
|
+
#### jne
|
280
|
+
Jumps to _A_ if in _cmp_ _A_ is not equal to _B_.
|
281
|
+
_A_ can be dereferenced.
|
282
|
+
OpCode: 0x27.
|
283
|
+
```
|
284
|
+
jne A
|
285
|
+
```
|
286
|
+
|
287
|
+
#### jng
|
288
|
+
Jumps to _A_ if in _cmp_ _A_ is not greater than _B_.
|
289
|
+
_A_ can be dereferenced.
|
290
|
+
OpCode: 0x28.
|
291
|
+
```
|
292
|
+
jng A
|
293
|
+
```
|
294
|
+
|
295
|
+
#### jnge
|
296
|
+
Jumps to _A_ if in _cmp_ _A_ is not greater or equal to _B_.
|
297
|
+
_A_ can be dereferenced.
|
298
|
+
OpCode: 0x29.
|
299
|
+
```
|
300
|
+
jnge A
|
301
|
+
```
|
302
|
+
|
303
|
+
#### jnl
|
304
|
+
Jumps to _A_ if in _cmp_ _A_ is not less than _B_.
|
305
|
+
_A_ can be dereferenced.
|
306
|
+
OpCode: 0x2a.
|
307
|
+
```
|
308
|
+
jnl A
|
309
|
+
```
|
310
|
+
|
311
|
+
#### jnle
|
312
|
+
Jumps to _A_ if in _cmp_ _A_ is not less or equal to _B_.
|
313
|
+
_A_ can be dereferenced.
|
314
|
+
OpCode: 0x2b.
|
315
|
+
```
|
316
|
+
jnle A
|
317
|
+
```
|
318
|
+
|
319
|
+
### Various
|
320
|
+
#### lea
|
321
|
+
Pushes address of _B_ into _A_.
|
322
|
+
OpCode: 0x80.
|
323
|
+
```
|
324
|
+
lea A, B
|
325
|
+
```
|
326
|
+
|
327
|
+
#### nop
|
328
|
+
Does nothing.
|
329
|
+
OpCode: 0x81.
|
330
|
+
```
|
331
|
+
nop
|
332
|
+
```
|
333
|
+
|
334
|
+
#### int
|
335
|
+
Generate software interrupt with ID specified by _A_.
|
336
|
+
_A_ can be dereferenced.
|
337
|
+
OpCode: 0x85.
|
338
|
+
```
|
339
|
+
int A
|
340
|
+
```
|
341
|
+
|
342
|
+
#### syscall
|
343
|
+
Asks Haxor VM to do "system" call. OpCode: 0x86.
|
344
|
+
```
|
345
|
+
syscall
|
346
|
+
```
|
347
|
+
|
348
|
+
## System calls
|
349
|
+
Using _syscall_ command you can run some system calls provided by Haxor VM.
|
350
|
+
System call number is passed via _sc_ register, arguments go via stack in reversed order.
|
351
|
+
|
352
|
+
### exit (01h)
|
353
|
+
Terminates VM process with specified exit code.
|
354
|
+
Takes 1 argument:
|
355
|
+
* exit code
|
356
|
+
```
|
357
|
+
push 100
|
358
|
+
mov sc, 01h
|
359
|
+
syscall
|
360
|
+
```
|
361
|
+
|
362
|
+
### printf (02h)
|
363
|
+
Prints formatted text into file specified by descriptor.
|
364
|
+
Takes 2 or more arguments:
|
365
|
+
* file descriptor (1 for standard output, 2 for standard error)
|
366
|
+
* format string
|
367
|
+
* data depending on format string...
|
368
|
+
```
|
369
|
+
lea r01, format_text
|
370
|
+
push r01
|
371
|
+
push 1
|
372
|
+
mov sc, 02h
|
373
|
+
```
|
374
|
+
|
375
|
+
### scanf (03h)
|
376
|
+
Converts data from file specified by descriptor.
|
377
|
+
Remember that memory is not automatically
|
378
|
+
allocated by this function. You need to prepare
|
379
|
+
space before calling this function.
|
380
|
+
Use length limits to avoid buffer overflow (e.g. %100s to take up to 100 characters from string).
|
381
|
+
In case of string your buffer must have 1 element more for closing '0'.
|
382
|
+
Takes 2 or more arguments:
|
383
|
+
* file descriptor (0 for standard input)
|
384
|
+
* format string
|
385
|
+
* addresses in memory to put data into them...
|
386
|
+
```
|
387
|
+
section .data
|
388
|
+
dw scanfmt, "%100s", 0
|
389
|
+
|
390
|
+
section .bss
|
391
|
+
label name
|
392
|
+
resw 101
|
393
|
+
|
394
|
+
lea r01, name
|
395
|
+
push r01
|
396
|
+
lea r01, scanfmt
|
397
|
+
push r01
|
398
|
+
push 0
|
399
|
+
mov sc, 03h
|
400
|
+
syscall
|
401
|
+
```
|
402
|
+
|
403
|
+
### random (04h)
|
404
|
+
Generates random integer from specified range.
|
405
|
+
Arguments:
|
406
|
+
* minimum (inclusive)
|
407
|
+
* maximum (inclusive)
|
408
|
+
Generated number is pushed onto stack.
|
409
|
+
```
|
410
|
+
mov sc, 04h
|
411
|
+
push 100
|
412
|
+
push 1
|
413
|
+
syscall
|
414
|
+
```
|