Skip to content

Commit

Permalink
GC-safe parser (#9) WIP
Browse files Browse the repository at this point in the history
move struct _GREG to greg.h, expose it in P as P->parser.
Idea by Peter Arthur
  • Loading branch information
rurban committed Dec 2, 2019
1 parent 3979057 commit d69e148
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 123 deletions.
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ core/potion.o: core/potion.c core/potion.h core/config.h core/internal.h \
core/opcodes.h core/khash.h core/table.h
@${ECHO} CC $@ -O0
@${CC} -c ${CFLAGS} -O0 ${INCS} -o $@ $<
core/syntax.o: core/syntax.c core/potion.h core/config.h core/internal.h \
core/asm.h core/ast.h tools/greg.h
@${ECHO} CC $@
@${CC} -c ${CFLAGS} ${INCS} -Itools -o $@ $<
ifneq (${FPIC},)
core/callcc.${OPIC}: core/callcc.c core/potion.h core/config.h core/internal.h
@${ECHO} CC $@ -O0 +frame-pointer
Expand All @@ -182,6 +186,10 @@ core/potion.${OPIC}: core/potion.c core/potion.h core/config.h core/internal.h \
core/opcodes.h core/khash.h core/table.h
@${ECHO} CC $@ -O0
@${CC} -c ${CFLAGS} -O0 ${FPIC} ${INCS} -o $@ $<
core/syntax.${OPIC}: core/syntax.c core/potion.h core/config.h core/internal.h \
core/asm.h core/ast.h tools/greg.h
@${ECHO} CC $@
@${CC} -c ${CFLAGS} ${FPIC} ${INCS} -Itools -o $@ $<
endif

core/potion.h: core/config.h
Expand Down Expand Up @@ -259,7 +267,7 @@ endif

${GREG}: tools/greg.c tools/compile.c tools/tree.c
@${ECHO} CC $@
@${CC} ${GREGCFLAGS} -o $@ tools/greg.c tools/compile.c tools/tree.c -Itools
@${CC} ${GREGCFLAGS} -Itools -o $@ tools/greg.c tools/compile.c tools/tree.c

bin/potion${EXE}: ${PIC_OBJ_POTION} lib/libpotion${DLL}
@${ECHO} LINK $@
Expand Down
53 changes: 53 additions & 0 deletions core/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ by Basile Starynkevitch.
#include "gc.h"
#include "khash.h"
#include "table.h"
#include "../tools/greg.h"

#if defined(DEBUG)
//mingw32 has struct timeval in sys/time.h
Expand Down Expand Up @@ -133,6 +134,56 @@ static inline int NEW_BIRTH_REGION(struct PNMemory *M, void **wb, int sz) {
return sz;
}

void potion_gc_minor_parser(PN parser) {
if(parser == 0)
return;

struct _GREG *G = (struct _GREG *)parser;
struct PNMemory *M = ((Potion *)G->data)->mem;
Potion *P = G->data;

if(PN_IS_PTR(G->ss)) {
GC_MINOR_UPDATE(G->ss);
potion_mark_minor(G->data, (const struct PNObject *) G->ss);
}
if(PN_IS_PTR(G->val[0])) {
GC_MINOR_UPDATE(G->val[0]);
potion_mark_minor(G->data, (const struct PNObject *) G->val[0]);
}
int c = G->val - G->vals;
for(int i = 0; i < c; i++) {
if(PN_IS_PTR(G->vals[i])) {
GC_MINOR_UPDATE(G->vals[i]);
potion_mark_minor(G->data, (const struct PNObject *) G->vals[i]);
}
}
}

void potion_gc_major_parser(PN parser) {
if(parser == 0)
return;

struct _GREG *G = (struct _GREG *)parser;
struct PNMemory *M = ((Potion *)G->data)->mem;
Potion *P = G->data;

if(PN_IS_PTR(G->ss)) {
GC_MAJOR_UPDATE(G->ss);
potion_mark_major(P, (const struct PNObject *) G->ss);
}
if(PN_IS_PTR(G->val[0])) {
GC_MAJOR_UPDATE(G->val[0]);
potion_mark_major(P, (const struct PNObject *) G->val[0]);
}
int c = G->val - G->vals;
for(int i = 0; i < c; i++) {
if(G->vals[i] != NULL && PN_IS_PTR(G->vals[i])) {
GC_MAJOR_UPDATE(G->vals[i]);
potion_mark_major(P, (const struct PNObject *) G->vals[i]);
}
}
}

/** \par
Both this function and potion_gc_major embody a simple
Cheney loop (also called a "two-finger collector.")
Expand Down Expand Up @@ -161,6 +212,7 @@ static int potion_gc_minor(Potion *P, int sz) {
potion_mark_stack(P, 1);

GC_MINOR_STRINGS();
potion_gc_minor_parser(P->parser);

wb = (void **)M->birth_storeptr;
for (storead = wb; storead < (void **)M->birth_hi; storead++) {
Expand Down Expand Up @@ -239,6 +291,7 @@ static int potion_gc_major(Potion *P, int siz) {
scanptr = 0;

GC_MAJOR_STRINGS();
potion_gc_minor_parser(P->parser);

pngc_page_delete((void *)prevoldlo, (char *)prevoldhi - (char *)prevoldlo);
prevoldlo = 0;
Expand Down
2 changes: 1 addition & 1 deletion core/potion.h
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ struct Potion_State {
int prec; ///< double precision

//parser-only:
PN input, source; ///< parser input and output (AST)
PN input, source, parser;///< parser input and output (AST)
int yypos; ///< parser buffer position
PNAsm * volatile pbuf; ///< parser buffer
PN line; ///< currently parsed line (for debug)
Expand Down
2 changes: 2 additions & 0 deletions core/syntax.y
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ PN potion_parse(Potion *P, PN code, char *filename) {
P->yypos = 0;
P->input = code;
P->source = PN_NIL;
P->parser = (PN)G;
P->pbuf = potion_asm_new(P);
#ifdef YY_DEBUG
yydebug = P->flags;
Expand Down Expand Up @@ -449,6 +450,7 @@ PN potion_sig(Potion *P, char *fmt) {
P->yypos = 0;
P->input = potion_byte_str(P, fmt);
P->source = out = PN_TUP0();
P->parser = (PN)G;
P->pbuf = NULL;
#ifdef YY_DEBUG
yydebug = P->flags;
Expand Down
39 changes: 4 additions & 35 deletions tools/compile.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* Copyright (c) 2007 by Ian Piumarta
* Copyright (c) 2011 by Amos Wenger nddrylliog@gmail.com
* Copyright (c) 2013 by perl11 org
* Copyright (c) 2013,2019 by perl11 org
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
Expand Down Expand Up @@ -470,7 +470,7 @@ static char *header= "\
#include <stdio.h>\n\
#include <stdlib.h>\n\
#include <string.h>\n\
struct _GREG;\n\
#include \"greg.h\"\n\
";

static char *preamble= "\
Expand Down Expand Up @@ -564,12 +564,6 @@ static char *preamble= "\
# define yyprintfvokrule(rule)\n\
# define yyprintfvfailrule(rule)\n\
#endif\n\
#ifndef YYSTYPE\n\
#define YYSTYPE int\n\
#endif\n\
#ifndef YY_XTYPE\n\
#define YY_XTYPE void *\n\
#endif\n\
#ifndef YY_XVAR\n\
#define YY_XVAR yyxvar\n\
#endif\n\
Expand All @@ -586,35 +580,9 @@ static char *preamble= "\
#define yydata G->data\n\
#define yy G->ss\n\
\n\
struct _yythunk; // forward declaration\n\
typedef void (*yyaction)(struct _GREG *G, char *yytext, int yyleng, struct _yythunk *thunkpos, YY_XTYPE YY_XVAR);\n\
typedef struct _yythunk { int begin, end; yyaction action; const char *name; struct _yythunk *next; } yythunk;\n\
\n\
typedef struct _GREG {\n\
char *buf;\n\
int buflen;\n\
int offset;\n\
int pos;\n\
int limit;\n\
char *text;\n\
int textlen;\n\
int begin;\n\
int end;\n\
yythunk *thunks;\n\
int thunkslen;\n\
int thunkpos;\n\
int lineno;\n\
char *filename;\n\
FILE *input;\n\
YYSTYPE ss;\n\
YYSTYPE *val;\n\
YYSTYPE *vals;\n\
int valslen;\n\
YY_XTYPE data;\n\
#ifdef YY_DEBUG\n\
int debug;\n\
#endif\n\
} GREG;\n\
\n\
YY_LOCAL(int) yyrefill(GREG *G)\n\
{\n\
Expand Down Expand Up @@ -925,7 +893,8 @@ YY_PARSE(void) YY_NAME(parse_free)(GREG *G)\n\

void Rule_compile_c_header(void)
{
fprintf(output, "/* A recursive-descent parser generated by greg %d.%d.%d */\n", GREG_MAJOR, GREG_MINOR, GREG_LEVEL);
fprintf(output, "/* A recursive-descent parser generated by greg %d.%d.%d */\n",
GREG_MAJOR, GREG_MINOR, GREG_LEVEL);
fprintf(output, "\n");
fprintf(output, "%s", header);
fprintf(output, "#define YYRULECOUNT %d\n", ruleCount);
Expand Down
Loading

0 comments on commit d69e148

Please sign in to comment.