From 86e15b58567da3aebdf152f61c68bb82caa6914c Mon Sep 17 00:00:00 2001 From: mrab Date: Tue, 14 May 2024 11:12:12 +0200 Subject: [PATCH] method assembly is actually used (yay) --- src/ByteCode/ClassFile/Generator.hs | 39 +++++++++++++++++------------ src/Main.hs | 2 +- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/ByteCode/ClassFile/Generator.hs b/src/ByteCode/ClassFile/Generator.hs index 8f7287a..a482f16 100644 --- a/src/ByteCode/ClassFile/Generator.hs +++ b/src/ByteCode/ClassFile/Generator.hs @@ -140,13 +140,7 @@ methodBuilder (MethodDeclaration returntype name parameters statement) input = l memberAccessFlags = accessPublic, memberNameIndex = (fromIntegral baseIndex), memberDescriptorIndex = (fromIntegral (baseIndex + 1)), - memberAttributes = [ - CodeAttribute { - attributeMaxStack = 420, - attributeMaxLocals = 420, - attributeCode = [Opreturn] - } - ] + memberAttributes = [] } in input { @@ -157,14 +151,26 @@ methodBuilder (MethodDeclaration returntype name parameters statement) input = l methodAssembler :: ClassFileBuilder MethodDeclaration methodAssembler (MethodDeclaration returntype name parameters statement) input = let - code = CodeAttribute { - attributeMaxStack = 420, - attributeMaxLocals = 420, - attributeCode = [Opiadd] - } - --methodConstantIndex = - in - input + methodConstantIndex = findMethodIndex input name + in case methodConstantIndex of + Nothing -> error ("Cannot find method entry in method pool for method: " ++ name) + Just index -> let + declaration = MethodDeclaration returntype name parameters statement + (pre, method : post) = splitAt index (methods input) + (_, bytecode) = assembleMethod declaration (constantPool input, []) + assembledMethod = method { + memberAttributes = [ + CodeAttribute { + attributeMaxStack = 420, + attributeMaxLocals = 420, + attributeCode = bytecode + } + ] + } + in + input { + methods = pre ++ (assembledMethod : post) + } @@ -195,8 +201,9 @@ comparisonOperation CompareGreaterOrEqual branchLocation = Opif_icmpge branchLoc assembleMethod :: Assembler MethodDeclaration -assembleMethod (MethodDeclaration _ _ _ (Block statements)) (constants, ops) = +assembleMethod (MethodDeclaration _ _ _ (TypedStatement _ (Block statements))) (constants, ops) = foldr assembleStatement (constants, ops) statements +assembleMethod (MethodDeclaration _ _ _ stmt) (_, _) = error ("Block expected for method body, got: " ++ show stmt) assembleStatement :: Assembler Statement assembleStatement (TypedStatement stype (Return expr)) (constants, ops) = case expr of diff --git a/src/Main.hs b/src/Main.hs index 130ed20..c2ffd1b 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -9,7 +9,7 @@ import ByteCode.ClassFile import Data.ByteString (pack, writeFile) main = do - let untypedAST = parse $ alexScanTokens "class Testklasse {int a; int b; void something(){} void something_else(){}}" + let untypedAST = parse $ alexScanTokens "class Testklasse {void something(){return;}}" let typedAST = head (typeCheckCompilationUnit untypedAST) let abstractClassFile = classBuilder typedAST emptyClassFile let assembledClassFile = pack (serialize abstractClassFile)