RegisterSet allRegs= new RegisterSet();
for (CodePath curPath : allCodePaths)
{
for (OnePathInstructionRegisterContents curInst : curPath.path)
{
InstructionUseInfo useInfo= beenTo.get(curInst.instruction).useInfo;
allRegs.orEq(useInfo.allWrites());
allRegs.orEq(useInfo.usedReg());
}
}
// Emit decl for each register
for (int regId : allRegs)
{
Element initElem= new Element(InstructionProcessor.cmd_define_register, InstructionProcessor.vm);
initElem.setAttribute("vartype", InstructionProcessor.cmd_define_register_attr_register);
initElem.setAttribute("num", regId + "");
toRet.functionInit.add(initElem);
}
// Find all the var elements, and add moves for them
int argIdx= 1;
for (Element curElem : (List<Element>) codeElement.getChildren())
{
if (curElem.getName().equals("var"))
{
Element moveArg= new Element(InstructionProcessor.cmd_move_argument, InstructionProcessor.vm);
moveArg.setAttribute("vx", curElem.getAttributeValue("register"));
moveArg.setAttribute("vx-type", curElem.getAttributeValue("type"));
String sourceName= curElem.getAttributeValue("name");
if (sourceName == "this")
{
moveArg.setAttribute("sourceArg", "self");
}
else
{
moveArg.setAttribute("sourceArg", argIdx + "");
argIdx++;
}
toRet.functionInit.add(moveArg);
allRegs.remove(curElem.getAttribute("register").getIntValue());
}
}
RegisterSet hasObj= RegisterSet.none();
for (CodePath curPath : allCodePaths)
{
// collect non obj writes
for (OnePathInstructionRegisterContents curInst : curPath.path)
{
InstructionUseInfo useInfo= beenTo.get(curInst.instruction).useInfo;
hasObj.orEq(useInfo.writesObj());
}
}
allRegs.andEq(hasObj);