diff options
author | Marek Safar <marek.safar@gmail.com> | 2010-08-29 20:53:47 +0400 |
---|---|---|
committer | Marek Safar <marek.safar@gmail.com> | 2010-08-31 11:33:56 +0400 |
commit | 17e20d8a72833e0c38501e77b660d580bc5f0b89 (patch) | |
tree | 4b741591c4b5d4fa3dd7f23d9790c23e83edf719 | |
parent | 45f89e6fd6ea1b73e217c7279a3cd659f6d54f38 (diff) |
Wrap any non-trivial rule into a method, speeds up the parser by 10-20%.
-rw-r--r-- | mcs/jay/defs.h | 2 | ||||
-rw-r--r-- | mcs/jay/main.c | 2 | ||||
-rw-r--r-- | mcs/jay/output.c | 35 | ||||
-rw-r--r-- | mcs/jay/reader.c | 129 | ||||
-rw-r--r-- | mcs/jay/skeleton.cs | 19 |
5 files changed, 133 insertions, 54 deletions
diff --git a/mcs/jay/defs.h b/mcs/jay/defs.h index 9e50b38d869..2aade48dac2 100644 --- a/mcs/jay/defs.h +++ b/mcs/jay/defs.h @@ -249,6 +249,7 @@ extern int nsyms; extern int ntokens; extern int nvars; extern int ntags; +extern int nmethods; extern char *line_format; extern char *default_line_format; @@ -258,6 +259,7 @@ extern char **symbol_name; extern short *symbol_value; extern short *symbol_prec; extern char *symbol_assoc; +extern char **methods; extern short *ritem; extern short *rlhs; diff --git a/mcs/jay/main.c b/mcs/jay/main.c index c074824b402..b47867d3e33 100644 --- a/mcs/jay/main.c +++ b/mcs/jay/main.c @@ -76,12 +76,14 @@ int nrules; int nsyms; int ntokens; int nvars; +int nmethods; int start_symbol; char **symbol_name; short *symbol_value; short *symbol_prec; char *symbol_assoc; +char **methods; short *ritem; short *rlhs; diff --git a/mcs/jay/output.c b/mcs/jay/output.c index 41ebfb1dac3..d1e2c14a1b2 100644 --- a/mcs/jay/output.c +++ b/mcs/jay/output.c @@ -103,10 +103,21 @@ output_rule_data() register int i; register int j; + printf("/*\n All more than 3 lines long rules are wrapped into a method\n*/\n"); + + for (i = 0; i < nmethods; ++i) + { + printf("%s", methods[i]); + FREE(methods[i]); + printf("\n\n"); + } + FREE(methods); + + printf(default_line_format, ++outline + 1); printf(" %s static %s short [] yyLhs = {%16d,", csharp ? "" : " protected", - csharp ? "" : " final", + csharp ? "readonly" : "final", symbol_value[start_symbol]); j = 10; @@ -128,7 +139,7 @@ output_rule_data() printf(" %s static %s short [] yyLen = {%12d,", csharp ? "" : "protected", - csharp ? "" : "final", + csharp ? "readonly" : "final", 2); j = 10; @@ -156,7 +167,7 @@ output_yydefred() printf(" %s static %s short [] yyDefRed = {%13d,", csharp ? "" : "protected", - csharp ? "" : "final", + csharp ? "readonly" : "final", (defred[0] ? defred[0] - 2 : 0)); j = 10; @@ -298,7 +309,7 @@ goto_actions() state_count = NEW2(nstates, short); k = default_goto(start_symbol + 1); - printf(" protected static %s short [] yyDgoto = {%14d,", csharp ? "" : "final", k); + printf(" protected static %s short [] yyDgoto = {%14d,", csharp ? "readonly" : "final", k); save_column(start_symbol + 1, k); j = 10; @@ -622,7 +633,7 @@ output_base() { register int i, j; - printf(" protected static %s short [] yySindex = {%13d,", csharp?"":"final", base[0]); + printf(" protected static %s short [] yySindex = {%13d,", csharp? "readonly":"final", base[0]); j = 10; for (i = 1; i < nstates; i++) @@ -641,7 +652,7 @@ output_base() outline += 2; printf("\n };\n protected static %s short [] yyRindex = {%13d,", - csharp ? "" : "final", + csharp ? "readonly" : "final", base[nstates]); j = 10; @@ -661,7 +672,7 @@ output_base() outline += 2; printf("\n };\n protected static %s short [] yyGindex = {%13d,", - csharp ? "" : "final", + csharp ? "readonly" : "final", base[2*nstates]); j = 10; @@ -691,7 +702,7 @@ output_table() register int i; register int j; - printf(" protected static %s short [] yyTable = {%14d,", csharp ? "" : "final", table[0]); + printf(" protected static %s short [] yyTable = {%14d,", csharp ? "readonly" : "final", table[0]); j = 10; for (i = 1; i <= high; i++) @@ -721,7 +732,7 @@ output_check() register int j; printf(" protected static %s short [] yyCheck = {%14d,", - csharp ? "" : "final", + csharp ? "readonly" : "final", check[0]); j = 10; @@ -851,13 +862,13 @@ output_debug() char * prefix = tflag ? "" : "//t"; ++outline; - printf(" protected static %s int yyFinal = %d;\n", csharp ? "" : "final", final_state); + printf(" protected %s int yyFinal = %d;\n", csharp ? "const" : "static final", final_state); ++outline; printf ("%s // Put this array into a separate class so it is only initialized if debugging is actually used\n", prefix); printf ("%s // Use MarshalByRefObject to disable inlining\n", prefix); printf("%s class YYRules %s {\n", prefix, csharp ? ": MarshalByRefObject" : ""); - printf("%s public static %s string [] yyRule = {\n", prefix, csharp ? "" : "final"); + printf("%s public static %s string [] yyRule = {\n", prefix, csharp ? "readonly" : "final"); for (i = 2; i < nrules; ++i) { printf("%s \"%s :", prefix, symbol_name[rlhs[i]]); @@ -920,7 +931,7 @@ output_debug() /* need yyNames for yyExpecting() */ - printf(" protected static %s string [] yyNames = {", csharp ? "" : "final"); + printf(" protected static %s string [] yyNames = {", csharp ? "readonly" : "final"); symnam = (char **) MALLOC((max+1)*sizeof(char *)); if (symnam == 0) no_space(); diff --git a/mcs/jay/reader.c b/mcs/jay/reader.c index 7096a6b4bba..f089456ed71 100644 --- a/mcs/jay/reader.c +++ b/mcs/jay/reader.c @@ -68,6 +68,8 @@ bucket **pitem; int maxrules; bucket **plhs; +int maxmethods; + int name_pool_size; char *name_pool; @@ -846,6 +848,7 @@ initialize_grammar() pitem[2] = 0; pitem[3] = 0; + nmethods = 0; nrules = 3; maxrules = 100; plhs = (bucket **) MALLOC(maxrules*sizeof(bucket *)); @@ -1037,21 +1040,25 @@ copy_action() int depth; int quote; char *tag; - register FILE *f = action_file; + FILE *f = action_file; int a_lineno = lineno; char *a_line = dup_line(); char *a_cptr = a_line + (cptr - line); + char buffer [10000]; + int len = 0; + int comment_lines = 0; + char *mbody; + memset (buffer, 0, 10000); if (last_was_action) insert_empty_rule(); last_was_action = 1; fprintf(f, "case %d:\n", nrules - 2); - fprintf(f, line_format, lineno, input_file_name); - putc(' ', f); putc(' ', f); if (*cptr == '=') ++cptr; n = 0; + for (i = nitems - 1; pitem[i]; --i) ++n; depth = 0; @@ -1069,9 +1076,13 @@ loop: tag = get_tag(1); c = *cptr; if (c == '$') - { if (tag && strcmp(tag, "Object")) - fprintf(f, "((%s)yyVal)", tag); - else fprintf(f, "yyVal"); + { + if (tag && strcmp(tag, "Object")) { + len += sprintf(buffer + len, "((%s)yyVal)", tag); + } else { + strcat (buffer + len, "yyVal"); + len += 5; + } ++cptr; FREE(d_line); goto loop; @@ -1081,8 +1092,9 @@ loop: i = get_number(); if (i > n) dollar_warning(d_lineno, i); if (tag && strcmp(tag, "Object")) - fprintf(f, "((%s)yyVals[%d+yyTop])", tag, i - n); - else fprintf(f, "yyVals[%d+yyTop]", i - n); + len += sprintf(buffer + len, "((%s)yyVals[%d+yyTop])", tag, i - n); + else + len += sprintf(buffer + len, "yyVals[%d+yyTop]", i - n); FREE(d_line); goto loop; } @@ -1091,8 +1103,9 @@ loop: ++cptr; i = -get_number() - n; if (tag && strcmp(tag, "Object")) - fprintf(f, "((%s)yyVals[%d+yyTop])", tag, i); - else fprintf(f, "yyVals[%d+yyTop]", i); + len += sprintf(buffer + len, "((%s)yyVals[%d+yyTop])", tag, i); + else + len += sprintf(buffer + len, "yyVals[%d+yyTop]", i); FREE(d_line); goto loop; } @@ -1103,7 +1116,8 @@ loop: { if (ntags && plhs[nrules]->tag == 0) untyped_lhs(); - fprintf(f, "yyVal"); + strcat (buffer, "yyVal"); + len += 5; cptr += 2; goto loop; } @@ -1118,17 +1132,18 @@ loop: tag = pitem[nitems + i - n - 1]->tag; if (tag == 0) untyped_rhs(i, pitem[nitems + i - n - 1]->name), - fprintf(f, "yyVals[%d+yyTop]", i - n); + len += sprintf(buffer + len, "yyVals[%d+yyTop]", i - n); else if (strcmp(tag, "Object")) - fprintf(f, "((%s)yyVals[%d+yyTop])", tag, i - n); + len += sprintf(buffer + len, "((%s)yyVals[%d+yyTop])", tag, i - n); else - fprintf(f, "yyVals[%d+yyTop]", i - n); + len += sprintf(buffer + len, "yyVals[%d+yyTop]", i - n); } else { if (i > n) dollar_warning(lineno, i); - fprintf(f, "yyVals[%d+yyTop]", i - n); + + len += sprintf(buffer + len,"yyVals[%d+yyTop]", i - n); } goto loop; } @@ -1138,7 +1153,7 @@ loop: i = get_number(); if (ntags) unknown_rhs(-i); - fprintf(f, "yyVals[%d+yyTop]", -i - n); + len += sprintf(buffer + len, "yyVals[%d+yyTop]", -i - n); goto loop; } } @@ -1146,12 +1161,12 @@ loop: { do { - putc(c, f); + buffer[len++] = c; c = *++cptr; } while (isalnum(c) || c == '_' || c == '$'); goto loop; } - putc(c, f); + buffer[len++] = c; ++cptr; switch (c) { @@ -1163,8 +1178,7 @@ loop: case ';': if (depth > 0) goto loop; - fprintf(f, "\nbreak;\n"); - return; + break; case '{': ++depth; @@ -1172,8 +1186,7 @@ loop: case '}': if (--depth > 0) goto loop; - fprintf(f, "\n break;\n"); - return; + break; case '\'': case '"': @@ -1186,7 +1199,7 @@ loop: for (;;) { c = *cptr++; - putc(c, f); + buffer[len++] = c; if (c == quote) { FREE(s_line); @@ -1197,7 +1210,7 @@ loop: if (c == '\\') { c = *cptr++; - putc(c, f); + buffer[len++] = c; if (c == '\n') { get_line(); @@ -1212,15 +1225,19 @@ loop: c = *cptr; if (c == '/') { - putc('*', f); + buffer[len++] = '*'; while ((c = *++cptr) != '\n') { - if (c == '*' && cptr[1] == '/') - fprintf(f, "* "); - else - putc(c, f); - } - fprintf(f, "*/\n"); + if (c == '*' && cptr[1] == '/'){ + buffer[len++] = '*'; + buffer[len++] = ' '; + } else { + buffer[len++] = c; + } + } + buffer[len++] = '*'; + buffer[len++] = '/'; + buffer[len++] = '\n'; goto next_line; } if (c == '*') @@ -1229,21 +1246,22 @@ loop: char *c_line = dup_line(); char *c_cptr = c_line + (cptr - line - 1); - putc('*', f); + buffer[len++] = '*'; ++cptr; for (;;) { c = *cptr++; - putc(c, f); + buffer[len++] = c; if (c == '*' && *cptr == '/') { - putc('/', f); + buffer[len++] = '/'; ++cptr; FREE(c_line); goto loop; } if (c == '\n') { + ++comment_lines; get_line(); if (line == 0) unterminated_comment(c_lineno, c_line, c_cptr); @@ -1255,6 +1273,49 @@ loop: default: goto loop; } + + if (comment_lines > 0) + comment_lines++; + + if ((lineno - (a_lineno + comment_lines)) > 2) + { + char mname[20]; + char line_define[256]; + + sprintf(mname, "case_%d()", nrules - 2); + + putc(' ', f); putc(' ', f); + fputs(mname, f); + fprintf(f, ";"); + if (nmethods == 0) + { + maxmethods = 100; + methods = NEW2(maxmethods, char *); + } + else if (nmethods == maxmethods) + { + maxmethods += 500; + methods = REALLOC (methods, maxmethods*sizeof(char *)); + } + + sprintf(line_define, line_format, a_lineno, input_file_name); + + mbody = NEW2(5+strlen(line_define)+1+strlen(mname)+strlen(buffer)+1, char); + strcpy(mbody, "void "); + strcat(mbody, mname); + strcat(mbody, "\n"); + strcat(mbody, line_define); + strcat(mbody, buffer); + methods[nmethods++] = mbody; + } + else + { + fprintf(f, line_format, lineno, input_file_name); + putc(' ', f); putc(' ', f); + fwrite(buffer, 1, len, f); + } + + fprintf(f, "\n break;\n"); } diff --git a/mcs/jay/skeleton.cs b/mcs/jay/skeleton.cs index db18294b1b2..f8fc4370b1f 100644 --- a/mcs/jay/skeleton.cs +++ b/mcs/jay/skeleton.cs @@ -126,6 +126,10 @@ t this.debug = (yydebug.yyDebug)yyd; . static int[] global_yyStates; . static object[] global_yyVals; . protected bool use_global_stacks; +. object[] yyVals; // value stack +. object yyVal; // value stack ptr +. int yyToken; // current input +. int yyTop; . . /** the generated parser. . Maintains a state and a value stack, currently with fixed maximum size. @@ -138,9 +142,8 @@ t this.debug = (yydebug.yyDebug)yyd; . if (yyMax <= 0) yyMax = 256; // initial size . int yyState = 0; // state stack ptr . int [] yyStates; // state stack -. Object yyVal = null; // value stack ptr -. Object [] yyVals; // value stack -. int yyToken = -1; // current input +. yyVal = null; +. yyToken = -1; . int yyErrorFlag = 0; // #tks to shift . if (use_global_stacks && global_yyStates != null) { . yyVals = global_yyVals; @@ -156,7 +159,7 @@ t this.debug = (yydebug.yyDebug)yyd; . local ## %{ ... %} after the first %% -. /*yyLoop:*/ for (int yyTop = 0;; ++ yyTop) { +. /*yyLoop:*/ for (yyTop = 0;; ++ yyTop) { . if (yyTop >= yyStates.Length) { // dynamically increase . global::System.Array.Resize (ref yyStates, yyStates.Length+yyMax); . global::System.Array.Resize (ref yyVals, yyVals.Length+yyMax); @@ -165,7 +168,7 @@ t this.debug = (yydebug.yyDebug)yyd; . yyVals[yyTop] = yyVal; t if (debug != null) debug.push(yyState, yyVal); . -. /*yyDiscarded:*/ for (;;) { // discarding a token does not change stack +. /*yyDiscarded:*/ while (true) { // discarding a token does not change stack . int yyN; . if ((yyN = yyDefRed[yyState]) == 0) { // else [default] reduce (yyN) . if (yyToken < 0) { @@ -228,7 +231,7 @@ t yyLex.value()); . int yyV = yyTop + 1-yyLen[yyN]; t if (debug != null) t debug.reduce(yyState, yyStates[yyV-1], yyN, YYRules.getRule (yyN), yyLen[yyN]); -. yyVal = yyDefault(yyV > yyTop ? null : yyVals[yyV]); +. yyVal = yyV > yyTop ? null : yyVals[yyV]; // yyVal = yyDefault(yyV > yyTop ? null : yyVals[yyV]); . switch (yyN) { actions ## code from the actions within the grammar @@ -259,9 +262,9 @@ t if (debug != null) debug.accept(yyVal); . yyState = yyDgoto[yyM]; t if (debug != null) debug.shift(yyStates[yyTop], yyState); . goto continue_yyLoop; -. continue_yyDiscarded: continue; // implements the named-loop continue: 'continue yyDiscarded' +. continue_yyDiscarded: ; // implements the named-loop continue: 'continue yyDiscarded' . } -. continue_yyLoop: continue; // implements the named-loop continue: 'continue yyLoop' +. continue_yyLoop: ; // implements the named-loop continue: 'continue yyLoop' . } . } . |