Skip to content

Commit 39f230d

Browse files
committed
Add jvmd & Fixed compile warning
1 parent bbf1c2a commit 39f230d

File tree

8 files changed

+120
-39
lines changed

8 files changed

+120
-39
lines changed

debug/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,8 @@ TARGET_LINK_LIBRARIES(jvmp libvmbase)
3939
ADD_EXECUTABLE(jar ${src_dir}/jar.c)
4040
TARGET_LINK_LIBRARIES(jar libminiz)
4141
TARGET_LINK_LIBRARIES(jar libvmbase)
42+
43+
# jvmd
44+
ADD_EXECUTABLE(jvmd ${src_dir}/jvmd.c)
45+
TARGET_LINK_LIBRARIES(jvmd libminiz)
46+
TARGET_LINK_LIBRARIES(jvmd libvmbase)

release/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,8 @@ TARGET_LINK_LIBRARIES(jvmp libvmbase)
3636
ADD_EXECUTABLE(jar ${src_dir}/jar.c)
3737
#TARGET_LINK_LIBRARIES(jar libminiz)
3838
TARGET_LINK_LIBRARIES(jar libvmbase)
39+
40+
# jvmd
41+
ADD_EXECUTABLE(jvmd ${src_dir}/jvmd.c)
42+
TARGET_LINK_LIBRARIES(jvmd libminiz)
43+
TARGET_LINK_LIBRARIES(jvmd libvmbase)

src/jar.c

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <string.h>
1414
#include "libjar.h"
1515

