Skip to content

Commit

Permalink
Add GAP kernel API (aka libgap)
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Pfeiffer committed Aug 16, 2018
1 parent 01e44a1 commit 3f7dd1c
Show file tree
Hide file tree
Showing 13 changed files with 359 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,6 @@ doc/gapmacrodoc.idx
/hpcgap/ward

/builds/

/libgap.la
/.libs
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ matrix:
# test error reporting and compiling (quickest job in this test suite)
- env: TEST_SUITES="testspecial test-compile"

# test libgap
- env: TEST_SUITES="testlibgap" CONFIGFLAGS="--enable-debug --enable-Werror --enable-libgap"

script:
- gcov --version
- bash etc/ci-prepare.sh && bash etc/ci.sh
Expand Down
3 changes: 3 additions & 0 deletions GNUmakefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ ADDGUARDS2 = @ADDGUARDS2@
# garbage collector source files
GC_SOURCES = @GC_SOURCES@

# Dynamic library
BUILD_LIBGAP = @BUILD_LIBGAP@

# compatibility mode
COMPAT_MODE = @COMPAT_MODE@
GAPARCH = @GAPARCH@
Expand Down
16 changes: 14 additions & 2 deletions Makefile.rules
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
all: gap$(EXEEXT) gac
.PHONY: all

libgap: libgap.la
libgap: libgap.la sysinfo.gap symlinks
.PHONY: libgap

# Backwards compatibility: add "default" target as alias for "all"
Expand Down Expand Up @@ -64,6 +64,9 @@ SOURCES += src/intfuncs.c
SOURCES += src/intrprtr.c
SOURCES += src/io.c
SOURCES += src/iostream.c
ifeq ($(BUILD_LIBGAP),yes)
SOURCES += src/libgap-api.c
endif
SOURCES += src/listfunc.c
SOURCES += src/listoper.c
SOURCES += src/lists.c
Expand Down Expand Up @@ -121,7 +124,6 @@ ifeq ($(HPCGAP),yes)
SOURCES += src/hpc/traverse.c
endif


########################################################################
# Preprocessor flags
#
Expand Down Expand Up @@ -395,6 +397,10 @@ gap$(EXEEXT): $(OBJS) cnf/GAP-LDFLAGS cnf/GAP-LIBS cnf/GAP-OBJS

endif

ifeq ($(BUILD_LIBGAP),yes)
libgap.so: $(OBJS)
$(QUIET_LINK)$(LINK) $(GAP_LDFLAGS) -shared $(OBJS) $(GAP_LIBS) -o $@
endif

########################################################################
# The "docomp" target regenerates the various src/c_*.c files, and
Expand Down Expand Up @@ -986,6 +992,12 @@ testbugfix: all
ReadGapRoot( "tst/testbugfix.g" );' | $(TESTGAP) | \
tee `date -u +dev/log/testbugfix2_%Y-%m-%d-%H-%M` )

testlibgap: libgap.la obj/tst/testlibgap/basic.lo
$(QUIET_LINK)$(LINK) obj/tst/testlibgap/basic.lo libgap.la -o test-libgap
./test-libgap -l $(top_srcdir) -m 32m -q -T > basic.out
diff $(top_srcdir)/tst/testlibgap/basic.expect basic.out
.PHONY: testlibgap

coverage:
gcov -o . $(SOURCES)

Expand Down
12 changes: 12 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,18 @@ AS_CASE([$with_gc],
)
AC_MSG_RESULT([$with_gc])

dnl
dnl User setting: dynamic library mode (off by default)
dnl
AC_ARG_ENABLE([libgap],
[AS_HELP_STRING([--enable-libgap], [enable building of dynamic library])],
[AC_DEFINE([BUILD_LIBGAP], [1], [define if building libgap])],
[enable_libgap=no]
)
AC_MSG_CHECKING([whether to enable dynamic library mode])
AC_MSG_RESULT([$enable_libgap])
AC_SUBST([BUILD_LIBGAP], [$enable_libgap])

dnl
dnl User setting: Debug mode (off by default)
dnl
Expand Down
4 changes: 4 additions & 0 deletions etc/ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ GAPInput

;;

testlibgap)
make testlibgap
;;

*)
if [[ ! -f $SRCDIR/tst/${TEST_SUITE}.g ]]
then
Expand Down
5 changes: 3 additions & 2 deletions lib/init.g
Original file line number Diff line number Diff line change
Expand Up @@ -1042,11 +1042,12 @@ if IsLIBGAP then
elif IsHPCGAP and THREAD_UI() then
ReadLib("hpc/consoleui.g");
MULTI_SESSION();
else
PROGRAM_CLEAN_UP();
elif not IsLIBGAP then
SESSION();
PROGRAM_CLEAN_UP();
fi;

PROGRAM_CLEAN_UP();


#############################################################################
Expand Down
8 changes: 0 additions & 8 deletions lib/streams.gi
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,6 @@ function( stream )
CloseStream(stream);
end );

BindGlobal("LIBGAP_EvalString",
function(string)
local instream, obj;
instream := InputTextString(string);
obj := READ_ALL_COMMANDS(instream, 0, false);
return obj;
end);


#############################################################################
##
Expand Down
7 changes: 7 additions & 0 deletions src/gap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1689,6 +1689,13 @@ static Int InitLibrary (
AssConstantGVar( GVarName( "IsHPCGAP" ), False );
#endif

#if defined(BUILD_LIBGAP)
AssConstantGVar( GVarName( "IsLIBGAP" ), True );
#else
AssConstantGVar( GVarName( "IsLIBGAP" ), False );
#endif


/* return success */
return PostRestore( module );
}
Expand Down
134 changes: 134 additions & 0 deletions src/libgap-api.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// LibGAP API - API for using GAP as shared library.

