Search in sources :

Example 1 with Bytes

use of suite.primitive.Bytes in project suite by stupidsing.

the class Assembler method assemble.

public Bytes assemble(String in0) {
    Set<Character> whitespaces = Collections.singleton('\n');
    Fun<String, List<Run>> gct = CommentPreprocessor.groupCommentPreprocessor(whitespaces);
    Fun<String, List<Run>> lct = CommentPreprocessor.lineCommentPreprocessor(whitespaces);
    String in1 = Preprocess.transform(List.of(gct, lct), in0).t0;
    Generalizer generalizer = new Generalizer();
    List<String> lines = List.of(in1.split("\n"));
    Pair<String, String> pe;
    int start = 0;
    while (!(pe = String_.split2(lines.get(start), "=")).t1.isEmpty()) {
        generalizer.getVariable(Atom.of(pe.t0)).bound(Suite.parse(pe.t1));
        start++;
    }
    List<Pair<Reference, Node>> lnis = // 
    Read.from(// 
    List_.right(lines, start)).map(line -> {
        Pair<String, String> pt = String_.split2(line, "\t");
        String label = pt.t0;
        String command = pt.t1;
        Reference reference = String_.isNotBlank(label) ? generalizer.getVariable(Atom.of(label)) : null;
        Node instruction = generalizer.generalize(Suite.parse(command));
        return Pair.of(reference, instruction);
    }).toList();
    return assemble(generalizer, lnis);
}
Also used : Read(suite.streamlet.Read) SewingProverBuilder2(suite.lp.search.SewingProverBuilder2) Fun(suite.util.FunUtil.Fun) ArrayList(java.util.ArrayList) Node(suite.node.Node) CommentPreprocessor(suite.parser.CommentPreprocessor) String_(suite.util.String_) Preprocess(suite.text.Preprocess) Run(suite.text.Preprocess.Run) Binder(suite.lp.doer.Binder) RuleSet(suite.lp.kb.RuleSet) Generalizer(suite.lp.doer.Generalizer) Reference(suite.node.Reference) Suite(suite.Suite) Trail(suite.lp.Trail) Finder(suite.lp.search.ProverBuilder.Finder) Set(java.util.Set) TermOp(suite.node.io.TermOp) Bytes(suite.primitive.Bytes) BytesBuilder(suite.primitive.Bytes.BytesBuilder) List_(suite.util.List_) To(suite.util.To) Tree(suite.node.Tree) Pair(suite.adt.pair.Pair) List(java.util.List) Atom(suite.node.Atom) Int(suite.node.Int) Collections(java.util.Collections) Fail(suite.util.Fail) Reference(suite.node.Reference) Node(suite.node.Node) Generalizer(suite.lp.doer.Generalizer) ArrayList(java.util.ArrayList) List(java.util.List) Pair(suite.adt.pair.Pair)

Example 2 with Bytes

use of suite.primitive.Bytes in project suite by stupidsing.

the class Amd64Interpret method interpret.

