diff --git a/src/ByteCode/ClassFile.hs b/src/ByteCode/ClassFile.hs index 38cf88e..37481db 100644 --- a/src/ByteCode/ClassFile.hs +++ b/src/ByteCode/ClassFile.hs @@ -113,10 +113,10 @@ opcodeEncodingLength (Opinvokespecial _) = 3 opcodeEncodingLength (Opgoto _) = 3 opcodeEncodingLength (Opsipush _) = 3 opcodeEncodingLength (Opldc_w _) = 3 -opcodeEncodingLength (Opaload _) = 3 -opcodeEncodingLength (Opiload _) = 3 -opcodeEncodingLength (Opastore _) = 3 -opcodeEncodingLength (Opistore _) = 3 +opcodeEncodingLength (Opaload _) = 4 +opcodeEncodingLength (Opiload _) = 4 +opcodeEncodingLength (Opastore _) = 4 +opcodeEncodingLength (Opistore _) = 4 opcodeEncodingLength (Opputfield _) = 3 opcodeEncodingLength (Opgetfield _) = 3 diff --git a/src/ByteCode/ClassFile/Generator.hs b/src/ByteCode/ClassFile/Generator.hs index 2eabd67..adb4d6f 100644 --- a/src/ByteCode/ClassFile/Generator.hs +++ b/src/ByteCode/ClassFile/Generator.hs @@ -262,6 +262,14 @@ assembleStatement (constants, ops, lvars) (TypedStatement _ (If expr if_stmt els else_length = sum (map opcodeEncodingLength ops_elsea) + 3 in (constants_ifa, ops ++ ops_cmp ++ [Opsipush 0, Opif_icmpeq if_length] ++ ops_ifa ++ [Opgoto else_length] ++ ops_elsea, lvars) +assembleStatement (constants, ops, lvars) (TypedStatement _ (While expr stmt)) = let + (constants_cmp, ops_cmp, _) = assembleExpression (constants, [], lvars) expr + (constants_stmta, ops_stmta, _) = assembleStatement (constants_cmp, [], lvars) stmt + -- +3 because we insert 2 gotos, one for the comparison, one for the goto back to the comparison + stmt_length = sum (map opcodeEncodingLength ops_stmta) + 6 + entire_length = stmt_length + sum (map opcodeEncodingLength ops_cmp) + in + (constants_stmta, ops ++ ops_cmp ++ [Opsipush 0, Opif_icmpeq stmt_length] ++ ops_stmta ++ [Opgoto (-entire_length)], lvars) assembleStatement (constants, ops, lvars) (TypedStatement _ (LocalVariableDeclaration (VariableDeclaration dtype name expr))) = let isPrimitive = elem dtype ["char", "boolean", "int"] (constants_init, ops_init, _) = case expr of