From 9ae6a3304357229bbafb82ac6c698596654c13fa Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Mon, 17 Aug 2009 11:53:56 -0400 Subject: [PATCH] c_src: gives c_src its own clibdir Signed-off-by: Jean Privat --- Makefile | 2 +- c_src/README | 5 ++ c_src/clib/nit_common.h | 146 +++++++++++++++++++++++++++++++++++++++++++++++ c_src/clib/nit_main.c | 80 ++++++++++++++++++++++++++ c_src/nitc._build.sh | 2 +- src/nc | 3 +- 6 files changed, 235 insertions(+), 3 deletions(-) create mode 100644 c_src/clib/nit_common.h create mode 100644 c_src/clib/nit_main.c diff --git a/Makefile b/Makefile index cfd9868..bb5e193 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,7 @@ bin/nitc: c_src/nitc src/parser/parser.nit mkdir .nit_compile 2> /dev/null || true cp c_src/*.o c_src/*.cksum .nit_compile src/git-gen-version.sh - c_src/nitc ${NITCOPT} -o bin/nitc -O src/nitc.nit + c_src/nitc ${NITCOPT} --clibdir c_src/clib -o bin/nitc -O src/nitc.nit bin/nitdoc: bin/nitc @echo '***************************************************************' diff --git a/c_src/README b/c_src/README index df47615..a29f198 100644 --- a/c_src/README +++ b/c_src/README @@ -1,2 +1,7 @@ This directory contains C files generated by nitc, the NIT compiler, of itself. They are used to boostrap nitc, the NIT compiler + +The directory 'clib' is the C library directory (option --clibdir) is needed both: + - to compile the C files of nitc in this directory + - by the nitc in this direcory to compile other programs + diff --git a/c_src/clib/nit_common.h b/c_src/clib/nit_common.h new file mode 100644 index 0000000..09561dd --- /dev/null +++ b/c_src/clib/nit_common.h @@ -0,0 +1,146 @@ +#ifndef NIT_COMMON_H +#define NIT_COMMON_H + +/* This file is part of NIT ( http://www.nitlanguage.org ). + * + * Copyright 2006-2009 Jean Privat + * + * This file is free software, which comes along with NIT. This software is + * distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. You can modify it is you want, provided this header + * is kept unaltered, and a notification of the changes is added. + * You are allowed to redistribute it and sell it, alone or is a part of + * another product. + */ + +#include +#include +#include + +#ifdef WITH_LIBGC +#include +#define malloc(x) GC_MALLOC((x)) +#define calloc(x,y) GC_MALLOC((x)*(y)) +#endif + + + +/* *** Types *** */ +typedef signed long int bigint; /* standard int value, must be larger that any poiner */ +typedef bigint (*fun_t) (bigint); /* generic function pointer */ +typedef bigint cid_t; /* class identifier */ +typedef bigint val_t; /* value (everything is a val_t) */ +typedef union obj_tu {union classtable_elt_tu * vft; val_t attr;} *obj_t; /* standard object */ +typedef union classtable_elt_tu { bigint i; fun_t f; cid_t cid;} classtable_elt_t; /* classtable element */ + +typedef classtable_elt_t * classtable_t; /* classtable */ + +/***************************************************************************** + * Types macros (primitive and less primitives) ****************************** + ***************************************************************************** + * + * ** types are: ** + * + * OBJ (obj_t) : standard object representation (including boxes + NIL) + * Int (int) : integers + * Char (car) : characters + * Bool (int) : booleans (true or false) + * + * X (x_t) : generic representatio of the previous four types + * + * ** macros are: ** + * + * int ISX(val_t v) : true if v is a X + * x_t VAL2X(val_t v) : convert a val_t to a primitive type (you should check ISX before) + * val_t X2VAL(x_t x) : convert a type to a val_t + * int XTAG : numeric identifier of a type + * int TAG(val_t v) : return the XTAG (ie TAG(Char2VAL('e')) == CharTag) + * + * classtable_t VAL2VFT(val_t v): the virtual function table of a value + * + * val_t NIT_NULL : the instance of the None class + *****************************************************************************/ + +#define TAG(x) ((int)(x) & 3) +#ifndef IntTAG +# define IntTAG 1 +#endif +#define TAG_Int(x) ((val_t)(((x)<<2)|IntTAG)) +#define UNTAG_Int(x) ((bigint)(x)>>2) +#ifndef CharTAG +# define CharTAG 2 +#endif +#define TAG_Char(x) ((val_t)((((int)(x))<<2)|CharTAG)) +#define UNTAG_Char(x) ((char)((int)(x)>>2)) +#ifndef BoolTAG +# define BoolTAG 3 +#endif +#define TAG_Bool(x) ((val_t)(((x)<<2)|BoolTAG)) +#define UNTAG_Bool(x) ((int)(x)>>2) +#ifndef OBJTAG +# define OBJTAG 0 +#endif + +#define ISOBJ(x) (TAG((x)) == OBJTAG) +#define VAL2OBJ(x) ((obj_t)(x)) +#define OBJ2VAL(o) ((val_t)(o)) +#define VAL2VFT(x) (ISOBJ(x) ? VAL2OBJ(x)->vft : TAG2VFT[TAG(x)]) +/*#define VAL2CID(x) (ISOBJ(x) ? (VAL2OBJ(x)->vft->cid) : (-TAG(x)))*/ +#define VAL2CID(x) (VAL2OBJ(x)->vft->cid) + +#define NIT_NULL ((val_t)0) +#define ISNULL(x) ((x)==NIT_NULL) + +/* Equal comparaison */ +/* O = Non nil object ; N = Object or nil ; P = Primitive */ +#define OBJ_IS_BOX(x) ((VAL2OBJ(x)->vft->i) < 0) +#define IS_BOX(x) (ISOBJ(x) && OBJ_IS_BOX(x)) +#define IS_EQUAL_BOX(x, y) (ISOBJ(y) && (VAL2OBJ(x)[1].vft==VAL2OBJ(y)[1].vft) && (VAL2OBJ(x)->vft==VAL2OBJ(y)->vft)) +#define IS_EQUAL_OO(x, y) ((x)==(y) || (IS_BOX(x) && IS_EQUAL_BOX((x), (y)))) +#define IS_EQUAL_ON(x, y) ((x)==(y) || (IS_BOX(x) && !ISNULL(y) && IS_EQUAL_BOX((x), (y)))) +#define IS_EQUAL_NN(x, y) ((x)==(y) || (!ISNULL(x) && IS_BOX(x) && !ISNULL(y) && IS_EQUAL_BOX((x), (y)))) + +/* Subclass comparaison */ +/* check is e is a subtype of c2 */ +/* Warning, only subclasse comparaison is performed, not subtype */ +/* e is a val (not nil) */ +/* c a class name (eg: Int) */ +#define VALISA(e, c) (VAL2VFT(e)[COLOR_ ## c].cid == (cid_t) ID_ ## c) +#define OBJISA(e, c) (VAL2OBJ(e)->vft[COLOR_ ## c].cid == (cid_t) ID_ ## c) + +#define VAL_ISA(e, c, i) (VAL2VFT((e))[(c)].cid == (cid_t)(i)) + +void * alloc(size_t); +extern val_t G_args; +extern val_t G_stdin; +extern val_t G_stdout; +extern val_t G_stderr; +extern val_t G_sys; + +extern int glob_argc; +extern char ** glob_argv; + +struct trace_t { + struct trace_t *prev; /* previous stack frame */ + const char *file; /* source filename */ + int line; /* line number */ + const char *meth; /* method name */ +}; +extern struct trace_t *tracehead; +typedef enum {true = (1==1),false = (0==1)} bool; + +void nit_exit(int); + +#define CALL(r,c) ((VAL2VFT(r)[c].f)) +#define ATTR(r,c) (*(val_t*)(VAL2OBJ(r)+c)) +#define ATTRS(r,c,o) ((VAL2OBJ(r)+VAL2VFT(r)[c].i)[o].attr) + +void prepare_signals(void); +extern classtable_t TAG2VFT[4]; + +/* This structure is used to store closure. + * Specific closure use a specific fun parameter. + */ +struct WBT_ {fun_t fun; val_t *has_broke; val_t broke_value; val_t *variable; struct WBT_ **closurevariable;}; +#endif diff --git a/c_src/clib/nit_main.c b/c_src/clib/nit_main.c new file mode 100644 index 0000000..5ad9df4 --- /dev/null +++ b/c_src/clib/nit_main.c @@ -0,0 +1,80 @@ +/* This file is part of NIT ( http://www.nitlanguage.org ). + * + * Copyright 2006-2009 Jean Privat + * + * This file is free software, which comes along with NIT. This software is + * distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. You can modify it is you want, provided this header + * is kept unaltered, and a notification of the changes is added. + * You are allowed to redistribute it and sell it, alone or is a part of + * another product. + */ + +#include "nit_common.h" +#include +#include + +#ifdef WITH_LIBGC +#define GC_DEBUG +#include +int nolibgc = 0; +void *simple_alloc(size_t s0); +void *alloc(size_t s0) +{ + if (!nolibgc) { return GC_MALLOC(s0); } + else return simple_alloc(s0); +} +#define alloc simple_alloc +#endif + +void * alloc(size_t s0) +{ + static char * alloc_pos = NULL; + static size_t alloc_size = 0; + void * res; + size_t s = ((s0+3)/4)*4; + if(alloc_size < s) { + alloc_size = s + 1024*1024; + alloc_pos = (char *)calloc(alloc_size, 1); + } + res = alloc_pos; + alloc_size -= s; + alloc_pos += s; + return res; +} + +int glob_argc; +char **glob_argv; +val_t G_sys; + +void exithandler(int s) { + fprintf(stderr, "Recieved signal %d\n", s); + nit_exit(1); +} +void prepare_signals(void) { +#if WITH_LIBGC + nolibgc = (getenv("NIT_NOLIBGC") != NULL); + if (!nolibgc) { + GC_INIT(); + } +#endif + signal(SIGINT,exithandler); + signal(SIGABRT,exithandler); + signal(SIGSEGV,exithandler); + signal(SIGILL, exithandler); + signal(SIGFPE, exithandler); + signal(SIGTERM,exithandler); + signal(SIGBUS, exithandler); +} +struct trace_t *tracehead = NULL; +void nit_exit(int i) { + fprintf(stderr, ",---- Stack trace -- - - -\n"); + while(tracehead != NULL) { + fprintf(stderr, "| %s (%s:%d)\n", tracehead->meth, tracehead->file, tracehead->line); + if (tracehead == tracehead->prev) break; + tracehead = tracehead->prev; + } + fprintf(stderr, "`------------------- - - -\n"); + exit(i); +} diff --git a/c_src/nitc._build.sh b/c_src/nitc._build.sh index 8057716..36eb7db 100755 --- a/c_src/nitc._build.sh +++ b/c_src/nitc._build.sh @@ -1,6 +1,6 @@ #!/bin/sh # This shell script is generated by NIT to compile the program nitc. -CLIBDIR="./../lib" +CLIBDIR="clib" ./../bin/gccx -d . -I $CLIBDIR -I ./../lib/standard/ -o nitc "$@" \ $CLIBDIR/nit_main.c\ ./abstracttool._sep.c\ diff --git a/src/nc b/src/nc index 80e181a..c6845f6 100755 --- a/src/nc +++ b/src/nc @@ -17,10 +17,11 @@ NITC="../c_src/nitc" NITCOPTS= #"-I parser -I metamodel -I syntax -I compiling -I tmp" +CSRC=$( (cd ../c_src; pwd) ) name=`basename $0` case $name in - nc) opts="";; + nc) opts="--clibdir ../c_src/clib";; nc2) opts="-p 2"; NITC="./nitc";; nc3) opts="-p 3"; NITC="./nitc_2";; nc4) opts="-p 4"; NITC="./nitc_3";; -- 1.7.9.5