#!/usr/bin/env python # TODO macros, argument checks. import sys import symbols from core import Label, Operation from x86 import getOperationArgumentCount, opcodes, Register, Assembler import parsers class Parser(parsers.Parser): @classmethod def getOperationArgumentCount(self, op): if(op == symbols.intern(".DB")): return(1) elif(op == symbols.intern(".GLOBAL")): return(1) elif(op == symbols.intern(".EXTERNAL")): return(1) else: return(getOperationArgumentCount(opcodes[op])) if __name__ == "__main__": if sys.platform == "win32": from coff import COFFWriter as Writer else: from elf64 import ELFWriter as Writer p = Parser(sys.stdin) a = Assembler() for item in p.parse(): a.add(item) writer = Writer(Writer.defaultObjectFileNameFormat % "a") for label in p.labels.values(): if not label.bResolved and not a.globalP(label): sys.stderr.write("error: %r is unresolved\n" % label) sys.exit(1) #label.setGlobal() writer.recordSymbol(label.ID.name, label.addr, label.bResolved, symbols.intern("local") if not a.globalP(label) else symbols.intern("global"), label.segment.ID.name if label.segment else None) for label in p.labels.values(): for addr, size, method, callback in label.fixups: if a.globalP(label): writer.recordFixup(label.ID.name, 0, addr, size, method) else: offset = label.addr # for 386, the offset is just in the actual operand. writer.recordFixup(label.segment.ID.name, 0, addr, size, method) writer.write(a.segments) writer.close() a.dump()