codec-beam-0.2.0: Erlang VM byte code assembler

Safe HaskellSafe
LanguageHaskell98

Codec.Beam

Contents

Description

If this is your first exposure to BEAM, __I highly recommend Erik Stenman's book: https://happi.github.io/theBeamBook__, which discusses BEAM's architecture in much more detail.

Synopsis

Documentation

encode #

Arguments

:: (Foldable f1, Foldable f2) 
=> Text

module name

-> f1 Metadata 
-> f2 Op

instructions

-> ByteString 

Create code for a BEAM module.

data Metadata #

Extra information regarding the contents of a BEAM module.

export :: Text -> Int -> Metadata #

Name and arity of a function that should be made public.

insertModuleInfo :: Metadata #

The Erlang compiler inserts two functions when compiling source files: module_info/0 and module_info/1. Some pieces of the Erlang toolchain expect this function to exist. For instance, the shell will crash if you try to use TAB (for auto-completion) on a BEAM module without these functions present. These functions have the same implementation, so you can use this Metadata to have the library generate and export them for you.

Syntax

data Op #

A virtual machine instruction—the main unit this library deals with. There are a finite number of instructions, enumerated in Codec.Beam.Instructions. Each new release of Erlang/OTP might introduce a few more and deprecate old ones.

newtype X #

A stack register. These are used to pass function arguments, and X 0 stores return values.

Constructors

X Int 
Instances
Eq X # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

(==) :: X -> X -> Bool #

(/=) :: X -> X -> Bool #

Ord X # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

compare :: X -> X -> Ordering #

(<) :: X -> X -> Bool #

(<=) :: X -> X -> Bool #

(>) :: X -> X -> Bool #

(>=) :: X -> X -> Bool #

max :: X -> X -> X #

min :: X -> X -> X #

Show X # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

showsPrec :: Int -> X -> ShowS #

show :: X -> String #

showList :: [X] -> ShowS #

IsSourceF X # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toSourceF :: X -> SourceF #

IsRegisterF X # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toRegisterF :: X -> RegisterF #

IsSource X # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toSource :: X -> Source #

IsRegister X # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toRegister :: X -> Register #

newtype Y #

A stack register for saving values across function calls. Anything you put in a X register can be overwritten inside a function call (or inside a function call inside a function call). Y registers let you avoid that—they must be allocated and de-allocated though.

Constructors

Y Int 
Instances
Eq Y # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

(==) :: Y -> Y -> Bool #

(/=) :: Y -> Y -> Bool #

Ord Y # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

compare :: Y -> Y -> Ordering #

(<) :: Y -> Y -> Bool #

(<=) :: Y -> Y -> Bool #

(>) :: Y -> Y -> Bool #

(>=) :: Y -> Y -> Bool #

max :: Y -> Y -> Y #

min :: Y -> Y -> Y #

Show Y # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

showsPrec :: Int -> Y -> ShowS #

show :: Y -> String #

showList :: [Y] -> ShowS #

IsSourceF Y # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toSourceF :: Y -> SourceF #

IsRegisterF Y # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toRegisterF :: Y -> RegisterF #

IsSource Y # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toSource :: Y -> Source #

IsRegister Y # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toRegister :: Y -> Register #

newtype F #

Floating point "register" for optimized floating point arithmetic. These are not treated as traditional stack registers.

Constructors

F Int 
Instances
Eq F # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

(==) :: F -> F -> Bool #

(/=) :: F -> F -> Bool #

Ord F # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

compare :: F -> F -> Ordering #

(<) :: F -> F -> Bool #

(<=) :: F -> F -> Bool #

(>) :: F -> F -> Bool #

(>=) :: F -> F -> Bool #

max :: F -> F -> F #

min :: F -> F -> F #

Show F # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

showsPrec :: Int -> F -> ShowS #

show :: F -> String #

showList :: [F] -> ShowS #

IsSourceF F # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toSourceF :: F -> SourceF #

IsRegisterF F # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toRegisterF :: F -> RegisterF #

data Nil #

The empty list.

Constructors

Nil 
Instances
Eq Nil # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

(==) :: Nil -> Nil -> Bool #

(/=) :: Nil -> Nil -> Bool #

Ord Nil # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

compare :: Nil -> Nil -> Ordering #

(<) :: Nil -> Nil -> Bool #

(<=) :: Nil -> Nil -> Bool #

(>) :: Nil -> Nil -> Bool #

(>=) :: Nil -> Nil -> Bool #

max :: Nil -> Nil -> Nil #

min :: Nil -> Nil -> Nil #

Show Nil # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

showsPrec :: Int -> Nil -> ShowS #

show :: Nil -> String #

showList :: [Nil] -> ShowS #

IsSource Nil # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toSource :: Nil -> Source #

newtype Label #