public int interpret(List<Instruction> instructions, Bytes code, Bytes input) {
    mem.position(positionCode0);
    mem.put(code.bs);
    eip = positionCode0;
    regs[esp] = baseStackx - 16;
    IntIntMap labels = new IntIntMap();
    for (int i = 0; i < instructions.size(); i++) {
        int i_ = i;
        Instruction instruction = instructions.get(i_);
        if (instruction.insn == Insn.LABEL)
            labels.update((int) ((OpImm) instruction.op0).imm, i0 -> i_ + 1);
    }
    while (true) {
        Instruction instruction = instructions.get(eip++);
        if (Boolean.FALSE)
            LogUtil.info(state(instruction));
        try {
            Operand op0 = instruction.op0;
            Operand op1 = instruction.op1;
            int source0, source1;
            IntSink assign;
            if (op0 instanceof OpImm) {
                source0 = (int) ((OpImm) op0).imm;
                assign = null;
            } else if (op0 instanceof OpMem) {
                int index = index(address((OpMem) op0));
                source0 = mem.getInt(index);
                assign = i -> mem.putInt(index, i);
            } else if (op0 instanceof OpReg) {
                int reg = ((OpReg) op0).reg;
                source0 = regs[reg];
                assign = i -> {
                    regs[reg] = i;
                };
            } else {
                source0 = 0;
                assign = null;
            }
            if (op1 instanceof OpImm)
                source1 = (int) ((OpImm) op1).imm;
            else if (op1 instanceof OpMem)
                source1 = mem.getInt(index(address((OpMem) op1)));
            else if (op1 instanceof OpReg)
                source1 = regs[((OpReg) op1).reg];
            else
                source1 = 0;
            switch(instruction.insn) {
                case ADD:
                    assign.sink(source0 + source1);
                    break;
                case CALL:
                    push(eip);
                    eip = labels.get(source0);
                    break;
                case CMP:
                    c = Integer.compare(source0, source1);
                    break;
                case DEC:
                    assign.sink(source0 - 1);
                    break;
                case INC:
                    assign.sink(source0 + 1);
                    break;
                case INT:
                    if (source0 == -128)
                        if (regs[eax] == 1)
                            return regs[ebx];
                        else if (regs[eax] == 3) {
                            int length = min(regs[edx], input.size());
                            int di = index(regs[ecx]);
                            for (int i = 0; i < length; i++) mem.put(di++, input.get(i));
                            input = input.range(length);
                            regs[eax] = length;
                        } else if (regs[eax] == 4) {
                            int length = regs[edx];
                            int si = index(regs[ecx]);
                            byte[] bs = new byte[length];
                            for (int i = 0; i < length; i++) bs[i] = mem.get(si++);
                            output.sink(Bytes.of(bs));
                        } else
                            Fail.t();
                    else
                        Fail.t();
                    break;
                case JE:
                    if (c == 0)
                        eip = labels.get(source0);
                    break;
                case JMP:
                    eip = labels.get(source0);
                    break;
                case JG:
                    if (0 < c)
                        eip = labels.get(source0);
                    break;
                case JGE:
                    if (0 <= c)
                        eip = labels.get(source0);
                    break;
                case JL:
                    if (c < 0)
                        eip = labels.get(source0);
                    break;
                case JLE:
                    if (c <= 0)
                        eip = labels.get(source0);
                    break;
                case JNE:
                    if (c != 0)
                        eip = labels.get(source0);
                    break;
                case LABEL:
                    break;
                case LEA:
                    assign.sink(address((OpMem) op1));
                    break;
                case MOV:
                    assign.sink(source1);
                    break;
                case POP:
                    assign.sink(pop());
                    break;
                case PUSH:
                    push(source0);
                    break;
                case RET:
                    eip = pop();
                    break;
                case SUB:
                    assign.sink(source0 - source1);
                    break;
                case XOR:
                    assign.sink(source0 ^ source1);
                    break;
                default:
                    Fail.t();
            }
        } catch (Exception ex) {
            LogUtil.info(state(instruction));
            throw ex;
        }
    }
}
Also used : Insn(suite.assembler.Amd64.Insn) Friends.min(suite.util.Friends.min) LogUtil(suite.os.LogUtil) OpMem(suite.assembler.Amd64.OpMem) IntIntMap(suite.primitive.adt.map.IntIntMap) Bytes(suite.primitive.Bytes) BytesBuilder(suite.primitive.Bytes.BytesBuilder) Instruction(suite.assembler.Amd64.Instruction) OpReg(suite.assembler.Amd64.OpReg) To(suite.util.To) ByteBuffer(java.nio.ByteBuffer) Funp_(suite.funp.Funp_) OpImm(suite.assembler.Amd64.OpImm) List(java.util.List) IntSink(suite.primitive.IntPrimitives.IntSink) Operand(suite.assembler.Amd64.Operand) Sink(suite.util.FunUtil.Sink) Fail(suite.util.Fail) IntIntMap(suite.primitive.adt.map.IntIntMap) OpImm(suite.assembler.Amd64.OpImm) Operand(suite.assembler.Amd64.Operand) IntSink(suite.primitive.IntPrimitives.IntSink) OpMem(suite.assembler.Amd64.OpMem) Instruction(suite.assembler.Amd64.Instruction) OpReg(suite.assembler.Amd64.OpReg)

