Search in sources :

Example 41 with MethodCode

use of com.github.anba.es6draft.compiler.assembler.Code.MethodCode in project es6draft by anba.

the class CodeGenerator method compile.

private <FUNCTION extends FunctionNode> MethodName compile(FUNCTION node, FunctionCompiler<FUNCTION> compiler) {
    CompletableFuture<String> source = getSource(node);
    String methodName = newUniqueName(node);
    FunctionCode call;
    if (node.isInline()) {
        // call method
        MethodCode callEntry = newMethod(scriptFrame(methodName), compiler.desc.call);
        boolean tailCall = compiler.callInline.compile(this, node, callEntry);
        call = new FunctionCode(callEntry.name(), null, null, tailCall);
    } else {
        // instantiation method
        MethodCode functionInit = newMethod(scriptFrame(methodName, "_init"), compiler.desc.instantiation);
        new FunctionDeclarationInstantiationGenerator(this).generate(node, functionInit);
        // runtime method
        MethodCode functionBody = newMethod(scriptFrame(methodName), compiler.desc.body);
        boolean tailCall = compiler.body.compile(this, node, functionBody);
        // call method
        MethodCode callEntry = newMethod(hiddenFrame(methodName, "_call"), compiler.desc.call);
        call = new FunctionCode(callEntry.name(), functionInit.name(), functionBody.name(), tailCall);
        compiler.call.compile(this, node, callEntry, call);
    }
    FunctionCode construct;
    Function<MethodName, MethodName> debugInfo = null;
    if (compiler.construct != null) {
        // construct method
        MethodTypeDescriptor constructDesc;
        if (call.tailCall) {
            assert node instanceof FunctionDefinition && IsStrict(node);
            constructDesc = FunctionDesc.ConstructorFunctionTailCall.construct;
        } else {
            constructDesc = compiler.desc.construct;
        }
        if (node.isInline()) {
            MethodCode constructEntry = newMethod(scriptFrame(methodName), constructDesc);
            boolean tailCall = compiler.constructInline.compile(this, node, constructEntry);
            construct = new FunctionCode(constructEntry.name(), null, null, tailCall);
        } else {
            MethodCode constructEntry = newMethod(hiddenFrame(methodName, "_construct"), constructDesc);
            construct = new FunctionCode(constructEntry.name(), call.instantiation, call.body, call.tailCall);
            compiler.construct.compile(this, node, constructEntry, construct);
        }
        // debug-info method
        if (isEnabled(Compiler.Option.DebugInfo)) {
            debugInfo = rt -> {
                MethodCode method = newMethod(hiddenFrame(methodName, "_dbg"), MethodDescriptors.DebugInfo);
                debugInfo(method, rt, call.entry, construct.entry, call.instantiation, call.body);
                return method.name();
            };
        }
    } else {
        construct = null;
        // debug-info method
        if (isEnabled(Compiler.Option.DebugInfo)) {
            debugInfo = rt -> {
                MethodCode method = newMethod(hiddenFrame(methodName, "_dbg"), MethodDescriptors.DebugInfo);
                debugInfo(method, rt, call.entry, call.instantiation, call.body);
                return method.name();
            };
        }
    }
    // runtime-info method
    MethodCode runtimeInfo = newMethod(hiddenFrame(methodName, "_rti"), MethodDescriptors.Function_RTI);
    new RuntimeInfoGenerator(this).runtimeInfo(node, runtimeInfo, call, construct, source.join(), debugInfo);
    return runtimeInfo.name();
}
Also used : MethodName(com.github.anba.es6draft.compiler.assembler.MethodName) MethodCode(com.github.anba.es6draft.compiler.assembler.Code.MethodCode) MethodTypeDescriptor(com.github.anba.es6draft.compiler.assembler.MethodTypeDescriptor)

Example 42 with MethodCode

use of com.github.anba.es6draft.compiler.assembler.Code.MethodCode in project es6draft by anba.

the class CodeGenerator method newMethod.

MethodCode newMethod(String methodName, MethodTypeDescriptor methodDescriptor) {
    final int access = Modifier.PUBLIC | Modifier.STATIC;
    MethodCode method = code.newMethod(access, methodName, methodDescriptor);
    // System.out.printf("add <%s, %s>%n", methodName, method.classCode.className);
    return method;
}
Also used : MethodCode(com.github.anba.es6draft.compiler.assembler.Code.MethodCode)

Example 43 with MethodCode

use of com.github.anba.es6draft.compiler.assembler.Code.MethodCode in project es6draft by anba.

the class CodeGenerator method compile.

/**
 * Compiles a module node.
 *
 * @param node
 *            the module node
 * @param moduleRecord
 *            the module record
 */
