Package org.openquark.cal.compiler

Examples of org.openquark.cal.compiler.QualifiedName


            int arity = currentMachineFunction.getArity();
            Map<QualifiedName, Integer> env = new HashMap<QualifiedName, Integer> ();
            String parameterNames[] = currentMachineFunction.getParameterNames();
            for (int i = 0; i < parameterNames.length; ++i) {
                String parameterName = parameterNames[i];                                    
                QualifiedName qn = QualifiedName.make(currentModule.getName(), parameterName);              
                env.put (qn, Integer.valueOf((arity - i)));             
            }

            //body.code (new Instruction.I_Println("Entering: " + cl.getQualifiedName().toString()));
View Full Code Here


                    String[] vars = getVars(alt, altTag);
                    Map<QualifiedName, Integer> newEnv = argOffset (0, p);

                    for (int j = 0; j < vars.length; ++j) {
                        QualifiedName qn = QualifiedName.make(moduleName, vars [j]);
                        newEnv.put (qn, Integer.valueOf(d + 1 + j));
                    }

                    // i_split: takes a dc object, tells it to push all (vars.length) fields onto the stack
                    InstructionList altGP = new InstructionList ();
                    altGP.code (new Instruction.I_Split (vars.length));

                    altGP.code (schemeR(alt.getAltExpr(), newEnv, d + vars.length));

                    Code code = new Code(altGP);
                    altTagToCodeMap.put(altTag, code);
                }
            }

            ErrorInfo errorInfo = sw.getErrorInfo() == null ? null : toRuntimeErrorInfo(sw.getErrorInfo());
            gp.code (new Instruction.I_Switch (altTagToCodeMap, errorInfo));

            return gp;
        }

        // Is e a data constructor field selection?
        Expression.DataConsSelection dataConsSelection = e.asDataConsSelection();
        if (dataConsSelection != null) {
            if (CODEGEN_DIAG) {
                MACHINE_LOGGER.log(Level.FINE, "    selectDC:");
            }

            gp.code (schemeC(dataConsSelection.getDCValueExpr(), p, d));
            gp.code (new Instruction.I_LazySelectDCField(dataConsSelection.getDataConstructor(),
                    dataConsSelection.getFieldIndex(),
                    toRuntimeErrorInfo (dataConsSelection.getErrorInfo())));

//          // Evaluate the code for the dc-valued expr.
//gp.code (schemeE (dataConsSelection.getDCValueExpr(), p, d));

//          // Extract the field value onto the stack.
//          int fieldIndex = dataConsSelection.getFieldIndex();
//          ErrorInfo errorInfo = dataConsSelection.getErrorInfo() == null ? null : new ErrorInfo(dataConsSelection.getErrorInfo());
//          gp.code (new Instruction.I_SelectDCField (dataConsSelection.getDataConstructor(), fieldIndex, errorInfo));

//          // Add a var to the env, and generate code for that var.
//          Expression.Var varName = dataConsSelection.getVarName();
//          QualifiedName varQualifiedName = varName.getName();

//          Map newEnv = argOffset (0, p);
//          newEnv.put (varQualifiedName, JavaPrimitives.makeInteger (d + 1));

//          gp.code (schemeR(varName, newEnv, d + 1));

            appendUpdateCode (gp, d);

            return gp;
        }

        // Is e a let expression?
        Expression.Let let = e.asLet();
        if (let != null) {
            // Currently the compiler doesn't differentialte between let and letrec scenarios.
            // As such we have to treat all lets as letrecs.

            Expression.Let.LetDefn[] defs = let.getDefns();

            EnvAndDepth ead = schemeXr (defs, p, d);           

            InstructionList gprecs = schemeCLetrec (defs, ead.env, ead.depth);

            gp.code (gprecs);

            gp.code (schemeR (let.getBody (), ead.env, ead.depth));

            return gp; 
        }

        // Is e a tail recursive call?
        if (e.asTailRecursiveCall() != null) {
            // The g-machine doesn't have a specific optimization for tail recursive calls
            // so we simply handle it as the original fully saturated application and let
            // the general tail call optimization handle it.
            return schemeR (e.asTailRecursiveCall().getApplForm(), p, d);
        }

        // Is e an application?
        Expression.Appl appl = e.asAppl();
        if (appl != null) {
            if (CODEGEN_DIAG) {
                MACHINE_LOGGER.log(Level.FINE, "    Application:");
            }

            InstructionList il = schemeRS (e, p, d, 0);
            if (il != null) {
                gp.code (il);
                return gp;
            }

            gp.code (schemeC (e, p, d));
            appendUpdateCode(gp, d);

            return gp;
        }

        // Is e a record update
        // e is a record update
        Expression.RecordUpdate recordUpdate = e.asRecordUpdate();
        if (recordUpdate != null) {
            if (CODEGEN_DIAG) {
                MACHINE_LOGGER.log(Level.FINE, "    Record update:");
            }
            gp.code (schemeE (e, p, d));
            appendUpdateCode (gp, d);
            return gp;
        }         

        // Is e a record extension
        // e is a record extension
        Expression.RecordExtension recordExtension = e.asRecordExtension();
        if (recordExtension != null) {
            if (CODEGEN_DIAG) {
                MACHINE_LOGGER.log(Level.FINE, "    Record extension:");
            }
            gp.code (schemeE (e, p, d));
            appendUpdateCode (gp, d);
            return gp;
       

        // e is a record selection
        Expression.RecordSelection recordSelection = e.asRecordSelection();
        if (recordSelection != null) {
            if (CODEGEN_DIAG) {
                MACHINE_LOGGER.log(Level.FINE, "    Record selection:");
            }
            gp.code (schemeE (e, p, d));
            appendUpdateCode (gp, d);
            return gp;
       

        // e is a record case
        Expression.RecordCase recordCase = e.asRecordCase();
        if (recordCase != null) {
            // Strictly compile the condition expression
            Expression conditionExpr = recordCase.getConditionExpr();
            gp.code (schemeE (conditionExpr, p, d));

            Map<QualifiedName, Integer> newEnv = argOffset (0, p);
            QualifiedName recordName = QualifiedName.make(currentMachineFunction.getQualifiedName().getModuleName(), "$recordCase");
            newEnv.put (recordName, Integer.valueOf(++d));

            //FieldName -> String
            SortedMap<FieldName, String> fieldBindingVarMap = recordCase.getFieldBindingVarMap();

            int recordPos = 0;

            // This creates, if necessary, a record equivalent to the original record minus the bound fields.         
            String baseRecordPatternVarName = recordCase.getBaseRecordPatternVarName();       
            if (baseRecordPatternVarName != null &&
                    !baseRecordPatternVarName.equals(Expression.RecordCase.WILDCARD_VAR)) {
                recordPos++;

                // Create a new record that is the original record minus the bound fields.
                QualifiedName qn = QualifiedName.make(currentMachineFunction.getQualifiedName().getModuleName(), baseRecordPatternVarName);
                newEnv.put (qn, Integer.valueOf(++d));

                // push the original record
                gp.code (new Instruction.I_Push(0));

                // consume the record on top of the stack and replace with an extended version
                gp.code (new Instruction.I_ExtendRecord());

                // Now remove fields from the record as appropriate.
                for (final FieldName fieldName : fieldBindingVarMap.keySet()) {
                  
                    gp.code (new Instruction.I_RemoveRecordField(fieldName.getCalSourceForm()));
                }

            }

            // Now push the values for the bound fields onto the stack.
            for (final Map.Entry<FieldName, String> entry : fieldBindingVarMap.entrySet()) {

                FieldName fieldName = entry.getKey();
                String bindingVarName = entry.getValue();

                //ignore anonymous pattern variables. These are guaranteed not to be used
                //by the result expression, and so don't need to be extracted from the condition record.
                if (!bindingVarName.equals(Expression.RecordCase.WILDCARD_VAR)) {

                    QualifiedName qn = QualifiedName.make(currentMachineFunction.getQualifiedName().getModuleName(), bindingVarName);
                    newEnv.put(qn, Integer.valueOf(++d));

                    gp.code (new Instruction.I_Push (recordPos));
                    gp.code (new Instruction.I_RecordSelection (fieldName.getCalSourceForm()));
                    recordPos++;
View Full Code Here

                  
                    String[] vars = getVars(alt, altTag);
                    Map<QualifiedName, Integer> newEnv = argOffset (0, p);

                    for (int j = 0; j < vars.length; ++j) {
                        QualifiedName qn = QualifiedName.make(moduleName, vars [j]);
                        newEnv.put (qn, Integer.valueOf(d + 1 + j));
                    }

                    InstructionList altGP = new InstructionList ();
                    altGP.code (new Instruction.I_Split (vars.length));
View Full Code Here

        retVal.depth = d + defs.length;
        Map<QualifiedName, Integer> newEnv = argOffset (0, env);

        for (int i = 0; i < defs.length; ++i) {
            Expression.Let.LetDefn def = defs [i];
            QualifiedName qn = QualifiedName.make(currentMachineFunction.getQualifiedName().getModuleName(), def.getVar());

            newEnv.put (qn, Integer.valueOf(d + i + 1));
        }

        retVal.env = newEnv;
View Full Code Here

                if (inst instanceof Instruction.I_PushGlobal) {
                    Instruction.I_PushGlobal pg = (Instruction.I_PushGlobal)inst;
                    // We need to handle the supercombinators that are hand-coded and
                    // therefore not in the actual Program object.
                    QualifiedName globalName = pg.getName();
                    NPrimitiveFunc npf = primitiveFuncMap.get (globalName);
                    if (npf != null) {
                        instructions[i] = new Instruction.I_PushPrimitiveNode(npf);
                    } else {
                        // We can do a fixup of the instruction if the function being pushed is not a CAF.
View Full Code Here

        if (basicOpExpressions != null) {
            if (basicOpExpressions.getPrimitiveOp() == PrimOps.PRIMOP_EAGER) {
                return true;
            }

            QualifiedName opName = basicOpExpressions.getName();
            MachineFunction mf = currentModule.getFunction(opName);
            if (mf == null) {
                return false;
            }

            if (mf.canFunctionBeEagerlyEvaluated()) {
                int nArgs = basicOpExpressions.getNArguments();
                int nWHNFArgs = 0;
                for (int i = 0; i < nArgs; ++i) {
                    Expression eArg = basicOpExpressions.getArgument(i);
                    if (canIgnoreLaziness(eArg, env)) {
                        nWHNFArgs++;
                    }
                }
                if (nArgs == nWHNFArgs) {
                    // All the args are in WHNF so ideally we can ignore laziness for
                    // this primitive operation.  However, there are some primitive
                    // ops where an additional condition, that the second argument is
                    // known to not be zero, is required.
                    String unqualifiedOpName = opName.getUnqualifiedName();
                    if (opName.getModuleName().equals(CAL_Prelude.MODULE_NAME) &&
                            (unqualifiedOpName.equals("divideLong") ||
                                    unqualifiedOpName.equals("remainderLong") ||
                                    unqualifiedOpName.equals("divideInt") ||
                                    unqualifiedOpName.equals("remainderInt") ||
                                    unqualifiedOpName.equals("divideShort") ||
View Full Code Here

    }
   
    private static void logEnvironment (Map<QualifiedName, Integer> env) {
        for (final Map.Entry<QualifiedName, Integer> entry : env.entrySet()) {
           
            QualifiedName key = entry.getKey();
            Integer val = entry.getValue();
            MACHINE_LOGGER.log(Level.FINE, "    " + key + ": " + val);
        }
    }
View Full Code Here

            case Instruction.T_Push:
                stack.push (stack.get (stack.size () - 1 - inst.getN()));
            break;
           
            case Instruction.T_PushGlobal:
                QualifiedName name = inst.getName();
                try {
                    GMachineFunction cl = (GMachineFunction)program.getCodeLabel(name);
                    if (cl != null) {
                        NGlobal ng = cl.makeNGlobal(executionContext);
                        stack.push (ng);
                    }               
                } catch (Program.ProgramException e) {
                    throw new CALExecutorException.InternalException ("Push Global: " + name + ": ", e);
                }
            break;
           
            case Instruction.T_PushGlobalN:
                stack.push (inst.getGlobalNode());
            break;
           
            case Instruction.T_PushPrimitiveNode:
                stack.push (inst.getNode());
            break;
           
            case Instruction.T_PushList:
                stack.push (inst.getNode());
            break;
           
            case Instruction.T_PushString:
                stack.push (inst.getNode());
            break;
           
            case Instruction.T_PushVVal:
                stack.push (inst.getNode());
            break;
           
            case Instruction.T_SelectDCField: {
                Node dcField = stack.peek().i_selectDCField((DataConstructor)inst.getInfo(), inst.getN(), ((Instruction.I_SelectDCField)inst).getErrorInfo());
                if (dcField != null) {
                    int updatePosition = stack.size () - 1;
                    stack.set(updatePosition, dcField);
                }
            }
            break;
           
            case Instruction.T_LazySelectDCField: {
                // create a lazy dc field selection node
                Node dcExpression = stack.pop ();
                Node lazyDCFieldSelection = new NDataConsFieldSelection(dcExpression, (DataConstructor)inst.getInfo(), inst.getN(), ((Instruction.I_LazySelectDCField)inst).getErrorInfo());
                stack.push (lazyDCFieldSelection);
            }
            break;
           
            case Instruction.T_Slide:
                n = inst.getN();
                Node o = stack.pop ();
                for (int i = 0; i < n; ++i) {
                    stack.pop ();
                }
               
                stack.push (o);
            break;
           
            case Instruction.T_Split:
                stack.peek ().i_split (this, inst.getN());
            break;
           
            case Instruction.T_Squeeze:
                stack.squeeze (inst.getX(), inst.getY());
            break;
           
            case Instruction.T_Switch:
                break;
           
            case Instruction.T_SwitchJ:
                i_switchJ (inst);
            break;
           
            case Instruction.T_Update:
                Node top = stack.pop ();
                int updatePosition = stack.size () - 1 - inst.getN();
                stack.get (updatePosition).setIndirectionNode(top);
                stack.set(updatePosition, top);
            break;
           
            // Create a new record node and push it onto the stack.
            case Instruction.T_CreateRecord:
                Node recordVal = new NRecordValue (inst.getN());
                stack.push (recordVal);
            break;
           
            // Modify the record on top of the stack.  It is
            // safe to modify the actual node because this instruction
            // only occurs as part of record creation/extension.
            case Instruction.T_PutRecordField:
                String fieldName = (String)inst.getInfo();
                Node fieldVal = stack.pop ();
                NRecordValue record = (NRecordValue)stack.peek();
                record.putValue(fieldName, fieldVal);
            break;
           
            // Replace the record on top of the stack with a copy
            // that can be modified.
            case Instruction.T_ExtendRecord:
                o = stack.pop();
                record = (NRecordValue)o;
                record = new NRecordValue (record);
                stack.push (record);
            break;
           
            // Replace the record value on top of the stack with
            // the value of a field in the record.
            case Instruction.T_RecordSelection:
                fieldName = (String)inst.getInfo();
                record = (NRecordValue)stack.pop();
                stack.push(record.getValue(fieldName));
            break;
           
            // Create a node representing the
            // application of record selection.
            case Instruction.T_LazyRecordSelection:
                fieldName = (String)inst.getInfo();
                o = stack.pop();
                stack.push (new NRecordSelection(fieldName, o));   
            break;
           
            case Instruction.T_LazyRecordUpdate:
            {
                o = stack.pop();
                record = new NRecordUpdate(o);
                stack.push (record);
                break;
            }
           
            // Create a node representing the application of
            // record extension
            case Instruction.T_LazyRecordExtension:
                o = stack.pop ();
                record = new NRecordExtension (o);
                stack.push (record);
            break;
           
            // Remove a field value from the record node on top
            // of the stack.  It is safe to modify the actual node
            // as this only happens as part of creating a new record instance.
            case Instruction.T_RemoveRecordField:
            {
                fieldName = (String)inst.getInfo();
                record = (NRecordValue)stack.peek();
                record.removeValue(fieldName);
                break;
            }
           
            case Instruction.T_Println:
                System.out.println(inst.getInfo().toString());
            break;
           
            case Instruction.T_CreateList:
                stack.push (new NValObject (new ArrayList<Object>()));
            break;
           
            case Instruction.T_PutListValue:
            {
                Node listNode = stack.pop ();
                Object listValue = listNode;
                if (listNode instanceof NVal) {
                    listValue = listNode.getValue();
                }
                List<Object> fl = UnsafeCast.unsafeCast(stack.peek().getValue());
                fl.add(listValue);
                break;
            }
           
            case Instruction.T_Cast:
            {
                i_Cast((Class<?>)inst.getInfo());
                break;
            }
           
            case Instruction.T_DebugProcessing:
            {
                // If tracing is turned on we want to generate a trace message for
                // the named function.
                QualifiedName functionName = inst.getName();
                if (executionContext.isDebugProcessingNeeded(functionName.getQualifiedName())) {
                    arity = inst.getArity();
                    Node args[] = new Node[arity];
                    // The arguments are on the top of the stack.
                    for (int i = 0; i < arity; ++i) {
                        args[i] = stack.get(stack.size() - i - 1);
                    }

                    executionContext.debugProcessing(functionName.getQualifiedName(), args);
                }               
                break;
            }
           
           
View Full Code Here

        reset ();
        if (entryPoint == null) {
            throw new CALExecutorException.InternalException ("null EntryPoint.", null);
        }
       
        final QualifiedName entryPointSCName = ((EntryPointImpl)entryPoint).getFunctionName();
                  
        if (arguments != null) {
            nPushedArguments = arguments.length;
            for (int i = nPushedArguments - 1; i >= 0; --i) {
                stack.push (new NValObject (arguments[i]));
View Full Code Here

                    arguments.consumeArgument();
                   
                    final String entryPointNameString = arguments.getAndConsumeArgument();
                    final String mainClassName = arguments.getAndConsumeArgument();

                    final QualifiedName entryPointName;
                    try {
                        entryPointName = QualifiedName.makeFromCompoundName(entryPointNameString);

                    } catch (final IllegalArgumentException e) {
                        logger.info(StandaloneJarToolMessages.getString("invalidEntryPointName", entryPointNameString));
View Full Code Here

TOP

Related Classes of org.openquark.cal.compiler.QualifiedName

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.