Skip to content

Commit 855846c

Browse files
author
Evan Phoenix
committed
Imported Evan's Simple Assembler.
1 parent 78cb4dd commit 855846c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+66117
-0
lines changed

vm/assembler/Makefile

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
INCLUDES=-I. -Iudis86-1.7
2+
3+
default: test
4+
5+
libudis86.a:
6+
(cd udis86-1.7; ./configure && make && cp libudis86/.libs/libudis86.a ../)
7+
8+
example/codegen: example/codegen.o libudis86.a
9+
g++ -ggdb3 -Wall $(INCLUDES) -o example/codegen libudis86.a example/codegen.o
10+
11+
example/codegen.o: codegen.cpp assembler_x86.hpp
12+
g++ -ggdb3 $(INCLUDES) -o example/codegen.o -c example/codegen.cpp
13+
14+
example/vm.o: example/vm.cpp assembler_x86.hpp
15+
g++ -ggdb3 -O2 $(INCLUDES) -o example/vm.o -c example/vm.cpp
16+
17+
example/vm: example/vm.o example/vm-main.cpp
18+
g++ -ggdb3 -O2 $(INCLUDES) -o example/vm-main.o -c example/vm-main.cpp
19+
g++ -ggdb3 -O2 -Wall $(INCLUDES) -o example/vm libudis86.a example/vm.o example/vm-main.o
20+
21+
test/test32: test/test32.o
22+
g++ -ggdb3 -Wall $(INCLUDES) -o test/test32 libudis86.a test/test32.o
23+
24+
test/test32.o: test/test32.cpp assembler_x86.hpp
25+
g++ -ggdb3 $(INCLUDES) -o test/test32.o -c test/test32.cpp
26+
27+
test/test64: test/test64.o
28+
g++ -ggdb3 -Wall $(INCLUDES) -o test/test64 libudis86.a test/test64.o
29+
30+
test/test64.o: test/test64.cpp assembler_x8664.hpp
31+
g++ -ggdb3 $(INCLUDES) -o test/test64.o -c test/test64.cpp
32+
33+
test: test/test32 test/test64
34+
@echo "---- 32 bit" && ./test/test32 && echo "---- 64 bit" && ./test/test64
35+
36+
clean:
37+
rm -f test/*.o test/test32 test/test64 example/*.o example/codegen example/vm
38+
39+
distclean: clean
40+
rm -f libudis86.a
41+
cd udis86-1.7; make distclean || true
42+
43+
.PHONY: test

vm/assembler/assembler.hpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <stdint.h>
2+
3+
namespace assembler {
4+
class Assembler {
5+
protected:
6+
uint8_t* buffer_;
7+
uint8_t* pc_;
8+
9+
void emit(uint8_t val) {
10+
*reinterpret_cast<uint8_t*>(pc_) = val;
11+
pc_ += 1;
12+
}
13+
14+
void emit_s(uint16_t val) {
15+
*reinterpret_cast<uint16_t*>(pc_) = val;
16+
pc_ += 2;
17+
}
18+
19+
void emit_w(uint32_t val) {
20+
*reinterpret_cast<uint32_t*>(pc_) = val;
21+
pc_ += 4;
22+
}
23+
24+
void emit_at_w(uint8_t* pc, uint32_t val) {
25+
*reinterpret_cast<uint32_t*>(pc) = val;
26+
}
27+
28+
void emit_dw(uint64_t val) {
29+
*reinterpret_cast<uint64_t*>(pc_) = val;
30+
pc_ += 8;
31+
}
32+
33+
public:
34+
Assembler() {
35+
buffer_ = new uint8_t[1024];
36+
pc_ = buffer_;
37+
}
38+
39+
~Assembler() {
40+
delete buffer_;
41+
}
42+
43+
void* buffer() {
44+
return (void*)buffer_;
45+
}
46+
47+
void* pc() {
48+
return (void*)pc_;
49+
}
50+
};
51+
}

0 commit comments

Comments
 (0)