diff --git a/src/samplesrc/test.pla b/src/samplesrc/test.pla index a8a1c26..73a29eb 100755 --- a/src/samplesrc/test.pla +++ b/src/samplesrc/test.pla @@ -204,5 +204,9 @@ puti(array:0); puts(" > "); puti(array:0); puts (" is ") puts(array:0 > array:0 ?? "TRUE\n" :: "FALSE\n") puti(array:0); puts(" < "); puti(array:0); puts (" is ") puts(array:0 < array:0 ?? "TRUE\n" :: "FALSE\n") -ptr = 0 +ptr = 5 +puts("5*0="); puti(ptr*0); putln +puts("5*1="); puti(ptr*1); putln +puts("5*2="); puti(ptr*2); putln +puts("5/1="); puti(ptr/1); putln done diff --git a/src/toolsrc/codegen.c b/src/toolsrc/codegen.c index b5fcb46..7471bcb 100755 --- a/src/toolsrc/codegen.c +++ b/src/toolsrc/codegen.c @@ -1230,6 +1230,17 @@ int crunch_seq(t_opseq **seq, int pass) freeops = 1; break; } + if (opnext->code == BINARY_CODE(SHL_TOKEN)) + { + op->code = DUP_CODE; + opnext->code == BINARY_CODE(ADD_TOKEN); + break; + } + if (opnext->code == BINARY_CODE(MUL_TOKEN) || opnext->code == BINARY_CODE(DIV_TOKEN)) + { + freeops = -2; + break; + } } switch (opnext->code) { @@ -1441,13 +1452,27 @@ int crunch_seq(t_opseq **seq, int pass) } break; case BINARY_CODE(MUL_TOKEN): - for (shiftcnt = 0; shiftcnt < 16; shiftcnt++) + if (op->val == 0) { - if (op->val == (1 << shiftcnt)) + op->code = DROP_CODE; + opnext->code = CONST_CODE; + opnext->val = 0; + } + else if (op->val == 2) + { + op->code = DUP_CODE; + opnext->code = BINARY_CODE(ADD_TOKEN); + } + else + { + for (shiftcnt = 0; shiftcnt < 16; shiftcnt++) { - op->val = shiftcnt; - opnext->code = BINARY_CODE(SHL_TOKEN); - break; + if (op->val == (1 << shiftcnt)) + { + op->val = shiftcnt; + opnext->code = BINARY_CODE(SHL_TOKEN); + break; + } } } break; diff --git a/src/toolsrc/codeopt.pla b/src/toolsrc/codeopt.pla index 385cd7f..66e97e4 100644 --- a/src/toolsrc/codeopt.pla +++ b/src/toolsrc/codeopt.pla @@ -79,6 +79,16 @@ def crunch_seq(seq, pass) freeops = 1 break fin + if nextop->opcode == SHL_CODE + op->opcode = DUP_CODE + nextop->opcode = ADD_CODE + op->opgroup = STACK_GROUP + break + fin + if nextop->opcode == MUL_CODE or nextop->opcode == DIV_CODE + freeops = -2 + break + fin fin when nextop->opcode is NEG_CODE @@ -258,13 +268,24 @@ def crunch_seq(seq, pass) fin break is MUL_CODE - for shiftcnt = 0 to 15 - if op=>opval == 1 << shiftcnt - op=>opval = shiftcnt - nextop->opcode = SHL_CODE - break - fin - next + if op=>opval == 0 + op->opcode = DROP_CODE + nextop->opcode = CONST_CODE + nextop->opval = 0 + nextop->opgroup = CONST_GROUP + elsif op=>opval == 2 + op->opcode = DUP_CODE + nextop->opcode = ADD_CODE + op->opgroup = STACK_GROUP + else + for shiftcnt = 2 to 15 + if op=>opval == 1 << shiftcnt + op=>opval = shiftcnt + nextop->opcode = SHL_CODE + break + fin + next + fin break is DIV_CODE for shiftcnt = 0 to 15