{-# LANGUAGE GeneralizedNewtypeDeriving, DeriveDataTypeable #-}moduleLLVM.Core.CodeGenMonad(-- * Module code generationCodeGenModule,runCodeGenModule,genMSym,getModule,GlobalMappings(..),addGlobalMapping,getGlobalMappings,-- * Function code generationCodeGenFunction,runCodeGenFunction,genFSym,getFunction,getBuilder,getFunctionModule,getExterns,putExterns,-- * ReexportliftIO)whereimportData.TypeableimportControl.Monad.StateimportControl.Applicative(Applicative,)importForeign.Ptr(Ptr,)importLLVM.Core.Util(Module,Builder,Function)--------------------------------------dataCGMState=CGMState{cgm_module::Module,cgm_externs::[(String,Function)],cgm_global_mappings::[(Function,Ptr())],cgm_next::!Int}deriving(Show,Typeable)newtypeCodeGenModulea=CGM(StateTCGMStateIOa)deriving(Functor,Applicative,Monad,MonadStateCGMState,MonadIO,Typeable)genMSym::String->CodeGenModuleStringgenMSymprefix=dos<-getletn=cgm_nextsput(s{cgm_next=n+1})return$"_"++prefix++showngetModule::CodeGenModuleModulegetModule=getscgm_modulerunCodeGenModule::Module->CodeGenModulea->IOarunCodeGenModulem(CGMbody)=doletcgm=CGMState{cgm_module=m,cgm_next=1,cgm_externs=[],cgm_global_mappings=[]}evalStateTbodycgm--------------------------------------dataCGFStater=CGFState{cgf_module::CGMState,cgf_builder::Builder,cgf_function::Function,cgf_next::!Int}deriving(Show,Typeable)newtypeCodeGenFunctionra=CGF(StateT(CGFStater)IOa)deriving(Functor,Applicative,Monad,MonadState(CGFStater),MonadIO,Typeable)genFSym::CodeGenFunctionaStringgenFSym=dos<-getletn=cgf_nextsput(s{cgf_next=n+1})return$"_L"++showngetFunction::CodeGenFunctionaFunctiongetFunction=getscgf_functiongetBuilder::CodeGenFunctionaBuildergetBuilder=getscgf_buildergetFunctionModule::CodeGenFunctionaModulegetFunctionModule=gets(cgm_module.cgf_module)getExterns::CodeGenFunctiona[(String,Function)]getExterns=gets(cgm_externs.cgf_module)putExterns::[(String,Function)]->CodeGenFunctiona()putExternses=docgf<-getletcgm'=(cgf_modulecgf){cgm_externs=es}put(cgf{cgf_module=cgm'})addGlobalMapping::Function->Ptr()->CodeGenFunctionr()addGlobalMappingvaluefunc=-- could be written in a nicer way using Data.Accessormodify$\cgf->letcgm=cgf_modulecgfincgf{cgf_module=cgm{cgm_global_mappings=(value,func):cgm_global_mappingscgm}}newtypeGlobalMappings=GlobalMappings[(Function,Ptr())]{- |
Get a list created by calls to 'staticFunction'
that must be passed to the execution engine
via 'LLVM.ExecutionEngine.addGlobalMappings'.
-}getGlobalMappings::CodeGenModuleGlobalMappingsgetGlobalMappings=gets(GlobalMappings.cgm_global_mappings)runCodeGenFunction::Builder->Function->CodeGenFunctionra->CodeGenModulearunCodeGenFunctionbldfn(CGFbody)=docgm<-getletcgf=CGFState{cgf_module=cgm,cgf_builder=bld,cgf_function=fn,cgf_next=1}(a,cgf')<-liftIO$runStateTbodycgfput(cgf_modulecgf')returna