Adding Builtins to SB-Prolog

Adding a builtin involves writing the C code for the desired case and installing it into the simulator. The files in the directory sim/builtin contain the C code for the builtin predicates supported by the system. The following procedure is to be followed when adding a builtin to the system:

  1. Installing C Code:
    1. Go to the directory sim/builtin.
    2. Look at the #defines in the file builtin.h, and choose a number N1 (between 0 and 255) which is not in use to be the builtin number for the new builtin.
    3. Add to the file builtin.h the line
      
       #define NEWBUILTIN N1
      
    4. The convention is that the code for builtin will be in a parameterless procedure named b_NEWBUILTIN. Modify the file init_branch.c in the directory sim/builtin by adding these lines:
      
       extern int b_NEWBUILTIN();
      
      and
      
       set_b_inst ( NEWBUILTIN, b_NEWBUILTIN );
      
      in the appropriate places.
    5. The builtins are compiled together into one object file, builtin. Update the file Makefile by appending the name of your object code file at the end of the line ``OBJS = …'' and insert the appropriate commands to compile your C source file, e.g.:
      
      OBJS = [ … other file names … ] newbuiltin.o 
      
      $\vdots$
      newbuiltin.o: $(HS)
      cc $(CFLAGS) newbuiltin.c

    6. Execute the updated make file to create an updated object file builtin.

    7. Go to the directory sim and execute make to install the new file builtin.

  2. Installing Prolog Code:

    Assume that the builtin predicate to be added is newbuiltin/4. The procedure for installing the Prolog code for this is as follows:

    1. Go to the SB-Prolog system directory lib/src, where the Prolog source for the library routines is kept.
    2. Each builtin definition is of the form
      pred( … ) :- '_$builtin'(N).
      where N is an integer, the builtin number of pred.
    3. Create a Prolog source file newbuiltin.P (notice correspondence with the name of the predicate being defined) containing the definition
      newbuiltin(A,B,C,D) :– '_$builtin'(N1).
      where N1 is the builtin number of the predicate newbuiltin, obtained when installing the C code for the builtin (see above).
    4. Compile this Prolog predicate, using the simulator and the compile predicate, into a file newbuiltin (notice correspondence with the name of the predicate being defined) in the SB-Prolog directory lib.