added stix_bfmtout() and stix_oofmtout()

This commit is contained in:
hyunghwan.chung 2016-06-01 15:33:03 +00:00
parent 037bbff42c
commit 0c4ce4eb04
15 changed files with 1341 additions and 180 deletions

View File

@ -43,6 +43,7 @@ libstix_la_SOURCES = \
decode.c \
dic.c \
exec.c \
fmt.c \
gc.c \
heap.c \
ignite.c \
@ -83,17 +84,17 @@ LIBADD_MOD_COMMON = -lstix
pkgmodexecdir = $(libdir)
pkgmodexec_LTLIBRARIES = libstix-snd.la libstix-con.la
pkgmodexec_LTLIBRARIES = libstix-snd.la #libstix-con.la
libstix_snd_la_SOURCES = mod-snd.c mod-snd.h
libstix_snd_la_CPPFLAGS = $(CPPFLAGS_MOD_COMMON)
libstix_snd_la_LDFLAGS = $(LDFLAGS_MOD_COMMON)
libstix_snd_la_LIBADD = $(LIBADD_MOD_COMMON)
libstix_con_la_SOURCES = mod-con.c mod-con.h
libstix_con_la_CPPFLAGS = $(CPPFLAGS_MOD_COMMON)
libstix_con_la_LDFLAGS = $(LDFLAGS_MOD_COMMON)
libstix_con_la_LIBADD = $(LIBADD_MOD_COMMON)
#libstix_con_la_SOURCES = mod-con.c mod-con.h
#libstix_con_la_CPPFLAGS = $(CPPFLAGS_MOD_COMMON)
#libstix_con_la_LDFLAGS = $(LDFLAGS_MOD_COMMON)
#libstix_con_la_LIBADD = $(LIBADD_MOD_COMMON)
endif

View File

@ -133,28 +133,16 @@ am__installdirs = "$(DESTDIR)$(pkglibdir)" \
"$(DESTDIR)$(pkgincludedir)"
LTLIBRARIES = $(pkglib_LTLIBRARIES) $(pkgmodexec_LTLIBRARIES)
am__DEPENDENCIES_1 =
@ENABLE_STATIC_MODULE_FALSE@libstix_con_la_DEPENDENCIES = \
@ENABLE_STATIC_MODULE_FALSE@ $(am__DEPENDENCIES_1)
am__libstix_con_la_SOURCES_DIST = mod-con.c mod-con.h
@ENABLE_STATIC_MODULE_FALSE@am_libstix_con_la_OBJECTS = \
@ENABLE_STATIC_MODULE_FALSE@ libstix_con_la-mod-con.lo
libstix_con_la_OBJECTS = $(am_libstix_con_la_OBJECTS)
AM_V_lt = $(am__v_lt_@AM_V@)
am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@)
am__v_lt_0 = --silent
am__v_lt_1 =
libstix_con_la_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC \
$(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=link $(CCLD) \
$(AM_CFLAGS) $(CFLAGS) $(libstix_con_la_LDFLAGS) $(LDFLAGS) -o \
$@
@ENABLE_STATIC_MODULE_FALSE@am_libstix_con_la_rpath = -rpath \
@ENABLE_STATIC_MODULE_FALSE@ $(pkgmodexecdir)
@ENABLE_STATIC_MODULE_FALSE@libstix_snd_la_DEPENDENCIES = \
@ENABLE_STATIC_MODULE_FALSE@ $(am__DEPENDENCIES_1)
am__libstix_snd_la_SOURCES_DIST = mod-snd.c mod-snd.h
@ENABLE_STATIC_MODULE_FALSE@am_libstix_snd_la_OBJECTS = \
@ENABLE_STATIC_MODULE_FALSE@ libstix_snd_la-mod-snd.lo
libstix_snd_la_OBJECTS = $(am_libstix_snd_la_OBJECTS)
AM_V_lt = $(am__v_lt_@AM_V@)
am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@)
am__v_lt_0 = --silent
am__v_lt_1 =
libstix_snd_la_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC \
$(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=link $(CCLD) \
$(AM_CFLAGS) $(CFLAGS) $(libstix_snd_la_LDFLAGS) $(LDFLAGS) -o \
@ -165,10 +153,10 @@ am__DEPENDENCIES_2 = $(am__DEPENDENCIES_1) $(am__DEPENDENCIES_1)
libstix_la_DEPENDENCIES = $(am__DEPENDENCIES_2)
am_libstix_la_OBJECTS = libstix_la-bigint.lo libstix_la-comp.lo \
libstix_la-debug.lo libstix_la-decode.lo libstix_la-dic.lo \
libstix_la-exec.lo libstix_la-gc.lo libstix_la-heap.lo \
libstix_la-ignite.lo libstix_la-obj.lo libstix_la-proc.lo \
libstix_la-rbt.lo libstix_la-stix.lo libstix_la-sym.lo \
libstix_la-utf8.lo libstix_la-utl.lo
libstix_la-exec.lo libstix_la-fmt.lo libstix_la-gc.lo \
libstix_la-heap.lo libstix_la-ignite.lo libstix_la-obj.lo \
libstix_la-proc.lo libstix_la-rbt.lo libstix_la-stix.lo \
libstix_la-sym.lo libstix_la-utf8.lo libstix_la-utl.lo
libstix_la_OBJECTS = $(am_libstix_la_OBJECTS)
libstix_la_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \
$(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \
@ -214,11 +202,10 @@ AM_V_CCLD = $(am__v_CCLD_@AM_V@)
am__v_CCLD_ = $(am__v_CCLD_@AM_DEFAULT_V@)
am__v_CCLD_0 = @echo " CCLD " $@;
am__v_CCLD_1 =
SOURCES = $(libstix_con_la_SOURCES) $(libstix_snd_la_SOURCES) \
$(libstix_la_SOURCES) $(stix_SOURCES)
DIST_SOURCES = $(am__libstix_con_la_SOURCES_DIST) \
$(am__libstix_snd_la_SOURCES_DIST) $(libstix_la_SOURCES) \
SOURCES = $(libstix_snd_la_SOURCES) $(libstix_la_SOURCES) \
$(stix_SOURCES)
DIST_SOURCES = $(am__libstix_snd_la_SOURCES_DIST) \
$(libstix_la_SOURCES) $(stix_SOURCES)
am__can_run_installinfo = \
case $$AM_UPDATE_INFO_DIR in \
n|no|NO) false;; \
@ -438,6 +425,7 @@ libstix_la_SOURCES = \
decode.c \
dic.c \
exec.c \
fmt.c \
gc.c \
heap.c \
ignite.c \
@ -471,15 +459,11 @@ stix_LDADD = $(LIBADD_LIB_COMMON) -lstix #-ldyncall_s
@ENABLE_STATIC_MODULE_FALSE@LDFLAGS_MOD_COMMON = $(LDFLAGS_LIB_COMMON)
@ENABLE_STATIC_MODULE_FALSE@LIBADD_MOD_COMMON = -lstix
@ENABLE_STATIC_MODULE_FALSE@pkgmodexecdir = $(libdir)
@ENABLE_STATIC_MODULE_FALSE@pkgmodexec_LTLIBRARIES = libstix-snd.la libstix-con.la
@ENABLE_STATIC_MODULE_FALSE@pkgmodexec_LTLIBRARIES = libstix-snd.la #libstix-con.la
@ENABLE_STATIC_MODULE_FALSE@libstix_snd_la_SOURCES = mod-snd.c mod-snd.h
@ENABLE_STATIC_MODULE_FALSE@libstix_snd_la_CPPFLAGS = $(CPPFLAGS_MOD_COMMON)
@ENABLE_STATIC_MODULE_FALSE@libstix_snd_la_LDFLAGS = $(LDFLAGS_MOD_COMMON)
@ENABLE_STATIC_MODULE_FALSE@libstix_snd_la_LIBADD = $(LIBADD_MOD_COMMON)
@ENABLE_STATIC_MODULE_FALSE@libstix_con_la_SOURCES = mod-con.c mod-con.h
@ENABLE_STATIC_MODULE_FALSE@libstix_con_la_CPPFLAGS = $(CPPFLAGS_MOD_COMMON)
@ENABLE_STATIC_MODULE_FALSE@libstix_con_la_LDFLAGS = $(LDFLAGS_MOD_COMMON)
@ENABLE_STATIC_MODULE_FALSE@libstix_con_la_LIBADD = $(LIBADD_MOD_COMMON)
all: stix-cfg.h
$(MAKE) $(AM_MAKEFLAGS) all-am
@ -601,9 +585,6 @@ clean-pkgmodexecLTLIBRARIES:
rm -f $${locs}; \
}
libstix-con.la: $(libstix_con_la_OBJECTS) $(libstix_con_la_DEPENDENCIES) $(EXTRA_libstix_con_la_DEPENDENCIES)
$(AM_V_CCLD)$(libstix_con_la_LINK) $(am_libstix_con_la_rpath) $(libstix_con_la_OBJECTS) $(libstix_con_la_LIBADD) $(LIBS)
libstix-snd.la: $(libstix_snd_la_OBJECTS) $(libstix_snd_la_DEPENDENCIES) $(EXTRA_libstix_snd_la_DEPENDENCIES)
$(AM_V_CCLD)$(libstix_snd_la_LINK) $(am_libstix_snd_la_rpath) $(libstix_snd_la_OBJECTS) $(libstix_snd_la_LIBADD) $(LIBS)
@ -669,13 +650,13 @@ mostlyclean-compile:
distclean-compile:
-rm -f *.tab.c
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libstix_con_la-mod-con.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libstix_la-bigint.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libstix_la-comp.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libstix_la-debug.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libstix_la-decode.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libstix_la-dic.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libstix_la-exec.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libstix_la-fmt.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libstix_la-gc.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libstix_la-heap.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libstix_la-ignite.Plo@am__quote@
@ -713,13 +694,6 @@ distclean-compile:
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LTCOMPILE) -c -o $@ $<
libstix_con_la-mod-con.lo: mod-con.c
@am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libstix_con_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT libstix_con_la-mod-con.lo -MD -MP -MF $(DEPDIR)/libstix_con_la-mod-con.Tpo -c -o libstix_con_la-mod-con.lo `test -f 'mod-con.c' || echo '$(srcdir)/'`mod-con.c
@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libstix_con_la-mod-con.Tpo $(DEPDIR)/libstix_con_la-mod-con.Plo
@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='mod-con.c' object='libstix_con_la-mod-con.lo' libtool=yes @AMDEPBACKSLASH@
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libstix_con_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o libstix_con_la-mod-con.lo `test -f 'mod-con.c' || echo '$(srcdir)/'`mod-con.c
libstix_snd_la-mod-snd.lo: mod-snd.c
@am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libstix_snd_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT libstix_snd_la-mod-snd.lo -MD -MP -MF $(DEPDIR)/libstix_snd_la-mod-snd.Tpo -c -o libstix_snd_la-mod-snd.lo `test -f 'mod-snd.c' || echo '$(srcdir)/'`mod-snd.c
@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libstix_snd_la-mod-snd.Tpo $(DEPDIR)/libstix_snd_la-mod-snd.Plo
@ -769,6 +743,13 @@ libstix_la-exec.lo: exec.c
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libstix_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o libstix_la-exec.lo `test -f 'exec.c' || echo '$(srcdir)/'`exec.c
libstix_la-fmt.lo: fmt.c
@am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libstix_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT libstix_la-fmt.lo -MD -MP -MF $(DEPDIR)/libstix_la-fmt.Tpo -c -o libstix_la-fmt.lo `test -f 'fmt.c' || echo '$(srcdir)/'`fmt.c
@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libstix_la-fmt.Tpo $(DEPDIR)/libstix_la-fmt.Plo
@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='fmt.c' object='libstix_la-fmt.lo' libtool=yes @AMDEPBACKSLASH@
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libstix_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o libstix_la-fmt.lo `test -f 'fmt.c' || echo '$(srcdir)/'`fmt.c
libstix_la-gc.lo: gc.c
@am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libstix_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT libstix_la-gc.lo -MD -MP -MF $(DEPDIR)/libstix_la-gc.Tpo -c -o libstix_la-gc.lo `test -f 'gc.c' || echo '$(srcdir)/'`gc.c
@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libstix_la-gc.Tpo $(DEPDIR)/libstix_la-gc.Plo
@ -1098,6 +1079,11 @@ uninstall-am: uninstall-binPROGRAMS uninstall-pkgincludeHEADERS \
uninstall-pkglibLTLIBRARIES uninstall-pkgmodexecLTLIBRARIES
#libstix_con_la_SOURCES = mod-con.c mod-con.h
#libstix_con_la_CPPFLAGS = $(CPPFLAGS_MOD_COMMON)
#libstix_con_la_LDFLAGS = $(LDFLAGS_MOD_COMMON)
#libstix_con_la_LIBADD = $(LIBADD_MOD_COMMON)
install-data-hook:
@echo "#ifndef _STIX_CFG_H_" > "$(DESTDIR)$(pkgincludedir)/stix-cfg.h"
@echo "#define _STIX_CFG_H_" >> "$(DESTDIR)$(pkgincludedir)/stix-cfg.h"

