Skip to content

Commit

Permalink
10_altinst_flat_1
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudroger committed Apr 16, 2018
1 parent f855819 commit c74a429
Show file tree
Hide file tree
Showing 4 changed files with 409 additions and 8 deletions.
4 changes: 4 additions & 0 deletions java/com/google/re2j/Compiler.java
Expand Up @@ -275,10 +275,14 @@ private Frag compile(Regexp re) {
return nop();
} else {
Frag f = null;
int[] ops = new int[re.subs.length];
int i = 0;
for (Regexp sub : re.subs) {
Frag f1 = compile(sub);
ops[i++] = f1.i;
f = (f == null) ? f1 : alt(f, f1);
}
((Inst.Alt2Inst)prog.inst[f.i]).ops = ops;
return f;
}
}
Expand Down
236 changes: 231 additions & 5 deletions java/com/google/re2j/Inst.java
Expand Up @@ -30,7 +30,7 @@ public static Inst of(int op) {
switch (op) {
case ALT:
case ALT_MATCH:
return new AltInst(op);
return new Alt2Inst(op);
case CAPTURE:
return new CaptureInst(op);
case EMPTY_WIDTH:
Expand Down Expand Up @@ -175,10 +175,11 @@ final boolean matchRune(int r) {
}
}

public static final class AltInst extends Inst {
Inst argInst;
public static final class Alt2Inst extends Inst {
Inst inst2;
int[] ops;

AltInst(int op) {
Alt2Inst(int op) {
super(op);
}

Expand All @@ -190,7 +191,232 @@ protected final Machine.Thread add(Machine.Queue q, int pos, int[] cap, int cond
q.add(pc);

t = outInst.add(q, pos, cap, cond, t, m);
t = argInst.add(q, pos, cap, cond, t, m);
t = inst2.add(q, pos, cap, cond, t, m);

return t;
}
}

public static final class Alt3Inst extends Inst {
Inst inst2;
Inst inst3;
int[] ops;

Alt3Inst(Alt2Inst orig) {
super(orig.op);
this.out = orig.out;
this.arg = orig.arg;
this.pc = orig.pc;
this.ops = orig.ops;
}

@Override
protected final Machine.Thread add(Machine.Queue q, int pos, int[] cap, int cond, Machine.Thread t, Machine m) {
if (q.contains(pc)) {
return t;
}
q.add(pc);

t = outInst.add(q, pos, cap, cond, t, m);
t = inst2.add(q, pos, cap, cond, t, m);
t = inst3.add(q, pos, cap, cond, t, m);

return t;
}
}
public static final class Alt4Inst extends Inst {
Inst inst2;
Inst inst3;
Inst inst4;
int[] ops;

Alt4Inst(Alt2Inst orig) {
super(orig.op);
this.out = orig.out;
this.arg = orig.arg;
this.pc = orig.pc;
this.ops = orig.ops;
}

@Override
protected final Machine.Thread add(Machine.Queue q, int pos, int[] cap, int cond, Machine.Thread t, Machine m) {
if (q.contains(pc)) {
return t;
}
q.add(pc);

t = outInst.add(q, pos, cap, cond, t, m);
t = inst2.add(q, pos, cap, cond, t, m);
t = inst3.add(q, pos, cap, cond, t, m);
t = inst4.add(q, pos, cap, cond, t, m);

return t;
}
}
public static final class Alt5Inst extends Inst {
Inst inst2;
Inst inst3;
Inst inst4;
Inst inst5;
int[] ops;

Alt5Inst(Alt2Inst orig) {
super(orig.op);
this.out = orig.out;
this.arg = orig.arg;
this.pc = orig.pc;
this.ops = orig.ops;
}

@Override
protected final Machine.Thread add(Machine.Queue q, int pos, int[] cap, int cond, Machine.Thread t, Machine m) {
if (q.contains(pc)) {
return t;
}
q.add(pc);

t = outInst.add(q, pos, cap, cond, t, m);
t = inst2.add(q, pos, cap, cond, t, m);
t = inst3.add(q, pos, cap, cond, t, m);
t = inst4.add(q, pos, cap, cond, t, m);
t = inst5.add(q, pos, cap, cond, t, m);

return t;
}
}

public static final class Alt6Inst extends Inst {
Inst inst2;
Inst inst3;
Inst inst4;
Inst inst5;
Inst inst6;
int[] ops;

Alt6Inst(Alt2Inst orig) {
super(orig.op);
this.out = orig.out;
this.arg = orig.arg;
this.pc = orig.pc;
this.ops = orig.ops;
}

@Override
protected final Machine.Thread add(Machine.Queue q, int pos, int[] cap, int cond, Machine.Thread t, Machine m) {
if (q.contains(pc)) {
return t;
}
q.add(pc);

t = outInst.add(q, pos, cap, cond, t, m);
t = inst2.add(q, pos, cap, cond, t, m);
t = inst3.add(q, pos, cap, cond, t, m);
t = inst4.add(q, pos, cap, cond, t, m);
t = inst5.add(q, pos, cap, cond, t, m);
t = inst6.add(q, pos, cap, cond, t, m);

return t;
}
}


public static final class Alt7Inst extends Inst {
Inst inst2;
Inst inst3;
Inst inst4;
Inst inst5;
Inst inst6;
Inst inst7;
int[] ops;

Alt7Inst(Alt2Inst orig) {
super(orig.op);
this.out = orig.out;
this.arg = orig.arg;
this.pc = orig.pc;
this.ops = orig.ops;
}

@Override
protected final Machine.Thread add(Machine.Queue q, int pos, int[] cap, int cond, Machine.Thread t, Machine m) {
if (q.contains(pc)) {
return t;
}
q.add(pc);

t = outInst.add(q, pos, cap, cond, t, m);
t = inst2.add(q, pos, cap, cond, t, m);
t = inst3.add(q, pos, cap, cond, t, m);
t = inst4.add(q, pos, cap, cond, t, m);
t = inst5.add(q, pos, cap, cond, t, m);
t = inst6.add(q, pos, cap, cond, t, m);
t = inst7.add(q, pos, cap, cond, t, m);

return t;
}
}

public static final class Alt8Inst extends Inst {
Inst inst2;
Inst inst3;
Inst inst4;
Inst inst5;
Inst inst6;
Inst inst7;
Inst inst8;
int[] ops;

Alt8Inst(Alt2Inst orig) {
super(orig.op);
this.out = orig.out;
this.arg = orig.arg;
this.pc = orig.pc;
this.ops = orig.ops;
}

@Override
protected final Machine.Thread add(Machine.Queue q, int pos, int[] cap, int cond, Machine.Thread t, Machine m) {
if (q.contains(pc)) {
return t;
}
q.add(pc);

t = outInst.add(q, pos, cap, cond, t, m);
t = inst2.add(q, pos, cap, cond, t, m);
t = inst3.add(q, pos, cap, cond, t, m);
t = inst4.add(q, pos, cap, cond, t, m);
t = inst5.add(q, pos, cap, cond, t, m);
t = inst6.add(q, pos, cap, cond, t, m);
t = inst7.add(q, pos, cap, cond, t, m);
t = inst8.add(q, pos, cap, cond, t, m);

return t;
}
}


public static final class AltManyInst extends Inst {
public Inst[] insts;
int[] ops;

AltManyInst(Alt2Inst orig) {
super(orig.op);
this.out = orig.out;
this.arg = orig.arg;
this.pc = orig.pc;
this.ops = orig.ops;
}

@Override
protected final Machine.Thread add(Machine.Queue q, int pos, int[] cap, int cond, Machine.Thread t, Machine m) {
if (q.contains(pc)) {
return t;
}
q.add(pc);

for (Inst inst : insts) {
t = inst.add(q, pos, cap, cond, t, m);
}

return t;
}
Expand Down
97 changes: 94 additions & 3 deletions java/com/google/re2j/Prog.java
Expand Up @@ -180,16 +180,107 @@ public String toString() {
}

public void linkInst() {
// enlarge alt to Alt Many
for(int i = 0; i < instSize; i++) {
Inst ii = inst[i];
if (Inst.isAltOp(ii.op)) {
Inst.Alt2Inst altInst = (Inst.Alt2Inst) ii;
if (altInst.ops != null) {

int altSize = altInst.ops.length;

if (altSize > 2) {
if (altSize == 3) {
this.inst[i] = new Inst.Alt3Inst(altInst);
} else if (altSize == 4) {
this.inst[i] = new Inst.Alt4Inst(altInst);
} else if (altSize == 5) {
this.inst[i] = new Inst.Alt5Inst(altInst);
} else if (altSize == 6) {
this.inst[i] = new Inst.Alt6Inst(altInst);
} else if (altSize == 7) {
this.inst[i] = new Inst.Alt7Inst(altInst);
} else if (altSize == 8) {
this.inst[i] = new Inst.Alt8Inst(altInst);
} else {
this.inst[i] = new Inst.AltManyInst(altInst);
}
}
}
}
}



for(int i = 0; i < instSize; i++) {
Inst ii = inst[i];
if (Inst.isAltOp(ii.op)) {
((Inst.AltInst)ii).argInst = inst[ii.arg];
}
ii.outInst = inst[ii.out];
if (ii instanceof Inst.Alt2Inst) {
Inst.Alt2Inst altInst = (Inst.Alt2Inst) ii;
altInst.outInst = inst[ii.out];
altInst.inst2 = inst[ii.arg];
} else if (ii instanceof Inst.Alt3Inst) {
Inst.Alt3Inst altInst = (Inst.Alt3Inst) ii;
altInst.outInst = inst[altInst.ops[0]];
altInst.inst2 = inst[altInst.ops[1]];
altInst.inst3 = inst[altInst.ops[2]];
} else if (ii instanceof Inst.Alt4Inst) {
Inst.Alt4Inst altInst = (Inst.Alt4Inst) ii;
altInst.outInst = inst[altInst.ops[0]];
altInst.inst2 = inst[altInst.ops[1]];
altInst.inst3 = inst[altInst.ops[2]];
altInst.inst4 = inst[altInst.ops[3]];
} else if (ii instanceof Inst.Alt5Inst) {
Inst.Alt5Inst altInst = (Inst.Alt5Inst) ii;
altInst.outInst = inst[altInst.ops[0]];
altInst.inst2 = inst[altInst.ops[1]];
altInst.inst3 = inst[altInst.ops[2]];
altInst.inst4 = inst[altInst.ops[3]];
altInst.inst5 = inst[altInst.ops[4]];
} else if (ii instanceof Inst.Alt6Inst) {
Inst.Alt6Inst altInst = (Inst.Alt6Inst) ii;
altInst.outInst = inst[altInst.ops[0]];
altInst.inst2 = inst[altInst.ops[1]];
altInst.inst3 = inst[altInst.ops[2]];
altInst.inst4 = inst[altInst.ops[3]];
altInst.inst5 = inst[altInst.ops[4]];
altInst.inst6 = inst[altInst.ops[5]];
} else if (ii instanceof Inst.Alt7Inst) {
Inst.Alt7Inst altInst = (Inst.Alt7Inst) ii;
altInst.outInst = inst[altInst.ops[0]];
altInst.inst2 = inst[altInst.ops[1]];
altInst.inst3 = inst[altInst.ops[2]];
altInst.inst4 = inst[altInst.ops[3]];
altInst.inst5 = inst[altInst.ops[4]];
altInst.inst6 = inst[altInst.ops[5]];
altInst.inst7 = inst[altInst.ops[6]];
} else if (ii instanceof Inst.Alt8Inst) {
Inst.Alt8Inst altInst = (Inst.Alt8Inst) ii;
altInst.outInst = inst[altInst.ops[0]];
altInst.inst2 = inst[altInst.ops[1]];
altInst.inst3 = inst[altInst.ops[2]];
altInst.inst4 = inst[altInst.ops[3]];
altInst.inst5 = inst[altInst.ops[4]];
altInst.inst6 = inst[altInst.ops[5]];
altInst.inst7 = inst[altInst.ops[6]];
altInst.inst8 = inst[altInst.ops[7]];
} else {
Inst.AltManyInst altInst = (Inst.AltManyInst) ii;
altInst.insts = buildManyAltInsts(0, altInst.ops);
}
} else {
ii.outInst = inst[ii.out];
}
ii.pc = i;
}
startInst = inst[start];
}

private Inst[] buildManyAltInsts(int start, int[] ops) {
Inst[] insts = new Inst[ops.length - start];
for (int j = start; j < ops.length; j++) {
insts[j - start] = inst[ops[j]];
}
return insts;
}
}

0 comments on commit c74a429

Please sign in to comment.