|
| 1 | +module callisto.backends.y16; |
| 2 | + |
| 3 | +import std.format; |
| 4 | +import callisto.util; |
| 5 | +import callisto.error; |
| 6 | +import callisto.parser; |
| 7 | +import callisto.compiler; |
| 8 | + |
| 9 | +private struct Word { |
| 10 | + bool inline; |
| 11 | + Node[] inlineNodes; |
| 12 | +} |
| 13 | + |
| 14 | +private struct Type { |
| 15 | + ulong size; |
| 16 | +} |
| 17 | + |
| 18 | +private struct Constant { |
| 19 | + Node value; |
| 20 | +} |
| 21 | + |
| 22 | +class BackendY16 : CompilerBackend { |
| 23 | + Word[string] words; |
| 24 | + Type[string] types; |
| 25 | + bool inScope; |
| 26 | + Constant[string] consts; |
| 27 | + uint blockCounter; |
| 28 | + |
| 29 | + override string[] GetVersions() => ["Y16"]; |
| 30 | + |
| 31 | + this() { |
| 32 | + types["u8"] = Type(1); |
| 33 | + types["i8"] = Type(1); |
| 34 | + types["u16"] = Type(2); |
| 35 | + types["i16"] = Type(2); |
| 36 | + types["addr"] = Type(2); |
| 37 | + types["size"] = Type(2); |
| 38 | + types["usize"] = Type(2); |
| 39 | + types["cell"] = Type(2); |
| 40 | + types["Array"] = Type(6); |
| 41 | + |
| 42 | + foreach (name, ref type ; types) { |
| 43 | + NewConst(format("%s.sizeof", name), cast(long) type.size); |
| 44 | + } |
| 45 | + |
| 46 | + // struct Array |
| 47 | + // usize length |
| 48 | + // usize memberSize |
| 49 | + // addr elements |
| 50 | + // end |
| 51 | + NewConst("Array.length", 0); |
| 52 | + NewConst("Array.memberSize", 2); |
| 53 | + NewConst("Array.elements", 4); |
| 54 | + } |
| 55 | + |
| 56 | + void NewConst(string name, long value, ErrorInfo error = ErrorInfo.init) { |
| 57 | + consts[name] = Constant(new IntegerNode(error, value)); |
| 58 | + } |
| 59 | + |
| 60 | + override void Init() { |
| 61 | + output ~= "cpp sr bs\n"; |
| 62 | + output ~= "ldi a __stack\n"; |
| 63 | + output ~= "addp sr a\n"; |
| 64 | + } |
| 65 | + |
| 66 | + override void End() { |
| 67 | + output ~= "hlt\n"; |
| 68 | + output ~= "__stack: fill 1024 0\n"; // 512 cell stack |
| 69 | + } |
| 70 | + |
| 71 | + override void CompileWord(WordNode node) { |
| 72 | + if (node.name in words) { |
| 73 | + auto word = words[node.name]; |
| 74 | + |
| 75 | + if (word.inline) { |
| 76 | + foreach (inode ; word.inlineNodes) { |
| 77 | + compiler.CompileNode(inode); |
| 78 | + } |
| 79 | + } |
| 80 | + else { |
| 81 | + output ~= format("callb __func__%s\n", node.name.Sanitise()); |
| 82 | + } |
| 83 | + } |
| 84 | + else if (node.name in consts) { |
| 85 | + compiler.CompileNode(consts[node.name].value); |
| 86 | + } |
| 87 | + else { |
| 88 | + Error(node.error, "Undefined identifier '%s'", node.name); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + override void CompileInteger(IntegerNode node) { |
| 93 | + output ~= format("ldi a %d\n", node.value); |
| 94 | + output ~= "wrw sr a\n"; |
| 95 | + output ~= "ldsi a 2\n"; |
| 96 | + output ~= "addp sr a\n"; |
| 97 | + } |
| 98 | + |
| 99 | + override void CompileFuncDef(FuncDefNode node) { |
| 100 | + if (node.inline) { |
| 101 | + words[node.name] = Word(true, node.nodes); |
| 102 | + } |
| 103 | + else { |
| 104 | + assert(!inScope); |
| 105 | + inScope = true; |
| 106 | + |
| 107 | + words[node.name] = Word(false, []); |
| 108 | + |
| 109 | + output ~= format("jmpb __func_end__%s\n", node.name.Sanitise()); |
| 110 | + output ~= format("__func__%s:\n", node.name.Sanitise()); |
| 111 | + |
| 112 | + foreach (ref inode ; node.nodes) { |
| 113 | + compiler.CompileNode(inode); |
| 114 | + } |
| 115 | + |
| 116 | + output ~= format("__func_return__%s:\n", node.name.Sanitise()); |
| 117 | + |
| 118 | + output ~= "ret\n"; |
| 119 | + output ~= format("__func_end__%s:\n", node.name.Sanitise()); |
| 120 | + inScope = false; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + override void CompileIf(IfNode node) { |
| 125 | + ++ blockCounter; |
| 126 | + auto blockNum = blockCounter; |
| 127 | + uint condCounter; |
| 128 | + |
| 129 | + foreach (i, ref condition ; node.condition) { |
| 130 | + foreach (ref inode ; condition) { |
| 131 | + compiler.CompileNode(inode); |
| 132 | + } |
| 133 | + |
| 134 | + output ~= "ldsi b 2\n"; |
| 135 | + output ~= "subp sr b\n"; |
| 136 | + output ~= "rdw a sr\n"; |
| 137 | + output ~= "ldsi b 0\n"; |
| 138 | + output ~= "cmp a b\n"; |
| 139 | + output ~= format("jzb __if_%d_%d\n", blockNum, condCounter + 1); |
| 140 | + |
| 141 | + foreach (ref inode ; node.doIf[i]) { |
| 142 | + compiler.CompileNode(inode); |
| 143 | + } |
| 144 | + |
| 145 | + output ~= format("jmpb __if_%d_end\n", blockNum); |
| 146 | + |
| 147 | + ++ condCounter; |
| 148 | + output ~= format("__if_%d_%d:\n", blockNum, condCounter); |
| 149 | + } |
| 150 | + |
| 151 | + if (node.hasElse) { |
| 152 | + foreach (ref inode ; node.doElse) { |
| 153 | + compiler.CompileNode(inode); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + output ~= format("__if_%d_end:\n", blockNum); |
| 158 | + } |
| 159 | + |
| 160 | + override void CompileWhile(WhileNode node) { |
| 161 | + ++ blockCounter; |
| 162 | + uint blockNum = blockCounter; |
| 163 | + |
| 164 | + output ~= format("jmpb __while_%d_condition\n", blockNum); |
| 165 | + output ~= format("__while_%d:\n", blockNum); |
| 166 | + |
| 167 | + foreach (ref inode ; node.doWhile) { |
| 168 | + compiler.CompileNode(inode); |
| 169 | + } |
| 170 | + |
| 171 | + output ~= format("__while_%d_condition:\n", blockNum); |
| 172 | + |
| 173 | + foreach (ref inode ; node.condition) { |
| 174 | + compiler.CompileNode(inode); |
| 175 | + } |
| 176 | + |
| 177 | + output ~= "ldsi b 2\n"; |
| 178 | + output ~= "subp sr b\n"; |
| 179 | + output ~= "rdw a sr\n"; |
| 180 | + output ~= "ldsi b 0\n"; |
| 181 | + output ~= "cmp a b\n"; |
| 182 | + output ~= format("jnzb __while_%d\n", blockNum); |
| 183 | + output ~= format("__while_%d_end:\n", blockNum); |
| 184 | + } |
| 185 | + |
| 186 | + override void CompileLet(LetNode node) { |
| 187 | + assert(0); |
| 188 | + } |
| 189 | + |
| 190 | + override void CompileArray(ArrayNode node) { |
| 191 | + assert(0); |
| 192 | + } |
| 193 | + |
| 194 | + override void CompileString(StringNode node) { |
| 195 | + assert(0); |
| 196 | + } |
| 197 | + |
| 198 | + override void CompileStruct(StructNode node) { |
| 199 | + assert(0); |
| 200 | + } |
| 201 | +} |
0 commit comments