|
| 1 | +from src.api import global_ as gl |
| 2 | +from src.api.constants import SCOPE, TYPE |
| 3 | +from src.api.global_ import optemps |
| 4 | +from src.arch.z80 import backend |
| 5 | +from src.arch.z80.backend.runtime import Labels as RuntimeLabel |
| 6 | +from src.arch.z80.visitor.translator_visitor import TranslatorVisitor |
| 7 | +from src.symbols.type_ import Type |
| 8 | + |
| 9 | + |
| 10 | +class BuiltinTranslator(TranslatorVisitor): |
| 11 | + """BUILTIN functions visitor. Eg. LEN(a$) or SIN(x)""" |
| 12 | + |
| 13 | + REQUIRES = backend.REQUIRES |
| 14 | + |
| 15 | + # region STRING Functions |
| 16 | + def visit_INKEY(self, node): |
| 17 | + self.runtime_call(RuntimeLabel.INKEY, Type.string.size) |
| 18 | + |
| 19 | + def visit_IN(self, node): |
| 20 | + self.ic_in(node.children[0].t) |
| 21 | + |
| 22 | + def visit_CODE(self, node): |
| 23 | + self.ic_fparam(gl.PTR_TYPE, node.operand.t) |
| 24 | + if node.operand.token not in ("STRING", "VAR") and node.operand.t != "_": |
| 25 | + self.ic_fparam(TYPE.ubyte, 1) # If the argument is not a variable, it must be freed |
| 26 | + else: |
| 27 | + self.ic_fparam(TYPE.ubyte, 0) |
| 28 | + |
| 29 | + self.runtime_call(RuntimeLabel.ASC, Type.ubyte.size) # Expect a char code |
| 30 | + |
| 31 | + def visit_CHR(self, node): |
| 32 | + self.ic_fparam(gl.STR_INDEX_TYPE, len(node.operand)) # Number of args |
| 33 | + self.runtime_call(RuntimeLabel.CHR, node.size) |
| 34 | + |
| 35 | + def visit_STR(self, node): |
| 36 | + self.ic_fparam(TYPE.float, node.children[0].t) |
| 37 | + self.runtime_call(RuntimeLabel.STR_FAST, node.type_.size) |
| 38 | + |
| 39 | + def visit_LEN(self, node): |
| 40 | + self.ic_lenstr(node.t, node.operand.t) |
| 41 | + |
| 42 | + def visit_VAL(self, node): |
| 43 | + self.ic_fparam(gl.PTR_TYPE, node.operand.t) |
| 44 | + if node.operand.token not in ("STRING", "VAR") and node.operand.t != "_": |
| 45 | + self.ic_fparam(TYPE.ubyte, 1) # If the argument is not a variable, it must be freed |
| 46 | + else: |
| 47 | + self.ic_fparam(TYPE.ubyte, 0) |
| 48 | + |
| 49 | + self.runtime_call(RuntimeLabel.VAL, node.type_.size) |
| 50 | + |
| 51 | + # endregion |
| 52 | + |
| 53 | + def visit_ABS(self, node): |
| 54 | + self.ic_abs(node.children[0].type_, node.t, node.children[0].t) |
| 55 | + |
| 56 | + def visit_RND(self, node): # A special "ZEROARY" function with no parameters |
| 57 | + self.runtime_call(RuntimeLabel.RND, Type.float_.size) |
| 58 | + |
| 59 | + def visit_PEEK(self, node): |
| 60 | + self.ic_load(node.type_, node.t, "*" + str(node.children[0].t)) |
| 61 | + |
| 62 | + # region MATH Functions |
| 63 | + def visit_SIN(self, node): |
| 64 | + self.ic_fparam(node.operand.type_, node.operand.t) |
| 65 | + self.runtime_call(RuntimeLabel.SIN, node.size) |
| 66 | + |
| 67 | + def visit_COS(self, node): |
| 68 | + self.ic_fparam(node.operand.type_, node.operand.t) |
| 69 | + self.runtime_call(RuntimeLabel.COS, node.size) |
| 70 | + |
| 71 | + def visit_TAN(self, node): |
| 72 | + self.ic_fparam(node.operand.type_, node.operand.t) |
| 73 | + self.runtime_call(RuntimeLabel.TAN, node.size) |
| 74 | + |
| 75 | + def visit_ASN(self, node): |
| 76 | + self.ic_fparam(node.operand.type_, node.operand.t) |
| 77 | + self.runtime_call(RuntimeLabel.ASN, node.size) |
| 78 | + |
| 79 | + def visit_ACS(self, node): |
| 80 | + self.ic_fparam(node.operand.type_, node.operand.t) |
| 81 | + self.runtime_call(RuntimeLabel.ACS, node.size) |
| 82 | + |
| 83 | + def visit_ATN(self, node): |
| 84 | + self.ic_fparam(node.operand.type_, node.operand.t) |
| 85 | + self.runtime_call(RuntimeLabel.ATN, node.size) |
| 86 | + |
| 87 | + def visit_EXP(self, node): |
| 88 | + self.ic_fparam(node.operand.type_, node.operand.t) |
| 89 | + self.runtime_call(RuntimeLabel.EXP, node.size) |
| 90 | + |
| 91 | + def visit_LN(self, node): |
| 92 | + self.ic_fparam(node.operand.type_, node.operand.t) |
| 93 | + self.runtime_call(RuntimeLabel.LN, node.size) |
| 94 | + |
| 95 | + def visit_SGN(self, node): |
| 96 | + s = self.TSUFFIX(node.operand.type_) |
| 97 | + self.ic_fparam(node.operand.type_, node.operand.t) |
| 98 | + |
| 99 | + label = { |
| 100 | + "i8": RuntimeLabel.SGNI8, |
| 101 | + "u8": RuntimeLabel.SGNU8, |
| 102 | + "i16": RuntimeLabel.SGNI16, |
| 103 | + "u16": RuntimeLabel.SGNU16, |
| 104 | + "i32": RuntimeLabel.SGNI32, |
| 105 | + "u32": RuntimeLabel.SGNU32, |
| 106 | + "f16": RuntimeLabel.SGNF16, |
| 107 | + "f": RuntimeLabel.SGNF, |
| 108 | + }[s] |
| 109 | + self.runtime_call(label, node.size) |
| 110 | + |
| 111 | + def visit_SQR(self, node): |
| 112 | + self.ic_fparam(node.operand.type_, node.operand.t) |
| 113 | + self.runtime_call(RuntimeLabel.SQR, node.size) |
| 114 | + |
| 115 | + # endregion |
| 116 | + |
| 117 | + def visit_LBOUND(self, node): |
| 118 | + yield node.operands[1] |
| 119 | + self.ic_param(gl.BOUND_TYPE, node.operands[1].t) |
| 120 | + entry = node.operands[0] |
| 121 | + if entry.scope == SCOPE.global_: |
| 122 | + self.ic_fparam(gl.PTR_TYPE, "#{}".format(entry.mangled)) |
| 123 | + elif entry.scope == SCOPE.parameter: |
| 124 | + self.ic_pload(gl.PTR_TYPE, entry.t, entry.offset) |
| 125 | + t1 = optemps.new_t() |
| 126 | + self.ic_fparam(gl.PTR_TYPE, t1) |
| 127 | + elif entry.scope == SCOPE.local: |
| 128 | + self.ic_paddr(-entry.offset, entry.t) |
| 129 | + t1 = optemps.new_t() |
| 130 | + self.ic_fparam(gl.PTR_TYPE, t1) |
| 131 | + self.runtime_call(RuntimeLabel.LBOUND, self.TYPE(gl.BOUND_TYPE).size) |
| 132 | + |
| 133 | + def visit_UBOUND(self, node): |
| 134 | + yield node.operands[1] |
| 135 | + self.ic_param(gl.BOUND_TYPE, node.operands[1].t) |
| 136 | + entry = node.operands[0] |
| 137 | + if entry.scope == SCOPE.global_: |
| 138 | + self.ic_fparam(gl.PTR_TYPE, "#{}".format(entry.mangled)) |
| 139 | + elif entry.scope == SCOPE.parameter: |
| 140 | + self.ic_pload(gl.PTR_TYPE, entry.t, entry.offset) |
| 141 | + t1 = optemps.new_t() |
| 142 | + self.ic_fparam(gl.PTR_TYPE, t1) |
| 143 | + elif entry.scope == SCOPE.local: |
| 144 | + self.ic_paddr(-entry.offset, entry.t) |
| 145 | + t1 = optemps.new_t() |
| 146 | + self.ic_fparam(gl.PTR_TYPE, t1) |
| 147 | + self.runtime_call(RuntimeLabel.UBOUND, self.TYPE(gl.BOUND_TYPE).size) |
| 148 | + |
| 149 | + def visit_USR_STR(self, node): |
| 150 | + # USR ADDR |
| 151 | + self.ic_fparam(TYPE.string, node.children[0].t) |
| 152 | + self.runtime_call(RuntimeLabel.USR_STR, node.type_.size) |
| 153 | + |
| 154 | + def visit_USR(self, node): |
| 155 | + """Machine code call from basic""" |
| 156 | + self.ic_fparam(gl.PTR_TYPE, node.children[0].t) |
| 157 | + self.runtime_call(RuntimeLabel.USR, node.type_.size) |
0 commit comments