def registerNode(node): global currentbody currentbody.append(IR.Node(node.ppt, node.nodetype, *node.data))
def aux(): "Accumulates all IR nodes defined by this line." if line.lookahead(0).type == "EOL": pass elif line.lookahead(1).type == ":": newlabel = line.expect("LABEL").value line.expect(":") result.append(IR.Node(ppt, "Label", newlabel, IR.PCExpr())) aux() elif line.lookahead(0).type == "*": global templabelcount templabelcount = templabelcount + 1 result.append( IR.Node(ppt, "Label", "*" + str(templabelcount), IR.PCExpr())) line.expect("*") aux() elif line.lookahead(0).type == "." or line.lookahead(0).type == "`": which = line.expect(".", "`").type if (which == "."): pragma = line.expect("LABEL").value else: pragma = "invoke" pragmaFunction = "pragma" + pragma.title() for mod in pragma_modules: if hasattr(mod, pragmaFunction): getattr(mod, pragmaFunction)(ppt, line, result) break else: Err.log("Unknown pragma " + pragma) else: # Instruction opcode = line.expect("OPCODE").value arg2 = None if line.lookahead(0).type == "#": mode = "Immediate" line.expect("#") arg = parse_expr(line) line.expect("EOL") elif line.lookahead(0).type == "(": line.expect("(") arg = parse_expr(line) if line.lookahead(0).type == ",": line.expect(",") if line.lookahead(0).type == "X": mode = "PointerX" line.expect("X") line.expect(")") line.expect("EOL") else: mode = "PointerSPY" line.expect("SP") line.expect(")") line.expect(",") line.expect("Y") line.expect("EOL") else: line.expect(")") tok = line.expect(",", "EOL").type if tok == "EOL": mode = "Pointer" else: if line.lookahead(0).type == "Y": mode = "PointerY" line.expect("Y") line.expect("EOL") else: mode = "PointerZ" line.expect("Z") line.expect("EOL") elif line.lookahead(0).type == "EOL": mode = "Implied" arg = None else: arg = parse_expr(line) tok = line.expect("EOL", ",").type if tok == ",": # Parser has to special-case the BBXn instructions, # Which uniquely take two addresses if opcode[:3] in ["bbs", "bbr"]: arg2 = parse_expr(line) mode = "Memory2" else: tok = line.expect("X", "Y", "Z").type if tok == "X": mode = "MemoryX" elif tok == "Y": mode = "MemoryY" else: mode = "MemoryZ" line.expect("EOL") else: mode = "Memory" result.append(IR.Node(ppt, mode, opcode, arg, arg2))
def pragmaMacro(ppt, line, result): "Begin a macro definition" lbl = line.expect("LABEL").value line.expect("EOL") result.append(IR.Node(ppt, "MacroBegin", lbl))
def pragmaMacend(ppt, line, result): "End a macro definition" line.expect("EOL") result.append(IR.Node(ppt, "MacroEnd"))
def pragmaScope(ppt, line, result): "Create a new lexical scoping block" line.expect("EOL") result.append(IR.Node(ppt, "ScopeBegin"))
def pragmaScend(ppt, line, result): "End the innermost lexical scoping block" line.expect("EOL") result.append(IR.Node(ppt, "ScopeEnd"))
def pragmaDword(ppt, line, result): "Raw data, a double-word at a time, little-endian" dwords = readData(line) result.append(IR.Node(ppt, "Dword", *dwords))
def pragmaDwordbe(ppt, line, result): "Raw data, a dword at a time, big-endian" dwords = readData(line) result.append(IR.Node(ppt, "DwordBE", *dwords))
def pragmaByte(ppt, line, result): "Raw data, a byte at a time" bytes = readData(line) result.append(IR.Node(ppt, "Byte", *bytes))
def pragmaWord(ppt, line, result): "Raw data, a word at a time, little-endian" words = readData(line) result.append(IR.Node(ppt, "Word", *words))
def pragmaAlias(ppt, line, result): "Assigns an arbitrary label" lbl = line.expect("LABEL").value target = FE.parse_expr(line) result.append(IR.Node(ppt, "Label", lbl, target))
def pragmaCheckpc(ppt, line, result): "Enforces that the PC has not exceeded a certain point" target = FE.parse_expr(line) line.expect("EOL") result.append(IR.Node(ppt, "CheckPC", target))
def pragmaOrg(ppt, line, result): "Relocates the PC with no output" newPC = FE.parse_expr(line) line.expect("EOL") result.append(IR.Node(ppt, "SetPC", newPC))
def parse_line(ppt, lexemelist): "Turn a line of source into an IR Node." Err.currentpoint = ppt result = [] line = ParseLine(lexemelist) def aux(): "Accumulates all IR nodes defined by this line." if line.lookahead(0).type == "EOL": pass elif line.lookahead(1).type == ":": newlabel = line.expect("LABEL").value line.expect(":") result.append(IR.Node(ppt, "Label", newlabel, IR.PCExpr())) aux() elif line.lookahead(0).type == "*": global templabelcount templabelcount = templabelcount + 1 result.append( IR.Node(ppt, "Label", "*" + str(templabelcount), IR.PCExpr())) line.expect("*") aux() elif line.lookahead(0).type == "." or line.lookahead(0).type == "`": which = line.expect(".", "`").type if (which == "."): pragma = line.expect("LABEL").value else: pragma = "invoke" pragmaFunction = "pragma" + pragma.title() for mod in pragma_modules: if hasattr(mod, pragmaFunction): getattr(mod, pragmaFunction)(ppt, line, result) break else: Err.log("Unknown pragma " + pragma) else: # Instruction opcode = line.expect("OPCODE").value if line.lookahead(0).type == "#": mode = "Immediate" line.expect("#") arg = parse_expr(line) line.expect("EOL") elif line.lookahead(0).type == "(": line.expect("(") arg = parse_expr(line) if line.lookahead(0).type == ",": mode = "PointerX" line.expect(",") line.expect("X") line.expect(")") line.expect("EOL") else: line.expect(")") tok = line.expect(",", "EOL").type if tok == "EOL": mode = "Pointer" else: mode = "PointerY" line.expect("Y") line.expect("EOL") elif line.lookahead(0).type == "EOL": mode = "Implied" arg = None else: arg = parse_expr(line) tok = line.expect("EOL", ",").type if tok == ",": tok = line.expect("X", "Y").type if tok == "X": mode = "MemoryX" else: mode = "MemoryY" line.expect("EOL") else: mode = "Memory" result.append(IR.Node(ppt, mode, opcode, arg)) aux() result = [node for node in result if node is not IR.NullNode] if len(result) == 0: return IR.NullNode if len(result) == 1: return result[0] return IR.SequenceNode(ppt, result)
def aux(): "Accumulates all IR nodes defined by this line." if line.lookahead(0).type == "EOL": pass elif line.lookahead(1).type == ":": newlabel=line.expect("LABEL").value line.expect(":") result.append(IR.Node(ppt, "Label", newlabel, IR.PCExpr())) aux() elif line.lookahead(0).type == "*": global templabelcount templabelcount = templabelcount + 1 result.append(IR.Node(ppt, "Label", "*"+str(templabelcount), IR.PCExpr())) line.expect("*") aux() elif line.lookahead(0).type == "." or line.lookahead(0).type == "`": which = line.expect(".", "`").type if (which == "."): pragma = line.expect("LABEL").value else: pragma = "invoke" pragmaFunction = "pragma"+pragma.title() for mod in pragma_modules: if hasattr(mod, pragmaFunction): getattr(mod, pragmaFunction)(ppt, line, result) break else: Err.log("Unknown pragma "+pragma) else: # Instruction opcode = line.expect("OPCODE").value if line.lookahead(0).type == "#": mode = "Immediate" line.expect("#") arg = parse_expr(line) line.expect("EOL") elif line.lookahead(0).type == "(": line.expect("(") arg = parse_expr(line) if line.lookahead(0).type == ",": mode = "IndirectX" line.expect(",") line.expect("X") line.expect(")") line.expect("EOL") else: line.expect(")") tok = line.expect(",", "EOL").type if tok == "EOL": mode = "Indirect" else: mode = "IndirectY" line.expect("Y") line.expect("EOL") elif line.lookahead(0).type == "EOL": mode = "Implied" arg = None else: arg = parse_expr(line) tok = line.expect("EOL", ",").type if tok == ",": tok = line.expect("X", "Y").type if tok == "X": mode = "MemoryX" else: mode = "MemoryY" line.expect("EOL") else: mode = "Memory" result.append(IR.Node(ppt, mode, opcode, arg))