codec-beam-0.1.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 # 

Methods

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

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

Ord X Source # 

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 # 

Methods

showsPrec :: Int -> X -> ShowS #

show :: X -> String #

showList :: [X] -> ShowS #

IsSourceF X Source # 

Methods

toSourceF :: X -> SourceF Source #

IsRegisterF X Source # 
IsSource X Source # 

Methods

toSource :: X -> Source Source #

IsRegister X Source # 

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 # 

Methods

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

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

Ord Y Source # 

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 # 

Methods

showsPrec :: Int -> Y -> ShowS #

show :: Y -> String #

showList :: [Y] -> ShowS #

IsSourceF Y Source # 

Methods

toSourceF :: Y -> SourceF Source #

IsRegisterF Y Source # 
IsSource Y Source # 

Methods

toSource :: Y -> Source Source #

IsRegister Y Source # 

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 # 

Methods

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

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

Ord F Source # 

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 # 

Methods

showsPrec :: Int -> F -> ShowS #

show :: F -> String #

showList :: [F] -> ShowS #

IsSourceF F Source # 

Methods

toSourceF :: F -> SourceF Source #

IsRegisterF F Source # 

data Nil Source #

The empty list.

Constructors

Nil 

Instances

Eq Nil Source # 

Methods

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

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

Ord Nil Source # 

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 # 

Methods

showsPrec :: Int -> Nil -> ShowS #

show :: Nil -> String #

showList :: [Nil] -> ShowS #

IsSource Nil Source # 

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 # 

Methods

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

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

Ord Label Source # 

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 # 

Methods

showsPrec :: Int -> Label -> ShowS #

show :: Label -> String #

showList :: [Label] -> ShowS #

data Lambda Source #

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

Constructors

Lambda 

Fields

data Import Source #

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

Argument constraints

data Register Source #

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

class IsRegister a where Source #

Minimal complete definition

toRegister

Methods

toRegister :: a -> Register Source #

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

class IsSource a where Source #

Minimal complete definition

toSource

Methods

toSource :: a -> Source Source #

data RegisterF Source #

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

data SourceF Source #

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

class IsSourceF a where Source #

Minimal complete definition

toSourceF

Methods

toSourceF :: a -> 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.