View File

@ -4310,7 +4310,7 @@ the compiler must collect all source method string collected so far.
need to write code to collect string.
*/
/* TODO: if (decoding is enabled... */
/* TODO: call stix_decode only if decoding is enabled... */
printf (">>>> BYTE CODE.....\n");
stix_decode (stix, mth);
@ -4374,6 +4374,7 @@ static int compile_method_definition (stix_t* stix)
printf (">>METHOD ");
print_oocs (&stix->c->mth.name);
printf ("\n");
if (stix->c->tok.type != STIX_IOTOK_LBRACE)
{
/* { expected */

View File

@ -87,41 +87,28 @@ void print_object (stix_t* stix, stix_oop_t oop)
{
if (oop == stix->_nil)
{
printf ("nil");
stix_bfmtout (stix, "nil");
}
else if (oop == stix->_true)
{
printf ("true");
stix_bfmtout (stix, "true");
}
else if (oop == stix->_false)
{
printf ("false");
stix_bfmtout (stix, "false");
}
else if (STIX_OOP_IS_SMOOI(oop))
{
printf ("%ld", (long int)STIX_OOP_TO_SMOOI(oop));
stix_bfmtout (stix, "%zd", STIX_OOP_TO_SMOOI(oop));
}
else if (STIX_OOP_IS_CHAR(oop))
{
stix_bch_t bcs[32];
stix_uch_t uch;
stix_oow_t ucslen, bcslen;
uch = STIX_OOP_TO_CHAR(oop);
bcslen = STIX_COUNTOF(bcs);
ucslen = 1;
if (stix_ucstoutf8 (&uch, &ucslen, bcs, &bcslen) >= 0)
{
printf ("$%.*s", (int)bcslen, bcs);
}
stix_bfmtout (stix, "$%.1C", STIX_OOP_TO_CHAR(oop));
}
else
{
stix_oop_class_t c;
stix_oocs_t s;
stix_oow_t i;
stix_bch_t bcs[32];
stix_oow_t ucslen, bcslen;
STIX_ASSERT (STIX_OOP_IS_POINTER(oop));
c = (stix_oop_class_t)STIX_OBJ_GET_CLASS(oop); /*STIX_CLASSOF(stix, oop);*/
@ -129,95 +116,75 @@ void print_object (stix_t* stix, stix_oop_t oop)
if ((stix_oop_t)c == stix->_large_negative_integer)
{
stix_oow_t i;
printf ("-16r");
stix_bfmtout (stix, "-16r");
for (i = STIX_OBJ_GET_SIZE(oop); i > 0;)
{
printf ("%0*lX", (int)(STIX_SIZEOF(stix_liw_t) * 2), (unsigned long)((stix_oop_liword_t)oop)->slot[--i]);
stix_bfmtout (stix, "%0*lX", (int)(STIX_SIZEOF(stix_liw_t) * 2), (unsigned long)((stix_oop_liword_t)oop)->slot[--i]);
}
}
else if ((stix_oop_t)c == stix->_large_positive_integer)
{
stix_oow_t i;
printf ("16r");
stix_bfmtout (stix, "16r");
for (i = STIX_OBJ_GET_SIZE(oop); i > 0;)
{
printf ("%0*lX", (int)(STIX_SIZEOF(stix_liw_t) * 2), (unsigned long)((stix_oop_liword_t)oop)->slot[--i]);
stix_bfmtout (stix, "%0*lX", (int)(STIX_SIZEOF(stix_liw_t) * 2), (unsigned long)((stix_oop_liword_t)oop)->slot[--i]);
}
}
else if (STIX_OBJ_GET_FLAGS_TYPE(oop) == STIX_OBJ_TYPE_CHAR)
{
if ((stix_oop_t)c == stix->_symbol) printf ("#");
else if ((stix_oop_t)c == stix->_string) printf ("'");
if ((stix_oop_t)c == stix->_symbol) stix_bfmtout (stix, "#");
else if ((stix_oop_t)c == stix->_string) stix_bfmtout (stix, "'");
for (i = 0; i < STIX_OBJ_GET_SIZE(oop); i++)
{
bcslen = STIX_COUNTOF(bcs);
ucslen = 1;
if (stix_ucstoutf8 (&((stix_oop_char_t)oop)->slot[i], &ucslen, bcs, &bcslen) >= 0)
{
printf ("%.*s", (int)bcslen, bcs);
}
}
if ((stix_oop_t)c == stix->_string) printf ("'");
stix_bfmtout (stix, "%.*S", STIX_OBJ_GET_SIZE(oop), ((stix_oop_char_t)oop)->slot);
if ((stix_oop_t)c == stix->_string) stix_bfmtout (stix, "'");
}
else if (STIX_OBJ_GET_FLAGS_TYPE(oop) == STIX_OBJ_TYPE_BYTE)
{
printf ("#[");
stix_bfmtout (stix, "#[");
for (i = 0; i < STIX_OBJ_GET_SIZE(oop); i++)
{
printf (" %d", ((stix_oop_byte_t)oop)->slot[i]);
stix_bfmtout (stix, " %d", ((stix_oop_byte_t)oop)->slot[i]);
}
printf ("]");
stix_bfmtout (stix, "]");
}
else if (STIX_OBJ_GET_FLAGS_TYPE(oop) == STIX_OBJ_TYPE_HALFWORD)
{
printf ("#[["); /* TODO: fix this symbol */
stix_bfmtout (stix, "#[["); /* TODO: fix this symbol */
for (i = 0; i < STIX_OBJ_GET_SIZE(oop); i++)
{
printf (" %lX", (unsigned long int)((stix_oop_halfword_t)oop)->slot[i]);
stix_bfmtout (stix, " %zX", (stix_oow_t)((stix_oop_halfword_t)oop)->slot[i]);
}
printf ("]]");
stix_bfmtout (stix, "]]");
}
else if (STIX_OBJ_GET_FLAGS_TYPE(oop) == STIX_OBJ_TYPE_WORD)
{
printf ("#[[["); /* TODO: fix this symbol */
stix_bfmtout (stix, "#[[["); /* TODO: fix this symbol */
for (i = 0; i < STIX_OBJ_GET_SIZE(oop); i++)
{
printf (" %lX", (unsigned long int)((stix_oop_word_t)oop)->slot[i]);
stix_bfmtout (stix, " %zX", ((stix_oop_word_t)oop)->slot[i]);
}
printf ("]]]");
stix_bfmtout (stix, "]]]");
}
else if ((stix_oop_t)c == stix->_array)
{
printf ("#(");
stix_bfmtout (stix, "#(");
for (i = 0; i < STIX_OBJ_GET_SIZE(oop); i++)
{
printf (" ");
stix_bfmtout (stix, " ");
print_object (stix, ((stix_oop_oop_t)oop)->slot[i]);
}
printf (")");
stix_bfmtout (stix, ")");
}
else if ((stix_oop_t)c == stix->_class)
{
/* print the class name */
for (i = 0; i < STIX_OBJ_GET_SIZE(((stix_oop_class_t)oop)->name); i++)
{
bcslen = STIX_COUNTOF(bcs);
ucslen = 1;
if (stix_ucstoutf8 (&((stix_oop_class_t)oop)->name->slot[i], &ucslen, bcs, &bcslen) >= 0)
{
printf ("%.*s", (int)bcslen, bcs);
}
}
stix_bfmtout (stix, "%.*S", STIX_OBJ_GET_SIZE(((stix_oop_class_t)oop)->name), ((stix_oop_class_t)oop)->name->slot);
}
else
{
s.ptr = ((stix_oop_char_t)c->name)->slot;
s.len = STIX_OBJ_GET_SIZE(c->name);
printf ("instance of ");
print_oocs (&s);
printf ("- (%p)", oop);
stix_bfmtout (stix, "instance of %.*S - (%p)", STIX_OBJ_GET_SIZE(c->name), ((stix_oop_char_t)c->name)->slot, oop);
}
}
}

View File

@ -26,10 +26,11 @@
#include "stix-prv.h"
# define DECODE_OUT_0(fmt) printf("\t" fmt "\n")
# define DECODE_OUT_1(fmt,a1) printf("\t" fmt "\n",a1)
# define DECODE_OUT_2(fmt,a1,a2) printf("\t" fmt "\n", a1, a2)
# define DECODE_OUT_3(fmt,a1,a2,a3) printf("\t" fmt "\n", a1, a2, a3)
# define DECODE_OUT_0(stix,fmt) stix_bfmtout(stix, "\t" fmt "\n")
# define DECODE_OUT_1(stix,fmt,a1) stix_bfmtout(stix, "\t" fmt "\n",a1)
# define DECODE_OUT_2(stix,fmt,a1,a2) stix_bfmtout(stix, "\t" fmt "\n", a1, a2)
# define DECODE_OUT_3(stix,fmt,a1,a2,a3) stix_bfmtout(stix, "\t" fmt "\n", a1, a2, a3)
#define FETCH_BYTE_CODE(stix) (cdptr[ip++])
@ -44,8 +45,11 @@
# define FETCH_PARAM_CODE_TO(stix,v_ooi) (v_ooi = FETCH_BYTE_CODE(stix))
#endif
/* TODO: check if ip shoots beyond the maximum length in fetching code and parameters */
static int decode (stix_t* stix, stix_oop_method_t mth)
int stix_decode (stix_t* stix, stix_oop_method_t mth)
{
stix_oob_t bcode, * cdptr;
stix_oow_t ip = 0, cdlen;
@ -74,7 +78,7 @@ static int decode (stix_t* stix, stix_oop_method_t mth)
case BCODE_PUSH_INSTVAR_7:
b1 = bcode & 0x7; /* low 3 bits */
push_instvar:
DECODE_OUT_1 ("push_instvar %lu", (unsigned long int)b1);
DECODE_OUT_1 (stix, "push_instvar %zd", b1);
break;
/* ------------------------------------------------- */
@ -92,7 +96,7 @@ static int decode (stix_t* stix, stix_oop_method_t mth)
case BCODE_STORE_INTO_INSTVAR_7:
b1 = bcode & 0x7; /* low 3 bits */
store_instvar:
DECODE_OUT_1 ("store_into_instvar %lu", (unsigned long int)b1);
DECODE_OUT_1 (stix, "store_into_instvar %zd", b1);
break;
case BCODE_POP_INTO_INSTVAR_X:
@ -108,7 +112,7 @@ static int decode (stix_t* stix, stix_oop_method_t mth)
case BCODE_POP_INTO_INSTVAR_7:
b1 = bcode & 0x7; /* low 3 bits */
pop_into_instvar:
DECODE_OUT_1 ("pop_into_instvar %lu", (unsigned long int)b1);
DECODE_OUT_1 (stix, "pop_into_instvar %zd", b1);
break;
/* ------------------------------------------------- */
@ -148,7 +152,7 @@ static int decode (stix_t* stix, stix_oop_method_t mth)
if ((bcode >> 4) & 1)
{
/* push - bit 4 on */
DECODE_OUT_1 ("push_tempvar %d", (int)b1);
DECODE_OUT_1 (stix, "push_tempvar %zd", b1);
}
else
{
@ -156,11 +160,11 @@ static int decode (stix_t* stix, stix_oop_method_t mth)
if ((bcode >> 3) & 1)
{
/* pop - bit 3 on */
DECODE_OUT_1 ("pop_into_tempvar %d", (int)b1);
DECODE_OUT_1 (stix, "pop_into_tempvar %zd", b1);
}
else
{
DECODE_OUT_1 ("store_into_tempvar %d", (int)b1);
DECODE_OUT_1 (stix, "store_into_tempvar %zd", b1);
}
}
break;
@ -180,7 +184,7 @@ static int decode (stix_t* stix, stix_oop_method_t mth)
case BCODE_PUSH_LITERAL_7:
b1 = bcode & 0x7; /* low 3 bits */
push_literal:
DECODE_OUT_1 ("push_literal %d", (int)b1);
DECODE_OUT_1 (stix, "push_literal %zd", b1);
break;
/* ------------------------------------------------- */
@ -208,16 +212,16 @@ static int decode (stix_t* stix, stix_oop_method_t mth)
{
if ((bcode >> 2) & 1)
{
DECODE_OUT_1("pop_into_object %d", (int)b1);
DECODE_OUT_1 (stix, "pop_into_object %zd", b1);
}
else
{
DECODE_OUT_1("store_into_object %d", (int)b1);
DECODE_OUT_1 (stix, "store_into_object %zd", b1);
}
}
else
{
DECODE_OUT_1("push_object %d", (int)b1);
DECODE_OUT_1 (stix, "push_object %zd", b1);
}
break;
@ -225,19 +229,19 @@ static int decode (stix_t* stix, stix_oop_method_t mth)
case BCODE_JUMP_FORWARD_X:
FETCH_PARAM_CODE_TO (stix, b1);
DECODE_OUT_1 ("jump_forward %d", (int)b1);
DECODE_OUT_1 (stix, "jump_forward %zd", b1);
break;
case BCODE_JUMP_FORWARD_0:
case BCODE_JUMP_FORWARD_1:
case BCODE_JUMP_FORWARD_2:
case BCODE_JUMP_FORWARD_3:
DECODE_OUT_1 ("jump_forward %d", (int)(bcode & 0x3)); /* low 2 bits */
DECODE_OUT_1 (stix, "jump_forward %zd", (bcode & 0x3)); /* low 2 bits */
break;
case BCODE_JUMP_BACKWARD_X:
FETCH_PARAM_CODE_TO (stix, b1);
DECODE_OUT_1 ("jump_backward %d", (int)b1);
DECODE_OUT_1 (stix, "jump_backward %zd", b1);
stix->ip += b1;
break;
@ -245,7 +249,7 @@ static int decode (stix_t* stix, stix_oop_method_t mth)
case BCODE_JUMP_BACKWARD_1:
case BCODE_JUMP_BACKWARD_2:
case BCODE_JUMP_BACKWARD_3:
DECODE_OUT_1 ("jump_backward %d", (int)(bcode & 0x3)); /* low 2 bits */
DECODE_OUT_1 (stix, "jump_backward %zd", (bcode & 0x3)); /* low 2 bits */
break;
case BCODE_JUMP_IF_TRUE_X:
@ -258,18 +262,18 @@ static int decode (stix_t* stix, stix_oop_method_t mth)
case BCODE_JUMP_IF_FALSE_1:
case BCODE_JUMP_IF_FALSE_2:
case BCODE_JUMP_IF_FALSE_3:
printf ("<<<<<<<<<<<<<< JUMP NOT IMPLEMENTED YET >>>>>>>>>>>> \n");
DECODE_OUT_0 (stix, "<<<<<<<<<<<<<< JUMP NOT IMPLEMENTED YET >>>>>>>>>>>>");
stix->errnum = STIX_ENOIMPL;
return -1;
case BCODE_JUMP2_FORWARD:
FETCH_PARAM_CODE_TO (stix, b1);
DECODE_OUT_1 ("jump2_forward %d", (int)b1);
DECODE_OUT_1 (stix, "jump2_forward %zd", b1);
break;
case BCODE_JUMP2_BACKWARD:
FETCH_PARAM_CODE_TO (stix, b1);
DECODE_OUT_1 ("jump2_backward %d", (int)b1);
DECODE_OUT_1 (stix, "jump2_backward %zd", b1);
break;
/* -------------------------------------------------------- */
@ -302,17 +306,17 @@ return -1;
if ((bcode >> 2) & 1)
{
DECODE_OUT_2 ("pop_into_ctxtempvar %d %d", (int)b1, (int)b2);
DECODE_OUT_2 (stix, "pop_into_ctxtempvar %zd %zd", b1, b2);
}
else
{
DECODE_OUT_2 ("store_into_ctxtempvar %d %d", (int)b1, (int)b2);
DECODE_OUT_2 (stix, "store_into_ctxtempvar %zd %zd", b1, b2);
}
}
else
{
/* push */
DECODE_OUT_2 ("push_ctxtempvar %d %d", (int)b1, (int)b2);
DECODE_OUT_2 (stix, "push_ctxtempvar %zd %zd", b1, b2);
}
break;
@ -348,16 +352,16 @@ return -1;
/* store or pop */
if ((bcode >> 2) & 1)
{
DECODE_OUT_2 ("pop_into_objvar %d %d", (int)b1, (int)b2);
DECODE_OUT_2 (stix, "pop_into_objvar %zd %zd", b1, b2);
}
else
{
DECODE_OUT_2 ("store_into_objvar %d %d", (int)b1, (int)b2);
DECODE_OUT_2 (stix, "store_into_objvar %zd %zd", b1, b2);
}
}
else
{
DECODE_OUT_2 ("push_objvar %d %d", (int)b1, (int)b2);
DECODE_OUT_2 (stix, "push_objvar %zd %zd", b1, b2);
}
break;
@ -383,77 +387,77 @@ return -1;
FETCH_BYTE_CODE_TO (stix, b2);
handle_send_message:
DECODE_OUT_3 ("send_message%s nargs=%d selector=%d", (((bcode >> 2) & 1)? "_to_super": ""), (int)b1, (int)b2);
DECODE_OUT_3 (stix, "send_message%hs %zd @%zd", (((bcode >> 2) & 1)? "_to_super": ""), b1, b2);
break;
/* -------------------------------------------------------- */
case BCODE_PUSH_RECEIVER:
DECODE_OUT_0 ("push_receiver");
DECODE_OUT_0 (stix, "push_receiver");
break;
case BCODE_PUSH_NIL:
DECODE_OUT_0 ("push_nil");
DECODE_OUT_0 (stix, "push_nil");
break;
case BCODE_PUSH_TRUE:
DECODE_OUT_0 ("push_true");
DECODE_OUT_0 (stix, "push_true");
break;
case BCODE_PUSH_FALSE:
DECODE_OUT_0 ("push_false");
DECODE_OUT_0 (stix, "push_false");
break;
case BCODE_PUSH_CONTEXT:
DECODE_OUT_0 ("push_context");
DECODE_OUT_0 (stix, "push_context");
break;
case BCODE_PUSH_NEGONE:
DECODE_OUT_0 ("push_negone");
DECODE_OUT_0 (stix, "push_negone");
break;
case BCODE_PUSH_ZERO:
DECODE_OUT_0 ("push_zero");
DECODE_OUT_0 (stix, "push_zero");
break;
case BCODE_PUSH_ONE:
DECODE_OUT_0 ("push_one");
DECODE_OUT_0 (stix, "push_one");
break;
case BCODE_PUSH_TWO:
DECODE_OUT_0 ("push_two");
DECODE_OUT_0 (stix, "push_two");
break;
case BCODE_PUSH_INTLIT:
FETCH_PARAM_CODE_TO (stix, b1);
DECODE_OUT_1 ("push_intlit %d", (int)b1);
DECODE_OUT_1 (stix, "push_intlit %zd", b1);
break;
case BCODE_PUSH_NEGINTLIT:
FETCH_PARAM_CODE_TO (stix, b1);
DECODE_OUT_1 ("push_negintlit %d", (int)-b1);
DECODE_OUT_1 (stix, "push_negintlit %zd", -b1);
break;
/* -------------------------------------------------------- */
case BCODE_DUP_STACKTOP:
DECODE_OUT_0 ("dup_stacktop");
DECODE_OUT_0 (stix, "dup_stacktop");
break;
case BCODE_POP_STACKTOP:
DECODE_OUT_0 ("pop_stacktop");
DECODE_OUT_0 (stix, "pop_stacktop");
break;
case BCODE_RETURN_STACKTOP:
DECODE_OUT_0 ("return_stacktop");
DECODE_OUT_0 (stix, "return_stacktop");
break;
case BCODE_RETURN_RECEIVER:
DECODE_OUT_0 ("return_receiver");
DECODE_OUT_0 (stix, "return_receiver");
break;
case BCODE_RETURN_FROM_BLOCK:
DECODE_OUT_0 ("return_from_block");
DECODE_OUT_0 (stix, "return_from_block");
break;
case BCODE_MAKE_BLOCK:
@ -462,7 +466,7 @@ return -1;
FETCH_PARAM_CODE_TO (stix, b1);
FETCH_PARAM_CODE_TO (stix, b2);
DECODE_OUT_2 ("make_block %d %d", (int)b1, (int)b2);
DECODE_OUT_2 (stix, "make_block %zd %zd", b1, b2);
STIX_ASSERT (b1 >= 0);
STIX_ASSERT (b2 >= b1);
@ -470,16 +474,16 @@ return -1;
case BCODE_SEND_BLOCK_COPY:
DECODE_OUT_0 ("send_block_copy");
DECODE_OUT_0 (stix, "send_block_copy");
break;
case BCODE_NOOP:
/* do nothing */
DECODE_OUT_0 ("noop");
DECODE_OUT_0 (stix, "noop");
break;
default:
printf ("UNKNOWN BYTE CODE ENCOUNTERED %x\n", (int)bcode);
DECODE_OUT_1 (stix, "UNKNOWN BYTE CODE ENCOUNTERED %x", (int)bcode);
stix->errnum = STIX_EINTERN;
break;
@ -489,15 +493,9 @@ printf ("UNKNOWN BYTE CODE ENCOUNTERED %x\n", (int)bcode);
/* print literal frame contents */
for (ip = 0; ip < STIX_OBJ_GET_SIZE(mth) - STIX_METHOD_NAMED_INSTVARS; ip++)
{
printf (" %8lu ", (unsigned long int)ip);
print_object (stix, mth->slot[ip]);
printf ("\n");
DECODE_OUT_2 (stix, " @%-3lu %O", (unsigned long int)ip, mth->slot[ip]);
}
return 0;
}
int stix_decode (stix_t* stix, stix_oop_method_t mth)
{
return decode (stix, mth);
}

