public ByteCodeGenerator(CompileManager manager, Method method) { Manager = manager; Method = method; byteCodeStream = new byte[64]; length = 0; variableList = new Variable[256]; if (method.Modifiers.HasFlag(Modifier.Static)) { variableCount = 0; MaxVariables = 0; } else { variableCount = 1; MaxVariables = 1; variableList[0] = new Variable(0, "this", method.DeclaringType); } variableListStack = new Stack<Tuple<short, Variable[]>>(); state = new State(); }
public Method Walk(Class c) { method = new Method(); // modifiers method.Modifiers = new ModifierListTranslator(node.GetChild(0)).Walk(); method.DeclaringType = c; int i = 1; ITree child = node.GetChild(i++); // generic if (child.Type == (int) JavaNodeType.GENERIC_TYPE_PARAM_LIST) { child = node.GetChild(i++); } // type if (child.Type == (int) JavaNodeType.TYPE) { method.ReturnType = new TypeTranslator(child).Walk(); child = node.GetChild(i++); } else { method.ReturnType = PrimativeTypes.Void; } // name method.Name = new IdentifierTranslator(child).Walk(); child = node.GetChild(i++); // parameters method.Parameters.AddRange(new MethodParameterTranslator(child).Walk()); child = node.GetChild(i++); // arrayDeclaratorList if (child.Type == (int) JavaNodeType.ARRAY_DECLARATOR_LIST) { //TODO; method.ArrayLevels = TypeTranslator.ProcessArray(child); child = node.GetChild(i++); } // throws if (child.Type == (int) JavaNodeType.THROWS_CLAUSE) { child = node.GetChild(i); } // body? if (child.Type == (int) JavaNodeType.BLOCK_SCOPE) { method.Body = new BlockTranslator(child).Walk(); } return method; }
public short AddConstantMethodref(Method method) { if (method == null) return 0; short classIndex = AddConstantClass(method.DeclaringType); short nameAndTypeIndex = AddConstantNameAndType(method.Name, method); CompileConstantMethodref methodConst = ConstantPool.OfType<CompileConstantMethodref>().FirstOrDefault( x => x.ClassIndex == classIndex && x.NameAndTypeIndex == nameAndTypeIndex); if (methodConst == null) { methodConst = new CompileConstantMethodref { PoolIndex = nextConstantIndex++, ClassIndex = classIndex, NameAndTypeIndex = nameAndTypeIndex }; ConstantPool.Add(methodConst); } return methodConst.PoolIndex; }
public MethodCompiler(Method method) { this.method = method; }
public void EmitInvokevirtual(short meth, Method method) { var argsize = (short)TypeCodeHelper.Width(method.Parameters.Select(x => x.Type)); EmitOp(OpCodeValue.invokevirtual); if (!alive) return; EmitShort(meth); state.Pop(argsize + 1); state.Push(method.ReturnType); }
public void EmitInvokespecial(short meth, Method method) { var argsize = (short)TypeCodeHelper.Width(method.Parameters.Select(x => x.Type)); EmitOp(OpCodeValue.invokespecial); if (!alive) return; EmitShort(meth); state.Pop(argsize); //TODO: if (method.Name == "<init>") // state.markInitialized((UninitializedType)state.Peek()); state.Pop(1); state.Push(method.ReturnType); }
public void EmitInvokedynamic(short desc, Method method) { // N.B. this format is under consideration by the JSR 292 EG var argsize = (short)TypeCodeHelper.Width(method.Parameters.Select(x => x.Type)); EmitOp(OpCodeValue.invokedynamic); if (!alive) return; EmitShort(desc); EmitShort(0); state.Pop(argsize); state.Push(method.ReturnType); }