+ printfCp "interface: loading \"%s\"\n" $ toString path
+ let ifpath = toString $ path `B.append` ".class"
+ cfile <- parseClassFile ifpath
+ -- load "superinterfaces" first
+ sequence_ [ loadInterface i | i <- interfaces cfile ]
+ immap <- getInterfaceMethodMap
+
+ -- load map again, because there could be new entries now
+ -- due to loading superinterfaces
+ imap' <- getInterfaceMap
+ let max_off = fromIntegral $ M.size immap * 4
+ -- create index of methods by this interface
+ let mm = zipbase max_off (classMethods cfile)
+
+ -- create for each method from *every* superinterface a entry to,
+ -- but just put in the same offset as it is already in the map
+ let (ifnames, methodnames) = unzip $ concat
+ [ zip (repeat ifname) (classMethods $ imap' M.! ifname)
+ | ifname <- interfaces cfile ]
+ let sm = zipWith (\x y -> (entry y, immap M.! getname x y)) ifnames methodnames
+
+ -- merge all offset tables
+ setInterfaceMethodMap $ M.fromList sm `M.union` M.fromList mm `M.union` immap
+ setInterfaceMap $ M.insert path cfile imap'
+ where
+ zipbase base = zipWith (\x y -> (entry y, x + base)) [0,4..]
+ entry = getname path
+ getname p y = p `B.append` methodName y `B.append` encode (methodSignature y)
+
+
+calculateFields :: Class Resolved -> Maybe ClassInfo -> IO (FieldMap, FieldMap)
+calculateFields cf superclass = do
+ -- TODO(bernhard): correct sizes. int only atm
+
+ let (sfields, ifields) = span (S.member ACC_STATIC . fieldAccessFlags) (classFields cf)
+
+ staticbase <- mallocClassData $ fromIntegral (length sfields) * 4
+ let i_sb = fromIntegral $ ptrToIntPtr staticbase
+ let sm = zipbase i_sb sfields
+ let sc_sm = getsupermap superclass ciStaticMap
+ -- new fields "overwrite" old ones, if they have the same name
+ let staticmap = M.fromList sm `M.union` sc_sm
+
+ let sc_im = getsupermap superclass ciFieldMap
+ -- "+ 4" for the method table pointer
+ let max_off = (4+) $ fromIntegral $ M.size sc_im * 4
+ let im = zipbase max_off ifields
+ -- new fields "overwrite" old ones, if they have the same name
+ let fieldmap = M.fromList im `M.union` sc_im
+
+ return (staticmap, fieldmap)
+ where
+ zipbase base = zipWith (\x y -> (fieldName y, x + base)) [0,4..]
+
+-- helper
+getsupermap :: Maybe ClassInfo -> (ClassInfo -> FieldMap) -> FieldMap
+getsupermap superclass getter = case superclass of Just x -> getter x; Nothing -> M.empty
+
+
+calculateMethodMap :: Class Resolved -> Maybe ClassInfo -> IO (FieldMap, Word32)
+calculateMethodMap cf superclass = do
+ let methods = filter
+ (\x -> (not . S.member ACC_STATIC . methodAccessFlags) x &&
+ ((/=) "<init>" . methodName) x)
+ (classMethods cf)
+ let sc_mm = getsupermap superclass ciMethodMap
+ let max_off = fromIntegral $ M.size sc_mm * 4
+ let mm = zipbase max_off methods
+ let methodmap = M.fromList mm `M.union` sc_mm
+
+ -- (+1): one slot for the interface-table-ptr
+ methodbase <- mallocClassData (((+1) $ fromIntegral $ M.size methodmap) * 4)
+ return (methodmap, fromIntegral $ ptrToIntPtr methodbase)
+ where zipbase base = zipWith (\x y -> (entry y, x + base)) [0,4..]
+ where entry y = methodName y `B.append` encode (methodSignature y)
+
+
+loadAndInitClass :: B.ByteString -> IO ClassInfo
+loadAndInitClass path = do
+ class_map <- getClassMap
+ ci <- case M.lookup path class_map of
+ Nothing -> loadClass path
+ Just x -> return x
+
+ -- first try to execute class initializer of superclass
+ when (path /= "java/lang/Object") (void $ loadAndInitClass $ superClass $ ciFile ci)
+
+ -- execute class initializer
+ case lookupMethod "<clinit>" (ciFile ci) of
+ Just m -> do
+ hmap <- parseMethod (ciFile ci) "<clinit>"
+ case hmap of
+ Just hmap' -> do
+ let mi = MethodInfo "<clinit>" path (methodSignature m)
+ entry <- compileBB hmap' mi
+ addMethodRef entry mi [path]
+ printfCp "executing static initializer from %s now\n" (toString path)
+ executeFuncPtr entry
+ printfCp "static initializer from %s done\n" (toString path)
+ Nothing -> error "loadClass: static initializer not found (WTF?). abort"
+ Nothing -> return ()
+
+ class_map' <- getClassMap
+ let new_ci = ci { ciInitDone = True }
+ setClassMap $ M.insert path new_ci class_map'
+ return new_ci