Compare commits

..

No commits in common. "a179dec3ea71a3b92c00812a08f4be34bef9f79a" and "f9f984568f0800234ed43562724aaa0db6473249" have entirely different histories.

2 changed files with 42 additions and 47 deletions

View File

@ -52,7 +52,7 @@ memberInfoDescriptor constants MemberInfo {
descriptor = constants!!((fromIntegral descriptorIndex) - 1)
in case descriptor of
Utf8Info descriptorText -> descriptorText
_ -> ("Invalid Item at Constant pool index " ++ show descriptorIndex)
_ -> ("Invalid Item at Constant pool index " ++ show descriptorIndex)
memberInfoName :: [ConstantInfo] -> MemberInfo -> String
@ -64,11 +64,11 @@ memberInfoName constants MemberInfo {
name = constants!!((fromIntegral nameIndex) - 1)
in case name of
Utf8Info nameText -> nameText
_ -> ("Invalid Item at Constant pool index " ++ show nameIndex)
_ -> ("Invalid Item at Constant pool index " ++ show nameIndex)
methodDescriptor :: MethodDeclaration -> String
methodDescriptor (MethodDeclaration returntype _ parameters _) = let
methodDescriptor (MethodDeclaration returntype _ parameters _) = let
parameter_types = [datatype | ParameterDeclaration datatype _ <- parameters]
in
"("
@ -104,8 +104,8 @@ classBuilder (Class name methods fields) _ = let
classFileWithAssembledMethods = foldr methodAssembler classFileWithMethods methods
in
classFileWithAssembledMethods
fieldBuilder :: ClassFileBuilder VariableDeclaration
fieldBuilder (VariableDeclaration datatype name _) input = let
@ -135,7 +135,7 @@ methodBuilder (MethodDeclaration returntype name parameters statement) input = l
Utf8Info name,
Utf8Info (methodDescriptor (MethodDeclaration returntype name parameters (Block [])))
]
method = MemberInfo {
memberAccessFlags = accessPublic,
memberNameIndex = (fromIntegral baseIndex),
@ -157,7 +157,7 @@ methodAssembler (MethodDeclaration returntype name parameters statement) input =
Just index -> let
declaration = MethodDeclaration returntype name parameters statement
(pre, method : post) = splitAt index (methods input)
(_, bytecode) = assembleMethod (constantPool input, []) declaration
(_, bytecode) = assembleMethod declaration (constantPool input, [])
assembledMethod = method {
memberAttributes = [
CodeAttribute {
@ -171,11 +171,11 @@ methodAssembler (MethodDeclaration returntype name parameters statement) input =
input {
methods = pre ++ (assembledMethod : post)
}
type Assembler a = ([ConstantInfo], [Operation]) -> a -> ([ConstantInfo], [Operation])
type Assembler a = a -> ([ConstantInfo], [Operation]) -> ([ConstantInfo], [Operation])
returnOperation :: DataType -> Operation
returnOperation dtype
@ -201,72 +201,67 @@ comparisonOperation CompareGreaterOrEqual branchLocation = Opif_icmpge branchLoc
assembleMethod :: Assembler MethodDeclaration
assembleMethod (constants, ops) (MethodDeclaration _ name _ (TypedStatement _ (Block statements)))
| name == "<init>" = let
(constants_a, ops_a) = foldl assembleStatement (constants, ops) statements
assembleMethod (MethodDeclaration _ name _ (TypedStatement _ (Block statements))) (constants, ops)
| name == "<init>" = let
(constants_a, ops_a) = foldr assembleStatement (constants, ops) statements
init_ops = [Opaload 0, Opinvokespecial 2]
in
(constants_a, init_ops ++ ops_a ++ [Opreturn])
| otherwise = let
(constants_a, ops_a) = foldl assembleStatement (constants, ops) statements
(constants_a, ops_a) = foldr assembleStatement (constants, ops) statements
init_ops = [Opaload 0]
in
(constants_a, init_ops ++ ops_a)
assembleMethod _ (MethodDeclaration _ _ _ stmt) = error ("Block expected for method body, got: " ++ show stmt)
assembleMethod (MethodDeclaration _ _ _ stmt) (_, _) = error ("Block expected for method body, got: " ++ show stmt)
assembleStatement :: Assembler Statement
assembleStatement (constants, ops) (TypedStatement stype (Return expr)) = case expr of
assembleStatement (TypedStatement stype (Return expr)) (constants, ops) = case expr of
Nothing -> (constants, ops ++ [Opreturn])
Just expr -> let
(expr_constants, expr_ops) = assembleExpression (constants, ops) expr
in
(expr_constants, expr_ops) = assembleExpression expr (constants, ops)
in
(expr_constants, expr_ops ++ [returnOperation stype])
assembleStatement (constants, ops) (TypedStatement _ (Block statements)) =
foldl assembleStatement (constants, ops) statements
assembleStatement (constants, ops) (TypedStatement _ (If expr if_stmt else_stmt)) = let
(constants_cmp, ops_cmp) = assembleExpression (constants, []) expr
(constants_ifa, ops_ifa) = assembleStatement (constants_cmp, []) if_stmt
(constants_elsea, ops_elsea) = case else_stmt of
Nothing -> (constants_ifa, [])
Just stmt -> assembleStatement (constants_ifa, []) stmt
-- +6 because we insert 2 gotos, one for if, one for else
if_length = sum (map opcodeEncodingLength ops_ifa) + 6
else_length = sum (map opcodeEncodingLength ops_ifa)
assembleStatement (TypedStatement _ (Block statements)) (constants, ops) =
foldr assembleStatement (constants, ops) statements
assembleStatement (TypedStatement _ (If expr if_stmt else_stmt)) (constants, ops) = let
(constants_cmp, ops_cmp) = assembleExpression expr (constants, [])
(constants_ifa, ops_ifa) = assembleStatement if_stmt (constants_cmp, [])
skip_length = sum (map opcodeEncodingLength ops_ifa)
in
(constants_ifa, ops ++ ops_cmp ++ [Opsipush 0, Opif_icmpeq if_length] ++ ops_ifa ++ [Opgoto else_length] ++ ops_elsea)
assembleStatement stmt _ = error ("Not yet implemented: " ++ show stmt)
(constants_ifa, ops ++ ops_cmp ++ [Opsipush 0, Opif_icmpeq skip_length] ++ ops_ifa)
assembleExpression :: Assembler Expression
assembleExpression (constants, ops) (TypedExpression _ (BinaryOperation op a b))
assembleExpression (TypedExpression _ (BinaryOperation op a b)) (constants, ops)
| elem op [Addition, Subtraction, Multiplication, Division, BitwiseAnd, BitwiseOr, BitwiseXor] = let
(aConstants, aOps) = assembleExpression (constants, ops) a
(bConstants, bOps) = assembleExpression (aConstants, aOps) b
(aConstants, aOps) = assembleExpression a (constants, ops)
(bConstants, bOps) = assembleExpression b (aConstants, aOps)
in
(bConstants, bOps ++ [binaryOperation op])
| elem op [CompareEqual, CompareNotEqual, CompareLessThan, CompareLessOrEqual, CompareGreaterThan, CompareGreaterOrEqual] = let
(aConstants, aOps) = assembleExpression (constants, ops) a
(bConstants, bOps) = assembleExpression (aConstants, aOps) b
(aConstants, aOps) = assembleExpression a (constants, ops)
(bConstants, bOps) = assembleExpression b (aConstants, aOps)
cmp_op = comparisonOperation op 9
cmp_ops = [cmp_op, Opsipush 0, Opgoto 6, Opsipush 1]
in
(bConstants, bOps ++ cmp_ops)
assembleExpression (constants, ops) (TypedExpression _ (CharacterLiteral literal)) =
assembleExpression (TypedExpression _ (CharacterLiteral literal)) (constants, ops) =
(constants, ops ++ [Opsipush (fromIntegral (ord literal))])
assembleExpression (constants, ops) (TypedExpression _ (BooleanLiteral literal)) =
assembleExpression (TypedExpression _ (BooleanLiteral literal)) (constants, ops) =
(constants, ops ++ [Opsipush (if literal then 1 else 0)])
assembleExpression (constants, ops) (TypedExpression _ (IntegerLiteral literal))
assembleExpression (TypedExpression _ (IntegerLiteral literal)) (constants, ops)
| literal <= 32767 && literal >= -32768 = (constants, ops ++ [Opsipush (fromIntegral literal)])
| otherwise = (constants ++ [IntegerInfo (fromIntegral literal)], ops ++ [Opldc_w (fromIntegral (1 + length constants))])
assembleExpression (constants, ops) (TypedExpression _ NullLiteral) =
assembleExpression (TypedExpression _ NullLiteral) (constants, ops) =
(constants, ops ++ [Opaconst_null])
assembleExpression (constants, ops) (TypedExpression etype (UnaryOperation Not expr)) = let
(exprConstants, exprOps) = assembleExpression (constants, ops) expr
assembleExpression (TypedExpression etype (UnaryOperation Not expr)) (constants, ops) = let
(exprConstants, exprOps) = assembleExpression expr (constants, ops)
newConstant = fromIntegral (1 + length exprConstants)
in case etype of
"int" -> (exprConstants ++ [IntegerInfo 0x7FFFFFFF], exprOps ++ [Opldc_w newConstant, Opixor])
"char" -> (exprConstants, exprOps ++ [Opsipush 0xFFFF, Opixor])
"boolean" -> (exprConstants, exprOps ++ [Opsipush 0x01, Opixor])
assembleExpression (constants, ops) (TypedExpression _ (UnaryOperation Minus expr)) = let
(exprConstants, exprOps) = assembleExpression (constants, ops) expr
assembleExpression (TypedExpression _ (UnaryOperation Minus expr)) (constants, ops) = let
(exprConstants, exprOps) = assembleExpression expr (constants, ops)
in
(exprConstants, exprOps ++ [Opineg])

View File

@ -184,7 +184,7 @@ referencetype : classorinterfacetype { $1 }
variabledeclarator : variabledeclaratorid { Declarator $1 Nothing }
| variabledeclaratorid ASSIGN variableinitializer { Declarator $1 (Just $3) }
blockstatement : localvariabledeclarationstatement { $1 } -- expected type statement
blockstatement : localvariabledeclarationstatement { $1 }
| statement { $1 }
formalparameter : type variabledeclaratorid { ParameterDeclaration $1 $2 }
@ -200,8 +200,8 @@ variableinitializer : expression { $1 }
localvariabledeclarationstatement : localvariabledeclaration SEMICOLON { $1 }
statement : statementwithouttrailingsubstatement{ $1 } -- statement returns a list of statements
| ifthenstatement { [$1] }
statement : statementwithouttrailingsubstatement{ $1 }
-- | ifthenstatement { }
-- | ifthenelsestatement { }
-- | whilestatement { }
@ -218,7 +218,7 @@ statementwithouttrailingsubstatement : block { [$1] }
-- | expressionstatement { }
| returnstatement { [$1] }
ifthenstatement : IF LBRACE expression RBRACE statement { If $3 (Block $5) Nothing }
ifthenstatement : IF LBRACE expression RBRACE statement { }
ifthenelsestatement : IF LBRACE expression RBRACE statementnoshortif ELSE statement { }