#ifndef CODE_H #define CODE_H #include #include #include #include #include "node.h" class Code { public: Code() = default; void addLine(const std::shared_ptr& line); const std::vector>& getLines() const; const string toString() const; // Two-pass assembler methods void assemble(); std::vector emitCode(); std::string emitText(); std::string dumpSymbols() const; std::string dumpCode() const; private: std::vector> _lines; // Assembler state std::unordered_map _symbolTable; std::vector _locationCounters; // Location counter per line int _startAddress = 0; int _programLength = 0; std::string _programName; int _baseRegister = -1; // -1 means not set // Pass 1: build symbol table and assign addresses void firstPass(); // Pass 2: generate code void secondPass(); // Helper methods int getInstructionLength(const std::shared_ptr& node, int locationCounter) const; std::vector generateInstruction(const InstructionNode* inst, int address); std::vector generateData(const DataNode* data); // Addressing mode selection struct AddressingResult { int nixbpe; // ni, x, b, p, e bits int displacement; // 12-bit or 20-bit bool success; }; AddressingResult selectAddressingMode(int targetAddress, int pc, bool indexed, bool immediate, bool indirect, bool extended) const; }; #endif // CODE_H