View File

@ -26,7 +26,6 @@
#include "stix-prv.h"
/* TODO: remove these headers after having migrated system-dependent functions of of this file */
#if defined(_WIN32)
# include <windows.h>
@ -1214,15 +1213,15 @@ static int prim_dump (stix_t* stix, stix_ooi_t nargs)
STIX_ASSERT (nargs >= 0);
printf ("RECEIVER: ");
stix_bfmtout (stix, "RECEIVER: ");
print_object (stix, STIX_STACK_GET(stix, stix->sp - nargs));
printf ("\n");
stix_bfmtout (stix, "\n");
for (i = nargs; i > 0; )
{
--i;
printf ("ARGUMENT: ");
stix_bfmtout (stix, "ARGUMENT: ");
print_object (stix, STIX_STACK_GET(stix, stix->sp - i));
printf ("\n");
stix_bfmtout (stix, "\n");
}
STIX_STACK_POPS (stix, nargs);
@ -2667,7 +2666,7 @@ typedef struct prim_t prim_t;
static prim_t primitives[] =
{
{ 0, MAX_NARGS, prim_dump, "_dump" },
{ 0, MAX_NARGS, prim_dump, "_dump" },
{ 1, 1, prim_identical, "_identical" },
{ 1, 1, prim_not_identical, "_not_identical" },

252
stix/lib/fmt.c Normal file
View File

@ -0,0 +1,252 @@
/*
* $Id$
*
Copyright (c) 2014-2016 Chung, Hyung-Hwan. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "stix-prv.h"
#include <stdio.h> /* for snrintf(). used for floating-point number formatting */
#include <stdarg.h>
#if defined(_MSC_VER) || defined(__BORLANDC__) || (defined(__WATCOMC__) && (__WATCOMC__ < 1200))
# define snprintf _snprintf
# if !defined(HAVE_SNPRINTF)
# define HAVE_SNPRINTF
# endif
#endif
#if defined(HAVE_QUADMATH_H)
# include <quadmath.h> /* for quadmath_snprintf() */
#endif
/* TODO: remove stdio.h and quadmath.h once snprintf gets replaced by own
floting-point conversion implementation*/
/* Max number conversion buffer length:
* stix_intmax_t in base 2, plus NUL byte. */
#define MAXNBUF (STIX_SIZEOF(stix_intmax_t) * 8 + 1)
enum
{
/* integer */
LF_C = (1 << 0),
LF_H = (1 << 1),
LF_J = (1 << 2),
LF_L = (1 << 3),
LF_Q = (1 << 4),
LF_T = (1 << 5),
LF_Z = (1 << 6),
/* long double */
LF_LD = (1 << 7),
/* __float128 */
LF_QD = (1 << 8)
};
static struct
{
stix_uint8_t flag; /* for single occurrence */
stix_uint8_t dflag; /* for double occurrence */
} lm_tab[26] =
{
{ 0, 0 }, /* a */
{ 0, 0 }, /* b */
{ 0, 0 }, /* c */
{ 0, 0 }, /* d */
{ 0, 0 }, /* e */
{ 0, 0 }, /* f */
{ 0, 0 }, /* g */
{ LF_H, LF_C }, /* h */
{ 0, 0 }, /* i */
{ LF_J, 0 }, /* j */
{ 0, 0 }, /* k */
{ LF_L, LF_Q }, /* l */
{ 0, 0 }, /* m */
{ 0, 0 }, /* n */
{ 0, 0 }, /* o */
{ 0, 0 }, /* p */
{ LF_Q, 0 }, /* q */
{ 0, 0 }, /* r */
{ 0, 0 }, /* s */
{ LF_T, 0 }, /* t */
{ 0, 0 }, /* u */
{ 0, 0 }, /* v */
{ 0, 0 }, /* w */
{ 0, 0 }, /* z */
{ 0, 0 }, /* y */
{ LF_Z, 0 }, /* z */
};
enum
{
FLAGC_DOT = (1 << 0),
FLAGC_SPACE = (1 << 1),
FLAGC_SHARP = (1 << 2),
FLAGC_SIGN = (1 << 3),
FLAGC_LEFTADJ = (1 << 4),
FLAGC_ZEROPAD = (1 << 5),
FLAGC_WIDTH = (1 << 6),
FLAGC_PRECISION = (1 << 7),
FLAGC_STAR1 = (1 << 8),
FLAGC_STAR2 = (1 << 9),
FLAGC_LENMOD = (1 << 10) /* length modifier */
};
static const stix_bch_t hex2ascii_lower[] =
{
'0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f','g','h','i','j','k','l','m',
'n','o','p','q','r','s','t','u','v','w','x','y','z'
};
static const stix_bch_t hex2ascii_upper[] =
{
'0','1','2','3','4','5','6','7','8','9',
'A','B','C','D','E','F','G','H','I','J','K','L','M',
'N','O','P','Q','R','S','T','U','V','W','X','H','Z'
};
static stix_ooch_t ooch_nullstr[] = { '(','n','u','l','l', ')','\0' };
static stix_bch_t bch_nullstr[] = { '(','n','u','l','l', ')','\0' };
typedef int (*stix_fmtout_put_t) (
stix_t* stix,
stix_ooch_t c
);
typedef struct stix_fmtout_t stix_fmtout_t;
struct stix_fmtout_t
{
stix_oow_t count; /* out */
stix_oow_t limit; /* in */
stix_fmtout_put_t put;
};
/* ------------------------------------------------------------------------- */
/*
* Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
* order; return an optional length and a pointer to the last character
* written in the buffer (i.e., the first character of the string).
* The buffer pointed to by `nbuf' must have length >= MAXNBUF.
*/
static stix_bch_t* sprintn_lower (stix_bch_t* nbuf, stix_uintmax_t num, int base, stix_ooi_t *lenp)
{
stix_bch_t* p;
p = nbuf;
*p = '\0';
do { *++p = hex2ascii_lower[num % base]; } while (num /= base);
if (lenp) *lenp = p - nbuf;
return p; /* returns the end */
}
static stix_bch_t* sprintn_upper (stix_bch_t* nbuf, stix_uintmax_t num, int base, stix_ooi_t *lenp)
{
stix_bch_t* p;
p = nbuf;
*p = '\0';
do { *++p = hex2ascii_upper[num % base]; } while (num /= base);
if (lenp) *lenp = p - nbuf;
return p; /* returns the end */
}
/* ------------------------------------------------------------------------- */
static int put_ooch (stix_t* stix, stix_ooch_t ch)
{
if (stix->log.len >= stix->log.capa)
{
stix_oow_t newcapa;
stix_ooch_t* tmp;
newcapa = stix->log.capa + 512; /* TODO: adjust this capacity */
tmp = stix_reallocmem (stix, stix->log.ptr, newcapa * STIX_SIZEOF(*tmp));
if (!tmp) return -1;
stix->log.ptr = tmp;
stix->log.capa = newcapa;
}
stix->log.ptr[stix->log.len++] = ch;
if (ch == '\n')
{
stix->vmprim.log_write (stix, 0, stix->log.ptr, stix->log.len);
/* TODO: error handling */
stix->log.len = 0;
}
return 1;
}
/* ------------------------------------------------------------------------- */
#undef fmtchar_t
#undef fmtoutv
#define fmtchar_t stix_bch_t
#define fmtoutv stix_bfmtoutv
#include "fmtoutv.h"
#undef fmtchar_t
#undef fmtoutv
#define fmtchar_t stix_ooch_t
#define fmtoutv stix_oofmtoutv
#include "fmtoutv.h"
stix_ooi_t stix_bfmtout (stix_t* stix, const stix_bch_t* fmt, ...)
{
stix_ooi_t x;
va_list ap;
stix_fmtout_t fo;
fo.count = 0;
fo.limit = STIX_TYPE_MAX(stix_ooi_t);
fo.put = put_ooch;
va_start (ap, fmt);
x = stix_bfmtoutv (stix, fmt, &fo, ap);
va_end (ap);
return (x <= -1)? -1: fo.count;
}
stix_ooi_t stix_oofmtout (stix_t* stix, const stix_ooch_t* fmt, ...)
{
stix_ooi_t x;
va_list ap;
stix_fmtout_t fo;
fo.count = 0;
fo.limit = STIX_TYPE_MAX(stix_ooi_t);
fo.put = put_ooch;
va_start (ap, fmt);
x = stix_oofmtoutv (stix, fmt, &fo, ap);
va_end (ap);
return (x <= -1)? -1: fo.count;
}

900
stix/lib/fmtoutv.h Normal file
View File

@ -0,0 +1,900 @@
/*
* $Id$
*
Copyright (c) 2014-2016 Chung, Hyung-Hwan. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This file contains a formatted output routine derived from kvprintf()
* of FreeBSD. It has been heavily modified and bug-fixed.
*/
/*
* Copyright (c) 1986, 1988, 1991, 1993
* The Regents of the University of California. All rights reserved.
* (c) UNIX System Laboratories, Inc.
* All or some portions of this file are derived from material licensed
* to the University of California by American Telephone and Telegraph
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
* the permission of UNIX System Laboratories, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
/* NOTE: data output is aborted if the data limit is reached or
* I/O error occurs */
#undef PUT_CHAR
#define PUT_CHAR(stix,c) do { \
int xx; \
if (data->count >= data->limit) goto done; \
if ((xx = data->put (stix, c)) <= -1) goto oops; \
if (xx == 0) goto done; \
data->count++; \
} while (0)
int fmtoutv (stix_t* stix, const fmtchar_t* fmt, stix_fmtout_t* data, va_list ap)
{
const fmtchar_t* percent;
stix_bch_t nbuf[MAXNBUF], bch;
const stix_bch_t* nbufp;
int n, base, neg, sign;
stix_ooi_t tmp, width, precision;
stix_ooch_t ch, padc;
int lm_flag, lm_dflag, flagc, numlen;
stix_uintmax_t num = 0;
int stop = 0;
#if 0
stix_bchbuf_t* fltfmt;
stix_oochbuf_t* fltout;
#endif
stix_bch_t* (*sprintn) (stix_bch_t* nbuf, stix_uintmax_t num, int base, stix_ooi_t* lenp);
data->count = 0;
#if 0
fltfmt = &stix->d->fltfmt;
fltout = &stix->d->fltout;
fltfmt->ptr = fltfmt->sbuf;
fltfmt->capa = STIX_COUNTOF(fltfmt->sbuf) - 1;
fltout->ptr = fltout->sbuf;
fltout->capa = STIX_COUNTOF(fltout->sbuf) - 1;
#endif
while (1)
{
while ((ch = *fmt++) != '%' || stop)
{
if (ch == '\0') goto done;
PUT_CHAR (stix, ch);
}
percent = fmt - 1;
padc = ' ';
width = 0; precision = 0;
neg = 0; sign = 0;
lm_flag = 0; lm_dflag = 0; flagc = 0;
sprintn = sprintn_lower;
reswitch:
switch (ch = *fmt++)
{
case '%': /* %% */
bch = ch;
goto print_lowercase_c;
/* flag characters */
case '.':
if (flagc & FLAGC_DOT) goto invalid_format;
flagc |= FLAGC_DOT;
goto reswitch;
case '#':
if (flagc & (FLAGC_WIDTH | FLAGC_DOT | FLAGC_LENMOD)) goto invalid_format;
flagc |= FLAGC_SHARP;
goto reswitch;
case ' ':
if (flagc & (FLAGC_WIDTH | FLAGC_DOT | FLAGC_LENMOD)) goto invalid_format;
flagc |= FLAGC_SPACE;
goto reswitch;
case '+': /* place sign for signed conversion */
if (flagc & (FLAGC_WIDTH | FLAGC_DOT | FLAGC_LENMOD)) goto invalid_format;
flagc |= FLAGC_SIGN;
goto reswitch;
case '-': /* left adjusted */
if (flagc & (FLAGC_WIDTH | FLAGC_DOT | FLAGC_LENMOD)) goto invalid_format;
if (flagc & FLAGC_DOT)
{
goto invalid_format;
}
else
{
flagc |= FLAGC_LEFTADJ;
if (flagc & FLAGC_ZEROPAD)
{
padc = ' ';
flagc &= ~FLAGC_ZEROPAD;
}
}
goto reswitch;
case '*': /* take the length from the parameter */
if (flagc & FLAGC_DOT)
{
if (flagc & (FLAGC_STAR2 | FLAGC_PRECISION)) goto invalid_format;
flagc |= FLAGC_STAR2;
precision = va_arg(ap, stix_ooi_t); /* this deviates from the standard printf that accepts 'int' */
if (precision < 0)
{
/* if precision is less than 0,
* treat it as if no .precision is specified */
flagc &= ~FLAGC_DOT;
precision = 0;
}
}
else
{
if (flagc & (FLAGC_STAR1 | FLAGC_WIDTH)) goto invalid_format;
flagc |= FLAGC_STAR1;
width = va_arg(ap, stix_ooi_t); /* it deviates from the standard printf that accepts 'int' */
if (width < 0)
{
/*
if (flagc & FLAGC_LEFTADJ)
flagc &= ~FLAGC_LEFTADJ;
else
*/
flagc |= FLAGC_LEFTADJ;
width = -width;
}
}
goto reswitch;
case '0': /* zero pad */
if (flagc & FLAGC_LENMOD) goto invalid_format;
if (!(flagc & (FLAGC_DOT | FLAGC_LEFTADJ)))
{
padc = '0';
flagc |= FLAGC_ZEROPAD;
goto reswitch;
}
/* end of flags characters */
case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
for (n = 0;; ++fmt)
{
n = n * 10 + ch - '0';
ch = *fmt;
if (ch < '0' || ch > '9') break;
}
if (flagc & FLAGC_DOT)
{
if (flagc & FLAGC_STAR2) goto invalid_format;
precision = n;
flagc |= FLAGC_PRECISION;
}
else
{
if (flagc & FLAGC_STAR1) goto invalid_format;
width = n;
flagc |= FLAGC_WIDTH;
}
goto reswitch;
/* length modifiers */
case 'h': /* short int */
case 'l': /* long int */
case 'q': /* long long int */
case 'j': /* uintmax_t */
case 'z': /* size_t */
case 't': /* ptrdiff_t */
if (lm_flag & (LF_LD | LF_QD)) goto invalid_format;
flagc |= FLAGC_LENMOD;
if (lm_dflag)
{
/* error */
goto invalid_format;
}
else if (lm_flag)
{
if (lm_tab[ch - 'a'].dflag && lm_flag == lm_tab[ch - 'a'].flag)
{
lm_flag &= ~lm_tab[ch - 'a'].flag;
lm_flag |= lm_tab[ch - 'a'].dflag;
lm_dflag |= lm_flag;
goto reswitch;
}
else
{
/* error */
goto invalid_format;
}
}
else
{
lm_flag |= lm_tab[ch - 'a'].flag;
goto reswitch;
}
break;
case 'L': /* long double */
if (flagc & FLAGC_LENMOD)
{
/* conflict with other length modifier */
goto invalid_format;
}
flagc |= FLAGC_LENMOD;
lm_flag |= LF_LD;
goto reswitch;
case 'Q': /* __float128 */
if (flagc & FLAGC_LENMOD)
{
/* conflict with other length modifier */
goto invalid_format;
}
flagc |= FLAGC_LENMOD;
lm_flag |= LF_QD;
goto reswitch;
/* end of length modifiers */
case 'n':
if (lm_flag & LF_J) /* j */
*(va_arg(ap, stix_intmax_t*)) = data->count;
else if (lm_flag & LF_Z) /* z */
*(va_arg(ap, stix_ooi_t*)) = data->count;
#if (STIX_SIZEOF_LONG_LONG > 0)
else if (lm_flag & LF_Q) /* ll */
*(va_arg(ap, long long int*)) = data->count;
#endif
else if (lm_flag & LF_L) /* l */
*(va_arg(ap, long int*)) = data->count;
else if (lm_flag & LF_H) /* h */
*(va_arg(ap, short int*)) = data->count;
else if (lm_flag & LF_C) /* hh */
*(va_arg(ap, char*)) = data->count;
else if (flagc & FLAGC_LENMOD)
{
stix->errnum = STIX_EINVAL;
goto oops;
}
else
*(va_arg(ap, int*)) = data->count;
break;
/* signed integer conversions */
case 'd':
case 'i': /* signed conversion */
base = 10;
sign = 1;
goto handle_sign;
/* end of signed integer conversions */
/* unsigned integer conversions */
case 'o':
base = 8;
goto handle_nosign;
case 'u':
base = 10;
goto handle_nosign;
case 'X':
sprintn = sprintn_upper;
case 'x':
base = 16;
goto handle_nosign;
/* end of unsigned integer conversions */
case 'p': /* pointer */
base = 16;
if (width == 0) flagc |= FLAGC_SHARP;
else flagc &= ~FLAGC_SHARP;
num = (stix_uintptr_t)va_arg(ap, void*);
goto number;
case 'c':
{
/* zeropad must not take effect for 'c' */
if (flagc & FLAGC_ZEROPAD) padc = ' ';
if (lm_flag & LF_L) goto uppercase_c;
lowercase_c:
bch = STIX_SIZEOF(stix_bch_t) < STIX_SIZEOF(int)? va_arg(ap, int): va_arg(ap, stix_bch_t);
print_lowercase_c:
/* precision 0 doesn't kill the letter */
width--;
if (!(flagc & FLAGC_LEFTADJ) && width > 0)
{
while (width--) PUT_CHAR (stix, padc);
}
PUT_CHAR (stix, bch);
if ((flagc & FLAGC_LEFTADJ) && width > 0)
{
while (width--) PUT_CHAR (stix, padc);
}
break;
}
case 'C':
{
stix_ooch_t ooch;
/* zeropad must not take effect for 'C' */
if (flagc & FLAGC_ZEROPAD) padc = ' ';
if (lm_flag & LF_H) goto lowercase_c;
uppercase_c:
ooch = STIX_SIZEOF(stix_ooch_t) < STIX_SIZEOF(int)? va_arg(ap, int): va_arg(ap, stix_ooch_t);
/* precision 0 doesn't kill the letter */
width--;
if (!(flagc & FLAGC_LEFTADJ) && width > 0)
{
while (width--) PUT_CHAR (stix, padc);
}
PUT_CHAR (stix, ooch);
if ((flagc & FLAGC_LEFTADJ) && width > 0)
{
while (width--) PUT_CHAR (stix, padc);
}
break;
}
case 's':
{
const stix_bch_t* bsp;
stix_oow_t bslen, slen;
/* zeropad must not take effect for 'S' */
if (flagc & FLAGC_ZEROPAD) padc = ' ';
if (lm_flag & LF_H) goto lowercase_s;
lowercase_s:
bsp = va_arg (ap, stix_bch_t*);
if (bsp == STIX_NULL) bsp = bch_nullstr;
/* get the length */
for (bslen = 0; bsp[bslen]; bslen++);
if (stix_utf8toucs (bsp, &bslen, STIX_NULL, &slen) <= -1)
{
/* conversion error */
stix->errnum = STIX_EECERR;
goto oops;
}
/* slen holds the length after conversion */
n = slen;
if ((flagc & FLAGC_DOT) && precision < slen) n = precision;
width -= n;
if (!(flagc & FLAGC_LEFTADJ) && width > 0)
{
while (width--) PUT_CHAR (stix, padc);
}
{
stix_ooch_t conv_buf[32];
stix_oow_t i, conv_len, src_len, tot_len = 0;
while (n > 0)
{
STIX_ASSERT (bslen > tot_len);
src_len = bslen - tot_len;
conv_len = STIX_COUNTOF(conv_buf);
/* this must not fail since the dry-run above was successful */
stix_utf8toucs (&bsp[tot_len], &src_len, conv_buf, &conv_len);
tot_len += src_len;
if (conv_len > n) conv_len = n;
for (i = 0; i < conv_len; i++)
{
PUT_CHAR (stix, conv_buf[i]);
}
n -= conv_len;
}
}
if ((flagc & FLAGC_LEFTADJ) && width > 0)
{
while (width--) PUT_CHAR (stix, padc);
}
break;
}
case 'S':
{
const stix_ooch_t* sp;
/* zeropad must not take effect for 's' */
if (flagc & FLAGC_ZEROPAD) padc = ' ';
if (lm_flag & LF_L) goto uppercase_s;
uppercase_s:
sp = va_arg (ap, stix_ooch_t*);
if (sp == STIX_NULL) sp = ooch_nullstr;
if (flagc & FLAGC_DOT)
{
for (n = 0; n < precision && sp[n]; n++);
}
else
{
for (n = 0; sp[n]; n++);
}
width -= n;
if (!(flagc & FLAGC_LEFTADJ) && width > 0)
{
while (width--) PUT_CHAR (stix, padc);
}
while (n--) PUT_CHAR (stix, *sp++);
if ((flagc & FLAGC_LEFTADJ) && width > 0)
{
while (width--) PUT_CHAR (stix, padc);
}
break;
}
case 'O': /* object - ignore precision, width, adjustment */
print_object (stix, va_arg (ap, stix_oop_t));
break;
#if 0
case 'e':
case 'E':
case 'f':
case 'F':
case 'g':
case 'G':
/*
case 'a':
case 'A':
*/
{
/* let me rely on snprintf until i implement float-point to string conversion */
int q;
stix_oow_t fmtlen;
#if (STIX_SIZEOF___FLOAT128 > 0) && defined(HAVE_QUADMATH_SNPRINTF)
__float128 v_qd;
#endif
long double v_ld;
double v_d;
int dtype = 0;
stix_oow_t newcapa;
if (lm_flag & LF_J)
{
#if (STIX_SIZEOF___FLOAT128 > 0) && defined(HAVE_QUADMATH_SNPRINTF) && (STIX_SIZEOF_FLTMAX_T == STIX_SIZEOF___FLOAT128)
v_qd = va_arg (ap, stix_fltmax_t);
dtype = LF_QD;
#elif STIX_SIZEOF_FLTMAX_T == STIX_SIZEOF_DOUBLE
v_d = va_arg (ap, stix_fltmax_t);
#elif STIX_SIZEOF_FLTMAX_T == STIX_SIZEOF_LONG_DOUBLE
v_ld = va_arg (ap, stix_fltmax_t);
dtype = LF_LD;
#else
#error Unsupported stix_flt_t
#endif
}
else if (lm_flag & LF_Z)
{
/* stix_flt_t is limited to double or long double */
/* precedence goes to double if sizeof(double) == sizeof(long double)
* for example, %Lf didn't work on some old platforms.
* so i prefer the format specifier with no modifier.
*/
#if STIX_SIZEOF_FLT_T == STIX_SIZEOF_DOUBLE
v_d = va_arg (ap, stix_flt_t);
#elif STIX_SIZEOF_FLT_T == STIX_SIZEOF_LONG_DOUBLE
v_ld = va_arg (ap, stix_flt_t);
dtype = LF_LD;
#else
#error Unsupported stix_flt_t
#endif
}
else if (lm_flag & (LF_LD | LF_L))
{
v_ld = va_arg (ap, long double);
dtype = LF_LD;
}
#if (STIX_SIZEOF___FLOAT128 > 0) && defined(HAVE_QUADMATH_SNPRINTF)
else if (lm_flag & (LF_QD | LF_Q))
{
v_qd = va_arg (ap, __float128);
dtype = LF_QD;
}
#endif
else if (flagc & FLAGC_LENMOD)
{
stix->errnum = STIX_EINVAL;
goto oops;
}
else
{
v_d = va_arg (ap, double);
}
fmtlen = fmt - percent;
if (fmtlen > fltfmt->capa)
{
if (fltfmt->ptr == fltfmt->sbuf)
{
fltfmt->ptr = STIX_MMGR_ALLOC (STIX_MMGR_GETDFL(), STIX_SIZEOF(*fltfmt->ptr) * (fmtlen + 1));
if (fltfmt->ptr == STIX_NULL) goto oops;
}
else
{
stix_mchar_t* tmpptr;
tmpptr = STIX_MMGR_REALLOC (STIX_MMGR_GETDFL(), fltfmt->ptr, STIX_SIZEOF(*fltfmt->ptr) * (fmtlen + 1));
if (tmpptr == STIX_NULL) goto oops;
fltfmt->ptr = tmpptr;
}
fltfmt->capa = fmtlen;
}
/* compose back the format specifier */
fmtlen = 0;
fltfmt->ptr[fmtlen++] = '%';
if (flagc & FLAGC_SPACE) fltfmt->ptr[fmtlen++] = ' ';
if (flagc & FLAGC_SHARP) fltfmt->ptr[fmtlen++] = '#';
if (flagc & FLAGC_SIGN) fltfmt->ptr[fmtlen++] = '+';
if (flagc & FLAGC_LEFTADJ) fltfmt->ptr[fmtlen++] = '-';
if (flagc & FLAGC_ZEROPAD) fltfmt->ptr[fmtlen++] = '0';
if (flagc & FLAGC_STAR1) fltfmt->ptr[fmtlen++] = '*';
else if (flagc & FLAGC_WIDTH)
{
fmtlen += stix_fmtuintmaxtombs (
&fltfmt->ptr[fmtlen], fltfmt->capa - fmtlen,
width, 10, -1, '\0', STIX_NULL);
}
if (flagc & FLAGC_DOT) fltfmt->ptr[fmtlen++] = '.';
if (flagc & FLAGC_STAR2) fltfmt->ptr[fmtlen++] = '*';
else if (flagc & FLAGC_PRECISION)
{
fmtlen += stix_fmtuintmaxtombs (
&fltfmt->ptr[fmtlen], fltfmt->capa - fmtlen,
precision, 10, -1, '\0', STIX_NULL);
}
if (dtype == LF_LD)
fltfmt->ptr[fmtlen++] = 'L';
#if (STIX_SIZEOF___FLOAT128 > 0)
else if (dtype == LF_QD)
fltfmt->ptr[fmtlen++] = 'Q';
#endif
fltfmt->ptr[fmtlen++] = ch;
fltfmt->ptr[fmtlen] = '\0';
#if defined(HAVE_SNPRINTF)
/* nothing special here */
#else
/* best effort to avoid buffer overflow when no snprintf is available.
* i really can't do much if it happens. */
newcapa = precision + width + 32;
if (fltout->capa < newcapa)
{
STIX_ASSERT (fltout->ptr == fltout->sbuf);
fltout->ptr = STIX_MMGR_ALLOC (STIX_MMGR_GETDFL(), STIX_SIZEOF(char_t) * (newcapa + 1));
if (fltout->ptr == STIX_NULL) goto oops;
fltout->capa = newcapa;
}
#endif
while (1)
{
if (dtype == LF_LD)
{
#if defined(HAVE_SNPRINTF)
q = snprintf ((stix_mchar_t*)fltout->ptr, fltout->capa + 1, fltfmt->ptr, v_ld);
#else
q = sprintf ((stix_mchar_t*)fltout->ptr, fltfmt->ptr, v_ld);
#endif
}
#if (STIX_SIZEOF___FLOAT128 > 0) && defined(HAVE_QUADMATH_SNPRINTF)
else if (dtype == LF_QD)
{
q = quadmath_snprintf ((stix_mchar_t*)fltout->ptr, fltout->capa + 1, fltfmt->ptr, v_qd);
}
#endif
else
{
#if defined(HAVE_SNPRINTF)
q = snprintf ((stix_mchar_t*)fltout->ptr, fltout->capa + 1, fltfmt->ptr, v_d);
#else
q = sprintf ((stix_mchar_t*)fltout->ptr, fltfmt->ptr, v_d);
#endif
}
if (q <= -1) goto oops;
if (q <= fltout->capa) break;
newcapa = fltout->capa * 2;
if (newcapa < q) newcapa = q;
if (fltout->ptr == fltout->sbuf)
{
fltout->ptr = STIX_MMGR_ALLOC (STIX_MMGR_GETDFL(), STIX_SIZEOF(char_t) * (newcapa + 1));
if (fltout->ptr == STIX_NULL) goto oops;
}
else
{
char_t* tmpptr;
tmpptr = STIX_MMGR_REALLOC (STIX_MMGR_GETDFL(), fltout->ptr, STIX_SIZEOF(char_t) * (newcapa + 1));
if (tmpptr == STIX_NULL) goto oops;
fltout->ptr = tmpptr;
}
fltout->capa = newcapa;
}
if (STIX_SIZEOF(char_t) != STIX_SIZEOF(stix_mchar_t))
{
fltout->ptr[q] = '\0';
while (q > 0)
{
q--;
fltout->ptr[q] = ((stix_mchar_t*)fltout->ptr)[q];
}
}
sp = fltout->ptr;
flagc &= ~FLAGC_DOT;
width = 0;
precision = 0;
goto print_lowercase_s;
}
#endif
handle_nosign:
sign = 0;
if (lm_flag & LF_J)
{
#if defined(__GNUC__) && \
(STIX_SIZEOF_UINTMAX_T > STIX_SIZEOF_OOW_T) && \
(STIX_SIZEOF_UINTMAX_T != STIX_SIZEOF_LONG_LONG) && \
(STIX_SIZEOF_UINTMAX_T != STIX_SIZEOF_LONG)
/* GCC-compiled binaries crashed when getting stix_uintmax_t with va_arg.
* This is just a work-around for it */
int i;
for (i = 0, num = 0; i < STIX_SIZEOF(stix_uintmax_t) / STIX_SIZEOF(stix_oow_t); i++)
{
#if defined(STIX_ENDIAN_BIG)
num = num << (8 * STIX_SIZEOF(stix_oow_t)) | (va_arg (ap, stix_oow_t));
#else
register int shift = i * STIX_SIZEOF(stix_oow_t);
stix_oow_t x = va_arg (ap, stix_oow_t);
num |= (stix_uintmax_t)x << (shift * 8);
#endif
}
#else
num = va_arg (ap, stix_uintmax_t);
#endif
}
#if 0
else if (lm_flag & LF_T)
num = va_arg (ap, stix_ptrdiff_t);
#endif
else if (lm_flag & LF_Z)
num = va_arg (ap, stix_oow_t);
#if (STIX_SIZEOF_LONG_LONG > 0)
else if (lm_flag & LF_Q)
num = va_arg (ap, unsigned long long int);
#endif
else if (lm_flag & (LF_L | LF_LD))
num = va_arg (ap, unsigned long int);
else if (lm_flag & LF_H)
num = (unsigned short int)va_arg (ap, int);
else if (lm_flag & LF_C)
num = (unsigned char)va_arg (ap, int);
else
num = va_arg (ap, unsigned int);
goto number;
handle_sign:
if (lm_flag & LF_J)
{
#if defined(__GNUC__) && \
(STIX_SIZEOF_INTMAX_T > STIX_SIZEOF_OOI_T) && \
(STIX_SIZEOF_UINTMAX_T != STIX_SIZEOF_LONG_LONG) && \
(STIX_SIZEOF_UINTMAX_T != STIX_SIZEOF_LONG)
/* GCC-compiled binraries crashed when getting stix_uintmax_t with va_arg.
* This is just a work-around for it */
int i;
for (i = 0, num = 0; i < STIX_SIZEOF(stix_intmax_t) / STIX_SIZEOF(stix_oow_t); i++)
{
#if defined(STIX_ENDIAN_BIG)
num = num << (8 * STIX_SIZEOF(stix_oow_t)) | (va_arg (ap, stix_oow_t));
#else
register int shift = i * STIX_SIZEOF(stix_oow_t);
stix_oow_t x = va_arg (ap, stix_oow_t);
num |= (stix_uintmax_t)x << (shift * 8);
#endif
}
#else
num = va_arg (ap, stix_intmax_t);
#endif
}
#if 0
else if (lm_flag & LF_T)
num = va_arg(ap, stix_ptrdiff_t);
#endif
else if (lm_flag & LF_Z)
num = va_arg (ap, stix_ooi_t);
#if (STIX_SIZEOF_LONG_LONG > 0)
else if (lm_flag & LF_Q)
num = va_arg (ap, long long int);
#endif
else if (lm_flag & (LF_L | LF_LD))
num = va_arg (ap, long int);
else if (lm_flag & LF_H)
num = (short int)va_arg (ap, int);
else if (lm_flag & LF_C)
num = (char)va_arg (ap, int);
else
num = va_arg (ap, int);
number:
if (sign && (stix_intmax_t)num < 0)
{
neg = 1;
num = -(stix_intmax_t)num;
}
nbufp = sprintn (nbuf, num, base, &tmp);
if ((flagc & FLAGC_SHARP) && num != 0)
{
if (base == 8) tmp++;
else if (base == 16) tmp += 2;
}
if (neg) tmp++;
else if (flagc & FLAGC_SIGN) tmp++;
else if (flagc & FLAGC_SPACE) tmp++;
numlen = nbufp - nbuf;
if ((flagc & FLAGC_DOT) && precision > numlen)
{
/* extra zeros fro precision specified */
tmp += (precision - numlen);
}
if (!(flagc & FLAGC_LEFTADJ) && !(flagc & FLAGC_ZEROPAD) && width > 0 && (width -= tmp) > 0)
{
while (width--) PUT_CHAR (stix, padc);
}
if (neg) PUT_CHAR (stix, '-');
else if (flagc & FLAGC_SIGN) PUT_CHAR (stix, '+');
else if (flagc & FLAGC_SPACE) PUT_CHAR (stix, ' ');
if ((flagc & FLAGC_SHARP) && num != 0)
{
if (base == 8)
{
PUT_CHAR (stix, '0');
}
else if (base == 16)
{
PUT_CHAR (stix, '0');
PUT_CHAR (stix, 'x');
}
}
if ((flagc & FLAGC_DOT) && precision > numlen)
{
/* extra zeros for precision specified */
while (numlen < precision)
{
PUT_CHAR (stix, '0');
numlen++;
}
}
if (!(flagc & FLAGC_LEFTADJ) && width > 0 && (width -= tmp) > 0)
{
while (width-- > 0) PUT_CHAR (stix, padc);
}
while (*nbufp) PUT_CHAR (stix, *nbufp--); /* output actual digits */
if ((flagc & FLAGC_LEFTADJ) && width > 0 && (width -= tmp) > 0)
{
while (width-- > 0) PUT_CHAR (stix, padc);
}
break;
invalid_format:
while (percent < fmt) PUT_CHAR (stix, *percent++);
break;
default:
while (percent < fmt) PUT_CHAR (stix, *percent++);
/*
* Since we ignore an formatting argument it is no
* longer safe to obey the remaining formatting
* arguments as the arguments will no longer match
* the format specs.
*/
stop = 1;
break;
}
}
done:
return 0;
oops:
return (stix_ooi_t)-1;
}
#undef PUT_CHAR

