/** * Checks if a variable is appearing within the last body functor in which it occurs, and only does so within * argument position. * * @param var The variable to check. * @param body The current body functor being processed. * * @return <tt>true</tt> iff this is the last body functor that the variable appears in and does so only in argument * position. */ private boolean isLastBodyTermInArgPositionOnly(Term var, Functor body) { return body == symbolTable.get(var.getSymbolKey(), SymbolTableKeys.SYMKEY_VAR_LAST_ARG_FUNCTOR); } }
/** * Functors are considered top-level when they appear at the top-level within a clause, or directly beneath a parent * conjunction or disjunction that is considered to be top-level. * * @param context The position context to examine. * * @return <tt>true</tt> iff the current position is a top-level functor. */ private boolean isTopLevel(PositionalContext context) { Term term = context.getTerm(); if (term.getSymbolKey() == null) { return false; } Boolean isTopLevel = (Boolean) symbolTable.get(term.getSymbolKey(), SymbolTableKeys.SYMKEY_TOP_LEVEL_FUNCTOR); return (isTopLevel == null) ? false : isTopLevel; } }
/** * Creates an instruction for the specified mnemonic that takes one register and one functor argument. * * @param mnemonic The instruction mnemonic. * @param mode1 The addressing mode to use with the register argument. * @param reg1 The register argument. * @param fn The functor argument. * @param reg1Term The term that is assigned or associated with reg1. */ public WAMInstruction(WAMInstructionSet mnemonic, byte mode1, byte reg1, FunctorName fn, Term reg1Term) { this.mnemonic = mnemonic; this.mode1 = mode1; this.reg1 = reg1; this.fn = fn; // Record the symbol keys of the term that resulted in the creation of the instruction, and are associated // with reg1 of the instruction. symbolKeyReg1 = reg1Term.getSymbolKey(); functorNameReg1 = reg1Term.isFunctor() ? ((Functor) reg1Term).getName() : null; }
/** * Creates an instruction for the specified mnemonic that takes a single register argument. * * @param mnemonic The instruction mnemonic. * @param mode1 The addressing mode to use with the register argument. * @param reg1 The single register argument. * @param reg1Term The term that is assigned or associated with reg1. */ public WAMInstruction(WAMInstructionSet mnemonic, byte mode1, byte reg1, Term reg1Term) { this.mnemonic = mnemonic; this.mode1 = mode1; this.reg1 = reg1; // Record the symbol keys of the term that resulted in the creation of the instruction, and are associated // with reg1 of the instruction. symbolKeyReg1 = reg1Term.getSymbolKey(); functorNameReg1 = reg1Term.isFunctor() ? ((Functor) reg1Term).getName() : null; }
/** * Allocates terms within a functor expression to registers. The outermost functor itself is not assigned to a * register in WAM (only in l0). Functors already directly assigned to argument registers will not be re-assigned by * this. Variables as arguments will be assigned but not as argument registers. * * @param expression The expression to walk over. */ protected void allocateTemporaryRegisters(Term expression) { // Need to assign registers to the whole syntax tree, working in from the outermost functor. The outermost // functor itself is not assigned to a register in l3 (only in l0). Functors already directly assigned to // argument registers will not be re-assigned by this, variables as arguments will be assigned. SearchMethod outInSearch = new BreadthFirstSearch<Term, Term>(); outInSearch.reset(); outInSearch.addStartState(expression); Iterator<Term> treeWalker = Searches.allSolutions(outInSearch); // Discard the outermost functor from the variable allocation. treeWalker.next(); // For each term encountered: set X++ = term. while (treeWalker.hasNext()) { Term term = treeWalker.next(); if (symbolTable.get(term.getSymbolKey(), SymbolTableKeys.SYMKEY_ALLOCATION) == null) { int allocation = (lastAllocatedTempReg++ & 0xff) | (REG_ADDR << 8); symbolTable.put(term.getSymbolKey(), SymbolTableKeys.SYMKEY_ALLOCATION, allocation); } } }
symbolTable.put(term.getSymbolKey(), SymbolTableKeys.SYMKEY_ALLOCATION, allocation);
(Boolean) symbolTable.get(parentTerm.getSymbolKey(), SymbolTableKeys.SYMKEY_TOP_LEVEL_FUNCTOR);
/** {@inheritDoc} */ public void visit(Functor functor) { // functor is ground if all of its arguments are ground. boolean ground = true; if (functor.getArguments() != null) { for (Term argument : functor.getArguments()) { SymbolKey symbolKey = argument.getSymbolKey(); TermDomain annotation = (TermDomain) symbolTable.get(symbolKey, TERM_DOMAIN); if ((annotation == null) || !annotation.ground) { ground = false; break; } } } /*log.fine((ground ? "ground " : "non-ground ") + functor.toString(interner, true, false));*/ symbolTable.put(functor.getSymbolKey(), TERM_DOMAIN, new TermDomain(ground)); }
/** {@inheritDoc} */ public void visit(Functor functor) { // functor is ground if all of its arguments are ground. boolean ground = true; if (functor.getArguments() != null) { for (Term argument : functor.getArguments()) { SymbolKey symbolKey = argument.getSymbolKey(); TermDomain annotation = (TermDomain) symbolTable.get(symbolKey, TERM_DOMAIN); if ((annotation == null) || !annotation.ground) { ground = false; break; } } } /*log.fine((ground ? "ground " : "non-ground ") + functor.toString(interner, true, false));*/ symbolTable.put(functor.getSymbolKey(), TERM_DOMAIN, new TermDomain(ground)); }
allocation = (Integer) symbolTable.get(nextArg.getSymbolKey(), SymbolTableKeys.SYMKEY_ALLOCATION); addrMode = (byte) ((allocation & 0xff00) >> 8); address = (byte) (allocation & 0xff); symbolTable.put(nextArg.getSymbolKey(), SymbolTableKeys.SYMKEY_VARIABLE_INTRO, VarIntroduction.Unify); (VarIntroduction) symbolTable.get(nextArg.getSymbolKey(), SymbolTableKeys.SYMKEY_VARIABLE_INTRO); nextArg); symbolTable.put(nextArg.getSymbolKey(), SymbolTableKeys.SYMKEY_VARIABLE_INTRO, null); int allocation = (Integer) symbolTable.get(nextVar.getSymbolKey(), SymbolTableKeys.SYMKEY_ALLOCATION); byte addrMode = (byte) ((allocation & 0xff00) >> 8); byte address = (byte) (allocation & 0xff); symbolTable.put(nextVar.getSymbolKey(), SymbolTableKeys.SYMKEY_VARIABLE_INTRO, VarIntroduction.Get);
/** {@inheritDoc} */ public SizeableLinkedList<WAMInstruction> compileBodyCall(Functor expression, boolean isFirstBody, boolean isLastBody, boolean chainRule, int permVarsRemaining) { SizeableLinkedList<WAMInstruction> instructions = new SizeableLinkedList<WAMInstruction>(); if (isFirstBody) { instructions.add(new WAMInstruction(NeckCut)); } else { Integer cutLevelVarAllocation = (Integer) defaultBuiltIn.getSymbolTable().get(CutLevelVariable.CUT_LEVEL_VARIABLE.getSymbolKey(), SymbolTableKeys.SYMKEY_ALLOCATION); instructions.add(new WAMInstruction(Cut, (byte) (cutLevelVarAllocation & 0xff))); } return instructions; }
(Integer) symbolTable.get(nextOutermostArg.getSymbolKey(), SymbolTableKeys.SYMKEY_ALLOCATION); symbolTable.put(nextOutermostArg.getSymbolKey(), SymbolTableKeys.SYMKEY_VARIABLE_INTRO, VarIntroduction.Put); instructions.add(instruction); symbolTable.put(nextOutermostArg.getSymbolKey(), SymbolTableKeys.SYMKEY_VAR_LAST_ARG_FUNCTOR, null); (Integer) symbolTable.get(nextArg.getSymbolKey(), SymbolTableKeys.SYMKEY_ALLOCATION); addrMode = (byte) ((allocation & 0xff00) >> 8); address = (byte) (allocation & 0xff); symbolTable.put(nextArg.getSymbolKey(), SymbolTableKeys.SYMKEY_VARIABLE_INTRO, VarIntroduction.Set); (VarIntroduction) symbolTable.get(nextArg.getSymbolKey(), SymbolTableKeys.SYMKEY_VARIABLE_INTRO); nextArg); symbolTable.put(nextArg.getSymbolKey(), SymbolTableKeys.SYMKEY_VARIABLE_INTRO, null);