Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ilma4 committed Feb 9, 2024
1 parent 2661a93 commit d8f4ae1
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions src/X86.lama
Original file line number Diff line number Diff line change
Expand Up @@ -314,28 +314,26 @@ fun compile (env, code) {
fun compileBinop([env, scode], s) {
-- printf("Compiling binop: %s. env: %s. code: %s", s.string, env.string, code.string);
var code = scode;
case pop2(env) of [b, a, env] -> case allocate(env) of [res, env] ->
case a of
R (n) -> code := code <+ Mov(a, eax)
| S (n) -> code := code <+ Pop(eax)
esac;
case pop2(env) of [b, a, env] -> case allocate(env) of [res, env] -> [env,
code := code <+ Mov(a, eax);
case s of
"+" -> [env, code <+ Binop(s, b, eax) <+ Mov(eax, res)]
| "-" -> [env, code <+ Binop(s, b, eax) <+ Mov(eax, res)]
| "*" -> [env, code <+ Binop(s, b, eax) <+ Mov(eax, res)]

| "/" -> [env, code <+ IDiv(b) <+ Mov(eax, res)]
| "%" -> [env, code <+ IDiv(b) <+ Mov(edx, res)]

| "&&" -> [env, code <+ Binop("!!", b, eax) <+ Binop("cmp", L(0), eax) <+ Set("ne", "%al")<+ Binop("&&", L(1), eax) <+ Mov(eax, res)]
| "<" -> [env, code <+ Binop("cmp", b, eax) <+ Set("l", "%al") <+ Binop("&&", L(1), eax) <+ Mov(eax, res)]
| ">" -> [env, code <+ Binop("cmp", b, eax) <+ Set("g", "%al") <+ Binop("&&", L(1), eax) <+ Mov(eax, res)]
| "<=" -> [env, code <+ Binop("cmp", b, eax) <+ Set("le", "%al") <+ Binop("&&", L(1), eax) <+ Mov(eax, res)]
| ">=" -> [env, code <+ Binop("cmp", b, eax) <+ Set("ge", "%al") <+ Binop("&&", L(1), eax) <+ Mov(eax, res)]
| "==" -> [env, code <+ Binop("cmp", b, eax) <+ Set("e", "%al") <+ Binop("&&", L(1), eax) <+ Mov(eax, res)]
| "!=" -> [env, code <+ Binop("cmp", b, eax) <+ Set("ne", "%al") <+ Binop("&&", L(1), eax) <+ Mov(eax, res)]
"+" -> code <+ Binop(s, b, eax) <+ Mov(eax, res)
| "-" -> code <+ Binop(s, b, eax) <+ Mov(eax, res)
| "*" -> code <+ Binop(s, b, eax) <+ Mov(eax, res)

| "/" -> code <+ IDiv(b) <+ Mov(eax, res)
| "%" -> code <+ IDiv(b) <+ Mov(edx, res)

| "&&" -> code <+ Binop("!!", b, eax) <+ Set("ne", "%al")<+ Binop("&&", L(1), eax) <+ Mov(eax, res)
| "<" -> code <+ Binop("cmp", b, eax) <+ Set("l", "%al") <+ Binop("&&", L(1), eax) <+ Mov(eax, res)
| ">" -> code <+ Binop("cmp", b, eax) <+ Set("g", "%al") <+ Binop("&&", L(1), eax) <+ Mov(eax, res)
| "<=" -> code <+ Binop("cmp", b, eax) <+ Set("le", "%al") <+ Binop("&&", L(1), eax) <+ Mov(eax, res)
| ">=" -> code <+ Binop("cmp", b, eax) <+ Set("ge", "%al") <+ Binop("&&", L(1), eax) <+ Mov(eax, res)
| "==" -> code <+ Binop("cmp", b, eax) <+ Set("e", "%al") <+ Binop("&&", L(1), eax) <+ Mov(eax, res)
| "!=" -> code <+ Binop("cmp", b, eax) <+ Set("ne", "%al") <+ Binop("&&", L(1), eax) <+ Mov(eax, res)
| _ -> failure("codegeneration for BINOP %s is not yet implemented\n", s.string)
esac
]
esac esac
}

Expand Down

0 comments on commit d8f4ae1

Please sign in to comment.