method assembly is actually used (yay)

This commit is contained in:
mrab 2024-05-14 11:12:12 +02:00
parent 393253c9bb
commit 86e15b5856
2 changed files with 24 additions and 17 deletions

View File

@ -140,13 +140,7 @@ methodBuilder (MethodDeclaration returntype name parameters statement) input = l
memberAccessFlags = accessPublic, memberAccessFlags = accessPublic,
memberNameIndex = (fromIntegral baseIndex), memberNameIndex = (fromIntegral baseIndex),
memberDescriptorIndex = (fromIntegral (baseIndex + 1)), memberDescriptorIndex = (fromIntegral (baseIndex + 1)),
memberAttributes = [ memberAttributes = []
CodeAttribute {
attributeMaxStack = 420,
attributeMaxLocals = 420,
attributeCode = [Opreturn]
}
]
} }
in in
input { input {
@ -157,14 +151,26 @@ methodBuilder (MethodDeclaration returntype name parameters statement) input = l
methodAssembler :: ClassFileBuilder MethodDeclaration methodAssembler :: ClassFileBuilder MethodDeclaration
methodAssembler (MethodDeclaration returntype name parameters statement) input = let methodAssembler (MethodDeclaration returntype name parameters statement) input = let
code = CodeAttribute { methodConstantIndex = findMethodIndex input name
attributeMaxStack = 420, in case methodConstantIndex of
attributeMaxLocals = 420, Nothing -> error ("Cannot find method entry in method pool for method: " ++ name)
attributeCode = [Opiadd] Just index -> let
} declaration = MethodDeclaration returntype name parameters statement
--methodConstantIndex = (pre, method : post) = splitAt index (methods input)
in (_, bytecode) = assembleMethod declaration (constantPool input, [])
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 :: Assembler MethodDeclaration
assembleMethod (MethodDeclaration _ _ _ (Block statements)) (constants, ops) = assembleMethod (MethodDeclaration _ _ _ (TypedStatement _ (Block statements))) (constants, ops) =
foldr assembleStatement (constants, ops) statements foldr assembleStatement (constants, ops) statements
assembleMethod (MethodDeclaration _ _ _ stmt) (_, _) = error ("Block expected for method body, got: " ++ show stmt)
assembleStatement :: Assembler Statement assembleStatement :: Assembler Statement
assembleStatement (TypedStatement stype (Return expr)) (constants, ops) = case expr of assembleStatement (TypedStatement stype (Return expr)) (constants, ops) = case expr of

View File

@ -9,7 +9,7 @@ import ByteCode.ClassFile
import Data.ByteString (pack, writeFile) import Data.ByteString (pack, writeFile)
main = do 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 typedAST = head (typeCheckCompilationUnit untypedAST)
let abstractClassFile = classBuilder typedAST emptyClassFile let abstractClassFile = classBuilder typedAST emptyClassFile
let assembledClassFile = pack (serialize abstractClassFile) let assembledClassFile = pack (serialize abstractClassFile)