Commit de1b0c8d authored by Matthias Braun's avatar Matthias Braun
Browse files

add obstack and libcore to libfirm

[r17711]
parent 0148115b
#!/bin/sh
invoke() {
echo "$@"
$@ || exit $?
}
# bison/flex make me crazy, all versions differ in subtle ways making it nearly
# impossible to distribute .l/.y files and integrate them into the build system
# so that they work everywhere. (All this ylwrap hackery from the automake guys
# just failed for me again). So now we do it differently: We distribute the
# generated C code and only people that actually change the .y and .l files
# invoke this script manually!
FLEX_FLAGS=
# we use -l for win32 compilers
BISON_FLAGS="-l -d"
invoke bison $BISON_FLAGS -o lc_config_parser.c lc_config_parser.y
invoke flex $FLEX_FLAGS -o lc_config_lexer.c lc_config_lexer.l
/*
libcore: library for basic data structures and algorithms.
Copyright (C) 2005 IPD Goos, Universit"at Karlsruhe, Germany
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "lc_common_t.h"
#include "lc_defines.h"
#include "lc_printf.h"
/* Default appendable implementations */
int lc_appendable_snwadd(lc_appendable_t *app, const char *str, size_t len,
unsigned int width, int left_just, char pad)
{
int res = 0;
int i;
int to_pad = width - len;
/* If not left justified, pad left */
for(i = 0; !left_just && i < to_pad; ++i)
res += lc_appendable_chadd(app, pad);
/* Send the visible portion of the string to the output. */
res += lc_appendable_snadd(app, str, len);
/* If left justified, pad right. */
for(i = 0; left_just && i < to_pad; ++i)
res += lc_appendable_chadd(app, pad);
return res;
}
void lc_appendable_init(lc_appendable_t *env, const lc_appendable_funcs_t *app,
void *obj, size_t limit)
{
env->obj = obj;
env->limit = limit;
env->written = 0;
env->app =app;
app->init(env);
}
static void default_init(UNUSED(lc_appendable_t *env))
{
}
static void default_finish(UNUSED(lc_appendable_t *env))
{
}
/*
* File appendable.
*/
static int file_snadd(lc_appendable_t *obj, const char *str, size_t n)
{
obj->written += n;
fwrite(str, sizeof(char), n, obj->obj);
return n;
}
static int file_chadd(lc_appendable_t *obj, int ch)
{
fputc(ch, obj->obj);
obj->written++;
return 1;
}
static lc_appendable_funcs_t app_file = {
default_init,
default_finish,
file_snadd,
file_chadd
};
const lc_appendable_funcs_t *lc_appendable_file = &app_file;
/*
* String appendable.
*/
static void str_init(lc_appendable_t *obj)
{
strncpy(obj->obj, "", obj->limit);
}
static int str_snadd(lc_appendable_t *obj, const char *str, size_t n)
{
size_t to_write = LC_MIN(obj->limit - obj->written - 1, n);
char *tgt = obj->obj;
strncpy(tgt + obj->written, str, to_write);
obj->written += to_write;
return to_write;
}
static int str_chadd(lc_appendable_t *obj, int ch)
{
if(obj->limit - obj->written > 1) {
char *tgt = obj->obj;
tgt[obj->written++] = (char) ch;
return 1;
}
return 0;
}
static void str_finish(lc_appendable_t *obj)
{
char *str = obj->obj;
str[obj->written] = '\0';
}
static lc_appendable_funcs_t app_string = {
str_init,
str_finish,
str_snadd,
str_chadd
};
const lc_appendable_funcs_t *lc_appendable_string = &app_string;
/*
* Obstack appendable
*/
static int obst_snadd(lc_appendable_t *obj, const char *str, size_t n)
{
struct obstack *obst = obj->obj;
obj->written += n;
obstack_grow(obst, str, n);
return n;
}
static int obst_chadd(lc_appendable_t *obj, int ch)
{
struct obstack *obst = obj->obj;
obstack_1grow(obst, (char) ch);
obj->written++;
return 1;
}
static lc_appendable_funcs_t app_obstack = {
default_init,
default_finish,
obst_snadd,
obst_chadd
};
const lc_appendable_funcs_t *lc_appendable_obstack = &app_obstack;
/*
libcore: library for basic data structures and algorithms.
Copyright (C) 2005 IPD Goos, Universit"at Karlsruhe, Germany
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* Something to which can be appended.
* @author Sebastian Hack
* @date 3.1.2005
*/
#ifndef _LIBCORE_APPENDABLE_H
#define _LIBCORE_APPENDABLE_H
#include <stddef.h>
struct _lc_appendable_funcs_t;
typedef struct _lc_appendable_t {
void *obj;
size_t written;
size_t limit;
const struct _lc_appendable_funcs_t *app;
} lc_appendable_t;
typedef struct _lc_appendable_funcs_t {
void (*init)(lc_appendable_t *obj);
void (*finish)(lc_appendable_t *obj);
int (*snadd)(lc_appendable_t *obj, const char *str, size_t len);
int (*chadd)(lc_appendable_t *obj, int ch);
} lc_appendable_funcs_t;
#define lc_appendable_snadd(obj,str,len) ((obj)->app->snadd(obj, str, len))
#define lc_appendable_chadd(obj,ch) ((obj)->app->chadd(obj, ch))
#define lc_appendable_finish(obj) ((obj)->app->finish(obj))
extern void lc_appendable_init(lc_appendable_t *app, const lc_appendable_funcs_t *funcs,
void *obj, size_t limit);
extern int lc_appendable_snwadd(lc_appendable_t *app, const char *str,
size_t len, unsigned int width, int left_just, char pad);
extern const lc_appendable_funcs_t *lc_appendable_file;
extern const lc_appendable_funcs_t *lc_appendable_string;
extern const lc_appendable_funcs_t *lc_appendable_obstack;
#endif
/*
libcore: library for basic data structures and algorithms.
Copyright (C) 2005 IPD Goos, Universit"at Karlsruhe, Germany
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _COMMON_T_H
#define _COMMON_T_H
#include <obstack.h>
#include <stdarg.h>
#include <stdio.h>
#define obstack_chunk_alloc malloc
#define obstack_chunk_free free
#define bcopy(src,dest,n) memcpy(dest,src,n)
#include <libcore/lc_config.h>
#define FUNCNAME LC_FUNCNAME
#define UNUSED(x) LC_UNUSED(x)
#define LONGLONG long /* LC_LONGLONG */
#define LONGDOUBLE double /* LC_LONGDOUBLE */
#ifdef LC_HAVE_C99
#define HAVE_C99 LC_HAVE_C99
#else /* LC_HAVE_C99 */
#ifdef _WIN32
/* Windows names for non-POSIX calls */
#define snprintf _snprintf
#define vsnprintf _vsnprintf
#endif /* WIN32 */
/* These both are not posix or ansi c but almost everywhere existent */
/* Daniel: Why not just include stdio.h?
extern int snprintf(char *buf, size_t size, const char *fmt, ...);
extern int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
*/
#endif /* LC_HAVE_C99 */
#endif /* _COMMON_T_H */
/*
libcore: library for basic data structures and algorithms.
Copyright (C) 2005 IPD Goos, Universit"at Karlsruhe, Germany
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* Central definitions for the libcore.
* @author Sebastian Hack
* @date 22.12.2004
*/
#ifndef _LC_CONFIG_H
#define _LC_CONFIG_H
#if defined(__STD_VERSION__) && __STD_VERSION >= 199901L
#define LC_HAVE_C99 1
#endif
/* ISO C99 Standard stuff */
#ifdef LC_HAVE_C99
#define LC_INLINE inline
#define LC_FUNCNAME __func__
#define LC_UNUSED(x) x
#define LC_LONGLONG long long
#define LC_LONGDOUBLE long double
/* definitions using GCC */
#elif defined(__GNUC__)
#define LC_INLINE __inline__
#define LC_FUNCNAME __FUNCTION__
#define LC_UNUSED(x) x __attribute__((__unused__))
#ifdef __STRICT_ANSI__
#define LC_LONGLONG long
#define LC_LONGDOUBLE double
#else
#define LC_LONGLONG long long
#define LC_LONGDOUBLE long double
#endif
#elif defined(_MSC_VER)
#define LC_INLINE __inline
#define LC_FUNCNAME "<unknown>"
#define LC_UNUSED(x) x
#define LC_LONGLONG __int64
#define LC_LONGDOUBLE long double
/* disable warning: 'foo' was declared deprecated, use 'bla' instead */
/* of course MS had to make 'bla' incompatible to 'foo', so a simple */
/* define will not work :-((( */
#pragma warning( disable : 4996 )
#ifdef _WIN32
#define snprintf _snprintf
#endif /* _WIN32 */
#if _MSC_VER <= 1200
typedef __int16 int16;
typedef __int32 int32;
typedef __int64 int64;
typedef unsigned __int16 uint16;
typedef unsigned __int32 uint32;
typedef unsigned __int64 uint64;
#endif /* _MSC_VER <= 1200 */
/* default definitions */
#else /* defined(_MSC_VER) */
#define LC_INLINE
#define LC_FUNCNAME "<unknown>"
#define LC_UNUSED(x)
#define LC_LONGLONG long
#define LC_LONGDOUBLE double
#endif
#endif /* _LC_CONFIG_H */
This diff is collapsed.
%option prefix="_lc_opt_"
%{
/*
libcore: library for basic data structures and algorithms.
Copyright (C) 2005 IPD Goos, Universit"at Karlsruhe, Germany
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include "lc_parser_t.h"
#include "lc_config_parser.h"
static int _lc_opt_wrap(void)
{
return 1;
}
%}
num [0-9]+
hexnum 0x[0-9a-fA-F]+
idcharsbegin [a-zA-Z$_]
idchars ({idcharsbegin}|[0-9])
ident {idcharsbegin}{idchars}*
ws [\t ]*
nl \n
%x LINE_COMMENT
%x BIG_COMMENT
%x DAT
%x DAT_CONT
%x STR
%%
"/*" { BEGIN(BIG_COMMENT); }
<BIG_COMMENT>"*/" { BEGIN(INITIAL); }
<BIG_COMMENT>\n PMANGLE(linenr)++;
<BIG_COMMENT>. ;
("#"|"//") { BEGIN(LINE_COMMENT); }
<LINE_COMMENT>\n { BEGIN(INITIAL); PMANGLE(linenr)++; }
<LINE_COMMENT>. ;
<INITIAL>[=:]{ws} { BEGIN(DAT); }
<DAT>\" { BEGIN(STR); }
<DAT>\\ { BEGIN(DAT_CONT); }
<DAT>. { _lc_opt_add_to_data_char(PMANGLE(text)[0]); }
<DAT>\n {
BEGIN(INITIAL);
PMANGLE(linenr)++;
_lc_opt_add_to_data_char('\0');
return DATA;
}
<DAT_CONT>\n { BEGIN(DAT); PMANGLE(linenr)++; }
<DAT_CONT>. ;
<STR>\" {
BEGIN(INITIAL);
_lc_opt_add_to_data_char('\0');
return DATA;
}
<STR>\\n _lc_opt_add_to_data_char('\n');
<STR>\\r _lc_opt_add_to_data_char('\r');
<STR>\\t _lc_opt_add_to_data_char('\t');
<STR>\\b _lc_opt_add_to_data_char('\b');
<STR>\\f _lc_opt_add_to_data_char('\f');
<STR>\\. _lc_opt_add_to_data_char(yytext[1]);
<STR>[^\"] _lc_opt_add_to_data_char(yytext[0]);
{ident} {
PMANGLE(lval).text.str = PMANGLE(text);
PMANGLE(lval).text.len = PMANGLE(leng);
return IDENT;
}
[/.] { return SEP; }
{ws} ;
{nl} PMANGLE(linenr)++;
. return yytext[0];
%%
This diff is collapsed.
/* A Bison parser, made by GNU Bison 2.3. */
/* Skeleton interface for Bison's Yacc-like parsers in C
Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002, 2003, 2004, 2005, 2006
Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program 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. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA. */
/* As a special exception, you may create a larger work that contains
part or all of the Bison parser skeleton and distribute that work
under terms of your choice, so long as that work isn't itself a
parser generator using the skeleton or a modified version thereof
as a parser skeleton. Alternatively, if you modify or redistribute
the parser skeleton itself, you may (at your option) remove this
special exception, which will cause the skeleton and the resulting
Bison output files to be licensed under the GNU General Public
License without this special exception.
This special exception was added by the Free Software Foundation in
version 2.2 of Bison. */
/* Tokens. */
#ifndef YYTOKENTYPE
# define YYTOKENTYPE
/* Put the tokens into the symbol table, so that GDB and other debuggers
know about them. */
enum yytokentype {
SEP = 258,
DATA = 259,
IDENT = 260
};
#endif
/* Tokens. */
#define SEP 258
#define DATA 259
#define IDENT 260
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
typedef union YYSTYPE
{
text_t text;
int num;
}
/* Line 1489 of yacc.c. */
YYSTYPE;
# define yystype YYSTYPE /* obsolescent; will be withdrawn */
# define YYSTYPE_IS_DECLARED 1
# define YYSTYPE_IS_TRIVIAL 1
#endif
extern YYSTYPE _lc_opt_lval;
%name-prefix="_lc_opt_"
%{
/*
libcore: library for basic data structures and algorithms.
Copyright (C) 2005 IPD Goos, Universit"at Karlsruhe, Germany
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <string.h>
#include "lc_opts_t.h"
#include "lc_parser_t.h"
static void group_open(void);
static void group_close(void);
static void lc_opt_set(void);
static void path_push(text_t text);
void PMANGLE(error)(const char *str);
int PMANGLE(linenr);
%}
%union {
text_t text;
int num;
}
%token SEP DATA