From 97e6e619fff121b1427ed4d66ec63ee9bbc6eb36 Mon Sep 17 00:00:00 2001 From: Chris Pressey Date: Wed, 12 Dec 2018 09:01:46 +0000 Subject: [PATCH 1/4] `cmp` can compare against a literal word. --- HISTORY.md | 3 ++- src/sixtypical/compiler.py | 10 ++++++++++ tests/SixtyPical Analysis.md | 22 ++++++++++++++++++++++ tests/SixtyPical Compilation.md | 16 +++++++++++----- 4 files changed, 45 insertions(+), 6 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 24db63e..4ebbea9 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -10,7 +10,8 @@ History of SixtyPical * When the range of a location is known, `inc` and `dec` on it will usually shift the known instead of invalidating it. * `cmp` instruction can now perform a 16-bit unsigned comparison - of `word` memory locations (at the cost of trashing `a`.) + of `word` memory locations and `word` literals (at the cost of + trashing the `a` register.) * Fixed pathological memory use in the lexical scanner - should be much less inefficient now when parsing large source files. * Reorganized the examples in `eg/rudiments/` to make them diff --git a/src/sixtypical/compiler.py b/src/sixtypical/compiler.py index a2b983d..2675b54 100644 --- a/src/sixtypical/compiler.py +++ b/src/sixtypical/compiler.py @@ -402,6 +402,16 @@ class Compiler(object): self.emitter.emit(CMP(Absolute(Offset(src_label, 1)))) self.emitter.resolve_label(end_label) return + if isinstance(src, ConstantRef) and src.type == TYPE_WORD: + dest_label = self.get_label(dest.name) + self.emitter.emit(LDA(Absolute(dest_label))) + self.emitter.emit(CMP(Immediate(Byte(src.high_byte())))) + end_label = Label('end_label') + self.emitter.emit(BNE(Relative(end_label))) + self.emitter.emit(LDA(Absolute(Offset(dest_label, 1)))) + self.emitter.emit(CMP(Immediate(Byte(src.low_byte())))) + self.emitter.resolve_label(end_label) + return cls = { 'a': CMP, 'x': CPX, diff --git a/tests/SixtyPical Analysis.md b/tests/SixtyPical Analysis.md index 0331e7e..23baa18 100644 --- a/tests/SixtyPical Analysis.md +++ b/tests/SixtyPical Analysis.md @@ -1207,6 +1207,28 @@ Some rudimentary tests for `cmp`. | } ? UnmeaningfulReadError: zb +`cmp` can compare against a literal word. + + | word za + | + | define main routine + | inputs za + | trashes a, z, c, n + | { + | cmp za, 4000 + | } + = ok + + | word za + | + | define main routine + | inputs za + | trashes z, c, n + | { + | cmp za, 4000 + | } + ? ForbiddenWriteError: a + ### and ### Some rudimentary tests for `and`. diff --git a/tests/SixtyPical Compilation.md b/tests/SixtyPical Compilation.md index d61ef30..87e427a 100644 --- a/tests/SixtyPical Compilation.md +++ b/tests/SixtyPical Compilation.md @@ -395,15 +395,21 @@ Compiling 16-bit `cmp`. | trashes a, z, c, n | { | cmp za, zb + | cmp za, 4000 | } = $080D LDA $EA61 - = $0810 CMP $081C + = $0810 CMP $0828 = $0813 BNE $081B = $0815 LDA $EA62 - = $0818 CMP $081D - = $081B RTS - = $081C .byte $BB - = $081D .byte $0B + = $0818 CMP $0829 + = $081B LDA $EA61 + = $081E CMP #$0F + = $0820 BNE $0827 + = $0822 LDA $EA62 + = $0825 CMP #$A0 + = $0827 RTS + = $0828 .byte $BB + = $0829 .byte $0B Compiling `if`. From d1a29709f2ea3a99d6d11221f0f57593d958f161 Mon Sep 17 00:00:00 2001 From: Chris Pressey Date: Wed, 12 Dec 2018 09:09:45 +0000 Subject: [PATCH 2/4] Add example test program for cmp-against-literal-word. Fix it. --- eg/rudiments/cmp-litword.60p | 64 +++++++++++++++++++++++++++++++++ src/sixtypical/compiler.py | 4 +-- tests/SixtyPical Compilation.md | 4 +-- 3 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 eg/rudiments/cmp-litword.60p diff --git a/eg/rudiments/cmp-litword.60p b/eg/rudiments/cmp-litword.60p new file mode 100644 index 0000000..5cf9793 --- /dev/null +++ b/eg/rudiments/cmp-litword.60p @@ -0,0 +1,64 @@ +// Include `support/${PLATFORM}.60p` before this source +// Should print ENGGL + +word w1 + +define main routine + outputs w1 + trashes a, x, y, z, n, c, v +{ + copy 4000, w1 + + cmp w1, 4000 + if z { + ld a, 69 // E + call chrout + } else { + ld a, 78 // N + call chrout + } + + copy 4000, w1 + + cmp w1, 4001 + if z { + ld a, 69 // E + call chrout + } else { + ld a, 78 // N + call chrout + } + + copy 20002, w1 + + cmp w1, 20001 // 20002 >= 20001 + if c { + ld a, 71 // G + call chrout + } else { + ld a, 76 // L + call chrout + } + + copy 20001, w1 + + cmp w1, 20001 // 20001 >= 20001 + if c { + ld a, 71 // G + call chrout + } else { + ld a, 76 // L + call chrout + } + + copy 20000, w1 + + cmp w1, 20001 // 20000 < 20001 + if c { + ld a, 71 // G + call chrout + } else { + ld a, 76 // L + call chrout + } +} diff --git a/src/sixtypical/compiler.py b/src/sixtypical/compiler.py index 2675b54..3659014 100644 --- a/src/sixtypical/compiler.py +++ b/src/sixtypical/compiler.py @@ -405,11 +405,11 @@ class Compiler(object): if isinstance(src, ConstantRef) and src.type == TYPE_WORD: dest_label = self.get_label(dest.name) self.emitter.emit(LDA(Absolute(dest_label))) - self.emitter.emit(CMP(Immediate(Byte(src.high_byte())))) + self.emitter.emit(CMP(Immediate(Byte(src.low_byte())))) end_label = Label('end_label') self.emitter.emit(BNE(Relative(end_label))) self.emitter.emit(LDA(Absolute(Offset(dest_label, 1)))) - self.emitter.emit(CMP(Immediate(Byte(src.low_byte())))) + self.emitter.emit(CMP(Immediate(Byte(src.high_byte())))) self.emitter.resolve_label(end_label) return cls = { diff --git a/tests/SixtyPical Compilation.md b/tests/SixtyPical Compilation.md index 87e427a..e00f954 100644 --- a/tests/SixtyPical Compilation.md +++ b/tests/SixtyPical Compilation.md @@ -403,10 +403,10 @@ Compiling 16-bit `cmp`. = $0815 LDA $EA62 = $0818 CMP $0829 = $081B LDA $EA61 - = $081E CMP #$0F + = $081E CMP #$A0 = $0820 BNE $0827 = $0822 LDA $EA62 - = $0825 CMP #$A0 + = $0825 CMP #$0F = $0827 RTS = $0828 .byte $BB = $0829 .byte $0B From 03a682ff08c4787bd6ec4b5a8fe574cb21083507 Mon Sep 17 00:00:00 2001 From: Chris Pressey Date: Wed, 12 Dec 2018 09:13:13 +0000 Subject: [PATCH 3/4] Make word-table print YY. --- eg/rudiments/word-table.60p | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/eg/rudiments/word-table.60p b/eg/rudiments/word-table.60p index ffad04a..54babbc 100644 --- a/eg/rudiments/word-table.60p +++ b/eg/rudiments/word-table.60p @@ -1,16 +1,41 @@ +// Include `support/${PLATFORM}.60p` before this source +// Should print YY + word one word table[256] many define main routine inputs one, many outputs one, many - trashes a, x, y, n, z + trashes a, x, y, c, n, z { ld x, 0 - ld y, 0 + ld y, 1 copy 777, one copy one, many + x + copy 888, one copy one, many + y + + ld x, 1 + ld y, 0 + copy many + x, one + cmp one, 888 + if z { + ld a, 89 + call chrout + } else { + ld a, 78 + call chrout + } + copy many + y, one + cmp one, 777 + if z { + ld a, 89 + call chrout + } else { + ld a, 78 + call chrout + } } From 0ec8970c766cb6761da1ab2e7c919e780d3133a1 Mon Sep 17 00:00:00 2001 From: Chris Pressey Date: Wed, 12 Dec 2018 09:27:25 +0000 Subject: [PATCH 4/4] Expand example with code that will likely become library support. --- eg/rudiments/example.60p | 47 ++++++++++++++++++++++++++++++++-------- eg/rudiments/if.60p | 12 ---------- 2 files changed, 38 insertions(+), 21 deletions(-) delete mode 100644 eg/rudiments/if.60p diff --git a/eg/rudiments/example.60p b/eg/rudiments/example.60p index c4475da..9eefa8e 100644 --- a/eg/rudiments/example.60p +++ b/eg/rudiments/example.60p @@ -1,14 +1,43 @@ byte lives +byte table[16] hexchars : "0123456789ABCDEF" + +define prbyte routine + inputs a, hexchars + trashes a, z, n, c, v +{ + save x { + save a { + st off, c + shr a + shr a + shr a + shr a + and a, 15 + ld x, a + ld a, hexchars + x + call chrout + } + save a { + and a, 15 + ld x, a + ld a, hexchars + x + call chrout + } + } +} + define main routine - inputs lives + inputs lives, hexchars outputs lives trashes a, x, z, n, c, v - { - ld a, 0 - st a, lives - ld x, lives - st off, c - add x, 1 - st x, lives - } +{ + ld a, 0 + st a, lives + ld x, lives + st off, c + inc x + st x, lives + ld a, lives + call prbyte +} diff --git a/eg/rudiments/if.60p b/eg/rudiments/if.60p deleted file mode 100644 index 9b0b968..0000000 --- a/eg/rudiments/if.60p +++ /dev/null @@ -1,12 +0,0 @@ -define main routine - inputs a - outputs a - trashes z, n, c -{ - cmp a, 42 - if z { - ld a, 7 - } else { - ld a, 23 - } -}