#include "libgap-api.h"

#include "bool.h"
#include "calls.h"
#include "gapstate.h"
#include "gvars.h"
#include "intobj.h"
#include "opers.h"
#include "plist.h"
#include "stringobj.h"

Obj FuncREAD_ALL_COMMANDS(
Obj self, Obj instream, Obj echo, Obj capture, Obj resultCallback);

// Setup and initialisation

void GAP_Initialize(int argc,
char ** argv,
char ** env,
CallbackFunc markBagsCallback,
CallbackFunc errorCallback)
{
InitializeGap(&argc, argv, env);
SetExtraMarkFuncBags(markBagsCallback);
STATE(JumpToCatchCallback) = errorCallback;
}

void GAP_Finalize(void)
{
FinishBags();
}


/*************************************************************************/
/*** Integer *************************************************************/
/*************************************************************************/

Obj GAP_IntObj_Int(Int value)
{
return INTOBJ_INT(value);
}

Int GAP_Int_IntObj(Obj value)
{
return INT_INTOBJ(value);
}

/*************************************************************************/
/*** Strings *************************************************************/
/*************************************************************************/

char * GAP_CSTR_STRING(Obj str)
{
return CSTR_STRING(str);
}

Obj GAP_MakeString(char * str)
{
return MakeString(str);
}

/*************************************************************************/
/*** List ****************************************************************/
/*************************************************************************/

Obj GAP_NewPList(UInt length)
{
return NEW_PLIST(T_PLIST, length);
}

void GAP_SetLenPList(Obj list, Int len)
{
SET_LEN_PLIST(list, len);
}

void GAP_AssPList(Obj list, Int pos, Obj elem)
{
AssPlist(list, pos, elem);
}

void GAP_SetElmPList(Obj list, Int pos, Obj elem)
{
SET_ELM_PLIST(list, pos, elem);
}

Obj GAP_ElmPList(Obj list, Int pos)
{
return ELM_PLIST(list, pos);
}

Int GAP_LenPList(Obj list)
{
return LEN_PLIST(list);
}

/*************************************************************************/
/*** GVars ***************************************************************/
/*************************************************************************/

Obj GAP_ValGVar(const char * name)
{
UInt gvar = GVarName(name);
if (gvar != 0) {
return ValGVar(gvar);
}
else {
return NULL;
}
}

/*************************************************************************/
/*** Functions ***********************************************************/
/*************************************************************************/

Obj GAP_CallFuncList(Obj func, Obj arg_list)
{
return CallFuncList(func, arg_list);
}

Obj GAP_EvalString(const char * cmd)
{
Obj instream;
Obj res;
Obj viewObjFunc, streamFunc;

streamFunc = GAP_ValGVar("InputTextString");
viewObjFunc = GAP_ValGVar("ViewObj");

instream = DoOperation1Args(streamFunc, MakeString(cmd));
res = FuncREAD_ALL_COMMANDS(0L, instream, False, True, viewObjFunc);
return res;
}
85 changes: 85 additions & 0 deletions src/libgap-api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// LibGAP API - API for using GAP as shared library.
// Most functions defined here are just wrappers of
// internal GAP kernel functions.

// The corresponding kernel functions are written before the functions
// declarations. Please see their documentation for more details.

#ifndef LIBGAP_API_H
#define LIBGAP_API_H

#include "gap.h"

typedef void (*CallbackFunc)(void);

// Initialisation and finalization
void GAP_Initialize(int argc,
char ** argv,
char ** env,
CallbackFunc markBagsCallback,
CallbackFunc errorCallback);

void GAP_Finalize(void);

/*************************************************************************/
/*** Integer *************************************************************/
/*************************************************************************/

// INTOBJ_INT
Obj GAP_IntObj_Int(Int);

// INT_INTOBJ
Int GAP_Int_IntObj(Obj);

/*************************************************************************/
/*** Strings *************************************************************/
/*************************************************************************/

// CSTR_STRING
char * GAP_CSTR_STRING(Obj);

// MakeString
Obj GAP_MakeString(char *);

/*************************************************************************/
/*** List ****************************************************************/
/*************************************************************************/

// NEW_PLIST( T_PLIST, length )
Obj GAP_NewPList(UInt length);

// SET_LEN_PLIST
void GAP_SetLenPList(Obj, Int);

// AssPlist
void GAP_AssPList(Obj list, Int pos, Obj elem);

// SET_ELM_PLIST
void GAP_SetElmPList(Obj, Int, Obj);

// ELM_PLIST
Obj GAP_ElmPList(Obj, Int);

// LEN_PLIST
Int GAP_LenPList(Obj);

/*************************************************************************/
/*** GVars ***************************************************************/
/*************************************************************************/

// Combines GVarName and ValGVar. For a given string, it returns the value
// of the gvar with name <name>, or NULL if the global variable is not
// defined.
Obj GAP_ValGVar(const char * name);

/*************************************************************************/
/*** Functions ***********************************************************/
/*************************************************************************/

// CallFuncList
Obj GAP_CallFuncList(Obj, Obj);


Obj GAP_EvalString(const char * cmd);

#endif
Loading

0 comments on commit 3f7dd1c

Please sign in to comment.