void compile(Module node, SourceTextModuleRecord moduleRecord) {
    // instantiation methods
    MethodCode moduleInit = newMethod(scriptFrame("~module_init"), MethodDescriptors.Module_Init);
    new ModuleDeclarationInstantiationGenerator(this).generate(node, moduleRecord, moduleInit);
    // runtime method
    MethodCode moduleBody = newMethod(scriptFrame("~module"), MethodDescriptors.Module_Code);
    moduleBody(node, moduleBody);
    // debug-info method
    Function<MethodName, MethodName> debugInfo = null;
    if (isEnabled(Compiler.Option.DebugInfo)) {
        debugInfo = rt -> {
            MethodCode method = newMethod("!module_dbg", MethodDescriptors.DebugInfo);
            debugInfo(method, rt, moduleInit.name(), moduleBody.name());
            return method.name();
        };
    }
    // runtime-info method
    MethodCode runtimeInfo = newMethod("!module_rti", MethodDescriptors.Module_RTI);
    RuntimeInfoGenerator.runtimeInfo(node, runtimeInfo, moduleInit.name(), moduleBody.name(), debugInfo);
    defaultConstructor(Methods.CompiledModule_Constructor, runtimeInfo.name());
}
Also used : MethodName(com.github.anba.es6draft.compiler.assembler.MethodName) MethodCode(com.github.anba.es6draft.compiler.assembler.Code.MethodCode)

Example 44 with MethodCode

use of com.github.anba.es6draft.compiler.assembler.Code.MethodCode in project es6draft by anba.

the class ExpressionGenerator method doExpression.

private OutlinedCall doExpression(DoExpression node, CodeVisitor mv) {
    if (!codegen.isEnabled(Compiler.Option.NoCompletion)) {
        CompletionValueVisitor.performCompletion(node);
    }
    MethodTypeDescriptor methodDescriptor = DoExpressionCodeVisitor.methodDescriptor(mv);
    MethodCode method = codegen.method(mv, "doexpr", methodDescriptor);
    return outlined(new DoExpressionCodeVisitor(node, method, mv), body -> {
        return statement(node.getStatement(), body);
    });
}
Also used : MethodCode(com.github.anba.es6draft.compiler.assembler.Code.MethodCode) MethodTypeDescriptor(com.github.anba.es6draft.compiler.assembler.MethodTypeDescriptor)

Example 45 with MethodCode

use of com.github.anba.es6draft.compiler.assembler.Code.MethodCode in project es6draft by anba.

the class ExpressionGenerator method spreadElement.

private OutlinedCall spreadElement(SpreadElementMethod node, CodeVisitor mv) {
    MethodTypeDescriptor methodDescriptor = SpreadElementCodeVisitor.methodDescriptor(mv);
    MethodCode method = codegen.method(mv, "spread", methodDescriptor);
    return outlined(new SpreadElementCodeVisitor(node, method, mv), body -> {
        Variable<ArrayObject> array = body.getArrayParameter();
        Variable<Integer> index = body.getIndexParameter();
        MutableValue<Integer> nextIndex = body.iarrayElement(body.getNextIndexParameter(), 0);
        spreadArray(node.getElements(), array, index, body);
        body.store(nextIndex, index);
        return Completion.Normal;
    });
}
Also used : ArrayObject(com.github.anba.es6draft.runtime.types.builtins.ArrayObject) BigInteger(java.math.BigInteger) MethodCode(com.github.anba.es6draft.compiler.assembler.Code.MethodCode) MethodTypeDescriptor(com.github.anba.es6draft.compiler.assembler.MethodTypeDescriptor)

Aggregations

MethodCode (com.github.anba.es6draft.compiler.assembler.Code.MethodCode)46 MethodTypeDescriptor (com.github.anba.es6draft.compiler.assembler.MethodTypeDescriptor)12 Completion (com.github.anba.es6draft.compiler.StatementGenerator.Completion)8 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)6 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)6 GeneratorState (com.github.anba.es6draft.compiler.CodeVisitor.GeneratorState)5 MethodName (com.github.anba.es6draft.compiler.assembler.MethodName)4 LexicalEnvironment (com.github.anba.es6draft.runtime.LexicalEnvironment)4 OrdinaryConstructorFunction (com.github.anba.es6draft.runtime.types.builtins.OrdinaryConstructorFunction)4 SwitchClause (com.github.anba.es6draft.ast.SwitchClause)3 Jump (com.github.anba.es6draft.compiler.assembler.Jump)3 LabelState (com.github.anba.es6draft.compiler.CodeVisitor.LabelState)2 ResumptionPoint (com.github.anba.es6draft.runtime.internal.ResumptionPoint)2 ArrayObject (com.github.anba.es6draft.runtime.types.builtins.ArrayObject)2 SimpleImmutableEntry (java.util.AbstractMap.SimpleImmutableEntry)2 ArrayList (java.util.ArrayList)2 MethodDefinition (com.github.anba.es6draft.ast.MethodDefinition)1 ValType (com.github.anba.es6draft.compiler.DefaultCodeGenerator.ValType)1 InstanceMethod (com.github.anba.es6draft.runtime.language.ClassOperations.InstanceMethod)1 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)1