Example 3 with Bytes

use of suite.primitive.Bytes in project suite by stupidsing.

the class B_TreeBuilder method build.

private B_Tree<Key, Value> build(Comparator<Key> comparator, PageFile alf0, PageFile sbf0, PageFile pf0) {
    B_TreeImpl<Key, Value> b_tree = new B_TreeImpl<>(Object_.nullsFirst(comparator));
    Serializer<Bytes> als = serialize.bytes(pageSize);
    B_TreeSuperblockSerializer sbs = new B_TreeSuperblockSerializer(b_tree);
    Serializer<Bytes> pys = serialize.bytes(pageSize);
    B_TreePageSerializer ps = new B_TreePageSerializer(b_tree);
    SerializedPageFile<Bytes> alf = SerializedFileFactory.serialized(alf0, als);
    SerializedPageFile<B_TreeImpl<Key, Value>.Superblock> sbf = SerializedFileFactory.serialized(sbf0, sbs);
    SerializedPageFile<Bytes> pyf = SerializedFileFactory.serialized(pf0, pys);
    SerializedPageFile<B_TreeImpl<Key, Value>.Page> pf = SerializedFileFactory.serialized(pf0, ps);
    b_tree.setAllocator(new AllocatorImpl(alf));
    b_tree.setSuperblockPageFile(sbf);
    b_tree.setPayloadFile(pyf);
    b_tree.setPageFile(pf);
    b_tree.setBranchFactor(16);
    return b_tree;
}
Also used : Bytes(suite.primitive.Bytes) AllocatorImpl(suite.file.impl.AllocatorImpl)

Example 4 with Bytes

use of suite.primitive.Bytes in project suite by stupidsing.

the class JournalledFileFactory method journalled.

