@@ -6,6 +6,7 @@ import List;
66import Buffer;
77import Fun;
88import Manifest;
9+ import Array;
910
1011-- Assembler language interface
1112-- The registers:
@@ -419,6 +420,17 @@ fun compileBinOp (op, lhs, rhs, res) {
419420 esac
420421}
421422
423+ fun regsSaved (call) {
424+ let saved = [rcx, rdx, rsi, rdi, r8, r9, r10, r11] in
425+ foldlArray(
426+ fun (c, r) {
427+ singletonBuffer(Push(r)) <+> c <+ Pop(r)
428+ },
429+ singletonBuffer(call),
430+ saved
431+ )
432+ }
433+
422434-- Compiles stack machine code into a list of x86 instructions. Takes an environment
423435-- and stack machine code, returns an updated environment and x86 code.
424436fun compile (env, code) {
@@ -429,15 +441,15 @@ fun compile (env, code) {
429441 case i of
430442 READ ->
431443 case env.allocate of
432- [s, env] -> [env, code <+
433- Push (rcx) <+ -- Read clobbers rcx
434- Call ("Lread") <+
435- Pop (rcx) <+ -- Restore rcx
444+ [s, env] -> [env, code <+>
445+ regsSaved(Call ("Lread")) <+
436446 Mov (rax, s)]
437447 esac
438448 | WRITE ->
439449 case env.pop of
440- [s, env] -> [env, code <+ Mov (s, rdi) <+ Call ("Lwrite")]
450+ [s, env] -> [env, code <+
451+ Mov (s, rdi) <+
452+ regsSaved(Call ("Lwrite"))]
441453 esac
442454 | ST (x) -> let env = env.addGlobal(x) in [env, code <+> move (env.peek, env.loc(x))]
443455 | LD (x) -> let [l, env] = env.addGlobal(x).allocate in [env, code <+> move (env.loc(x), l)]
0 commit comments