예제 #1
0
파일: dcpu16.py 프로젝트: srjek/dcpu16
    def __init__(self, value, lineNum, parent):
        self.lineNum = lineNum
        self.parent = parent
        self.size = 0
        self.lastLabels = []
        self.isInt = False
        self.error = None
        try:
            self.extra = eval_0xSCAmodified(value, {}, preEval=True)
            self.labels = extractVaribles(self.extra, "")
        except Exception as err:
            self.printError(repr(err))
            self.labels = []
            self.extra = None
            return

        labels = {"$$globalLabel":""}
        for x in self.labels:
            labels[x] = address(address(1))
        try:
            self.size = len(self._extraWords(labels))
        except NameError as err:
            self.printError("We encountered an error that shouldn't happen, this probably represents a bug in the assembler.")
            self.printError(repr(err))
        return
예제 #2
0
 def optimize(self, labels):
     if self.macroArgs == None: return False
     if "$$macro$"+self.macroName not in labels:
         self.error = "Macro "+repr(self.macroName)+" was not defined"
         self.codeblock = None
         return
     macro = labels["$$macro$"+self.macroName]
     if len(self.macroArgs) != len(macro[0]):
         self.error = "Macro "+repr(self.macroName)+" is defined for "+str(len(macro[0]))+" args, not "+str(len(self.macroArgs))+" args."
         self.codeblock = None
         return
     labels_ = copy.copy(labels)
     args = {}
     for i in range(len(macro[0])):
         argName = macro[0][i]
         arg = self.macroArgs[i]
         arg.optimize(labels)
         args[argName] = address(address(arg.build(labels)))
         labels_[argName] = args[argName]
     codeblock = []
     for code in macro[1]:
         codeblock.append(code.clone())
     self.codeblock = codeblock
     while self.optCodeblock(labels_): pass
     for label in labels_.keys():
         if label not in labels:
             if label not in macro[0] or labels_[label] != args[label]:
                 labels[label] = labels_[label]
     return False
예제 #3
0
 def build(self, labels):
     if self.error != None:
         self.printError(self.error)
     if self.codeblock == None or self.macroArgs == None: return ()
     if ("$$macro$"+self.macroName) not in labels:
         self.printError("Macro "+repr(self.macroName)+" was not defined")
         return
     macro = labels["$$macro$"+self.macroName]
     if len(self.macroArgs) != len(macro[0]):
         self.printError("Macro "+repr(self.macroName)+" is defined for "+str(len(macro[0]))+" args, not "+str(len(self.macroArgs))+" args.")
         return
     labels_ = copy.copy(labels)
     args = {}
     for i in range(len(macro[0])):
         argName = macro[0][i]
         arg = self.macroArgs[i]
         arg.optimize(labels)
         args[argName] = address(address(arg.build(labels)))
         labels_[argName] = args[argName]
     code = []
     for i in self.codeblock:
         code.extend(i.build(labels_))
     for label in labels_.keys():
         if label not in labels:
             if label not in macro[0] or labels_[label] != args[label]:
                 labels[label] = labels_[label]
     return tuple(code)
예제 #4
0
파일: dcpu16.py 프로젝트: srjek/dcpu16
 def __init__(self, part, lineNum, parent, allowShortLiteral):
     self.lineNum = lineNum
     self.parent = parent
     registers = value.registers
     cpu = value.cpu
     self.value = None
     self.extra = None
     self.shortLiteral = False
     self.allowShortLiteral = allowShortLiteral
     if type(part) == type(0x42):
         self.value = part & 0x3F
     elif type(part) == type(""):
         if part.startswith("[") and part.endswith("]"):
             part = part[1:-1]
             if part.lower() in registers:
                 self.value = 0x08 | registers[part.lower()]
             elif part.upper() == "SP++":
                 self.value = cpu["POP"]
             elif part.upper() == "--SP":
                 self.value = cpu["PUSH"]
             elif part.upper() == "SP+[PC++]":
                 self.value = cpu["PICK"]
             elif part.upper() == "SP":
                 self.value = cpu["PEEK"]
             elif part.upper() == "[PC++]":
                 self.value = 0x1E
             elif part.upper() == "PC++":
                 self.value = 0x1F
             else:
                 self.value = -1
                 self.extra = part
         elif part.lower() in registers:
             self.value = registers[part.lower()]
         elif part.upper() in cpu:
             self.value = cpu[part.upper()]
         else:
             self.value = 0x1F
             self.extra = part
     else:
         self.value = 0x1F
         self.extra = part
     labels = {}
     self.lastLabels = []
     self.labels = ()
     if self.extra != None:
         try:
             self.extra = eval_0xSCAmodified(self.extra, {}, preEval=True)
             self.labels = extractVaribles(self.extra, "")
         except Exception as err:
             self.printError(repr(err))
             return
     if len(self.labels) == 0:    #We need to force an optimize for those short literals
         self.labels = ["NotReally"]
         self.optimize({"NotReally":address(address(0)), "$$globalLabel":""})
         self.labels = []
         self.lastLabels = []
예제 #5
0
 def build(self, labels):
     value = self.a.build(labels)
     
     if self.directive == "define":
         labels[self.varible] = address(address(value))
     elif self.directive == "equ":
         labels["$$labels"][self.varible] = address(address(value))
     elif self.directive == "undefine":
         try:
             labels.pop(self.varible)
         except KeyError:
             pass
     return ()
예제 #6
0
 def optimize(self, labels):
     result = self.a.optimize(labels)
     
     if self.directive in ("define", "equ"):
         lastValue = self.value
         self.value = self.a.build(labels)
         if self.directive == "equ":
             labels["$$labels"][self.varible] = address(address(self.value))
         else:
             labels[self.varible] = address(address(self.value))
         return (lastValue != self.value)
     elif self.directive == "undefine":
         try:
             labels.pop(self.varible)
         except KeyError:
             pass
     return False