From 1c39ac0bb7e0ae17d38a0c8791ec4be6e9ee0e5d Mon Sep 17 00:00:00 2001 From: Kazunori Ueda Date: Thu, 30 Mar 2023 23:59:37 +0900 Subject: [PATCH 1/3] fix problem of allocatom of symbol atoms --- src/compile/GuardCompiler.java | 11 ++++++---- src/compile/Optimizer.java | 37 +++++++++++++++++++++++++++++----- src/compile/RuleCompiler.java | 4 +++- 3 files changed, 42 insertions(+), 10 deletions(-) diff --git a/src/compile/GuardCompiler.java b/src/compile/GuardCompiler.java index fff74724..4b0e7f75 100644 --- a/src/compile/GuardCompiler.java +++ b/src/compile/GuardCompiler.java @@ -377,8 +377,8 @@ else if (func.getName().equals("ground") || func.getName().equals("hlground")) { error("COMPILE ERROR: incompatible attributes in ground constraints"); } hlgroundAttrs.put(def1, attrAtoms); - // System.out.println("#hlgroundAttrs: " + hlgroundAttrs.get(def1).length); - // System.out.println("typedCxtTypes: " + typedCxtTypes); + // System.err.println("#hlgroundAttrs: " + hlgroundAttrs.get(def1).length); + // System.err.println("typedCxtTypes: " + typedCxtTypes); checkHLGroundLink(func, def1); } // ガードインライン @@ -444,8 +444,8 @@ else if (func.getName().startsWith("custom_")) { // Env.p("VAR## "+srcPath); if (srcPath == UNBOUND) continue FixType; uniqVars.add(srcPath); - // System.out.println("typedCxtTypes, defK: " + typedCxtTypes + " " + defK); - // System.out.println("hlgroundAttrs: " + hlgroundAttrs); + // System.err.println("typedCxtTypes, defK: " + typedCxtTypes + " " + defK); + // System.err.println("hlgroundAttrs: " + hlgroundAttrs); } match.add(new Instruction(Instruction.UNIQ, uniqVars)); @@ -880,6 +880,7 @@ private void bindToFunctor(ContextDef def, Functor func) throws CompileException int atomid = varCount++; typedCxtSrcs.put(def, atomid); typedCxtDefs.add(def); + // System.err.println("typedCxtDefs " + typedCxtDefs + " " + atomid + " " + varCount); match.add(new Instruction(Instruction.ALLOCATOM, atomid, func)); } else { checkUnaryProcessContext(def); @@ -1295,9 +1296,11 @@ List getAtomActuals() { for (int i = 0; i < atoms.size(); i++) { args.add(atomPaths.get(atoms.get(i))); } + // System.err.println("typedCxtDefs = " + typedCxtDefs); for (ContextDef def : typedCxtDefs) { if (typedCxtTypes.get(def) == UNARY_ATOM_TYPE) args.add(typedcxtToSrcPath(def)); } + // System.err.println("args: " + args); return args; } diff --git a/src/compile/Optimizer.java b/src/compile/Optimizer.java index 04c57567..b778f7ea 100644 --- a/src/compile/Optimizer.java +++ b/src/compile/Optimizer.java @@ -73,10 +73,14 @@ public static void setLevel(int level) { */ public static void optimizeRule(Rule rule) { // TODO 最適化器を統合する + // System.err.println("rule.guard0 " + rule.guard); Compactor.compactRule(rule); + // System.err.println("rule.guard1 " + rule.guard); // TODO 本質的にインライン展開が必要ないものは、展開しなくてもできるようにする - if (fInlining || fGuardMove || fGrouping || fReuseMem || fLoop || rule.isTypeDef) { - // head と guard をくっつける + // ガードの allocatom は現行の slim で扱えないので必ず allocatomReduce + // で消去する必要がある.このため常に head と guard をくっつける + // if (fInlining || fGuardMove || fGrouping || fReuseMem || fLoop || rule.isTypeDef) { + if (true) { inlineExpandTailJump(rule.memMatch); rule.guardLabel = null; rule.guard = null; @@ -257,15 +261,36 @@ private static void findproccxtMove(List insts) { // seiji } } - /** allocatom に続く getfunc を loadfunc に変換する + /** ガードでの allocatom に続く getfunc を loadfunc に変換する * allocatom は単項アトムにしか使わないので,isunary の検査も消去する - * allocatom が参照されなくなった場合は対応する freeatom とともに除去する + * allocatom が参照されなくなった(または最初からされない)場合は + * 対応する freeatom とともに除去する * @param match ガードまでの命令列 * @param body 右辺命令列 */ + // TODO: ガードで本来必要なのはファンクタだけなので,要らなくなるアトムを + // 作って解放するのでなく,最初からファンクタだけを扱うのが望ましい private static void allocatomReduce(List match, List body) { // ueda int maxm = match.size(); int maxb = body.size(); + + // typedef のコンパイル時に呼ばれた場合(命令列 match の最後が + // jump でなく succreturn の場合)は何もしない + Instruction jump = match.get(maxm - 1); + // System.err.println("jump: " + jump); + if (jump.getKind() != Instruction.JUMP) return; + + // 通常ルールの場合は,freeatom 命令の消去作業に備えて, + // allocatom (guard), freeatom (body) のレジスタ番号の対応関係表を作る + // 対応関係はガードからボディへのjump命令からわかる + List memargs = (List) jump.getArg2(); + List atomargs = (List) jump.getArg3(); + HashMap map = new HashMap(); + for (int i = 0; i < atomargs.size(); i++) { + map.put(atomargs.get(i), memargs.size() + i); + } + // System.err.println("map: " + map); + for (int i = 0; i < maxm; i++) { if (match.get(i).getKind() == Instruction.ALLOCATOM) { boolean referred = false; @@ -292,6 +317,7 @@ private static void allocatomReduce(List match, List b for (int k = 0; k < size; k++) { if (inst.getArgType(k) == Instruction.ARG_ATOM) { if ((Integer) inst.data.get(k) == allocreg) { + // System.err.println("ALLOCATOM referred! " + inst); referred = true; break; } @@ -306,7 +332,8 @@ private static void allocatomReduce(List match, List b for (int j = 0; j < maxb; j++) { if (body.get(j).getKind() == Instruction.FREEATOM) { int freereg = (Integer) body.get(j).getArg1(); - if (allocreg == freereg) { + // System.err.println("allocreg, freereg: " + allocreg + " " + freereg); + if ((int)map.get(allocreg) == freereg) { body.remove(j); maxb--; } diff --git a/src/compile/RuleCompiler.java b/src/compile/RuleCompiler.java index c0f34df1..7d66c8f6 100644 --- a/src/compile/RuleCompiler.java +++ b/src/compile/RuleCompiler.java @@ -258,7 +258,7 @@ private List computeRHSLinks() { } // unary型付プロセス文脈のリンク出現 - // System.out.println("computeRHSLinks 2: " + rhstypedcxtpaths.keySet()); + // System.err.println("computeRHSLinks 2: " + rhstypedcxtpaths.keySet()); for (ProcessContext atom : rhstypedcxtpaths.keySet()) { body.add(new Instruction(Instruction.ALLOCLINK, varcount, rhstypedcxtToPath(atom), 0)); rhslinkpath.put(atom.args[0], varcount); @@ -655,6 +655,7 @@ private void compile_g() throws CompileException { } varcount = gc.varCount; guard.add(0, Instruction.spec(formals, varcount)); + // System.err.println("generating jump: " + gc.getAtomActuals()); guard.add( Instruction.jump( theRule.bodyLabel, gc.getMemActuals(), gc.getAtomActuals(), gc.getVarActuals())); @@ -839,6 +840,7 @@ private void removeLHSTypedProcesses() { private void freeLHSTypedProcesses() { for (ContextDef def : rs.typedProcessContexts.values()) { if (gc.typedCxtTypes.get(def) == GuardCompiler.UNARY_ATOM_TYPE) { + // System.err.println("freeLHSTypedProcesses " + def + " " + typedcxtToSrcPath(def)); body.add(new Instruction(Instruction.FREEATOM, typedcxtToSrcPath(def))); } else if (gc.typedCxtTypes.get(def) == GuardCompiler.GROUND_LINK_TYPE) { body.add(new Instruction(Instruction.FREEGROUND, groundToSrcPath(def))); From 89a4af2d9761af4902346b2f20a818c8e495d5c4 Mon Sep 17 00:00:00 2001 From: Kazunori Ueda Date: Fri, 31 Mar 2023 00:15:55 +0900 Subject: [PATCH 2/3] minor fix of comment --- src/compile/Optimizer.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/compile/Optimizer.java b/src/compile/Optimizer.java index b778f7ea..2ccb5faa 100644 --- a/src/compile/Optimizer.java +++ b/src/compile/Optimizer.java @@ -77,8 +77,9 @@ public static void optimizeRule(Rule rule) { Compactor.compactRule(rule); // System.err.println("rule.guard1 " + rule.guard); // TODO 本質的にインライン展開が必要ないものは、展開しなくてもできるようにする - // ガードの allocatom は現行の slim で扱えないので必ず allocatomReduce - // で消去する必要がある.このため常に head と guard をくっつける + // ガードでのシンボルアトムの allocatom は現行の slim で扱えないので + // 必ず allocatomReduce で消去する必要がある(データアトムは大丈夫). + // このため,コンパイルオプションにかかわらず常に head と guard をくっつける // if (fInlining || fGuardMove || fGrouping || fReuseMem || fLoop || rule.isTypeDef) { if (true) { inlineExpandTailJump(rule.memMatch); From 809b3ec2b9c8a7f152358c625ce3c15782f73c48 Mon Sep 17 00:00:00 2001 From: lang Date: Thu, 30 Mar 2023 15:33:28 +0000 Subject: [PATCH 3/3] Automated: Reformat Java source code. --- src/compile/GuardCompiler.java | 8 ++++---- src/compile/Optimizer.java | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/compile/GuardCompiler.java b/src/compile/GuardCompiler.java index 4b0e7f75..af2c4123 100644 --- a/src/compile/GuardCompiler.java +++ b/src/compile/GuardCompiler.java @@ -377,8 +377,8 @@ else if (func.getName().equals("ground") || func.getName().equals("hlground")) { error("COMPILE ERROR: incompatible attributes in ground constraints"); } hlgroundAttrs.put(def1, attrAtoms); - // System.err.println("#hlgroundAttrs: " + hlgroundAttrs.get(def1).length); - // System.err.println("typedCxtTypes: " + typedCxtTypes); + // System.err.println("#hlgroundAttrs: " + hlgroundAttrs.get(def1).length); + // System.err.println("typedCxtTypes: " + typedCxtTypes); checkHLGroundLink(func, def1); } // ガードインライン @@ -444,8 +444,8 @@ else if (func.getName().startsWith("custom_")) { // Env.p("VAR## "+srcPath); if (srcPath == UNBOUND) continue FixType; uniqVars.add(srcPath); - // System.err.println("typedCxtTypes, defK: " + typedCxtTypes + " " + defK); - // System.err.println("hlgroundAttrs: " + hlgroundAttrs); + // System.err.println("typedCxtTypes, defK: " + typedCxtTypes + " " + defK); + // System.err.println("hlgroundAttrs: " + hlgroundAttrs); } match.add(new Instruction(Instruction.UNIQ, uniqVars)); diff --git a/src/compile/Optimizer.java b/src/compile/Optimizer.java index 2ccb5faa..abe0a350 100644 --- a/src/compile/Optimizer.java +++ b/src/compile/Optimizer.java @@ -269,8 +269,8 @@ private static void findproccxtMove(List insts) { // seiji * @param match ガードまでの命令列 * @param body 右辺命令列 */ - // TODO: ガードで本来必要なのはファンクタだけなので,要らなくなるアトムを - // 作って解放するのでなく,最初からファンクタだけを扱うのが望ましい + // TODO: ガードで本来必要なのはファンクタだけなので,要らなくなるアトムを + // 作って解放するのでなく,最初からファンクタだけを扱うのが望ましい private static void allocatomReduce(List match, List body) { // ueda int maxm = match.size(); int maxb = body.size(); @@ -288,7 +288,7 @@ private static void allocatomReduce(List match, List b List atomargs = (List) jump.getArg3(); HashMap map = new HashMap(); for (int i = 0; i < atomargs.size(); i++) { - map.put(atomargs.get(i), memargs.size() + i); + map.put(atomargs.get(i), memargs.size() + i); } // System.err.println("map: " + map); @@ -334,7 +334,7 @@ private static void allocatomReduce(List match, List b if (body.get(j).getKind() == Instruction.FREEATOM) { int freereg = (Integer) body.get(j).getArg1(); // System.err.println("allocreg, freereg: " + allocreg + " " + freereg); - if ((int)map.get(allocreg) == freereg) { + if ((int) map.get(allocreg) == freereg) { body.remove(j); maxb--; }