View File

@ -329,7 +329,7 @@ printf ("MOD_GETSYM [%s]\n", &buf[0]);
}
/* ========================================================================= */
static void log_write (stix_t* stix, int mask, const stix_ooch_t* msg, stix_oow_t len)
{
#if defined(_WIN32)
@ -342,10 +342,10 @@ static void log_write (stix_t* stix, int mask, const stix_ooch_t* msg, stix_oow_
msgidx = 0;
while (1)
while (len > 0)
{
ucslen = len;
bcslen = STIX_COUNTOF(buf) - len;
bcslen = STIX_COUNTOF(buf);
n = stix_ucstoutf8 (&msg[msgidx], &ucslen, buf, &bcslen);
if (n == 0)
@ -592,7 +592,6 @@ int main (int argc, char* argv[])
return -1;
}
{
stix_oow_t tab_size;

View File

@ -573,8 +573,32 @@ struct stix_compiler_t
} mth;
};
typedef struct stix_bchbuf_t stix_bchbuf_t;
struct stix_bchbuf_t
{
stix_bch_t sbuf[128];
stix_bch_t* ptr;
stix_oow_t capa;
};
typedef struct stix_oochbuf_t stix_oochbuf_t;
struct stix_oochbuf_t
{
stix_ooch_t sbuf[128];
stix_ooch_t* ptr;
stix_oow_t capa;
};
struct stix_decoder_t
{
stix_oochbuf_t fltout;
stix_bchbuf_t fltfmt;
};
#endif
#if defined(STIX_USE_OBJECT_TRAILER)
/* let it point to the trailer of the method */
# define SET_ACTIVE_METHOD_CODE(stix) ((stix)->active_code = (stix_oob_t*)&(stix)->active_method->slot[STIX_OBJ_GET_SIZE((stix)->active_method) + 1 - STIX_METHOD_NAMED_INSTVARS])
@ -1299,6 +1323,21 @@ int stix_getprimno (
const stix_oocs_t* name
);
/* ========================================================================= */
/* fmt.c */
/* ========================================================================= */
stix_ooi_t stix_bfmtout (
stix_t* stix,
const stix_bch_t* fmt,
...
);
stix_ooi_t stix_oofmtout (
stix_t* stix,
const stix_ooch_t* fmt,
...
);
/* TODO: remove debugging functions */
/* ========================================================================= */
/* debug.c */
@ -1309,6 +1348,8 @@ void print_oocs (const stix_oocs_t* name);
void print_object (stix_t* stix, stix_oop_t oop);
void dump_object (stix_t* stix, stix_oop_t oop, const char* title);
#if defined(__cplusplus)
}
#endif

