Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/ground #148

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/compile/GuardCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
// ガードインライン
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1295,9 +1296,11 @@ List<Integer> 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;
}

Expand Down
38 changes: 33 additions & 5 deletions src/compile/Optimizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,15 @@ 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;
Expand Down Expand Up @@ -257,15 +262,36 @@ private static void findproccxtMove(List<Instruction> insts) { // seiji
}
}

/** allocatom に続く getfunc を loadfunc に変換する
/** ガードでの allocatom に続く getfunc を loadfunc に変換する
* allocatom は単項アトムにしか使わないので,isunary の検査も消去する
* allocatom が参照されなくなった場合は対応する freeatom とともに除去する
* allocatom が参照されなくなった(または最初からされない)場合は
* 対応する freeatom とともに除去する
* @param match ガードまでの命令列
* @param body 右辺命令列
*/
// TODO: ガードで本来必要なのはファンクタだけなので,要らなくなるアトムを
// 作って解放するのでなく,最初からファンクタだけを扱うのが望ましい
private static void allocatomReduce(List<Instruction> match, List<Instruction> 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;
Expand All @@ -292,6 +318,7 @@ private static void allocatomReduce(List<Instruction> match, List<Instruction> 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;
}
Expand All @@ -306,7 +333,8 @@ private static void allocatomReduce(List<Instruction> match, List<Instruction> 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--;
}
Expand Down
4 changes: 3 additions & 1 deletion src/compile/RuleCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ private List<LinkOccurrence> 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);
Expand Down Expand Up @@ -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()));
Expand Down Expand Up @@ -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)));
Expand Down