Skip to content

Commit

Permalink
fix read and write calls: save and restore ecx and edx
Browse files Browse the repository at this point in the history
  • Loading branch information
danyaberezun committed Mar 6, 2024
1 parent 33f7ba7 commit a0c2c52
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/X86.lama
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,23 @@ fun makeEnv (stack, stackSlots, globals) {
stackSlots
}

[envString, allocate, push, pop, pop2, addGlobal, loc, getGlobals, getStackSize]
(* generates code that
1. saves two caller-saved registers (ecx, edx),
2. adds @push_args_code,
3. calls function @func_name,
4. adds @addl_code
5. restores registers
*)
fun genCallF (func_name, push_args_code, addl_code) {
emptyBuffer()
<+ Push (ecx) <+ Push (edx)
<+> push_args_code
<+ Call (func_name)
<+> addl_code
<+ Pop (ecx) <+ Pop (edx)
}

[envString, allocate, push, pop, pop2, addGlobal, loc, getGlobals, getStackSize, genCallF]
}

-- Exported accessors
Expand Down Expand Up @@ -204,6 +220,10 @@ fun getStackSize (env) {
env [8] ()
}

fun genCallF (env, func_name, push_args_code, addl_code) {
env [9] (func_name, push_args_code, addl_code)
}

-- Creates an initial environment
fun initEnv () {
makeEnv (0, emptySet (compare), emptySet (compare))
Expand Down Expand Up @@ -288,11 +308,11 @@ fun compile (env, code) {
case i of
READ ->
case env.allocate of
[s, env] -> [env, code <+ Call ("Lread") <+ Mov (eax, s)]
[s, env] -> [env, code <+> genCallF(env, "Lread", emptyBuffer(), emptyBuffer()) <+ Mov (eax, s)]
esac
| WRITE ->
case env.pop of
[s, env] -> [env, code <+ Push (s) <+ Call ("Lwrite") <+ Pop (eax)]
[s, env] -> [env, code <+> genCallF(env, "Lwrite", singletonBuffer(Push(s)), singletonBuffer(Pop (eax)))]
esac
| _ -> failure ("codegeneration for instruction %s is not yet implemented\n", i.string)
esac
Expand Down

0 comments on commit a0c2c52

Please sign in to comment.