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 Source #

Arguments

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

module name

-> f1 Metadata 
-> f2 Op

instructions

-> ByteString 

Create code for a BEAM module.

data Metadata Source #

Extra information regarding the contents of a BEAM module.

export :: Text -> Int -> Metadata Source #

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

insertModuleInfo :: Metadata Source #

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 Source #

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 Source #

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

Constructors

X Int 
Instances
Eq X Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

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

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

Ord X Source # 
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 Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

showsPrec :: Int -> X -> ShowS #

show :: X -> String #

showList :: [X] -> ShowS #

IsSourceF X Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toSourceF :: X -> SourceF Source #

IsRegisterF X Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

IsSource X Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toSource :: X -> Source Source #

IsRegister X Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

newtype Y Source #

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 Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

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

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

Ord Y Source # 
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 Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

showsPrec :: Int -> Y -> ShowS #

show :: Y -> String #

showList :: [Y] -> ShowS #

IsSourceF Y Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toSourceF :: Y -> SourceF Source #

IsRegisterF Y Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

IsSource Y Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toSource :: Y -> Source Source #

IsRegister Y Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

newtype F Source #

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

Constructors

F Int 
Instances
Eq F Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

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

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

Ord F Source # 
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 Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

showsPrec :: Int -> F -> ShowS #

show :: F -> String #

showList :: [F] -> ShowS #

IsSourceF F Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toSourceF :: F -> SourceF Source #

IsRegisterF F Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

data Nil Source #

The empty list.

Constructors

Nil 
Instances
Eq Nil Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

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

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

Ord Nil Source # 
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 Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

showsPrec :: Int -> Nil -> ShowS #

show :: Nil -> String #

showList :: [Nil] -> ShowS #

IsSource Nil Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toSource :: Nil -> Source Source #

newtype Label Source #

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 Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

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

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

Ord Label Source # 
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 Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

showsPrec :: Int -> Label -> ShowS #

show :: Label -> String #

showList :: [Label] -> ShowS #

data Literal Source #

Erlang literals, stored on the heap.

Instances
Eq Literal Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

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

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

Ord Literal Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Show Literal Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

IsSourceF Literal Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

IsSource Literal Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

data Lambda Source #

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

Constructors

Lambda 

Fields

Instances
Eq Lambda Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

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

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

Ord Lambda Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Show Lambda Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

data Import Source #

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

Instances
Eq Import Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

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

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

Ord Import Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Show Import Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Argument constraints

data Register Source #

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

Instances
IsRegister Register Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

class IsRegister a where Source #

Minimal complete definition

toRegister

Methods

toRegister :: a -> Register Source #

Instances
IsRegister Register Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

IsRegister Y Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

IsRegister X Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

data Source 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 Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

class IsSource a where Source #

Minimal complete definition

toSource

Methods

toSource :: a -> Source Source #

Instances
IsSource Int Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toSource :: Int -> Source Source #

IsSource Text Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toSource :: Text -> Source Source #

IsSource Source Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

IsSource Literal Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

IsSource Nil Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toSource :: Nil -> Source Source #

IsSource Y Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toSource :: Y -> Source Source #

IsSource X Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toSource :: X -> Source Source #

data RegisterF Source #

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

Instances
IsRegisterF RegisterF Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

class IsRegisterF a where Source #

Minimal complete definition

toRegisterF

Methods

toRegisterF :: a -> RegisterF Source #

Instances
IsRegisterF RegisterF Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

IsRegisterF F Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

IsRegisterF Y Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

IsRegisterF X Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

data SourceF Source #

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

Instances
IsSourceF SourceF Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

class IsSourceF a where Source #

Minimal complete definition

toSourceF

Methods

toSourceF :: a -> SourceF Source #

Instances
IsSourceF SourceF Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

IsSourceF Literal Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

IsSourceF F Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toSourceF :: F -> SourceF Source #

IsSourceF Y Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toSourceF :: Y -> SourceF Source #

IsSourceF X Source # 
Instance details

Defined in Codec.Beam.Internal.Syntax

Methods

toSourceF :: X -> SourceF Source #

BIF helpers

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

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

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

Convert BIF to a normal import with one argument.

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

Convert BIF to a normal import with two arguments.

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

Convert BIF to a normal import with three arguments.

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

Convert BIF to a normal import with four arguments.