diff options
-rw-r--r-- | lisc/emit.c | 17 | ||||
-rw-r--r-- | lisc/isel.c | 1 | ||||
-rw-r--r-- | lisc/lisc.h | 1 | ||||
-rw-r--r-- | lisc/parse.c | 1 | ||||
-rw-r--r-- | lisc/test/collatz.ssa | 15 |
5 files changed, 26 insertions, 9 deletions
diff --git a/lisc/emit.c b/lisc/emit.c index 22e9073..2f96c3c 100644 --- a/lisc/emit.c +++ b/lisc/emit.c @@ -137,6 +137,7 @@ eins(Ins i, Fn *fn, FILE *f) static char *otoa[NOp] = { [OAdd] = "add", [OSub] = "sub", + [OMul] = "imul", [OAnd] = "and", [OLoad] = "mov", [OLoadss] = "movsw", @@ -154,10 +155,26 @@ eins(Ins i, Fn *fn, FILE *f) [OStores - OStorel] = "w", [OStoreb - OStorel] = "b", }; + Ref r0, r1; int reg; int64_t val; switch (i.op) { + case OMul: + if (rtype(i.arg[1]) == RCon) { + r0 = i.arg[1]; + r1 = i.arg[0]; + } else { + r0 = i.arg[0]; + r1 = i.arg[1]; + } + if (rtype(r0) == RCon && rtype(r1) == RTmp) { + val = fn->con[r0.val].val; + fprintf(f, "\timul $%"PRId64", %%%s, %%%s\n", + val, rtoa(r1.val), rtoa(i.to.val)); + break; + } + /* fall through */ case OAdd: case OSub: case OAnd: diff --git a/lisc/isel.c b/lisc/isel.c index c36773c..ac0159f 100644 --- a/lisc/isel.c +++ b/lisc/isel.c @@ -179,6 +179,7 @@ sel(Ins i, Fn *fn) goto Emit; case OAdd: case OSub: + case OMul: case OAnd: case OCopy: if (fn->tmp[i.to.val].type == TLong) diff --git a/lisc/lisc.h b/lisc/lisc.h index 6e9ce6c..8159224 100644 --- a/lisc/lisc.h +++ b/lisc/lisc.h @@ -125,6 +125,7 @@ enum Op { OSub, ODiv, ORem, + OMul, OAnd, OCmp, OCmp1 = OCmp + NCmp-1, diff --git a/lisc/parse.c b/lisc/parse.c index 07e1ad9..ae9f27b 100644 --- a/lisc/parse.c +++ b/lisc/parse.c @@ -16,6 +16,7 @@ OpDesc opdesc[NOp] = { [OSub] = { "sub", 2, 2 }, [ODiv] = { "div", 2, 2 }, [ORem] = { "rem", 2, 2 }, + [OMul] = { "mul", 2, 2 }, [OAnd] = { "and", 2, 2 }, [OStorel] = { "storel", 2, 0 }, [OStorew] = { "storew", 2, 0 }, diff --git a/lisc/test/collatz.ssa b/lisc/test/collatz.ssa index 9696194..8918c76 100644 --- a/lisc/test/collatz.ssa +++ b/lisc/test/collatz.ssa @@ -32,8 +32,7 @@ jnz %p, @odd, @even @odd - %nn =w add %n0, %n0 # compute %n2 = 3 * %n0 + 1 - %n1 =w add %n0, %nn + %n1 =w mul 3, %n0 # compute %n2 = 3 * %n0 + 1 %n2 =w add %n1, 1 jmp @cloop @@ -42,17 +41,15 @@ jmp @cloop @getmemo - %idx00 =l add %n0, %n0 - %idx0 =l add %idx00, %idx00 - %loc0 =l add %idx0, %mem - %cn0 =w load %loc0 - %c2 =w add %c0, %cn0 + %idx0 =l mul %n0, 4 + %loc0 =l add %idx0, %mem + %cn0 =w load %loc0 + %c2 =w add %c0, %cn0 @endcl %c =w phi @getmemo %c2, @cloop %c0 - %idx10 =l add %n, %n - %idx1 =l add %idx10, %idx10 + %idx1 =l mul %n, 4 %loc1 =l add %idx1, %mem storew %c, %loc1 # memorize the result %n9 =w add 1, %n |