diff --git a/project.cabal b/project.cabal index 520b172..5a98c99 100644 --- a/project.cabal +++ b/project.cabal @@ -16,7 +16,7 @@ executable compiler src/ByteCode, src/ByteCode/ClassFile build-tool-depends: alex:alex, happy:happy - other-modules: Parser.Lexer, Ast, Parser.JavaParser, ByteCode.ByteUtil, ByteCode.ClassFile, ByteCode.ClassFile.Generator + other-modules: Parser.Lexer, Ast, Parser.JavaParser, ByteCode.ByteUtil, ByteCode.ClassFile, ByteCode.ClassFile.Generator, ByteCode.Constants test-suite tests type: exitcode-stdio-1.0 diff --git a/src/ByteCode/ClassFile.hs b/src/ByteCode/ClassFile.hs index b1aa39c..8d23be2 100644 --- a/src/ByteCode/ClassFile.hs +++ b/src/ByteCode/ClassFile.hs @@ -1,21 +1,17 @@ -module ByteCode.ClassFile where +module ByteCode.ClassFile( + ConstantInfo(..), + Attribute(..), + MemberInfo(..), + ClassFile(..), + serialize +) where import Data.Word import Data.Int import Data.ByteString (unpack) import Data.ByteString.UTF8 (fromString) import ByteCode.ByteUtil - -tagClass = 0x07 -tagFieldref = 0x09 -tagMethodref = 0x0A -tagNameandtype = 0x0C -tagInteger = 0x03 -tagUtf8 = 0x01 - -accessPublic = 0x01 -accessPrivate = 0x02 -accessProtected = 0x04 +import ByteCode.Constants data ConstantInfo = ClassInfo Word16 | FieldRefInfo Word16 Word16 diff --git a/src/ByteCode/ClassFile/Generator.hs b/src/ByteCode/ClassFile/Generator.hs index 9bcbfff..5005f9d 100644 --- a/src/ByteCode/ClassFile/Generator.hs +++ b/src/ByteCode/ClassFile/Generator.hs @@ -1 +1,31 @@ -module ByteCode.ClassFile.Generator where +module ByteCode.ClassFile.Generator( + fromAST +) where + +import ByteCode.Constants +import ByteCode.ClassFile (ClassFile (..), ConstantInfo (..)) +import Ast + + +baseConstants = [ + ClassInfo 4, + MethodRefInfo 1 3, + NameAndTypeInfo 5 6, + Utf8Info "java/lang/Object", + Utf8Info "", + Utf8Info "()V" + ] + + +fromAST :: Class -> ClassFile +fromAST (Class name methods fields) = let + in + ClassFile { + constantPool = baseConstants, + accessFlags = accessPublic, + thisClass = 1, + superClass = 1, + fields = [], + methods = [], + attributes = [] + } \ No newline at end of file diff --git a/src/ByteCode/Constants.hs b/src/ByteCode/Constants.hs new file mode 100644 index 0000000..28b4a9c --- /dev/null +++ b/src/ByteCode/Constants.hs @@ -0,0 +1,25 @@ +module ByteCode.Constants where +import Data.Word + + +tagClass :: Word8 +tagFieldref :: Word8 +tagMethodref :: Word8 +tagNameandtype :: Word8 +tagInteger :: Word8 +tagUtf8 :: Word8 + +accessPublic :: Word16 +accessPrivate :: Word16 +accessProtected :: Word16 + +tagClass = 0x07 +tagFieldref = 0x09 +tagMethodref = 0x0A +tagNameandtype = 0x0C +tagInteger = 0x03 +tagUtf8 = 0x01 + +accessPublic = 0x01 +accessPrivate = 0x02 +accessProtected = 0x04 \ No newline at end of file