16+
#define JAR_VER "0.0.1"
17+
1618
static void usage() {
1719
printf("Usage\n");
1820
printf(" %s [-c f1 f2 ...] [-d] path \n", "jar");
@@ -63,29 +65,47 @@ static void on_finish(void *param) {
6365
}
6466

6567
int main(int argc, char *argv[]) {
66-
if (argc <= 2) {
68+
if (argc < 2) {
6769
usage();
6870
return -1;
6971
}
7072

73+
if (2 == argc) {
74+
if (0 == strncmp("-v", argv[1], 2)) {
75+
printf("jar %s compiled on %s [email protected].\n", JAR_VER, __DATE__);
76+
return 0;
77+
} else {
78+
usage();
79+
return -1;
80+
}
81+
}
82+
7183
if (strcmp(argv[1], "-d") == 0) {
7284
if (argc != 3) {
7385
usage();
7486
return -1;
7587
}
7688

77-
executeUnpackJar (argv[2], on_start, on_progress, NULL, on_finish, NULL);
78-
}
79-
else if (strcmp(argv[1], "-c") == 0) {
89+
executeUnpackJar (argv[2],
90+
on_start,
91+
on_progress,
92+
NULL,
93+
on_finish,
94+
NULL);
95+
96+
return 0;
97+
98+
} else if (strcmp(argv[1], "-c") == 0) {
8099
if (argc <= 3) {
81100
usage();
82101
return -1;
83102
}
84-
else {
85-
executePackJar ((const char**)&argv[2], argc-3, argv[argc-1]);
86-
}
87-
}
88-
else {
103+
104+
executePackJar ((const char**)&argv[2], argc-3, argv[argc-1]);
105+
106+
return 0;
107+
108+
} else {
89109
usage();
90110
return -1;
91111
}

src/jvm.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ int parseCmdLine(int argc, char **argv, Property **props) {
281281
}
282282
else if (argc == 2) {
283283
if (strcmp(argv[1], "-v") == 0) {
284-
printf("jvm %s [email protected]\n", JAVA_VERSION);
284+
printf("jvm %s compiled on %s [email protected]\n", JAVA_VERSION, __DATE__);
285285
exit(0);
286286
}
287287
else if (strcmp(argv[1], "-h") == 0) {
@@ -322,4 +322,6 @@ int main(int argc, char *argv[]) {
322322
initVM(&initArgs, &vm);
323323
startVM(&vm);
324324
destroyVM(&vm);
325+
326+
return 0;
325327
}

src/jvmd.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/********************************************
2+
* file name: jvmd.c
3+
* description: java virtual machine debugger
4+
* author: kari.zhang
5+
*
6+
* modifications:
7+
*
8+
* 1. Created by Kari.Zhang @ 2016-01-11
9+
* *****************************************/
10+
11+
#include <stdio.h>
12+
#include <string.h>
13+
14+
#define JVMD_VER "0.0.1"
15+
16+
static void logDebugger();
17+
static void usage();
18+
19+
int main(int argc, char *argv[]) {
20+
21+
if (2 != argc) {
22+
usage();
23+
return -1;
24+
}
25+
26+
if (0 == strncmp(argv[1], "-v", 2)) {
27+
logDebugger();
28+
return 0;
29+
} else if (*argv[1] == '-') {
30+
usage();
31+
return -1;
32+
} else {
33+
printf("Not implemented yet!\n");
34+
return 0;
35+
}
36+
}
37+
38+
static void logDebugger() {
39+
printf("jvmd %s compiled on %s [email protected]\n", JVMD_VER, __DATE__);
40+
}
41+
42+
static void usage() {
43+
printf("Usage:\n");
44+
printf(" jvmd CLASS_NAME\n");
45+
}

src/jvmp.c

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,41 @@
1515
#include <string.h>
1616
#include "class.h"
1717

18+
#define JVMP_VER "0.0.1"
19+
1820
static void usage();
1921

2022
int main(int argc, char *argv[]) {
2123
if (2 != argc) {
2224
usage();
2325
return -1;
2426
}
25-
char path[256];
26-
memset(path, 0, 256);
27-
strcpy(path, argv[1]);
28-
Class *mainClass = loadClassFromFile(path, argv[1]);
29-
if (NULL == mainClass) {
30-
fprintf(stderr, "Failed loadClass from file\n");
31-
return -1;
32-
}
33-
ClassEntry *clsEntry = CLASS_CE(mainClass);
34-
logClassEntry(clsEntry);
35-
36-
//TOD
37-
// freeClass();
27+
28+
if (0 == strncmp(argv[1], "-v", 2)) {
29+
printf("jvmp %s compiled on %s [email protected]\n", JVMP_VER, __DATE__);
30+
return 0;
31+
32+
} else if (*argv[1] == '-') {
33+
usage();
34+
return -1;
35+
36+
} else {
37+
char path[256];
38+
memset(path, 0, 256);
39+
strcpy(path, argv[1]);
40+
Class *mainClass = loadClassFromFile(path, argv[1]);
41+
if (NULL == mainClass) {
42+
fprintf(stderr, "Failed loadClass from file\n");
43+
return -1;
44+
}
45+
ClassEntry *clsEntry = CLASS_CE(mainClass);
46+
logClassEntry(clsEntry);
47+
48+
//TOD
49+
// freeClass();
50+
51+
return 0;
52+
}
3853
}
3954

4055
static void usage() {

src/vm/class.c

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ static void readConstPool(ClassEntry* class, U2 cp_count, U1** base) {
6262
ConstPool *constPool = newConstPool(cp_count);
6363
assert(NULL != constPool);
6464
int i;
65-
int j;
6665
for (i = 1; i < cp_count; ++i) {
6766
READ_U1(tag, *base);
6867
constPool->entries[i].tag = tag;
@@ -170,7 +169,6 @@ static void readClassField(Class *cls, U2 field_count, U1** base) {
170169
char *name = NULL;
171170
U2 acc_flags;
172171
U2 attr_count;
173-
U2 clsidx;
174172
U2 nameidx;
175173
U2 typeidx;
176174

@@ -576,7 +574,6 @@ static void readAnnotationElementValue(ClassEntry* class, U1** base) {
576574
U2 type_idx;
577575
U2 num_pairs;
578576
U2 elem_name_idx;
579-
U1 tag;
580577
int i, j;
581578
for (i = 0; i < anno_count; ++i) {
582579
READ_U2(type_idx, *base);
@@ -831,7 +828,7 @@ Class* loadClassFromFile(char *path, char *classname) {
831828
fclose(fp);
832829
return NULL;
833830
}
834-
Class *class = defineClass(classname, buff, size);
831+
Class *class = defineClass(classname, (const char *)buff, size);
835832
free(buff);
836833
fclose(fp);
837834
return class;
@@ -1052,7 +1049,9 @@ void logConstPoolEntry(const ConstPool* pool, const ConstPoolEntry* entry)
10521049
case CONST_Double:
10531050
u4high = entry->info.double_info.high_bytes;
10541051
u4low = entry->info.double_info.low_bytes;
1055-
u8 = u4high<<32 + u4low;
1052+
u8 = u4high;
1053+
u8 <<= 32;
1054+
u8 += u4low;
10561055
printf("double\t%" PRIu64 "\n", u8);
10571056
break;
10581057

@@ -1196,16 +1195,6 @@ void logClassEntry(ClassEntry *clsEntry)
11961195
printf(" major version: %d\n", (int)clsEntry->reserve[1]);
11971196
printf(" Constant pool count:%d\n", clsEntry->constPool->length);
11981197

1199-
int cls_idx;
1200-
int nametype_idx;
1201-
int name_idx;
1202-
int type_idx;
1203-
int index;
1204-
1205-
U4 u4high;
1206-
U4 u4low;
1207-
U8 u8;
1208-
12091198
for (i = 1; i < clsEntry->constPool->length; ++i) {
12101199
printf(" #%d = ", i);
12111200
logConstPoolEntry(clsEntry->constPool,

src/vm/instruction.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ bool needAddOffset (U1 opcode)
459459
*/
460460
#define INST_FUNC(X) func_inst_##X
461461
#define _T(X) #X
462-
#define INIT_INST(X, Y) X, _T(X), 0, Y, (Y + 1), INST_FUNC(X), 0
462+
#define INIT_INST(X, Y) {X, _T(X), {0}, Y, (Y + 1), INST_FUNC(X), 0}
463463
static Instruction sInstructionTable[] = {
464464
INIT_INST(nop, 0), // 0x00
465465
INIT_INST(aconst_null, 0), // 0x01

0 commit comments

Comments
 (0)