Skip to content

Instantly share code, notes, and snippets.

@hns
Created November 22, 2011 19:18
Show Gist options
  • Save hns/1386613 to your computer and use it in GitHub Desktop.
Save hns/1386613 to your computer and use it in GitHub Desktop.
case Do_getParamOrVarConst:
// Push name of parameter using another switch
// over paramAndVarCount
paramAndVarCount = n.getParamAndVarCount();
boolean [] constness = n.getParamAndVarConst();
if (paramAndVarCount == 0) {
// The runtime should never call the method in this
// case but to make bytecode verifier happy return null
// as throwing execption takes more code
cfw.add(ByteCode.ICONST_0);
cfw.add(ByteCode.IRETURN);
} else if (paramAndVarCount == 1) {
// As above do not check for valid index but always
// return the name of the first param
cfw.addPush(constness[0]);
cfw.add(ByteCode.IRETURN);
} else {
// Search for first const
int first = 0;
while (first < paramAndVarCount && !constness[first]) {
++first;
}
if (first == paramAndVarCount) {
// No const, no need to do a switch statement
cfw.add(ByteCode.ICONST_0);
cfw.add(ByteCode.IRETURN);
} else {
int last = paramAndVarCount - 1;
while (last > first && !constness[last]) {
--last;
}
// Do switch over getParamOrVarName
cfw.addILoad(1); // param or var index
// do switch from first .. last mapping default
// case to false
int paramSwitchStart = cfw.addTableSwitch(first, last);
// Generate default and non-const cases
cfw.markTableSwitchDefault(paramSwitchStart);
for (int j = first; j <= last; ++j) {
if (cfw.getStackTop() != 0) Kit.codeBug();
if (!constness[j]) {
cfw.markTableSwitchCase(paramSwitchStart,
j - first);
}
}
cfw.add(ByteCode.ICONST_0);
cfw.add(ByteCode.IRETURN);
// Generate const cases
for (int j = first; j <= last; ++j) {
if (cfw.getStackTop() != 0) Kit.codeBug();
if (constness[j]) {
cfw.markTableSwitchCase(paramSwitchStart,
j - first);
}
}
cfw.addPush(ByteCode.ICONST_1);
cfw.add(ByteCode.IRETURN);
}
}
break;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment