pike.git/
src/
operators.c
Branch:
Tag:
Non-build tags
All tags
No tags
1996-04-13
1996-04-13 03:16:26 by Fredrik Hübinette (Hubbe) <hubbe@hubbe.net>
8a630c813b4ffa5e6c46106d7efbea9206300cbf (
124
lines) (+
70
/-
54
)
[
Show
|
Annotate
]
Branch:
branches/E-12
peephole stuff fixed, along with some other optimization stuff
Rev: src/operators.c:1.8
18:
#include "error.h" #include "docode.h" #include "add_efun.h"
+
#include "peep.h"
+
#include "lex.h"
#define COMPARISON(ID,NAME,EXPR) \ void ID(INT32 args) \
71:
struct lpc_string *r; char *buf;
-
if
(args
==1
) return;
+
switch
(args)
+
{
+
case 1:
return;
+
default:
size=0;
-
+
for(e=-args;e<0;e++) size+=sp[e].u.string->len;
-
if(args==2)
-
{
-
r=add_shared_strings(sp[-2].u.string,sp[-1].u.string);
-
}else{
+
r=begin_shared_string(size); buf=r->str; for(e=-args;e<0;e++) {
-
+
#if 1
+
int q;
+
for(q=0;q<sp[e].u.string->len;q++)
+
buf[q]=sp[e].u.string->str[q];
+
#else
MEMCPY(buf,sp[e].u.string->str,sp[e].u.string->len);
-
+
#endif
buf+=sp[e].u.string->len; } r=end_shared_string(r); }
-
for(e=-args;e<0;e++)
-
{
-
free_string(sp[e].u.string);
-
}
+
+
for(e=-args;e<0;e++) free_string(sp[e].u.string);
sp-=args; push_string(r); break;
229:
case 2: do_docode(CDR(n),DO_NOT_COPY);
-
ins_f_byte
(F_ADD);
+
emit2
(F_ADD);
return 1; default:
297:
do_docode(CDR(n),DO_NOT_COPY); if(CAR(n)->u.sval.u.efun->function == f_eq)
-
ins_f_byte
(F_EQ);
+
emit2
(F_EQ);
else if(CAR(n)->u.sval.u.efun->function == f_ne)
-
ins_f_byte
(F_NE);
+
emit2
(F_NE);
else if(CAR(n)->u.sval.u.efun->function == f_lt)
-
ins_f_byte
(F_LT);
+
emit2
(F_LT);
else if(CAR(n)->u.sval.u.efun->function == f_le)
-
ins_f_byte
(F_LE);
+
emit2
(F_LE);
else if(CAR(n)->u.sval.u.efun->function == f_gt)
-
ins_f_byte
(F_GT);
+
emit2
(F_GT);
else if(CAR(n)->u.sval.u.efun->function == f_ge)
-
ins_f_byte
(F_GE);
+
emit2
(F_GE);
else fatal("Couldn't generate comparison!\n"); return 1;
414:
{ case 1: do_docode(CDR(n),DO_NOT_COPY);
-
ins_f_byte
(F_NEGATE);
+
emit2
(F_NEGATE);
return 1; case 2: do_docode(CDR(n),DO_NOT_COPY);
-
ins_f_byte
(F_SUBTRACT);
+
emit2
(F_SUBTRACT);
return 1; } return 0;
489:
case 2: do_docode(CDR(n),0);
-
ins_f_byte
(F_AND);
+
emit2
(F_AND);
return 1; default:
562:
case 2: do_docode(CDR(n),0);
-
ins_f_byte
(F_OR);
+
emit2
(F_OR);
return 1; default:
635:
case 2: do_docode(CDR(n),0);
-
ins_f_byte
(F_XOR);
+
emit2
(F_XOR);
return 1; default:
648:
if(sp[-2].type != T_INT) error("Bad argument 1 to <<\n"); if(sp[-1].type != T_INT) error("Bad argument 2 to <<\n"); sp--;
-
sp[-1].u.integer
<<
= sp[
0
].u.integer;
+
sp[-1].u.integer = sp[
-1
].u.integer
<< sp->u.integer
;
} void f_lsh(INT32 args)
663:
if(count_args(CDR(n))==2) { do_docode(CDR(n),DO_NOT_COPY);
-
ins_f_byte
(F_LSH);
+
emit2
(F_LSH);
return 1; } return 0;
674:
if(sp[-2].type != T_INT) error("Bad argument 1 to >>\n"); if(sp[-1].type != T_INT) error("Bad argument 2 to >>\n"); sp--;
-
sp[-1].u.integer
>>
= sp[
0
].u.integer;
+
sp[-1].u.integer = sp[
-1
].u.integer
>> sp->u.integer
;
} void f_rsh(INT32 args)
689:
if(count_args(CDR(n))==2) { do_docode(CDR(n),DO_NOT_COPY);
-
ins_f_byte
(F_RSH);
+
emit2
(F_RSH);
return 1; } return 0;
761:
case 2: do_docode(CDR(n),0);
-
ins_f_byte
(F_MULTIPLY);
+
emit2
(F_MULTIPLY);
return 1; default:
819:
if(count_args(CDR(n))==2) { do_docode(CDR(n),DO_NOT_COPY);
-
ins_f_byte
(F_DIVIDE);
+
emit2
(F_DIVIDE);
return 1; } return 0;
866:
if(count_args(CDR(n))==2) { do_docode(CDR(n),DO_NOT_COPY);
-
ins_f_byte
(F_MOD);
+
emit2
(F_MOD);
return 1; } return 0;
896:
if(count_args(CDR(n))==1) { do_docode(CDR(n),DO_NOT_COPY);
-
ins_f_byte
(F_NOT);
+
emit2
(F_NOT);
return 1; } return 0;
904:
void o_compl() {
-
if
(sp[-1].type
!= T_INT
)
error("Bad
argument
to
~\n");
+
switch
(sp[-1].type
)
+
{
+
case
T_INT
:
sp[-1].u.integer = ~ sp[-1].u.integer;
-
+
break;
+
+
case T_FLOAT:
+
sp[-1].u.float_number = -1.0 - sp[-1].u.float_number;
+
break;
+
+
default:
+
error("Bad argument to ~\n");
}
-
+
}
void f_compl(INT32 args) {
919:
if(count_args(CDR(n))==1) { do_docode(CDR(n),DO_NOT_COPY);
-
ins_f_byte
(F_COMPL);
+
emit2
(F_COMPL);
return 1; } return 0;
999:
void init_operators() {
-
add_efun2("`==",f_eq,"function(mixed,mixed:int)",
0
,0,generate_comparison);
-
add_efun2("`!=",f_ne,"function(mixed,mixed:int)",
0
,0,generate_comparison);
-
add_efun2("`<", f_lt,"function(int|float,int|float:int)|function(string,string:int)",
0
,0,generate_comparison);
-
add_efun2("`<=",f_le,"function(int|float,int|float:int)|function(string,string:int)",
0
,0,generate_comparison);
-
add_efun2("`>", f_gt,"function(int|float,int|float:int)|function(string,string:int)",
0
,0,generate_comparison);
-
add_efun2("`>=",f_ge,"function(int|float,int|float:int)|function(string,string:int)",
0
,0,generate_comparison);
+
add_efun2("`==",f_eq,"function(mixed,mixed:int)",
OPT_TRY_OPTIMIZE
,0,generate_comparison);
+
add_efun2("`!=",f_ne,"function(mixed,mixed:int)",
OPT_TRY_OPTIMIZE
,0,generate_comparison);
+
add_efun2("`<", f_lt,"function(int|float,int|float:int)|function(string,string:int)",
OPT_TRY_OPTIMIZE
,0,generate_comparison);
+
add_efun2("`<=",f_le,"function(int|float,int|float:int)|function(string,string:int)",
OPT_TRY_OPTIMIZE
,0,generate_comparison);
+
add_efun2("`>", f_gt,"function(int|float,int|float:int)|function(string,string:int)",
OPT_TRY_OPTIMIZE
,0,generate_comparison);
+
add_efun2("`>=",f_ge,"function(int|float,int|float:int)|function(string,string:int)",
OPT_TRY_OPTIMIZE
,0,generate_comparison);
-
add_efun2("`+",f_add,"function(int...:int)|!function(int...:mixed)&function(int|float...:float)|!function(int|float...:mixed)&function(string|int|float...:string)|function(array...:array)|function(mapping...:mapping)|function(list...:list)",
0
,optimize_binary,generate_sum);
+
add_efun2("`+",f_add,"function(int...:int)|!function(int...:mixed)&function(int|float...:float)|!function(int|float...:mixed)&function(string|int|float...:string)|function(array...:array)|function(mapping...:mapping)|function(list...:list)",
OPT_TRY_OPTIMIZE
,optimize_binary,generate_sum);
-
add_efun2("`-",f_minus,"function(int:int)|function(float:float)|function(array,array:array)|function(mapping,mapping:mapping)|function(list,list:list)|function(float|int,float:float)|function(float,int:float)|function(int,int:int)|function(string,string:string)",
0
,0,generate_minus);
+
add_efun2("`-",f_minus,"function(int:int)|function(float:float)|function(array,array:array)|function(mapping,mapping:mapping)|function(list,list:list)|function(float|int,float:float)|function(float,int:float)|function(int,int:int)|function(string,string:string)",
OPT_TRY_OPTIMIZE
,0,generate_minus);
-
add_efun2("`&",f_and,"function(int...:int)|function(mapping...:mapping)|function(list...:list)|function(array...:array)",
0
,optimize_binary,generate_and);
+
add_efun2("`&",f_and,"function(int...:int)|function(mapping...:mapping)|function(list...:list)|function(array...:array)",
OPT_TRY_OPTIMIZE
,optimize_binary,generate_and);
-
add_efun2("`|",f_or,"function(int...:int)|function(mapping...:mapping)|function(list...:list)|function(array...:array)",
0
,optimize_binary,generate_or);
+
add_efun2("`|",f_or,"function(int...:int)|function(mapping...:mapping)|function(list...:list)|function(array...:array)",
OPT_TRY_OPTIMIZE
,optimize_binary,generate_or);
-
add_efun2("`^",f_xor,"function(int...:int)|function(mapping...:mapping)|function(list...:list)|function(array...:array)",
0
,optimize_binary,generate_xor);
+
add_efun2("`^",f_xor,"function(int...:int)|function(mapping...:mapping)|function(list...:list)|function(array...:array)",
OPT_TRY_OPTIMIZE
,optimize_binary,generate_xor);
-
add_efun2("`<<",f_lsh,"function(int,int:int)",
0
,0,generate_lsh);
-
add_efun2("`>>",f_rsh,"function(int,int:int)",
0
,0,generate_rsh);
+
add_efun2("`<<",f_lsh,"function(int,int:int)",
OPT_TRY_OPTIMIZE
,0,generate_lsh);
+
add_efun2("`>>",f_rsh,"function(int,int:int)",
OPT_TRY_OPTIMIZE
,0,generate_rsh);
-
add_efun2("`*",f_multiply,"function(int...:int)|!function(int...:mixed)&function(float|int...:float)|function(string*,string:string)",
0
,optimize_binary,generate_multiply);
+
add_efun2("`*",f_multiply,"function(int...:int)|!function(int...:mixed)&function(float|int...:float)|function(string*,string:string)",
OPT_TRY_OPTIMIZE
,optimize_binary,generate_multiply);
-
add_efun2("`/",f_divide,"function(int,int:int)|function(float|int,float:float)|function(float,int:float)|function(string,string:string*)",
0
,0,generate_divide);
+
add_efun2("`/",f_divide,"function(int,int:int)|function(float|int,float:float)|function(float,int:float)|function(string,string:string*)",
OPT_TRY_OPTIMIZE
,0,generate_divide);
-
add_efun2("`%",f_mod,"function(int,int:int)|!function(int,int:mixed)&function(int|float,int|float:float)",
0
,0,generate_mod);
+
add_efun2("`%",f_mod,"function(int,int:int)|!function(int,int:mixed)&function(int|float,int|float:float)",
OPT_TRY_OPTIMIZE
,0,generate_mod);
-
add_efun2("`!",f_not,"function(mixed:int)",
0
,0,generate_not);
-
add_efun2("`~",f_compl,"function(int:int)",
0
,0,generate_compl);
+
add_efun2("`!",f_not,"function(mixed:int)",
OPT_TRY_OPTIMIZE
,0,generate_not);
+
add_efun2("`~",f_compl,"function(int:int)
|function(float:float)
",
OPT_TRY_OPTIMIZE
,0,generate_compl);
}