-
Notifications
You must be signed in to change notification settings - Fork 0
/
VMc.cxx
48 lines (40 loc) · 1.25 KB
/
VMc.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//=============================================================================
// ■ VMc.cxx
//-----------------------------------------------------------------------------
// VM编译器,编译到字节码。
// 扩展名用了.cxx,意思是“× 不要自动编译”。
//=============================================================================
#include "ASM76.hpp"
using namespace ASM76;
int main(int argc, char** argv) {
if (argc > 2) {
if (strcmp(argv[1], "-d") == 0) {
printf("Disassembling %s\n", argv[2]);
Disassembler d(ObjectCode::read_file(argv[2]));
char* s2 = d.disassemble();
puts(s2);
}
} else if (argc > 1) {
char* src_file = argv[1];
char* src = NULL;
size_t file_size = slurp(src_file, &src);
if (file_size < 0) {
perror("Src read failed");
return 1;
}
printf("Compiling %s, src file size %ld\n", src_file, file_size);
Assembler a(src);
a.scan();
Program p = a.assemble();
free(src);
char* obj_file = new char[strlen(src_file) + 5]; // .obj + NULL
sprintf(obj_file, "%s.obj", src_file);
ObjectCode::write_file(obj_file, p);
delete[] obj_file;
free(p.instruct);
} else {
puts("VM compiler. VM/76 with BIOS version 'Still in Alpha'");
puts("Usage: $./VMc [src]");
}
return 0;
}