packages feed

alpha 0.9.0.3 → 0.9.5

raw patch · 6 files changed

+27/−31 lines, 6 filesdep +bindings-posix

Dependencies added: bindings-posix

Files

alpha.cabal view
@@ -1,5 +1,5 @@ name:           alpha-version:        0.9.0.3+version:        0.9.5 synopsis:       A compiler for the Alpha language description:    Alpha is a programming language that aims at being very simple and                  low-level, so as to be efficient, while at the same time@@ -20,8 +20,8 @@   location: git://github.com/lih/Alpha.git  executable alpha-  build-depends:  base<=4.6.0.0, unix, containers, array, mtl, bytestring, bimap, ghc-prim, directory, filepath, cereal, parsec, transformers+  build-depends:  base<=4.6.0.0, unix, containers, array, mtl, bytestring, bimap, ghc-prim, directory, filepath, cereal, parsec, transformers, bindings-posix   main-is:        Alpha.hs   other-modules:  Alpha, Compile, Compile.State, Compile.Utils, Context, Context.Language, Context.Types, Elf, ID, My.Control.Monad, My.Control.Monad.State, My.Control.Monad.TimeLine, My.Data.Graph, My.Data.List, My.Prelude, Options, PCode, PCode.Builtin, PCode.Instruction, PCode.Value, Serialize, Specialize, Specialize.Architecture, Specialize.Types, Specialize.X86, Syntax, Syntax.Parse, Translate   hs-source-dirs: src-  c-sources:      src/writeElf.c src/writeElf.h+  c-sources:      src/writeElf.c
src/Alpha.hs view
@@ -1,14 +1,16 @@-{-# LANGUAGE ViewPatterns, NoMonomorphismRestriction #-}+{-# LANGUAGE ViewPatterns, NoMonomorphismRestriction, ParallelListComp #-} import System.Environment as SE import System.FilePath import System.Directory import System.Posix.Files+import Foreign hiding (void) import Data.Ord import Data.Maybe import qualified Data.Map as M import qualified Data.Bimap as BM import qualified Data.Serialize as Ser import qualified Data.ByteString as B+import Data.ByteString.Unsafe import Data.List import My.Control.Monad import My.Control.Monad.State hiding ((<.>))@@ -34,7 +36,7 @@   PrintVersion -> printVersion   Compile -> print s >> doCompile s   -version = "0.9.0.3"+version = "0.9.5" printHelp = putStrLn helpMsg printVersion = putStrLn $ "Alpha version "++version   @@ -57,8 +59,11 @@       importLanguage compileLanguage language       rootSym <- stateF languageF $ internSym root       getAddressComp (outputArch opts) rootSym-      chunks <- sortBy (comparing fst) $< M.elems $< gets compAddresses-      undefined+      (addrs,ptrs) <- unzip $< sortBy (comparing fst) $< M.elems $< gets compAddresses+      top <- gets compTop+      contents <- B.concat $< sequence [withForeignPtr ptr $ \p -> unsafePackCStringLen (castPtr p,size) +                                       | ptr <- ptrs | size <- zipWith (-) (tail addrs++[top]) addrs]+      writeElf language contents     compileLanguage name = do       source <- fromMaybe (fail $ "Couldn't find source file for language "++name) $< findSource name       let langFile = languageFile name@@ -75,7 +80,7 @@       let sTree = concat $ parseAlpha src str       code <- mapM compileExpr sTree       languageState $ modify $ \e -> exportLanguage $ e { loadCode = foldr concatCode [] code }-      where compileExpr expr = print expr >> do+      where compileExpr expr = print (fmap Str expr) >> do               symExpr <- languageState $ envCast expr               trExpr <- doTransform symExpr               (code,imports) <- languageState $ compile Nothing trExpr
src/Context.hs view
@@ -7,7 +7,9 @@               ,execCode) where  import System.IO.Unsafe (unsafePerformIO)+import Bindings.Posix.Sys.Mman import Foreign hiding (unsafePerformIO,unsafeForeignPtrToPtr)+import Foreign.C import Foreign.ForeignPtr.Unsafe import Data.Maybe import Data.ByteString.Unsafe@@ -25,6 +27,8 @@ import Syntax import Specialize.Architecture +foreign import ccall "mprotect" mprotect :: Ptr () -> CSize -> CInt -> IO CInt + withRef ref val x = readIORef ref >>= \v -> writeIORef ref val >> x >>= \x -> writeIORef ref v >> return x  contextRef = unsafePerformIO $ newIORef (undefined :: Context)@@ -77,7 +81,11 @@ evalCode :: (FunPtr f -> f) -> Code -> IO f evalCode wrap code = do    binary <- snd $ specialize hostArch getAddressJIT code-  unsafeUseAsCString binary $ return . wrap . castPtrToFunPtr+  putStrLn $ "Evaluating code "++show code+  unsafeUseAsCStringLen binary $ \(p,s) -> do +    mprotect (castPtr p) (fromIntegral s) (c'PROT_READ .|. c'PROT_WRITE .|. c'PROT_EXEC)+    return $ wrap $ castPtrToFunPtr p+execCode [] = return () execCode instrs = do   id <- languageState $ state createSym                   join $ evalCode mkProc (Code [] instrs (symBind id))
src/Specialize.hs view
@@ -8,4 +8,4 @@ import Data.ByteString  specialize :: Monad m => Architecture -> (ID -> m Int) -> Code -> (Int,m ByteString)-specialize arch assoc (Code args code ret) = (1,return $ pack [0x83])+specialize arch assoc (Code args code ret) = (1,return $ pack [0xC3])
src/writeElf.c view
@@ -1,8 +1,11 @@ #include <unistd.h> #include <libelf.h>-#include "writeElf.h"  #define SETSTRUCT(v,t,vals...) { t tmp = vals; v = tmp; }++typedef unsigned char byte;++void writeElf(int fd,byte* data,int dataSize);  void writeElf(int fd,byte* data,int dataSize) {    Elf64_Ehdr eh;    Elf64_Phdr pht[1];
− src/writeElf.h
@@ -1,20 +0,0 @@-#ifndef WRITEELF-#define WRITEELF--typedef unsigned char byte;--void writeElf(int fd,byte* data,int dataSize);--#endif // WRITEELF--/* -Copyright (c) 2012, Coiffier Marc <marc.coiffier@gmail.com>-All rights reserved.--Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:--    Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.-    Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.--THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DPCodeECT, INDPCodeECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/