From 596e3664cda06be2813326f2dd44c573d43c1c57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20H=C3=BCbinette=20=28Hubbe=29?= <hubbe@hubbe.net> Date: Tue, 31 Mar 1998 16:47:49 -0800 Subject: [PATCH] optimized, improved and bugfixed Rev: bin/mkpeep.pike:1.7 Rev: src/cpp.c:1.22 Rev: src/interpret.c:1.73 Rev: src/language.yacc:1.68 Rev: src/lex.c:1.47 Rev: src/peep.c:1.21 Rev: src/peep.in:1.14 --- bin/mkpeep.pike | 26 +++++++++++++++----------- src/cpp.c | 23 +++++++++++++++-------- src/interpret.c | 22 +++++++++++++++++++++- src/language.yacc | 3 ++- src/lex.c | 4 +++- src/peep.c | 10 +++++----- src/peep.in | 38 +++++++++++++++++++++++++++----------- 7 files changed, 88 insertions(+), 38 deletions(-) diff --git a/bin/mkpeep.pike b/bin/mkpeep.pike index c294fbe090..50aff84642 100755 --- a/bin/mkpeep.pike +++ b/bin/mkpeep.pike @@ -1,6 +1,6 @@ #!/usr/local/bin/pike -/* $Id: mkpeep.pike,v 1.6 1997/09/22 01:00:49 hubbe Exp $ */ +/* $Id: mkpeep.pike,v 1.7 1998/04/01 00:47:18 hubbe Exp $ */ import Simulate; @@ -9,7 +9,7 @@ import Simulate; string skipwhite(string s) { #if DEBUG > 9 - perror("skipwhite("+s+")\n"); + werror("skipwhite("+s+")\n"); #endif sscanf(s,"%*[ \t\n]%s",s); @@ -22,7 +22,7 @@ int find_end(string s) int e,parlvl=1; #if DEBUG > 8 - perror("find_end("+s+")\n"); + werror("find_end("+s+")\n"); #endif for(e=1;e<strlen(s);e++) @@ -37,7 +37,7 @@ int find_end(string s) break; } } - perror("Syntax error.\n"); + werror("Syntax error (1).\n"); exit(1); } @@ -51,7 +51,7 @@ mixed split(string s) opcodes=0; #ifdef DEBUG - perror("split("+s+")\n"); + werror("split("+s+")\n"); #endif b=({}); @@ -102,7 +102,7 @@ mixed split(string s) int i=search(b, ":"); if(i==-1) { - perror("Syntax error.\n"); + werror("Syntax error (%O).\n",b); return 0; } @@ -145,7 +145,7 @@ mixed split(string s) } #ifdef DEBUG - perror(sprintf("%O\n",({a,b}))); + werror(sprintf("%O\n",({a,b}))); #endif return ({a,b,opcodes, line}); @@ -162,7 +162,7 @@ string treat(string expr) string num, type, rest; if(sscanf(tmp[e],"%d%c%s",num,type,rest)!=3) { - perror("Syntax error.\n"); + werror("Syntax error (3).\n"); exit(2); } num--; @@ -354,15 +354,19 @@ int main(int argc, string *argv) mapping tests=([]); - f=read_bytes(argv[1]); + f=cpp(read_bytes(argv[1]),argv[1]); foreach(explode(f,"\n"),f) { string *a,*b; mapping tmp; sscanf(f,"%s#",f); - if(!strlen(f)) continue; - data+=({split(f)}); + foreach(f/";",f) + { + f=skipwhite(f); + if(!strlen(f)) continue; + data+=({split(f)}); + } } // write(sprintf("%O\n",data)); diff --git a/src/cpp.c b/src/cpp.c index 2e47e3f933..32e01c8c85 100644 --- a/src/cpp.c +++ b/src/cpp.c @@ -5,7 +5,7 @@ \*/ /* - * $Id: cpp.c,v 1.21 1998/03/28 15:41:55 grubba Exp $ + * $Id: cpp.c,v 1.22 1998/04/01 00:47:46 hubbe Exp $ */ #include "global.h" #include "dynamic_buffer.h" @@ -609,15 +609,22 @@ static INT32 low_cpp(struct cpp *this, for(arg=0;arg<d->args;arg++) { - if(arg && data[pos]==',') pos++; - SKIPWHITE(); - arguments[arg].arg=data + pos; - - if(data[pos]==')') + if(arg && data[pos]==',') { - cpp_error(this,"Too few arguments to macro."); - break; + pos++; + SKIPWHITE(); + }else{ + SKIPWHITE(); + if(data[pos]==')') + { + char buffer[1024]; + sprintf(buffer,"Too few arguments to macro %s, expected %d.",d->link.s->str,d->args); + cpp_error(this,buffer); + break; + } } + arguments[arg].arg=data + pos; + while(1) { diff --git a/src/interpret.c b/src/interpret.c index 81256f565b..648e89b8e2 100644 --- a/src/interpret.c +++ b/src/interpret.c @@ -4,7 +4,7 @@ ||| See the files COPYING and DISCLAIMER for more information. \*/ #include "global.h" -RCSID("$Id: interpret.c,v 1.72 1998/03/31 21:52:17 hubbe Exp $"); +RCSID("$Id: interpret.c,v 1.73 1998/04/01 00:47:46 hubbe Exp $"); #include "interpret.h" #include "object.h" #include "program.h" @@ -1173,6 +1173,26 @@ static int eval_instruction(unsigned char *pc) CJUMP(F_BRANCH_WHEN_GT, is_gt); CJUMP(F_BRANCH_WHEN_GE,!is_lt); + CASE(F_BRANCH_AND_POP_WHEN_ZERO); + if(!IS_ZERO(sp-1)) + { + pc+=sizeof(INT32); + }else{ + DOJUMP(); + pop_stack(); + } + break; + + CASE(F_BRANCH_AND_POP_WHEN_NON_ZERO); + if(!IS_ZERO(sp-1)) + { + pc+=sizeof(INT32); + }else{ + DOJUMP(); + pop_stack(); + } + break; + CASE(F_LAND); if(!IS_ZERO(sp-1)) { diff --git a/src/language.yacc b/src/language.yacc index a224cb26bc..3f1af533ab 100644 --- a/src/language.yacc +++ b/src/language.yacc @@ -17,6 +17,7 @@ %token F_APPLY_ASSIGN_LOCAL F_APPLY_ASSIGN_LOCAL_AND_POP %token F_BRANCH F_BRANCH_WHEN_ZERO F_BRANCH_WHEN_NON_ZERO +%token F_BRANCH_AND_POP_WHEN_ZERO F_BRANCH_AND_POP_WHEN_NON_ZERO %token F_BRANCH_WHEN_LT F_BRANCH_WHEN_GT %token F_BRANCH_WHEN_LE F_BRANCH_WHEN_GE %token F_BRANCH_WHEN_EQ F_BRANCH_WHEN_NE @@ -169,7 +170,7 @@ /* This is the grammar definition of Pike. */ #include "global.h" -RCSID("$Id: language.yacc,v 1.67 1998/03/31 21:52:19 hubbe Exp $"); +RCSID("$Id: language.yacc,v 1.68 1998/04/01 00:47:47 hubbe Exp $"); #ifdef HAVE_MEMORY_H #include <memory.h> #endif diff --git a/src/lex.c b/src/lex.c index 6eaac9eff4..d9ebea8327 100644 --- a/src/lex.c +++ b/src/lex.c @@ -4,7 +4,7 @@ ||| See the files COPYING and DISCLAIMER for more information. \*/ #include "global.h" -RCSID("$Id: lex.c,v 1.46 1998/03/31 21:52:20 hubbe Exp $"); +RCSID("$Id: lex.c,v 1.47 1998/04/01 00:47:48 hubbe Exp $"); #include "language.h" #include "array.h" #include "lex.h" @@ -146,6 +146,8 @@ struct keyword instr_names[]= { "branch if >=", F_BRANCH_WHEN_GE, I_ISJUMP }, { "branch if ==", F_BRANCH_WHEN_EQ, I_ISJUMP }, { "branch if !=", F_BRANCH_WHEN_NE, I_ISJUMP }, +{ "branch & pop if zero", F_BRANCH_AND_POP_WHEN_ZERO, I_ISJUMP }, +{ "branch & pop if !zero", F_BRANCH_AND_POP_WHEN_NON_ZERO, I_ISJUMP }, { "++Loop", F_INC_LOOP, I_ISJUMP }, { "++Loop!=", F_INC_NEQ_LOOP, I_ISJUMP }, { "--Loop", F_DEC_LOOP, I_ISJUMP }, diff --git a/src/peep.c b/src/peep.c index c730f23c38..3da51755f6 100644 --- a/src/peep.c +++ b/src/peep.c @@ -12,7 +12,7 @@ #include "peep.h" #include "dmalloc.h" -RCSID("$Id: peep.c,v 1.20 1998/03/31 21:52:23 hubbe Exp $"); +RCSID("$Id: peep.c,v 1.21 1998/04/01 00:47:48 hubbe Exp $"); struct p_instr_s { @@ -59,7 +59,7 @@ int insert_opcode(unsigned int f, #ifdef DEBUG if(!hasarg(f) && b) - fatal("hasarg() is wrong!\n"); + fatal("hasarg(%d) is wrong!\n",f); #endif p=(p_instr *)low_make_buf_space(sizeof(p_instr), &instrbuf); @@ -82,7 +82,7 @@ int insert_opcode2(int f,int current_line, struct pike_string *current_file) { #ifdef DEBUG if(hasarg(f)) - fatal("hasarg() is wrong!\n"); + fatal("hasarg(%d) is wrong!\n",f); #endif return insert_opcode(f,0,current_line, current_file); } @@ -340,7 +340,7 @@ int insopt(int f, INT32 b, int cl, struct pike_string *cf) #ifdef DEBUG if(!hasarg(f) && b) - fatal("hasarg() is wrong!\n"); + fatal("hasarg(%d) is wrong!\n",f); #endif p=(p_instr *)low_make_buf_space(sizeof(p_instr), &instrbuf); @@ -368,7 +368,7 @@ int insopt2(int f, int cl, struct pike_string *cf) { #ifdef DEBUG if(hasarg(f)) - fatal("hasarg() is wrong!\n"); + fatal("hasarg(%d) is wrong!\n",f); #endif return insopt(f,0,cl, cf); } diff --git a/src/peep.in b/src/peep.in index 80526b2a3f..306f7b8713 100644 --- a/src/peep.in +++ b/src/peep.in @@ -1,6 +1,7 @@ -# -# $Id: peep.in,v 1.13 1998/03/31 21:52:23 hubbe Exp $ -# +// +// $Id: peep.in,v 1.14 1998/04/01 00:47:49 hubbe Exp $ +// + NOP : POP_N_ELEMS (0) : POP_N_ELEMS POP_VALUE : POP_N_ELEMS ($1a + 1) @@ -36,14 +37,14 @@ CLEAR_LOCAL NUMBER(0) ASSIGN_LOCAL_AND_POP ($1a) : CLEAR_LOCAL($1a) CLEAR_LOCAL NUMBER(0) ASSIGN_LOCAL_AND_POP ($1a+1) : CLEAR_2_LOCAL($1a) CLEAR_2_LOCAL CLEAR_2_LOCAL($1a+2) : CLEAR_4_LOCAL($1a) -#CONST_1 MULTIPLY : NEGATE -#CONST0 MULTIPLY : POP_VALUE CONST0 -#CONST1 MULTIPLY : -#NUMBER MULTIPLY [count_bits($1a)==1]: NUMBER(my_log2($1a)) LSH +// CONST_1 MULTIPLY : NEGATE +// CONST0 MULTIPLY : POP_VALUE CONST0 +// CONST1 MULTIPLY : +// NUMBER MULTIPLY [count_bits($1a)==1]: NUMBER(my_log2($1a)) LSH -#CONST_1 DIVIDE : NEGATE -#CONST1 DIVIDE : -#NUMBER DIVIDE [count_bits($1a)==1]: NUMBER(my_log2($1a)) RSH +// CONST_1 DIVIDE : NEGATE +// CONST1 DIVIDE : +// NUMBER DIVIDE [count_bits($1a)==1]: NUMBER(my_log2($1a)) RSH CONST0 SUBTRACT: CONST0 XOR: @@ -184,7 +185,7 @@ APPLY_ASSIGN_LOCAL_AND_POP BYTE MARK_AND_LOCAL ($2a) : APPLY_ASSIGN_LOCAL($1a) B ASSIGN_LOCAL ASSIGN_LOCAL($1a) : ASSIGN_LOCAL($1a) -# MARK_X rules +// MARK_X rules MARK_X(0) : MARK LOCAL MARK_X [$2a>0] : MARK_X($2a-1) LOCAL($1a) STRING MARK_X [$2a>0] : MARK_X($2a-1) STRING($1a) @@ -201,3 +202,18 @@ LFUN MARK_X [$2a>0] : MARK_X($2a-1) LFUN($1a) LTOSVAL MARK_X [$2a>0] : MARK_X($2a-1) LTOSVAL LOCAL_LVALUE MARK_X [$2a>1] : MARK_X($2a-2) LOCAL_LVALUE($1a) 2_LOCALS BYTE MARK_X [$2a>0] : LOCAL($1a) MARK_X($3a-1) LOCAL($2a) + +#define BasicBranch2(Branch,Oper,Pre,Post,Variable) \ + Variable Branch Pre Variable ($1a) : Variable ($1a) Oper ($2a) Post ; \ + ASSIGN_##Variable Branch Pre Variable ($1a) : ASSIGN_##Variable($1a) Oper ($2a) Post ; + +#define BasicBranch1(Branch,Oper,Pre,Post) \ + BasicBranch2(Branch,Oper,Pre,Post,LOCAL) \ + BasicBranch2(Branch,Oper,Pre,Post,GLOBAL) + +#define BasicBranch0(Branch,Oper) \ + BasicBranch1(Branch,Oper,MARK,MARK_X(1)) \ + BasicBranch1(Branch,Oper, , ) + +BasicBranch0(BRANCH_WHEN_NON_ZERO, BRANCH_AND_POP_WHEN_NON_ZERO) +BasicBranch0(BRANCH_WHEN_ZERO, BRANCH_AND_POP_WHEN_ZERO) -- GitLab