Search in sources :

Example 1 with UndefinedMemory

use of php.runtime.memory.helper.UndefinedMemory in project jphp by jphp-compiler.

the class ExpressionStmtCompiler method writePushMemory.

public void writePushMemory(Memory memory) {
    Memory.Type type = Memory.Type.REFERENCE;
    if (memory instanceof UndefinedMemory) {
        code.add(new FieldInsnNode(GETSTATIC, Type.getInternalName(Memory.class), "UNDEFINED", Type.getDescriptor(Memory.class)));
    } else if (memory instanceof NullMemory) {
        code.add(new FieldInsnNode(GETSTATIC, Type.getInternalName(Memory.class), "NULL", Type.getDescriptor(Memory.class)));
    } else if (memory instanceof FalseMemory) {
        writePushConstBoolean(false);
        return;
    } else if (memory instanceof TrueMemory) {
        writePushConstBoolean(true);
        return;
    } else if (memory instanceof KeyValueMemory) {
        writePushMemory(((KeyValueMemory) memory).key);
        writePopBoxing();
        writePushMemory(((KeyValueMemory) memory).getValue());
        writePopBoxing();
        Object arrayKey = ((KeyValueMemory) memory).getArrayKey();
        if (arrayKey instanceof String) {
            writePushConstString((String) arrayKey);
            writeSysStaticCall(KeyValueMemory.class, "valueOf", Memory.class, Memory.class, Memory.class, String.class);
        } else if (arrayKey instanceof Long) {
            writePushConstLong((long) arrayKey);
            writeSysStaticCall(KeyValueMemory.class, "valueOf", Memory.class, Memory.class, Memory.class, Long.TYPE);
        } else {
            writeSysStaticCall(KeyValueMemory.class, "valueOf", Memory.class, Memory.class, Memory.class);
        }
        setStackPeekAsImmutable();
        return;
    } else if (memory instanceof ReferenceMemory) {
        code.add(new TypeInsnNode(NEW, Type.getInternalName(ReferenceMemory.class)));
        code.add(new InsnNode(DUP));
        code.add(new MethodInsnNode(INVOKESPECIAL, Type.getInternalName(ReferenceMemory.class), Constants.INIT_METHOD, "()V", false));
    } else if (memory instanceof ArrayMemory) {
        ArrayMemory array = (ArrayMemory) memory;
        if (!array.isList()) {
            if (array.size() == 0) {
                writeSysStaticCall(ArrayMemory.class, "emptyMap", ArrayMemory.class);
            } else {
                writePushConstInt(array.size());
                writeSysStaticCall(ArrayMemory.class, "createHashed", ArrayMemory.class, int.class);
            }
        } else {
            if (array.size() == 0) {
                writeSysStaticCall(ArrayMemory.class, "emptyList", ArrayMemory.class);
            } else {
                writePushConstInt(array.size());
                writeSysStaticCall(ArrayMemory.class, "createListed", ArrayMemory.class, int.class);
            }
        }
        ForeachIterator foreachIterator = ((ArrayMemory) memory).foreachIterator(false, false);
        if (array.isList() && array.size() > 1 && array.size() <= 5) {
            // dup array
            writePushDup();
            while (foreachIterator.next()) {
                // value
                writePushMemory(foreachIterator.getValue());
                writePopBoxing();
            }
            Class<?>[] argTypes = new Class[array.size()];
            for (int i = 0; i < array.size(); i++) {
                argTypes[i] = Memory.class;
            }
            writeSysDynamicCall(ArrayMemory.class, "add", void.class, argTypes);
        } else {
            while (foreachIterator.next()) {
                writePushDup();
                if (array.isList()) {
                    writePushMemory(foreachIterator.getValue());
                    writePopBoxing();
                    writeSysDynamicCall(ArrayMemory.class, "add", ReferenceMemory.class, Memory.class);
                } else {
                    Memory key = foreachIterator.getMemoryKey();
                    if (key instanceof LongMemory) {
                        writePushConstLong(key.toLong());
                        writePushMemory(foreachIterator.getValue());
                        writePopBoxing();
                        writeSysDynamicCall(ArrayMemory.class, "put", ReferenceMemory.class, Long.TYPE, Memory.class);
                    } else {
                        writePushMemory(key);
                        if (!key.isString()) {
                            writePopBoxing();
                        }
                        writePushMemory(foreachIterator.getValue());
                        writePopBoxing();
                        writeSysDynamicCall(ArrayMemory.class, "put", ReferenceMemory.class, Object.class, Memory.class);
                    }
                }
                writePopAll(1);
            }
        }
        /*stackPop();
            stackPush(memory);*/
        setStackPeekAsImmutable();
        return;
    } else {
        switch(memory.type) {
            case INT:
                {
                    code.add(new LdcInsnNode(memory.toLong()));
                    type = Memory.Type.INT;
                }
                break;
            case DOUBLE:
                {
                    code.add(new LdcInsnNode(memory.toDouble()));
                    type = Memory.Type.DOUBLE;
                }
                break;
            case STRING:
                {
                    code.add(new LdcInsnNode(memory.toString()));
                    type = Memory.Type.STRING;
                }
                break;
        }
    }
    stackPush(type);
    setStackPeekAsImmutable();
}
Also used : UndefinedMemory(php.runtime.memory.helper.UndefinedMemory) Memory(php.runtime.Memory) UndefinedMemory(php.runtime.memory.helper.UndefinedMemory) ForeachIterator(php.runtime.lang.ForeachIterator) IObject(php.runtime.lang.IObject) CompileClass(php.runtime.ext.support.compile.CompileClass)

Aggregations

Memory (php.runtime.Memory)1 CompileClass (php.runtime.ext.support.compile.CompileClass)1 ForeachIterator (php.runtime.lang.ForeachIterator)1 IObject (php.runtime.lang.IObject)1 UndefinedMemory (php.runtime.memory.helper.UndefinedMemory)1