Mark a spot in the code, so that you can jump to it with a function or condition. Start with Label 1 and go up from there.

Constructors

Label Int 
Instances
Eq Label # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

(==) :: Label -> Label -> Bool #

(/=) :: Label -> Label -> Bool #

Ord Label # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

compare :: Label -> Label -> Ordering #

(<) :: Label -> Label -> Bool #

(<=) :: Label -> Label -> Bool #

(>) :: Label -> Label -> Bool #

(>=) :: Label -> Label -> Bool #

max :: Label -> Label -> Label #

min :: Label -> Label -> Label #

Show Label # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

showsPrec :: Int -> Label -> ShowS #

show :: Label -> String #

showList :: [Label] -> ShowS #

data Literal #

Erlang literals, stored on the heap.

Instances
Eq Literal # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

(==) :: Literal -> Literal -> Bool #

(/=) :: Literal -> Literal -> Bool #

Ord Literal # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Show Literal # 
Instance details

Defined in Codec.Beam.Internal.Syntax

IsSourceF Literal # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toSourceF :: Literal -> SourceF #

IsSource Literal # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toSource :: Literal -> Source #

data Lambda #

Turn a named function into a fun, for use with make_fun2.

Constructors

Lambda 

Fields

Instances
Eq Lambda # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

(==) :: Lambda -> Lambda -> Bool #

(/=) :: Lambda -> Lambda -> Bool #

Ord Lambda # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Show Lambda # 
Instance details

Defined in Codec.Beam.Internal.Syntax

data Import #

Reference a function from another module. For example, Import "array" "map" 2 refers to the stdlib function: array:map/2.

Instances
Eq Import # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

(==) :: Import -> Import -> Bool #

(/=) :: Import -> Import -> Bool #

Ord Import # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Show Import # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Argument constraints

data Register #

Either type of stack register, X or Y. Instructions that work with this type, use IsRegister for convenience.

Instances
IsRegister Register # 
Instance details

Defined in Codec.Beam.Internal.Syntax

class IsRegister a where #

Methods

toRegister :: a -> Register #

Instances
IsRegister Register # 
Instance details

Defined in Codec.Beam.Internal.Syntax

IsRegister Y # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toRegister :: Y -> Register #

IsRegister X # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toRegister :: X -> Register #

data Source #

Any sort of Erlang value. Instructions that work with this type, use IsSource for convenience. Note the IsSource instance for Text, which represents a stack atom (in contrast with the Atom constructor in Literal, which is heap-stored).

Instances
IsSource Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toSource :: Source -> Source #

class IsSource a where #

Methods

toSource :: a -> Source #

Instances
IsSource Int # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toSource :: Int -> Source #

IsSource Text # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toSource :: Text -> Source #

IsSource Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toSource :: Source -> Source #

IsSource Literal # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toSource :: Literal -> Source #

IsSource Nil # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toSource :: Nil -> Source #

IsSource Y # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toSource :: Y -> Source #

IsSource X # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toSource :: X -> Source #

data RegisterF #

Memory for manipulating F, for use with fmove. Instructions that work with this type, use IsRegisterF for convenience.

Instances
IsRegisterF RegisterF # 
Instance details

Defined in Codec.Beam.Internal.Syntax

class IsRegisterF a where #

Methods

toRegisterF :: a -> RegisterF #

Instances
IsRegisterF RegisterF # 
Instance details

Defined in Codec.Beam.Internal.Syntax

IsRegisterF F # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toRegisterF :: F -> RegisterF #

IsRegisterF Y # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toRegisterF :: Y -> RegisterF #

IsRegisterF X # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toRegisterF :: X -> RegisterF #

data SourceF #

Something that can be coerced into F, for use with fmove. Instructions that work with this type, use IsSourceF for convenience.

Instances
IsSourceF SourceF # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toSourceF :: SourceF -> SourceF #

class IsSourceF a where #

Methods

toSourceF :: a -> SourceF #

Instances
IsSourceF SourceF # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toSourceF :: SourceF -> SourceF #

IsSourceF Literal # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toSourceF :: Literal -> SourceF #

IsSourceF F # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toSourceF :: F -> SourceF #

IsSourceF Y # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toSourceF :: Y -> SourceF #

IsSourceF X # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toSourceF :: X -> SourceF #

BIF helpers

importBif0 :: Bif0 a => a -> Import #

Convert BIF to a normal import with zero arguments, which can be used with call and friends.

importBif1 :: Bif1 a => a -> Import #

Convert BIF to a normal import with one argument.

importBif2 :: Bif2 a => a -> Import #

Convert BIF to a normal import with two arguments.

importBif3 :: Bif3 a => a -> Import #

Convert BIF to a normal import with three arguments.

importBif4 :: Bif4 a => a -> Import #

Convert BIF to a normal import with four arguments.