//Code generator for bytecode #include #include #include "Format.h" #include "Generator.h" #include "Opcodes.h" namespace pim { namespace compiler { Generator::Generator() : output(std::cout), labelCounter(0), programCounter(0) { } void Generator::defineLabel(std::string label) { Label newLabel; newLabel.Name = label; newLabel.Position = programCounter;//program.size(); labelPositions.push_back(newLabel); } void Generator::writeOpcode(int opcode) { programCounter++; program.push_back(opcode); } void Generator::writeConstant(std::string constant) { writeConstant(format::StringToNumber(constant)); } void Generator::writeConstant(int constant) { program.push_back(constant & 0xFF); program.push_back((constant>>8) & 0xFF); program.push_back((constant>>16) & 0xFF); program.push_back((constant>>24) & 0xFF); } void Generator::writeConstantPlaceholder(std::string label) { placeholders.push_back(Placeholder(program.size(), label)); program.push_back(0); program.push_back(0); program.push_back(0); program.push_back(0); } void Generator::writeConstantPlaceholder(int * value) { valuePlaceholders.push_back(ValuePlaceholder(program.size(), value)); program.push_back(0); program.push_back(0); program.push_back(0); program.push_back(0); } void Generator::writeConstantPropertyPlaceholder(std::string property) { propertyPlaceholders.push_back(PropertyPlaceholder(program.size(), property)); program.push_back(0); program.push_back(0); program.push_back(0); program.push_back(0); } void Generator::writeConstantMacroPlaceholder(std::string macro) { macroPlaceholders.push_back(MacroPlaceholder(program.size(), macro)); program.push_back(0); program.push_back(0); program.push_back(0); program.push_back(0); } std::vector Generator::Finish() { //All compile time labels, macros, etc for(std::vector::iterator iter = placeholders.begin(), end = placeholders.end(); iter != end; ++iter) { bool found = false; Placeholder cPosition = *iter; for(std::vector