private static // 
JournalledPageFile journalled(// 
PageFile df, // 
PageFile jpf, // 
PageFile ppf, int pageSize) {
    Serializer<Bytes> bytesSerializer = serialize.bytes(pageSize);
    Serializer<JournalEntry> journalEntrySerializer = new Serializer<>() {

        public JournalEntry read(DataInput_ dataInput) throws IOException {
            int pointer = dataInput.readInt();
            Bytes bytes = bytesSerializer.read(dataInput);
            return new JournalEntry(pointer, bytes);
        }

        public void write(DataOutput_ dataOutput, JournalEntry journalEntry) throws IOException {
            dataOutput.writeInt(journalEntry.pointer);
            bytesSerializer.write(dataOutput, journalEntry.bytes);
        }
    };
    PageFile dataFile = df;
    SerializedPageFile<JournalEntry> journalPageFile = SerializedFileFactory.serialized(jpf, journalEntrySerializer);
    SerializedPageFile<Integer> pointerPageFile = SerializedFileFactory.serialized(ppf, serialize.int_);
    int nCommittedJournalEntries0 = pointerPageFile.load(0);
    List<JournalEntry> journalEntries = new ArrayList<>();
    for (int jp = 0; jp < nCommittedJournalEntries0; jp++) journalEntries.add(journalPageFile.load(jp));
    return new JournalledPageFile() {

        private int nCommittedJournalEntries = nCommittedJournalEntries0;

        public void close() throws IOException {
            dataFile.close();
            journalPageFile.close();
            pointerPageFile.close();
        }

        public synchronized Bytes load(int pointer) {
            IntObjPair<JournalEntry> pair = findPageInJournal(pointer);
            if (pair != null)
                return pair.t1.bytes;
            else
                return dataFile.load(pointer);
        }

        public synchronized void save(int pointer, Bytes bytes) {
            IntObjPair<JournalEntry> pair = findDirtyPageInJournal(pointer);
            int jp;
            JournalEntry journalEntry;
            if (pair != null) {
                jp = pair.t0;
                journalEntry = pair.t1;
            } else {
                jp = journalEntries.size();
                journalEntries.add(journalEntry = new JournalEntry(pointer, null));
            }
            journalEntry.bytes = bytes;
            journalPageFile.save(jp, journalEntry);
        }

        /**
         * Marks a snapshot that data can be recovered to.
         */
        public synchronized void commit() {
            while (nCommittedJournalEntries < journalEntries.size()) {
                JournalEntry journalEntry = journalEntries.get(nCommittedJournalEntries++);
                dataFile.save(journalEntry.pointer, journalEntry.bytes);
            }
            if (8 < nCommittedJournalEntries)
                saveJournal();
        }

        /**
         * Makes sure the current snapshot of data is saved and recoverable
         * on failure, upon the return of method call.
         */
        public synchronized void sync() {
            journalPageFile.sync();
            saveJournal();
            pointerPageFile.sync();
        }

        private void saveJournal() {
            pointerPageFile.save(0, nCommittedJournalEntries);
            if (128 < nCommittedJournalEntries)
                applyJournal();
        }

        /**
         * Shortens the journal by applying them to page file.
         */
        public synchronized void applyJournal() {
            // make sure all changes are written to main file
            dataFile.sync();
            // clear all committed entries
            journalEntries.subList(0, nCommittedJournalEntries).clear();
            // reset committed pointer
            pointerPageFile.save(0, nCommittedJournalEntries = 0);
            pointerPageFile.sync();
            // write back entries for next commit
            for (int jp = 0; jp < journalEntries.size(); jp++) journalPageFile.save(jp, journalEntries.get(jp));
        }

        private IntObjPair<JournalEntry> findPageInJournal(int pointer) {
            return findPageInJournal(pointer, 0);
        }

        private IntObjPair<JournalEntry> findDirtyPageInJournal(int pointer) {
            return findPageInJournal(pointer, nCommittedJournalEntries);
        }

        private IntObjPair<JournalEntry> findPageInJournal(int pointer, int start) {
            IntObjPair<JournalEntry> pair = null;
            for (int jp = start; jp < journalEntries.size(); jp++) {
                JournalEntry journalEntry = journalEntries.get(jp);
                if (journalEntry.pointer == pointer)
                    pair = IntObjPair.of(jp, journalEntry);
            }
            return pair;
        }
    };
}
Also used : PageFile(suite.file.PageFile) SerializedPageFile(suite.file.SerializedPageFile) JournalledPageFile(suite.file.JournalledPageFile) DataOutput_(suite.util.DataOutput_) ArrayList(java.util.ArrayList) JournalledPageFile(suite.file.JournalledPageFile) Bytes(suite.primitive.Bytes) DataInput_(suite.util.DataInput_) Serializer(suite.util.Serialize.Serializer)

Example 5 with Bytes

use of suite.primitive.Bytes in project suite by stupidsing.

the class FileSystemKeySet method increment.

private Bytes increment(Bytes bytes) {
    if (!bytes.isEmpty()) {
        Bytes bytes1 = bytes.range(0, -1);
        byte b1 = (byte) (bytes.get(-1) + 1);
        if (b1 != 0)
            return Bytes.concat(bytes1, Bytes.of(b1));
        else
            return Bytes.concat(increment(bytes1), Bytes.of((byte) 0));
    } else
        return bytes;
}
Also used : Bytes(suite.primitive.Bytes)

Aggregations

Bytes (suite.primitive.Bytes)53 Test (org.junit.Test)15 BytesBuilder (suite.primitive.Bytes.BytesBuilder)8 IOException (java.io.IOException)4 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 OutputStream (java.io.OutputStream)3 Pair (suite.adt.pair.Pair)3 DataInput_ (suite.util.DataInput_)3 To (suite.util.To)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Path (java.nio.file.Path)2 Amd64Interpret (suite.assembler.Amd64Interpret)2 Condition (suite.concurrent.Condition)2 Extent (suite.file.ExtentAllocator.Extent)2 ImperativeCompiler (suite.ip.ImperativeCompiler)2 DataOutput_ (suite.util.DataOutput_)2 Fail (suite.util.Fail)2 BufferedInputStream (java.io.BufferedInputStream)1