View File

@ -55,7 +55,7 @@ extern "C" {
/* ========================================================================= */
/* stix-utl.c */
/* utl.c */
/* ========================================================================= */
stix_oow_t stix_hashbytes (
const stix_oob_t* ptr,
@ -147,6 +147,7 @@ stix_oow_t stix_countbcstr (
const stix_bch_t* str
);
#if defined(__cplusplus)
}
#endif

View File

@ -157,6 +157,16 @@ void stix_fini (stix_t* stix)
/* deregister all callbacks */
while (stix->cblist) stix_deregcb (stix, stix->cblist);
if (stix->log.ptr)
{
/* make sure to flush your log message */
/* TODO: flush unwritten message */
stix_freemem (stix, stix->log.ptr);
stix->log.capa = 0;
stix->log.len = 0;
}
}
stix_mmgr_t* stix_getmmgr (stix_t* stix)

View File

@ -738,6 +738,7 @@ typedef struct stix_prim_mod_data_t stix_prim_mod_data_t;
* ========================================================================= */
#if defined(STIX_INCLUDE_COMPILER)
typedef struct stix_compiler_t stix_compiler_t;
typedef struct stix_decoder_t stix_decoder_t;
#endif
struct stix_t
@ -757,6 +758,14 @@ struct stix_t
stix_cb_t* cblist;
stix_rbt_t pmtable; /* primitive module table */
struct
{
stix_ooch_t* ptr;
stix_oow_t len;
stix_oow_t capa;
} log;
/* ========================= */
stix_heap_t* permheap; /* TODO: put kernel objects to here */
@ -841,11 +850,10 @@ struct stix_t
#if defined(STIX_INCLUDE_COMPILER)
stix_compiler_t* c;
stix_decoder_t* d;
#endif
};
#if defined(STIX_USE_PROCSTK)
/* TODO: stack bound check when pushing */
#define STIX_STACK_PUSH(stix,v) \

