Skip to content

Commit 323b9e5

Browse files
author
Louis Jenkins
committed
Began implementation of I/O support
1 parent 1578f34 commit 323b9e5

File tree

5 files changed

+102
-28
lines changed

5 files changed

+102
-28
lines changed

ConditionalTest.class

374 Bytes
Binary file not shown.

ConditionalTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ class ConditionalTest
44
public static void main (String[] args) {
55
int x = 0;
66
for (int i = 0; i < 100; i++)
7-
x++;
7+
System.out.println("Idx: " + i + ";Val: " + x++);
88
}
99
}

ConditionalTest.txt

Lines changed: 70 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,57 @@
11
Classfile /C:/github/Minimal-JVM/ConditionalTest.class
2-
Last modified Nov 24, 2016; size 342 bytes
3-
MD5 checksum 8f4ba641b3a06cd0389f3e7fdcc7df21
2+
Last modified Nov 25, 2016; size 716 bytes
3+
MD5 checksum 23360d50cddaa082c043a9bf8978cdf8
44
Compiled from "ConditionalTest.java"
55
class ConditionalTest
66
minor version: 0
77
major version: 52
88
flags: ACC_SUPER
99
Constant pool:
10-
#1 = Methodref #3.#13 // java/lang/Object."<init>":()V
11-
#2 = Class #14 // ConditionalTest
12-
#3 = Class #15 // java/lang/Object
13-
#4 = Utf8 <init>
14-
#5 = Utf8 ()V
15-
#6 = Utf8 Code
16-
#7 = Utf8 LineNumberTable
17-
#8 = Utf8 main
18-
#9 = Utf8 ([Ljava/lang/String;)V
19-
#10 = Utf8 StackMapTable
20-
#11 = Utf8 SourceFile
21-
#12 = Utf8 ConditionalTest.java
22-
#13 = NameAndType #4:#5 // "<init>":()V
23-
#14 = Utf8 ConditionalTest
24-
#15 = Utf8 java/lang/Object
10+
#1 = Methodref #12.#22 // java/lang/Object."<init>":()V
11+
#2 = Fieldref #23.#24 // java/lang/System.out:Ljava/io/PrintStream;
12+
#3 = Class #25 // java/lang/StringBuilder
13+
#4 = Methodref #3.#22 // java/lang/StringBuilder."<init>":()V
14+
#5 = String #26 // Idx:
15+
#6 = Methodref #3.#27 // java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
16+
#7 = Methodref #3.#28 // java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
17+
#8 = String #29 // ;Val:
18+
#9 = Methodref #3.#30 // java/lang/StringBuilder.toString:()Ljava/lang/String;
19+
#10 = Methodref #31.#32 // java/io/PrintStream.println:(Ljava/lang/String;)V
20+
#11 = Class #33 // ConditionalTest
21+
#12 = Class #34 // java/lang/Object
22+
#13 = Utf8 <init>
23+
#14 = Utf8 ()V
24+
#15 = Utf8 Code
25+
#16 = Utf8 LineNumberTable
26+
#17 = Utf8 main
27+
#18 = Utf8 ([Ljava/lang/String;)V
28+
#19 = Utf8 StackMapTable
29+
#20 = Utf8 SourceFile
30+
#21 = Utf8 ConditionalTest.java
31+
#22 = NameAndType #13:#14 // "<init>":()V
32+
#23 = Class #35 // java/lang/System
33+
#24 = NameAndType #36:#37 // out:Ljava/io/PrintStream;
34+
#25 = Utf8 java/lang/StringBuilder
35+
#26 = Utf8 Idx:
36+
#27 = NameAndType #38:#39 // append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
37+
#28 = NameAndType #38:#40 // append:(I)Ljava/lang/StringBuilder;
38+
#29 = Utf8 ;Val:
39+
#30 = NameAndType #41:#42 // toString:()Ljava/lang/String;
40+
#31 = Class #43 // java/io/PrintStream
41+
#32 = NameAndType #44:#45 // println:(Ljava/lang/String;)V
42+
#33 = Utf8 ConditionalTest
43+
#34 = Utf8 java/lang/Object
44+
#35 = Utf8 java/lang/System
45+
#36 = Utf8 out
46+
#37 = Utf8 Ljava/io/PrintStream;
47+
#38 = Utf8 append
48+
#39 = Utf8 (Ljava/lang/String;)Ljava/lang/StringBuilder;
49+
#40 = Utf8 (I)Ljava/lang/StringBuilder;
50+
#41 = Utf8 toString
51+
#42 = Utf8 ()Ljava/lang/String;
52+
#43 = Utf8 java/io/PrintStream
53+
#44 = Utf8 println
54+
#45 = Utf8 (Ljava/lang/String;)V
2555
{
2656
ConditionalTest();
2757
descriptor: ()V
@@ -38,29 +68,43 @@ Constant pool:
3868
descriptor: ([Ljava/lang/String;)V
3969
flags: ACC_PUBLIC, ACC_STATIC
4070
Code:
41-
stack=2, locals=3, args_size=1
71+
stack=3, locals=3, args_size=1
4272
0: iconst_0
4373
1: istore_1
4474
2: iconst_0
4575
3: istore_2
4676
4: iload_2
4777
5: bipush 100
48-
7: if_icmpge 19
49-
10: iinc 1, 1
50-
13: iinc 2, 1
51-
16: goto 4
52-
19: return
78+
7: if_icmpge 53
79+
10: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
80+
13: new #3 // class java/lang/StringBuilder
81+
16: dup
82+
17: invokespecial #4 // Method java/lang/StringBuilder."<init>":()V
83+
20: ldc #5 // String Idx:
84+
22: invokevirtual #6 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
85+
25: iload_2
86+
26: invokevirtual #7 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
87+
29: ldc #8 // String ;Val:
88+
31: invokevirtual #6 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
89+
34: iload_1
90+
35: iinc 1, 1
91+
38: invokevirtual #7 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
92+
41: invokevirtual #9 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
93+
44: invokevirtual #10 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
94+
47: iinc 2, 1
95+
50: goto 4
96+
53: return
5397
LineNumberTable:
5498
line 5: 0
5599
line 6: 2
56100
line 7: 10
57-
line 6: 13
58-
line 8: 19
101+
line 6: 47
102+
line 8: 53
59103
StackMapTable: number_of_entries = 2
60104
frame_type = 253 /* append */
61105
offset_delta = 4
62106
locals = [ int, int ]
63107
frame_type = 250 /* chop */
64-
offset_delta = 14
108+
offset_delta = 48
65109
}
66110
SourceFile: "ConditionalTest.java"

VirtualMachine/ByteCode.hs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module VirtualMachine.ByteCode where
1414
where
1515
toValue :: CP_Info -> IO Value
1616
toValue info = return $ case tag info of
17+
1 -> VString . show . utf8_bytes $ info
1718
3 -> VInt . fromIntegral . bytes $ info
1819
4 -> VFloat . wordToFloat . bytes $ info
1920
5 -> VLong (fromIntegral . high_bytes $ info) `shift` 32 .|. (fromIntegral . low_bytes $ info)
@@ -54,8 +55,31 @@ module VirtualMachine.ByteCode where
5455
>>= \pc -> getNextShort frame >>= \n -> readIORef frame >>= flip (writeIORef . program_counter . code_segment) (fromIntegral (fromIntegral pc + n - 1))
5556
-- Return
5657
| bc == 177 = return ()
58+
-- Runtime Stubs
59+
| bc >= 178 || bc <= 195 = runtimeStub env frame bc
5760
| otherwise = error $ "Bad ByteCode Instruction: " ++ show bc
5861

62+
runtimeStub :: Runtime_Environment -> StackFrame -> ByteCode -> IO ()
63+
runtimeStub env frame bc
64+
-- getstatic: 2 bytes wide
65+
| bc == 178 = replicateM_ 2 (getNextBC frame)
66+
-- invokevirtual: (append, println). NOTE: MUST HAVE ONLY ONE PARAMETER ELSE UNDEFINED
67+
| bc == 182 = getNextShort frame >>= \method_idx -> (readIORef . current_class) env
68+
>>= \c -> case methodName c method_idx of
69+
"append" -> (appendValues <$> popOp frame <*> popOp frame) >>= pushOp frame
70+
where
71+
appendValues x y = VString $ show x ++ show y
72+
"println" -> popOp frame >>= print
73+
_ -> error "Bad Method Call!"
74+
where
75+
methodName :: Class -> Word16 -> String
76+
methodName clazz method_idx = let
77+
cpool = constant_pool clazz
78+
method_ref = cpool !! fromIntegral method_idx
79+
name_and_type = cpool !! fromIntegral (name_and_type_index method_ref)
80+
utf8_name = cpool !! fromIntegral (name_index name_and_type)
81+
in show . utf8_bytes $ utf8_name
82+
5983
{-
6084
Loads transfer values from a slot in the Local Variable array to the Operand
6185
Stack. Note as well that we can safely ignore the second index for DWORD-sized

VirtualMachine/Types.hs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,13 @@ module VirtualMachine.Types where
4747
liftVDouble2 :: (Double -> Double -> Double) -> Value -> Value -> Value
4848
liftVDouble2 f (VDouble x) (VDouble y) = VDouble (f x y)
4949

50+
liftVString :: (String -> String) -> Value -> Value
51+
liftVString f (VString x) = VString (f x)
52+
liftVString2 :: (String -> String -> String) -> Value -> Value -> Value
53+
liftVString2 f (VString x) (VString y) = VString (f x y)
54+
5055
-- Wrap Java native primivitve types in Haskell types
51-
data Value = VInt Int | VLong Integer| VFloat Float | VDouble Double | VReference Object
56+
data Value = VInt Int | VLong Integer| VFloat Float | VDouble Double | VReference Object | VString String
5257
deriving (Eq, Ord)
5358

5459
instance Num Value where
@@ -100,6 +105,7 @@ module VirtualMachine.Types where
100105
show (VFloat x) = show x
101106
show (VDouble x) = show x
102107
show (VReference x) = printf "0x%X" x
108+
show (VString x) = x
103109

104110
instance Bits Value where
105111
shift (VInt x) = VInt . shift x

0 commit comments

Comments
 (0)