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 15, 2018
1 parent 2a96670 commit 36fdda2
Show file tree
Hide file tree
Showing 10 changed files with 261 additions and 11 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 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
17 changes: 16 additions & 1 deletion 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 @@ -404,6 +407,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 @@ -995,6 +1002,14 @@ testbugfix: all
ReadGapRoot( "tst/testbugfix.g" );' | $(TESTGAP) | \
tee `date -u +dev/log/testbugfix2_%Y-%m-%d-%H-%M` )

obj/libgap-test-0001.lo: $(top_srcdir)/tst/testlibgap/0001.c
$(QUIET_CC)$(COMPILE) $(DEPFLAGS) $(GAP_CFLAGS) $(CC_EXTRA_FLAGS) $(GAP_CPPFLAGS) -c $< -o $@

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

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
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
6 changes: 6 additions & 0 deletions src/gasman.h
Original file line number Diff line number Diff line change
Expand Up @@ -1050,4 +1050,10 @@ void *AllocateMemoryBlock(UInt size);
Int enableMemCheck(Char ** argv, void * dummy);
#endif

/*
* If not 0 this function will be called in
* CollectBags to allow users of libgap to mark bags
*/
extern TNumExtraMarkFuncBags ExtraMarkFuncBags;

#endif // GAP_GASMAN_H
127 changes: 127 additions & 0 deletions src/libgap-api.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// 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);
ExtraMarkFuncBags = 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;
}
84 changes: 84 additions & 0 deletions src/libgap-api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// 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

0 comments on commit 36fdda2

Please sign in to comment.