View File

@ -496,7 +496,7 @@ int stix_utf8toucs (const stix_bch_t* bcs, stix_oow_t* bcslen, stix_uch_t* ucs,
}
}
int stix_ucstoutf8 (const stix_uch_t* ucs, stix_oow_t *ucslen, stix_bch_t* bcs, stix_oow_t* bcslen)
int stix_ucstoutf8 (const stix_uch_t* ucs, stix_oow_t* ucslen, stix_bch_t* bcs, stix_oow_t* bcslen)
{
if (*ucslen == ~(stix_oow_t)0)
{
@ -514,7 +514,7 @@ int stix_ucstoutf8 (const stix_uch_t* ucs, stix_oow_t *ucslen, stix_bch_t* bcs,
stix_oow_t stix_ucslen (const stix_uch_t* ucs)
{
const stix_uch_t* ptr = ucs;
while (*ptr) ptr = STIX_INCPTR(const stix_uch_t, ptr, 1);
return STIX_SUBPTR(const stix_uch_t, ptr, ucs);
while (*ptr) ptr++;
return ptr - ucs;
}
*/

View File

@ -1,4 +1,3 @@
/*
* $Id$
*
@ -188,4 +187,3 @@ stix_bch_t* stix_findbchar (const stix_bch_t* ptr, stix_oow_t len, stix_bch_t c)
return STIX_NULL;
}