Erlang Central

Difference between revisions of "Adding my own BIF"

From ErlangCentral Wiki

(8 intermediate revisions by one user not shown)
Line 1:Line 1:
0) run configure
+
===caveat===
 
+
unless you really know what you're doing, you'll be better off using a linked-in driver or a port.
1) add your bifs to erts/emulator/beam/bif.tab
+
===steps===
 
+
:1. run configure
e.g.
+
:2. add your bifs to erts/emulator/beam/bif.tab
{
+
<tt>
 
bif re:grep/2
 
bif re:grep/2
 
bif re:compile/1
 
bif re:compile/1
}
+
</tt>
2) create a C file
+
:3. create a C file
 
+
<tt>
e.g.
+
 
erts/emulator/beam/erl_bif_re.c
 
erts/emulator/beam/erl_bif_re.c
 
+
</tt>
3) add your C file to erts/emulator/<arch>/Makefile
+
:4. add your C file to erts/emulator/<arch>/Makefile
 
+
<tt>
e.g.
+
 
RUN_OBJS = $(OBJDIR)/erl_bif_re.o \
 
RUN_OBJS = $(OBJDIR)/erl_bif_re.o \
 
+
</tt>
4) implement your bifs by stealing bits from existing erl_bif_*.c files
+
:5. implement your bifs by stealing bits from existing erl_bif_*.c files
 
+
<pre>
e.g.
+
 
BIF_RETTYPE re_grep_2(BIF_ALIST_2){
 
BIF_RETTYPE re_grep_2(BIF_ALIST_2){
Eterm result;
+
  Eterm result;
result = magic_function();
+
  result = magic_function();  
BIF_RET(result);
+
  BIF_RET(result);
 
}
 
}
 +
</pre>
 +
:6. run make; make install
 +
===notes===
 +
*steps 0-3 need only be done once.
  
5) run make; make install
+
*note that if you add
 
+
<pre>
  steps 0-3 need only be done once.
+
 
+
  note that if you add
+
 
bif re:grep/2
 
bif re:grep/2
  to bif.tab
+
</pre>
there should be a erl_bif_re.c
+
to bif.tab there should be a  
  that implements
+
<tt>
 +
erl_bif_re.c
 +
</tt>
 +
that implements
 +
<pre>
 
BIF_RETTYPE re_grep_2(BIF_ALIST_2);
 
BIF_RETTYPE re_grep_2(BIF_ALIST_2);
 +
</pre>
 +
[[Category:HowTo]]

Revision as of 17:13, 31 July 2006

caveat

unless you really know what you're doing, you'll be better off using a linked-in driver or a port.

steps

1. run configure
2. add your bifs to erts/emulator/beam/bif.tab

bif re:grep/2 bif re:compile/1

3. create a C file

erts/emulator/beam/erl_bif_re.c

4. add your C file to erts/emulator/<arch>/Makefile

RUN_OBJS = $(OBJDIR)/erl_bif_re.o \

5. implement your bifs by stealing bits from existing erl_bif_*.c files
BIF_RETTYPE re_grep_2(BIF_ALIST_2){
  Eterm result;
  result = magic_function(); 
  BIF_RET(result);
}
6. run make; make install

notes

  • steps 0-3 need only be done once.
  • note that if you add
bif re:grep/2

to bif.tab there should be a erl_bif_re.c that implements

BIF_RETTYPE re_grep_2(BIF_ALIST_2);